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,
89 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
90 [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
91 [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
92 [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
97 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
99 struct blob_attr *cur;
102 if (tb[SERVICE_SET_INSTANCES]) {
104 vlist_update(&s->instances);
105 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
106 service_instance_add(s, cur);
109 vlist_flush(&s->instances);
116 service_delete(struct service *s)
118 vlist_flush_all(&s->instances);
119 avl_delete(&services, &s->avl);
129 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
130 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
134 SERVICE_DEL_ATTR_NAME,
135 SERVICE_DEL_ATTR_INSTANCE,
136 __SERVICE_DEL_ATTR_MAX,
139 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
140 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
141 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
146 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
147 struct ubus_request_data *req, const char *method,
148 struct blob_attr *msg)
150 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
151 struct service *s = NULL;
153 int ret = UBUS_STATUS_INVALID_ARGUMENT;
154 bool add = !strcmp(method, "add");
156 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
157 cur = tb[SERVICE_ATTR_NAME];
161 name = blobmsg_data(cur);
163 s = avl_find_element(&services, name, s, avl);
165 DEBUG(1, "Update service %s\n", name);
166 return service_update(s, msg, tb, add);
169 DEBUG(1, "Create service %s\n", name);
170 s = service_alloc(name);
172 return UBUS_STATUS_UNKNOWN_ERROR;
174 ret = service_update(s, msg, tb, add);
178 avl_insert(&services, &s->avl);
188 service_dump(struct service *s)
190 struct service_instance *in;
193 c = blobmsg_open_table(&b, s->name);
194 i = blobmsg_open_table(&b, "instances");
195 vlist_for_each_element(&s->instances, in, node)
196 instance_dump(&b, in);
197 blobmsg_close_table(&b, i);
198 blobmsg_close_table(&b, c);
202 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
203 struct ubus_request_data *req, const char *method,
204 struct blob_attr *msg)
208 blob_buf_init(&b, 0);
209 avl_for_each_element(&services, s, avl)
212 ubus_send_reply(ctx, req, b.head);
218 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
219 struct ubus_request_data *req, const char *method,
220 struct blob_attr *msg)
222 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
223 struct service *s, *tmp;
224 struct service_instance *in;
226 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
228 cur = tb[SERVICE_DEL_ATTR_NAME];
230 avl_for_each_element_safe(&services, s, avl, tmp)
235 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
237 return UBUS_STATUS_NOT_FOUND;
239 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
245 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
247 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
248 return UBUS_STATUS_NOT_FOUND;
251 vlist_delete(&s->instances, &in->node);
257 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
258 struct ubus_request_data *req, const char *method,
259 struct blob_attr *msg)
261 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
264 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
266 cur = tb[SERVICE_ATTR_NAME];
268 return UBUS_STATUS_INVALID_ARGUMENT;
270 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
272 return UBUS_STATUS_NOT_FOUND;
274 if (!strcmp(method, "update_start"))
275 vlist_update(&s->instances);
277 vlist_flush(&s->instances);
282 static struct ubus_method main_object_methods[] = {
283 UBUS_METHOD("set", service_handle_set, service_set_attrs),
284 UBUS_METHOD("add", service_handle_set, service_set_attrs),
285 UBUS_METHOD("list", service_handle_list, service_attrs),
286 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
287 UBUS_METHOD("update_start", service_handle_update, service_attrs),
288 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
291 static struct ubus_object_type main_object_type =
292 UBUS_OBJECT_TYPE("service", main_object_methods);
294 static struct ubus_object main_object = {
296 .type = &main_object_type,
297 .methods = main_object_methods,
298 .n_methods = ARRAY_SIZE(main_object_methods),
301 void ubus_init_service(struct ubus_context *ctx)
303 avl_init(&services, avl_strcmp, false, NULL);
304 ubus_add_object(ctx, &main_object);