main.c revision 1.17 1 /* $NetBSD: main.c,v 1.17 2013/10/21 17:14:06 mbalmer 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.17 2013/10/21 17:14:06 mbalmer 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
45 #include "prog_ops.h"
46
47 int main(int, char **);
48 static void usage(void) __dead;
49 static int modstatcmp(const void *, const void *);
50
51 static const char *classes[] = {
52 "any",
53 "misc",
54 "vfs",
55 "driver",
56 "exec",
57 "secmodel",
58 "lua"
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 int
70 main(int argc, char **argv)
71 {
72 struct iovec iov;
73 modstat_t *ms;
74 size_t len;
75 const char *name;
76 char sbuf[32];
77 int ch, rc, modauto = 1;
78 size_t maxnamelen = 16, i, modautolen;
79 char loadable = '\0';
80
81 name = NULL;
82
83 while ((ch = getopt(argc, argv, "Aaen:")) != -1) {
84 switch (ch) {
85 case 'A': /* FALLTHROUGH */
86 case 'a': /* FALLTHROUGH */
87 case 'e':
88 loadable = (char)ch;
89 break;
90 case 'n':
91 name = optarg;
92 break;
93 default:
94 usage();
95 /* NOTREACHED */
96 }
97 }
98
99 argc -= optind;
100 argv += optind;
101 if (argc == 1 && name == NULL)
102 name = argv[0];
103 else if (argc != 0)
104 usage();
105
106 if (prog_init && prog_init() == -1)
107 err(1, "prog init failed");
108
109 if (loadable == 'A' || loadable == 'a') {
110 if (prog_modctl(MODCTL_EXISTS, (void *)(uintptr_t)1)) {
111 switch (errno) {
112 case ENOSYS:
113 errx(EXIT_FAILURE, "The kernel was compiled "
114 "without options MODULAR.");
115 break;
116 case EPERM:
117 errx(EXIT_FAILURE, "Modules can not be "
118 "autoloaded right now.");
119 break;
120 default:
121 err(EXIT_FAILURE, "modctl_exists for autoload");
122 break;
123 }
124 } else {
125 if (loadable == 'A') {
126 modautolen = sizeof(modauto);
127 rc = sysctlbyname("kern.module.autoload",
128 &modauto, &modautolen, NULL, 0);
129 if (rc != 0) {
130 err(EXIT_FAILURE, "sysctl "
131 "kern.module.autoload failed.");
132 }
133 }
134 errx(EXIT_SUCCESS, "Modules can be autoloaded%s.",
135 modauto ? "" : ", but kern.module.autoload = 0");
136 }
137 }
138
139 if (loadable == 'e') {
140 if (prog_modctl(MODCTL_EXISTS, (void *)(uintptr_t)0)) {
141 switch (errno) {
142 case ENOSYS:
143 errx(EXIT_FAILURE, "The kernel was compiled "
144 "without options MODULAR.");
145 break;
146 case EPERM:
147 errx(EXIT_FAILURE, "You are not allowed to "
148 "load modules right now.");
149 break;
150 default:
151 err(EXIT_FAILURE, "modctl_exists for autoload");
152 break;
153 }
154 } else {
155 errx(EXIT_SUCCESS, "You can load modules.");
156 }
157 }
158
159 for (len = 8192;;) {
160 iov.iov_base = malloc(len);
161 iov.iov_len = len;
162 if (prog_modctl(MODCTL_STAT, &iov)) {
163 err(EXIT_FAILURE, "modctl(MODCTL_STAT)");
164 }
165 if (len >= iov.iov_len) {
166 break;
167 }
168 free(iov.iov_base);
169 len = iov.iov_len;
170 }
171
172 len = iov.iov_len / sizeof(modstat_t);
173 qsort(iov.iov_base, len, sizeof(modstat_t), modstatcmp);
174 for (i = 0, ms = iov.iov_base; i < len; i++, ms++) {
175 size_t namelen = strlen(ms->ms_name);
176 if (maxnamelen < namelen)
177 maxnamelen = namelen;
178 }
179 printf("%-*s %-10s %-10s %-5s %-16s %-8s %s \n",
180 (int)maxnamelen, "NAME", "CLASS", "SOURCE", "REFS", "ADDRESS",
181 "SIZE", "REQUIRES");
182 for (ms = iov.iov_base; len != 0; ms++, len--) {
183 const char *class;
184 const char *source;
185
186 if (name != NULL && strcmp(ms->ms_name, name) != 0) {
187 continue;
188 }
189 if (ms->ms_required[0] == '\0') {
190 ms->ms_required[0] = '-';
191 ms->ms_required[1] = '\0';
192 }
193 if (ms->ms_size == 0) {
194 sbuf[0] = '-';
195 sbuf[1] = '\0';
196 } else {
197 snprintf(sbuf, sizeof(sbuf), "%u", ms->ms_size);
198 }
199 if (ms->ms_class <= class_max)
200 class = classes[ms->ms_class];
201 else
202 class = "UNKNOWN";
203 if (ms->ms_source < source_max)
204 source = sources[ms->ms_source];
205 else
206 source = "UNKNOWN";
207
208 printf("%-*s %-10s %-10s %-5d %-16" PRIx64 " %-8s %s\n",
209 (int)maxnamelen, ms->ms_name, class, source, ms->ms_refcnt,
210 ms->ms_addr, sbuf, ms->ms_required);
211 }
212
213 exit(EXIT_SUCCESS);
214 }
215
216 static void
217 usage(void)
218 {
219
220 (void)fprintf(stderr, "Usage: %s [-Aaen] [name]\n", getprogname());
221 exit(EXIT_FAILURE);
222 }
223
224 static int
225 modstatcmp(const void *a, const void *b)
226 {
227 const modstat_t *msa, *msb;
228
229 msa = a;
230 msb = b;
231
232 return strcmp(msa->ms_name, msb->ms_name);
233 }
234