xref: /netbsd-src/sbin/modstat/main.c (revision 4fee23f98c45552038ad6b5bd05124a41302fb01)
1 /*	$NetBSD: main.c,v 1.13 2011/06/03 16:35:35 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.13 2011/06/03 16:35:35 pgoyette 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 	    (int)maxnamelen, "NAME", "CLASS", "SOURCE", "REFS", "SIZE",
119 	    "REQUIRES");
120 	for (ms = iov.iov_base; len != 0; ms++, len--) {
121 		const char *class;
122 		const char *source;
123 
124 		if (name != NULL && strcmp(ms->ms_name, name) != 0) {
125 			continue;
126 		}
127 		if (ms->ms_required[0] == '\0') {
128 			ms->ms_required[0] = '-';
129 			ms->ms_required[1] = '\0';
130 		}
131 		if (ms->ms_size == 0) {
132 			sbuf[0] = '-';
133 			sbuf[1] = '\0';
134 		} else {
135 			snprintf(sbuf, sizeof(sbuf), "%u", ms->ms_size);
136 		}
137 		if (ms->ms_class <= class_max)
138 			class = classes[ms->ms_class];
139 		else
140 			class = "UNKNOWN";
141 		if (ms->ms_source < source_max)
142 			source = sources[ms->ms_source];
143 		else
144 			source = "UNKNOWN";
145 
146 		printf("%-*s %-10s %-10s %-5d %-8s %s\n",
147 		    (int)maxnamelen, ms->ms_name, class, source, ms->ms_refcnt,
148 		    sbuf, ms->ms_required);
149 	}
150 
151 	exit(EXIT_SUCCESS);
152 }
153 
154 static void
155 usage(void)
156 {
157 
158 	(void)fprintf(stderr, "Usage: %s [-n name]\n", getprogname());
159 	exit(EXIT_FAILURE);
160 }
161 
162 static int
163 modstatcmp(const void *a, const void *b)
164 {
165 	const modstat_t *msa, *msb;
166 
167 	msa = a;
168 	msb = b;
169 
170 	return strcmp(msa->ms_name, msb->ms_name);
171 }
172