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 struct avl_tree services;
26 static struct blob_buf b;
29 service_instance_add(struct service *s, struct blob_attr *attr)
31 struct service_instance *in;
33 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
36 in = calloc(1, sizeof(*in));
40 instance_init(in, s, attr);
41 vlist_add(&s->instances, &in->node, (void *) in->name);
45 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
46 struct vlist_node *node_old)
48 struct service_instance *in_o = NULL, *in_n = NULL;
51 in_o = container_of(node_old, struct service_instance, node);
54 in_n = container_of(node_new, struct service_instance, node);
57 DEBUG(2, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
58 instance_update(in_o, in_n);
61 DEBUG(2, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
65 DEBUG(2, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
70 static struct service *
71 service_alloc(const char *name)
76 s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
77 strcpy(new_name, name);
79 vlist_init(&s->instances, avl_strcmp, service_instance_update);
80 s->instances.keep_old = true;
83 INIT_LIST_HEAD(&s->validators);
91 SERVICE_SET_INSTANCES,
97 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
98 [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
99 [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
100 [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
101 [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
102 [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
106 service_update(struct service *s, struct blob_attr **tb, bool add)
108 struct blob_attr *cur;
117 service_validate_del(s);
119 if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
120 s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
123 trigger_add(s->trigger, s);
126 if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
127 blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
128 service_validate_add(s, cur);
131 if (tb[SERVICE_SET_INSTANCES]) {
133 vlist_update(&s->instances);
134 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
135 service_instance_add(s, cur);
138 vlist_flush(&s->instances);
141 rc(s->name, "running");
147 service_delete(struct service *s)
149 vlist_flush_all(&s->instances);
150 avl_delete(&services, &s->avl);
154 service_validate_del(s);
162 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
163 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
167 SERVICE_DEL_ATTR_NAME,
168 SERVICE_DEL_ATTR_INSTANCE,
169 __SERVICE_DEL_ATTR_MAX,
172 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
173 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
174 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
178 SERVICE_LIST_ATTR_VERBOSE,
179 __SERVICE_LIST_ATTR_MAX,
182 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
183 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
192 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
193 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
194 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
204 static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
205 [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
206 [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
207 [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
211 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
212 struct ubus_request_data *req, const char *method,
213 struct blob_attr *msg)
215 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
216 struct service *s = NULL;
218 bool add = !strcmp(method, "add");
221 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
222 cur = tb[SERVICE_ATTR_NAME];
224 return UBUS_STATUS_INVALID_ARGUMENT;
226 name = blobmsg_data(cur);
228 s = avl_find_element(&services, name, s, avl);
230 DEBUG(2, "Update service %s\n", name);
231 return service_update(s, tb, add);
234 DEBUG(2, "Create service %s\n", name);
235 s = service_alloc(name);
237 return UBUS_STATUS_UNKNOWN_ERROR;
239 ret = service_update(s, tb, add);
243 avl_insert(&services, &s->avl);
249 service_dump(struct service *s, int verbose)
251 struct service_instance *in;
254 c = blobmsg_open_table(&b, s->name);
256 if (!avl_is_empty(&s->instances.avl)) {
257 i = blobmsg_open_table(&b, "instances");
258 vlist_for_each_element(&s->instances, in, node)
259 instance_dump(&b, in, verbose);
260 blobmsg_close_table(&b, i);
262 if (verbose && s->trigger)
263 blobmsg_add_blob(&b, s->trigger);
264 if (verbose && !list_empty(&s->validators))
265 service_validate_dump(&b, s);
266 blobmsg_close_table(&b, c);
270 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
271 struct ubus_request_data *req, const char *method,
272 struct blob_attr *msg)
274 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
278 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
280 if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
283 blob_buf_init(&b, 0);
284 avl_for_each_element(&services, s, avl)
285 service_dump(s, verbose);
287 ubus_send_reply(ctx, req, b.head);
293 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
294 struct ubus_request_data *req, const char *method,
295 struct blob_attr *msg)
297 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
299 struct service_instance *in;
301 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
303 cur = tb[SERVICE_DEL_ATTR_NAME];
305 return UBUS_STATUS_NOT_FOUND;
307 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
309 return UBUS_STATUS_NOT_FOUND;
311 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
317 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
319 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
320 return UBUS_STATUS_NOT_FOUND;
323 vlist_delete(&s->instances, &in->node);
329 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
330 struct ubus_request_data *req, const char *method,
331 struct blob_attr *msg)
333 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
336 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
338 cur = tb[SERVICE_ATTR_NAME];
340 return UBUS_STATUS_INVALID_ARGUMENT;
342 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
344 return UBUS_STATUS_NOT_FOUND;
346 if (!strcmp(method, "update_start"))
347 vlist_update(&s->instances);
349 vlist_flush(&s->instances);
355 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
356 struct ubus_request_data *req, const char *method,
357 struct blob_attr *msg)
359 struct blob_attr *tb[__EVENT_MAX];
362 return UBUS_STATUS_INVALID_ARGUMENT;
364 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
365 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
366 return UBUS_STATUS_INVALID_ARGUMENT;
368 trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
374 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
375 struct ubus_request_data *req, const char *method,
376 struct blob_attr *msg)
378 struct blob_attr *tb[__VALIDATE_MAX];
379 char *p = NULL, *t = NULL;
382 return UBUS_STATUS_INVALID_ARGUMENT;
384 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
385 if (tb[VALIDATE_SERVICE]) {
388 if (tb[VALIDATE_PACKAGE])
389 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
391 if (tb[VALIDATE_TYPE])
392 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
394 blob_buf_init(&b, 0);
395 service_validate_dump_all(&b, p, t);
396 ubus_send_reply(ctx, req, b.head);
401 static struct ubus_method main_object_methods[] = {
402 UBUS_METHOD("set", service_handle_set, service_set_attrs),
403 UBUS_METHOD("add", service_handle_set, service_set_attrs),
404 UBUS_METHOD("list", service_handle_list, service_attrs),
405 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
406 UBUS_METHOD("update_start", service_handle_update, service_attrs),
407 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
408 UBUS_METHOD("event", service_handle_event, event_policy),
409 UBUS_METHOD("validate", service_handle_validate, validate_policy),
412 static struct ubus_object_type main_object_type =
413 UBUS_OBJECT_TYPE("service", main_object_methods);
415 static struct ubus_object main_object = {
417 .type = &main_object_type,
418 .methods = main_object_methods,
419 .n_methods = ARRAY_SIZE(main_object_methods),
423 service_start_early(char *name, char *cmdline)
425 void *instances, *instance, *command, *respawn;
428 blob_buf_init(&b, 0);
429 blobmsg_add_string(&b, "name", name);
430 instances = blobmsg_open_table(&b, "instances");
431 instance = blobmsg_open_table(&b, "instance1");
432 command = blobmsg_open_array(&b, "command");
433 t = strtok(cmdline, " ");
435 blobmsg_add_string(&b, NULL, t);
436 t = strtok(NULL, " ");
438 blobmsg_close_array(&b, command);
439 respawn = blobmsg_open_array(&b, "respawn");
440 blobmsg_add_string(&b, NULL, "3600");
441 blobmsg_add_string(&b, NULL, "1");
442 blobmsg_add_string(&b, NULL, "0");
443 blobmsg_close_array(&b, respawn);
444 blobmsg_close_table(&b, instance);
445 blobmsg_close_table(&b, instances);
447 return service_handle_set(NULL, NULL, NULL, "add", b.head);
450 void ubus_init_service(struct ubus_context *ctx)
452 ubus_add_object(ctx, &main_object);
458 avl_init(&services, avl_strcmp, false, NULL);
459 service_validate_init();