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