|
1 | 1 | #define _GNU_SOURCE |
2 | 2 | #include <errno.h> |
3 | 3 | #include <fcntl.h> |
4 | | -#include <ftw.h> |
5 | 4 | #include <getopt.h> |
6 | | -#include <libkmod.h> |
7 | 5 | #include <linux/random.h> // RNDADDENTROPY |
8 | 6 | #include <net/if.h> |
9 | 7 | #include <netinet/ip.h> |
|
17 | 15 | #include <sys/stat.h> |
18 | 16 | #include <sys/sysmacros.h> |
19 | 17 | #include <sys/types.h> |
20 | | -#include <sys/utsname.h> |
21 | 18 | #include <sys/wait.h> |
22 | 19 | #include <unistd.h> |
23 | 20 |
|
@@ -55,20 +52,15 @@ static int opentcp(unsigned short port) |
55 | 52 | #endif |
56 | 53 |
|
57 | 54 | #define DEFAULT_PATH_ENV "PATH=/sbin:/usr/sbin:/bin:/usr/bin" |
58 | | -#define OPEN_FDS 15 |
59 | 55 |
|
60 | 56 | const char *const default_envp[] = { |
61 | 57 | DEFAULT_PATH_ENV, |
62 | 58 | NULL, |
63 | 59 | }; |
64 | 60 |
|
65 | | -// global kmod k_ctx so we can access it in the file tree traversal |
66 | | -struct kmod_ctx *k_ctx; |
67 | | - |
68 | 61 | // When nothing is passed, default to the LCOWv1 behavior. |
69 | 62 | const char *const default_argv[] = { "/bin/gcs", "-loglevel", "debug", "-logfile=/run/gcs/gcs.log" }; |
70 | 63 | const char *const default_shell = "/bin/sh"; |
71 | | -const char *const lib_modules = "/lib/modules"; |
72 | 64 |
|
73 | 65 | struct Mount { |
74 | 66 | const char *source, *target, *type; |
@@ -406,110 +398,6 @@ int reap_until(pid_t until_pid) { |
406 | 398 | } |
407 | 399 | } |
408 | 400 |
|
409 | | -// load_module gets the module from the absolute path to the module and then |
410 | | -// inserts into the kernel. |
411 | | -int load_module(struct kmod_ctx *ctx, const char *module_path) { |
412 | | - struct kmod_module *mod = NULL; |
413 | | - int err; |
414 | | - |
415 | | - #ifdef DEBUG |
416 | | - printf("loading module: %s\n", module_path); |
417 | | - #endif |
418 | | - |
419 | | - err = kmod_module_new_from_path(ctx, module_path, &mod); |
420 | | - if (err < 0) { |
421 | | - return err; |
422 | | - } |
423 | | - |
424 | | - err = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL); |
425 | | - if (err < 0) { |
426 | | - kmod_module_unref(mod); |
427 | | - return err; |
428 | | - } |
429 | | - |
430 | | - kmod_module_unref(mod); |
431 | | - return 0; |
432 | | -} |
433 | | - |
434 | | -// parse_tree_entry is called by ftw for each directory and file in the file tree. |
435 | | -// If this entry is a file and has a .ko file extension, attempt to load into kernel. |
436 | | -int parse_tree_entry(const char *fpath, const struct stat *sb, int typeflag) { |
437 | | - int result; |
438 | | - const char *ext; |
439 | | - |
440 | | - if (typeflag != FTW_F) { |
441 | | - // do nothing if this isn't a file |
442 | | - return 0; |
443 | | - } |
444 | | - |
445 | | - ext = strrchr(fpath, '.'); |
446 | | - if (!ext || ext == fpath) { |
447 | | - // no file extension found in the filepath |
448 | | - return 0; |
449 | | - } |
450 | | - |
451 | | - if ((result = strcmp(ext, ".ko")) != 0) { |
452 | | - // file does not have .ko extension so it is not a kernel module |
453 | | - return 0; |
454 | | - } |
455 | | - |
456 | | - // print warning if we fail to load the module, but don't fail fn so |
457 | | - // we keep trying to load the rest of the modules. |
458 | | - result = load_module(k_ctx, fpath); |
459 | | - if (result != 0) { |
460 | | - warn2("failed to load module", fpath); |
461 | | - } |
462 | | - return 0; |
463 | | -} |
464 | | - |
465 | | -// load_all_modules finds the modules in the image and loads them using kmod, |
466 | | -// which accounts for ordering requirements. |
467 | | -void load_all_modules() { |
468 | | - int max_path = 256; |
469 | | - char modules_dir[max_path]; |
470 | | - struct utsname uname_data; |
471 | | - int ret; |
472 | | - |
473 | | - // get information on the running kernel |
474 | | - ret = uname(&uname_data); |
475 | | - if (ret != 0) { |
476 | | - die("failed to get kernel information"); |
477 | | - } |
478 | | - |
479 | | - // create the absolute path of the modules directory this looks |
480 | | - // like /lib/modules/<uname.release> |
481 | | - ret = snprintf(modules_dir, max_path, "%s/%s", lib_modules, uname_data.release); |
482 | | - if (ret < 0) { |
483 | | - die("failed to create the modules directory path"); |
484 | | - } else if (ret > max_path) { |
485 | | - die("modules directory buffer larger than expected"); |
486 | | - } |
487 | | - |
488 | | - if (k_ctx == NULL) { |
489 | | - k_ctx = kmod_new(NULL, NULL); |
490 | | - if (k_ctx == NULL) { |
491 | | - die("failed to create kmod context"); |
492 | | - } |
493 | | - } |
494 | | - |
495 | | - kmod_load_resources(k_ctx); |
496 | | - ret = ftw(modules_dir, parse_tree_entry, OPEN_FDS); |
497 | | - if (ret < 0) { |
498 | | - kmod_unref(k_ctx); |
499 | | - die("failed to load kmod resources"); |
500 | | - } else if (ret != 0) { |
501 | | - // Don't fail on error from walking the file tree and loading modules right now. |
502 | | - // ftw may return an error if the modules directory doesn't exist, which |
503 | | - // may be the case for some images. Additionally, we don't currently support |
504 | | - // using a denylist when loading modules, so we may try to load modules |
505 | | - // that cannot be loaded until later, such as nvidia modules which fail to |
506 | | - // load if no device is present. |
507 | | - warn("error adding modules"); |
508 | | - } |
509 | | - |
510 | | - kmod_unref(k_ctx); |
511 | | -} |
512 | | - |
513 | 401 | #ifdef DEBUG |
514 | 402 | int debug_main(int argc, char **argv) { |
515 | 403 | unsigned int ports[3] = {2056, 2056, 2056}; |
@@ -640,11 +528,6 @@ int main(int argc, char **argv) { |
640 | 528 | init_entropy(entropy_port); |
641 | 529 | } |
642 | 530 |
|
643 | | - #ifdef DEBUG |
644 | | - printf("loading modules\n"); |
645 | | - #endif |
646 | | - load_all_modules(); |
647 | | - |
648 | 531 | pid_t pid = launch(child_argc, child_argv); |
649 | 532 | if (debug_shell != NULL) { |
650 | 533 | // The debug shell takes over as the primary child. |
|
0 commit comments