Page MenuHomeFreeBSD
Paste P726

Command-Line Input
ActivePublic

Authored by ngie on Mon, Nov 23, 6:54 PM.
Project Tags
None
Referenced Files
F177899522: Command-Line Input
Mon, Nov 23, 6:54 PM
Subscribers
None
#include <sys/types.h>
#include <sys/linker.h>
#include <sys/module.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
#if 0
/*
* 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;
}
#endif
/* 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;
}