| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
*/ |
|---|
| 5 |
|
|---|
| 6 |
#include "noit_defines.h" |
|---|
| 7 |
|
|---|
| 8 |
#include <stdio.h> |
|---|
| 9 |
#include <dlfcn.h> |
|---|
| 10 |
|
|---|
| 11 |
#include "noit_module.h" |
|---|
| 12 |
#include "noit_conf.h" |
|---|
| 13 |
#include "utils/noit_hash.h" |
|---|
| 14 |
#include "utils/noit_log.h" |
|---|
| 15 |
|
|---|
| 16 |
struct __extended_module_data { |
|---|
| 17 |
void *userdata; |
|---|
| 18 |
}; |
|---|
| 19 |
|
|---|
| 20 |
static noit_hash_table modules = NOIT_HASH_EMPTY; |
|---|
| 21 |
|
|---|
| 22 |
int noit_module_load(const char *file, const char *name) { |
|---|
| 23 |
char module_file[PATH_MAX]; |
|---|
| 24 |
char *base; |
|---|
| 25 |
void *dlhandle; |
|---|
| 26 |
void *dlsymbol; |
|---|
| 27 |
noit_module_t *module; |
|---|
| 28 |
|
|---|
| 29 |
if(!noit_conf_get_string(NULL, "/noit/modules/@directory", &base)) |
|---|
| 30 |
base = strdup(""); |
|---|
| 31 |
|
|---|
| 32 |
if(file[0] == '/') |
|---|
| 33 |
strlcpy(module_file, file, sizeof(module_file)); |
|---|
| 34 |
else |
|---|
| 35 |
snprintf(module_file, sizeof(module_file), "%s/%s.%s", |
|---|
| 36 |
base, file, MODULEEXT); |
|---|
| 37 |
free(base); |
|---|
| 38 |
|
|---|
| 39 |
dlhandle = dlopen(module_file, RTLD_LAZY | RTLD_GLOBAL); |
|---|
| 40 |
if(!dlhandle) { |
|---|
| 41 |
noitL(noit_stderr, "Cannot open image '%s': %s\n", |
|---|
| 42 |
module_file, dlerror()); |
|---|
| 43 |
return -1; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
dlsymbol = dlsym(dlhandle, name); |
|---|
| 47 |
if(!dlsymbol) { |
|---|
| 48 |
noitL(noit_stderr, "Cannot find '%s' in image '%s': %s\n", |
|---|
| 49 |
name, module_file, dlerror()); |
|---|
| 50 |
dlclose(dlhandle); |
|---|
| 51 |
return -1; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
if(noit_module_validate_magic((noit_module_t *)dlsymbol) == -1) { |
|---|
| 55 |
noitL(noit_stderr, "I can't understand module %s\n", name); |
|---|
| 56 |
dlclose(dlhandle); |
|---|
| 57 |
return -1; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
module = calloc(1, sizeof(*module)); |
|---|
| 61 |
memcpy(module, dlsymbol, sizeof(*module)); |
|---|
| 62 |
module->opaque_handle = calloc(1, sizeof(struct __extended_module_data)); |
|---|
| 63 |
|
|---|
| 64 |
if(module->onload(module)) { |
|---|
| 65 |
free(module); |
|---|
| 66 |
return -1; |
|---|
| 67 |
} |
|---|
| 68 |
noit_hash_store(&modules, module->name, strlen(module->name), module); |
|---|
| 69 |
return 0; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
noit_module_t * noit_module_lookup(const char *name) { |
|---|
| 73 |
noit_module_t *module; |
|---|
| 74 |
|
|---|
| 75 |
if(noit_hash_retrieve(&modules, name, strlen(name), (void **)&module)) { |
|---|
| 76 |
return module; |
|---|
| 77 |
} |
|---|
| 78 |
return NULL; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
void noit_module_init() { |
|---|
| 82 |
noit_conf_section_t *sections; |
|---|
| 83 |
int i, cnt = 0; |
|---|
| 84 |
|
|---|
| 85 |
sections = noit_conf_get_sections(NULL, "/noit/modules//module", &cnt); |
|---|
| 86 |
if(!sections) return; |
|---|
| 87 |
for(i=0; i<cnt; i++) { |
|---|
| 88 |
noit_hash_table *config; |
|---|
| 89 |
noit_module_t *module; |
|---|
| 90 |
char module_file[PATH_MAX]; |
|---|
| 91 |
char module_name[256]; |
|---|
| 92 |
if(!noit_conf_get_stringbuf(sections[i], "ancestor-or-self::node()/@image", |
|---|
| 93 |
module_file, sizeof(module_file))) { |
|---|
| 94 |
noitL(noit_stderr, "No image defined in module stanza %d\n", i+1); |
|---|
| 95 |
continue; |
|---|
| 96 |
} |
|---|
| 97 |
if(!noit_conf_get_stringbuf(sections[i], "ancestor-or-self::node()/@name", |
|---|
| 98 |
module_name, sizeof(module_name))) { |
|---|
| 99 |
noitL(noit_stderr, "No name defined in module stanza %d\n", i+1); |
|---|
| 100 |
continue; |
|---|
| 101 |
} |
|---|
| 102 |
if(noit_module_load(module_file, module_name)) { |
|---|
| 103 |
noitL(noit_stderr, "Could not load %s:%s\n", module_file, module_name); |
|---|
| 104 |
continue; |
|---|
| 105 |
} |
|---|
| 106 |
config = noit_conf_get_hash(sections[i], "config"); |
|---|
| 107 |
module = noit_module_lookup(module_name); |
|---|
| 108 |
if(module->config && module->config(module, config)) { |
|---|
| 109 |
noitL(noit_stderr, |
|---|
| 110 |
"Configure failed on %s:%s\n", module_file, module_name); |
|---|
| 111 |
continue; |
|---|
| 112 |
} |
|---|
| 113 |
if(module->init && module->init(module)) { |
|---|
| 114 |
noitL(noit_stderr, |
|---|
| 115 |
"Initialized failed on %s:%s\n", module_file, module_name); |
|---|
| 116 |
continue; |
|---|
| 117 |
} |
|---|
| 118 |
noitL(noit_stderr, "Module %s:%s successfully loaded.\n", |
|---|
| 119 |
module_file, module_name); |
|---|
| 120 |
} |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
void *noit_module_get_userdata(noit_module_t *mod) { |
|---|
| 124 |
struct __extended_module_data *emd; |
|---|
| 125 |
emd = (struct __extended_module_data *)mod->opaque_handle; |
|---|
| 126 |
return emd->userdata; |
|---|
| 127 |
} |
|---|
| 128 |
void noit_module_set_userdata(noit_module_t *mod, void *newdata) { |
|---|
| 129 |
struct __extended_module_data *emd; |
|---|
| 130 |
emd = (struct __extended_module_data *)mod->opaque_handle; |
|---|
| 131 |
emd->userdata = newdata; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|