1 #include <libubox/avl-cmp.h>
5 struct avl_tree services;
6 static struct blob_buf b;
9 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
10 struct vlist_node *node_old)
12 struct service_instance *in_o = NULL, *in_n = NULL;
15 in_o = container_of(node_old, struct service_instance, node);
18 in_n = container_of(node_new, struct service_instance, node);
24 /* full match, nothing to do */
29 /* kill old process */
34 /* start new process */
39 service_instance_add(struct service *s, struct blob_attr *attr)
41 struct service_instance *in;
42 const char *name = blobmsg_name(attr);
44 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
47 in = calloc(1, sizeof(*in));
52 vlist_add(&s->instances, &in->node, (void *) name);
55 static struct service *
56 service_alloc(const char *name)
60 s = calloc(1, sizeof(*s));
61 vlist_init(&s->instances, avl_strcmp, service_instance_update);
69 SERVICE_ATTR_INSTANCES,
73 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
74 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
75 [SERVICE_ATTR_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
76 [SERVICE_ATTR_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
81 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb)
83 struct blob_attr *old_config = s->config;
84 struct blob_attr *cur;
87 /* only the pointer changes, the content stays the same,
88 * no avl update necessary */
89 s->name = s->avl.key = blobmsg_data(tb[SERVICE_ATTR_NAME]);
92 if (tb[SERVICE_ATTR_INSTANCES]) {
93 vlist_update(&s->instances);
94 blobmsg_for_each_attr(cur, tb[SERVICE_ATTR_INSTANCES], rem) {
95 service_instance_add(s, cur);
97 vlist_flush(&s->instances);
106 service_delete(struct service *s)
108 vlist_flush_all(&s->instances);
109 avl_delete(&services, &s->avl);
115 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
116 struct ubus_request_data *req, const char *method,
117 struct blob_attr *msg)
119 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
120 struct service *s = NULL;
122 int ret = UBUS_STATUS_INVALID_ARGUMENT;
124 msg = blob_memdup(msg);
126 return UBUS_STATUS_UNKNOWN_ERROR;
128 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
129 cur = tb[SERVICE_ATTR_NAME];
133 name = blobmsg_data(cur);
135 s = avl_find_element(&services, name, s, avl);
137 return service_update(s, msg, tb);
139 s = service_alloc(name);
141 return UBUS_STATUS_UNKNOWN_ERROR;
143 ret = service_update(s, msg, tb);
147 avl_insert(&services, &s->avl);
157 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
158 struct ubus_request_data *req, const char *method,
159 struct blob_attr *msg)
163 blob_buf_init(&b, 0);
164 avl_for_each_element(&services, s, avl) {
167 c = blobmsg_open_table(&b, s->name);
168 blobmsg_close_table(&b, c);
171 ubus_send_reply(ctx, req, b.head);
181 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_MAX] = {
182 [SERVICE_DEL_NAME] = { "name", BLOBMSG_TYPE_STRING },
187 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
188 struct ubus_request_data *req, const char *method,
189 struct blob_attr *msg)
191 struct blob_attr *tb[__SERVICE_DEL_MAX], *cur;
192 struct service *s, *tmp;
194 blobmsg_parse(service_del_attrs, __SERVICE_DEL_MAX, tb, blob_data(msg), blob_len(msg));
196 cur = tb[SERVICE_ATTR_NAME];
198 avl_for_each_element_safe(&services, s, avl, tmp)
203 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
205 return UBUS_STATUS_NOT_FOUND;
211 static struct ubus_method main_object_methods[] = {
212 { .name = "list", .handler = service_handle_list },
213 { .name = "set", .handler = service_handle_set },
214 { .name = "delete", .handler = service_handle_delete },
217 static struct ubus_object_type main_object_type =
218 UBUS_OBJECT_TYPE("service", main_object_methods);
220 static struct ubus_object main_object = {
222 .type = &main_object_type,
223 .methods = main_object_methods,
224 .n_methods = ARRAY_SIZE(main_object_methods),
227 void procd_init_service(struct ubus_context *ctx)
229 avl_init(&services, avl_strcmp, false, NULL);
230 ubus_add_object(ctx, &main_object);