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>
21 struct avl_tree services;
22 static struct blob_buf b;
25 service_instance_add(struct service *s, struct blob_attr *attr)
27 struct service_instance *in;
29 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
32 in = calloc(1, sizeof(*in));
36 instance_init(in, s, attr);
37 vlist_add(&s->instances, &in->node, (void *) in->name);
41 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
42 struct vlist_node *node_old)
44 struct service_instance *in_o = NULL, *in_n = NULL;
47 in_o = container_of(node_old, struct service_instance, node);
50 in_n = container_of(node_new, struct service_instance, node);
53 DEBUG(1, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
54 instance_update(in_o, in_n);
57 DEBUG(1, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
58 instance_stop(in_o, false);
61 DEBUG(1, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
66 static struct service *
67 service_alloc(const char *name)
72 s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
73 strcpy(new_name, name);
75 vlist_init(&s->instances, avl_strcmp, service_instance_update);
76 s->instances.keep_old = true;
86 SERVICE_SET_INSTANCES,
91 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
92 [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
93 [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
94 [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
95 [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
99 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
101 struct blob_attr *cur;
107 if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
108 s->trigger = tb[SERVICE_SET_TRIGGER];
109 trigger_add(s->trigger, s);
112 if (tb[SERVICE_SET_INSTANCES]) {
114 vlist_update(&s->instances);
115 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
116 service_instance_add(s, cur);
119 vlist_flush(&s->instances);
126 service_delete(struct service *s)
128 vlist_flush_all(&s->instances);
129 avl_delete(&services, &s->avl);
140 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
141 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
145 SERVICE_DEL_ATTR_NAME,
146 SERVICE_DEL_ATTR_INSTANCE,
147 __SERVICE_DEL_ATTR_MAX,
150 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
151 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
152 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
156 SERVICE_LIST_ATTR_VERBOSE,
157 __SERVICE_LIST_ATTR_MAX,
160 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
161 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
170 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
171 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
172 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
176 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
177 struct ubus_request_data *req, const char *method,
178 struct blob_attr *msg)
180 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
181 struct service *s = NULL;
183 int ret = UBUS_STATUS_INVALID_ARGUMENT;
184 bool add = !strcmp(method, "add");
186 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
187 cur = tb[SERVICE_ATTR_NAME];
191 name = blobmsg_data(cur);
193 s = avl_find_element(&services, name, s, avl);
195 DEBUG(1, "Update service %s\n", name);
196 return service_update(s, msg, tb, add);
199 DEBUG(1, "Create service %s\n", name);
200 s = service_alloc(name);
202 return UBUS_STATUS_UNKNOWN_ERROR;
204 ret = service_update(s, msg, tb, add);
208 avl_insert(&services, &s->avl);
218 service_dump(struct service *s, int verbose)
220 struct service_instance *in;
223 if (avl_is_empty(&s->instances.avl) && !verbose)
226 c = blobmsg_open_table(&b, s->name);
227 if (verbose && s->trigger)
228 blobmsg_add_blob(&b, s->trigger);
229 i = blobmsg_open_table(&b, "instances");
230 vlist_for_each_element(&s->instances, in, node)
231 instance_dump(&b, in, verbose);
232 blobmsg_close_table(&b, i);
233 blobmsg_close_table(&b, c);
237 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
238 struct ubus_request_data *req, const char *method,
239 struct blob_attr *msg)
241 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
245 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
247 if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
250 blob_buf_init(&b, 0);
251 avl_for_each_element(&services, s, avl)
252 service_dump(s, verbose);
254 ubus_send_reply(ctx, req, b.head);
260 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
261 struct ubus_request_data *req, const char *method,
262 struct blob_attr *msg)
264 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
266 struct service_instance *in;
268 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
270 cur = tb[SERVICE_DEL_ATTR_NAME];
272 return UBUS_STATUS_NOT_FOUND;
274 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
276 return UBUS_STATUS_NOT_FOUND;
278 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
284 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
286 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
287 return UBUS_STATUS_NOT_FOUND;
290 vlist_delete(&s->instances, &in->node);
296 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
297 struct ubus_request_data *req, const char *method,
298 struct blob_attr *msg)
300 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
303 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
305 cur = tb[SERVICE_ATTR_NAME];
307 return UBUS_STATUS_INVALID_ARGUMENT;
309 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
311 return UBUS_STATUS_NOT_FOUND;
313 if (!strcmp(method, "update_start"))
314 vlist_update(&s->instances);
316 vlist_flush(&s->instances);
322 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
323 struct ubus_request_data *req, const char *method,
324 struct blob_attr *msg)
326 struct blob_attr *tb[__EVENT_MAX];
329 return UBUS_STATUS_INVALID_ARGUMENT;
331 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
332 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
333 return UBUS_STATUS_INVALID_ARGUMENT;
335 trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
340 static struct ubus_method main_object_methods[] = {
341 UBUS_METHOD("set", service_handle_set, service_set_attrs),
342 UBUS_METHOD("add", service_handle_set, service_set_attrs),
343 UBUS_METHOD("list", service_handle_list, service_attrs),
344 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
345 UBUS_METHOD("update_start", service_handle_update, service_attrs),
346 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
347 UBUS_METHOD("event", service_handle_event, event_policy),
350 static struct ubus_object_type main_object_type =
351 UBUS_OBJECT_TYPE("service", main_object_methods);
353 static struct ubus_object main_object = {
355 .type = &main_object_type,
356 .methods = main_object_methods,
357 .n_methods = ARRAY_SIZE(main_object_methods),
360 void ubus_init_service(struct ubus_context *ctx)
362 avl_init(&services, avl_strcmp, false, NULL);
363 ubus_add_object(ctx, &main_object);