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