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>
22 struct avl_tree services;
23 static struct blob_buf b;
26 service_instance_add(struct service *s, struct blob_attr *attr)
28 struct service_instance *in;
30 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
33 in = calloc(1, sizeof(*in));
37 instance_init(in, s, attr);
38 vlist_add(&s->instances, &in->node, (void *) in->name);
42 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
43 struct vlist_node *node_old)
45 struct service_instance *in_o = NULL, *in_n = NULL;
48 in_o = container_of(node_old, struct service_instance, node);
51 in_n = container_of(node_new, struct service_instance, node);
54 DEBUG(1, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
55 instance_update(in_o, in_n);
58 DEBUG(1, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
62 DEBUG(1, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
67 static struct service *
68 service_alloc(const char *name)
73 s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
74 strcpy(new_name, name);
76 vlist_init(&s->instances, avl_strcmp, service_instance_update);
77 s->instances.keep_old = true;
87 SERVICE_SET_INSTANCES,
92 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
93 [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
94 [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
95 [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
96 [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
100 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
102 struct blob_attr *cur;
111 if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
112 s->trigger = malloc(blob_pad_len(tb[SERVICE_SET_TRIGGER]));
115 memcpy(s->trigger, tb[SERVICE_SET_TRIGGER], blob_pad_len(tb[SERVICE_SET_TRIGGER]));
116 trigger_add(s->trigger, s);
119 if (tb[SERVICE_SET_INSTANCES]) {
121 vlist_update(&s->instances);
122 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
123 service_instance_add(s, cur);
126 vlist_flush(&s->instances);
129 rc(s->name, "running");
135 service_delete(struct service *s)
137 vlist_flush_all(&s->instances);
138 avl_delete(&services, &s->avl);
150 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
151 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
155 SERVICE_DEL_ATTR_NAME,
156 SERVICE_DEL_ATTR_INSTANCE,
157 __SERVICE_DEL_ATTR_MAX,
160 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
161 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
162 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
166 SERVICE_LIST_ATTR_VERBOSE,
167 __SERVICE_LIST_ATTR_MAX,
170 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
171 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
180 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
181 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
182 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
186 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
187 struct ubus_request_data *req, const char *method,
188 struct blob_attr *msg)
190 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
191 struct service *s = NULL;
193 int ret = UBUS_STATUS_INVALID_ARGUMENT;
194 bool add = !strcmp(method, "add");
196 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
197 cur = tb[SERVICE_ATTR_NAME];
201 name = blobmsg_data(cur);
203 s = avl_find_element(&services, name, s, avl);
205 DEBUG(1, "Update service %s\n", name);
206 return service_update(s, msg, tb, add);
209 DEBUG(1, "Create service %s\n", name);
210 s = service_alloc(name);
212 return UBUS_STATUS_UNKNOWN_ERROR;
214 ret = service_update(s, msg, tb, add);
218 avl_insert(&services, &s->avl);
228 service_dump(struct service *s, int verbose)
230 struct service_instance *in;
233 c = blobmsg_open_table(&b, s->name);
235 if (avl_is_empty(&s->instances.avl)) {
236 blobmsg_close_table(&b, c);
240 i = blobmsg_open_table(&b, "instances");
241 vlist_for_each_element(&s->instances, in, node)
242 instance_dump(&b, in, verbose);
243 blobmsg_close_table(&b, i);
244 if (verbose && s->trigger)
245 blobmsg_add_blob(&b, s->trigger);
246 blobmsg_close_table(&b, c);
250 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
251 struct ubus_request_data *req, const char *method,
252 struct blob_attr *msg)
254 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
258 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
260 if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
263 blob_buf_init(&b, 0);
264 avl_for_each_element(&services, s, avl)
265 service_dump(s, verbose);
267 ubus_send_reply(ctx, req, b.head);
273 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
274 struct ubus_request_data *req, const char *method,
275 struct blob_attr *msg)
277 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
279 struct service_instance *in;
281 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
283 cur = tb[SERVICE_DEL_ATTR_NAME];
285 return UBUS_STATUS_NOT_FOUND;
287 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
289 return UBUS_STATUS_NOT_FOUND;
291 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
297 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
299 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
300 return UBUS_STATUS_NOT_FOUND;
303 vlist_delete(&s->instances, &in->node);
309 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
310 struct ubus_request_data *req, const char *method,
311 struct blob_attr *msg)
313 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
316 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
318 cur = tb[SERVICE_ATTR_NAME];
320 return UBUS_STATUS_INVALID_ARGUMENT;
322 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
324 return UBUS_STATUS_NOT_FOUND;
326 if (!strcmp(method, "update_start"))
327 vlist_update(&s->instances);
329 vlist_flush(&s->instances);
335 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
336 struct ubus_request_data *req, const char *method,
337 struct blob_attr *msg)
339 struct blob_attr *tb[__EVENT_MAX];
342 return UBUS_STATUS_INVALID_ARGUMENT;
344 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
345 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
346 return UBUS_STATUS_INVALID_ARGUMENT;
348 trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
353 static struct ubus_method main_object_methods[] = {
354 UBUS_METHOD("set", service_handle_set, service_set_attrs),
355 UBUS_METHOD("add", service_handle_set, service_set_attrs),
356 UBUS_METHOD("list", service_handle_list, service_attrs),
357 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
358 UBUS_METHOD("update_start", service_handle_update, service_attrs),
359 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
360 UBUS_METHOD("event", service_handle_event, event_policy),
363 static struct ubus_object_type main_object_type =
364 UBUS_OBJECT_TYPE("service", main_object_methods);
366 static struct ubus_object main_object = {
368 .type = &main_object_type,
369 .methods = main_object_methods,
370 .n_methods = ARRAY_SIZE(main_object_methods),
373 void ubus_init_service(struct ubus_context *ctx)
375 avl_init(&services, avl_strcmp, false, NULL);
376 ubus_add_object(ctx, &main_object);