int mod_registry_foreach(mod_callback_t callback, FAR void *arg) { FAR struct module_s *modp; int ret; /* Get exclusive access to the module registry */ mod_registry_lock(); /* Visit each installed module */ for (modp = g_mod_registry; modp != NULL; modp = modp->flink) { /* Perform the callback */ ret = callback(modp, arg); if (ret != 0) { return ret; } } mod_registry_unlock(); return OK; }
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; }