Changeset caa7b86e0a2913bf4f9e3254320a05dd636b2e06
- Timestamp:
- 01/26/08 20:48:28 (5 years ago)
- git-parent:
- Files:
-
- src/eventer/eventer.c (modified) (2 diffs)
- src/eventer/eventer.h (modified) (1 diff)
- src/noit_conf.c (modified) (8 diffs)
- src/noit_conf.h (modified) (2 diffs)
- src/noit_console.c (modified) (1 diff)
- src/noit_console.h (modified) (1 diff)
- src/noit_listener.c (modified) (4 diffs)
- src/noit_listener.h (modified) (1 diff)
- src/noit_module.c (modified) (1 diff)
- src/noit_module.h (modified) (2 diffs)
- src/noitd.c (modified) (3 diffs)
- src/sample.conf (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/eventer/eventer.c
r919b09a rcaa7b86 2 2 #include "eventer/eventer_impl.h" 3 3 #include "eventer/eventer_POSIX_fd_opset.h" 4 #include "utils/noit_hash.h" 4 5 5 6 eventer_t eventer_alloc() { … … 27 28 } 28 29 30 static noit_hash_table __name_to_func = NOIT_HASH_EMPTY; 31 static noit_hash_table __func_to_name = NOIT_HASH_EMPTY; 32 int eventer_name_callback(const char *name, eventer_func_t f) { 33 noit_hash_replace(&__name_to_func, strdup(name), strlen(name), f, free, NULL); 34 noit_hash_replace(&__func_to_name, (char *)f, sizeof(f), strdup(name), 35 NULL, free); 36 return 0; 37 } 38 eventer_func_t eventer_callback_for_name(const char *name) { 39 eventer_func_t f; 40 if(noit_hash_retrieve(&__name_to_func, name, strlen(name), (void **)&f)) 41 return f; 42 return (eventer_func_t)NULL; 43 } 44 const char *eventer_name_for_callback(eventer_func_t f) { 45 const char *name; 46 if(noit_hash_retrieve(&__func_to_name, (char *)f, sizeof(f), (void **)&name)) 47 return name; 48 return NULL; 49 } 50 29 51 int eventer_choose(const char *name) { 30 52 eventer_impl_t choice; src/eventer/eventer.h
rb62cf2b rcaa7b86 53 53 API_EXPORT(void) eventer_free(eventer_t); 54 54 API_EXPORT(int) eventer_timecompare(void *a, void *b); 55 API_EXPORT(int) eventer_name_callback(const char *name, eventer_func_t f); 56 API_EXPORT(const char *) 57 eventer_name_for_callback(eventer_func_t f); 58 API_EXPORT(eventer_func_t) 59 eventer_callback_for_name(const char *name); 55 60 56 61 typedef struct _eventer_impl { src/noit_conf.c
r0268e62 rcaa7b86 5 5 6 6 #include "noit_defines.h" 7 7 8 #include <stdio.h> 9 #include <assert.h> 10 #include <libxml/parser.h> 11 #include <libxml/tree.h> 12 #include <libxml/xpath.h> 13 8 14 #include "noit_conf.h" 9 15 #include "utils/noit_hash.h" … … 11 17 /* tmp hash impl, replace this with something nice */ 12 18 static noit_hash_table _tmp_config = NOIT_HASH_EMPTY; 13 19 static xmlDocPtr master_config = NULL; 20 static xmlXPathContextPtr xpath_ctxt = NULL; 14 21 15 22 static noit_hash_table _compiled_fallback = NOIT_HASH_EMPTY; … … 38 45 (void *)strdup(config_info[i].val)); 39 46 } 47 xmlInitParser(); 48 xmlXPathInit(); 40 49 } 41 50 42 51 int noit_conf_load(const char *path) { 52 xmlDocPtr new_config; 53 new_config = xmlParseFile(path); 54 if(new_config) { 55 if(master_config) xmlFreeDoc(master_config); 56 if(xpath_ctxt) xmlXPathFreeContext(xpath_ctxt); 57 58 master_config = new_config; 59 xpath_ctxt = xmlXPathNewContext(master_config); 60 return 0; 61 } 43 62 return -1; 44 63 } … … 47 66 } 48 67 49 int noit_conf_get_string(const char *path, char **value) { 50 char *str; 51 if(noit_hash_retrieve(&_tmp_config, 52 path, strlen(path), (void **)&str)) { 53 *value = strdup(str); 68 noit_conf_section_t noit_conf_get_section(noit_conf_section_t section, 69 const char *path) { 70 noit_conf_section_t subsection = NULL; 71 xmlXPathObjectPtr pobj; 72 xmlXPathContextPtr current_ctxt; 73 xmlNodePtr current_node = (xmlNodePtr)section; 74 75 current_ctxt = xpath_ctxt; 76 if(current_node) { 77 current_ctxt = xmlXPathNewContext(master_config); 78 current_ctxt->node = current_node; 79 } 80 pobj = xmlXPathEval((xmlChar *)path, current_ctxt); 81 if(!pobj) goto out; 82 if(pobj->type != XPATH_NODESET) goto out; 83 if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out; 84 subsection = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); 85 out: 86 if(current_ctxt && current_ctxt != xpath_ctxt) 87 xmlXPathFreeContext(current_ctxt); 88 return subsection; 89 } 90 noit_conf_section_t *noit_conf_get_sections(noit_conf_section_t section, 91 const char *path, 92 int *cnt) { 93 int i; 94 noit_conf_section_t *sections; 95 xmlXPathObjectPtr pobj; 96 xmlXPathContextPtr current_ctxt; 97 xmlNodePtr current_node = (xmlNodePtr)section; 98 99 *cnt = 0; 100 current_ctxt = xpath_ctxt; 101 if(current_node) { 102 current_ctxt = xmlXPathNewContext(master_config); 103 current_ctxt->node = current_node; 104 } 105 pobj = xmlXPathEval((xmlChar *)path, current_ctxt); 106 if(!pobj) goto out; 107 if(pobj->type != XPATH_NODESET) goto out; 108 if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out; 109 *cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); 110 sections = calloc(*cnt, sizeof(*sections)); 111 for(i=0; i<*cnt; i++) 112 sections[i] = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i); 113 out: 114 if(current_ctxt && current_ctxt != xpath_ctxt) 115 xmlXPathFreeContext(current_ctxt); 116 return sections; 117 } 118 int _noit_conf_get_string(noit_conf_section_t section, 119 const char *path, char **value) { 120 char *str; 121 int i; 122 xmlXPathObjectPtr pobj; 123 xmlXPathContextPtr current_ctxt; 124 xmlNodePtr current_node = (xmlNodePtr)section; 125 126 current_ctxt = xpath_ctxt; 127 if(current_node) { 128 current_ctxt = xmlXPathNewContext(master_config); 129 current_ctxt->node = current_node; 130 } 131 pobj = xmlXPathEval((xmlChar *)path, current_ctxt); 132 if(pobj) { 133 switch(pobj->type) { 134 case XPATH_NODESET: 135 if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) return 0; 136 i = xmlXPathNodeSetGetLength(pobj->nodesetval); 137 assert(i == 1); 138 *value = (char *)xmlXPathCastNodeSetToString(pobj->nodesetval); 139 break; 140 default: 141 *value = (char *)xmlXPathCastToString(pobj); 142 } 143 goto found; 54 144 } 55 145 if(noit_hash_retrieve(&_compiled_fallback, 56 146 path, strlen(path), (void **)&str)) { 147 *value = str; 148 goto found; 149 } 150 return 0; 151 found: 152 if(current_ctxt && current_ctxt != xpath_ctxt) 153 xmlXPathFreeContext(current_ctxt); 154 return 1; 155 } 156 int noit_conf_get_string(noit_conf_section_t section, 157 const char *path, char **value) { 158 char *str; 159 if(_noit_conf_get_string(section,path,&str)) { 57 160 *value = strdup(str); 58 } 59 return 0; 60 } 61 int noit_conf_set_string(const char *path, const char *value) { 161 return 1; 162 } 163 return 0; 164 } 165 int noit_conf_get_stringbuf(noit_conf_section_t section, 166 const char *path, char *buf, int len) { 167 char *str; 168 if(_noit_conf_get_string(section,path,&str)) { 169 strlcpy(buf, str, len); 170 return 1; 171 } 172 return 0; 173 } 174 int noit_conf_set_string(noit_conf_section_t section, 175 const char *path, const char *value) { 62 176 noit_hash_replace(&_tmp_config, 63 177 strdup(path), strlen(path), (void *)strdup(value), … … 65 179 return 1; 66 180 } 67 int noit_conf_get_int(const char *path, int *value) { 181 int noit_conf_get_int(noit_conf_section_t section, 182 const char *path, int *value) { 68 183 char *str; 69 184 long longval; 70 if(noit_conf_get_string( path,&str)) {185 if(noit_conf_get_string(section,path,&str)) { 71 186 int base = 10; 72 187 if(str[0] == '0') { … … 81 196 return 0; 82 197 } 83 int noit_conf_set_int(const char *path, int value) { 198 int noit_conf_set_int(noit_conf_section_t section, 199 const char *path, int value) { 84 200 char buffer[32]; 85 201 snprintf(buffer, 32, "%d", value); 86 return noit_conf_set_string(path, buffer); 87 } 88 int noit_conf_get_float(const char *path, float *value) { 89 char *str; 90 if(noit_conf_get_string(path, &str)) { 202 return noit_conf_set_string(section,path,buffer); 203 } 204 int noit_conf_get_float(noit_conf_section_t section, 205 const char *path, float *value) { 206 char *str; 207 if(noit_conf_get_string(section,path,&str)) { 91 208 *value = atof(str); 92 209 free(str); … … 95 212 return 0; 96 213 } 97 int noit_conf_set_float(const char *path, float value) { 214 int noit_conf_set_float(noit_conf_section_t section, 215 const char *path, float value) { 98 216 char buffer[32]; 99 217 snprintf(buffer, 32, "%f", value); 100 return noit_conf_set_string(path, buffer); 101 } 102 int noit_conf_get_boolean(const char *path, noit_conf_boolean *value) { 103 char *str; 104 if(noit_conf_get_string(path, &str)) { 105 if(!strcasecmp(str, "true")) *value = true; 106 else *value = false; 218 return noit_conf_set_string(section,path,buffer); 219 } 220 int noit_conf_get_boolean(noit_conf_section_t section, 221 const char *path, noit_conf_boolean *value) { 222 char *str; 223 if(noit_conf_get_string(section,path,&str)) { 224 if(!strcasecmp(str, "true")) *value = noit_true; 225 else *value = noit_false; 107 226 free(str); 108 227 return 1; … … 110 229 return 0; 111 230 } 112 int noit_conf_set_boolean(const char *path, noit_conf_boolean value) { 113 if(value == true) 114 return noit_conf_set_string(path, "true"); 115 return noit_conf_set_string(path, "false"); 116 } 117 231 int noit_conf_set_boolean(noit_conf_section_t section, 232 const char *path, noit_conf_boolean value) { 233 if(value == noit_true) 234 return noit_conf_set_string(section,path,"true"); 235 return noit_conf_set_string(section,path,"false"); 236 } 237 src/noit_conf.h
r0268e62 rcaa7b86 9 9 #include "noit_defines.h" 10 10 11 typedef enum { true, false } noit_conf_boolean; 11 typedef enum { noit_true, noit_false } noit_conf_boolean; 12 typedef void * noit_conf_section_t; 12 13 13 14 API_EXPORT(void) noit_conf_init(); … … 15 16 API_EXPORT(int) noit_conf_save(const char *path); 16 17 17 API_EXPORT(int) noit_conf_get_string(const char *path, char **value); 18 API_EXPORT(int) noit_conf_get_int(const char *path, int *value); 19 API_EXPORT(int) noit_conf_get_float(const char *path, float *value); 20 API_EXPORT(int) noit_conf_get_boolean(const char *path, noit_conf_boolean *value); 18 API_EXPORT(noit_conf_section_t) 19 noit_conf_get_section(noit_conf_section_t section, const char *path); 20 API_EXPORT(noit_conf_section_t *) 21 noit_conf_get_sections(noit_conf_section_t section, const char *path, 22 int *cnt); 21 23 22 API_EXPORT(int) noit_conf_set_string(const char *path, const char *value); 23 API_EXPORT(int) noit_conf_set_int(const char *path, int value); 24 API_EXPORT(int) noit_conf_set_float(const char *path, float value); 25 API_EXPORT(int) noit_conf_set_boolean(const char *path, noit_conf_boolean value); 24 API_EXPORT(int) noit_conf_get_string(noit_conf_section_t section, 25 const char *path, char **value); 26 API_EXPORT(int) noit_conf_get_stringbuf(noit_conf_section_t section, 27 const char *path, char *value, int len); 28 API_EXPORT(int) noit_conf_get_int(noit_conf_section_t section, 29 const char *path, int *value); 30 API_EXPORT(int) noit_conf_get_float(noit_conf_section_t section, 31 const char *path, float *value); 32 API_EXPORT(int) noit_conf_get_boolean(noit_conf_section_t section, 33 const char *path, noit_conf_boolean *value); 34 35 API_EXPORT(int) noit_conf_set_string(noit_conf_section_t section, 36 const char *path, const char *value); 37 API_EXPORT(int) noit_conf_set_int(noit_conf_section_t section, 38 const char *path, int value); 39 API_EXPORT(int) noit_conf_set_float(noit_conf_section_t section, 40 const char *path, float value); 41 API_EXPORT(int) noit_conf_set_boolean(noit_conf_section_t section, 42 const char *path, noit_conf_boolean value); 26 43 27 44 #endif src/noit_console.c
r7629c91 rcaa7b86 9 9 #include <unistd.h> 10 10 11 #include "eventer/eventer.h" 11 12 #include "utils/noit_log.h" 12 13 #include "noit_listener.h" 13 14 #include "noit_poller.h" 15 #include "noit_console.h" 16 17 void 18 noit_console_init() { 19 eventer_name_callback("noit_console", noit_console_handler); 20 } 14 21 15 22 int src/noit_console.h
rf41090d rcaa7b86 10 10 #include "eventer/eventer.h" 11 11 12 int 13 noit_console_handler(eventer_t e, int mask, void *closure, 14 struct timeval *now); 12 API_EXPORT(void) noit_console_init(); 13 14 API_EXPORT(int) 15 noit_console_handler(eventer_t e, int mask, void *closure, 16 struct timeval *now); 15 17 16 18 #endif src/noit_listener.c
r7212f9d rcaa7b86 17 17 #include "utils/noit_log.h" 18 18 #include "noit_listener.h" 19 #include "noit_conf.h" 19 20 20 21 static int … … 71 72 struct sockaddr_in6 addr6; 72 73 } s; 74 const char *event_name; 73 75 76 noit_log(noit_debug, NULL, "noit_listener(%s, %d, %d, %d, %s, %p)\n", 77 host, port, type, backlog, 78 (event_name = eventer_name_for_callback(handler))?event_name:"??", 79 closure); 74 80 family = AF_INET; 75 81 rv = inet_pton(family, host, &a); … … 78 84 rv = inet_pton(family, host, &a); 79 85 if(rv != 1) { 80 noit_log(noit_stderr, NULL, "Cannot translate '%s' to IP\n", host); 81 return -1; 86 if(!strcmp(host, "*")) { 87 family = AF_INET; 88 a.addr4.s_addr = INADDR_ANY; 89 } else { 90 noit_log(noit_stderr, NULL, "Cannot translate '%s' to IP\n", host); 91 return -1; 92 } 82 93 } 83 94 } … … 133 144 return 0; 134 145 } 146 147 void 148 noit_listener_init() { 149 int i, cnt = 0; 150 noit_conf_section_t *listener_configs; 151 152 listener_configs = noit_conf_get_sections(NULL, "/global/listeners/listener", 153 &cnt); 154 noit_log(noit_stderr, NULL, "Found %d /global/listeners/listener stanzas\n", 155 cnt); 156 for(i=0; i<cnt; i++) { 157 char address[256]; 158 char type[256]; 159 unsigned short port; 160 int portint; 161 int backlog; 162 eventer_func_t f; 163 164 if(!noit_conf_get_stringbuf(listener_configs[i], 165 "type", type, sizeof(type))) { 166 noit_log(noit_stderr, NULL, 167 "No type specified in listener stanza %d\n", i+1); 168 continue; 169 } 170 f = eventer_callback_for_name(type); 171 if(!f) { 172 noit_log(noit_stderr, NULL, 173 "Cannot find handler for listener type: '%s'\n", type); 174 continue; 175 } 176 if(!noit_conf_get_int(listener_configs[i], "port", &portint)) 177 portint = 0; 178 port = (unsigned short) portint; 179 if(portint == 0 || (port != portint)) { 180 noit_log(noit_stderr, NULL, 181 "Invalid port [%d] specified in stanza %d\n", port, i+1); 182 continue; 183 } 184 if(!noit_conf_get_stringbuf(listener_configs[i], 185 "address", address, sizeof(address))) { 186 address[0] = '*'; 187 address[1] = '\0'; 188 } 189 if(!noit_conf_get_int(listener_configs[i], "backlog", &backlog)) 190 backlog = 5; 191 192 noit_listener(address, port, SOCK_STREAM, backlog, f, NULL); 193 } 194 } src/noit_listener.h
rf41090d rcaa7b86 17 17 } * listener_closure_t; 18 18 19 int 19 API_EXPORT(void) noit_listener_init(); 20 21 API_EXPORT(int) 20 22 noit_listener(char *host, unsigned short port, int type, 21 23 int backlog, eventer_func_t handler, void *closure); src/noit_module.c
r0268e62 rcaa7b86 5 5 6 6 #include "noit_defines.h" 7 8 #include <stdio.h> 9 #include <dlfcn.h> 10 7 11 #include "noit_module.h" 12 #include "noit_conf.h" 8 13 #include "utils/noit_hash.h" 14 #include "utils/noit_log.h" 9 15 10 16 static noit_hash_table modules = NOIT_HASH_EMPTY; 11 17 12 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; 13 59 } 60 14 61 noit_module_t * noit_module_lookup(const char *name) { 15 62 noit_module_t *module; src/noit_module.h
r0268e62 rcaa7b86 11 11 #include "noit_poller.h" 12 12 13 #define NOIT_MODULE_MAGIC 0x4017DA7A 14 13 15 typedef struct { 16 uint32_t magic; 17 uint32_t version; 14 18 char *name; 15 19 char *description; … … 20 24 } noit_module_t; 21 25 26 #define MODULE_MAGIC(a) ((a)->magic) 27 #define MODULE_VERSION(a) ((a)->version) 28 29 #define noit_module_validate_magic(a) \ 30 ((MODULE_MAGIC(a) == NOIT_MODULE_MAGIC)?0:-1) 31 22 32 API_EXPORT(int) 23 33 noit_module_load(const char *file, const char *name); src/noitd.c
r2c65def rcaa7b86 8 8 #include <fcntl.h> 9 9 10 #include "getopt_long.h"11 10 #include "eventer/eventer.h" 12 11 #include "utils/noit_log.h" … … 35 34 36 35 int main(int argc, char **argv) { 36 char conf_str[1024]; 37 37 parse_clargs(argc, argv); 38 38 39 /* First initialize logging, so we can log errors */ 39 40 noit_log_init(); 40 41 if(debug) … … 42 43 noit_log_stream_add_stream(noit_error, noit_stderr); 43 44 45 /* Next load the configs */ 44 46 noit_conf_init(); 45 47 if(noit_conf_load(config_file) == -1) { 46 48 fprintf(stderr, "Cannot load config: '%s'\n", config_file); 47 49 } 48 if(eventer_choose("kqueue") == -1) { 49 fprintf(stderr, "Cannot choose kqueue\n"); 50 51 /* Lastly, run through all other system inits */ 52 if(!noit_conf_get_stringbuf(NULL, "/global/eventer/implementation", 53 conf_str, sizeof(conf_str))) { 54 noit_log(noit_stderr, NULL, "Cannot find '%s' in configuration\n", 55 "/global/eventer/implementation"); 56 exit(-1); 57 } 58 if(eventer_choose(conf_str) == -1) { 59 noit_log(noit_stderr, NULL, "Cannot choose eventer %s\n", conf_str); 50 60 exit(-1); 51 61 } 52 62 if(eventer_init() == -1) { 53 fprintf(stderr, "Cannot init kqueue\n");63 noit_log(noit_stderr, NULL, "Cannot init eventer %s\n", conf_str); 54 64 exit(-1); 55 65 } 66 noit_console_init(); 56 67 57 noit_listener("127.0.0.1", 23123, SOCK_STREAM, 5, noit_console_handler, NULL); 68 noit_listener_init(); 69 58 70 eventer_loop(); 59 71 return 0;
