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);
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,
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 },
108 service_update(struct service *s, struct blob_attr **tb, bool add)
110 struct blob_attr *cur;
119 service_validate_del(s);
121 if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
122 s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
125 trigger_add(s->trigger, s);
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);
133 if (tb[SERVICE_SET_INSTANCES]) {
135 vlist_update(&s->instances);
136 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
137 service_instance_add(s, cur);
140 vlist_flush(&s->instances);
145 rc(s->name, "running");
151 service_delete(struct service *s)
153 vlist_flush_all(&s->instances);
163 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
164 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
168 SERVICE_DEL_ATTR_NAME,
169 SERVICE_DEL_ATTR_INSTANCE,
170 __SERVICE_DEL_ATTR_MAX,
173 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
174 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
175 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
179 SERVICE_LIST_ATTR_NAME,
180 SERVICE_LIST_ATTR_VERBOSE,
181 __SERVICE_LIST_ATTR_MAX,
184 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
185 [SERVICE_LIST_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
186 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
190 SERVICE_SIGNAL_ATTR_NAME,
191 SERVICE_SIGNAL_ATTR_INSTANCE,
192 SERVICE_SIGNAL_ATTR_SIGNAL,
193 __SERVICE_SIGNAL_ATTR_MAX,
196 static const struct blobmsg_policy service_signal_attrs[__SERVICE_SIGNAL_ATTR_MAX] = {
197 [SERVICE_SIGNAL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
198 [SERVICE_SIGNAL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
199 [SERVICE_SIGNAL_ATTR_SIGNAL] = { "signal", BLOBMSG_TYPE_INT32 },
208 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
209 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
210 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
220 static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
221 [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
222 [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
223 [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
233 static const struct blobmsg_policy get_data_policy[] = {
234 [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING },
235 [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
236 [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING },
240 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
241 struct ubus_request_data *req, const char *method,
242 struct blob_attr *msg)
244 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
245 struct service *s = NULL;
247 bool add = !strcmp(method, "add");
250 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
251 cur = tb[SERVICE_ATTR_NAME];
253 return UBUS_STATUS_INVALID_ARGUMENT;
255 name = blobmsg_data(cur);
257 s = avl_find_element(&services, name, s, avl);
259 DEBUG(2, "Update service %s\n", name);
260 return service_update(s, tb, add);
263 DEBUG(2, "Create service %s\n", name);
264 s = service_alloc(name);
266 return UBUS_STATUS_UNKNOWN_ERROR;
268 ret = service_update(s, tb, add);
272 avl_insert(&services, &s->avl);
274 service_event("service.start", s->name, NULL);
280 service_dump(struct service *s, bool verbose)
282 struct service_instance *in;
285 c = blobmsg_open_table(&b, s->name);
287 if (!avl_is_empty(&s->instances.avl)) {
288 i = blobmsg_open_table(&b, "instances");
289 vlist_for_each_element(&s->instances, in, node)
290 instance_dump(&b, in, verbose);
291 blobmsg_close_table(&b, i);
293 if (verbose && s->trigger)
294 blobmsg_add_blob(&b, s->trigger);
295 if (verbose && !list_empty(&s->validators))
296 service_validate_dump(&b, s);
297 blobmsg_close_table(&b, c);
301 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
302 struct ubus_request_data *req, const char *method,
303 struct blob_attr *msg)
305 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
307 const char *name = NULL;
308 bool verbose = false;
310 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
312 if (tb[SERVICE_LIST_ATTR_VERBOSE])
313 verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
314 if (tb[SERVICE_LIST_ATTR_NAME])
315 name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
317 blob_buf_init(&b, 0);
318 avl_for_each_element(&services, s, avl) {
319 if (name && strcmp(s->name, name) != 0)
322 service_dump(s, verbose);
325 ubus_send_reply(ctx, req, b.head);
331 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
332 struct ubus_request_data *req, const char *method,
333 struct blob_attr *msg)
335 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
337 struct service_instance *in;
339 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
341 cur = tb[SERVICE_DEL_ATTR_NAME];
343 return UBUS_STATUS_NOT_FOUND;
345 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
347 return UBUS_STATUS_NOT_FOUND;
349 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
355 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
357 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
358 return UBUS_STATUS_NOT_FOUND;
361 vlist_delete(&s->instances, &in->node);
367 service_handle_kill(struct service_instance *in, int sig)
369 if (kill(in->proc.pid, sig) == 0)
373 case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
374 case EPERM: return UBUS_STATUS_PERMISSION_DENIED;
375 case ESRCH: return UBUS_STATUS_NOT_FOUND;
378 return UBUS_STATUS_UNKNOWN_ERROR;
382 service_handle_signal(struct ubus_context *ctx, struct ubus_object *obj,
383 struct ubus_request_data *req, const char *method,
384 struct blob_attr *msg)
386 struct blob_attr *tb[__SERVICE_SIGNAL_ATTR_MAX], *cur;
388 struct service_instance *in;
392 blobmsg_parse(service_signal_attrs, __SERVICE_SIGNAL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
394 cur = tb[SERVICE_SIGNAL_ATTR_SIGNAL];
396 sig = blobmsg_get_u32(cur);
398 cur = tb[SERVICE_SIGNAL_ATTR_NAME];
400 return UBUS_STATUS_NOT_FOUND;
402 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
404 return UBUS_STATUS_NOT_FOUND;
406 cur = tb[SERVICE_SIGNAL_ATTR_INSTANCE];
408 vlist_for_each_element(&s->instances, in, node)
409 rv = service_handle_kill(in, sig);
414 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
416 ERROR("instance %s not found\n", blobmsg_get_string(cur));
417 return UBUS_STATUS_NOT_FOUND;
420 return service_handle_kill(in, sig);
424 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
425 struct ubus_request_data *req, const char *method,
426 struct blob_attr *msg)
428 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
431 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
433 cur = tb[SERVICE_SET_NAME];
435 return UBUS_STATUS_INVALID_ARGUMENT;
437 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
439 return UBUS_STATUS_NOT_FOUND;
441 if (!strcmp(method, "update_start"))
442 vlist_update(&s->instances);
444 vlist_flush(&s->instances);
450 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
451 struct ubus_request_data *req, const char *method,
452 struct blob_attr *msg)
454 struct blob_attr *tb[__EVENT_MAX];
457 return UBUS_STATUS_INVALID_ARGUMENT;
459 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
460 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
461 return UBUS_STATUS_INVALID_ARGUMENT;
463 trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
469 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
470 struct ubus_request_data *req, const char *method,
471 struct blob_attr *msg)
473 struct blob_attr *tb[__VALIDATE_MAX];
474 char *p = NULL, *t = NULL;
477 return UBUS_STATUS_INVALID_ARGUMENT;
479 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
480 if (tb[VALIDATE_SERVICE]) {
483 if (tb[VALIDATE_PACKAGE])
484 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
486 if (tb[VALIDATE_TYPE])
487 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
489 blob_buf_init(&b, 0);
490 service_validate_dump_all(&b, p, t);
491 ubus_send_reply(ctx, req, b.head);
497 service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
498 struct ubus_request_data *req, const char *method,
499 struct blob_attr *msg)
501 struct service_instance *in;
503 struct blob_attr *tb[__DATA_MAX];
504 const char *name = NULL;
505 const char *instance = NULL;
506 const char *type = NULL;
508 blobmsg_parse(get_data_policy, __DATA_MAX, tb, blob_data(msg), blob_len(msg));
510 name = blobmsg_data(tb[DATA_NAME]);
511 if (tb[DATA_INSTANCE])
512 instance = blobmsg_data(tb[DATA_INSTANCE]);
514 type = blobmsg_data(tb[DATA_TYPE]);
516 blob_buf_init(&b, 0);
517 avl_for_each_element(&services, s, avl) {
520 if (name && strcmp(name, s->name))
523 vlist_for_each_element(&s->instances, in, node) {
524 struct blobmsg_list_node *var;
527 if (instance && strcmp(instance, in->name))
530 blobmsg_list_for_each(&in->data, var) {
532 strcmp(blobmsg_name(var->data), type))
536 cs = blobmsg_open_table(&b, s->name);
538 ci = blobmsg_open_table(&b, in->name);
540 blobmsg_add_blob(&b, var->data);
544 blobmsg_close_table(&b, ci);
548 blobmsg_close_table(&b, cs);
551 ubus_send_reply(ctx, req, b.head);
555 static struct ubus_method main_object_methods[] = {
556 UBUS_METHOD("set", service_handle_set, service_set_attrs),
557 UBUS_METHOD("add", service_handle_set, service_set_attrs),
558 UBUS_METHOD("list", service_handle_list, service_list_attrs),
559 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
560 UBUS_METHOD("signal", service_handle_signal, service_signal_attrs),
561 UBUS_METHOD("update_start", service_handle_update, service_attrs),
562 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
563 UBUS_METHOD("event", service_handle_event, event_policy),
564 UBUS_METHOD("validate", service_handle_validate, validate_policy),
565 UBUS_METHOD("get_data", service_get_data, get_data_policy),
568 static struct ubus_object_type main_object_type =
569 UBUS_OBJECT_TYPE("service", main_object_methods);
571 static struct ubus_object main_object = {
573 .type = &main_object_type,
574 .methods = main_object_methods,
575 .n_methods = ARRAY_SIZE(main_object_methods),
579 service_start_early(char *name, char *cmdline)
581 void *instances, *instance, *command, *respawn;
584 blob_buf_init(&b, 0);
585 blobmsg_add_string(&b, "name", name);
586 instances = blobmsg_open_table(&b, "instances");
587 instance = blobmsg_open_table(&b, "instance1");
588 command = blobmsg_open_array(&b, "command");
589 t = strtok(cmdline, " ");
591 blobmsg_add_string(&b, NULL, t);
592 t = strtok(NULL, " ");
594 blobmsg_close_array(&b, command);
595 respawn = blobmsg_open_array(&b, "respawn");
596 blobmsg_add_string(&b, NULL, "3600");
597 blobmsg_add_string(&b, NULL, "1");
598 blobmsg_add_string(&b, NULL, "0");
599 blobmsg_close_array(&b, respawn);
600 blobmsg_close_table(&b, instance);
601 blobmsg_close_table(&b, instances);
603 return service_handle_set(NULL, NULL, NULL, "add", b.head);
606 void service_stopped(struct service *s)
608 if (s->deleted && avl_is_empty(&s->instances.avl)) {
609 service_event("service.stop", s->name, NULL);
610 avl_delete(&services, &s->avl);
614 service_validate_del(s);
618 void service_event(const char *type, const char *service, const char *instance)
623 blob_buf_init(&b, 0);
624 blobmsg_add_string(&b, "service", service);
626 blobmsg_add_string(&b, "instance", instance);
627 ubus_notify(ctx, &main_object, type, b.head, -1);
630 void ubus_init_service(struct ubus_context *_ctx)
633 ubus_add_object(ctx, &main_object);