1 #include <libubox/avl-cmp.h>
6 struct avl_tree services;
7 static struct blob_buf b;
10 service_instance_add(struct service *s, struct blob_attr *attr)
12 struct service_instance *in;
13 const char *name = blobmsg_name(attr);
15 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
18 in = calloc(1, sizeof(*in));
22 instance_init(in, s, attr);
23 vlist_add(&s->instances, &in->node, (void *) name);
27 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
28 struct vlist_node *node_old)
30 struct service_instance *in_o = NULL, *in_n = NULL;
33 in_o = container_of(node_old, struct service_instance, node);
36 in_n = container_of(node_new, struct service_instance, node);
39 DPRINTF("Update instance %s::%s\n", in_o->srv->name, in_o->name);
40 instance_update(in_o, in_n);
43 DPRINTF("Free instance %s::%s\n", in_o->srv->name, in_o->name);
44 instance_stop(in_o, false);
47 DPRINTF("Create instance %s::%s\n", in_o->srv->name, in_o->name);
52 static struct service *
53 service_alloc(const char *name)
57 s = calloc(1, sizeof(*s));
58 vlist_init(&s->instances, avl_strcmp, service_instance_update);
59 s->instances.keep_old = true;
67 SERVICE_ATTR_INSTANCES,
71 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
72 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
73 [SERVICE_ATTR_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
74 [SERVICE_ATTR_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
79 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb)
81 struct blob_attr *old_config = s->config;
82 struct blob_attr *cur;
85 /* only the pointer changes, the content stays the same,
86 * no avl update necessary */
87 s->name = s->avl.key = blobmsg_data(tb[SERVICE_ATTR_NAME]);
90 if (tb[SERVICE_ATTR_INSTANCES]) {
91 vlist_update(&s->instances);
92 blobmsg_for_each_attr(cur, tb[SERVICE_ATTR_INSTANCES], rem) {
93 service_instance_add(s, cur);
95 vlist_flush(&s->instances);
104 service_delete(struct service *s)
106 vlist_flush_all(&s->instances);
107 avl_delete(&services, &s->avl);
113 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
114 struct ubus_request_data *req, const char *method,
115 struct blob_attr *msg)
117 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
118 struct service *s = NULL;
120 int ret = UBUS_STATUS_INVALID_ARGUMENT;
122 msg = blob_memdup(msg);
124 return UBUS_STATUS_UNKNOWN_ERROR;
126 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
127 cur = tb[SERVICE_ATTR_NAME];
131 name = blobmsg_data(cur);
133 s = avl_find_element(&services, name, s, avl);
135 DPRINTF("Update service %s\n", name);
136 return service_update(s, msg, tb);
139 DPRINTF("Create service %s\n", name);
140 s = service_alloc(name);
142 return UBUS_STATUS_UNKNOWN_ERROR;
144 ret = service_update(s, msg, tb);
148 avl_insert(&services, &s->avl);
158 service_dump(struct service *s)
160 struct service_instance *in;
163 c = blobmsg_open_table(&b, s->name);
164 i = blobmsg_open_table(&b, "instances");
165 vlist_for_each_element(&s->instances, in, node)
166 instance_dump(&b, in);
167 blobmsg_close_table(&b, i);
168 blobmsg_close_table(&b, c);
172 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
173 struct ubus_request_data *req, const char *method,
174 struct blob_attr *msg)
178 blob_buf_init(&b, 0);
179 avl_for_each_element(&services, s, avl)
182 ubus_send_reply(ctx, req, b.head);
192 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_MAX] = {
193 [SERVICE_DEL_NAME] = { "name", BLOBMSG_TYPE_STRING },
198 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
199 struct ubus_request_data *req, const char *method,
200 struct blob_attr *msg)
202 struct blob_attr *tb[__SERVICE_DEL_MAX], *cur;
203 struct service *s, *tmp;
205 blobmsg_parse(service_del_attrs, __SERVICE_DEL_MAX, tb, blob_data(msg), blob_len(msg));
207 cur = tb[SERVICE_ATTR_NAME];
209 avl_for_each_element_safe(&services, s, avl, tmp)
214 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
216 return UBUS_STATUS_NOT_FOUND;
222 static struct ubus_method main_object_methods[] = {
223 { .name = "list", .handler = service_handle_list },
224 { .name = "set", .handler = service_handle_set },
225 { .name = "delete", .handler = service_handle_delete },
228 static struct ubus_object_type main_object_type =
229 UBUS_OBJECT_TYPE("service", main_object_methods);
231 static struct ubus_object main_object = {
233 .type = &main_object_type,
234 .methods = main_object_methods,
235 .n_methods = ARRAY_SIZE(main_object_methods),
238 void procd_init_service(struct ubus_context *ctx)
240 avl_init(&services, avl_strcmp, false, NULL);
241 ubus_add_object(ctx, &main_object);