add wildcard support to trigger matching code
[project/procd.git] / service / watch.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <stdlib.h>
16 #include <unistd.h>
17
18 #include <libubox/blobmsg_json.h>
19
20 #include "../procd.h"
21
22 struct watch_object {
23         struct list_head list;
24
25         void *id;
26         char *name;
27 };
28
29 struct watch_subscribe {
30         struct uloop_timeout t;
31         uint32_t id;
32 };
33
34 static struct ubus_event_handler watch_event;
35 static struct ubus_subscriber watch_subscribe;
36 static LIST_HEAD(watch_objects);
37
38 static void watch_subscribe_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
39                 const char *type, struct blob_attr *msg)
40 {
41         static const struct blobmsg_policy policy = {
42                 "path", BLOBMSG_TYPE_STRING
43         };
44         struct watch_object *o;
45         struct blob_attr *attr;
46         const char *path;
47
48         DEBUG(3, "ubus event %s\n", type);
49         if (strcmp(type, "ubus.object.add") != 0)
50                 return;
51
52         blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
53         if (!attr)
54                 return;
55
56         path = blobmsg_data(attr);
57         DEBUG(3, "ubus path %s\n", path);
58
59         list_for_each_entry(o, &watch_objects, list) {
60                 unsigned int id;
61
62                 if (strcmp(o->name, path))
63                         continue;
64                 if (ubus_lookup_id(ctx, path, &id))
65                         continue;
66                 if (!ubus_subscribe(ctx, &watch_subscribe, id))
67                         return;
68                 ERROR("failed to suscribe %d\n", id);
69         }
70 }
71
72 void
73 watch_add(const char *_name, void *id)
74 {
75         int len = strlen(_name);
76         char *name;
77         struct watch_object *o = calloc_a(sizeof(*o), &name, len + 1);
78
79         o->name = name;
80         strcpy(name, _name);
81         o->id = id;
82         list_add(&o->list, &watch_objects);
83 }
84
85 void
86 watch_del(void *id)
87 {
88         struct watch_object *t, *n;
89
90         list_for_each_entry_safe(t, n, &watch_objects, list) {
91                 if (t->id != id)
92                         continue;
93                 list_del(&t->list);
94                 free(t);
95         }
96 }
97
98 static int
99 watch_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
100                 struct ubus_request_data *req, const char *method,
101                 struct blob_attr *msg)
102 {
103         if (1 || debug >= 3) {
104                 char *str;
105
106                 str = blobmsg_format_json(msg, true);
107                 DEBUG(3, "Received ubus notify '%s': %s\n", method, str);
108                 free(str);
109         }
110
111         trigger_event(method, msg);
112         return 0;
113 }
114
115 void
116 watch_ubus(struct ubus_context *ctx)
117 {
118         watch_event.cb = watch_subscribe_cb;
119         watch_subscribe.cb = watch_notify_cb;
120         if (ubus_register_event_handler(ctx, &watch_event, "ubus.object.add"))
121                 ERROR("failed to add ubus event handler\n");
122         if (ubus_register_subscriber(ctx, &watch_subscribe))
123                 ERROR("failed to register ubus subscriber\n");
124 }