Esempio n. 1
0
struct mod_reg *mod_register(const char *name, struct mod_reg_info *reg_info, void *priv) {

	if (mod_load_dependencies(reg_info->dependencies) != POM_OK)
		return NULL;

	struct mod_reg *reg = malloc(sizeof(struct mod_reg));
	if (!reg) {
		pom_oom(sizeof(struct mod_reg));
		return NULL;
	}
	memset(reg, 0, sizeof(struct mod_reg));

	reg->name = strdup(name);
	reg->info = reg_info;

	if (!reg->name || pthread_mutex_init(&reg->lock, NULL)) {
		if (reg->name)
			free(reg->name);
		free(reg);

		pomlog(POMLOG_ERR "Not enough memory to allocate name and filename of struct mod_reg or failed to initialize the lock");

		return NULL;
	}


	pomlog(POMLOG_DEBUG "Module %s loaded, registering components ...", reg->name);
	reg->priv = priv;

	if (reg->info->register_func(reg) != POM_OK) {
		pomlog(POMLOG_WARN "Error while registering the components of module %s", reg->name);
		mod_unload(reg);
		return NULL;
	}

	pom_mutex_lock(&reg->lock);
	if (!reg->refcount) {
		pom_mutex_unlock(&reg->lock);
		pthread_mutex_destroy(&reg->lock);
		pomlog(POMLOG_WARN "Module %s did not register anything", reg->name);
		free(reg->name);
		free(reg);
		return NULL;
	}

	pom_mutex_unlock(&reg->lock);

	pom_mutex_lock(&mod_reg_lock);

	reg->next = mod_reg_head;
	mod_reg_head = reg;
	if (reg->next)
		reg->next->prev = reg;

	pom_mutex_unlock(&mod_reg_lock);

	return reg;

}
Esempio n. 2
0
int mod_load(FAR struct mod_loadinfo_s *loadinfo)
{
  int ret;

  svdbg("loadinfo: %p\n", loadinfo);
  DEBUGASSERT(loadinfo && loadinfo->filfd >= 0);

  /* Load section headers into memory */

  ret = mod_loadshdrs(loadinfo);
  if (ret < 0)
    {
      sdbg("ERROR: mod_loadshdrs failed: %d\n", ret);
      goto errout_with_buffers;
    }

  /* Determine total size to allocate */

  mod_elfsize(loadinfo);

  /* Allocate (and zero) memory for the ELF file. */

  /* Allocate memory to hold the ELF image */

  loadinfo->textalloc = (uintptr_t)kmm_zalloc(loadinfo->textsize + loadinfo->datasize);
  if (!loadinfo->textalloc)
    {
      sdbg("ERROR: Failed to allocate memory for the module\n");
      ret = -ENOMEM;
      goto errout_with_buffers;
    }

  loadinfo->datastart = loadinfo->textalloc + loadinfo->textsize;

  /* Load ELF section data into memory */

  ret = mod_loadfile(loadinfo);
  if (ret < 0)
    {
      sdbg("ERROR: mod_loadfile failed: %d\n", ret);
      goto errout_with_buffers;
    }

  return OK;

  /* Error exits */

errout_with_buffers:
  mod_unload(loadinfo);
  return ret;
}
Esempio n. 3
0
int
svm_stop()
{
	int rval = RET_SUCCESS;
	int timeval = 0;
	int sleep_int = 5;

	metahalt();

	if ((rval = mod_unload(MD_MODULE)) != 0) {
		timeval += sleep_int;
		(void) sleep(sleep_int);
		while (timeval < MAX_TIMEOUT) {
			if ((rval = mod_unload(MD_MODULE)) == 0) {
				debug_printf("svm_stop(): mod_unload succeeded."
						" Time %d\n", timeval);

				break;
			}

			debug_printf("svm_stop(): mod_unload failed. Trying "
				"in  %d s (%d)\n", sleep_int, timeval);

			timeval += sleep_int;
			(void) sleep(sleep_int);
			metahalt();
		}

		if (rval != 0) {
			rval = RET_ERROR;
			debug_printf("svm_stop(): mod_unload FAILED!\n");
		}
	}

	return (rval);
}
Esempio n. 4
0
struct mod_reg *mod_load(char *name) {

	pom_mutex_lock(&mod_reg_lock);
	struct mod_reg *tmp;
	for (tmp = mod_reg_head; tmp && strcmp(tmp->name, name); tmp = tmp->next);
	if (tmp) {
		pom_mutex_unlock(&mod_reg_lock);
		pomlog(POMLOG_WARN "Module %s is already registered", name);
		return NULL;
	}
	pom_mutex_unlock(&mod_reg_lock);

	char filename[FILENAME_MAX];
	memset(filename, 0, FILENAME_MAX);

	char *env_libdir = getenv(MOD_LIBDIR_ENV_VAR);

	if (env_libdir)
		strcpy(filename, env_libdir);
	else
		strcpy(filename, POM_LIBDIR);

	if (filename[strlen(filename) - 1] != '/')
		strcat(filename, "/");

	strcat(filename, name);
	strcat(filename, POM_LIB_EXT);

	void *dl_handle = dlopen(filename, RTLD_FLAGS);

	if (!dl_handle) {
		pomlog(POMLOG_ERR "Unable to load module %s : %s", name, dlerror());
		return NULL;
	}

	dlerror();

	char func_name[FILENAME_MAX];
	strcpy(func_name, name);
	strcat(func_name, "_reg_info");

	struct mod_reg_info* (*mod_reg_func) () = NULL;
	mod_reg_func = dlsym(dl_handle, func_name);
	if (!mod_reg_func) {
		dlclose(dl_handle);
		pomlog(POMLOG_ERR "Function %s not found in module %s", func_name, filename);
		return NULL;
	}

