procd: Don't use syslog before its initialization
[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 AVL_TREE(services, avl_strcmp, false, NULL);
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, "Stop instance %s::%s\n", in_o->srv->name, in_o->name);
63                 instance_stop(in_o);
64         } else if (in_n) {
65                 DEBUG(2, "Start instance %s::%s\n", in_n->srv->name, in_n->name);
66                 instance_start(in_n);
67         }
68         blob_buf_init(&b, 0);
69         trigger_event("instance.update", b.head);
70 }
71
72 static struct service *
73 service_alloc(const char *name)
74 {
75         struct service *s;
76         char *new_name;
77
78         s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
79         strcpy(new_name, name);
80
81         vlist_init(&s->instances, avl_strcmp, service_instance_update);
82         s->instances.no_delete = true;
83         s->name = new_name;
84         s->avl.key = s->name;
85         INIT_LIST_HEAD(&s->validators);
86
87         return s;
88 }
89
90 enum {
91         SERVICE_SET_NAME,
92         SERVICE_SET_SCRIPT,
93         SERVICE_SET_INSTANCES,
94         SERVICE_SET_TRIGGER,
95         SERVICE_SET_VALIDATE,
96         __SERVICE_SET_MAX
97 };
98
99 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
100         [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
101         [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
102         [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
103         [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
104         [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
105 };
106
107 static int
108 service_update(struct service *s, struct blob_attr **tb, bool add)
109 {
110         struct blob_attr *cur;
111         int rem;
112
113         if (s->trigger) {
114                 trigger_del(s);
115                 free(s->trigger);
116                 s->trigger = NULL;
117         }
118
119         service_validate_del(s);
120
121         if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
122                 s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
123                 if (!s->trigger)
124                         return -1;
125                 trigger_add(s->trigger, s);
126         }
127
128         if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
129                 blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
130                         service_validate_add(s, cur);
131         }
132
133         if (tb[SERVICE_SET_INSTANCES]) {
134                 if (!add)
135                         vlist_update(&s->instances);
136                 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
137                         service_instance_add(s, cur);
138                 }
139                 if (!add)
140                         vlist_flush(&s->instances);
141         }
142
143         s->deleted = false;
144
145         rc(s->name, "running");
146
147         return 0;
148 }
149
150 static void
151 service_delete(struct service *s)
152 {
153         vlist_flush_all(&s->instances);
154         s->deleted = true;
155         service_stopped(s);
156 }
157
158 enum {
159         SERVICE_ATTR_NAME,
160         __SERVICE_ATTR_MAX,
161 };
162
163 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
164         [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
165 };
166
167 enum {
168         SERVICE_DEL_ATTR_NAME,
169         SERVICE_DEL_ATTR_INSTANCE,
170         __SERVICE_DEL_ATTR_MAX,
171 };
172
173 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
174         [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
175         [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
176 };
177
178 enum {
179         SERVICE_LIST_ATTR_NAME,
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_NAME] = { "name", BLOBMSG_TYPE_STRING },
186         [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
187 };
188
189 enum {
190         SERVICE_SIGNAL_ATTR_NAME,
191         SERVICE_SIGNAL_ATTR_INSTANCE,
192         SERVICE_SIGNAL_ATTR_SIGNAL,
193         __SERVICE_SIGNAL_ATTR_MAX,
194 };
195
196 static const struct blobmsg_policy service_signal_attrs[__SERVICE_SIGNAL_ATTR_MAX] = {
197         [SERVICE_SIGNAL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
198         [SERVICE_SIGNAL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
199         [SERVICE_SIGNAL_ATTR_SIGNAL] = { "signal", BLOBMSG_TYPE_INT32 },
200 };
201
202 enum {
203         EVENT_TYPE,
204         EVENT_DATA,
205         __EVENT_MAX
206 };
207
208 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
209         [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
210         [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
211 };
212
213 enum {
214         VALIDATE_PACKAGE,
215         VALIDATE_TYPE,
216         VALIDATE_SERVICE,
217         __VALIDATE_MAX
218 };
219
220 static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
221         [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
222         [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
223         [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
224 };
225
226 enum {
227         DATA_NAME,
228         DATA_INSTANCE,
229         DATA_TYPE,
230         __DATA_MAX
231 };
232
233 static const struct blobmsg_policy get_data_policy[] = {
234         [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING },
235         [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
236         [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING },
237 };
238
239 static int
240 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
241                    struct ubus_request_data *req, const char *method,
242                    struct blob_attr *msg)
243 {
244         struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
245         struct service *s = NULL;
246         const char *name;
247         bool add = !strcmp(method, "add");
248         int ret;
249
250         blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
251         cur = tb[SERVICE_ATTR_NAME];
252         if (!cur)
253                 return UBUS_STATUS_INVALID_ARGUMENT;
254
255         name = blobmsg_data(cur);
256
257         s = avl_find_element(&services, name, s, avl);
258         if (s) {
259                 DEBUG(2, "Update service %s\n", name);
260                 return service_update(s, tb, add);
261         }
262
263         DEBUG(2, "Create service %s\n", name);
264         s = service_alloc(name);
265         if (!s)
266                 return UBUS_STATUS_UNKNOWN_ERROR;
267
268         ret = service_update(s, tb, add);
269         if (ret)
270                 return ret;
271
272         avl_insert(&services, &s->avl);
273
274         service_event("service.start", s->name, NULL);
275
276         return 0;
277 }
278
279 static void
280 service_dump(struct service *s, bool verbose)
281 {
282         struct service_instance *in;
283         void *c, *i;
284
285         c = blobmsg_open_table(&b, s->name);
286
287         if (!avl_is_empty(&s->instances.avl)) {
288                 i = blobmsg_open_table(&b, "instances");
289                 vlist_for_each_element(&s->instances, in, node)
290                         instance_dump(&b, in, verbose);
291                 blobmsg_close_table(&b, i);
292         }
293         if (verbose && s->trigger)
294                 blobmsg_add_blob(&b, s->trigger);
295         if (verbose && !list_empty(&s->validators))
296                 service_validate_dump(&b, s);
297         blobmsg_close_table(&b, c);
298 }
299
300 static int
301 service_handle_list(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_LIST_ATTR_MAX];
306         struct service *s;
307         const char *name = NULL;
308         bool verbose = false;
309
310         blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
311
312         if (tb[SERVICE_LIST_ATTR_VERBOSE])
313                 verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
314         if (tb[SERVICE_LIST_ATTR_NAME])
315                 name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
316
317         blob_buf_init(&b, 0);
318         avl_for_each_element(&services, s, avl) {
319                 if (name && strcmp(s->name, name) != 0)
320                         continue;
321
322                 service_dump(s, verbose);
323         }
324
325         ubus_send_reply(ctx, req, b.head);
326
327         return 0;
328 }
329
330 static int
331 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
332                     struct ubus_request_data *req, const char *method,
333                     struct blob_attr *msg)
334 {
335         struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
336         struct service *s;
337         struct service_instance *in;
338
339         blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
340
341         cur = tb[SERVICE_DEL_ATTR_NAME];
342         if (!cur)
343                 return UBUS_STATUS_NOT_FOUND;
344
345         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
346         if (!s)
347                 return UBUS_STATUS_NOT_FOUND;
348
349         cur = tb[SERVICE_DEL_ATTR_INSTANCE];
350         if (!cur) {
351                 service_delete(s);
352                 return 0;
353         }
354
355         in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
356         if (!in) {
357                 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
358                 return UBUS_STATUS_NOT_FOUND;
359         }
360
361         vlist_delete(&s->instances, &in->node);
362
363         return 0;
364 }
365
366 static int
367 service_handle_kill(struct service_instance *in, int sig)
368 {
369         if (kill(in->proc.pid, sig) == 0)
370                 return 0;
371
372         switch (errno) {
373         case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
374         case EPERM:  return UBUS_STATUS_PERMISSION_DENIED;
375         case ESRCH:  return UBUS_STATUS_NOT_FOUND;
376         }
377
378         return UBUS_STATUS_UNKNOWN_ERROR;
379 }
380
381 static int
382 service_handle_signal(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[__SERVICE_SIGNAL_ATTR_MAX], *cur;
387         struct service *s;
388         struct service_instance *in;
389         int sig = SIGHUP;
390         int rv = 0;
391
392         blobmsg_parse(service_signal_attrs, __SERVICE_SIGNAL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
393
394         cur = tb[SERVICE_SIGNAL_ATTR_SIGNAL];
395         if (cur)
396                 sig = blobmsg_get_u32(cur);
397
398         cur = tb[SERVICE_SIGNAL_ATTR_NAME];
399         if (!cur)
400                 return UBUS_STATUS_NOT_FOUND;
401
402         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
403         if (!s)
404                 return UBUS_STATUS_NOT_FOUND;
405
406         cur = tb[SERVICE_SIGNAL_ATTR_INSTANCE];
407         if (!cur) {
408                 vlist_for_each_element(&s->instances, in, node)
409                         rv = service_handle_kill(in, sig);
410
411                 return rv;
412         }
413
414         in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
415         if (!in) {
416                 ERROR("instance %s not found\n", blobmsg_get_string(cur));
417                 return UBUS_STATUS_NOT_FOUND;
418         }
419
420         return service_handle_kill(in, sig);
421 }
422
423 static int
424 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
425                       struct ubus_request_data *req, const char *method,
426                       struct blob_attr *msg)
427 {
428         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
429         struct service *s;
430
431         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
432
433         cur = tb[SERVICE_SET_NAME];
434         if (!cur)
435                 return UBUS_STATUS_INVALID_ARGUMENT;
436
437         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
438         if (!s)
439                 return UBUS_STATUS_NOT_FOUND;
440
441         if (!strcmp(method, "update_start"))
442                 vlist_update(&s->instances);
443         else
444                 vlist_flush(&s->instances);
445
446         return 0;
447 }
448
449 static int
450 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
451                         struct ubus_request_data *req, const char *method,
452                         struct blob_attr *msg)
453 {
454         struct blob_attr *tb[__EVENT_MAX];
455
456         if (!msg)
457                 return UBUS_STATUS_INVALID_ARGUMENT;
458
459         blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
460         if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
461                 return UBUS_STATUS_INVALID_ARGUMENT;
462
463         trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
464
465         return 0;
466 }
467
468 static int
469 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
470                         struct ubus_request_data *req, const char *method,
471                         struct blob_attr *msg)
472 {
473         struct blob_attr *tb[__VALIDATE_MAX];
474         char *p = NULL, *t = NULL;
475
476         if (!msg)
477                 return UBUS_STATUS_INVALID_ARGUMENT;
478
479         blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
480         if (tb[VALIDATE_SERVICE]) {
481                 return 0;
482         }
483         if (tb[VALIDATE_PACKAGE])
484                 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
485
486         if (tb[VALIDATE_TYPE])
487                 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
488
489         blob_buf_init(&b, 0);
490         service_validate_dump_all(&b, p, t);
491         ubus_send_reply(ctx, req, b.head);
492
493         return 0;
494 }
495
496 static int
497 service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
498                  struct ubus_request_data *req, const char *method,
499                  struct blob_attr *msg)
500 {
501         struct service_instance *in;
502         struct service *s;
503         struct blob_attr *tb[__DATA_MAX];
504         const char *name = NULL;
505         const char *instance = NULL;
506         const char *type = NULL;
507
508         blobmsg_parse(get_data_policy, __DATA_MAX, tb, blob_data(msg), blob_len(msg));
509         if (tb[DATA_NAME])
510                 name = blobmsg_data(tb[DATA_NAME]);
511         if (tb[DATA_INSTANCE])
512                 instance = blobmsg_data(tb[DATA_INSTANCE]);
513         if (tb[DATA_TYPE])
514                 type = blobmsg_data(tb[DATA_TYPE]);
515
516         blob_buf_init(&b, 0);
517         avl_for_each_element(&services, s, avl) {
518                 void *cs = NULL;
519
520                 if (name && strcmp(name, s->name))
521                         continue;
522
523                 vlist_for_each_element(&s->instances, in, node) {
524                         struct blobmsg_list_node *var;
525                         void *ci = NULL;
526
527                         if (instance && strcmp(instance, in->name))
528                                 continue;
529
530                         blobmsg_list_for_each(&in->data, var) {
531                                 if (type &&
532                                     strcmp(blobmsg_name(var->data), type))
533                                         continue;
534
535                                 if (!cs)
536                                         cs = blobmsg_open_table(&b, s->name);
537                                 if (!ci)
538                                         ci = blobmsg_open_table(&b, in->name);
539
540                                 blobmsg_add_blob(&b, var->data);
541                         }
542
543                         if (ci)
544                                 blobmsg_close_table(&b, ci);
545                 }
546
547                 if (cs)
548                         blobmsg_close_table(&b, cs);
549         }
550
551         ubus_send_reply(ctx, req, b.head);
552         return 0;
553 }
554
555 static struct ubus_method main_object_methods[] = {
556         UBUS_METHOD("set", service_handle_set, service_set_attrs),
557         UBUS_METHOD("add", service_handle_set, service_set_attrs),
558         UBUS_METHOD("list", service_handle_list, service_list_attrs),
559         UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
560         UBUS_METHOD("signal", service_handle_signal, service_signal_attrs),
561         UBUS_METHOD("update_start", service_handle_update, service_attrs),
562         UBUS_METHOD("update_complete", service_handle_update, service_attrs),
563         UBUS_METHOD("event", service_handle_event, event_policy),
564         UBUS_METHOD("validate", service_handle_validate, validate_policy),
565         UBUS_METHOD("get_data", service_get_data, get_data_policy),
566 };
567
568 static struct ubus_object_type main_object_type =
569         UBUS_OBJECT_TYPE("service", main_object_methods);
570
571 static struct ubus_object main_object = {
572         .name = "service",
573         .type = &main_object_type,
574         .methods = main_object_methods,
575         .n_methods = ARRAY_SIZE(main_object_methods),
576 };
577
578 int
579 service_start_early(char *name, char *cmdline)
580 {
581         void *instances, *instance, *command, *respawn;
582         char *t;
583
584         blob_buf_init(&b, 0);
585         blobmsg_add_string(&b, "name", name);
586         instances = blobmsg_open_table(&b, "instances");
587         instance = blobmsg_open_table(&b, "instance1");
588         command = blobmsg_open_array(&b, "command");
589         t = strtok(cmdline, " ");
590         while (t) {
591                 blobmsg_add_string(&b, NULL, t);
592                 t = strtok(NULL, " ");
593         }
594         blobmsg_close_array(&b, command);
595         respawn = blobmsg_open_array(&b, "respawn");
596         blobmsg_add_string(&b, NULL, "3600");
597         blobmsg_add_string(&b, NULL, "1");
598         blobmsg_add_string(&b, NULL, "0");
599         blobmsg_close_array(&b, respawn);
600         blobmsg_close_table(&b, instance);
601         blobmsg_close_table(&b, instances);
602
603         return service_handle_set(NULL, NULL, NULL, "add", b.head);
604 }
605
606 void service_stopped(struct service *s)
607 {
608         if (s->deleted && avl_is_empty(&s->instances.avl)) {
609                 service_event("service.stop", s->name, NULL);
610                 avl_delete(&services, &s->avl);
611                 trigger_del(s);
612                 free(s->trigger);
613                 free(s);
614                 service_validate_del(s);
615         }
616 }
617
618 void service_event(const char *type, const char *service, const char *instance)
619 {
620         if (!ctx)
621                 return;
622
623         blob_buf_init(&b, 0);
624         blobmsg_add_string(&b, "service", service);
625         if (instance)
626                 blobmsg_add_string(&b, "instance", instance);
627         ubus_notify(ctx, &main_object, type, b.head, -1);
628 }
629
630 void ubus_init_service(struct ubus_context *_ctx)
631 {
632         ctx = _ctx;
633         ubus_add_object(ctx, &main_object);
634 }