#include #include #include #include #include #include #include int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "usage: kmod_loaded kmod\n"); exit(1); } const char *kmod = argv[1]; struct kld_file_stat fstat = { .version = sizeof(fstat) }; struct module_stat mstat = { .version = sizeof(mstat) }; size_t len = strlen(kmod); int fid, mid; /* * A driver was either loaded as a module, or built in to the kernel * already. * * TODO(ngie): determine if the loops below are even necessary if * modfind(2) is used. */ if (modfind(kmod) != -1) { goto done; } /* Need to see if a file with the name, `kmod`, is loaded. */ for (fid = kldnext(0); fid > 0; fid = kldnext(fid)) { if (kldstat(fid, &fstat) != 0) continue; if (strcmp(fstat.name, kmod) == 0) { warnx("fstat.name matched"); goto done; } if (strncmp(fstat.name, kmod, len) == 0 && strcmp(fstat.name + len, ".ko") == 0) goto done; for (mid = kldfirstmod(fid); mid > 0; mid = modfnext(mid)) { if (modstat(mid, &mstat) != 0) continue; warnx("mstat.name: %s", mstat.name); if (strcmp(mstat.name, kmod) == 0) goto done; } } errx(1, "%s not loaded", kmod); done: return 0; }