ffbc58463ce95c6f6fbe4c67c4f40020fd2a209d
[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_TRIGGER,
87         __SERVICE_SET_MAX
88 };
89
90 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
91         [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
92         [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
93         [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
94         [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
95 };
96
97 static int
98 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
99 {
100         struct blob_attr *cur;
101         int rem;
102
103         s->trigger = tb[SERVICE_SET_TRIGGER];
104
105         if (tb[SERVICE_SET_INSTANCES]) {
106                 if (!add)
107                         vlist_update(&s->instances);
108                 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
109                         service_instance_add(s, cur);
110                 }
111                 if (!add)
112                         vlist_flush(&s->instances);
113         }
114
115         return 0;
116 }
117
118 static void
119 service_delete(struct service *s)
120 {
121         vlist_flush_all(&s->instances);
122         avl_delete(&services, &s->avl);
123         free(s->config);
124         free(s);
125 }
126
127 enum {
128         SERVICE_ATTR_NAME,
129         __SERVICE_ATTR_MAX,
130 };
131
132 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
133         [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
134 };
135
136 enum {
137         SERVICE_DEL_ATTR_NAME,
138         SERVICE_DEL_ATTR_INSTANCE,
139         __SERVICE_DEL_ATTR_MAX,
140 };
141
142 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
143         [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
144         [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
145 };
146
147 enum {
148         SERVICE_LIST_ATTR_VERBOSE,
149         __SERVICE_LIST_ATTR_MAX,
150 };
151
152 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
153         [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_INT32 },
154 };
155
156 enum {
157         EVENT_TYPE,
158         EVENT_DATA,
159         __EVENT_MAX
160 };
161
162 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
163         [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
164         [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
165 };
166
167 static int
168 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
169                    struct ubus_request_data *req, const char *method,
170                    struct blob_attr *msg)
171 {
172         struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
173         struct service *s = NULL;
174         const char *name;
175         int ret = UBUS_STATUS_INVALID_ARGUMENT;
176         bool add = !strcmp(method, "add");
177
178         blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
179         cur = tb[SERVICE_ATTR_NAME];
180         if (!cur)
181                 goto free;
182
183         name = blobmsg_data(cur);
184
185         s = avl_find_element(&services, name, s, avl);
186         if (s) {
187                 DEBUG(1, "Update service %s\n", name);
188                 return service_update(s, msg, tb, add);
189         }
190
191         DEBUG(1, "Create service %s\n", name);
192         s = service_alloc(name);
193         if (!s)
194                 return UBUS_STATUS_UNKNOWN_ERROR;
195
196         ret = service_update(s, msg, tb, add);
197         if (ret)
198                 goto free;
199
200         avl_insert(&services, &s->avl);
201
202         return 0;
203
204 free:
205         free(msg);
206         return ret;
207 }
208
209 static void
210 service_dump(struct service *s, int verbose)
211 {
212         struct service_instance *in;
213         void *c, *i;
214
215         if (avl_is_empty(&s->instances.avl))
216                 return;
217
218         c = blobmsg_open_table(&b, s->name);
219         i = blobmsg_open_table(&b, "instances");
220         if (verbose && s->trigger)
221                 blobmsg_add_blob(&b, s->trigger);
222         vlist_for_each_element(&s->instances, in, node)
223                 instance_dump(&b, in, verbose);
224         blobmsg_close_table(&b, i);
225         blobmsg_close_table(&b, c);
226 }
227
228 static int
229 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
230                     struct ubus_request_data *req, const char *method,
231                     struct blob_attr *msg)
232 {
233         struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
234         struct service *s;
235         int verbose = 0;
236
237         blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
238
239         if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_u32(tb[SERVICE_LIST_ATTR_VERBOSE]))
240                 verbose = 1;
241
242         blob_buf_init(&b, 0);
243         avl_for_each_element(&services, s, avl)
244                 service_dump(s, verbose);
245
246         ubus_send_reply(ctx, req, b.head);
247
248         return 0;
249 }
250
251 static int
252 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
253                     struct ubus_request_data *req, const char *method,
254                     struct blob_attr *msg)
255 {
256         struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
257         struct service *s, *tmp;
258         struct service_instance *in;
259
260         blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
261
262         cur = tb[SERVICE_DEL_ATTR_NAME];
263         if (!cur) {
264                 avl_for_each_element_safe(&services, s, avl, tmp)
265                         service_delete(s);
266                 return 0;
267         }
268
269         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
270         if (!s)
271                 return UBUS_STATUS_NOT_FOUND;
272
273         cur = tb[SERVICE_DEL_ATTR_INSTANCE];
274         if (!cur) {
275                 service_delete(s);
276                 return 0;
277         }
278
279         in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
280         if (!in) {
281                 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
282                 return UBUS_STATUS_NOT_FOUND;
283         }
284
285         vlist_delete(&s->instances, &in->node);
286
287         return 0;
288 }
289
290 static int
291 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
292                       struct ubus_request_data *req, const char *method,
293                       struct blob_attr *msg)
294 {
295         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
296         struct service *s;
297
298         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
299
300         cur = tb[SERVICE_ATTR_NAME];
301         if (!cur)
302                 return UBUS_STATUS_INVALID_ARGUMENT;
303
304         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
305         if (!s)
306                 return UBUS_STATUS_NOT_FOUND;
307
308         if (!strcmp(method, "update_start"))
309                 vlist_update(&s->instances);
310         else
311                 vlist_flush(&s->instances);
312
313         return 0;
314 }
315
316 static int
317 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
318                         struct ubus_request_data *req, const char *method,
319                         struct blob_attr *msg)
320 {
321         struct blob_attr *tb[__EVENT_MAX];
322
323         if (!msg)
324                 return UBUS_STATUS_INVALID_ARGUMENT;
325
326         blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
327         if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
328                 return UBUS_STATUS_INVALID_ARGUMENT;
329
330         trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
331
332         return 0;
333 }
334
335 enum {
336         TRIGGER_ATTR,
337         __TRIGGER_MAX
338 };
339
340 static const struct blobmsg_policy trigger_policy[__TRIGGER_MAX] = {
341         [TRIGGER_ATTR] = { .name = "triggers", .type = BLOBMSG_TYPE_ARRAY },
342 };
343
344 static int service_handle_trigger(struct ubus_context *ctx, struct ubus_object *obj,
345                         struct ubus_request_data *req, const char *method,
346                         struct blob_attr *msg)
347 {
348         struct blob_attr *tb[__TRIGGER_MAX];
349
350         if (!msg)
351                 return UBUS_STATUS_INVALID_ARGUMENT;
352
353         blobmsg_parse(trigger_policy, __TRIGGER_MAX, tb, blob_data(msg), blob_len(msg));
354         if (!tb[TRIGGER_ATTR])
355                 return UBUS_STATUS_INVALID_ARGUMENT;
356
357         trigger_add(tb[TRIGGER_ATTR], NULL);
358
359         return 0;
360 }
361
362 static struct ubus_method main_object_methods[] = {
363         UBUS_METHOD("set", service_handle_set, service_set_attrs),
364         UBUS_METHOD("add", service_handle_set, service_set_attrs),
365         UBUS_METHOD("list", service_handle_list, service_attrs),
366         UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
367         UBUS_METHOD("update_start", service_handle_update, service_attrs),
368         UBUS_METHOD("update_complete", service_handle_update, service_attrs),
369         UBUS_METHOD("event", service_handle_event, event_policy),
370         UBUS_METHOD("trigger", service_handle_trigger, trigger_policy),
371 };
372
373 static struct ubus_object_type main_object_type =
374         UBUS_OBJECT_TYPE("service", main_object_methods);
375
376 static struct ubus_object main_object = {
377         .name = "service",
378         .type = &main_object_type,
379         .methods = main_object_methods,
380         .n_methods = ARRAY_SIZE(main_object_methods),
381 };
382
383 void ubus_init_service(struct ubus_context *ctx)
384 {
385         avl_init(&services, avl_strcmp, false, NULL);
386         ubus_add_object(ctx, &main_object);
387 }