Merge commit 'b1e7e97d3b60469b243b3b2e22c7d8cbd11c7c90'
[unleashed.git] / usr / src / cmd / getdevpolicy / getdevpolicy.c
blobaba5a1a1e67d581df7d9d6d0420ca7e64c6f69c3
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
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>
42 static char *progname;
44 static const priv_impl_info_t *pi;
45 static size_t sz;
47 static void
48 fatalerr(const char *s)
50 (void) fprintf(stderr, "%s: %s: %s\n", progname, s, strerror(errno));
51 exit(1);
52 /* NOTREACHED */
55 static void
56 fatal(const char *s)
58 (void) fprintf(stderr, "%s: %s\n", progname, s);
59 exit(1);
60 /* NOTREACHED */
63 static void
64 printpolicy(const devplcysys_t *ds)
66 char *ss;
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);
76 static void
77 getpolicy(void)
79 int nitems = 0;
80 char *mem = NULL;
81 int i;
82 devplcysys_t *ds;
83 char major[256];
85 if (modctl(MODGETDEVPOLICY, &nitems, sz, mem) == 0 || errno != ENOMEM)
86 fatalerr("modctl(MODGETDEVPOLICY)");
88 mem = malloc(nitems * sz);
89 if (mem == NULL)
90 fatal("Out of memory");
92 if (modctl(MODGETDEVPOLICY, &nitems, sz, mem) != 0)
93 fatalerr("modctl");
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');
120 (void) putchar('\n');
121 printpolicy(ds);
125 static void
126 getdevpolicy(const char *dev)
128 devplcysys_t *ds;
130 ds = malloc(sz);
132 if (ds == NULL)
133 fatal("Out of memory");
135 if (modctl(MODGETDEVPOLICYBYNAME, sz, ds, dev) != 0)
136 fatalerr("modctl");
138 (void) printf("%s\n", dev);
139 printpolicy(ds);
140 free(ds);
144 main(int argc, char **argv)
146 progname = basename(argv[0]);
148 if ((pi = getprivimplinfo()) == NULL)
149 fatalerr("getprivimplinfo");
151 sz = DEVPLCYSYS_SZ(pi);
153 if (argc == 1) {
154 getpolicy();
155 return (0);
158 while (*++argv != NULL)
159 getdevpolicy(*argv);
161 return (0);