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/avl-cmp.h>
20 struct avl_tree services;
21 static struct blob_buf b;
24 service_instance_add(struct service *s, struct blob_attr *attr)
26 struct service_instance *in;
28 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
31 in = calloc(1, sizeof(*in));
35 instance_init(in, s, attr);
36 vlist_add(&s->instances, &in->node, (void *) in->name);
40 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
41 struct vlist_node *node_old)
43 struct service_instance *in_o = NULL, *in_n = NULL;
46 in_o = container_of(node_old, struct service_instance, node);
49 in_n = container_of(node_new, struct service_instance, node);
52 DEBUG(1, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
53 instance_update(in_o, in_n);
56 DEBUG(1, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
57 instance_stop(in_o, false);
60 DEBUG(1, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
65 static struct service *
66 service_alloc(const char *name)
71 s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
72 strcpy(new_name, name);
74 vlist_init(&s->instances, avl_strcmp, service_instance_update);
75 s->instances.keep_old = true;
85 SERVICE_SET_INSTANCES,
90 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
91 [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
92 [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
93 [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
94 [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
98 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
100 struct blob_attr *cur;
103 s->trigger = tb[SERVICE_SET_TRIGGER];
105 if (tb[SERVICE_SET_INSTANCES]) {
107 vlist_update(&s->instances);
108 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
109 service_instance_add(s, cur);
112 vlist_flush(&s->instances);
119 service_delete(struct service *s)
121 vlist_flush_all(&s->instances);
122 avl_delete(&services, &s->avl);
132 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
133 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
137 SERVICE_DEL_ATTR_NAME,
138 SERVICE_DEL_ATTR_INSTANCE,
139 __SERVICE_DEL_ATTR_MAX,
142 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
143 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
144 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
148 SERVICE_LIST_ATTR_VERBOSE,
149 __SERVICE_LIST_ATTR_MAX,
152 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
153 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_INT32 },
162 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
163 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
164 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
168 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
169 struct ubus_request_data *req, const char *method,
170 struct blob_attr *msg)
172 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
173 struct service *s = NULL;
175 int ret = UBUS_STATUS_INVALID_ARGUMENT;
176 bool add = !strcmp(method, "add");
178 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
179 cur = tb[SERVICE_ATTR_NAME];
183 name = blobmsg_data(cur);
185 s = avl_find_element(&services, name, s, avl);
187 DEBUG(1, "Update service %s\n", name);
188 return service_update(s, msg, tb, add);
191 DEBUG(1, "Create service %s\n", name);
192 s = service_alloc(name);
194 return UBUS_STATUS_UNKNOWN_ERROR;
196 ret = service_update(s, msg, tb, add);
200 avl_insert(&services, &s->avl);
210 service_dump(struct service *s, int verbose)
212 struct service_instance *in;
215 c = blobmsg_open_table(&b, s->name);
216 i = blobmsg_open_table(&b, "instances");
217 if (verbose && s->trigger)
218 blobmsg_add_blob(&b, s->trigger);
219 vlist_for_each_element(&s->instances, in, node)
220 instance_dump(&b, in, verbose);
221 blobmsg_close_table(&b, i);
222 blobmsg_close_table(&b, c);
226 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
227 struct ubus_request_data *req, const char *method,
228 struct blob_attr *msg)
230 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
234 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
236 if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_u32(tb[SERVICE_LIST_ATTR_VERBOSE]))
239 blob_buf_init(&b, 0);
240 avl_for_each_element(&services, s, avl)
241 service_dump(s, verbose);
243 ubus_send_reply(ctx, req, b.head);
249 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
250 struct ubus_request_data *req, const char *method,
251 struct blob_attr *msg)
253 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
254 struct service *s, *tmp;
255 struct service_instance *in;
257 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
259 cur = tb[SERVICE_DEL_ATTR_NAME];
261 avl_for_each_element_safe(&services, s, avl, tmp)
266 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
268 return UBUS_STATUS_NOT_FOUND;
270 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
276 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
278 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
279 return UBUS_STATUS_NOT_FOUND;
282 vlist_delete(&s->instances, &in->node);
288 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
289 struct ubus_request_data *req, const char *method,
290 struct blob_attr *msg)
292 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
295 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
297 cur = tb[SERVICE_ATTR_NAME];
299 return UBUS_STATUS_INVALID_ARGUMENT;
301 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
303 return UBUS_STATUS_NOT_FOUND;
305 if (!strcmp(method, "update_start"))
306 vlist_update(&s->instances);
308 vlist_flush(&s->instances);
314 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
315 struct ubus_request_data *req, const char *method,
316 struct blob_attr *msg)
318 struct blob_attr *tb[__EVENT_MAX];
321 return UBUS_STATUS_INVALID_ARGUMENT;
323 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
324 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
325 return UBUS_STATUS_INVALID_ARGUMENT;
327 trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
337 static const struct blobmsg_policy trigger_policy[__TRIGGER_MAX] = {
338 [TRIGGER_ATTR] = { .name = "triggers", .type = BLOBMSG_TYPE_ARRAY },
341 static int service_handle_trigger(struct ubus_context *ctx, struct ubus_object *obj,
342 struct ubus_request_data *req, const char *method,
343 struct blob_attr *msg)
345 struct blob_attr *tb[__TRIGGER_MAX];
348 return UBUS_STATUS_INVALID_ARGUMENT;
350 blobmsg_parse(trigger_policy, __TRIGGER_MAX, tb, blob_data(msg), blob_len(msg));
351 if (!tb[TRIGGER_ATTR])
352 return UBUS_STATUS_INVALID_ARGUMENT;
354 trigger_add(tb[TRIGGER_ATTR], NULL);
359 static struct ubus_method main_object_methods[] = {
360 UBUS_METHOD("set", service_handle_set, service_set_attrs),
361 UBUS_METHOD("add", service_handle_set, service_set_attrs),
362 UBUS_METHOD("list", service_handle_list, service_attrs),
363 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
364 UBUS_METHOD("update_start", service_handle_update, service_attrs),
365 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
366 UBUS_METHOD("event", service_handle_event, event_policy),
367 UBUS_METHOD("trigger", service_handle_trigger, trigger_policy),
370 static struct ubus_object_type main_object_type =
371 UBUS_OBJECT_TYPE("service", main_object_methods);
373 static struct ubus_object main_object = {
375 .type = &main_object_type,
376 .methods = main_object_methods,
377 .n_methods = ARRAY_SIZE(main_object_methods),
380 void ubus_init_service(struct ubus_context *ctx)
382 avl_init(&services, avl_strcmp, false, NULL);
383 ubus_add_object(ctx, &main_object);