int kld_isloaded(const char *name) { struct kld_file_stat fstat; struct module_stat mstat; const char *ko; int fid, mid; for (fid = kldnext(0); fid > 0; fid = kldnext(fid)) { fstat.version = sizeof(fstat); if (kldstat(fid, &fstat) != 0) continue; /* check if the file name matches the supplied name */ if (strcmp(fstat.name, name) == 0) return (1); /* strip .ko and try again */ if ((ko = strstr(fstat.name, ".ko")) != NULL && strlen(name) == (size_t)(ko - fstat.name) && strncmp(fstat.name, name, ko - fstat.name) == 0) return (1); /* look for a matching module within the file */ for (mid = kldfirstmod(fid); mid > 0; mid = modfnext(mid)) { mstat.version = sizeof(mstat); if (modstat(mid, &mstat) != 0) continue; if (strcmp(mstat.name, name) == 0) return (1); } } return (0); }
/* * Load the if_bridge.ko module in kernel if not already there. */ int bridge_kmod_load(void) { int fileid, modid; const char mod_name[] = "if_bridge"; struct module_stat mstat; /* Scan files in kernel. */ mstat.version = sizeof(struct module_stat); for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { /* Scan modules in file. */ for (modid = kldfirstmod(fileid); modid > 0; modid = modfnext(modid)) { if (modstat(modid, &mstat) < 0) continue; if (strcmp(mod_name, mstat.name) == 0) return (0); } } /* Not present - load it. */ if (kldload(mod_name) < 0) { syslog(LOG_ERR, "failed to load %s kernel module", mod_name); return (-1); } return (1); }
/* * Get kernel items: first the kernel itself, then the loaded modules. */ static void swrun_OS_get_kinfo(void) { int fileid; struct swrun_entry *entry; struct kld_file_stat stat; for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { stat.version = sizeof(struct kld_file_stat); if (kldstat(fileid, &stat) < 0) { syslog(LOG_ERR, "kldstat() failed: %m"); continue; } /* * kernel and kernel files (*.ko) will be indexed starting with * NO_PID + 1; NO_PID is PID_MAX + 1 thus it will be no risk to * overlap with real PIDs which are in range of 1 .. NO_PID */ entry = swrun_entry_find_by_index(NO_PID + 1 + stat.id); if (entry == NULL) { /* new entry - get memory for it */ entry = swrun_entry_create(NO_PID + 1 + stat.id); if (entry == NULL) continue; } entry->flags |= HR_SWRUN_FOUND; /* mark it as found */ kld_file_stat_to_swrun(&stat, entry); } }
int virHostValidateBhyve(void) { int ret = 0; int fileid = 0; struct kld_file_stat stat; bool vmm_loaded = false, if_tap_loaded = false; bool if_bridge_loaded = false, nmdm_loaded = false; for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { stat.version = sizeof(struct kld_file_stat); if (kldstat(fileid, &stat) < 0) continue; if (STREQ(stat.name, "vmm.ko")) vmm_loaded = true; else if (STREQ(stat.name, "if_tap.ko")) if_tap_loaded = true; else if (STREQ(stat.name, "if_bridge.ko")) if_bridge_loaded = true; else if (STREQ(stat.name, "nmdm.ko")) nmdm_loaded = true; } MODULE_STATUS_FAIL(vmm, "will not be able to start VMs"); MODULE_STATUS_WARN(if_tap, "networking will not work"); MODULE_STATUS_WARN(if_bridge, "bridged networking will not work"); MODULE_STATUS_WARN(nmdm, "nmdm console will not work"); return ret; }
void ifmaybeload(const char *name) { #ifndef __rtems__ #define MOD_PREFIX_LEN 3 /* "if_" */ struct module_stat mstat; int fileid, modid; char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp; const char *cp; /* loading suppressed by the user */ if (noload) return; /* trim the interface number off the end */ strlcpy(ifname, name, sizeof(ifname)); for (dp = ifname; *dp != 0; dp++) if (isdigit(*dp)) { *dp = 0; break; } /* turn interface and unit into module name */ strcpy(ifkind, "if_"); strlcpy(ifkind + MOD_PREFIX_LEN, ifname, sizeof(ifkind) - MOD_PREFIX_LEN); /* scan files in kernel */ mstat.version = sizeof(struct module_stat); for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { /* scan modules in file */ for (modid = kldfirstmod(fileid); modid > 0; modid = modfnext(modid)) { if (modstat(modid, &mstat) < 0) continue; /* strip bus name if present */ if ((cp = strchr(mstat.name, '/')) != NULL) { cp++; } else { cp = mstat.name; } /* already loaded? */ if (strncmp(ifname, cp, strlen(ifname) + 1) == 0 || strncmp(ifkind, cp, strlen(ifkind) + 1) == 0) return; } } /* not present, we should try to load it */ kldload(ifkind); #endif }
int ShowLoadedKernelModules() { int FileID = 0; DrawStatScreen(); for(FileID = kldnext(0); FileID > 0; FileID = kldnext(FileID)) printstats(FileID, verbose); wgetch(DLG); count=1; return 0; }
void ifmaybeload(const char *name) { struct module_stat mstat; int fileid, modid; char ifkind[35], *dp; const char *cp; /* turn interface and unit into module name */ strcpy(ifkind, "if_"); for (cp = name, dp = ifkind + 3; (*cp != 0) && !isdigit(*cp); cp++, dp++) *dp = *cp; *dp = 0; /* scan files in kernel */ mstat.version = sizeof(struct module_stat); for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { /* scan modules in file */ for (modid = kldfirstmod(fileid); modid > 0; modid = modfnext(modid)) { if (modstat(modid, &mstat) < 0) continue; /* strip bus name if present */ if ((cp = strchr(mstat.name, '/')) != NULL) { cp++; } else { cp = mstat.name; } /* already loaded? */ if (strncmp(name, cp, strlen(cp)) == 0 || strncmp(ifkind, cp, strlen(cp)) == 0) return; } } /* not present, we should try to load it */ kldload(ifkind); }
/* * Get the linker file list using the kld interface. * Works with a live kernel only. */ void asf_kld() { struct kld_file_stat kfs; int fid = 0; /* indicates the beginning of the linker file list */ while ((fid = kldnext(fid)) != 0) { if (fid == -1) err(2, "kldnext"); kfs.version = sizeof(kfs); /* must be set for kldstat(2) */ /* Get info on this linker file */ if (kldstat(fid, &kfs) == -1) err(2, "kldstat"); if (strcmp(kfs.name, KERNFILE) == 0) continue; /* Add to our list of linker files */ kfile_add(kfs.name, kfs.address); } }
int main(int argc, char** argv) { int c; int verbose = 0; int fileid = 0; int quiet = 0; char* filename = NULL; char* modname = NULL; char* p; while ((c = getopt(argc, argv, "i:m:n:qv")) != -1) switch (c) { case 'i': fileid = (int)strtoul(optarg, &p, 10); if (*p != '\0') usage(); break; case 'm': modname = optarg; break; case 'n': filename = optarg; break; case 'q': quiet = 1; break; case 'v': verbose = 1; break; default: usage(); } argc -= optind; argv += optind; if (argc != 0) usage(); if (modname != NULL) { int modid; struct module_stat stat; if ((modid = modfind(modname)) < 0) { if (!quiet) warn("can't find module %s", modname); return 1; } else if (quiet) { return 0; } stat.version = sizeof(struct module_stat); if (modstat(modid, &stat) < 0) warn("can't stat module id %d", modid); else { printf("Id Refs Name\n"); printf("%3d %4d %s\n", stat.id, stat.refs, stat.name); } return 0; } if (filename != NULL) { if ((fileid = kldfind(filename)) < 0) err(1, "can't find file %s", filename); } printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); if (fileid != 0) printfile(fileid, verbose); else for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) printfile(fileid, verbose); return 0; }
/* * Unload all the loaded modules and then refresh the module cache with the * latest list of loaded modules and their address ranges. */ void dtrace_update(dtrace_hdl_t *dtp) { dt_module_t *dmp; DIR *dirp; #if defined(__FreeBSD__) int fileid; #endif for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL; dmp = dt_list_next(dmp)) dt_module_unload(dtp, dmp); #if defined(sun) /* * Open /system/object and attempt to create a libdtrace module for * each kernel module that is loaded on the current system. */ if (!(dtp->dt_oflags & DTRACE_O_NOSYS) && (dirp = opendir(OBJFS_ROOT)) != NULL) { struct dirent *dp; while ((dp = readdir(dirp)) != NULL) { if (dp->d_name[0] != '.') dt_module_update(dtp, dp->d_name); } (void) closedir(dirp); } #elif defined(__FreeBSD__) /* * Use FreeBSD's kernel loader interface to discover what kernel * modules are loaded and create a libdtrace module for each one. */ for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { struct kld_file_stat k_stat; k_stat.version = sizeof(k_stat); if (kldstat(fileid, &k_stat) == 0) dt_module_update(dtp, &k_stat); } #endif /* * Look up all the macro identifiers and set di_id to the latest value. * This code collaborates with dt_lex.l on the use of di_id. We will * need to implement something fancier if we need to support non-ints. */ dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid(); dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid(); dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid(); dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid(); dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0); dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid(); #if defined(sun) dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid(); #endif dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0); #if defined(sun) dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid(); #endif dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid(); /* * Cache the pointers to the modules representing the base executable * and the run-time linker in the dtrace client handle. Note that on * x86 krtld is folded into unix, so if we don't find it, use unix * instead. */ dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix"); dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld"); if (dtp->dt_rtld == NULL) dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix"); /* * If this is the first time we are initializing the module list, * remove the module for genunix from the module list and then move it * to the front of the module list. We do this so that type and symbol * queries encounter genunix and thereby optimize for the common case * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below. */ if (dtp->dt_exec != NULL && dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) { dt_list_delete(&dtp->dt_modlist, dtp->dt_exec); dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec); } }
int main(int argc, char** argv) { int c; int humanized = 0; int verbose = 0; int fileid = 0; int quiet = 0; char* filename = NULL; char* modname = NULL; char* p; while ((c = getopt(argc, argv, "dhi:m:n:qv")) != -1) switch (c) { case 'd': showdata = 1; break; case 'h': humanized = 1; break; case 'i': fileid = (int)strtoul(optarg, &p, 10); if (*p != '\0') usage(); break; case 'm': modname = optarg; break; case 'n': filename = optarg; break; case 'q': quiet = 1; break; case 'v': verbose = 1; break; default: usage(); } argc -= optind; argv += optind; if (argc != 0) usage(); if (modname != NULL) { int modid; struct module_stat stat; if ((modid = modfind(modname)) < 0) { if (!quiet) warn("can't find module %s", modname); return 1; } else if (quiet) { return 0; } stat.version = sizeof(struct module_stat); if (modstat(modid, &stat) < 0) warn("can't stat module id %d", modid); else { if (showdata) { printf("Id Refs Name data..(int, uint, ulong)\n"); printf("%3d %4d %s (%d, %u, 0x%lx)\n", stat.id, stat.refs, stat.name, stat.data.intval, stat.data.uintval, stat.data.ulongval); } else { printf("Id Refs Name\n"); printf("%3d %4d %s\n", stat.id, stat.refs, stat.name); } } return 0; } if (filename != NULL) { if ((fileid = kldfind(filename)) < 0) { if (!quiet) warn("can't find file %s", filename); return 1; } else if (quiet) { return 0; } } if (humanized) printf("Id Refs Address%*c %5s Name\n", POINTER_WIDTH - 7, ' ', "Size"); else printf("Id Refs Address%*c %8s Name\n", POINTER_WIDTH - 7, ' ', "Size"); if (fileid != 0) printfile(fileid, verbose, humanized); else for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) printfile(fileid, verbose, humanized); return 0; }