Example #1
0
static int
command_echo(int argc, char *argv[])
{
    char	*s;
    int		nl, ch;
    
    nl = 0;
    optind = 1;
    optreset = 1;
    while ((ch = getopt(argc, argv, "n")) != -1) {
	switch(ch) {
	case 'n':
	    nl = 1;
	    break;
	case '?':
	default:
	    /* getopt has already reported an error */
	    return(CMD_OK);
	}
    }
    argv += (optind);
    argc -= (optind);

    s = unargv(argc, argv);
    if (s != NULL) {
	printf("%s", s);
	free(s);
    }
    if (!nl)
	printf("\n");
    return(CMD_OK);
}
/*
 * Load specified KLD. If path is omitted, then try to locate it via
 * search path.
 */
int
file_loadkernel(char *filename, int argc, char *argv[])
{
    struct preloaded_file	*fp, *last_file;
    int				err;

    /* 
     * Check if KLD already loaded
     */
    fp = file_findfile(filename, NULL);
    if (fp) {
	command_seterr("warning: KLD '%s' already loaded", filename);
	free(filename);
	return (0);
    }
    for (last_file = preloaded_files; 
	 last_file != NULL && last_file->f_next != NULL;
	 last_file = last_file->f_next)
	;

    do {
	err = file_load(filename, loadaddr, &fp);
	if (err)
	    break;
	fp->f_args = unargv(argc, argv);
	loadaddr = fp->f_addr + fp->f_size;
	file_insert_tail(fp);		/* Add to the list of loaded files */
    } while(0);
    if (err == EFTYPE)
	command_seterr("don't know how to load module '%s'", filename);
    if (err && fp)
	file_discard(fp);
    free(filename);
    return (err);
}
int
command_boot(int argc, char *argv[])
{
    struct preloaded_file	*fp;
    
    /*
     * See if the user has specified an explicit kernel to boot.
     */
    if ((argc > 1) && (argv[1][0] != '-')) {
	
	/* XXX maybe we should discard everything and start again? */
	if (file_findfile(NULL, NULL) != NULL) {
	    command_seterr("can't boot '%s', kernel module already loaded",
		argv[1]);
	    return(CMD_ERROR);
	}

	/* find/load the kernel module */
	if (file_loadkernel(argv[1], argc - 2, argv + 2) != 0)
	    return(CMD_ERROR);

	/* we have consumed all arguments */
	argc = 1;
    }

    /*
     * See if there is a kernel module already loaded
     */
    if (file_findfile(NULL, NULL) == NULL)
	if (loadakernel(0, argc - 1, argv + 1))
	    /* we have consumed all arguments */
	    argc = 1;

    /*
     * Loaded anything yet?
     */
    if ((fp = file_findfile(NULL, NULL)) == NULL) {
	command_seterr("no bootable kernel");
	return(CMD_ERROR);
    }

    /*
     * If we were given arguments, discard any previous.
     * XXX should we merge arguments?  Hard to DWIM.
     */
    if (argc > 1) {
	if (fp->f_args != NULL)	
	    free(fp->f_args);
	fp->f_args = unargv(argc - 1, argv + 1);
    }

    /* Hook for platform-specific autoloading of modules */
    if (archsw.arch_autoload() != 0)
	return(CMD_ERROR);

    /* Call the exec handler from the loader matching the kernel */
    command_seterr("%s", strerror(file_formats[fp->f_loader]->l_exec(fp)));
    return(CMD_ERROR);
}
Example #4
0
static int
command_boot(int argc, char *argv[])
{
    struct preloaded_file	*fp;
    char *local_module_path;
    char *exported_module_path;

    /*
     * See if the user has specified an explicit kernel to boot.
     */
    if ((argc > 1) && (argv[1][0] != '-')) {

	/* XXX maybe we should discard everything and start again? */
	if (file_findfile(NULL, NULL) != NULL) {
	    sprintf(command_errbuf, "can't boot '%s', kernel module already loaded", argv[1]);
	    return(CMD_ERROR);
	}

	/* find/load the kernel module */
	if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0)
	    return(CMD_ERROR);
	/* we have consumed all arguments */
	argc = 1;
    }

    /*
     * See if there is a kernel module already loaded
     */
    if (file_findfile(NULL, NULL) == NULL)
	if (loadakernel(0, argc - 1, argv + 1))
	    /* we have consumed all arguments */
	    argc = 1;

    /*
     * Loaded anything yet?
     */
    if ((fp = file_findfile(NULL, NULL)) == NULL) {
	command_errmsg = "no bootable kernel";
	return(CMD_ERROR);
    }

    /*
     * If we were given arguments, discard any previous.
     * XXX should we merge arguments?  Hard to DWIM.
     */
    if (argc > 1) {
	if (fp->f_args != NULL)
	    free(fp->f_args);
	fp->f_args = unargv(argc - 1, argv + 1);
    }

    /* Hook for platform-specific autoloading of modules */
    if (archsw.arch_autoload() != 0)
	return(CMD_ERROR);

    /*
     * Exec the kernel.  We have to shift our exported_module_path
     * (which has the correct /boot prefix for the kernel) over to
     * module_path.  If the exec fails we switch it back.
     */
    exported_module_path = getenv("exported_module_path");
    if (exported_module_path) {
	    exported_module_path = strdup(exported_module_path);
	    local_module_path = getenv("module_path");
	    if (local_module_path)
		local_module_path = strdup(local_module_path);
	    setenv("module_path", exported_module_path, 1);
	    unsetenv("exported_module_path");
    }

    /* Call the exec handler from the loader matching the kernel */
    file_formats[fp->f_loader]->l_exec(fp);

    if (exported_module_path) {
	    if (local_module_path) {
		setenv("module_path", local_module_path, 1);
		free(local_module_path);
	    } else {
		unsetenv("module_path");
	    }
	    setenv("exported_module_path", exported_module_path, 1);
	    free(exported_module_path);
    }
    return(CMD_ERROR);
}
Example #5
0
/*
 * Load the module (name), pass it (argc),(argv).
 * Don't do any dependancy checking.
 */
