23532e88ed92a863f501aa4962935d96c99eef31
[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 **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 = blob_memdup(tb[SERVICE_SET_TRIGGER]);
121 if (!s->trigger)
122 return -1;
123 trigger_add(s->trigger, s);
124 }
125
126 if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
127 blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
128 service_validate_add(s, cur);
129 }
130
131 if (tb[SERVICE_SET_INSTANCES]) {
132 if (!add)
133 vlist_update(&s->instances);
134 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
135 service_instance_add(s, cur);
136 }
137 if (!add)
138 vlist_flush(&s->instances);
139 }
140
141 rc(s->name, "running");
142
143 return 0;
144 }
145
146 static void
147 service_delete(struct service *s)
148 {
149 vlist_flush_all(&s->instances);
150 avl_delete(&services, &s->avl);
151 trigger_del(s);
152 s->trigger = NULL;
153 free(s->trigger);
154 free(s);
155 service_validate_del(s);
156 }
157
158 enum {
159 SERVICE_ATTR_NAME,
160 __SERVICE_ATTR_MAX,
161 };
162
163 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
164 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
165 };
166
167 enum {
168 SERVICE_DEL_ATTR_NAME,
169 SERVICE_DEL_ATTR_INSTANCE,
170 __SERVICE_DEL_ATTR_MAX,
171 };
172
173 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
174 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
175 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
176 };
177
178 enum {
179 SERVICE_LIST_ATTR_VERBOSE,
180 __SERVICE_LIST_ATTR_MAX,
181 };
182
183 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
184 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
185 };
186
187 enum {
188 EVENT_TYPE,
189 EVENT_DATA,
190 __EVENT_MAX
191 };
192
193 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
194 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
195 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
196 };
197
198 enum {
199 VALIDATE_PACKAGE,
200 VALIDATE_TYPE,
201 VALIDATE_SERVICE,
202 __VALIDATE_MAX
203 };
204
205 static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
206 [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
207 [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
208 [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
209 };
210
211 static int
212 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
213 struct ubus_request_data *req, const char *method,
214 struct blob_attr *msg)
215 {
216 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
217 struct service *s = NULL;
218 const char *name;
219 bool add = !strcmp(method, "add");
220 int ret;
221
222 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
223 cur = tb[SERVICE_ATTR_NAME];
224 if (!cur)
225 return UBUS_STATUS_INVALID_ARGUMENT;
226
227 name = blobmsg_data(cur);
228
229 s = avl_find_element(&services, name, s, avl);
230 if (s) {
231 DEBUG(2, "Update service %s\n", name);
232 return service_update(s, tb, add);
233 }
234
235 DEBUG(2, "Create service %s\n", name);
236 s = service_alloc(name);
237 if (!s)
238 return UBUS_STATUS_UNKNOWN_ERROR;
239
240 ret = service_update(s, tb, add);
241 if (ret)
242 return ret;
243
244 avl_insert(&services, &s->avl);
245
246 return 0;
247 }
248
249 static void
250 service_dump(struct service *s, int verbose)
251 {
252 struct service_instance *in;
253 void *c, *i;
254
255 c = blobmsg_open_table(&b, s->name);
256
257 if (!avl_is_empty(&s->instances.avl)) {
258 i = blobmsg_open_table(&b, "instances");
259 vlist_for_each_element(&s->instances, in, node)
260 instance_dump(&b, in, verbose);
261 blobmsg_close_table(&b, i);
262 }
263 if (verbose && s->trigger)
264 blobmsg_add_blob(&b, s->trigger);
265 if (verbose && !list_empty(&s->validators))
266 service_validate_dump(&b, s);
267 blobmsg_close_table(&b, c);
268 }
269
270 static int
271 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
272 struct ubus_request_data *req, const char *method,
273 struct blob_attr *msg)
274 {
275 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
276 struct service *s;
277 int verbose = 0;
278
279 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
280
281 if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
282 verbose = 1;
283
284 blob_buf_init(&b, 0);
285 avl_for_each_element(&services, s, avl)
286 service_dump(s, verbose);
287
288 ubus_send_reply(ctx, req, b.head);
289
290 return 0;
291 }
292
293 static int
294 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
295 struct ubus_request_data *req, const char *method,
296 struct blob_attr *msg)
297 {
298 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
299 struct service *s;
300 struct service_instance *in;
301
302 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
303
304 cur = tb[SERVICE_DEL_ATTR_NAME];
305 if (!cur)
306 return UBUS_STATUS_NOT_FOUND;
307
308 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
309 if (!s)
310 return UBUS_STATUS_NOT_FOUND;
311
312 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
313 if (!cur) {
314 service_delete(s);
315 return 0;
316 }
317
318 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
319 if (!in) {
320 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
321 return UBUS_STATUS_NOT_FOUND;
322 }
323
324 vlist_delete(&s->instances, &in->node);
325
326 return 0;
327 }
328
329 static int
330 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
331 struct ubus_request_data *req, const char *method,
332 struct blob_attr *msg)
333 {
334 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
335 struct service *s;
336
337 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
338
339 cur = tb[SERVICE_ATTR_NAME];
340 if (!cur)
341 return UBUS_STATUS_INVALID_ARGUMENT;
342
343 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
344 if (!s)
345 return UBUS_STATUS_NOT_FOUND;
346
347 if (!strcmp(method, "update_start"))
348 vlist_update(&s->instances);
349 else
350 vlist_flush(&s->instances);
351
352 return 0;
353 }
354
355 static int
356 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
357 struct ubus_request_data *req, const char *method,
358 struct blob_attr *msg)
359 {
360 struct blob_attr *tb[__EVENT_MAX];
361
362 if (!msg)
363 return UBUS_STATUS_INVALID_ARGUMENT;
364
365 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
366 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
367 return UBUS_STATUS_INVALID_ARGUMENT;
368
369 trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
370
371 return 0;
372 }
373
374 static int
375 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
376 struct ubus_request_data *req, const char *method,
377 struct blob_attr *msg)
378 {
379 struct blob_attr *tb[__VALIDATE_MAX];
380 char *p = NULL, *t = NULL;
381
382 if (!msg)
383 return UBUS_STATUS_INVALID_ARGUMENT;
384
385 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
386 if (tb[VALIDATE_SERVICE]) {
387 return 0;
388 }
389 if (tb[VALIDATE_PACKAGE])
390 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
391
392 if (tb[VALIDATE_TYPE])
393 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
394
395 blob_buf_init(&b, 0);
396 service_validate_dump_all(&b, p, t);
397 ubus_send_reply(ctx, req, b.head);
398
399 return 0;
400 }
401
402 static struct ubus_method main_object_methods[] = {
403 UBUS_METHOD("set", service_handle_set, service_set_attrs),
404 UBUS_METHOD("add", service_handle_set, service_set_attrs),
405 UBUS_METHOD("list", service_handle_list, service_attrs),
406 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
407 UBUS_METHOD("update_start", service_handle_update, service_attrs),
408 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
409 UBUS_METHOD("event", service_handle_event, event_policy),
410 UBUS_METHOD("validate", service_handle_validate, validate_policy),
411 };
412
413 static struct ubus_object_type main_object_type =
414 UBUS_OBJECT_TYPE("service", main_object_methods);
415
416 static struct ubus_object main_object = {
417 .name = "service",
418 .type = &main_object_type,
419 .methods = main_object_methods,
420 .n_methods = ARRAY_SIZE(main_object_methods),
421 };
422
423 int
424 service_start_early(char *name, char *cmdline)
425 {
426 void *instances, *instance, *command, *respawn;
427 char *t;
428
429 blob_buf_init(&b, 0);
430 blobmsg_add_string(&b, "name", name);
431 instances = blobmsg_open_table(&b, "instances");
432 instance = blobmsg_open_table(&b, "instance1");
433 command = blobmsg_open_array(&b, "command");
434 t = strtok(cmdline, " ");
435 while (t) {
436 blobmsg_add_string(&b, NULL, t);
437 t = strtok(NULL, " ");
438 }
439 blobmsg_close_array(&b, command);
440 respawn = blobmsg_open_array(&b, "respawn");
441 blobmsg_add_string(&b, NULL, "3600");
442 blobmsg_add_string(&b, NULL, "1");
443 blobmsg_add_string(&b, NULL, "0");
444 blobmsg_close_array(&b, respawn);
445 blobmsg_close_table(&b, instance);
446 blobmsg_close_table(&b, instances);
447
448 return service_handle_set(NULL, NULL, NULL, "add", b.head);
449 }
450
451 void ubus_init_service(struct ubus_context *ctx)
452 {
453 ubus_add_object(ctx, &main_object);
454 }
455
456 void
457 service_init(void)
458 {
459 avl_init(&services, avl_strcmp, false, NULL);
460 service_validate_init();
461 }
462