// 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 show_modversions(struct kmod_ctx *ctx, const char *filename) { struct kmod_list *l, *list = NULL; struct kmod_module *mod; int err = kmod_module_new_from_path(ctx, filename, &mod); if (err < 0) { LOG("Module %s not found.\n", filename); return err; } err = kmod_module_get_versions(mod, &list); if (err < 0) { LOG("could not get modversions of %s: %s\n", filename, strerror(-err)); kmod_module_unref(mod); return err; } kmod_list_foreach(l, list) { const char *symbol = kmod_module_version_get_symbol(l); uint64_t crc = kmod_module_version_get_crc(l); printf("0x%08"PRIx64"\t%s\n", crc, symbol); } kmod_module_versions_free_list(list); kmod_module_unref(mod); return 0; }
static int modinfo_path_do(struct kmod_ctx *ctx, const char *path) { struct kmod_module *mod; int err = kmod_module_new_from_path(ctx, path, &mod); if (err < 0) { LOG("Module file %s not found.\n", path); return err; } err = modinfo_do(mod); kmod_module_unref(mod); return err; }
static int do_rmmod(int argc, char**argv) { struct kmod_ctx *ctx; const char *null_config = NULL; int flags = 0; int i, err, r = 0; for (;;) { int c, idx = 0; c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); if (c == -1) break; switch (c) { case 'f': flags |= KMOD_REMOVE_FORCE; break; case 's': use_syslog = 1; break; case 'v': verbose++; break; case 'h': help(argc,argv); return EXIT_SUCCESS; case 'V': puts(PACKAGE " version " VERSION); return EXIT_SUCCESS; case '?': return EXIT_FAILURE; default: ERR("unexpected getopt_long() value '%c'.\n", c); return EXIT_FAILURE; } } log_open(use_syslog); if (optind >= argc) { ERR("missing module name.\n"); r = EXIT_FAILURE; goto done; } ctx = kmod_new(NULL, &null_config); if (!ctx) { ERR("kmod_new() failed!\n"); r = EXIT_FAILURE; goto done; } log_setup_kmod_log(ctx, verbose); for (i = optind; i < argc; i++) { struct kmod_module *mod; const char *arg = argv[i]; struct stat st; if (stat(arg, &st) == 0) err = kmod_module_new_from_path(ctx, arg, &mod); else err = kmod_module_new_from_name(ctx, arg, &mod); if (err < 0) { ERR("could not use module %s: %s\n", arg, strerror(-err)); break; } if (!(flags & KMOD_REMOVE_FORCE) && check_module_inuse(mod) < 0) { r++; goto next; } err = kmod_module_remove_module(mod, flags); if (err < 0) { ERR("could not remove module %s: %s\n", arg, strerror(-err)); r++; } next: kmod_module_unref(mod); } kmod_unref(ctx); done: log_close(); return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE; }
// 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; }