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