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