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