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