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