c5f5bf307bf30fb723df1b8cad6212a8113f88f6
[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/blobmsg_json.h>
16 #include <libubox/avl-cmp.h>
17 #include "procd.h"
18 #include "service.h"
19 #include "instance.h"
20 #include "rcS.h"
21
22 struct avl_tree services;
23 static struct blob_buf b;
24
25 static void
26 service_instance_add(struct service *s, struct blob_attr *attr)
27 {
28         struct service_instance *in;
29
30         if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
31                 return;
32
33         in = calloc(1, sizeof(*in));
34         if (!in)
35                 return;
36
37         instance_init(in, s, attr);
38         vlist_add(&s->instances, &in->node, (void *) in->name);
39 }
40
41 static void
42 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
43                         struct vlist_node *node_old)
44 {
45         struct service_instance *in_o = NULL, *in_n = NULL;
46
47         if (node_old)
48                 in_o = container_of(node_old, struct service_instance, node);
49
50         if (node_new)
51                 in_n = container_of(node_new, struct service_instance, node);
52
53         if (in_o && in_n) {
54                 DEBUG(1, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
55                 instance_update(in_o, in_n);
56                 instance_free(in_n);
57         } else if (in_o) {
58                 DEBUG(1, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
59                 instance_stop(in_o);
60                 instance_free(in_o);
61         } else if (in_n) {
62                 DEBUG(1, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
63                 instance_start(in_n);
64         }
65 }
66
67 static struct service *
68 service_alloc(const char *name)
69 {
70         struct service *s;
71         char *new_name;
72
73         s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
74         strcpy(new_name, name);
75
76         vlist_init(&s->instances, avl_strcmp, service_instance_update);
77         s->instances.keep_old = true;
78         s->name = new_name;
79         s->avl.key = s->name;
80         INIT_LIST_HEAD(&s->validators);
81
82         return s;
83 }
84
85 enum {
86         SERVICE_SET_NAME,
87         SERVICE_SET_SCRIPT,
88         SERVICE_SET_INSTANCES,
89         SERVICE_SET_TRIGGER,
90         SERVICE_SET_VALIDATE,
91         __SERVICE_SET_MAX
92 };
93
94 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
95         [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
96         [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
97         [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
98         [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
99         [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
100 };
101
102 static int
103 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
104 {
105         struct blob_attr *cur;
106         int rem;
107
108         if (s->trigger) {
109                 trigger_del(s);
110                 free(s->trigger);
111                 s->trigger = NULL;
112         }
113
114         service_validate_del(s);
115
116         if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
117                 s->trigger = malloc(blob_pad_len(tb[SERVICE_SET_TRIGGER]));
118                 if (!s->trigger)
119                         return -1;
120                 memcpy(s->trigger, tb[SERVICE_SET_TRIGGER], blob_pad_len(tb[SERVICE_SET_TRIGGER]));
121                 trigger_add(s->trigger, s);
122         }
123
124         if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
125                 blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
126                         service_validate_add(s, cur);
127         }
128
129         if (tb[SERVICE_SET_INSTANCES]) {
130                 if (!add)
131                         vlist_update(&s->instances);
132                 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
133                         service_instance_add(s, cur);
134                 }
135                 if (!add)
136                         vlist_flush(&s->instances);
137         }
138
139         rc(s->name, "running");
140
141         return 0;
142 }
143
144 static void
145 service_delete(struct service *s)
146 {
147         vlist_flush_all(&s->instances);
148         avl_delete(&services, &s->avl);
149         trigger_del(s);
150         s->trigger = NULL;
151         free(s->trigger);
152         free(s);
153         service_validate_del(s);
154 }
155
156 enum {
157         SERVICE_ATTR_NAME,
158         __SERVICE_ATTR_MAX,
159 };
160
161 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
162         [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
163 };
164
165 enum {
166         SERVICE_DEL_ATTR_NAME,
167         SERVICE_DEL_ATTR_INSTANCE,
168         __SERVICE_DEL_ATTR_MAX,
169 };
170
171 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
172         [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
173         [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
174 };
175
176 enum {
177         SERVICE_LIST_ATTR_VERBOSE,
178         __SERVICE_LIST_ATTR_MAX,
179 };
180
181 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
182         [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
183 };
184
185 enum {
186         EVENT_TYPE,
187         EVENT_DATA,
188         __EVENT_MAX
189 };
190
191 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
192         [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
193         [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
194 };
195
196 enum {
197         VALIDATE_PACKAGE,
198         VALIDATE_TYPE,
199         VALIDATE_SERVICE,
200         __VALIDATE_MAX
201 };
202
203 static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
204         [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
205         [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
206         [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
207 };
208
209 static int
210 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
211                    struct ubus_request_data *req, const char *method,
212                    struct blob_attr *msg)
213 {
214         struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
215         struct service *s = NULL;
216         const char *name;
217         int ret = UBUS_STATUS_INVALID_ARGUMENT;
218         bool add = !strcmp(method, "add");
219
220         blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
221         cur = tb[SERVICE_ATTR_NAME];
222         if (!cur)
223                 goto free;
224
225         name = blobmsg_data(cur);
226
227         s = avl_find_element(&services, name, s, avl);
228         if (s) {
229                 DEBUG(1, "Update service %s\n", name);
230                 return service_update(s, msg, tb, add);
231         }
232
233         DEBUG(1, "Create service %s\n", name);
234         s = service_alloc(name);
235         if (!s)
236                 return UBUS_STATUS_UNKNOWN_ERROR;
237
238         ret = service_update(s, msg, tb, add);
239         if (ret)
240                 goto free;
241
242         avl_insert(&services, &s->avl);
243
244         return 0;
245
246 free:
247         free(msg);
248         return ret;
249 }
250
251 static void
252 service_dump(struct service *s, int verbose)
253 {
254         struct service_instance *in;
255         void *c, *i;
256
257         c = blobmsg_open_table(&b, s->name);
258
259         if (avl_is_empty(&s->instances.avl)) {
260                 blobmsg_close_table(&b, c);
261                 return;
262         }
263
264         i = blobmsg_open_table(&b, "instances");
265         vlist_for_each_element(&s->instances, in, node)
266                 instance_dump(&b, in, verbose);
267         blobmsg_close_table(&b, i);
268         if (verbose && s->trigger)
269                 blobmsg_add_blob(&b, s->trigger);
270         if (verbose && !list_empty(&s->validators))
271                 service_validate_dump(&b, s);
272         blobmsg_close_table(&b, c);
273 }
274
275 static int
276 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
277                     struct ubus_request_data *req, const char *method,
278                     struct blob_attr *msg)
279 {
280         struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
281         struct service *s;
282         int verbose = 0;
283
284         blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
285
286         if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
287                 verbose = 1;
288
289         blob_buf_init(&b, 0);
290         avl_for_each_element(&services, s, avl)
291                 service_dump(s, verbose);
292
293         ubus_send_reply(ctx, req, b.head);
294
295         return 0;
296 }
297
298 static int
299 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
300                     struct ubus_request_data *req, const char *method,
301                     struct blob_attr *msg)
302 {
303         struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
304         struct service *s;
305         struct service_instance *in;
306
307         blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
308
309         cur = tb[SERVICE_DEL_ATTR_NAME];
310         if (!cur)
311                 return UBUS_STATUS_NOT_FOUND;
312
313         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
314         if (!s)
315                 return UBUS_STATUS_NOT_FOUND;
316
317         cur = tb[SERVICE_DEL_ATTR_INSTANCE];
318         if (!cur) {
319                 service_delete(s);
320                 return 0;
321         }
322
323         in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
324         if (!in) {
325                 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
326                 return UBUS_STATUS_NOT_FOUND;
327         }
328
329         vlist_delete(&s->instances, &in->node);
330
331         return 0;
332 }
333
334 static int
335 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
336                       struct ubus_request_data *req, const char *method,
337                       struct blob_attr *msg)
338 {
339         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
340         struct service *s;
341
342         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
343
344         cur = tb[SERVICE_ATTR_NAME];
345         if (!cur)
346                 return UBUS_STATUS_INVALID_ARGUMENT;
347
348         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
349         if (!s)
350                 return UBUS_STATUS_NOT_FOUND;
351
352         if (!strcmp(method, "update_start"))
353                 vlist_update(&s->instances);
354         else
355                 vlist_flush(&s->instances);
356
357         return 0;
358 }
359
360 static int
361 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
362                         struct ubus_request_data *req, const char *method,
363                         struct blob_attr *msg)
364 {
365         struct blob_attr *tb[__EVENT_MAX];
366
367         if (!msg)
368                 return UBUS_STATUS_INVALID_ARGUMENT;
369
370         blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
371         if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
372                 return UBUS_STATUS_INVALID_ARGUMENT;
373
374         trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
375
376         return 0;
377 }
378
379 static int
380 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
381                         struct ubus_request_data *req, const char *method,
382                         struct blob_attr *msg)
383 {
384         struct blob_attr *tb[__VALIDATE_MAX];
385         char *p = NULL, *t = NULL;
386
387         if (!msg)
388                 return UBUS_STATUS_INVALID_ARGUMENT;
389
390         blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
391         if (tb[VALIDATE_SERVICE]) {
392                 return 0;
393         }
394         if (tb[VALIDATE_PACKAGE])
395                 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
396
397         if (tb[VALIDATE_TYPE])
398                 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
399
400         blob_buf_init(&b, 0);
401         service_validate_dump_all(&b, p, t);
402         ubus_send_reply(ctx, req, b.head);
403
404         return 0;
405 }
406
407 static struct ubus_method main_object_methods[] = {
408         UBUS_METHOD("set", service_handle_set, service_set_attrs),
409         UBUS_METHOD("add", service_handle_set, service_set_attrs),
410         UBUS_METHOD("list", service_handle_list, service_attrs),
411         UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
412         UBUS_METHOD("update_start", service_handle_update, service_attrs),
413         UBUS_METHOD("update_complete", service_handle_update, service_attrs),
414         UBUS_METHOD("event", service_handle_event, event_policy),
415         UBUS_METHOD("validate", service_handle_validate, validate_policy),
416 };
417
418 static struct ubus_object_type main_object_type =
419         UBUS_OBJECT_TYPE("service", main_object_methods);
420
421 static struct ubus_object main_object = {
422         .name = "service",
423         .type = &main_object_type,
424         .methods = main_object_methods,
425         .n_methods = ARRAY_SIZE(main_object_methods),
426 };
427
428 void ubus_init_service(struct ubus_context *ctx)
429 {
430         ubus_add_object(ctx, &main_object);
431 }
432
433 void
434 service_init(void)
435 {
436         avl_init(&services, avl_strcmp, false, NULL);
437         service_validate_init();
438 }
439