root/src/noit_module.c

Revision b09f3a7643ce0f65ba32059bec4c7971b369b03c, 3.7 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

I can ping things...whahahahahaha

  • Property mode set to 100644
Line 
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, name, MODULEEXT);
37   free(base);
38
39   dlhandle = dlopen(module_file, RTLD_LAZY | RTLD_GLOBAL);
40   if(!dlhandle) {
41     noit_log(noit_stderr, NULL, "Cannot open image '%s': %s\n",
42              module_file, dlerror());
43     return -1;
44   }
45
46   dlsymbol = dlsym(dlhandle, name);
47   if(!dlsymbol) {
48     noit_log(noit_stderr, NULL, "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     noit_log(noit_stderr, NULL, "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], "image",
93                                 module_file, sizeof(module_file))) {
94       noit_log(noit_stderr, NULL,
95                "No image defined in module stanza %d\n", i+1);
96       continue;
97     }
98     if(!noit_conf_get_stringbuf(sections[i], "name",
99                                 module_name, sizeof(module_name))) {
100       noit_log(noit_stderr, NULL,
101                "No name defined in module stanza %d\n", i+1);
102       continue;
103     }
104     if(noit_module_load(module_file, module_name)) {
105       noit_log(noit_stderr, NULL,
106                "Could not load %s:%s\n", module_file, module_name);
107       continue;
108     }
109     config = noit_conf_get_hash(sections[i], "config/*");
110     module = noit_module_lookup(module_name);
111     if(module->config(module, config)) {
112       noit_log(noit_stderr, NULL,
113                "Configure failed on %s:%s\n", module_file, module_name);
114       continue;
115     }
116     if(module->init(module)) {
117       noit_log(noit_stderr, NULL,
118                "Initialized failed on %s:%s\n", module_file, module_name);
119       continue;
120     }
121     noit_log(noit_stderr, NULL, "Module %s:%s successfully loaded.\n",
122              module_file, module_name);
123   }
124 }
125
126 void *noit_module_get_userdata(noit_module_t *mod) {
127   struct __extended_module_data *emd;
128   emd = (struct __extended_module_data *)mod->opaque_handle;
129   return emd->userdata;
130 }
131 void noit_module_set_userdata(noit_module_t *mod, void *newdata) {
132   struct __extended_module_data *emd;
133   emd = (struct __extended_module_data *)mod->opaque_handle;
134   emd->userdata = newdata;
135 }
136
Note: See TracBrowser for help on using the browser.