2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
15 #include <libubox/blobmsg_json.h>
16 #include <libubox/avl-cmp.h>
25 AVL_TREE(services, avl_strcmp, false, NULL);
26 static struct blob_buf b;
27 static struct ubus_context *ctx;
30 service_instance_add(struct service *s, struct blob_attr *attr)
32 struct service_instance *in;
34 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
37 in = calloc(1, sizeof(*in));
41 instance_init(in, s, attr);
42 vlist_add(&s->instances, &in->node, (void *) in->name);
46 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
47 struct vlist_node *node_old)
49 struct service_instance *in_o = NULL, *in_n = NULL;
52 in_o = container_of(node_old, struct service_instance, node);
55 in_n = container_of(node_new, struct service_instance, node);
58 DEBUG(2, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
59 instance_update(in_o, in_n);
62 DEBUG(2, "Stop instance %s::%s\n", in_o->srv->name, in_o->name);
63 instance_stop(in_o, true);
64 } else if (in_n && in_n->srv->autostart) {
65 DEBUG(2, "Start instance %s::%s\n", in_n->srv->name, in_n->name);
69 trigger_event("instance.update", b.head);
72 static struct service *
73 service_alloc(const char *name)
78 s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
79 strcpy(new_name, name);
81 vlist_init(&s->instances, avl_strcmp, service_instance_update);
82 s->instances.no_delete = true;
85 INIT_LIST_HEAD(&s->validators);
93 SERVICE_SET_INSTANCES,
96 SERVICE_SET_AUTOSTART,
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 [SERVICE_SET_AUTOSTART] = { "autostart", BLOBMSG_TYPE_BOOL },
110 service_update(struct service *s, struct blob_attr **tb, bool add)
112 struct blob_attr *cur;
121 service_validate_del(s);
123 if (tb[SERVICE_SET_AUTOSTART] && !blobmsg_get_bool(tb[SERVICE_SET_AUTOSTART]))
124 s->autostart = false;
128 if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
129 s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
132 trigger_add(s->trigger, s);
135 if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
136 blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
137 service_validate_add(s, cur);
140 if (tb[SERVICE_SET_INSTANCES]) {
142 vlist_update(&s->instances);
143 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
144 service_instance_add(s, cur);
147 vlist_flush(&s->instances);
152 rc(s->name, "running");
158 service_delete(struct service *s)
160 vlist_flush_all(&s->instances);
170 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
171 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
175 SERVICE_DEL_ATTR_NAME,
176 SERVICE_DEL_ATTR_INSTANCE,
177 __SERVICE_DEL_ATTR_MAX,
180 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
181 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
182 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
186 SERVICE_LIST_ATTR_NAME,
187 SERVICE_LIST_ATTR_VERBOSE,
188 __SERVICE_LIST_ATTR_MAX,
191 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
192 [SERVICE_LIST_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
193 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
197 SERVICE_SIGNAL_ATTR_NAME,
198 SERVICE_SIGNAL_ATTR_INSTANCE,
199 SERVICE_SIGNAL_ATTR_SIGNAL,
200 __SERVICE_SIGNAL_ATTR_MAX,
203 static const struct blobmsg_policy service_signal_attrs[__SERVICE_SIGNAL_ATTR_MAX] = {
204 [SERVICE_SIGNAL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
205 [SERVICE_SIGNAL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
206 [SERVICE_SIGNAL_ATTR_SIGNAL] = { "signal", BLOBMSG_TYPE_INT32 },
210 SERVICE_STATE_ATTR_SPAWN,
211 SERVICE_STATE_ATTR_NAME,
212 __SERVICE_STATE_ATTR_MAX,
215 static const struct blobmsg_policy service_state_attrs[__SERVICE_STATE_ATTR_MAX] = {
216 [SERVICE_STATE_ATTR_SPAWN] = { "spawn", BLOBMSG_TYPE_BOOL },
217 [SERVICE_STATE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
226 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
227 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
228 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
238 static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
239 [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
240 [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
241 [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
251 static const struct blobmsg_policy get_data_policy[] = {
252 [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING },
253 [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
254 [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING },
258 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
259 struct ubus_request_data *req, const char *method,
260 struct blob_attr *msg)
262 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
263 struct service *s = NULL;
265 bool add = !strcmp(method, "add");
268 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
269 cur = tb[SERVICE_ATTR_NAME];
271 return UBUS_STATUS_INVALID_ARGUMENT;
273 name = blobmsg_data(cur);
275 s = avl_find_element(&services, name, s, avl);
277 DEBUG(2, "Update service %s\n", name);
278 return service_update(s, tb, add);
281 DEBUG(2, "Create service %s\n", name);
282 s = service_alloc(name);
284 return UBUS_STATUS_UNKNOWN_ERROR;
286 ret = service_update(s, tb, add);
290 avl_insert(&services, &s->avl);
292 service_event("service.start", s->name, NULL);
298 service_dump(struct service *s, bool verbose)
300 struct service_instance *in;
303 c = blobmsg_open_table(&b, s->name);
306 blobmsg_add_u8(&b, "autostart", false);
308 if (!avl_is_empty(&s->instances.avl)) {
309 i = blobmsg_open_table(&b, "instances");
310 vlist_for_each_element(&s->instances, in, node)
311 instance_dump(&b, in, verbose);
312 blobmsg_close_table(&b, i);
314 if (verbose && s->trigger)
315 blobmsg_add_blob(&b, s->trigger);
316 if (verbose && !list_empty(&s->validators))
317 service_validate_dump(&b, s);
318 blobmsg_close_table(&b, c);
322 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
323 struct ubus_request_data *req, const char *method,
324 struct blob_attr *msg)
326 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
328 const char *name = NULL;
329 bool verbose = false;
331 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
333 if (tb[SERVICE_LIST_ATTR_VERBOSE])
334 verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
335 if (tb[SERVICE_LIST_ATTR_NAME])
336 name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
338 blob_buf_init(&b, 0);
339 avl_for_each_element(&services, s, avl) {
340 if (name && strcmp(s->name, name) != 0)
343 service_dump(s, verbose);
346 ubus_send_reply(ctx, req, b.head);
352 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
353 struct ubus_request_data *req, const char *method,
354 struct blob_attr *msg)
356 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
358 struct service_instance *in;
360 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
362 cur = tb[SERVICE_DEL_ATTR_NAME];
364 return UBUS_STATUS_NOT_FOUND;
366 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
368 return UBUS_STATUS_NOT_FOUND;
370 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
376 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
378 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
379 return UBUS_STATUS_NOT_FOUND;
382 vlist_delete(&s->instances, &in->node);
388 service_handle_kill(struct service_instance *in, int sig)
390 if (kill(in->proc.pid, sig) == 0)
394 case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
395 case EPERM: return UBUS_STATUS_PERMISSION_DENIED;
396 case ESRCH: return UBUS_STATUS_NOT_FOUND;
399 return UBUS_STATUS_UNKNOWN_ERROR;
403 service_handle_signal(struct ubus_context *ctx, struct ubus_object *obj,
404 struct ubus_request_data *req, const char *method,
405 struct blob_attr *msg)
407 struct blob_attr *tb[__SERVICE_SIGNAL_ATTR_MAX], *cur;
409 struct service_instance *in;
413 blobmsg_parse(service_signal_attrs, __SERVICE_SIGNAL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
415 cur = tb[SERVICE_SIGNAL_ATTR_SIGNAL];
417 sig = blobmsg_get_u32(cur);
419 cur = tb[SERVICE_SIGNAL_ATTR_NAME];
421 return UBUS_STATUS_NOT_FOUND;
423 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
425 return UBUS_STATUS_NOT_FOUND;
427 cur = tb[SERVICE_SIGNAL_ATTR_INSTANCE];
429 vlist_for_each_element(&s->instances, in, node)
430 rv = service_handle_kill(in, sig);
435 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
437 ERROR("instance %s not found\n", blobmsg_get_string(cur));
438 return UBUS_STATUS_NOT_FOUND;
441 return service_handle_kill(in, sig);
445 service_handle_state(struct ubus_context *ctx, struct ubus_object *obj,
446 struct ubus_request_data *req, const char *method,
447 struct blob_attr *msg)
449 struct blob_attr *tb[__SERVICE_STATE_ATTR_MAX];
451 struct service_instance *in;
454 blobmsg_parse(service_state_attrs, __SERVICE_STATE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
456 if (!tb[SERVICE_STATE_ATTR_SPAWN])
457 return UBUS_STATUS_INVALID_ARGUMENT;
459 if (!tb[SERVICE_STATE_ATTR_NAME])
460 return UBUS_STATUS_NOT_FOUND;
462 s = avl_find_element(&services, blobmsg_data(tb[SERVICE_STATE_ATTR_NAME]), s, avl);
464 return UBUS_STATUS_NOT_FOUND;
466 spawn = !!blobmsg_get_u8(tb[SERVICE_STATE_ATTR_SPAWN]);
467 vlist_for_each_element(&s->instances, in, node) {
468 if (!!in->proc.pending == !!spawn)
470 else if (!in->proc.pending)
473 instance_stop(in, false);
476 return UBUS_STATUS_OK;
480 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
481 struct ubus_request_data *req, const char *method,
482 struct blob_attr *msg)
484 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
487 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
489 cur = tb[SERVICE_SET_NAME];
491 return UBUS_STATUS_INVALID_ARGUMENT;
493 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
495 return UBUS_STATUS_NOT_FOUND;
497 if (!strcmp(method, "update_start"))
498 vlist_update(&s->instances);
500 vlist_flush(&s->instances);
506 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
507 struct ubus_request_data *req, const char *method,
508 struct blob_attr *msg)
510 struct blob_attr *tb[__EVENT_MAX];
513 return UBUS_STATUS_INVALID_ARGUMENT;
515 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
516 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
517 return UBUS_STATUS_INVALID_ARGUMENT;
519 trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
525 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
526 struct ubus_request_data *req, const char *method,
527 struct blob_attr *msg)
529 struct blob_attr *tb[__VALIDATE_MAX];
530 char *p = NULL, *t = NULL;
533 return UBUS_STATUS_INVALID_ARGUMENT;
535 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
536 if (tb[VALIDATE_SERVICE]) {
539 if (tb[VALIDATE_PACKAGE])
540 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
542 if (tb[VALIDATE_TYPE])
543 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
545 blob_buf_init(&b, 0);
546 service_validate_dump_all(&b, p, t);
547 ubus_send_reply(ctx, req, b.head);
553 service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
554 struct ubus_request_data *req, const char *method,
555 struct blob_attr *msg)
557 struct service_instance *in;
559 struct blob_attr *tb[__DATA_MAX];
560 const char *name = NULL;
561 const char *instance = NULL;
562 const char *type = NULL;
564 blobmsg_parse(get_data_policy, __DATA_MAX, tb, blob_data(msg), blob_len(msg));
566 name = blobmsg_data(tb[DATA_NAME]);
567 if (tb[DATA_INSTANCE])
568 instance = blobmsg_data(tb[DATA_INSTANCE]);
570 type = blobmsg_data(tb[DATA_TYPE]);
572 blob_buf_init(&b, 0);
573 avl_for_each_element(&services, s, avl) {
576 if (name && strcmp(name, s->name))
579 vlist_for_each_element(&s->instances, in, node) {
580 struct blobmsg_list_node *var;
583 if (instance && strcmp(instance, in->name))
586 blobmsg_list_for_each(&in->data, var) {
588 strcmp(blobmsg_name(var->data), type))
592 cs = blobmsg_open_table(&b, s->name);
594 ci = blobmsg_open_table(&b, in->name);
596 blobmsg_add_blob(&b, var->data);
600 blobmsg_close_table(&b, ci);
604 blobmsg_close_table(&b, cs);
607 ubus_send_reply(ctx, req, b.head);
611 static struct ubus_method main_object_methods[] = {
612 UBUS_METHOD("set", service_handle_set, service_set_attrs),
613 UBUS_METHOD("add", service_handle_set, service_set_attrs),
614 UBUS_METHOD("list", service_handle_list, service_list_attrs),
615 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
616 UBUS_METHOD("signal", service_handle_signal, service_signal_attrs),
617 UBUS_METHOD("update_start", service_handle_update, service_attrs),
618 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
619 UBUS_METHOD("event", service_handle_event, event_policy),
620 UBUS_METHOD("validate", service_handle_validate, validate_policy),
621 UBUS_METHOD("get_data", service_get_data, get_data_policy),
622 UBUS_METHOD("state", service_handle_state, service_state_attrs),
625 static struct ubus_object_type main_object_type =
626 UBUS_OBJECT_TYPE("service", main_object_methods);
628 static struct ubus_object main_object = {
630 .type = &main_object_type,
631 .methods = main_object_methods,
632 .n_methods = ARRAY_SIZE(main_object_methods),
636 service_start_early(char *name, char *cmdline)
638 void *instances, *instance, *command, *respawn;
641 blob_buf_init(&b, 0);
642 blobmsg_add_string(&b, "name", name);
643 instances = blobmsg_open_table(&b, "instances");
644 instance = blobmsg_open_table(&b, "instance1");
645 command = blobmsg_open_array(&b, "command");
646 t = strtok(cmdline, " ");
648 blobmsg_add_string(&b, NULL, t);
649 t = strtok(NULL, " ");
651 blobmsg_close_array(&b, command);
652 respawn = blobmsg_open_array(&b, "respawn");
653 blobmsg_add_string(&b, NULL, "3600");
654 blobmsg_add_string(&b, NULL, "1");
655 blobmsg_add_string(&b, NULL, "0");
656 blobmsg_close_array(&b, respawn);
657 blobmsg_close_table(&b, instance);
658 blobmsg_close_table(&b, instances);
660 return service_handle_set(NULL, NULL, NULL, "add", b.head);
663 void service_stopped(struct service *s)
665 if (s->deleted && avl_is_empty(&s->instances.avl)) {
666 service_event("service.stop", s->name, NULL);
667 avl_delete(&services, &s->avl);
671 service_validate_del(s);
675 void service_event(const char *type, const char *service, const char *instance)
680 blob_buf_init(&b, 0);
681 blobmsg_add_string(&b, "service", service);
683 blobmsg_add_string(&b, "instance", instance);
684 ubus_notify(ctx, &main_object, type, b.head, -1);
687 void ubus_init_service(struct ubus_context *_ctx)
690 ubus_add_object(ctx, &main_object);