// test_insert.cmt static __noreturn int test_insert(const struct test *t) { struct kmod_ctx *ctx; struct kmod_module *mod; const char *null_config = NULL; int err; ctx = kmod_new(NULL, &null_config); if (ctx == NULL) exit(EXIT_FAILURE); err = kmod_module_new_from_path(ctx, "/ext4-x86_64.ko", &mod); if (err != 0) { ERR("could not create module from path: %m\n"); exit(EXIT_FAILURE); } err = kmod_module_insert_module(mod, 0, NULL); if (err != 0) { ERR("could not insert module: %m\n"); exit(EXIT_FAILURE); } kmod_unref(ctx); exit(EXIT_SUCCESS); }
static int insmod_do_insert_module(struct kmod_module *mod, const char *opts) { int flags = 0, err; SHOW("insmod %s %s\n", kmod_module_get_path(mod), opts ? opts : ""); if (dry_run) return 0; if (strip_modversion || force) flags |= KMOD_INSERT_FORCE_MODVERSION; if (strip_vermagic || force) flags |= KMOD_INSERT_FORCE_VERMAGIC; err = kmod_module_insert_module(mod, flags, opts); switch (err) { case -EEXIST: /* * We checked for EEXIST with an earlier call to * retrieve the initstate, but to avoid a race * condition, we don't make any assumptions and handle * the error again here */ if (!first_time) err = 0; else ERR("Module %s already in kernel.\n", kmod_module_get_name(mod)); break; case -EPERM: ERR("could not insert '%s': %s\n", kmod_module_get_name(mod), strerror(-err)); break; } return err; }
// do_insmod.cmt static int do_insmod(int argc, char *argv[]) { struct kmod_ctx *ctx; struct kmod_module *mod; const char *filename; char *opts = NULL; size_t optslen = 0; int i, err; const char *null_config = NULL; printf("begin to parse para\n"); for (;;) { int c, idx = 0; c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); printf("c = %d\n", c); if (c == -1) break; switch (c) { case 'p': case 's': case 'f': /* ignored, for compatibility only */ break; case 'h': help(); return EXIT_SUCCESS; case 'V': puts(PACKAGE " version " VERSION); return EXIT_SUCCESS; case '?': return EXIT_FAILURE; default: printf("no this para\n"); ERR("unexpected getopt_long() value '%c'.\n", c); return EXIT_FAILURE; } } if (optind >= argc) { ERR("missing filename.\n"); return EXIT_FAILURE; } filename = argv[optind]; if (strcmp(filename, "-") == 0) { ERR("this tool does not support loading from stdin!\n"); return EXIT_FAILURE; } for (i = optind + 1; i < argc; i++) { size_t len = strlen(argv[i]); void *tmp = realloc(opts, optslen + len + 2); if (tmp == NULL) { ERR("out of memory\n"); free(opts); return EXIT_FAILURE; } opts = tmp; if (optslen > 0) { opts[optslen] = ' '; optslen++; } memcpy(opts + optslen, argv[i], len); optslen += len; opts[optslen] = '\0'; } ctx = kmod_new(NULL, &null_config); if (!ctx) { ERR("kmod_new() failed!\n"); free(opts); return EXIT_FAILURE; } err = kmod_module_new_from_path(ctx, filename, &mod); if (err < 0) { ERR("could not load module %s: %s\n", filename, strerror(-err)); goto end; } err = kmod_module_insert_module(mod, 0, opts); if (err < 0) { ERR("could not insert module %s: %s\n", filename, mod_strerror(-err)); } kmod_module_unref(mod); end: kmod_unref(ctx); free(opts); return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; }