Home | History | Annotate | Line # | Download | only in modstat
main.c revision 1.20
      1  1.20  christos /*	$NetBSD: main.c,v 1.20 2015/11/04 16:04:55 christos Exp $	*/
      2   1.1        ad 
      3   1.1        ad /*-
      4   1.1        ad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5   1.1        ad  * All rights reserved.
      6   1.1        ad  *
      7   1.1        ad  * Redistribution and use in source and binary forms, with or without
      8   1.1        ad  * modification, are permitted provided that the following conditions
      9   1.1        ad  * are met:
     10   1.1        ad  * 1. Redistributions of source code must retain the above copyright
     11   1.1        ad  *    notice, this list of conditions and the following disclaimer.
     12   1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     14   1.1        ad  *    documentation and/or other materials provided with the distribution.
     15   1.1        ad  *
     16   1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1        ad  */
     28   1.1        ad 
     29   1.1        ad #include <sys/cdefs.h>
     30   1.1        ad #ifndef lint
     31  1.20  christos __RCSID("$NetBSD: main.c,v 1.20 2015/11/04 16:04:55 christos Exp $");
     32   1.1        ad #endif /* !lint */
     33   1.1        ad 
     34   1.1        ad #include <sys/module.h>
     35  1.15   jnemeth #include <sys/param.h>
     36  1.15   jnemeth #include <sys/sysctl.h>
     37   1.1        ad 
     38  1.15   jnemeth #include <err.h>
     39  1.15   jnemeth #include <errno.h>
     40   1.1        ad #include <stdio.h>
     41   1.1        ad #include <stdlib.h>
     42   1.1        ad #include <string.h>
     43   1.1        ad #include <unistd.h>
     44   1.1        ad 
     45  1.11     pooka #include "prog_ops.h"
     46  1.11     pooka 
     47   1.1        ad int	main(int, char **);
     48   1.1        ad static void	usage(void) __dead;
     49   1.6        ad static int	modstatcmp(const void *, const void *);
     50   1.1        ad 
     51   1.1        ad static const char *classes[] = {
     52   1.1        ad 	"any",
     53   1.1        ad 	"misc",
     54   1.1        ad 	"vfs",
     55   1.1        ad 	"driver",
     56   1.1        ad 	"exec",
     57  1.18   mbalmer 	"secmodel"
     58   1.1        ad };
     59  1.10     pooka const unsigned int class_max = __arraycount(classes);
     60   1.1        ad 
     61   1.1        ad static const char *sources[] = {
     62   1.5        ad 	"builtin",
     63   1.1        ad 	"boot",
     64   1.1        ad 	"filesys",
     65   1.1        ad };
     66  1.10     pooka const unsigned int source_max = __arraycount(sources);
     67   1.1        ad 
     68  1.20  christos static const char *modflags[] = {
     69  1.20  christos 	"-", "f", "a", "af"
     70  1.20  christos };
     71  1.20  christos 
     72   1.1        ad int
     73   1.1        ad main(int argc, char **argv)
     74   1.1        ad {
     75   1.1        ad 	struct iovec iov;
     76   1.1        ad 	modstat_t *ms;
     77   1.1        ad 	size_t len;
     78   1.1        ad 	const char *name;
     79   1.1        ad 	char sbuf[32];
     80  1.15   jnemeth 	int ch, rc, modauto = 1;
     81  1.15   jnemeth 	size_t maxnamelen = 16, i, modautolen;
     82  1.15   jnemeth 	char loadable = '\0';
     83   1.1        ad 
     84   1.1        ad 	name = NULL;
     85   1.1        ad 
     86  1.15   jnemeth 	while ((ch = getopt(argc, argv, "Aaen:")) != -1) {
     87   1.1        ad 		switch (ch) {
     88  1.15   jnemeth 		case 'A':			/* FALLTHROUGH */
     89  1.15   jnemeth 		case 'a':			/* FALLTHROUGH */
     90  1.15   jnemeth 		case 'e':
     91  1.15   jnemeth 			loadable = (char)ch;
     92  1.15   jnemeth 			break;
     93   1.1        ad 		case 'n':
     94   1.1        ad 			name = optarg;
     95   1.1        ad 			break;
     96   1.1        ad 		default:
     97   1.1        ad 			usage();
     98   1.1        ad 			/* NOTREACHED */
     99   1.1        ad 		}
    100   1.1        ad 	}
    101   1.1        ad 
    102   1.1        ad 	argc -= optind;
    103   1.1        ad 	argv += optind;
    104  1.14   mbalmer 	if (argc == 1 && name == NULL)
    105  1.14   mbalmer 		name = argv[0];
    106  1.14   mbalmer 	else if (argc != 0)
    107   1.1        ad 		usage();
    108   1.1        ad 
    109  1.11     pooka 	if (prog_init && prog_init() == -1)
    110  1.11     pooka 		err(1, "prog init failed");
    111  1.11     pooka 
    112  1.15   jnemeth 	if (loadable == 'A' || loadable == 'a') {
    113  1.15   jnemeth 		if (prog_modctl(MODCTL_EXISTS, (void *)(uintptr_t)1)) {
    114  1.15   jnemeth 			switch (errno) {
    115  1.15   jnemeth 			case ENOSYS:
    116  1.15   jnemeth 				errx(EXIT_FAILURE, "The kernel was compiled "
    117  1.15   jnemeth 				    "without options MODULAR.");
    118  1.15   jnemeth 				break;
    119  1.15   jnemeth 			case EPERM:
    120  1.15   jnemeth 				errx(EXIT_FAILURE, "Modules can not be "
    121  1.15   jnemeth 				    "autoloaded right now.");
    122  1.15   jnemeth 				break;
    123  1.15   jnemeth 			default:
    124  1.15   jnemeth 				err(EXIT_FAILURE, "modctl_exists for autoload");
    125  1.15   jnemeth 				break;
    126  1.15   jnemeth 			}
    127  1.15   jnemeth 		} else {
    128  1.15   jnemeth 			if (loadable == 'A') {
    129  1.15   jnemeth 				modautolen = sizeof(modauto);
    130  1.15   jnemeth 				rc = sysctlbyname("kern.module.autoload",
    131  1.15   jnemeth 				    &modauto, &modautolen, NULL, 0);
    132  1.15   jnemeth 				if (rc != 0) {
    133  1.15   jnemeth 					err(EXIT_FAILURE, "sysctl "
    134  1.15   jnemeth 					    "kern.module.autoload failed.");
    135  1.15   jnemeth 				}
    136  1.15   jnemeth 			}
    137  1.15   jnemeth 			errx(EXIT_SUCCESS, "Modules can be autoloaded%s.",
    138  1.15   jnemeth 			modauto ? "" : ", but kern.module.autoload = 0");
    139  1.15   jnemeth 		}
    140  1.15   jnemeth 	}
    141  1.15   jnemeth 
    142  1.15   jnemeth 	if (loadable == 'e') {
    143  1.15   jnemeth 		if (prog_modctl(MODCTL_EXISTS, (void *)(uintptr_t)0)) {
    144  1.15   jnemeth 			switch (errno) {
    145  1.15   jnemeth 			case ENOSYS:
    146  1.15   jnemeth 				errx(EXIT_FAILURE, "The kernel was compiled "
    147  1.15   jnemeth 				    "without options MODULAR.");
    148  1.15   jnemeth 				break;
    149  1.15   jnemeth 			case EPERM:
    150  1.15   jnemeth 				errx(EXIT_FAILURE, "You are not allowed to "
    151  1.15   jnemeth 				    "load modules right now.");
    152  1.15   jnemeth 				break;
    153  1.15   jnemeth 			default:
    154  1.15   jnemeth 				err(EXIT_FAILURE, "modctl_exists for autoload");
    155  1.15   jnemeth 				break;
    156  1.15   jnemeth 			}
    157  1.15   jnemeth 		} else {
    158  1.15   jnemeth 			errx(EXIT_SUCCESS, "You can load modules.");
    159  1.15   jnemeth 		}
    160  1.15   jnemeth 	}
    161  1.15   jnemeth 
    162   1.9     pooka 	for (len = 8192;;) {
    163   1.1        ad 		iov.iov_base = malloc(len);
    164   1.1        ad 		iov.iov_len = len;
    165  1.11     pooka 		if (prog_modctl(MODCTL_STAT, &iov)) {
    166   1.1        ad 			err(EXIT_FAILURE, "modctl(MODCTL_STAT)");
    167   1.1        ad 		}
    168   1.1        ad 		if (len >= iov.iov_len) {
    169   1.1        ad 			break;
    170   1.1        ad 		}
    171   1.1        ad 		free(iov.iov_base);
    172   1.2        ad 		len = iov.iov_len;
    173   1.1        ad 	}
    174   1.1        ad 
    175   1.1        ad 	len = iov.iov_len / sizeof(modstat_t);
    176   1.6        ad 	qsort(iov.iov_base, len, sizeof(modstat_t), modstatcmp);
    177  1.12    nonaka 	for (i = 0, ms = iov.iov_base; i < len; i++, ms++) {
    178  1.12    nonaka 		size_t namelen = strlen(ms->ms_name);
    179  1.12    nonaka 		if (maxnamelen < namelen)
    180  1.12    nonaka 			maxnamelen = namelen;
    181  1.12    nonaka 	}
    182  1.20  christos 	printf("%-*s %-8s %-8s %-4s %-5s %-16s %-7s %s \n",
    183  1.20  christos 	    (int)maxnamelen, "NAME", "CLASS", "SOURCE", "FLAG", "REFS",
    184  1.19  pgoyette 	    "ADDRESS", "SIZE", "REQUIRES");
    185   1.1        ad 	for (ms = iov.iov_base; len != 0; ms++, len--) {
    186  1.10     pooka 		const char *class;
    187  1.10     pooka 		const char *source;
    188  1.10     pooka 
    189   1.1        ad 		if (name != NULL && strcmp(ms->ms_name, name) != 0) {
    190   1.1        ad 			continue;
    191   1.1        ad 		}
    192   1.1        ad 		if (ms->ms_required[0] == '\0') {
    193   1.1        ad 			ms->ms_required[0] = '-';
    194   1.1        ad 			ms->ms_required[1] = '\0';
    195   1.1        ad 		}
    196   1.1        ad 		if (ms->ms_size == 0) {
    197   1.1        ad 			sbuf[0] = '-';
    198   1.1        ad 			sbuf[1] = '\0';
    199   1.1        ad 		} else {
    200   1.1        ad 			snprintf(sbuf, sizeof(sbuf), "%u", ms->ms_size);
    201   1.1        ad 		}
    202  1.10     pooka 		if (ms->ms_class <= class_max)
    203  1.10     pooka 			class = classes[ms->ms_class];
    204  1.10     pooka 		else
    205  1.10     pooka 			class = "UNKNOWN";
    206  1.10     pooka 		if (ms->ms_source < source_max)
    207  1.10     pooka 			source = sources[ms->ms_source];
    208  1.10     pooka 		else
    209  1.10     pooka 			source = "UNKNOWN";
    210  1.10     pooka 
    211  1.20  christos 		printf("%-*s %-8s %-8s %-4s %-5d %-16" PRIx64 " %-7s %s\n",
    212  1.20  christos 		    (int)maxnamelen, ms->ms_name, class, source,
    213  1.20  christos 		    modflags[ms->ms_flags & (__arraycount(modflags) - 1)],
    214  1.19  pgoyette 		    ms->ms_refcnt, ms->ms_addr, sbuf, ms->ms_required);
    215   1.1        ad 	}
    216   1.1        ad 
    217   1.1        ad 	exit(EXIT_SUCCESS);
    218   1.1        ad }
    219   1.1        ad 
    220   1.1        ad static void
    221   1.1        ad usage(void)
    222   1.1        ad {
    223   1.1        ad 
    224  1.15   jnemeth 	(void)fprintf(stderr, "Usage: %s [-Aaen] [name]\n", getprogname());
    225   1.1        ad 	exit(EXIT_FAILURE);
    226   1.1        ad }
    227   1.6        ad 
    228   1.6        ad static int
    229   1.6        ad modstatcmp(const void *a, const void *b)
    230   1.6        ad {
    231   1.6        ad 	const modstat_t *msa, *msb;
    232   1.6        ad 
    233   1.6        ad 	msa = a;
    234   1.6        ad 	msb = b;
    235   1.6        ad 
    236   1.6        ad 	return strcmp(msa->ms_name, msb->ms_name);
    237   1.6        ad }
    238