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