xref: /onnv-gate/usr/src/lib/libbsm/common/getdevicerange.c (revision 1676:37f4a3e2bd99)
1*1676Sjpk /*
2*1676Sjpk  * CDDL HEADER START
3*1676Sjpk  *
4*1676Sjpk  * The contents of this file are subject to the terms of the
5*1676Sjpk  * Common Development and Distribution License (the "License").
6*1676Sjpk  * You may not use this file except in compliance with the License.
7*1676Sjpk  *
8*1676Sjpk  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1676Sjpk  * or http://www.opensolaris.org/os/licensing.
10*1676Sjpk  * See the License for the specific language governing permissions
11*1676Sjpk  * and limitations under the License.
12*1676Sjpk  *
13*1676Sjpk  * When distributing Covered Code, include this CDDL HEADER in each
14*1676Sjpk  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1676Sjpk  * If applicable, add the following below this CDDL HEADER, with the
16*1676Sjpk  * fields enclosed by brackets "[]" replaced with your own identifying
17*1676Sjpk  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1676Sjpk  *
19*1676Sjpk  * CDDL HEADER END
20*1676Sjpk  */
21*1676Sjpk 
22*1676Sjpk /*
23*1676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*1676Sjpk  * Use is subject to license terms.
25*1676Sjpk  */
26*1676Sjpk 
27*1676Sjpk #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*1676Sjpk 
29*1676Sjpk #include <stdlib.h>
30*1676Sjpk #include <errno.h>
31*1676Sjpk #include <tsol/label.h>
32*1676Sjpk #include <bsm/devices.h>
33*1676Sjpk 
34*1676Sjpk 
35*1676Sjpk /*
36*1676Sjpk  * getdevicerange
37*1676Sjpk  *	Gets the minimum and maximum labels within which the device can
38*1676Sjpk  *	be used. If label range is not specified for the device in
39*1676Sjpk  *	device_allocate, defaults to admin_low and admin_high.
40*1676Sjpk  *	Returns malloc'ed blrange pointer, or NULL on any error.
41*1676Sjpk  */
42*1676Sjpk blrange_t *
getdevicerange(const char * dev)43*1676Sjpk getdevicerange(const char *dev)
44*1676Sjpk {
45*1676Sjpk 	int		err;
46*1676Sjpk 	char		*lstr;
47*1676Sjpk 	devalloc_t	*da;
48*1676Sjpk 	devmap_t	*dm;
49*1676Sjpk 	blrange_t	*range;
50*1676Sjpk 
51*1676Sjpk 	errno = 0;
52*1676Sjpk 	if ((range = malloc(sizeof (blrange_t))) == NULL)
53*1676Sjpk 		return (NULL);
54*1676Sjpk 	if ((range->lower_bound = blabel_alloc()) == NULL) {
55*1676Sjpk 		free(range);
56*1676Sjpk 		return (NULL);
57*1676Sjpk 	}
58*1676Sjpk 	if ((range->upper_bound = blabel_alloc()) == NULL) {
59*1676Sjpk 		blabel_free(range->lower_bound);
60*1676Sjpk 		free(range);
61*1676Sjpk 		return (NULL);
62*1676Sjpk 	}
63*1676Sjpk 
64*1676Sjpk 	/*
65*1676Sjpk 	 * If an entry is found for the named device,
66*1676Sjpk 	 * return its label range.
67*1676Sjpk 	 */
68*1676Sjpk 	setdaent();
69*1676Sjpk 	if ((da = getdanam((char *)dev)) == NULL) {
70*1676Sjpk 		setdmapent();
71*1676Sjpk 		/* check for an actual device file */
72*1676Sjpk 		if ((dm = getdmapdev((char *)dev)) != NULL) {
73*1676Sjpk 			da = getdanam(dm->dmap_devname);
74*1676Sjpk 			freedmapent(dm);
75*1676Sjpk 		}
76*1676Sjpk 		enddmapent();
77*1676Sjpk 	}
78*1676Sjpk 	enddaent();
79*1676Sjpk 	if (da == NULL) {
80*1676Sjpk 		bsllow(range->lower_bound);
81*1676Sjpk 		bslhigh(range->upper_bound);
82*1676Sjpk 	} else {
83*1676Sjpk 		lstr = kva_match(da->da_devopts, DAOPT_MINLABEL);
84*1676Sjpk 		if (lstr == NULL) {
85*1676Sjpk 			bsllow(range->lower_bound);
86*1676Sjpk 		} else if (stobsl(lstr, range->lower_bound, NO_CORRECTION,
87*1676Sjpk 		    &err) == 0) {
88*1676Sjpk 			blabel_free(range->lower_bound);
89*1676Sjpk 			blabel_free(range->upper_bound);
90*1676Sjpk 			free(range);
91*1676Sjpk 			errno = ENOTSUP;
92*1676Sjpk 			return (NULL);
93*1676Sjpk 		}
94*1676Sjpk 		lstr = kva_match(da->da_devopts, DAOPT_MAXLABEL);
95*1676Sjpk 		if (lstr == NULL) {
96*1676Sjpk 			bslhigh(range->upper_bound);
97*1676Sjpk 		} else if (stobsl(lstr, range->upper_bound, NO_CORRECTION,
98*1676Sjpk 		    &err) == 0) {
99*1676Sjpk 			blabel_free(range->lower_bound);
100*1676Sjpk 			blabel_free(range->upper_bound);
101*1676Sjpk 			free(range);
102*1676Sjpk 			errno = ENOTSUP;
103*1676Sjpk 			return (NULL);
104*1676Sjpk 		}
105*1676Sjpk 		freedaent(da);
106*1676Sjpk 	}
107*1676Sjpk 
108*1676Sjpk 	return (range);
109*1676Sjpk }
110