modprobe.c (15731B)
1 2 #if FEATURE_LINUX_SYSCALL 3 #ifndef _GNU_SOURCE 4 #define _GNU_SOURCE 5 #endif 6 #endif 7 #include "arg.h" 8 #include "fs.h" 9 #include "paths.h" 10 #include "util.h" 11 12 #include <ctype.h> 13 #include <dirent.h> 14 #include <errno.h> 15 #include <fcntl.h> 16 #include <fnmatch.h> 17 #include <stdarg.h> 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include <sys/stat.h> 22 #include <sys/syscall.h> 23 #include <sys/types.h> 24 #include <sys/utsname.h> 25 #include <unistd.h> 26 27 #if FEATURE_MODPROBE_SYSLOG 28 #include <syslog.h> 29 #endif 30 31 #define HASH_SIZE 256 32 #ifndef LINE_MAX 33 #define LINE_MAX 4096 34 #endif 35 36 enum ModFlags { 37 MOD_LOADED = 1 << 0, 38 MOD_BLACKLISTED = 1 << 1, 39 MOD_QUEUED = 1 << 2, 40 MOD_SEEN_DEP = 1 << 3, 41 }; 42 43 struct StrNode { 44 char *str; 45 struct StrNode *next; 46 }; 47 48 struct Module { 49 char *name; 50 char *path; 51 char *options; 52 struct StrNode *deps; 53 struct StrNode *aliases; 54 int flags; 55 struct Module *next; 56 }; 57 58 static struct Module *mod_db[HASH_SIZE]; 59 static struct StrNode *probes = NULL; 60 static struct StrNode *moddirs = NULL; 61 static char *cmdopts = NULL; 62 63 static int aflag = 0; 64 static int rflag = 0; 65 static int qflag = 0; 66 static int vflag = 0; 67 static int lflag = 0; 68 69 #if FEATURE_MODPROBE_SHOW_DEPENDS 70 static int Dflag = 0; 71 #endif 72 73 #if FEATURE_MODPROBE_BLACKLIST 74 static int bflag = 0; 75 #endif 76 77 #if FEATURE_MODPROBE_SYSLOG 78 static int sflag = 0; 79 #endif 80 81 /* logging wrappers: handles syslog delegation and quiet mode suppression */ 82 83 static void 84 pr_warn(const char *fmt, ...) 85 { 86 va_list ap; 87 int saved_errno = errno; 88 #if FEATURE_MODPROBE_SYSLOG 89 char buf[1024]; 90 #endif 91 92 if (qflag) 93 return; 94 95 va_start(ap, fmt); 96 #if FEATURE_MODPROBE_SYSLOG 97 if (sflag) { 98 vsnprintf(buf, sizeof(buf), fmt, ap); 99 if (fmt[0] && fmt[strlen(fmt) - 1] == ':') 100 syslog(LOG_ERR, "%s %s", buf, strerror(saved_errno)); 101 else 102 syslog(LOG_ERR, "%s", buf); 103 va_end(ap); 104 return; 105 } 106 #endif 107 fprintf(stderr, "%s: ", argv0); 108 vfprintf(stderr, fmt, ap); 109 if (fmt[0] && fmt[strlen(fmt) - 1] == ':') 110 fprintf(stderr, " %s\n", strerror(saved_errno)); 111 else 112 fputc('\n', stderr); 113 va_end(ap); 114 } 115 116 static void 117 pr_info(const char *fmt, ...) 118 { 119 va_list ap; 120 121 if (qflag) 122 return; 123 124 va_start(ap, fmt); 125 #if FEATURE_MODPROBE_SYSLOG 126 if (sflag) { 127 vsyslog(LOG_INFO, fmt, ap); 128 va_end(ap); 129 return; 130 } 131 #endif 132 vprintf(fmt, ap); 133 putchar('\n'); 134 va_end(ap); 135 } 136 137 /* string structures: singly linked lists used for directories, deps, and 138 * aliases */ 139 140 static void 141 strlist_append(struct StrNode **list, const char *str) 142 { 143 struct StrNode *n, *tail; 144 145 n = ecalloc(1, sizeof(*n)); 146 n->str = estrdup(str); 147 148 if (!*list) { 149 *list = n; 150 return; 151 } 152 for (tail = *list; tail->next; tail = tail->next) 153 ; 154 tail->next = n; 155 } 156 157 static char * 158 append_opts(char *opts, const char *add) 159 { 160 size_t olen, alen; 161 char *newopts; 162 163 if (!add) 164 return opts; 165 if (!opts) 166 return estrdup(add); 167 168 olen = strlen(opts); 169 alen = strlen(add); 170 newopts = ecalloc(1, olen + alen + 2); 171 memcpy(newopts, opts, olen); 172 newopts[olen] = ' '; 173 memcpy(newopts + olen + 1, add, alen); 174 175 return newopts; 176 } 177 178 /* module database: hash table allows fast realname queries for aliases */ 179 180 static unsigned int 181 hash(const char *s) 182 { 183 unsigned int h = 5381; 184 185 while (*s) 186 h = ((h << 5) + h) + *s++; 187 return h % HASH_SIZE; 188 } 189 190 static void 191 normalize_name(char *dst, const char *src) 192 { 193 const char *base; 194 int i; 195 196 base = strrchr(src, '/'); 197 if (base) 198 base++; 199 else 200 base = src; 201 202 for (i = 0; i < 255 && base[i] && base[i] != '.'; i++) 203 dst[i] = (base[i] == '-') ? '_' : base[i]; 204 dst[i] = '\0'; 205 } 206 207 static struct Module * 208 get_module(const char *path_or_name, int create) 209 { 210 char name[256]; 211 unsigned int h; 212 struct Module *m; 213 214 normalize_name(name, path_or_name); 215 h = hash(name); 216 217 for (m = mod_db[h]; m; m = m->next) { 218 if (strcmp(m->name, name) == 0) 219 return m; 220 } 221 222 if (!create) 223 return NULL; 224 225 m = ecalloc(1, sizeof(*m)); 226 m->name = estrdup(name); 227 m->next = mod_db[h]; 228 mod_db[h] = m; 229 230 return m; 231 } 232 233 /* config parsing: recursively loads aliases and module options from dir tree */ 234 235 static void 236 parse_config_file(const char *path) 237 { 238 FILE *fp; 239 char line[LINE_MAX]; 240 char *p, *cmd, *arg1, *arg2; 241 struct Module *m; 242 243 if (!(fp = fopen(path, "r"))) 244 return; 245 246 while (fgets(line, sizeof(line), fp)) { 247 p = strchr(line, '#'); 248 if (p) 249 *p = '\0'; 250 251 cmd = strtok(line, " \t\n"); 252 if (!cmd) 253 continue; 254 255 arg1 = strtok(NULL, " \t\n"); 256 if (!arg1) 257 continue; 258 259 if (strcmp(cmd, "alias") == 0) { 260 arg2 = strtok(NULL, " \t\n"); 261 if (!arg2) 262 continue; 263 m = get_module(arg1, 1); 264 strlist_append(&m->aliases, arg2); 265 } else if (strcmp(cmd, "options") == 0) { 266 arg2 = strtok(NULL, "\n"); 267 if (!arg2) 268 continue; 269 while (*arg2 == ' ' || *arg2 == '\t') 270 arg2++; 271 m = get_module(arg1, 1); 272 m->options = append_opts(m->options, arg2); 273 } 274 #if FEATURE_MODPROBE_BLACKLIST 275 else if (strcmp(cmd, "blacklist") == 0) { 276 m = get_module(arg1, 1); 277 m->flags |= MOD_BLACKLISTED; 278 } 279 #endif 280 } 281 fclose(fp); 282 } 283 284 static void 285 config_cb(int fd, const char *path, struct stat *st, void *data, struct recursor *r) 286 { 287 size_t len; 288 289 (void)fd; 290 (void)data; 291 (void)r; 292 293 if (S_ISREG(st->st_mode)) { 294 len = strlen(path); 295 if (len > 5 && strcmp(path + len - 5, ".conf") == 0) 296 parse_config_file(path); 297 } 298 } 299 300 static void 301 read_configs(void) 302 { 303 struct recursor r = {.fn = config_cb, .maxdepth = 1, .follow = 'H', .flags = DIRFIRST}; 304 struct stat st; 305 306 parse_config_file("/etc/modprobe.conf"); 307 308 if (stat("/etc/modprobe.d", &st) == 0 && S_ISDIR(st.st_mode)) 309 recurse(AT_FDCWD, "/etc/modprobe.d", NULL, &r); 310 } 311 312 static void 313 parse_dep_file(const char *path) 314 { 315 FILE *fp; 316 char line[LINE_MAX]; 317 char *p, *tok; 318 struct Module *m; 319 320 if (!(fp = fopen(path, "r"))) { 321 pr_warn("fopen %s:", path); 322 return; 323 } 324 325 while (fgets(line, sizeof(line), fp)) { 326 p = strchr(line, ':'); 327 if (!p) 328 continue; 329 *p = '\0'; 330 p++; 331 332 m = get_module(line, 1); 333 if (!m->path) 334 m->path = estrdup(line); 335 m->flags |= MOD_SEEN_DEP; 336 337 while ((tok = strtok(p, " \t\n"))) { 338 p = NULL; 339 if (*tok) 340 strlist_append(&m->deps, tok); 341 } 342 } 343 fclose(fp); 344 } 345 346 static void 347 mark_loaded(void) 348 { 349 FILE *fp; 350 char line[LINE_MAX]; 351 char *p; 352 struct Module *m; 353 354 if (!(fp = fopen(ARUU_LINUX_PATH_PROC_MODULES, "r"))) 355 return; 356 357 while (fgets(line, sizeof(line), fp)) { 358 p = strchr(line, ' '); 359 if (p) 360 *p = '\0'; 361 else { 362 p = strchr(line, '\n'); 363 if (p) 364 *p = '\0'; 365 } 366 m = get_module(line, 1); 367 m->flags |= MOD_LOADED; 368 } 369 fclose(fp); 370 } 371 372 static void 373 list_modules(const char *pattern) 374 { 375 FILE *fp; 376 char line[LINE_MAX]; 377 char *p, *name; 378 379 if (!(fp = fopen("modules.dep", "r"))) { 380 pr_warn("fopen modules.dep:"); 381 return; 382 } 383 384 while (fgets(line, sizeof(line), fp)) { 385 p = strchr(line, ':'); 386 if (!p) 387 continue; 388 *p = '\0'; 389 390 name = strrchr(line, '/'); 391 name = name ? name + 1 : line; 392 393 p = strrchr(name, '.'); 394 if (p) 395 *p = '\0'; 396 397 if (!pattern || fnmatch(pattern, name, 0) == 0) { 398 if (p) 399 *p = '.'; 400 printf("%s\n", line); /* intended output, not a log */ 401 } 402 } 403 fclose(fp); 404 } 405 406 /* kernel interaction */ 407 408 static int 409 load_module(const char *path, const char *opts) 410 { 411 int fd, ret; 412 413 fd = open(path, O_RDONLY | O_CLOEXEC); 414 if (fd < 0) { 415 pr_warn("open %s:", path); 416 return -1; 417 } 418 ret = syscall(__NR_finit_module, fd, opts ? opts : "", 0); 419 close(fd); 420 return ret; 421 } 422 423 static int 424 unload_module(const char *name) 425 { 426 return syscall(__NR_delete_module, name, O_NONBLOCK); 427 } 428 429 /* modprobe actions: load and unload requested module graph */ 430 431 static void 432 process_module(struct Module *m) 433 { 434 struct StrNode *dep; 435 struct Module *dm; 436 char *opts; 437 438 if (!m->path) { 439 pr_warn("module %s not found in modules.dep", m->name); 440 return; 441 } 442 443 if (rflag) { 444 if (m->flags & MOD_LOADED) { 445 if (unload_module(m->name) == 0) 446 m->flags &= ~MOD_LOADED; 447 else 448 pr_warn("unload %s:", m->name); 449 } 450 return; 451 } 452 453 for (dep = m->deps; dep; dep = dep->next) { 454 dm = get_module(dep->str, 0); 455 if (dm && !(dm->flags & MOD_LOADED)) 456 process_module(dm); 457 } 458 459 if (m->flags & MOD_LOADED) { 460 if (vflag) 461 pr_info("%s already loaded", m->name); 462 return; 463 } 464 465 opts = m->options; 466 if (cmdopts && probes && strcmp(probes->str, m->name) == 0) 467 opts = append_opts(opts, cmdopts); 468 469 #if FEATURE_MODPROBE_SHOW_DEPENDS 470 if (Dflag) { 471 printf(opts ? "insmod %s %s\n" : "insmod %s\n", m->path, opts); /* output data */ 472 if (opts != m->options) 473 free(opts); 474 return; 475 } 476 #endif 477 478 if (load_module(m->path, opts) == 0) { 479 m->flags |= MOD_LOADED; 480 if (vflag) 481 pr_info("loaded %s '%s'", m->path, opts ? opts : ""); 482 } else { 483 pr_warn("load %s:", m->path); 484 } 485 486 if (opts != m->options) 487 free(opts); 488 } 489 490 /* a modalias, such as pci:v00008086d00009A49sv...bc03sc00i00, is a device's 491 * own name and names no module: the modules.alias lines that match it as a 492 * glob do. true when one did */ 493 static int 494 probe_pattern(const char *name) 495 { 496 struct Module *pm; 497 struct StrNode *alias; 498 struct Module *am; 499 int hit = 0; 500 unsigned int i; 501 502 for (i = 0; i < HASH_SIZE; i++) { 503 for (pm = mod_db[i]; pm; pm = pm->next) { 504 if (!pm->aliases || !strpbrk(pm->name, "*?[") || fnmatch(pm->name, name, 0) != 0) 505 continue; 506 for (alias = pm->aliases; alias; alias = alias->next) { 507 am = get_module(alias->str, 1); 508 #if FEATURE_MODPROBE_BLACKLIST 509 if (bflag && (am->flags & MOD_BLACKLISTED)) 510 continue; 511 #endif 512 if (vflag) 513 pr_info("probing alias %s -> %s", name, alias->str); 514 process_module(am); 515 hit = 1; 516 } 517 } 518 } 519 return hit; 520 } 521 522 static void 523 do_probe(const char *name) 524 { 525 struct Module *m, *am; 526 struct StrNode *alias; 527 char norm[256]; 528 529 normalize_name(norm, name); 530 m = get_module(norm, 1); 531 532 #if FEATURE_MODPROBE_BLACKLIST 533 if (bflag && (m->flags & MOD_BLACKLISTED)) 534 return; 535 #endif 536 537 if (!m->aliases) { 538 // a modalias, or a name that is a module's own 539 if (!m->path && probe_pattern(norm)) 540 return; 541 if (vflag) 542 pr_info("probing %s by name", norm); 543 process_module(m); 544 return; 545 } 546 547 for (alias = m->aliases; alias; alias = alias->next) { 548 am = get_module(alias->str, 1); 549 #if FEATURE_MODPROBE_BLACKLIST 550 if (am->flags & MOD_BLACKLISTED) 551 continue; 552 #endif 553 if (vflag) 554 pr_info("probing alias %s -> %s", norm, alias->str); 555 process_module(am); 556 } 557 } 558 559 static void 560 usage(void) 561 { 562 eprintf( 563 "usage: %s [-alqrv" 564 #if FEATURE_MODPROBE_SHOW_DEPENDS 565 "D" 566 #endif 567 #if FEATURE_MODPROBE_BLACKLIST 568 "b" 569 #endif 570 #if FEATURE_MODPROBE_SYSLOG 571 "s" 572 #endif 573 "] [-S version] " 574 #if FEATURE_MODPROBE_DIR_OVERRIDE 575 "[-d dir] " 576 #endif 577 "module [symbol=value ...]\n", 578 argv0 579 ); 580 } 581 582 // ?man modprobe: add or remove modules from the Linux kernel 583 // ?man arguments: module [symbol=value ...] 584 // ?man modprobe loads or removes kernel modules from the running system 585 // ?man It reads modules.dep, modules.alias, and modules.symbols from the appropriate 586 // ?man /lib/modules/release directory to resolve module names and dependencies, 587 // ?man loading prerequisites first. 588 // ?man without -r, modprobe loads the named module (and any required dependencies) 589 // ?man into the kernel 590 int 591 main(int argc, char *argv[]) 592 { 593 struct utsname uts; 594 struct StrNode *pn; 595 char path[PATH_MAX]; 596 char *version = NULL; 597 int i, ret = 0; 598 599 ARGBEGIN 600 { 601 case 'a': 602 // ?man -a: Load all modules named on the command line (rather than stopping after the first) 603 aflag = 1; 604 break; 605 case 'r': 606 // ?man -r: Remove the named modules from the kernel dependencies are not automatically 607 // ?man removed 608 rflag = 1; 609 break; 610 case 'q': 611 // ?man -q: Quiet mode. Suppress error messages 612 qflag = 1; 613 break; 614 case 'v': 615 // ?man -v: Verbose mode. Print each action taken 616 vflag = 1; 617 break; 618 case 'l': 619 // ?man -l: List available modules matching the optional _pattern_ (a fnmatch(3) glob) 620 lflag = 1; 621 break; 622 case 'S': 623 // ?man -S:version: Use version instead of the running kernel release 624 version = EARGF(usage()); 625 break; 626 #if FEATURE_MODPROBE_SHOW_DEPENDS 627 case 'D': 628 // ?man -D: Print the sequence of insmod commands that would be used to load the module, 629 // ?man without actually loading anything 630 Dflag = 1; 631 break; 632 #endif 633 #if FEATURE_MODPROBE_BLACKLIST 634 case 'b': 635 // ?man -b: Skip modules listed as blacklist in /etc/modprobe.d/ 636 bflag = 1; 637 break; 638 #endif 639 #if FEATURE_MODPROBE_SYSLOG 640 case 's': 641 // ?man -s: Log messages to syslog(3) (facility LOG_DAEMON) instead of standard error 642 sflag = 1; 643 break; 644 #endif 645 #if FEATURE_MODPROBE_DIR_OVERRIDE 646 case 'd': 647 // ?man -d:dir: use dir as the base directory for module files instead of /lib/modules/release 648 strlist_append(&moddirs, EARGF(usage())); 649 break; 650 #endif 651 default: 652 usage(); 653 } 654 ARGEND 655 656 if (!argc && !rflag && !lflag) 657 usage(); 658 659 #if FEATURE_MODPROBE_SYSLOG 660 if (sflag) 661 openlog("modprobe", LOG_PID, LOG_DAEMON); 662 #endif 663 664 if (!moddirs) { 665 if (!version) { 666 if (uname(&uts) < 0) 667 eprintf("uname:"); 668 version = uts.release; 669 } 670 snprintf(path, sizeof(path), "/lib/modules/%s", version); 671 strlist_append(&moddirs, path); 672 } 673 674 mark_loaded(); 675 read_configs(); 676 677 /* traverse all provided or default base directories for symbol, alias, 678 * and dependency tracking */ 679 for (pn = moddirs; pn; pn = pn->next) { 680 if (chdir(pn->str) < 0) { 681 pr_warn("chdir %s:", pn->str); 682 continue; 683 } 684 685 if (lflag) { 686 list_modules(argc ? argv[0] : NULL); 687 continue; 688 } 689 690 parse_config_file("modules.symbols"); 691 parse_config_file("modules.alias"); 692 parse_dep_file("modules.dep"); 693 } 694 695 if (lflag) 696 goto end; 697 698 if (aflag || rflag) { 699 for (i = 0; i < argc; i++) 700 strlist_append(&probes, argv[i]); 701 } else if (argc > 0) { 702 strlist_append(&probes, argv[0]); 703 for (i = 1; i < argc; i++) 704 cmdopts = append_opts(cmdopts, argv[i]); 705 } 706 707 if (rflag && !argc) { 708 if (syscall(__NR_delete_module, NULL, O_NONBLOCK) < 0) 709 eprintf("delete_module:"); 710 goto end; 711 } 712 713 for (pn = probes; pn; pn = pn->next) { 714 do_probe(pn->str); 715 } 716 717 end: 718 #if FEATURE_MODPROBE_SYSLOG 719 if (sflag) 720 closelog(); 721 #endif 722 if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>")) 723 ret = 2; 724 725 // ?man 726 // ?man ## FILES 727 // ?man 728 // ?man `/etc/modprobe.conf` 729 // ?man : Global configuration file 730 // ?man 731 // ?man Files under `/etc/modprobe.d/` 732 // ?man : Per-module configuration snippets 733 // ?man 734 // ?man `/lib/modules/`_release_`/modules.dep` 735 // ?man : Module dependency map generated by `depmod(8)` 736 // ?man 737 // ?man ## EXIT STATUS 738 // ?man 739 // ?man 0 740 // ?man : Success 741 // ?man 742 // ?man 1 743 // ?man : Module not found or kernel rejected the operation 744 // ?man 745 // ?man 2 746 // ?man : I/O error on stdout or stdin 747 // ?man 748 // ?man ## SEE ALSO 749 // ?man 750 // ?man insmod(8), rmmod(8), lsmod(8), depmod(8) 751 // ?man 752 753 return ret; 754 }