main.c revision 1.21 1 /* $NetBSD: main.c,v 1.21 2015/12/02 00:56:09 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: main.c,v 1.21 2015/12/02 00:56:09 pgoyette Exp $");
32 #endif /* !lint */
33
34 #include <sys/module.h>
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37
38 #include <err.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <stdbool.h>
45
46 #include "prog_ops.h"
47
48 int main(int, char **);
49 static void usage(void) __dead;
50 static int modstatcmp(const void *, const void *);
51
52 static const char *classes[] = {
53 "any",
54 "misc",
55 "vfs",
56 "driver",
57 "exec",
58 "secmodel"
59 };
60 const unsigned int class_max = __arraycount(classes);
61
62 static const char *sources[] = {
63 "builtin",
64 "boot",
65 "filesys",
66 };
67 const unsigned int source_max = __arraycount(sources);
68
69 static const char *modflags[] = {
70 "-", "f", "a", "af"
71 };
72
73 int
74 main(int argc, char **argv)
75 {
76 struct iovec iov;
77 modstat_t *ms;
78 size_t len;
79 const char *name;
80 char sbuf[32];
81 int ch, rc, modauto = 1;
82 size_t maxnamelen = 16, i, modautolen;
83 char loadable = '\0';
84 bool address = false;
85
86 name = NULL;
87
88 while ((ch = getopt(argc, argv, "Aaekn:")) != -1) {
89 switch (ch) {
90 case 'A': /* FALLTHROUGH */
91 case 'a': /* FALLTHROUGH */
92 case 'e':
93 loadable = (char)ch;
94 break;
95 case 'k':
96 address = true;
97 break;
98 case 'n':
99 name = optarg;
100 break;
101 default:
102 usage();
103 /* NOTREACHED */
104 }
105 }
106
107 argc -= optind;
108 argv += optind;
109 if (argc == 1 && name == NULL)
110 name = argv[0];
111 else if (argc != 0)
112 usage();
113
114 if (prog_init && prog_init() == -1)
115 err(1, "prog init failed");
116
117 if (loadable == 'A' || loadable == 'a') {
118 if (prog_modctl(MODCTL_EXISTS, (void *)(uintptr_t)1)) {
119 switch (errno) {
120 case ENOSYS:
121 errx(EXIT_FAILURE, "The kernel was compiled "
122 "without options MODULAR.");
123 break;
124 case EPERM:
125 errx(EXIT_FAILURE, "Modules can not be "
126 "autoloaded right now.");
127 break;
128 default:
129 err(EXIT_FAILURE, "modctl_exists for autoload");
130 break;
131 }
132 } else {
133 if (loadable == 'A') {
134 modautolen = sizeof(modauto);
135 rc = sysctlbyname("kern.module.autoload",
136 &modauto, &modautolen, NULL, 0);
137 if (rc != 0) {
138 err(EXIT_FAILURE, "sysctl "
139 "kern.module.autoload failed.");
140 }
141 }
142 errx(EXIT_SUCCESS, "Modules can be autoloaded%s.",
143 modauto ? "" : ", but kern.module.autoload = 0");
144 }
145 }
146
147 if (loadable == 'e') {
148 if (prog_modctl(MODCTL_EXISTS, (void *)(uintptr_t)0)) {
149 switch (errno) {
150 case ENOSYS:
151 errx(EXIT_FAILURE, "The kernel was compiled "
152 "without options MODULAR.");
153 break;
154 case EPERM:
155 errx(EXIT_FAILURE, "You are not allowed to "
156 "load modules right now.");
157 break;
158 default:
159 err(EXIT_FAILURE, "modctl_exists for autoload");
160 break;
161 }
162 } else {
163 errx(EXIT_SUCCESS, "You can load modules.");
164 }
165 }
166
167 for (len = 8192;;) {
168 iov.iov_base = malloc(len);
169 iov.iov_len = len;
170 if (prog_modctl(MODCTL_STAT, &iov)) {
171 err(EXIT_FAILURE, "modctl(MODCTL_STAT)");
172 }
173 if (len >= iov.iov_len) {
174 break;
175 }
176 free(iov.iov_base);
177 len = iov.iov_len;
178 }
179
180 len = iov.iov_len / sizeof(modstat_t);
181 qsort(iov.iov_base, len, sizeof(modstat_t), modstatcmp);
182 for (i = 0, ms = iov.iov_base; i < len; i++, ms++) {
183 size_t namelen = strlen(ms->ms_name);
184 if (maxnamelen < namelen)
185 maxnamelen = namelen;
186 }
187 printf("%-*s %-8s %-8s %-4s %-5s ",
188 (int)maxnamelen, "NAME", "CLASS", "SOURCE", "FLAG", "REFS");
189 if (address)
190 printf("%-16s ", "ADDRESS");
191 printf("%-7s %s \n", "SIZE", "REQUIRES");
192 for (ms = iov.iov_base; len != 0; ms++, len--) {
193 const char *class;
194 const char *source;
195
196 if (name != NULL && strcmp(ms->ms_name, name) != 0) {
197 continue;
198 }
199 if (ms->ms_required[0] == '\0') {
200 ms->ms_required[0] = '-';
201 ms->ms_required[1] = '\0';
202 }
203 if (ms->ms_size == 0) {
204 sbuf[0] = '-';
205 sbuf[1] = '\0';
206 } else {
207 snprintf(sbuf, sizeof(sbuf), "%u", ms->ms_size);
208 }
209 if (ms->ms_class <= class_max)
210 class = classes[ms->ms_class];
211 else
212 class = "UNKNOWN";
213 if (ms->ms_source < source_max)
214 source = sources[ms->ms_source];
215 else
216 source = "UNKNOWN";
217
218 printf("%-*s %-8s %-8s %-4s %-5d ",
219 (int)maxnamelen, ms->ms_name, class, source,
220 modflags[ms->ms_flags & (__arraycount(modflags) - 1)],
221 ms->ms_refcnt);
222 if (address)
223 printf("%-16" PRIx64 " ", ms->ms_addr);
224 printf("%-7s %s\n", sbuf, ms->ms_required);
225 }
226
227 exit(EXIT_SUCCESS);
228 }
229
230 static void
231 usage(void)
232 {
233
234 (void)fprintf(stderr, "Usage: %s [-Aaen] [name]\n", getprogname());
235 exit(EXIT_FAILURE);
236 }
237
238 static int
239 modstatcmp(const void *a, const void *b)
240 {
241 const modstat_t *msa, *msb;
242
243 msa = a;
244 msb = b;
245
246 return strcmp(msa->ms_name, msb->ms_name);
247 }
248