xref: /netbsd-src/sbin/modstat/main.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: main.c,v 1.11 2010/12/13 20:48:45 pooka 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.11 2010/12/13 20:48:45 pooka 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 
75 	name = NULL;
76 
77 	while ((ch = getopt(argc, argv, "n:")) != -1) {
78 		switch (ch) {
79 		case 'n':
80 			name = optarg;
81 			break;
82 		default:
83 			usage();
84 			/* NOTREACHED */
85 		}
86 	}
87 
88 	argc -= optind;
89 	argv += optind;
90 	if (argc != 0)
91 		usage();
92 
93 	if (prog_init && prog_init() == -1)
94 		err(1, "prog init failed");
95 
96 	for (len = 8192;;) {
97 		iov.iov_base = malloc(len);
98 		iov.iov_len = len;
99 		if (prog_modctl(MODCTL_STAT, &iov)) {
100 			err(EXIT_FAILURE, "modctl(MODCTL_STAT)");
101 		}
102 		if (len >= iov.iov_len) {
103 			break;
104 		}
105 		free(iov.iov_base);
106 		len = iov.iov_len;
107 	}
108 
109 	printf("%-16s %-10s %-10s %-5s %-8s %s\n",
110 	    "NAME", "CLASS", "SOURCE", "REFS", "SIZE", "REQUIRES");
111 	len = iov.iov_len / sizeof(modstat_t);
112 	qsort(iov.iov_base, len, sizeof(modstat_t), modstatcmp);
113 	for (ms = iov.iov_base; len != 0; ms++, len--) {
114 		const char *class;
115 		const char *source;
116 
117 		if (name != NULL && strcmp(ms->ms_name, name) != 0) {
118 			continue;
119 		}
120 		if (ms->ms_required[0] == '\0') {
121 			ms->ms_required[0] = '-';
122 			ms->ms_required[1] = '\0';
123 		}
124 		if (ms->ms_size == 0) {
125 			sbuf[0] = '-';
126 			sbuf[1] = '\0';
127 		} else {
128 			snprintf(sbuf, sizeof(sbuf), "%u", ms->ms_size);
129 		}
130 		if (ms->ms_class <= class_max)
131 			class = classes[ms->ms_class];
132 		else
133 			class = "UNKNOWN";
134 		if (ms->ms_source < source_max)
135 			source = sources[ms->ms_source];
136 		else
137 			source = "UNKNOWN";
138 
139 		printf("%-16s %-10s %-10s %-5d %-8s %s\n",
140 		    ms->ms_name, class, source, ms->ms_refcnt, sbuf,
141 		    ms->ms_required);
142 	}
143 
144 	exit(EXIT_SUCCESS);
145 }
146 
147 static void
148 usage(void)
149 {
150 
151 	(void)fprintf(stderr, "Usage: %s [-n name]\n", getprogname());
152 	exit(EXIT_FAILURE);
153 }
154 
155 static int
156 modstatcmp(const void *a, const void *b)
157 {
158 	const modstat_t *msa, *msb;
159 
160 	msa = a;
161 	msb = b;
162 
163 	return strcmp(msa->ms_name, msb->ms_name);
164 }
165