udevtrigger: use a helper function for subdir scanning
[project/procd.git] / service.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 <libubox/blobmsg_json.h>
16 #include <libubox/avl-cmp.h>
17 #include "procd.h"
18 #include "service.h"
19 #include "instance.h"
20
21 struct avl_tree services;
22 static struct blob_buf b;
23
24 static void
25 service_instance_add(struct service *s, struct blob_attr *attr)
26 {
27         struct service_instance *in;
28
29         if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
30                 return;
31
32         in = calloc(1, sizeof(*in));
33         if (!in)
34                 return;
35
36         instance_init(in, s, attr);
37         vlist_add(&s->instances, &in->node, (void *) in->name);
38 }
39
40 static void
41 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
42                         struct vlist_node *node_old)
43 {
44         struct service_instance *in_o = NULL, *in_n = NULL;
45
46         if (node_old)
47                 in_o = container_of(node_old, struct service_instance, node);
48
49         if (node_new)
50                 in_n = container_of(node_new, struct service_instance, node);
51
52         if (in_o && in_n) {
53                 DEBUG(1, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
54                 instance_update(in_o, in_n);
55                 instance_free(in_n);
56         } else if (in_o) {
57                 DEBUG(1, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
58                 instance_stop(in_o);
59                 instance_free(in_o);
60         } else if (in_n) {
61                 DEBUG(1, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
62                 instance_start(in_n);
63         }
64 }
65
66 static struct service *
67 service_alloc(const char *name)
68 {
69         struct service *s;
70         char *new_name;
71
72         s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
73         strcpy(new_name, name);
74
75         vlist_init(&s->instances, avl_strcmp, service_instance_update);
76         s->instances.keep_old = true;
77         s->name = new_name;
78         s->avl.key = s->name;
79
80         return s;
81 }
82
83 enum {
84         SERVICE_SET_NAME,
85         SERVICE_SET_SCRIPT,
86         SERVICE_SET_INSTANCES,
87         SERVICE_SET_TRIGGER,
88         __SERVICE_SET_MAX
89 };
90
91 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
92         [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
93         [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
94         [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
95         [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
96 };
97
98 static int
99 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
100 {
101         struct blob_attr *cur;
102         int rem;
103
104         if (s->trigger) {
105                 trigger_del(s);
106                 free(s->trigger);
107                 s->trigger = NULL;
108         }
109
110         if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
111                 s->trigger = malloc(blob_len(tb[SERVICE_SET_TRIGGER]));
112                 if (!s->trigger)
113                         return -1;
114                 memcpy(s->trigger, tb[SERVICE_SET_TRIGGER], blob_len(tb[SERVICE_SET_TRIGGER]));
115                 trigger_add(s->trigger, s);
116         }
117
118         if (tb[SERVICE_SET_INSTANCES]) {
119                 if (!add)
120                         vlist_update(&s->instances);
121                 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
122                         service_instance_add(s, cur);
123                 }
124                 if (!add)
125                         vlist_flush(&s->instances);
126         }
127
128         return 0;
129 }
130
131 static void
132 service_delete(struct service *s)
133 {
134         vlist_flush_all(&s->instances);
135         avl_delete(&services, &s->avl);
136         trigger_del(s);
137         s->trigger = NULL;
138         free(s->trigger);
139         free(s->config);
140         free(s);
141 }
142
143 enum {
144         SERVICE_ATTR_NAME,
145         __SERVICE_ATTR_MAX,
146 };
147
148 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
149         [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
150 };
151
152 enum {
153         SERVICE_DEL_ATTR_NAME,
154         SERVICE_DEL_ATTR_INSTANCE,
155         __SERVICE_DEL_ATTR_MAX,
156 };
157
158 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
159         [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
160         [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
161 };
162
163 enum {
164         SERVICE_LIST_ATTR_VERBOSE,
165         __SERVICE_LIST_ATTR_MAX,
166 };
167
168 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
169         [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
170 };
171
172 enum {
173         EVENT_TYPE,
174         EVENT_DATA,
175         __EVENT_MAX
176 };
177
178 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
179         [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
180         [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
181 };
182
183 static int
184 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
185                    struct ubus_request_data *req, const char *method,
186                    struct blob_attr *msg)
187 {
188         struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
189         struct service *s = NULL;
190         const char *name;
191         int ret = UBUS_STATUS_INVALID_ARGUMENT;
192         bool add = !strcmp(method, "add");
193
194         blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
195         cur = tb[SERVICE_ATTR_NAME];
196         if (!cur)
197                 goto free;
198
199         name = blobmsg_data(cur);
200
201         s = avl_find_element(&services, name, s, avl);
202         if (s) {
203                 DEBUG(1, "Update service %s\n", name);
204                 return service_update(s, msg, tb, add);
205         }
206
207         DEBUG(1, "Create service %s\n", name);
208         s = service_alloc(name);
209         if (!s)
210                 return UBUS_STATUS_UNKNOWN_ERROR;
211
212         ret = service_update(s, msg, tb, add);
213         if (ret)
214                 goto free;
215
216         avl_insert(&services, &s->avl);
217
218         return 0;
219
220 free:
221         free(msg);
222         return ret;
223 }
224
225 static void
226 service_dump(struct service *s, int verbose)
227 {
228         struct service_instance *in;
229         void *c, *i;
230
231         c = blobmsg_open_table(&b, s->name);
232
233         if (avl_is_empty(&s->instances.avl)) {
234                 blobmsg_close_table(&b, c);
235                 return;
236         }
237
238         i = blobmsg_open_table(&b, "instances");
239         vlist_for_each_element(&s->instances, in, node)
240                 instance_dump(&b, in, verbose);
241         blobmsg_close_table(&b, i);
242         if (verbose && s->trigger)
243                 blobmsg_add_blob(&b, s->trigger);
244         blobmsg_close_table(&b, c);
245 }
246
247 static int
248 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
249                     struct ubus_request_data *req, const char *method,
250                     struct blob_attr *msg)
251 {
252         struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
253         struct service *s;
254         int verbose = 0;
255
256         blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
257
258         if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
259                 verbose = 1;
260
261         blob_buf_init(&b, 0);
262         avl_for_each_element(&services, s, avl)
263                 service_dump(s, verbose);
264
265         ubus_send_reply(ctx, req, b.head);
266
267         return 0;
268 }
269
270 static int
271 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
272                     struct ubus_request_data *req, const char *method,
273                     struct blob_attr *msg)
274 {
275         struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
276         struct service *s;
277         struct service_instance *in;
278
279         blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
280
281         cur = tb[SERVICE_DEL_ATTR_NAME];
282         if (!cur)
283                 return UBUS_STATUS_NOT_FOUND;
284
285         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
286         if (!s)
287                 return UBUS_STATUS_NOT_FOUND;
288
289         cur = tb[SERVICE_DEL_ATTR_INSTANCE];
290         if (!cur) {
291                 service_delete(s);
292                 return 0;
293         }
294
295         in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
296         if (!in) {
297                 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
298                 return UBUS_STATUS_NOT_FOUND;
299         }
300
301         vlist_delete(&s->instances, &in->node);
302
303         return 0;
304 }
305
306 static int
307 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
308                       struct ubus_request_data *req, const char *method,
309                       struct blob_attr *msg)
310 {
311         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
312         struct service *s;
313
314         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
315
316         cur = tb[SERVICE_ATTR_NAME];
317         if (!cur)
318                 return UBUS_STATUS_INVALID_ARGUMENT;
319
320         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
321         if (!s)
322                 return UBUS_STATUS_NOT_FOUND;
323
324         if (!strcmp(method, "update_start"))
325                 vlist_update(&s->instances);
326         else
327                 vlist_flush(&s->instances);
328
329         return 0;
330 }
331
332 static int
333 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
334                         struct ubus_request_data *req, const char *method,
335                         struct blob_attr *msg)
336 {
337         struct blob_attr *tb[__EVENT_MAX];
338
339         if (!msg)
340                 return UBUS_STATUS_INVALID_ARGUMENT;
341
342         blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
343         if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
344                 return UBUS_STATUS_INVALID_ARGUMENT;
345
346         trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
347
348         return 0;
349 }
350
351 static struct ubus_method main_object_methods[] = {
352         UBUS_METHOD("set", service_handle_set, service_set_attrs),
353         UBUS_METHOD("add", service_handle_set, service_set_attrs),
354         UBUS_METHOD("list", service_handle_list, service_attrs),
355         UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
356         UBUS_METHOD("update_start", service_handle_update, service_attrs),
357         UBUS_METHOD("update_complete", service_handle_update, service_attrs),
358         UBUS_METHOD("event", service_handle_event, event_policy),
359 };
360
361 static struct ubus_object_type main_object_type =
362         UBUS_OBJECT_TYPE("service", main_object_methods);
363
364 static struct ubus_object main_object = {
365         .name = "service",
366         .type = &main_object_type,
367         .methods = main_object_methods,
368         .n_methods = ARRAY_SIZE(main_object_methods),
369 };
370
371 void ubus_init_service(struct ubus_context *ctx)
372 {
373         avl_init(&services, avl_strcmp, false, NULL);
374         ubus_add_object(ctx, &main_object);
375 }