	struct mod_reg_info *reg_info = mod_reg_func();
	if (!reg_info) {
		dlclose(dl_handle);
		pomlog(POMLOG_ERR "Function %s returned NULL", func_name);
		return NULL;
	}

	if (reg_info->api_ver != MOD_API_VER) {
		dlclose(dl_handle);
		pomlog(POMLOG_ERR "API version of module %s does not match : expected %u got %u", name, MOD_API_VER, reg_info->api_ver);
		return NULL;
	}

	if (mod_load_dependencies(reg_info->dependencies) != POM_OK) {
		dlclose(dl_handle);
		return NULL;
	}

	struct mod_reg *reg = malloc(sizeof(struct mod_reg));
	if (!reg) {
		dlclose(dl_handle);
		pomlog(POMLOG_ERR "Not enough memory to allocate struct mod_reg");
		return NULL;
	}

	memset(reg, 0, sizeof(struct mod_reg));

	reg->priv = dl_handle;
	reg->filename = strdup(filename);
	reg->name = strdup(name);
	reg->info = reg_info;

	if (!reg->filename || !reg->name || pthread_mutex_init(&reg->lock, NULL)) {
		if (reg->filename)
			free(reg->filename);
		if (reg->name)
			free(reg->name);
		free(reg);
		dlclose(dl_handle);

		pomlog(POMLOG_ERR "Not enough memory to allocate name and filename of struct mod_reg or failed to initialize the lock");

		return NULL;
	}

	pom_mutex_lock(&mod_reg_lock);

	reg->next = mod_reg_head;
	mod_reg_head = reg;
	if (reg->next)
		reg->next->prev = reg;

	pom_mutex_unlock(&mod_reg_lock);

	pomlog(POMLOG_DEBUG "Module %s loaded, registering components ...", reg->name);

	if (reg->info->register_func(reg) != POM_OK) {
		pomlog(POMLOG_WARN "Error while registering the components of module %s", reg->name);
		mod_unload(reg);
		return NULL;
	}

	pom_mutex_lock(&reg->lock);
	if (!reg->refcount) {
		pom_mutex_unlock(&reg->lock);
		pomlog(POMLOG_DEBUG "Module %s did not register anything. Unloading it", reg->name);
		mod_unload(reg);
		return NULL;
	}

	pom_mutex_unlock(&reg->lock);

	return reg;

}
Esempio n. 5
0
uint32_t __stdcall module_unload(uint8_t *init_data){
	module_init_return *init = (module_init_return*)init_data;
	module_unload_t mod_unload = (module_unload_t)init->exports->unload_module;
	return mod_unload((uint32_t*)init_data);
}
Esempio n. 6
0
int insmod(FAR const char *filename, FAR const char *modulename)
{
  struct mod_loadinfo_s loadinfo;
  FAR struct module_s *modp;
  mod_initializer_t initializer;
  int ret;

  DEBUGASSERT(filename != NULL && modulename != NULL);
  sinfo("Loading file: %s\n", filename);

  /* Get exclusive access to the module registry */

  mod_registry_lock();

  /* Check if this module is already installed */

  if (mod_registry_find(modulename) != NULL)
    {
      mod_registry_unlock();
      ret = -EEXIST;
      goto errout_with_lock;
    }

  /* Initialize the ELF library to load the program binary. */

  ret = mod_initialize(filename, &loadinfo);
  mod_dumploadinfo(&loadinfo);
  if (ret != 0)
    {
      serr("ERROR: Failed to initialize to load module: %d\n", ret);
      goto errout_with_lock;
    }

  /* Allocate a module registry entry to hold the module data */

  modp = (FAR struct module_s *)kmm_zalloc(sizeof(struct module_s));
  if (ret != 0)
    {
      sinfo("Failed to initialize for load of ELF program: %d\n", ret);
      goto errout_with_loadinfo;
    }

  /* Save the module name in the registry entry */

  strncpy(modp->modulename, modulename, MODULENAME_MAX);

  /* Load the program binary */

  ret = mod_load(&loadinfo);
  mod_dumploadinfo(&loadinfo);
  if (ret != 0)
    {
      sinfo("Failed to load ELF program binary: %d\n", ret);
      goto errout_with_registry_entry;
    }

  /* Bind the program to the kernel symbol table */

  ret = mod_bind(&loadinfo);
  if (ret != 0)
    {
      sinfo("Failed to bind symbols program binary: %d\n", ret);
      goto errout_with_load;
    }

  /* Return the load information */

  modp->alloc       = (FAR void *)loadinfo.textalloc;
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
  modp->textsize    = loadinfo.textsize;
  modp->datasize    = loadinfo.datasize;
#endif

  /* Get the module initializer entry point */

  initializer = (mod_initializer_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
  modp->initializer = initializer;
#endif
  mod_dumpinitializer(initializer, &loadinfo);

  /* Call the module initializer */

  ret = initializer(&modp->uninitializer, &modp->arg);
  if (ret < 0)
    {
      sinfo("Failed to initialize the module: %d\n", ret);
      goto errout_with_load;
    }

  /* Add the new module entry to the registry */

  mod_registry_add(modp);

  mod_uninitialize(&loadinfo);
  mod_registry_unlock();
  return OK;

errout_with_load:
  mod_unload(&loadinfo);
errout_with_registry_entry:
  kmm_free(modp);
errout_with_loadinfo:
  mod_uninitialize(&loadinfo);
errout_with_lock:
  mod_registry_unlock();
  set_errno(-ret);
  return ERROR;
}