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