trigger: reduce indentation level in trigger_event()
[project/procd.git] / service / trigger.c
index b7bdbc5..4cfea31 100644 (file)
@@ -16,9 +16,6 @@
 #include <sys/socket.h>
 #include <sys/types.h>
 
-#include <linux/types.h>
-#include <linux/netlink.h>
-
 #include <libubox/blobmsg_json.h>
 #include <libubox/json_script.h>
 #include <libubox/runqueue.h>
@@ -77,6 +74,9 @@ rule_load_script(struct json_script_ctx *ctx, const char *name)
 {
        struct trigger *t = container_of(ctx, struct trigger, jctx);
 
+       if (strcmp(name, t->type) != 0)
+               return NULL;
+
        return json_script_file_from_blobmsg(t->type, t->rule, blob_pad_len(t->rule));
 }
 
@@ -88,13 +88,21 @@ static void q_job_run(struct runqueue *q, struct runqueue_task *t)
        j->cmd->handler(j, j->exec, j->env);
 }
 
+static void trigger_free(struct trigger *t)
+{
+       json_script_free(&t->jctx);
+       uloop_timeout_cancel(&t->delay);
+       free(t->data);
+       list_del(&t->list);
+       free(t);
+}
+
 static void q_job_complete(struct runqueue *q, struct runqueue_task *p)
 {
        struct job *j = container_of(p, struct job, proc.task);
 
        if (j->trigger->remove) {
-               list_del(&j->trigger->list);
-               free(j->trigger);
+               trigger_free(j->trigger);
        } else {
                j->trigger->pending = 0;
        }
@@ -217,8 +225,9 @@ static void trigger_delay_cb(struct uloop_timeout *tout)
 {
        struct trigger *t = container_of(tout, struct trigger, delay);
 
-       json_script_run(&t->jctx, "foo", t->data);
+       json_script_run(&t->jctx, t->type, t->data);
        free(t->data);
+       t->data = NULL;
 }
 
 static struct trigger* _trigger_add(char *type, struct blob_attr *rule, int timeout, void *id)
@@ -302,8 +311,8 @@ void trigger_del(void *id)
                        t->remove = 1;
                        continue;
                }
-               list_del(&t->list);
-               free(t);
+
+               trigger_free(t);
        }
 }
 
@@ -314,20 +323,29 @@ void trigger_init(void)
        q.max_running_tasks = 1;
 }
 
-void trigger_event(char *type, struct blob_attr *data)
+static bool trigger_match(const char *event, const char *match)
+{
+       char *wildcard = strstr(match, ".*");
+       if (wildcard)
+               return !strncmp(event, match, wildcard - match);
+       return !strcmp(event, match);
+}
+
+void trigger_event(const char *type, struct blob_attr *data)
 {
        struct trigger *t;
 
        list_for_each_entry(t, &triggers, list) {
-               if (t->pending || t->remove)
+               if (t->remove)
                        continue;
-               if (!strcmp(t->type, type)) {
-                       if (t->timeout) {
-                               t->data = blob_memdup(data);
-                               uloop_timeout_set(&t->delay, t->timeout);
-                       } else {
-                               json_script_run(&t->jctx, "foo", data);
-                       }
+               if (!trigger_match(type, t->type))
+                       continue;
+               if (t->timeout) {
+                       free(t->data);
+                       t->data = blob_memdup(data);
+                       uloop_timeout_set(&t->delay, t->timeout);
+               } else {
+                       json_script_run(&t->jctx, t->type, data);
                }
        }
 }