Ejemplo n.º 1
0
int module_load(const char *name, int argc, char **argv) {
	int i;
	Module *module = NULL;
	Module **free_spot = NULL;
	
	if(!name)
		return -1;
	
	if(module_is_loaded(name)) {
		kprintf(LOG_LEVEL_WARNING, "Module '%s' is already loaded\n", name);
		return -1;
	}
	
	for(i = 0; i < MODULES_MAX; i++) {
		if(!_module_registered[i]) {
			continue;
		}
		if(!strcmp(module->name, _module_registered[i]->name)) {
			module = _module_registered[i];
			break;
		}
	}
	
	if(!module) {
		kprintf(LOG_LEVEL_ERROR, "Module '%s' is not registered in kernel\n", name);
		return -1;
	}
	
	if(module->depends) {
		for(i = 0; module->depends[i]; i++) {
			if(!module_is_loaded(module->depends[i])) {
				kprintf(LOG_LEVEL_ERROR, "Module '%s' is missing dependency '%s'\n", name, module->depends[i]);
				return -1;
			}
		}
	}
	
	for(i = 0; i < MODULES_MAX; i++) {
		if(!_module_loaded[i]) {
			free_spot = &_module_loaded[i];
			break;
		}
	}
	
	if(!free_spot) {
		kprintf(LOG_LEVEL_ERROR, "Cannot load module, max modules is %i\n", MODULES_MAX);
		return -1;
	}
	*free_spot = module;
	
	kprintf(LOG_LEVEL_DEBUG, "Loading module %s\n", name);
	if(module->init(argc, argv) < 0) {
		//TODO: properly deinit and unload module instead
		panic("Module failed to initialize");
	}
	kprintf(LOG_LEVEL_INFO, "Loaded module %s\n", name);
	
	return 0;
}
Ejemplo n.º 2
0
/**
 * Attempts to unload a module if loaded, for ten seconds before
 * giving up
 *
 * @param driver The name of the driver (not a filename)
 * @return 1 if the driver is succesfully unloaded, 0 otherwise
 */
int module_unload(char *driver) {
  if (module_is_loaded(driver) == 1) {
    bb_log(LOG_INFO, "Unloading %s driver\n", driver);
    char *mod_argv[] = {
      "rmmod",
      "--wait",
      driver,
      NULL
    };
    bb_run_fork_wait(mod_argv, 10);
    if (module_is_loaded(driver) == 1) {
      bb_log(LOG_ERR, "Unloading %s driver timed out.\n", driver);
      return 0;
    }
  }
  return 1;
}
Ejemplo n.º 3
0
/**
 * Attempts to load a module. If the module has not been loaded after ten
 * seconds, give up
 *
 * @param module_name The filename of the module to be loaded
 * @param driver The name of the driver to be loaded
 * @return 1 if the driver is succesfully loaded, 0 otherwise
 */
int module_load(char *module_name, char *driver) {
  if (module_is_loaded(driver) == 0) {
    /* the module has not loaded yet, try to load it */
    bb_log(LOG_INFO, "Loading driver %s (module %s)\n", driver, module_name);
    char *mod_argv[] = {
      "modprobe",
      module_name,
      NULL
    };
    bb_run_fork_wait(mod_argv, 10);
    if (module_is_loaded(driver) == 0) {
      bb_log(LOG_ERR, "Module %s could not be loaded (timeout?)\n", module_name);
      return 0;
    }
  }
  return 1;
}
Ejemplo n.º 4
0
	Value import(StringConstPtr _file) {
		std::string file;
		string_to_std_string(_file, file);
		std::string path;
		if (expand_load_path(file, path))
		{
			Module* module = NULL;
			if (module_is_loaded(path, module)) {
				return module->module;
			} else {
				module = load_module(path);
				return module->module;
			}
		}
		throw_exception_with_description("File not found in any load path: %@", file.c_str());
		return NULL;
	}
Ejemplo n.º 5
0
/**
 * Attempts to load a module.
 *
 * @param module_name The filename of the module to be loaded
 * @param driver The name of the driver to be loaded
 * @return 1 if the driver is succesfully loaded, 0 otherwise
 */
int module_load(char *module_name, char *driver) {
  int err = 0;
  int flags = KMOD_PROBE_IGNORE_LOADED;
  struct kmod_list *l, *list = NULL;

  if (module_is_loaded(driver) == 0) {
    /* the module has not loaded yet, try to load it */

    bb_log(LOG_INFO, "Loading driver '%s' (module '%s')\n", driver, module_name);
    err = kmod_module_new_from_lookup(bb_status.kmod_ctx, module_name, &list);

    if (err < 0) {
      bb_log(LOG_DEBUG, "kmod_module_new_from_lookup(%s) failed (err: %d).\n",
        module_name, err);
      return 0;
    }

    if (list == NULL) {
      bb_log(LOG_ERR, "Module '%s' not found.\n", module_name);
      return 0;
    }

    kmod_list_foreach(l, list) {
      struct kmod_module *mod = kmod_module_get_module(l);

      bb_log(LOG_DEBUG, "Loading module '%s'.\n", kmod_module_get_name(mod));
      err = kmod_module_probe_insert_module(mod, flags, NULL, NULL, NULL, 0);

      if (err < 0) {
        bb_log(LOG_DEBUG, "kmod_module_probe_insert_module(%s) failed (err: %d).\n",
          kmod_module_get_name(mod), err);
      }

      kmod_module_unref(mod);

      if (err < 0) {
        break;
      }
    }

    kmod_module_unref_list(list);
  }