service: get rid of service_init and service_validate_init, use static avl tree initi...
[project/procd.git] / service / trigger.c
index 480367b..216e1f2 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));
 }
 
@@ -225,7 +225,7 @@ 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;
 }
@@ -323,20 +323,28 @@ 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 (trigger_match(type, t->type)) {
                        if (t->timeout) {
                                free(t->data);
                                t->data = blob_memdup(data);
                                uloop_timeout_set(&t->delay, t->timeout);
                        } else {
-                               json_script_run(&t->jctx, "foo", data);
+                               json_script_run(&t->jctx, t->type, data);
                        }
                }
        }