Example #1
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);
}
Example #2
0
File: kld.c Project: 2asoft/freebsd
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);
}
Example #3
0
static void
printfile(int fileid, int verbose, int humanized)
{
    struct kld_file_stat stat;
    int modid;
    char buf[5];

    stat.version = sizeof(struct kld_file_stat);
    if (kldstat(fileid, &stat) < 0) {
	err(1, "can't stat file id %d", fileid);
    } else {
	if (humanized) {
	       humanize_number(buf, sizeof(buf), stat.size,
	           "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE);

	       printf("%2d %4d %p %5s %s",
	           stat.id, stat.refs, stat.address, buf, stat.name);
	} else {
		printf("%2d %4d %p %8zx %s",
		    stat.id, stat.refs, stat.address, stat.size, stat.name);
	}
    }

    if (verbose) {
	printf(" (%s)\n", stat.pathname);
	printf("\tContains modules:\n");
	printf("\t\tId Name\n");
	for (modid = kldfirstmod(fileid); modid > 0;
	     modid = modfnext(modid))
	    printmod(modid);
    } else
	printf("\n");
}
Example #4
0
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
}
Example #5
0
static void
printfile(int fileid, int verbose)
{
    struct kld_file_stat stat;
    int modid;

    stat.version = sizeof(struct kld_file_stat);
    if (kldstat(fileid, &stat) < 0)
	warn("can't stat file id %d", fileid);
    else
	printf("%2d %4d %p %-8jx %s\n",
	       stat.id, stat.refs, stat.address, (uintmax_t)stat.size, 
	       stat.name);

    if (verbose) {
	printf("\tContains modules:\n");
	printf("\t\tId Name\n");
	for (modid = kldfirstmod(fileid); modid > 0;
	     modid = modfnext(modid))
	    printmod(modid);
    }
}
Example #6
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);
}