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 *config, 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);
155 service_validate_del(s);
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_VERBOSE,
180 __SERVICE_LIST_ATTR_MAX,
183 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
184 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
193 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
194 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
195 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
205 static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
206 [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
207 [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
208 [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
212 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
213 struct ubus_request_data *req, const char *method,
214 struct blob_attr *msg)
216 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
217 struct service *s = NULL;
219 int ret = UBUS_STATUS_INVALID_ARGUMENT;
220 bool add = !strcmp(method, "add");
222 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
223 cur = tb[SERVICE_ATTR_NAME];
227 name = blobmsg_data(cur);
229 s = avl_find_element(&services, name, s, avl);
231 DEBUG(2, "Update service %s\n", name);
232 return service_update(s, msg, tb, add);
235 DEBUG(2, "Create service %s\n", name);
236 s = service_alloc(name);
238 return UBUS_STATUS_UNKNOWN_ERROR;
240 ret = service_update(s, msg, tb, add);
244 avl_insert(&services, &s->avl);
254 service_dump(struct service *s, int verbose)
256 struct service_instance *in;
259 c = blobmsg_open_table(&b, s->name);
261 if (!avl_is_empty(&s->instances.avl)) {
262 i = blobmsg_open_table(&b, "instances");
263 vlist_for_each_element(&s->instances, in, node)
264 instance_dump(&b, in, verbose);
265 blobmsg_close_table(&b, i);
267 if (verbose && s->trigger)
268 blobmsg_add_blob(&b, s->trigger);
269 if (verbose && !list_empty(&s->validators))
270 service_validate_dump(&b, s);
271 blobmsg_close_table(&b, c);
275 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
276 struct ubus_request_data *req, const char *method,
277 struct blob_attr *msg)
279 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
283 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
285 if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
288 blob_buf_init(&b, 0);
289 avl_for_each_element(&services, s, avl)
290 service_dump(s, verbose);
292 ubus_send_reply(ctx, req, b.head);
298 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
299 struct ubus_request_data *req, const char *method,
300 struct blob_attr *msg)
302 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
304 struct service_instance *in;
306 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
308 cur = tb[SERVICE_DEL_ATTR_NAME];
310 return UBUS_STATUS_NOT_FOUND;
312 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
314 return UBUS_STATUS_NOT_FOUND;
316 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
322 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
324 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
325 return UBUS_STATUS_NOT_FOUND;
328 vlist_delete(&s->instances, &in->node);
334 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
335 struct ubus_request_data *req, const char *method,
336 struct blob_attr *msg)
338 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
341 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
343 cur = tb[SERVICE_ATTR_NAME];
345 return UBUS_STATUS_INVALID_ARGUMENT;
347 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
349 return UBUS_STATUS_NOT_FOUND;
351 if (!strcmp(method, "update_start"))
352 vlist_update(&s->instances);
354 vlist_flush(&s->instances);
360 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
361 struct ubus_request_data *req, const char *method,
362 struct blob_attr *msg)
364 struct blob_attr *tb[__EVENT_MAX];
367 return UBUS_STATUS_INVALID_ARGUMENT;
369 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
370 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
371 return UBUS_STATUS_INVALID_ARGUMENT;
373 trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
379 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
380 struct ubus_request_data *req, const char *method,
381 struct blob_attr *msg)
383 struct blob_attr *tb[__VALIDATE_MAX];
384 char *p = NULL, *t = NULL;
387 return UBUS_STATUS_INVALID_ARGUMENT;
389 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
390 if (tb[VALIDATE_SERVICE]) {
393 if (tb[VALIDATE_PACKAGE])
394 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
396 if (tb[VALIDATE_TYPE])
397 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
399 blob_buf_init(&b, 0);
400 service_validate_dump_all(&b, p, t);
401 ubus_send_reply(ctx, req, b.head);
406 static struct ubus_method main_object_methods[] = {
407 UBUS_METHOD("set", service_handle_set, service_set_attrs),
408 UBUS_METHOD("add", service_handle_set, service_set_attrs),
409 UBUS_METHOD("list", service_handle_list, service_attrs),
410 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
411 UBUS_METHOD("update_start", service_handle_update, service_attrs),
412 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
413 UBUS_METHOD("event", service_handle_event, event_policy),
414 UBUS_METHOD("validate", service_handle_validate, validate_policy),
417 static struct ubus_object_type main_object_type =
418 UBUS_OBJECT_TYPE("service", main_object_methods);
420 static struct ubus_object main_object = {
422 .type = &main_object_type,
423 .methods = main_object_methods,
424 .n_methods = ARRAY_SIZE(main_object_methods),
428 service_start_early(char *name, char *cmdline)
430 void *instances, *instance, *command, *respawn;
433 blob_buf_init(&b, 0);
434 blobmsg_add_string(&b, "name", name);
435 instances = blobmsg_open_table(&b, "instances");
436 instance = blobmsg_open_table(&b, "instance1");
437 command = blobmsg_open_array(&b, "command");
438 t = strtok(cmdline, " ");
440 blobmsg_add_string(&b, NULL, t);
441 t = strtok(NULL, " ");
443 blobmsg_close_array(&b, command);
444 respawn = blobmsg_open_array(&b, "respawn");
445 blobmsg_add_string(&b, NULL, "3600");
446 blobmsg_add_string(&b, NULL, "1");
447 blobmsg_add_string(&b, NULL, "0");
448 blobmsg_close_array(&b, respawn);
449 blobmsg_close_table(&b, instance);
450 blobmsg_close_table(&b, instances);
452 return service_handle_set(NULL, NULL, NULL, "add", b.head);
455 void ubus_init_service(struct ubus_context *ctx)
457 ubus_add_object(ctx, &main_object);
463 avl_init(&services, avl_strcmp, false, NULL);
464 service_validate_init();