Changeset fc9516a2d8a7585aaaffbaab77009199ca907c90
- Timestamp:
- 06/05/08 14:46:45
(5 years ago)
- Author:
- Theo Schlossnagle <jesus@omniti.com>
- git-committer:
- Theo Schlossnagle <jesus@omniti.com> 1212677205 +0000
- git-parent:
[483ffbb2a71b31651cb48c74c7436508890169f4]
- git-author:
- Theo Schlossnagle <jesus@omniti.com> 1212677205 +0000
- Message:
make loaders have configs, add a directory config to lua for pulling scripts, refs #28
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r483ffbb |
rfc9516a |
|
| 18 | 18 | static noit_log_stream_t nldeb = NULL; |
|---|
| 19 | 19 | static noit_hash_table noit_coros = NOIT_HASH_EMPTY; |
|---|
| | 20 | |
|---|
| | 21 | struct loader_conf { |
|---|
| | 22 | char *script_dir; |
|---|
| | 23 | }; |
|---|
| | 24 | static struct loader_conf *__get_loader_conf(noit_module_loader_t *self) { |
|---|
| | 25 | struct loader_conf *c; |
|---|
| | 26 | c = noit_image_get_userdata(&self->hdr); |
|---|
| | 27 | if(!c) { |
|---|
| | 28 | c = calloc(1, sizeof(*c)); |
|---|
| | 29 | noit_image_set_userdata(&self->hdr, c); |
|---|
| | 30 | } |
|---|
| | 31 | return c; |
|---|
| | 32 | } |
|---|
| | 33 | static void |
|---|
| | 34 | noit_lua_loader_set_directory(noit_module_loader_t *self, char *dir) { |
|---|
| | 35 | struct loader_conf *c = __get_loader_conf(self); |
|---|
| | 36 | if(c->script_dir) free(c->script_dir); |
|---|
| | 37 | c->script_dir = strdup(dir); |
|---|
| | 38 | } |
|---|
| | 39 | static const char * |
|---|
| | 40 | noit_lua_loader_get_directory(noit_module_loader_t *self) { |
|---|
| | 41 | struct loader_conf *c = __get_loader_conf(self); |
|---|
| | 42 | return c->script_dir; |
|---|
| | 43 | } |
|---|
| 20 | 44 | |
|---|
| 21 | 45 | noit_lua_check_info_t * |
|---|
| … | … | |
| 556 | 580 | } |
|---|
| 557 | 581 | static noit_module_t * |
|---|
| 558 | | noit_lua_load(noit_module_loader_t *loader, |
|---|
| 559 | | char *module_name, |
|---|
| 560 | | noit_conf_section_t section) { |
|---|
| | 582 | noit_lua_loader_load(noit_module_loader_t *loader, |
|---|
| | 583 | char *module_name, |
|---|
| | 584 | noit_conf_section_t section) { |
|---|
| 561 | 585 | noit_module_t *m; |
|---|
| 562 | 586 | lua_State *L; |
|---|
| … | … | |
| 564 | 588 | char *script; |
|---|
| 565 | 589 | char *object; |
|---|
| | 590 | char scriptfile[MAXPATHLEN]; |
|---|
| 566 | 591 | |
|---|
| 567 | 592 | noitL(nldeb, "Loading lua module: %s\n", module_name); |
|---|
| … | … | |
| 575 | 600 | return NULL; |
|---|
| 576 | 601 | } |
|---|
| | 602 | snprintf(scriptfile, sizeof(scriptfile), "%s/%s", |
|---|
| | 603 | noit_lua_loader_get_directory(loader), script); |
|---|
| | 604 | free(script); |
|---|
| 577 | 605 | |
|---|
| 578 | 606 | m = noit_blank_module(); |
|---|
| … | … | |
| 584 | 612 | lmc = calloc(1, sizeof(*lmc)); |
|---|
| 585 | 613 | lmc->object = object; |
|---|
| 586 | | lmc->script = script; |
|---|
| | 614 | lmc->script = strdup(scriptfile); |
|---|
| 587 | 615 | |
|---|
| 588 | 616 | L = lmc->lua_state = lua_open(); |
|---|
| … | … | |
| 616 | 644 | |
|---|
| 617 | 645 | static int |
|---|
| | 646 | noit_lua_loader_config(noit_module_loader_t *self, noit_hash_table *o) { |
|---|
| | 647 | char *dir = "."; |
|---|
| | 648 | noit_hash_retrieve(o, "directory", strlen("directory"), (void **)&dir); |
|---|
| | 649 | noit_lua_loader_set_directory(self, dir); |
|---|
| | 650 | return 0; |
|---|
| | 651 | } |
|---|
| | 652 | static int |
|---|
| 618 | 653 | noit_lua_loader_onload(noit_image_t *self) { |
|---|
| 619 | 654 | nlerr = noit_log_stream_find("error/serf"); |
|---|
| … | … | |
| 632 | 667 | noit_lua_loader_onload, |
|---|
| 633 | 668 | }, |
|---|
| 634 | | noit_lua_load |
|---|
| | 669 | noit_lua_loader_config, |
|---|
| | 670 | NULL, |
|---|
| | 671 | noit_lua_loader_load |
|---|
| 635 | 672 | }; |
|---|
| r5bf243f |
rfc9516a |
|
| 27 | 27 | NULL |
|---|
| 28 | 28 | }, |
|---|
| | 29 | NULL, |
|---|
| | 30 | NULL, |
|---|
| 29 | 31 | noit_load_module_image |
|---|
| 30 | 32 | }; |
|---|
| … | … | |
| 191 | 193 | continue; |
|---|
| 192 | 194 | } |
|---|
| | 195 | if(loader->config) { |
|---|
| | 196 | int rv; |
|---|
| | 197 | noit_hash_table *config; |
|---|
| | 198 | config = noit_conf_get_hash(sections[i], "config"); |
|---|
| | 199 | rv = loader->config(loader, config); |
|---|
| | 200 | if(rv == 0) { |
|---|
| | 201 | noit_hash_destroy(config, free, free); |
|---|
| | 202 | free(config); |
|---|
| | 203 | } |
|---|
| | 204 | else if(rv < 0) { |
|---|
| | 205 | noitL(noit_stderr, "Failed to config loader %s\n", loader_name); |
|---|
| | 206 | continue; |
|---|
| | 207 | } |
|---|
| | 208 | } |
|---|
| | 209 | if(loader->init && loader->init(loader)) |
|---|
| | 210 | noitL(noit_stderr, "Failed to init loader %s\n", loader_name); |
|---|
| 193 | 211 | } |
|---|
| 194 | 212 | if(sections) free(sections); |
|---|
| r5bf243f |
rfc9516a |
|
| 12 | 12 | |
|---|
| 13 | 13 | #define NOIT_LOADER_MAGIC 0xA7AD7104 |
|---|
| 14 | | #define NOIT_LOADER_ABI_VERSION 2 |
|---|
| | 14 | #define NOIT_LOADER_ABI_VERSION 3 |
|---|
| 15 | 15 | |
|---|
| 16 | 16 | typedef struct _noit_image { |
|---|
| … | … | |
| 23 | 23 | } noit_image_t; |
|---|
| 24 | 24 | |
|---|
| | 25 | /* onload: is called immediately after the module is loaded and before it |
|---|
| | 26 | * is configured. |
|---|
| | 27 | */ |
|---|
| | 28 | |
|---|
| 25 | 29 | typedef struct _noit_module_loader { |
|---|
| 26 | 30 | noit_image_t hdr; |
|---|
| | 31 | int (*config)(struct _noit_module_loader *, noit_hash_table *config); |
|---|
| | 32 | int (*init)(struct _noit_module_loader *); |
|---|
| 27 | 33 | struct _noit_module *(*load)(struct _noit_module_loader *loader, |
|---|
| 28 | 34 | char *module_name, |
|---|
| 29 | 35 | noit_conf_section_t section); |
|---|
| 30 | 36 | } noit_module_loader_t; |
|---|
| | 37 | |
|---|
| | 38 | /* config: is called once to configure the loader itself |
|---|
| | 39 | * init: is called once, post config to initialize the module |
|---|
| | 40 | * load: is called each time the loader is asked to load a module |
|---|
| | 41 | */ |
|---|
| 31 | 42 | |
|---|
| 32 | 43 | #define NOIT_MODULE_MAGIC 0x4017DA7A |
|---|
| … | … | |
| 41 | 52 | void (*cleanup)(struct _noit_module *, noit_check_t *); |
|---|
| 42 | 53 | } noit_module_t; |
|---|
| | 54 | |
|---|
| | 55 | /* config: is called to pass the config into the module. |
|---|
| | 56 | * init: is called once to initialize the module |
|---|
| | 57 | * initiate_check: is called so start the module against checks |
|---|
| | 58 | * cleanup: is called if a particular check is stopped |
|---|
| | 59 | */ |
|---|
| 43 | 60 | |
|---|
| 44 | 61 | #define NOIT_IMAGE_MAGIC(a) ((a)->magic) |
|---|