END_TEST START_TEST (module_load_cmdtab_test) { int res; module m; cmdtable cmdtab[] = { { CMD, C_RETR, G_READ, NULL, TRUE, FALSE, CL_READ }, { HOOK, "foo", G_READ, NULL, FALSE, FALSE }, { 0, NULL } }; res = pr_module_load_cmdtab(NULL); fail_unless(res < 0, "Failed to handle null arguments"); fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, strerror(errno), errno); memset(&m, 0, sizeof(m)); res = pr_module_load_cmdtab(&m); fail_unless(res < 0, "Failed to handle null module name"); fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, strerror(errno), errno); m.name = "testsuite"; res = pr_module_load_cmdtab(&m); fail_unless(res == 0, "Failed to load module cmdtab: %s", strerror(errno)); pr_module_unload(&m); fail_unless(res == 0, "Failed to unload module: %s", strerror(errno)); m.name = "testsuite"; m.cmdtable = cmdtab; res = pr_module_load_cmdtab(&m); fail_unless(res == 0, "Failed to load module cmdtab: %s", strerror(errno)); pr_module_unload(&m); fail_unless(res == 0, "Failed to unload module: %s", strerror(errno)); }
int pr_module_load(module *m) { char buf[256]; if (m == NULL || m->name == NULL) { errno = EINVAL; return -1; } /* Check the API version the module wants to use. */ if (m->api_version < PR_MODULE_API_VERSION) { errno = EACCES; return -1; } /* Do not allow multiple modules with the same name. */ memset(buf, '\0', sizeof(buf)); snprintf(buf, sizeof(buf), "mod_%s.c", m->name); buf[sizeof(buf)-1] = '\0'; if (pr_module_get(buf) != NULL) { errno = EEXIST; return -1; } /* Invoke the module's initialization routine. */ if (!m->init || m->init() >= 0) { /* Assign a priority to this module. */ m->priority = curr_module_pri++; /* Add the module's config, cmd, and auth tables. */ if (pr_module_load_conftab(m) < 0) { return -1; } if (pr_module_load_cmdtab(m) < 0) { return -1; } if (pr_module_load_authtab(m) < 0) { return -1; } /* Add the module to the loaded_modules list. */ if (loaded_modules) { m->next = loaded_modules; loaded_modules->prev = m; } loaded_modules = m; /* Generate an event. */ pr_event_generate("core.module-load", buf); return 0; } errno = EPERM; return -1; }