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;
28 static struct ubus_object main_object;
31 service_instance_add(struct service *s, struct blob_attr *attr)
33 struct service_instance *in;
35 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
38 in = calloc(1, sizeof(*in));
42 instance_init(in, s, attr);
43 vlist_add(&s->instances, &in->node, (void *) in->name);
47 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
48 struct vlist_node *node_old)
50 struct service_instance *in_o = NULL, *in_n = NULL;
53 in_o = container_of(node_old, struct service_instance, node);
56 in_n = container_of(node_new, struct service_instance, node);
59 DEBUG(2, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
60 instance_update(in_o, in_n);
63 DEBUG(2, "Stop instance %s::%s\n", in_o->srv->name, in_o->name);
64 instance_stop(in_o, true);
65 } else if (in_n && in_n->srv->autostart) {
66 DEBUG(2, "Start instance %s::%s\n", in_n->srv->name, in_n->name);
70 trigger_event("instance.update", b.head);
73 static struct service *
74 service_alloc(const char *name)
79 s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
80 strcpy(new_name, name);
82 vlist_init(&s->instances, avl_strcmp, service_instance_update);
83 s->instances.no_delete = true;
86 INIT_LIST_HEAD(&s->validators);
94 SERVICE_SET_INSTANCES,
97 SERVICE_SET_AUTOSTART,
101 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
102 [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
103 [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
104 [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
105 [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
106 [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
107 [SERVICE_SET_AUTOSTART] = { "autostart", BLOBMSG_TYPE_BOOL },
111 service_update(struct service *s, struct blob_attr **tb, bool add)
113 struct blob_attr *cur;
122 service_validate_del(s);
124 if (tb[SERVICE_SET_AUTOSTART] && !blobmsg_get_bool(tb[SERVICE_SET_AUTOSTART]))
125 s->autostart = false;
129 if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
130 s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
133 trigger_add(s->trigger, s);
136 if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
137 blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
138 service_validate_add(s, cur);
141 if (tb[SERVICE_SET_INSTANCES]) {
143 vlist_update(&s->instances);
144 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
145 service_instance_add(s, cur);
148 vlist_flush(&s->instances);
153 rc(s->name, "running");
159 service_delete(struct service *s)
161 vlist_flush_all(&s->instances);
171 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
172 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
176 SERVICE_DEL_ATTR_NAME,
177 SERVICE_DEL_ATTR_INSTANCE,
178 __SERVICE_DEL_ATTR_MAX,
181 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
182 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
183 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
187 SERVICE_LIST_ATTR_NAME,
188 SERVICE_LIST_ATTR_VERBOSE,
189 __SERVICE_LIST_ATTR_MAX,
192 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
193 [SERVICE_LIST_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
194 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
198 SERVICE_SIGNAL_ATTR_NAME,
199 SERVICE_SIGNAL_ATTR_INSTANCE,
200 SERVICE_SIGNAL_ATTR_SIGNAL,
201 __SERVICE_SIGNAL_ATTR_MAX,
204 static const struct blobmsg_policy service_signal_attrs[__SERVICE_SIGNAL_ATTR_MAX] = {
205 [SERVICE_SIGNAL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
206 [SERVICE_SIGNAL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
207 [SERVICE_SIGNAL_ATTR_SIGNAL] = { "signal", BLOBMSG_TYPE_INT32 },
211 SERVICE_STATE_ATTR_SPAWN,
212 SERVICE_STATE_ATTR_NAME,
213 __SERVICE_STATE_ATTR_MAX,
216 static const struct blobmsg_policy service_state_attrs[__SERVICE_STATE_ATTR_MAX] = {
217 [SERVICE_STATE_ATTR_SPAWN] = { "spawn", BLOBMSG_TYPE_BOOL },
218 [SERVICE_STATE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
227 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
228 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
229 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
239 static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
240 [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
241 [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
242 [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
252 static const struct blobmsg_policy get_data_policy[] = {
253 [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING },
254 [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
255 [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING },
259 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
260 struct ubus_request_data *req, const char *method,
261 struct blob_attr *msg)
263 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
264 struct service *s = NULL;
266 bool add = !strcmp(method, "add");
269 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
270 cur = tb[SERVICE_SET_NAME];
272 return UBUS_STATUS_INVALID_ARGUMENT;
274 name = blobmsg_data(cur);
276 s = avl_find_element(&services, name, s, avl);
278 DEBUG(2, "Update service %s\n", name);
279 return service_update(s, tb, add);
282 DEBUG(2, "Create service %s\n", name);
283 s = service_alloc(name);
285 return UBUS_STATUS_UNKNOWN_ERROR;
287 ret = service_update(s, tb, add);
291 avl_insert(&services, &s->avl);
293 service_event("service.start", s->name, NULL);
299 service_dump(struct service *s, bool verbose)
301 struct service_instance *in;
304 c = blobmsg_open_table(&b, s->name);
307 blobmsg_add_u8(&b, "autostart", false);
309 if (!avl_is_empty(&s->instances.avl)) {
310 i = blobmsg_open_table(&b, "instances");
311 vlist_for_each_element(&s->instances, in, node)
312 instance_dump(&b, in, verbose);
313 blobmsg_close_table(&b, i);
315 if (verbose && s->trigger)
316 blobmsg_add_blob(&b, s->trigger);
317 if (verbose && !list_empty(&s->validators))
318 service_validate_dump(&b, s);
319 blobmsg_close_table(&b, c);
323 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
324 struct ubus_request_data *req, const char *method,
325 struct blob_attr *msg)
327 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
329 const char *name = NULL;
330 bool verbose = false;
332 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
334 if (tb[SERVICE_LIST_ATTR_VERBOSE])
335 verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
336 if (tb[SERVICE_LIST_ATTR_NAME])
337 name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
339 blob_buf_init(&b, 0);
340 avl_for_each_element(&services, s, avl) {
341 if (name && strcmp(s->name, name) != 0)
344 service_dump(s, verbose);
347 ubus_send_reply(ctx, req, b.head);
353 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
354 struct ubus_request_data *req, const char *method,
355 struct blob_attr *msg)
357 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
359 struct service_instance *in;
361 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
363 cur = tb[SERVICE_DEL_ATTR_NAME];
365 return UBUS_STATUS_NOT_FOUND;
367 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
369 return UBUS_STATUS_NOT_FOUND;
371 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
377 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
379 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
380 return UBUS_STATUS_NOT_FOUND;
383 vlist_delete(&s->instances, &in->node);
389 service_handle_kill(struct service_instance *in, int sig)
391 if (kill(in->proc.pid, sig) == 0)
395 case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
396 case EPERM: return UBUS_STATUS_PERMISSION_DENIED;
397 case ESRCH: return UBUS_STATUS_NOT_FOUND;
400 return UBUS_STATUS_UNKNOWN_ERROR;
404 service_handle_signal(struct ubus_context *ctx, struct ubus_object *obj,
405 struct ubus_request_data *req, const char *method,
406 struct blob_attr *msg)
408 struct blob_attr *tb[__SERVICE_SIGNAL_ATTR_MAX], *cur;
410 struct service_instance *in;
414 blobmsg_parse(service_signal_attrs, __SERVICE_SIGNAL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
416 cur = tb[SERVICE_SIGNAL_ATTR_SIGNAL];
418 sig = blobmsg_get_u32(cur);
420 cur = tb[SERVICE_SIGNAL_ATTR_NAME];
422 return UBUS_STATUS_NOT_FOUND;
424 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
426 return UBUS_STATUS_NOT_FOUND;
428 cur = tb[SERVICE_SIGNAL_ATTR_INSTANCE];
430 vlist_for_each_element(&s->instances, in, node)
431 rv = service_handle_kill(in, sig);
436 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
438 ERROR("instance %s not found\n", blobmsg_get_string(cur));
439 return UBUS_STATUS_NOT_FOUND;
442 return service_handle_kill(in, sig);
446 service_handle_state(struct ubus_context *ctx, struct ubus_object *obj,
447 struct ubus_request_data *req, const char *method,
448 struct blob_attr *msg)
450 struct blob_attr *tb[__SERVICE_STATE_ATTR_MAX];
452 struct service_instance *in;
455 blobmsg_parse(service_state_attrs, __SERVICE_STATE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
457 if (!tb[SERVICE_STATE_ATTR_SPAWN])
458 return UBUS_STATUS_INVALID_ARGUMENT;
460 if (!tb[SERVICE_STATE_ATTR_NAME])
461 return UBUS_STATUS_NOT_FOUND;
463 s = avl_find_element(&services, blobmsg_data(tb[SERVICE_STATE_ATTR_NAME]), s, avl);
465 return UBUS_STATUS_NOT_FOUND;
467 spawn = !!blobmsg_get_u8(tb[SERVICE_STATE_ATTR_SPAWN]);
468 vlist_for_each_element(&s->instances, in, node) {
469 if (!!in->proc.pending == !!spawn)
471 else if (!in->proc.pending)
474 instance_stop(in, false);
477 return UBUS_STATUS_OK;
481 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
482 struct ubus_request_data *req, const char *method,
483 struct blob_attr *msg)
485 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
488 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
490 cur = tb[SERVICE_SET_NAME];
492 return UBUS_STATUS_INVALID_ARGUMENT;
494 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
496 return UBUS_STATUS_NOT_FOUND;
498 if (!strcmp(method, "update_start"))
499 vlist_update(&s->instances);
501 vlist_flush(&s->instances);
506 static void ubus_event_bcast(const char *type, const char *param1, const char *val1,
507 const char *param2, const char *val2)
512 blob_buf_init(&b, 0);
514 blobmsg_add_string(&b, param1, val1);
516 blobmsg_add_string(&b, param2, val2);
517 ubus_notify(ctx, &main_object, type, b.head, -1);
521 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
522 struct ubus_request_data *req, const char *method,
523 struct blob_attr *msg)
525 struct blob_attr *tb[__EVENT_MAX];
529 return UBUS_STATUS_INVALID_ARGUMENT;
531 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
532 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
533 return UBUS_STATUS_INVALID_ARGUMENT;
535 event = blobmsg_get_string(tb[EVENT_TYPE]);
536 trigger_event(event, tb[EVENT_DATA]);
538 if (!strcmp(event, "config.change")) {
539 struct blob_attr *tb2[__DATA_MAX];
541 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb2,
542 blobmsg_data(tb[EVENT_DATA]), blobmsg_len(tb[EVENT_DATA]));
543 if (tb2[VALIDATE_PACKAGE])
544 ubus_event_bcast("config.change", "config",
545 blobmsg_get_string(tb2[VALIDATE_PACKAGE]), NULL, NULL);
551 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
552 struct ubus_request_data *req, const char *method,
553 struct blob_attr *msg)
555 struct blob_attr *tb[__VALIDATE_MAX];
556 char *p = NULL, *t = NULL;
559 return UBUS_STATUS_INVALID_ARGUMENT;
561 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
562 if (tb[VALIDATE_SERVICE]) {
565 if (tb[VALIDATE_PACKAGE])
566 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
568 if (tb[VALIDATE_TYPE])
569 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
571 blob_buf_init(&b, 0);
572 service_validate_dump_all(&b, p, t);
573 ubus_send_reply(ctx, req, b.head);
579 service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
580 struct ubus_request_data *req, const char *method,
581 struct blob_attr *msg)
583 struct service_instance *in;
585 struct blob_attr *tb[__DATA_MAX];
586 const char *name = NULL;
587 const char *instance = NULL;
588 const char *type = NULL;
590 blobmsg_parse(get_data_policy, __DATA_MAX, tb, blob_data(msg), blob_len(msg));
592 name = blobmsg_data(tb[DATA_NAME]);
593 if (tb[DATA_INSTANCE])
594 instance = blobmsg_data(tb[DATA_INSTANCE]);
596 type = blobmsg_data(tb[DATA_TYPE]);
598 blob_buf_init(&b, 0);
599 avl_for_each_element(&services, s, avl) {
602 if (name && strcmp(name, s->name))
605 vlist_for_each_element(&s->instances, in, node) {
606 struct blobmsg_list_node *var;
609 if (instance && strcmp(instance, in->name))
612 blobmsg_list_for_each(&in->data, var) {
614 strcmp(blobmsg_name(var->data), type))
618 cs = blobmsg_open_table(&b, s->name);
620 ci = blobmsg_open_table(&b, in->name);
622 blobmsg_add_blob(&b, var->data);
626 blobmsg_close_table(&b, ci);
630 blobmsg_close_table(&b, cs);
633 ubus_send_reply(ctx, req, b.head);
637 static struct ubus_method main_object_methods[] = {
638 UBUS_METHOD("set", service_handle_set, service_set_attrs),
639 UBUS_METHOD("add", service_handle_set, service_set_attrs),
640 UBUS_METHOD("list", service_handle_list, service_list_attrs),
641 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
642 UBUS_METHOD("signal", service_handle_signal, service_signal_attrs),
643 UBUS_METHOD("update_start", service_handle_update, service_attrs),
644 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
645 UBUS_METHOD("event", service_handle_event, event_policy),
646 UBUS_METHOD("validate", service_handle_validate, validate_policy),
647 UBUS_METHOD("get_data", service_get_data, get_data_policy),
648 UBUS_METHOD("state", service_handle_state, service_state_attrs),
651 static struct ubus_object_type main_object_type =
652 UBUS_OBJECT_TYPE("service", main_object_methods);
654 static struct ubus_object main_object = {
656 .type = &main_object_type,
657 .methods = main_object_methods,
658 .n_methods = ARRAY_SIZE(main_object_methods),
662 service_start_early(char *name, char *cmdline)
664 void *instances, *instance, *command, *respawn;
667 blob_buf_init(&b, 0);
668 blobmsg_add_string(&b, "name", name);
669 instances = blobmsg_open_table(&b, "instances");
670 instance = blobmsg_open_table(&b, "instance1");
671 command = blobmsg_open_array(&b, "command");
672 t = strtok(cmdline, " ");
674 blobmsg_add_string(&b, NULL, t);
675 t = strtok(NULL, " ");
677 blobmsg_close_array(&b, command);
678 respawn = blobmsg_open_array(&b, "respawn");
679 blobmsg_add_string(&b, NULL, "3600");
680 blobmsg_add_string(&b, NULL, "1");
681 blobmsg_add_string(&b, NULL, "0");
682 blobmsg_close_array(&b, respawn);
683 blobmsg_close_table(&b, instance);
684 blobmsg_close_table(&b, instances);
686 return service_handle_set(NULL, NULL, NULL, "add", b.head);
689 void service_stopped(struct service *s)
691 if (s->deleted && avl_is_empty(&s->instances.avl)) {
692 service_event("service.stop", s->name, NULL);
693 avl_delete(&services, &s->avl);
695 service_validate_del(s);
701 void service_event(const char *type, const char *service, const char *instance)
703 ubus_event_bcast(type, "service", service, "instance", instance);
706 void ubus_init_service(struct ubus_context *_ctx)
709 ubus_add_object(ctx, &main_object);