main.c revision 1.12 1 /* $NetBSD: main.c,v 1.12 2011/06/03 15:34:46 nonaka 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.12 2011/06/03 15:34:46 nonaka Exp $");
32 #endif /* !lint */
33
34 #include <sys/module.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <err.h>
41
42 #include "prog_ops.h"
43
44 int main(int, char **);
45 static void usage(void) __dead;
46 static int modstatcmp(const void *, const void *);
47
48 static const char *classes[] = {
49 "any",
50 "misc",
51 "vfs",
52 "driver",
53 "exec",
54 "secmodel",
55 };
56 const unsigned int class_max = __arraycount(classes);
57
58 static const char *sources[] = {
59 "builtin",
60 "boot",
61 "filesys",
62 };
63 const unsigned int source_max = __arraycount(sources);
64
65 int
66 main(int argc, char **argv)
67 {
68 struct iovec iov;
69 modstat_t *ms;
70 size_t len;
71 const char *name;
72 char sbuf[32];
73 int ch;
74 size_t maxnamelen = 16, i;
75
76 name = NULL;
77
78 while ((ch = getopt(argc, argv, "n:")) != -1) {
79 switch (ch) {
80 case 'n':
81 name = optarg;
82 break;
83 default:
84 usage();
85 /* NOTREACHED */
86 }
87 }
88
89 argc -= optind;
90 argv += optind;
91 if (argc != 0)
92 usage();
93
94 if (prog_init && prog_init() == -1)
95 err(1, "prog init failed");
96
97 for (len = 8192;;) {
98 iov.iov_base = malloc(len);
99 iov.iov_len = len;
100 if (prog_modctl(MODCTL_STAT, &iov)) {
101 err(EXIT_FAILURE, "modctl(MODCTL_STAT)");
102 }
103 if (len >= iov.iov_len) {
104 break;
105 }
106 free(iov.iov_base);
107 len = iov.iov_len;
108 }
109
110 len = iov.iov_len / sizeof(modstat_t);
111 qsort(iov.iov_base, len, sizeof(modstat_t), modstatcmp);
112 for (i = 0, ms = iov.iov_base; i < len; i++, ms++) {
113 size_t namelen = strlen(ms->ms_name);
114 if (maxnamelen < namelen)
115 maxnamelen = namelen;
116 }
117 printf("%-*s %-10s %-10s %-5s %-8s %s\n",
118 maxnamelen, "NAME", "CLASS", "SOURCE", "REFS", "SIZE", "REQUIRES");
119 for (ms = iov.iov_base; len != 0; ms++, len--) {
120 const char *class;
121 const char *source;
122
123 if (name != NULL && strcmp(ms->ms_name, name) != 0) {
124 continue;
125 }
126 if (ms->ms_required[0] == '\0') {
127 ms->ms_required[0] = '-';
128 ms->ms_required[1] = '\0';
129 }
130 if (ms->ms_size == 0) {
131 sbuf[0] = '-';
132 sbuf[1] = '\0';
133 } else {
134 snprintf(sbuf, sizeof(sbuf), "%u", ms->ms_size);
135 }
136 if (ms->ms_class <= class_max)
137 class = classes[ms->ms_class];
138 else
139 class = "UNKNOWN";
140 if (ms->ms_source < source_max)
141 source = sources[ms->ms_source];
142 else
143 source = "UNKNOWN";
144
145 printf("%-*s %-10s %-10s %-5d %-8s %s\n",
146 maxnamelen, ms->ms_name, class, source, ms->ms_refcnt, sbuf,
147 ms->ms_required);
148 }
149
150 exit(EXIT_SUCCESS);
151 }
152
153 static void
154 usage(void)
155 {
156
157 (void)fprintf(stderr, "Usage: %s [-n name]\n", getprogname());
158 exit(EXIT_FAILURE);
159 }
160
161 static int
162 modstatcmp(const void *a, const void *b)
163 {
164 const modstat_t *msa, *msb;
165
166 msa = a;
167 msb = b;
168
169 return strcmp(msa->ms_name, msb->ms_name);
170 }
171