xref: /titanic_50/usr/src/cmd/getdevpolicy/getdevpolicy.c (revision e07d9cb85217949d497b02d7211de8a197d2f2eb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <priv.h>
34 #include <string.h>
35 #include <libgen.h>
36 #include <errno.h>
37 #include <sys/devpolicy.h>
38 #include <sys/modctl.h>
39 #include <sys/vnode.h>
40 #include <sys/stat.h>
41 
42 static char *progname;
43 
44 static const priv_impl_info_t *pi;
45 static size_t sz;
46 
47 static void
48 fatalerr(const char *s)
49 {
50 	(void) fprintf(stderr, "%s: %s: %s\n", progname, s, strerror(errno));
51 	exit(1);
52 	/* NOTREACHED */
53 }
54 
55 static void
56 fatal(const char *s)
57 {
58 	(void) fprintf(stderr, "%s: %s\n", progname, s);
59 	exit(1);
60 	/* NOTREACHED */
61 }
62 
63 static void
64 printpolicy(const devplcysys_t *ds)
65 {
66 	char *ss;
67 
68 	ss = priv_set_to_str(DEVPLCYSYS_RDP(ds, pi), ',', PRIV_STR_SHORT);
69 	(void) printf("\t"DEVPLCY_TKN_RDP"=%s\n", ss);
70 	free(ss);
71 	ss = priv_set_to_str(DEVPLCYSYS_WRP(ds, pi), ',', PRIV_STR_SHORT);
72 	(void) printf("\t"DEVPLCY_TKN_WRP"=%s\n", ss);
73 	free(ss);
74 }
75 
76 static void
77 getpolicy(void)
78 {
79 	int nitems = 0;
80 	char *mem = NULL;
81 	int i;
82 	devplcysys_t *ds;
83 	char major[256];
84 
85 	if (modctl(MODGETDEVPOLICY, &nitems, sz, mem) == 0 || errno != ENOMEM)
86 		fatalerr("modctl(MODGETDEVPOLICY)");
87 
88 	mem = malloc(nitems * sz);
89 	if (mem == NULL)
90 		fatal("Out of memory");
91 
92 	if (modctl(MODGETDEVPOLICY, &nitems, sz, mem) != 0)
93 		fatalerr("modctl");
94 
95 	for (i = 0; i < nitems; i++) {
96 		/* LINTED: alignment */
97 		ds = (devplcysys_t *)(mem + i * sz);
98 		if (i == 0) {
99 			(void) printf("DEFAULT");
100 		} else {
101 			if (modctl(MODGETNAME, major, sizeof (major),
102 			    &ds->dps_maj) != 0)
103 				continue;
104 			(void) printf("%s:", major);
105 			if (ds->dps_minornm[0] != '\0') {
106 				(void) printf("%s", ds->dps_minornm);
107 			} else {
108 				/* (minor[-minor]) */
109 				(void) printf("(%u", (uint_t)ds->dps_lomin);
110 				if (ds->dps_lomin != ds->dps_himin)
111 					(void) printf("-%u",
112 						(uint_t)ds->dps_himin);
113 				(void) putchar(')');
114 				if (ds->dps_isblock)
115 					(void) putchar('b');
116 				else
117 					(void) putchar('c');
118 			}
119 		}
120 		(void) putchar('\n');
121 		printpolicy(ds);
122 	}
123 }
124 
125 static void
126 getdevpolicy(const char *dev)
127 {
128 	devplcysys_t *ds;
129 
130 	ds = malloc(sz);
131 
132 	if (ds == NULL)
133 		fatal("Out of memory");
134 
135 	if (modctl(MODGETDEVPOLICYBYNAME, sz, ds, dev) != 0)
136 		fatalerr("modctl");
137 
138 	(void) printf("%s\n", dev);
139 	printpolicy(ds);
140 	free(ds);
141 }
142 
143 int
144 main(int argc, char **argv)
145 {
146 	progname = basename(argv[0]);
147 
148 	if ((pi = getprivimplinfo()) == NULL)
149 		fatalerr("getprivimplinfo");
150 
151 	sz = DEVPLCYSYS_SZ(pi);
152 
153 	if (argc == 1) {
154 		getpolicy();
155 		return (0);
156 	}
157 
158 	while (*++argv != NULL)
159 		getdevpolicy(*argv);
160 
161 	return (0);
162 }
163