瀏覽代碼

ncd: pass BReactor and BProcessManager in module func_globalinit, add func_globalfree

ambrop7 15 年之前
父節點
當前提交
fe46c08569
共有 4 個文件被更改,包括 27 次插入4 次删除
  1. 8 1
      ncd/NCDModule.h
  2. 1 1
      ncd/modules/depend.c
  3. 1 1
      ncd/modules/net_dns.c
  4. 17 1
      ncd/ncd.c

+ 8 - 1
ncd/NCDModule.h

@@ -46,6 +46,11 @@ typedef int (*NCDModule_handler_getvar) (void *user, const char *modname, const
 
 struct NCDModule;
 
+struct NCDModuleInitParams {
+    BReactor *reactor;
+    BProcessManager *manager;
+};
+
 typedef struct {
     const char *name;
     const struct NCDModule *m;
@@ -77,7 +82,8 @@ void NCDModuleInst_Backend_Died (NCDModuleInst *n, int is_error);
 int NCDModuleInst_Backend_GetVar (NCDModuleInst *n, const char *modname, const char *varname, NCDValue *out);
 void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
 
-typedef int (*NCDModule_func_globalinit) (void);
+typedef int (*NCDModule_func_globalinit) (const struct NCDModuleInitParams params);
+typedef void (*NCDModule_func_globalfree) (void);
 typedef void * (*NCDModule_func_new) (NCDModuleInst *params);
 typedef void (*NCDModule_func_free) (void *o);
 typedef void (*NCDModule_func_die) (void *o);
@@ -95,6 +101,7 @@ struct NCDModule {
 
 struct NCDModuleGroup {
     NCDModule_func_globalinit func_globalinit;
+    NCDModule_func_globalfree func_globalfree;
     const struct NCDModule *modules;
 };
 

+ 1 - 1
ncd/modules/depend.c

@@ -74,7 +74,7 @@ static struct provide * find_provide (const char *name)
     return NULL;
 }
 
-static int func_globalinit (void)
+static int func_globalinit (struct NCDModuleInitParams params)
 {
     // init provides list
     LinkedList2_Init(&provides);

+ 1 - 1
ncd/modules/net_dns.c

@@ -173,7 +173,7 @@ static int set_servers (void)
     return 1;
 }
 
-static int func_globalinit (void)
+static int func_globalinit (struct NCDModuleInitParams params)
 {
     LinkedList2_Init(&instances);
     

+ 17 - 1
ncd/ncd.c

@@ -270,12 +270,20 @@ int main (int argc, char **argv)
     // fee config file memory
     free(file);
     
+    // init module params
+    struct NCDModuleInitParams params = {
+        .reactor = &ss,
+        .manager = &manager
+    };
+    
     // init modules
+    size_t num_inited_modules = 0;
     for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
-        if ((*g)->func_globalinit && !(*g)->func_globalinit()) {
+        if ((*g)->func_globalinit && !(*g)->func_globalinit(params)) {
             BLog(BLOG_ERROR, "globalinit failed for some module");
             goto fail5;
         }
+        num_inited_modules++;
     }
     
     // init processes list
@@ -308,6 +316,14 @@ int main (int argc, char **argv)
     // free init job
     BPending_Free(&init_job);
 fail5:
+    // free modules
+    while (num_inited_modules > 0) {
+        const struct NCDModuleGroup **g = &ncd_modules[num_inited_modules - 1];
+        if ((*g)->func_globalfree) {
+            (*g)->func_globalfree();
+        }
+        num_inited_modules--;
+    }
     // free configuration
     NCDConfig_free_interfaces(configuration);
 fail3: