main.c revision 1.2.8.2 1 1.2.8.2 matt /* $NetBSD: main.c,v 1.2.8.2 2008/03/23 00:44:24 matt Exp $ */
2 1.2.8.2 matt
3 1.2.8.2 matt /*-
4 1.2.8.2 matt * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.2.8.2 matt * All rights reserved.
6 1.2.8.2 matt *
7 1.2.8.2 matt * Redistribution and use in source and binary forms, with or without
8 1.2.8.2 matt * modification, are permitted provided that the following conditions
9 1.2.8.2 matt * are met:
10 1.2.8.2 matt * 1. Redistributions of source code must retain the above copyright
11 1.2.8.2 matt * notice, this list of conditions and the following disclaimer.
12 1.2.8.2 matt * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.8.2 matt * notice, this list of conditions and the following disclaimer in the
14 1.2.8.2 matt * documentation and/or other materials provided with the distribution.
15 1.2.8.2 matt * 3. All advertising materials mentioning features or use of this software
16 1.2.8.2 matt * must display the following acknowledgement:
17 1.2.8.2 matt * This product includes software developed by the NetBSD
18 1.2.8.2 matt * Foundation, Inc. and its contributors.
19 1.2.8.2 matt * 4. Neither the name of The NetBSD Foundation nor the names of its
20 1.2.8.2 matt * contributors may be used to endorse or promote products derived
21 1.2.8.2 matt * from this software without specific prior written permission.
22 1.2.8.2 matt *
23 1.2.8.2 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.2.8.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.2.8.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.2.8.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.2.8.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.2.8.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.2.8.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.2.8.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.2.8.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.2.8.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.2.8.2 matt * POSSIBILITY OF SUCH DAMAGE.
34 1.2.8.2 matt */
35 1.2.8.2 matt
36 1.2.8.2 matt #include <sys/cdefs.h>
37 1.2.8.2 matt #ifndef lint
38 1.2.8.2 matt __RCSID("$NetBSD: main.c,v 1.2.8.2 2008/03/23 00:44:24 matt Exp $");
39 1.2.8.2 matt #endif /* !lint */
40 1.2.8.2 matt
41 1.2.8.2 matt #include <sys/module.h>
42 1.2.8.2 matt
43 1.2.8.2 matt #include <stdio.h>
44 1.2.8.2 matt #include <stdlib.h>
45 1.2.8.2 matt #include <string.h>
46 1.2.8.2 matt #include <unistd.h>
47 1.2.8.2 matt #include <err.h>
48 1.2.8.2 matt
49 1.2.8.2 matt int main(int, char **);
50 1.2.8.2 matt static void usage(void) __dead;
51 1.2.8.2 matt
52 1.2.8.2 matt static const char *classes[] = {
53 1.2.8.2 matt "any",
54 1.2.8.2 matt "misc",
55 1.2.8.2 matt "vfs",
56 1.2.8.2 matt "driver",
57 1.2.8.2 matt "exec",
58 1.2.8.2 matt };
59 1.2.8.2 matt
60 1.2.8.2 matt static const char *sources[] = {
61 1.2.8.2 matt "kernel",
62 1.2.8.2 matt "boot",
63 1.2.8.2 matt "filesys",
64 1.2.8.2 matt };
65 1.2.8.2 matt
66 1.2.8.2 matt int
67 1.2.8.2 matt main(int argc, char **argv)
68 1.2.8.2 matt {
69 1.2.8.2 matt struct iovec iov;
70 1.2.8.2 matt modstat_t *ms;
71 1.2.8.2 matt size_t len;
72 1.2.8.2 matt const char *name;
73 1.2.8.2 matt char sbuf[32];
74 1.2.8.2 matt int ch;
75 1.2.8.2 matt
76 1.2.8.2 matt name = NULL;
77 1.2.8.2 matt
78 1.2.8.2 matt while ((ch = getopt(argc, argv, "n:")) != -1) {
79 1.2.8.2 matt switch (ch) {
80 1.2.8.2 matt case 'n':
81 1.2.8.2 matt name = optarg;
82 1.2.8.2 matt break;
83 1.2.8.2 matt default:
84 1.2.8.2 matt usage();
85 1.2.8.2 matt /* NOTREACHED */
86 1.2.8.2 matt }
87 1.2.8.2 matt }
88 1.2.8.2 matt
89 1.2.8.2 matt argc -= optind;
90 1.2.8.2 matt argv += optind;
91 1.2.8.2 matt if (argc != 0)
92 1.2.8.2 matt usage();
93 1.2.8.2 matt
94 1.2.8.2 matt for (len = 4096;;) {
95 1.2.8.2 matt iov.iov_base = malloc(len);
96 1.2.8.2 matt iov.iov_len = len;
97 1.2.8.2 matt if (modctl(MODCTL_STAT, &iov)) {
98 1.2.8.2 matt err(EXIT_FAILURE, "modctl(MODCTL_STAT)");
99 1.2.8.2 matt }
100 1.2.8.2 matt if (len >= iov.iov_len) {
101 1.2.8.2 matt break;
102 1.2.8.2 matt }
103 1.2.8.2 matt free(iov.iov_base);
104 1.2.8.2 matt len = iov.iov_len;
105 1.2.8.2 matt }
106 1.2.8.2 matt
107 1.2.8.2 matt printf("NAME\t\tCLASS\tSOURCE\tREFS\tSIZE\tREQUIRES\n");
108 1.2.8.2 matt len = iov.iov_len / sizeof(modstat_t);
109 1.2.8.2 matt for (ms = iov.iov_base; len != 0; ms++, len--) {
110 1.2.8.2 matt if (name != NULL && strcmp(ms->ms_name, name) != 0) {
111 1.2.8.2 matt continue;
112 1.2.8.2 matt }
113 1.2.8.2 matt if (ms->ms_required[0] == '\0') {
114 1.2.8.2 matt ms->ms_required[0] = '-';
115 1.2.8.2 matt ms->ms_required[1] = '\0';
116 1.2.8.2 matt }
117 1.2.8.2 matt if (ms->ms_size == 0) {
118 1.2.8.2 matt sbuf[0] = '-';
119 1.2.8.2 matt sbuf[1] = '\0';
120 1.2.8.2 matt } else {
121 1.2.8.2 matt snprintf(sbuf, sizeof(sbuf), "%u", ms->ms_size);
122 1.2.8.2 matt }
123 1.2.8.2 matt printf("%-16s%s\t%s\t%d\t%s\t%s\n",
124 1.2.8.2 matt ms->ms_name, classes[ms->ms_class], sources[ms->ms_source],
125 1.2.8.2 matt ms->ms_refcnt, sbuf, ms->ms_required);
126 1.2.8.2 matt }
127 1.2.8.2 matt
128 1.2.8.2 matt exit(EXIT_SUCCESS);
129 1.2.8.2 matt }
130 1.2.8.2 matt
131 1.2.8.2 matt static void
132 1.2.8.2 matt usage(void)
133 1.2.8.2 matt {
134 1.2.8.2 matt
135 1.2.8.2 matt (void)fprintf(stderr, "Usage: %s [-n name]", getprogname());
136 1.2.8.2 matt exit(EXIT_FAILURE);
137 1.2.8.2 matt }
138