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