xref: /illumos-gate/usr/src/cmd/getdevpolicy/getdevpolicy.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <unistd.h>
30*7c478bd9Sstevel@tonic-gate #include <ctype.h>
31*7c478bd9Sstevel@tonic-gate #include <priv.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <libgen.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate static char *progname;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate static const priv_impl_info_t *pi;
43*7c478bd9Sstevel@tonic-gate static size_t sz;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static void
fatalerr(const char * s)46*7c478bd9Sstevel@tonic-gate fatalerr(const char *s)
47*7c478bd9Sstevel@tonic-gate {
48*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", progname, s, strerror(errno));
49*7c478bd9Sstevel@tonic-gate 	exit(1);
50*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
51*7c478bd9Sstevel@tonic-gate }
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static void
fatal(const char * s)54*7c478bd9Sstevel@tonic-gate fatal(const char *s)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s\n", progname, s);
57*7c478bd9Sstevel@tonic-gate 	exit(1);
58*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static void
printpolicy(const devplcysys_t * ds)62*7c478bd9Sstevel@tonic-gate printpolicy(const devplcysys_t *ds)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	char *ss;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	ss = priv_set_to_str(DEVPLCYSYS_RDP(ds, pi), ',', PRIV_STR_SHORT);
67*7c478bd9Sstevel@tonic-gate 	(void) printf("\t"DEVPLCY_TKN_RDP"=%s\n", ss);
68*7c478bd9Sstevel@tonic-gate 	free(ss);
69*7c478bd9Sstevel@tonic-gate 	ss = priv_set_to_str(DEVPLCYSYS_WRP(ds, pi), ',', PRIV_STR_SHORT);
70*7c478bd9Sstevel@tonic-gate 	(void) printf("\t"DEVPLCY_TKN_WRP"=%s\n", ss);
71*7c478bd9Sstevel@tonic-gate 	free(ss);
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate static void
getpolicy(void)75*7c478bd9Sstevel@tonic-gate getpolicy(void)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	int nitems = 0;
78*7c478bd9Sstevel@tonic-gate 	char *mem = NULL;
79*7c478bd9Sstevel@tonic-gate 	int i;
80*7c478bd9Sstevel@tonic-gate 	devplcysys_t *ds;
81*7c478bd9Sstevel@tonic-gate 	char major[256];
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	if (modctl(MODGETDEVPOLICY, &nitems, sz, mem) == 0 || errno != ENOMEM)
84*7c478bd9Sstevel@tonic-gate 		fatalerr("modctl(MODGETDEVPOLICY)");
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	mem = malloc(nitems * sz);
87*7c478bd9Sstevel@tonic-gate 	if (mem == NULL)
88*7c478bd9Sstevel@tonic-gate 		fatal("Out of memory");
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	if (modctl(MODGETDEVPOLICY, &nitems, sz, mem) != 0)
91*7c478bd9Sstevel@tonic-gate 		fatalerr("modctl");
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nitems; i++) {
94*7c478bd9Sstevel@tonic-gate 		/* LINTED: alignment */
95*7c478bd9Sstevel@tonic-gate 		ds = (devplcysys_t *)(mem + i * sz);
96*7c478bd9Sstevel@tonic-gate 		if (i == 0) {
97*7c478bd9Sstevel@tonic-gate 			(void) printf("DEFAULT");
98*7c478bd9Sstevel@tonic-gate 		} else {
99*7c478bd9Sstevel@tonic-gate 			if (modctl(MODGETNAME, major, sizeof (major),
100*7c478bd9Sstevel@tonic-gate 			    &ds->dps_maj) != 0)
101*7c478bd9Sstevel@tonic-gate 				continue;
102*7c478bd9Sstevel@tonic-gate 			(void) printf("%s:", major);
103*7c478bd9Sstevel@tonic-gate 			if (ds->dps_minornm[0] != '\0') {
104*7c478bd9Sstevel@tonic-gate 				(void) printf("%s", ds->dps_minornm);
105*7c478bd9Sstevel@tonic-gate 			} else {
106*7c478bd9Sstevel@tonic-gate 				/* (minor[-minor]) */
107*7c478bd9Sstevel@tonic-gate 				(void) printf("(%u", (uint_t)ds->dps_lomin);
108*7c478bd9Sstevel@tonic-gate 				if (ds->dps_lomin != ds->dps_himin)
109*7c478bd9Sstevel@tonic-gate 					(void) printf("-%u",
110*7c478bd9Sstevel@tonic-gate 						(uint_t)ds->dps_himin);
111*7c478bd9Sstevel@tonic-gate 				(void) putchar(')');
112*7c478bd9Sstevel@tonic-gate 				if (ds->dps_isblock)
113*7c478bd9Sstevel@tonic-gate 					(void) putchar('b');
114*7c478bd9Sstevel@tonic-gate 				else
115*7c478bd9Sstevel@tonic-gate 					(void) putchar('c');
116*7c478bd9Sstevel@tonic-gate 			}
117*7c478bd9Sstevel@tonic-gate 		}
118*7c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
119*7c478bd9Sstevel@tonic-gate 		printpolicy(ds);
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate static void
getdevpolicy(const char * dev)124*7c478bd9Sstevel@tonic-gate getdevpolicy(const char *dev)
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate 	devplcysys_t *ds;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	ds = malloc(sz);
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (ds == NULL)
131*7c478bd9Sstevel@tonic-gate 		fatal("Out of memory");
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if (modctl(MODGETDEVPOLICYBYNAME, sz, ds, dev) != 0)
134*7c478bd9Sstevel@tonic-gate 		fatalerr("modctl");
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	(void) printf("%s\n", dev);
137*7c478bd9Sstevel@tonic-gate 	printpolicy(ds);
138*7c478bd9Sstevel@tonic-gate 	free(ds);
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)142*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
143*7c478bd9Sstevel@tonic-gate {
144*7c478bd9Sstevel@tonic-gate 	progname = basename(argv[0]);
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if ((pi = getprivimplinfo()) == NULL)
147*7c478bd9Sstevel@tonic-gate 		fatalerr("getprivimplinfo");
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	sz = DEVPLCYSYS_SZ(pi);
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	if (argc == 1) {
152*7c478bd9Sstevel@tonic-gate 		getpolicy();
153*7c478bd9Sstevel@tonic-gate 		return (0);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	while (*++argv != NULL)
157*7c478bd9Sstevel@tonic-gate 		getdevpolicy(*argv);
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	return (0);
160*7c478bd9Sstevel@tonic-gate }
161