xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_ctf_open.c (revision 2becb8cdf9cd77e1a76224f216be15d6ae3b9ec9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*2becb8cdSRobert Mustacchi  * Copyright (c) 2015, Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * libctf open/close interposition layer
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * The mdb flavor of the interposition layer serves only to make ctf_bufopen
327c478bd9Sstevel@tonic-gate  * calls easier.  The kmdb flavor (the real reason for the layer) has more
337c478bd9Sstevel@tonic-gate  * intelligence behind mdb_ctf_open() than does this one.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <mdb/mdb_ctf.h>
377c478bd9Sstevel@tonic-gate #include <libctf.h>
38*2becb8cdSRobert Mustacchi #include <sys/types.h>
39*2becb8cdSRobert Mustacchi #include <sys/stat.h>
40*2becb8cdSRobert Mustacchi #include <fcntl.h>
41*2becb8cdSRobert Mustacchi #include <errno.h>
42*2becb8cdSRobert Mustacchi #include <unistd.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate ctf_file_t *
457c478bd9Sstevel@tonic-gate mdb_ctf_open(const char *filename, int *errp)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	return (ctf_open(filename, errp));
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate void
517c478bd9Sstevel@tonic-gate mdb_ctf_close(ctf_file_t *fp)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	ctf_close(fp);
547c478bd9Sstevel@tonic-gate }
55*2becb8cdSRobert Mustacchi 
56*2becb8cdSRobert Mustacchi int
57*2becb8cdSRobert Mustacchi mdb_ctf_write(const char *filename, ctf_file_t *fp)
58*2becb8cdSRobert Mustacchi {
59*2becb8cdSRobert Mustacchi 	int fd, ret;
60*2becb8cdSRobert Mustacchi 
61*2becb8cdSRobert Mustacchi 	if ((fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644)) < 0)
62*2becb8cdSRobert Mustacchi 		return (errno);
63*2becb8cdSRobert Mustacchi 
64*2becb8cdSRobert Mustacchi 	if (ctf_write(fp, fd) == CTF_ERR) {
65*2becb8cdSRobert Mustacchi 		(void) close(fd);
66*2becb8cdSRobert Mustacchi 		return (CTF_ERR);
67*2becb8cdSRobert Mustacchi 	}
68*2becb8cdSRobert Mustacchi 
69*2becb8cdSRobert Mustacchi 	ret = close(fd);
70*2becb8cdSRobert Mustacchi 	if (ret != 0)
71*2becb8cdSRobert Mustacchi 		ret = errno;
72*2becb8cdSRobert Mustacchi 	return (ret);
73*2becb8cdSRobert Mustacchi }
74