xref: /titanic_50/usr/src/lib/libadm/common/devattr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2 */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate /*
36*7c478bd9Sstevel@tonic-gate  *  devattr.c
37*7c478bd9Sstevel@tonic-gate  *
38*7c478bd9Sstevel@tonic-gate  *  Contents:
39*7c478bd9Sstevel@tonic-gate  *	devattr()	Get the value of a attribute for a specific device
40*7c478bd9Sstevel@tonic-gate  */
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  *  Header files needed
44*7c478bd9Sstevel@tonic-gate  *	<sys/types.h>		System Data Types
45*7c478bd9Sstevel@tonic-gate  *	<stdio.h>		Standard I/O Definitions
46*7c478bd9Sstevel@tonic-gate  *	<errno.h>		Error-value definitions
47*7c478bd9Sstevel@tonic-gate  *	<string.h>		String function and constant definitions
48*7c478bd9Sstevel@tonic-gate  *	<devmgmt.h>		Device table definitions available to the world
49*7c478bd9Sstevel@tonic-gate  *	"devtab.h"		Local device table definitions
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
53*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
54*7c478bd9Sstevel@tonic-gate #include	<errno.h>
55*7c478bd9Sstevel@tonic-gate #include	<string.h>
56*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
57*7c478bd9Sstevel@tonic-gate #include	<devmgmt.h>
58*7c478bd9Sstevel@tonic-gate #include	"devtab.h"
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate /*
61*7c478bd9Sstevel@tonic-gate  *  Local constant definitions
62*7c478bd9Sstevel@tonic-gate  */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate /*
66*7c478bd9Sstevel@tonic-gate  *  Local static data
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate /*
70*7c478bd9Sstevel@tonic-gate  *  char *devattr(device, attr)
71*7c478bd9Sstevel@tonic-gate  *
72*7c478bd9Sstevel@tonic-gate  *	This function searches the device table, looking for the device
73*7c478bd9Sstevel@tonic-gate  *	specified by <device>.  If it finds a record corresponding to that
74*7c478bd9Sstevel@tonic-gate  *	device (see below for a definition of that correspondence), it
75*7c478bd9Sstevel@tonic-gate  *	extracts the value of the field <attr> from that record, if any.
76*7c478bd9Sstevel@tonic-gate  *	It returns a pointer to that value, or (char *) NULL if none.
77*7c478bd9Sstevel@tonic-gate  *
78*7c478bd9Sstevel@tonic-gate  *  Arguments:
79*7c478bd9Sstevel@tonic-gate  *	device		Pointer to the character-string that describes the
80*7c478bd9Sstevel@tonic-gate  *			device whose record is to be looked for
81*7c478bd9Sstevel@tonic-gate  *	attr		The device's attribute to be looked for
82*7c478bd9Sstevel@tonic-gate  *
83*7c478bd9Sstevel@tonic-gate  *  Returns:  char *
84*7c478bd9Sstevel@tonic-gate  *	A pointer to the character-string containing the value of the
85*7c478bd9Sstevel@tonic-gate  *	attribute <attr> for the device <device>, or (char *) NULL if none
86*7c478bd9Sstevel@tonic-gate  *	was found.  If the function returns (char *) NULL and the error was
87*7c478bd9Sstevel@tonic-gate  *	detected by this function, it sets "errno" to indicate the problem.
88*7c478bd9Sstevel@tonic-gate  *
89*7c478bd9Sstevel@tonic-gate  *  "errno" Values:
90*7c478bd9Sstevel@tonic-gate  *	EPERM		Permissions deny reading access of the device-table
91*7c478bd9Sstevel@tonic-gate  *			file
92*7c478bd9Sstevel@tonic-gate  *	ENOENT		The specified device-table file could not be found
93*7c478bd9Sstevel@tonic-gate  *	ENODEV		Device not found in the device table
94*7c478bd9Sstevel@tonic-gate  *	EINVAL		The device does not have that attribute defined
95*7c478bd9Sstevel@tonic-gate  *	ENOMEM		No memory available
96*7c478bd9Sstevel@tonic-gate  */
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate char *
devattr(char * device,char * attribute)99*7c478bd9Sstevel@tonic-gate devattr(
100*7c478bd9Sstevel@tonic-gate 	char   *device,		/* The device ) we're to look for */
101*7c478bd9Sstevel@tonic-gate 	char   *attribute)	/* The attribute to extract */
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	/* Automatic data */
104*7c478bd9Sstevel@tonic-gate 	struct devtabent	*record;	/* Retrieved record */
105*7c478bd9Sstevel@tonic-gate 	struct attrval		*p;		/* attr/val records */
106*7c478bd9Sstevel@tonic-gate 	char			*val;		/* Extracted value */
107*7c478bd9Sstevel@tonic-gate 	char			*rtnval;	/* Value to return */
108*7c478bd9Sstevel@tonic-gate 	int			found;		/* TRUE if attribute found */
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	/* Get the record for the specified device */
112*7c478bd9Sstevel@tonic-gate 	if (!(record = _getdevrec(device))) {
113*7c478bd9Sstevel@tonic-gate 		_enddevtab();
114*7c478bd9Sstevel@tonic-gate 		return (NULL);
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	/* Search the record for the specified attribute */
118*7c478bd9Sstevel@tonic-gate 	found = FALSE;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	/* Did they ask for the device alias? */
121*7c478bd9Sstevel@tonic-gate 	if (strcmp(attribute, DTAB_ALIAS) == 0) {
122*7c478bd9Sstevel@tonic-gate 	    val = (record->alias != NULL) ? record->alias : "";
123*7c478bd9Sstevel@tonic-gate 	    found = TRUE;
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	/* Did they ask for the character-special device? */
127*7c478bd9Sstevel@tonic-gate 	else if (strcmp(attribute, DTAB_CDEVICE) == 0) {
128*7c478bd9Sstevel@tonic-gate 	    val = (record->cdevice != NULL) ? record->cdevice : "";
129*7c478bd9Sstevel@tonic-gate 	    found = TRUE;
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	/* Did they ask for the block-special device? */
133*7c478bd9Sstevel@tonic-gate 	else if (strcmp(attribute, DTAB_BDEVICE) == 0) {
134*7c478bd9Sstevel@tonic-gate 	    val = (record->bdevice != NULL) ? record->bdevice : "";
135*7c478bd9Sstevel@tonic-gate 	    found = TRUE;
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	/* Did they ask for the pathname? */
139*7c478bd9Sstevel@tonic-gate 	else if (strcmp(attribute, DTAB_PATHNAME) == 0) {
140*7c478bd9Sstevel@tonic-gate 	    val = (record->pathname != NULL) ? record->pathname : "";
141*7c478bd9Sstevel@tonic-gate 	    found = TRUE;
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	else {
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	/*
147*7c478bd9Sstevel@tonic-gate 	 * Didn't ask for one of the easy ones, search the attr/val
148*7c478bd9Sstevel@tonic-gate 	 * structure
149*7c478bd9Sstevel@tonic-gate 	 */
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	    p = record->attrlist;
152*7c478bd9Sstevel@tonic-gate 	    while (!found && (p)) {
153*7c478bd9Sstevel@tonic-gate 		if (strcmp(p->attr, attribute) == 0) {
154*7c478bd9Sstevel@tonic-gate 		    val = p->val;
155*7c478bd9Sstevel@tonic-gate 		    found = TRUE;
156*7c478bd9Sstevel@tonic-gate 		} else p = p->next;
157*7c478bd9Sstevel@tonic-gate 	    }
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	/*
161*7c478bd9Sstevel@tonic-gate 	 * If the attribute was found, copy it into malloc()ed space.
162*7c478bd9Sstevel@tonic-gate 	 * If not, set errno appropriately; we'll return NULL
163*7c478bd9Sstevel@tonic-gate 	 */
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	if (found) {
166*7c478bd9Sstevel@tonic-gate 	    if (rtnval = malloc(strlen(val)+1))
167*7c478bd9Sstevel@tonic-gate 		(void) strcpy(rtnval, val);
168*7c478bd9Sstevel@tonic-gate 	    else errno = ENOMEM;
169*7c478bd9Sstevel@tonic-gate 	} else {
170*7c478bd9Sstevel@tonic-gate 	    rtnval = NULL;
171*7c478bd9Sstevel@tonic-gate 	    errno = EINVAL;
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	/* Free the space allocated to the struct devtabent structure */
175*7c478bd9Sstevel@tonic-gate 	_freedevtabent(record);
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	_enddevtab();
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	/* Fini */
180*7c478bd9Sstevel@tonic-gate 	return (rtnval);
181*7c478bd9Sstevel@tonic-gate }
182