Prechádzať zdrojové kódy

ncd/modules/command_template: improve interface, use a struct

ambrop7 15 rokov pred
rodič
commit
1caa56c976

+ 9 - 35
ncd/modules/command_template.c

@@ -32,20 +32,10 @@
 #define STATE_DELETING_LOCK 5
 #define STATE_DELETING 6
 
-struct instance {
-    NCDModuleInst *i;
-    command_template_build_cmdline build_cmdline;
-    int blog_channel;
-    BEventLockJob elock_job;
-    int state;
-    int have_process;
-    BProcess process;
-};
+static int start_process (command_template_instance *o, int remove);
+static void process_handler (command_template_instance *o, int normally, uint8_t normally_exit_status);
 
-static int start_process (struct instance *o, int remove);
-static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status);
-
-int start_process (struct instance *o, int remove)
+int start_process (command_template_instance *o, int remove)
 {
     int ret = 0;
     
@@ -72,7 +62,7 @@ fail0:
     return ret;
 }
 
-static void lock_handler (struct instance *o)
+static void lock_handler (command_template_instance *o)
 {
     ASSERT(o->state == STATE_ADDING_LOCK || o->state == STATE_DELETING_LOCK)
     ASSERT(!o->have_process)
@@ -92,7 +82,7 @@ static void lock_handler (struct instance *o)
     o->state = (remove ? STATE_DELETING : STATE_ADDING);
 }
 
-void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
+void process_handler (command_template_instance *o, int normally, uint8_t normally_exit_status)
 {
     ASSERT(o->have_process)
     ASSERT(o->state == STATE_ADDING || o->state == STATE_ADDING_NEED_DELETE || o->state == STATE_DELETING)
@@ -137,15 +127,8 @@ void process_handler (struct instance *o, int normally, uint8_t normally_exit_st
     }
 }
 
-void * command_template_new (NCDModuleInst *i, command_template_build_cmdline build_cmdline, int blog_channel, BEventLock *elock)
+int command_template_new (command_template_instance *o, NCDModuleInst *i, command_template_build_cmdline build_cmdline, int blog_channel, BEventLock *elock)
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    
     // init arguments
     o->i = i;
     o->build_cmdline = build_cmdline;
@@ -163,16 +146,11 @@ void * command_template_new (NCDModuleInst *i, command_template_build_cmdline bu
     // set state
     o->state = STATE_ADDING_LOCK;
     
-    return o;
-    
-fail0:
-    return NULL;
+    return 1;
 }
 
-void command_template_func_free (void *vo)
+void command_template_free (command_template_instance *o)
 {
-    struct instance *o = vo;
-    
     // free process
     if (o->have_process) {
         // kill process
@@ -184,14 +162,10 @@ void command_template_func_free (void *vo)
     
     // free lock job
     BEventLockJob_Free(&o->elock_job);
-    
-    // free instance
-    free(o);
 }
 
-void command_template_func_die (void *vo)
+void command_template_die (command_template_instance *o)
 {
-    struct instance *o = vo;
     ASSERT(o->state == STATE_ADDING_LOCK || o->state == STATE_ADDING || o->state == STATE_DONE)
     
     switch (o->state) {

+ 13 - 3
ncd/modules/command_template.h

@@ -36,8 +36,18 @@
 
 typedef int (*command_template_build_cmdline) (NCDModuleInst *i, int remove, char **exec, CmdLine *cl);
 
-void * command_template_new (NCDModuleInst *i, command_template_build_cmdline build_cmdline, int blog_channel, BEventLock *elock);
-void command_template_func_free (void *vo);
-void command_template_func_die (void *vo);
+typedef struct {
+    NCDModuleInst *i;
+    command_template_build_cmdline build_cmdline;
+    int blog_channel;
+    BEventLockJob elock_job;
+    int state;
+    int have_process;
+    BProcess process;
+} command_template_instance;
+
+int command_template_new (command_template_instance *o, NCDModuleInst *i, command_template_build_cmdline build_cmdline, int blog_channel, BEventLock *elock);
+void command_template_free (command_template_instance *o);
+void command_template_die (command_template_instance *o);
 
 #endif

+ 46 - 6
ncd/modules/net_iptables.c

@@ -42,6 +42,10 @@
 
 BEventLock iptables_lock;
 
+struct instance {
+    command_template_instance cti;
+};
+
 static int build_append_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
 {
     // read arguments
@@ -179,27 +183,63 @@ static void func_globalfree (void)
     BEventLock_Free(&iptables_lock);
 }
 
+static void * func_new (NCDModuleInst *i, command_template_build_cmdline build_cmdline)
+{
+    struct instance *o = malloc(sizeof(*o));
+    if (!o) {
+        BLog(BLOG_ERROR, "malloc failed");
+        goto fail0;
+    }
+    
+    if (!command_template_new(&o->cti, i, build_cmdline, BLOG_CURRENT_CHANNEL, &iptables_lock)) {
+        goto fail1;
+    }
+    
+    return o;
+    
+fail1:
+    free(o);
+fail0:
+    return NULL;
+}
+
 static void * append_func_new (NCDModuleInst *i)
 {
-    return command_template_new(i, build_append_cmdline, BLOG_CURRENT_CHANNEL, &iptables_lock);
+    return func_new(i, build_append_cmdline);
 }
 
 static void * policy_func_new (NCDModuleInst *i)
 {
-    return command_template_new(i, build_policy_cmdline, BLOG_CURRENT_CHANNEL, &iptables_lock);
+    return func_new(i, build_policy_cmdline);
+}
+
+static void func_free (void *vo)
+{
+    struct instance *o = vo;
+    
+    command_template_free(&o->cti);
+    
+    free(o);
+}
+
+static void func_die (void *vo)
+{
+    struct instance *o = vo;
+    
+    command_template_die(&o->cti);
 }
 
 static const struct NCDModule modules[] = {
     {
         .type = "net.iptables.append",
         .func_new = append_func_new,
-        .func_free = command_template_func_free,
-        .func_die = command_template_func_die
+        .func_free = func_free,
+        .func_die = func_die
     }, {
         .type = "net.iptables.policy",
         .func_new = policy_func_new,
-        .func_free = command_template_func_free,
-        .func_die = command_template_func_die
+        .func_free = func_free,
+        .func_die = func_die
     }, {
         .type = NULL
     }