procd: service: Support writing pidfiles
[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, "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_NAME,
183         SERVICE_LIST_ATTR_VERBOSE,
184         __SERVICE_LIST_ATTR_MAX,
185 };
186
187 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
188         [SERVICE_LIST_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
189         [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
190 };
191
192 enum {
193         EVENT_TYPE,
194         EVENT_DATA,
195         __EVENT_MAX
196 };
197
198 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
199         [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
200         [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
201 };
202
203 enum {
204         VALIDATE_PACKAGE,
205         VALIDATE_TYPE,
206         VALIDATE_SERVICE,
207         __VALIDATE_MAX
208 };
209
210 static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
211         [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
212         [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
213         [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
214 };
215
216 enum {
217         DATA_NAME,
218         DATA_INSTANCE,
219         DATA_TYPE,
220         __DATA_MAX
221 };
222
223 static const struct blobmsg_policy get_data_policy[] = {
224         [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING },
225         [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
226         [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING },
227 };
228
229 static int
230 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
231                    struct ubus_request_data *req, const char *method,
232                    struct blob_attr *msg)
233 {
234         struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
235         struct service *s = NULL;
236         const char *name;
237         bool add = !strcmp(method, "add");
238         int ret;
239
240         blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
241         cur = tb[SERVICE_ATTR_NAME];
242         if (!cur)
243                 return UBUS_STATUS_INVALID_ARGUMENT;
244
245         name = blobmsg_data(cur);
246
247         s = avl_find_element(&services, name, s, avl);
248         if (s) {
249                 DEBUG(2, "Update service %s\n", name);
250                 return service_update(s, tb, add);
251         }
252
253         DEBUG(2, "Create service %s\n", name);
254         s = service_alloc(name);
255         if (!s)
256                 return UBUS_STATUS_UNKNOWN_ERROR;
257
258         ret = service_update(s, tb, add);
259         if (ret)
260                 return ret;
261
262         avl_insert(&services, &s->avl);
263
264         service_event("service.start", s->name, NULL);
265
266         return 0;
267 }
268
269 static void
270 service_dump(struct service *s, bool verbose)
271 {
272         struct service_instance *in;
273         void *c, *i;
274
275         c = blobmsg_open_table(&b, s->name);
276
277         if (!avl_is_empty(&s->instances.avl)) {
278                 i = blobmsg_open_table(&b, "instances");
279                 vlist_for_each_element(&s->instances, in, node)
280                         instance_dump(&b, in, verbose);
281                 blobmsg_close_table(&b, i);
282         }
283         if (verbose && s->trigger)
284                 blobmsg_add_blob(&b, s->trigger);
285         if (verbose && !list_empty(&s->validators))
286                 service_validate_dump(&b, s);
287         blobmsg_close_table(&b, c);
288 }
289
290 static int
291 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
292                     struct ubus_request_data *req, const char *method,
293                     struct blob_attr *msg)
294 {
295         struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
296         struct service *s;
297         const char *name = NULL;
298         bool verbose = false;
299
300         blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
301
302         if (tb[SERVICE_LIST_ATTR_VERBOSE])
303                 verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
304         if (tb[SERVICE_LIST_ATTR_NAME])
305                 name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
306
307         blob_buf_init(&b, 0);
308         avl_for_each_element(&services, s, avl) {
309                 if (name && strcmp(s->name, name) != 0)
310                         continue;
311
312                 service_dump(s, verbose);
313         }
314
315         ubus_send_reply(ctx, req, b.head);
316
317         return 0;
318 }
319
320 static int
321 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
322                     struct ubus_request_data *req, const char *method,
323                     struct blob_attr *msg)
324 {
325         struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
326         struct service *s;
327         struct service_instance *in;
328
329         blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
330
331         cur = tb[SERVICE_DEL_ATTR_NAME];
332         if (!cur)
333                 return UBUS_STATUS_NOT_FOUND;
334
335         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
336         if (!s)
337                 return UBUS_STATUS_NOT_FOUND;
338
339         cur = tb[SERVICE_DEL_ATTR_INSTANCE];
340         if (!cur) {
341                 service_delete(s);
342                 return 0;
343         }
344
345         in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
346         if (!in) {
347                 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
348                 return UBUS_STATUS_NOT_FOUND;
349         }
350
351         vlist_delete(&s->instances, &in->node);
352
353         return 0;
354 }
355
356 static int
357 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
358                       struct ubus_request_data *req, const char *method,
359                       struct blob_attr *msg)
360 {
361         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
362         struct service *s;
363
364         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
365
366         cur = tb[SERVICE_ATTR_NAME];
367         if (!cur)
368                 return UBUS_STATUS_INVALID_ARGUMENT;
369
370         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
371         if (!s)
372                 return UBUS_STATUS_NOT_FOUND;
373
374         if (!strcmp(method, "update_start"))
375                 vlist_update(&s->instances);
376         else
377                 vlist_flush(&s->instances);
378
379         return 0;
380 }
381
382 static int
383 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
384                         struct ubus_request_data *req, const char *method,
385                         struct blob_attr *msg)
386 {
387         struct blob_attr *tb[__EVENT_MAX];
388
389         if (!msg)
390                 return UBUS_STATUS_INVALID_ARGUMENT;
391
392         blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
393         if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
394                 return UBUS_STATUS_INVALID_ARGUMENT;
395
396         trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
397
398         return 0;
399 }
400
401 static int
402 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
403                         struct ubus_request_data *req, const char *method,
404                         struct blob_attr *msg)
405 {
406         struct blob_attr *tb[__VALIDATE_MAX];
407         char *p = NULL, *t = NULL;
408
409         if (!msg)
410                 return UBUS_STATUS_INVALID_ARGUMENT;
411
412         blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
413         if (tb[VALIDATE_SERVICE]) {
414                 return 0;
415         }
416         if (tb[VALIDATE_PACKAGE])
417                 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
418
419         if (tb[VALIDATE_TYPE])
420                 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
421
422         blob_buf_init(&b, 0);
423         service_validate_dump_all(&b, p, t);
424         ubus_send_reply(ctx, req, b.head);
425
426         return 0;
427 }
428
429 static int
430 service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
431                  struct ubus_request_data *req, const char *method,
432                  struct blob_attr *msg)
433 {
434         struct service_instance *in;
435         struct service *s;
436         struct blob_attr *tb[__DATA_MAX];
437         const char *name = NULL;
438         const char *instance = NULL;
439         const char *type = NULL;
440
441         blobmsg_parse(get_data_policy, __DATA_MAX, tb, blob_data(msg), blob_len(msg));
442         if (tb[DATA_NAME])
443                 name = blobmsg_data(tb[DATA_NAME]);
444         if (tb[DATA_INSTANCE])
445                 instance = blobmsg_data(tb[DATA_INSTANCE]);
446         if (tb[DATA_TYPE])
447                 type = blobmsg_data(tb[DATA_TYPE]);
448
449         blob_buf_init(&b, 0);
450         avl_for_each_element(&services, s, avl) {
451                 void *cs = NULL;
452
453                 if (name && strcmp(name, s->name))
454                         continue;
455
456                 vlist_for_each_element(&s->instances, in, node) {
457                         struct blobmsg_list_node *var;
458                         void *ci = NULL;
459
460                         if (instance && strcmp(instance, in->name))
461                                 continue;
462
463                         blobmsg_list_for_each(&in->data, var) {
464                                 if (type &&
465                                     strcmp(blobmsg_name(var->data), type))
466                                         continue;
467
468                                 if (!cs)
469                                         cs = blobmsg_open_table(&b, s->name);
470                                 if (!ci)
471                                         ci = blobmsg_open_table(&b, in->name);
472
473                                 blobmsg_add_blob(&b, var->data);
474                         }
475
476                         if (ci)
477                                 blobmsg_close_table(&b, ci);
478                 }
479
480                 if (cs)
481                         blobmsg_close_table(&b, cs);
482         }
483
484         ubus_send_reply(ctx, req, b.head);
485         return 0;
486 }
487
488 static struct ubus_method main_object_methods[] = {
489         UBUS_METHOD("set", service_handle_set, service_set_attrs),
490         UBUS_METHOD("add", service_handle_set, service_set_attrs),
491         UBUS_METHOD("list", service_handle_list, service_list_attrs),
492         UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
493         UBUS_METHOD("update_start", service_handle_update, service_attrs),
494         UBUS_METHOD("update_complete", service_handle_update, service_attrs),
495         UBUS_METHOD("event", service_handle_event, event_policy),
496         UBUS_METHOD("validate", service_handle_validate, validate_policy),
497         UBUS_METHOD("get_data", service_get_data, get_data_policy),
498 };
499
500 static struct ubus_object_type main_object_type =
501         UBUS_OBJECT_TYPE("service", main_object_methods);
502
503 static struct ubus_object main_object = {
504         .name = "service",
505         .type = &main_object_type,
506         .methods = main_object_methods,
507         .n_methods = ARRAY_SIZE(main_object_methods),
508 };
509
510 int
511 service_start_early(char *name, char *cmdline)
512 {
513         void *instances, *instance, *command, *respawn;
514         char *t;
515
516         blob_buf_init(&b, 0);
517         blobmsg_add_string(&b, "name", name);
518         instances = blobmsg_open_table(&b, "instances");
519         instance = blobmsg_open_table(&b, "instance1");
520         command = blobmsg_open_array(&b, "command");
521         t = strtok(cmdline, " ");
522         while (t) {
523                 blobmsg_add_string(&b, NULL, t);
524                 t = strtok(NULL, " ");
525         }
526         blobmsg_close_array(&b, command);
527         respawn = blobmsg_open_array(&b, "respawn");
528         blobmsg_add_string(&b, NULL, "3600");
529         blobmsg_add_string(&b, NULL, "1");
530         blobmsg_add_string(&b, NULL, "0");
531         blobmsg_close_array(&b, respawn);
532         blobmsg_close_table(&b, instance);
533         blobmsg_close_table(&b, instances);
534
535         return service_handle_set(NULL, NULL, NULL, "add", b.head);
536 }
537
538 void service_event(const char *type, const char *service, const char *instance)
539 {
540         if (!ctx)
541                 return;
542
543         blob_buf_init(&b, 0);
544         blobmsg_add_string(&b, "service", service);
545         if (instance)
546                 blobmsg_add_string(&b, "instance", instance);
547         ubus_notify(ctx, &main_object, type, b.head, -1);
548 }
549
550 void ubus_init_service(struct ubus_context *_ctx)
551 {
552         ctx = _ctx;
553         ubus_add_object(ctx, &main_object);
554 }