| 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 |
static noit_hash_table modules = NOIT_HASH_EMPTY; |
|---|
| 17 |
|
|---|
| 18 |
int noit_module_load(const char *file, const char *name) { |
|---|
| 19 |
char module_file[PATH_MAX]; |
|---|
| 20 |
char *base; |
|---|
| 21 |
void *dlhandle; |
|---|
| 22 |
void *dlsymbol; |
|---|
| 23 |
noit_module_t *module; |
|---|
| 24 |
|
|---|
| 25 |
if(!noit_conf_get_string("/global/modules/directory", &base)) |
|---|
| 26 |
base = strdup(""); |
|---|
| 27 |
|
|---|
| 28 |
if(file[0] == '/') |
|---|
| 29 |
strlcpy(module_file, file, sizeof(module_file)); |
|---|
| 30 |
else |
|---|
| 31 |
snprintf(module_file, sizeof(module_file), "%s/%s.%s", |
|---|
| 32 |
base, name, MODULEEXT); |
|---|
| 33 |
free(base); |
|---|
| 34 |
|
|---|
| 35 |
dlhandle = dlopen(module_file, RTLD_LAZY | RTLD_GLOBAL); |
|---|
| 36 |
if(!dlhandle) { |
|---|
| 37 |
noit_log(noit_stderr, NULL, "Cannot open image '%s': %s\n", |
|---|
| 38 |
module_file, dlerror()); |
|---|
| 39 |
return -1; |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
dlsymbol = dlsym(dlhandle, name); |
|---|
| 43 |
if(!dlsymbol) { |
|---|
| 44 |
noit_log(noit_stderr, NULL, "Cannot find '%s' in image '%s': %s\n", |
|---|
| 45 |
name, module_file, dlerror()); |
|---|
| 46 |
dlclose(dlhandle); |
|---|
| 47 |
return -1; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
module = (noit_module_t *)dlsymbol; |
|---|
| 51 |
if(noit_module_validate_magic(module) == -1) { |
|---|
| 52 |
noit_log(noit_stderr, NULL, "I can't understand module %s\n", name); |
|---|
| 53 |
dlclose(dlhandle); |
|---|
| 54 |
return -1; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
noit_hash_store(&modules, module->name, strlen(module->name), module); |
|---|
| 58 |
return 0; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
noit_module_t * noit_module_lookup(const char *name) { |
|---|
| 62 |
noit_module_t *module; |
|---|
| 63 |
|
|---|
| 64 |
if(noit_hash_retrieve(&modules, name, strlen(name), (void **)&module)) { |
|---|
| 65 |
return module; |
|---|
| 66 |
} |
|---|
| 67 |
return NULL; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|