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