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