int main(int argc, char *argv[]) { int r, k; struct kmod_ctx *ctx; r = parse_argv(argc, argv); if (r <= 0) return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; log_set_target(LOG_TARGET_AUTO); log_parse_environment(); log_open(); umask(0022); if (parse_proc_cmdline(parse_proc_cmdline_item) < 0) return EXIT_FAILURE; ctx = kmod_new(NULL, NULL); if (!ctx) { log_error("Failed to allocate memory for kmod."); goto finish; } kmod_load_resources(ctx); kmod_set_log_fn(ctx, systemd_kmod_log, NULL); r = 0; if (argc > optind) { int i; for (i = optind; i < argc; i++) { k = apply_file(ctx, argv[i], false); if (k < 0 && r == 0) r = k; } } else { _cleanup_free_ char **files = NULL; char **fn, **i; STRV_FOREACH(i, arg_proc_cmdline_modules) { k = load_module(ctx, *i); if (k < 0 && r == 0) r = k; } k = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs); if (k < 0) { log_error("Failed to enumerate modules-load.d files: %s", strerror(-k)); if (r == 0) r = k; goto finish; } STRV_FOREACH(fn, files) { k = apply_file(ctx, *fn, true); if (k < 0 && r == 0) r = k; }
struct kmod_ctx* modalias_init(void) { if (aliasdefault.empty()) set_default_alias_file(); std::string dkms_file(table_name_dir + "dkms-modules.alias"); /* We only use canned aliases as last resort. */ const char *alias_filelist[] = { "/run/modprobe.d", "/etc/modprobe.d", "/lib/modprobe.d", "/lib/module-init-tools/ldetect-lst-modules.alias", aliasdefault.c_str(), dkms_file.c_str(), nullptr, }; /* Init libkmod */ struct kmod_ctx *ctx = kmod_new(dirname.c_str(), alias_filelist); if (!ctx) { fputs("Error: kmod_new() failed!\n", stderr); kmod_unref(ctx); ctx = nullptr; } kmod_load_resources(ctx); return ctx; }
/* called at udev startup and reload */ static int builtin_kmod_init(void) { if (ctx) return 0; ctx = kmod_new(NULL, NULL); if (!ctx) return -ENOMEM; log_debug("Load module index"); kmod_set_log_fn(ctx, udev_kmod_log, NULL); kmod_load_resources(ctx); return 0; }
static int modprobe_lttng(struct kern_modules_param *modules, int entries, int required) { int ret = 0, i; struct kmod_ctx *ctx; ctx = kmod_new(NULL, NULL); if (!ctx) { PERROR("Unable to create kmod library context"); ret = -ENOMEM; goto error; } kmod_set_log_fn(ctx, log_kmod, NULL); kmod_load_resources(ctx); for (i = 0; i < entries; i++) { struct kmod_module *mod = NULL; ret = kmod_module_new_from_name(ctx, modules[i].name, &mod); if (ret < 0) { PERROR("Failed to create kmod module for %s", modules[i].name); goto error; } ret = kmod_module_probe_insert_module(mod, KMOD_PROBE_IGNORE_LOADED, NULL, NULL, NULL, NULL); if (ret < 0) { if (required) { ERR("Unable to load required module %s", modules[i].name); goto error; } else { DBG("Unable to load optional module %s; continuing", modules[i].name); ret = 0; } } else { DBG("Modprobe successfully %s", modules[i].name); } kmod_module_unref(mod); } error: if (ctx) { kmod_unref(ctx); } return ret; }
int kmod_setup(void) { unsigned i; struct kmod_ctx *ctx = NULL; struct kmod_module *mod; int err; for (i = 0; i < ELEMENTSOF(kmod_table); i += 2) { if (access(kmod_table[i+1], F_OK) >= 0) continue; log_debug("Your kernel apparently lacks built-in %s support. Might be a good idea to compile it in. " "We'll now try to work around this by loading the module...", kmod_table[i]); if (!ctx) { ctx = kmod_new(NULL, NULL); if (!ctx) { log_error("Failed to allocate memory for kmod"); return -ENOMEM; } kmod_set_log_fn(ctx, systemd_kmod_log, NULL); kmod_load_resources(ctx); } err = kmod_module_new_from_name(ctx, kmod_table[i], &mod); if (err < 0) { log_error("Failed to load module '%s'", kmod_table[i]); continue; } err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL); if (err == 0) log_info("Inserted module '%s'", kmod_module_get_name(mod)); else if (err == KMOD_PROBE_APPLY_BLACKLIST) log_info("Module '%s' is blacklisted", kmod_module_get_name(mod)); else log_error("Failed to insert '%s'", kmod_module_get_name(mod)); kmod_module_unref(mod); } if (ctx) kmod_unref(ctx); return 0; }
int transport_load_kmod(char *transport_name) { struct kmod_ctx *ctx; struct kmod_module *mod; int rc; ctx = kmod_new(NULL, NULL); if (!ctx) { log_error("Could not load transport module %s. Out of " "memory.", transport_name); return ISCSI_ERR_NOMEM; } kmod_load_resources(ctx); /* * dumb dumb dumb - named iscsi_tcp and ib_iser differently from * transport name */ if (!strcmp(transport_name, "tcp")) rc = kmod_module_new_from_name(ctx, "iscsi_tcp", &mod); else if (!strcmp(transport_name, "iser")) rc = kmod_module_new_from_name(ctx, "ib_iser", &mod); else rc = kmod_module_new_from_name(ctx, transport_name, &mod); if (rc) { log_error("Failed to load module %s.", transport_name); rc = ISCSI_ERR_TRANS_NOT_FOUND; goto unref_mod; } rc = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL); if (rc) { log_error("Could not insert module %s. Kmod error %d", transport_name, rc); rc = ISCSI_ERR_TRANS_NOT_FOUND; } kmod_module_unref(mod); unref_mod: kmod_unref(ctx); return rc; }
int main( int argc, char **argv ) { int r; struct kmod_ctx *ctx; ctx = kmod_new(NULL, NULL); if (!ctx) { printf("Failed to allocate memory for kmod\n"); } kmod_load_resources(ctx); /* Load g_mass_storage module */ r = load_module(ctx, "g_mass_storage"); if (r == EAGAIN) load_module(ctx, "g_mass_storage"); kmod_unref(ctx); return 0; }
int kmod_setup(void) { #ifdef HAVE_KMOD static const struct { const char *module; const char *path; bool warn_if_unavailable:1; bool warn_if_module:1; bool (*condition_fn)(void); } kmod_table[] = { /* auto-loading on use doesn't work before udev is up */ { "autofs4", "/sys/class/misc/autofs", true, false, NULL }, /* early configure of ::1 on the loopback device */ { "ipv6", "/sys/module/ipv6", false, true, NULL }, /* this should never be a module */ { "unix", "/proc/net/unix", true, true, NULL }, /* IPC is needed before we bring up any other services */ { "kdbus", "/sys/fs/kdbus", false, false, is_kdbus_wanted }, #ifdef HAVE_LIBIPTC /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */ { "ip_tables", "/proc/net/ip_tables_names", false, false, NULL }, #endif }; struct kmod_ctx *ctx = NULL; unsigned int i; int r; if (have_effective_cap(CAP_SYS_MODULE) == 0) return 0; for (i = 0; i < ELEMENTSOF(kmod_table); i++) { struct kmod_module *mod; if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0) continue; if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn()) continue; if (kmod_table[i].warn_if_module) log_debug("Your kernel apparently lacks built-in %s support. Might be " "a good idea to compile it in. We'll now try to work around " "this by loading the module...", kmod_table[i].module); if (!ctx) { ctx = kmod_new(NULL, NULL); if (!ctx) return log_oom(); kmod_set_log_fn(ctx, systemd_kmod_log, NULL); kmod_load_resources(ctx); } r = kmod_module_new_from_name(ctx, kmod_table[i].module, &mod); if (r < 0) { log_error("Failed to lookup module '%s'", kmod_table[i].module); continue; } r = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL); if (r == 0) log_debug("Inserted module '%s'", kmod_module_get_name(mod)); else if (r == KMOD_PROBE_APPLY_BLACKLIST) log_info("Module '%s' is blacklisted", kmod_module_get_name(mod)); else { bool print_warning = kmod_table[i].warn_if_unavailable || (r < 0 && r != -ENOENT); log_full_errno(print_warning ? LOG_WARNING : LOG_DEBUG, r, "Failed to insert module '%s': %m", kmod_module_get_name(mod)); } kmod_module_unref(mod); } if (ctx) kmod_unref(ctx); #endif return 0; }
static int do_modinfo(int argc, char *argv[]) { struct kmod_ctx *ctx; char dirname_buf[PATH_MAX]; const char *dirname = NULL; const char *kversion = NULL; const char *root = NULL; const char *null_config = NULL; int i, err; for (;;) { int c, idx = 0; c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); if (c == -1) break; switch (c) { case 'a': field = "author"; break; case 'd': field = "description"; break; case 'l': field = "license"; break; case 'p': field = "parm"; break; case 'n': field = "filename"; break; case '0': separator = '\0'; break; case 'F': field = optarg; break; case 'k': kversion = optarg; break; case 'b': root = optarg; break; case 'h': help(basename(argv[0])); return EXIT_SUCCESS; case 'V': puts(PACKAGE " version " VERSION); return EXIT_SUCCESS; case '?': return EXIT_FAILURE; default: fprintf(stderr, "Error: unexpected getopt_long() value '%c'.\n", c); return EXIT_FAILURE; } } if (optind >= argc) { fprintf(stderr, "Error: missing module or filename.\n"); return EXIT_FAILURE; } if (root != NULL || kversion != NULL) { struct utsname u; if (root == NULL) root = ""; if (kversion == NULL) { if (uname(&u) < 0) { fprintf(stderr, "Error: uname() failed: %s\n", strerror(errno)); return EXIT_FAILURE; } kversion = u.release; } snprintf(dirname_buf, sizeof(dirname_buf), "%s" ROOTPREFIX "/lib/modules/%s", root, kversion); dirname = dirname_buf; } ctx = kmod_new(dirname, &null_config); if (!ctx) { fputs("Error: kmod_new() failed!\n", stderr); return EXIT_FAILURE; } kmod_load_resources(ctx); err = 0; for (i = optind; i < argc; i++) { const char *name = argv[i]; struct stat st; int r; if (stat(name, &st) == 0 && S_ISREG(st.st_mode)) r = modinfo_path_do(ctx, name); else r = modinfo_alias_do(ctx, name); if (r < 0) err = r; } kmod_unref(ctx); return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; }