static int
mod_loadmodule(char *name, int argc, char *argv[], struct loaded_module **mpp)
{
    struct loaded_module	*mp;
    int				i, err;
    char			*cp;

    /* locate the module on the search path */
    cp = mod_searchmodule(name);
    if (cp == NULL) {
	sprintf(command_errbuf, "can't find '%s'", name);
	return (ENOENT);
    }
    name = cp;

    cp = strrchr(name, '/');
    if (cp)
        cp++;
    else
        cp = name;
    /* see if module is already loaded */
    mp = mod_findmodule(cp, NULL);
    if (mp) {
	*mpp = mp;
	return (EEXIST);
    }

    err = 0;
    for (i = 0, mp = NULL; (module_formats[i] != NULL) && (mp == NULL); i++) {
	if ((err = (module_formats[i]->l_load)(name, loadaddr, &mp)) != 0) {

	    /* Unknown to this handler? */
	    if (err == EFTYPE)
		continue;
		
	    /* Fatal error */
	    sprintf(command_errbuf, "can't load module '%s': %s", name, strerror(err));
	    free(name);
	    return (err);
	} else {

	    /* Load was OK, set args */
	    mp->m_args = unargv(argc, argv);

	    /* where can we put the next one? */
	    loadaddr = mp->m_addr + mp->m_size;
	
	    /* remember the loader */
	    mp->m_loader = i;

	    /* Add to the list of loaded modules */
	    mod_append(mp);
	    *mpp = mp;

	    break;
	}
    }
    if (err == EFTYPE)
	sprintf(command_errbuf, "don't know how to load module '%s'", name);
    free(name);
    return (err);
}