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