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