bugfix of error handling while open()
[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         c = blobmsg_open_table(&b, s->name);
216         i = blobmsg_open_table(&b, "instances");
217         if (verbose && s->trigger)
218                 blobmsg_add_blob(&b, s->trigger);
219         vlist_for_each_element(&s->instances, in, node)
220                 instance_dump(&b, in, verbose);
221         blobmsg_close_table(&b, i);
222         blobmsg_close_table(&b, c);
223 }
224
225 static int
226 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
227                     struct ubus_request_data *req, const char *method,
228                     struct blob_attr *msg)
229 {
230         struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
231         struct service *s;
232         int verbose = 0;
233
234         blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
235
236         if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_u32(tb[SERVICE_LIST_ATTR_VERBOSE]))
237                 verbose = 1;
238
239         blob_buf_init(&b, 0);
240         avl_for_each_element(&services, s, avl)
241                 service_dump(s, verbose);
242
243         ubus_send_reply(ctx, req, b.head);
244
245         return 0;
246 }
247
248 static int
249 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
250                     struct ubus_request_data *req, const char *method,
251                     struct blob_attr *msg)
252 {
253         struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
254         struct service *s, *tmp;
255         struct service_instance *in;
256
257         blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
258
259         cur = tb[SERVICE_DEL_ATTR_NAME];
260         if (!cur) {
261                 avl_for_each_element_safe(&services, s, avl, tmp)
262                         service_delete(s);
263                 return 0;
264         }
265
266         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
267         if (!s)
268                 return UBUS_STATUS_NOT_FOUND;
269
270         cur = tb[SERVICE_DEL_ATTR_INSTANCE];
271         if (!cur) {
272                 service_delete(s);
273                 return 0;
274         }
275
276         in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
277         if (!in) {
278                 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
279                 return UBUS_STATUS_NOT_FOUND;
280         }
281
282         vlist_delete(&s->instances, &in->node);
283
284         return 0;
285 }
286
287 static int
288 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
289                       struct ubus_request_data *req, const char *method,
290                       struct blob_attr *msg)
291 {
292         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
293         struct service *s;
294
295         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
296
297         cur = tb[SERVICE_ATTR_NAME];
298         if (!cur)
299                 return UBUS_STATUS_INVALID_ARGUMENT;
300
301         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
302         if (!s)
303                 return UBUS_STATUS_NOT_FOUND;
304
305         if (!strcmp(method, "update_start"))
306                 vlist_update(&s->instances);
307         else
308                 vlist_flush(&s->instances);
309
310         return 0;
311 }
312
313 static int
314 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
315                         struct ubus_request_data *req, const char *method,
316                         struct blob_attr *msg)
317 {
318         struct blob_attr *tb[__EVENT_MAX];
319
320         if (!msg)
321                 return UBUS_STATUS_INVALID_ARGUMENT;
322
323         blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
324         if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
325                 return UBUS_STATUS_INVALID_ARGUMENT;
326
327         trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
328
329         return 0;
330 }
331
332 enum {
333         TRIGGER_ATTR,
334         __TRIGGER_MAX
335 };
336
337 static const struct blobmsg_policy trigger_policy[__TRIGGER_MAX] = {
338         [TRIGGER_ATTR] = { .name = "triggers", .type = BLOBMSG_TYPE_ARRAY },
339 };
340
341 static int service_handle_trigger(struct ubus_context *ctx, struct ubus_object *obj,
342                         struct ubus_request_data *req, const char *method,
343                         struct blob_attr *msg)
344 {
345         struct blob_attr *tb[__TRIGGER_MAX];
346
347         if (!msg)
348                 return UBUS_STATUS_INVALID_ARGUMENT;
349
350         blobmsg_parse(trigger_policy, __TRIGGER_MAX, tb, blob_data(msg), blob_len(msg));
351         if (!tb[TRIGGER_ATTR])
352                 return UBUS_STATUS_INVALID_ARGUMENT;
353
354         trigger_add(tb[TRIGGER_ATTR], NULL);
355
356         return 0;
357 }
358
359 static struct ubus_method main_object_methods[] = {
360         UBUS_METHOD("set", service_handle_set, service_set_attrs),
361         UBUS_METHOD("add", service_handle_set, service_set_attrs),
362         UBUS_METHOD("list", service_handle_list, service_attrs),
363         UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
364         UBUS_METHOD("update_start", service_handle_update, service_attrs),
365         UBUS_METHOD("update_complete", service_handle_update, service_attrs),
366         UBUS_METHOD("event", service_handle_event, event_policy),
367         UBUS_METHOD("trigger", service_handle_trigger, trigger_policy),
368 };
369
370 static struct ubus_object_type main_object_type =
371         UBUS_OBJECT_TYPE("service", main_object_methods);
372
373 static struct ubus_object main_object = {
374         .name = "service",
375         .type = &main_object_type,
376         .methods = main_object_methods,
377         .n_methods = ARRAY_SIZE(main_object_methods),
378 };
379
380 void ubus_init_service(struct ubus_context *ctx)
381 {
382         avl_init(&services, avl_strcmp, false, NULL);
383         ubus_add_object(ctx, &main_object);
384 }