remove pyzfs
[unleashed.git] / usr / src / lib / libdevinfo / devinfo_dli.c
bloba0120027eebd8cedced22363bc759009d39880cf
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Interfaces for getting device configuration data from kernel
31 * through the devinfo driver.
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <stdarg.h>
43 #include <unistd.h>
44 #include <libgen.h>
45 #include "libdevinfo.h"
47 /* Function Prototypes */
48 static int di_dli_open(char *, int, short, int);
50 #define DLI_NAME 0x1
53 * Private hotplug interfaces to be used between cfgadm pci plugin and
54 * devfsadm link generator.
59 * returns a devlink info file name derived from <path>
60 * callers need to free the returned string
62 char *
63 di_dli_name(char *path)
65 #define dliroot "/etc/devices/dli/info."
66 #define dliroot_len (sizeof (dliroot) - 1)
68 char *dlipath;
69 int dlipathsz;
70 char *basep;
72 basep = basename(path);
73 dlipathsz = strlen(basep) + dliroot_len + 1;
74 dlipath = malloc(sizeof (char) * dlipathsz);
76 (void) snprintf(dlipath, dlipathsz, "%s%s", dliroot, basep);
77 dlipath[dlipathsz - 1] = '\0';
78 return (dlipath);
80 #undef dlipre
81 #undef dlipre_len
82 #undef dliroot
83 #undef dliroot_len
87 static int
88 di_dli_open(char *path, int oflag, short l_type, int flags)
90 int fd;
91 char *dlipath, *dlipath_dir, *dlipath_dup;
92 struct stat statbuf;
93 int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
94 flock_t lock;
96 dlipath = (flags & DLI_NAME) ? di_dli_name(path) : (char *)path;
97 dlipath_dup = strdup(dlipath);
98 dlipath_dir = dirname(dlipath_dup);
100 if (stat(dlipath_dir, &statbuf) < 0) {
101 if (mkdirp(dlipath_dir,
102 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
103 fd = -1;
104 goto OUT;
108 fd = open(dlipath, oflag, mode);
109 if (fd < 0)
110 goto OUT;
112 if (fchmod(fd, mode) < 0) {
113 (void) close(fd);
114 fd = -1;
115 goto OUT;
118 bzero(&lock, sizeof (lock));
119 lock.l_type = l_type;
120 if (fcntl(fd, F_SETLKW, &lock) < 0) {
121 (void) close(fd);
122 fd = -1;
124 OUT:
125 free(dlipath_dup);
126 if (flags & DLI_NAME)
127 free(dlipath);
128 return (fd);
133 di_dli_openr(char *path)
135 return (di_dli_open(path, O_RDONLY, F_RDLCK, DLI_NAME));
140 di_dli_openw(char *path)
142 return (di_dli_open(path, O_RDWR | O_SYNC | O_TRUNC | O_CREAT,
143 F_WRLCK, DLI_NAME));
147 void
148 di_dli_close(int fd)
150 flock_t lock;
151 if (fd < 0)
152 return;
154 bzero(&lock, sizeof (lock));
155 lock.l_type = F_UNLCK;
156 (void) fcntl(fd, F_SETLK, &lock);
157 (void) close(fd);