make ubus handling use uloop timers
[project/procd.git] / service.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
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
8  *
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.
13  */
14
15 #include <libubox/avl-cmp.h>
16 #include "procd.h"
17 #include "service.h"
18 #include "instance.h"
19
20 struct avl_tree services;
21 static struct blob_buf b;
22
23 static void
24 service_instance_add(struct service *s, struct blob_attr *attr)
25 {
26         struct service_instance *in;
27
28         if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
29                 return;
30
31         in = calloc(1, sizeof(*in));
32         if (!in)
33                 return;
34
35         instance_init(in, s, attr);
36         vlist_add(&s->instances, &in->node, (void *) in->name);
37 }
38
39 static void
40 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
41                         struct vlist_node *node_old)
42 {
43         struct service_instance *in_o = NULL, *in_n = NULL;
44
45         if (node_old)
46                 in_o = container_of(node_old, struct service_instance, node);
47
48         if (node_new)
49                 in_n = container_of(node_new, struct service_instance, node);
50
51         if (in_o && in_n) {
52                 DEBUG(1, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
53                 instance_update(in_o, in_n);
54                 instance_free(in_n);
55         } else if (in_o) {
56                 DEBUG(1, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
57                 instance_stop(in_o, false);
58                 instance_free(in_o);
59         } else if (in_n) {
60                 DEBUG(1, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
61                 instance_start(in_n);
62         }
63 }
64
65 static struct service *
66 service_alloc(const char *name)
67 {
68         struct service *s;
69         char *new_name;
70
71         s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
72         strcpy(new_name, name);
73
74         vlist_init(&s->instances, avl_strcmp, service_instance_update);
75         s->instances.keep_old = true;
76         s->name = new_name;
77         s->avl.key = s->name;
78
79         return s;
80 }
81
82 enum {
83         SERVICE_SET_NAME,
84         SERVICE_SET_SCRIPT,
85         SERVICE_SET_INSTANCES,
86         __SERVICE_SET_MAX
87 };
88
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 },
93 };
94
95
96 static int
97 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
98 {
99         struct blob_attr *cur;
100         int rem;
101
102         if (tb[SERVICE_SET_INSTANCES]) {
103                 if (!add)
104                         vlist_update(&s->instances);
105                 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
106                         service_instance_add(s, cur);
107                 }
108                 if (!add)
109                         vlist_flush(&s->instances);
110         }
111
112         return 0;
113 }
114
115 static void
116 service_delete(struct service *s)
117 {
118         vlist_flush_all(&s->instances);
119         avl_delete(&services, &s->avl);
120         free(s->config);
121         free(s);
122 }
123
124 enum {
125         SERVICE_ATTR_NAME,
126         __SERVICE_ATTR_MAX,
127 };
128
129 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
130         [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
131 };
132
133 enum {
134         SERVICE_DEL_ATTR_NAME,
135         SERVICE_DEL_ATTR_INSTANCE,
136         __SERVICE_DEL_ATTR_MAX,
137 };
138
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 },
142 };
143
144
145 static int
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)
149 {
150         struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
151         struct service *s = NULL;
152         const char *name;
153         int ret = UBUS_STATUS_INVALID_ARGUMENT;
154         bool add = !strcmp(method, "add");
155
156         blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
157         cur = tb[SERVICE_ATTR_NAME];
158         if (!cur)
159                 goto free;
160
161         name = blobmsg_data(cur);
162
163         s = avl_find_element(&services, name, s, avl);
164         if (s) {
165                 DEBUG(1, "Update service %s\n", name);
166                 return service_update(s, msg, tb, add);
167         }
168
169         DEBUG(1, "Create service %s\n", name);
170         s = service_alloc(name);
171         if (!s)
172                 return UBUS_STATUS_UNKNOWN_ERROR;
173
174         ret = service_update(s, msg, tb, add);
175         if (ret)
176                 goto free;
177
178         avl_insert(&services, &s->avl);
179
180         return 0;
181
182 free:
183         free(msg);
184         return ret;
185 }
186
187 static void
188 service_dump(struct service *s)
189 {
190         struct service_instance *in;
191         void *c, *i;
192
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);
199 }
200
201 static int
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)
205 {
206         struct service *s;
207
208         blob_buf_init(&b, 0);
209         avl_for_each_element(&services, s, avl)
210                 service_dump(s);
211
212         ubus_send_reply(ctx, req, b.head);
213
214         return 0;
215 }
216
217 static int
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)
221 {
222         struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
223         struct service *s, *tmp;
224         struct service_instance *in;
225
226         blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
227
228         cur = tb[SERVICE_DEL_ATTR_NAME];
229         if (!cur) {
230                 avl_for_each_element_safe(&services, s, avl, tmp)
231                         service_delete(s);
232                 return 0;
233         }
234
235         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
236         if (!s)
237                 return UBUS_STATUS_NOT_FOUND;
238
239         cur = tb[SERVICE_DEL_ATTR_INSTANCE];
240         if (!cur) {
241                 service_delete(s);
242                 return 0;
243         }
244
245         in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
246         if (!in) {
247                 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
248                 return UBUS_STATUS_NOT_FOUND;
249         }
250
251         vlist_delete(&s->instances, &in->node);
252
253         return 0;
254 }
255
256 static int
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)
260 {
261         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
262         struct service *s;
263
264         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
265
266         cur = tb[SERVICE_ATTR_NAME];
267         if (!cur)
268                 return UBUS_STATUS_INVALID_ARGUMENT;
269
270         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
271         if (!s)
272                 return UBUS_STATUS_NOT_FOUND;
273
274         if (!strcmp(method, "update_start"))
275                 vlist_update(&s->instances);
276         else
277                 vlist_flush(&s->instances);
278
279         return 0;
280 }
281
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),
289 };
290
291 static struct ubus_object_type main_object_type =
292         UBUS_OBJECT_TYPE("service", main_object_methods);
293
294 static struct ubus_object main_object = {
295         .name = "service",
296         .type = &main_object_type,
297         .methods = main_object_methods,
298         .n_methods = ARRAY_SIZE(main_object_methods),
299 };
300
301 void ubus_init_service(struct ubus_context *ctx)
302 {
303         avl_init(&services, avl_strcmp, false, NULL);
304         ubus_add_object(ctx, &main_object);
305 }