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