1d262917641ea741ef11c875962e55f957711951
[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 AVL_TREE(services, avl_strcmp, false, NULL);
26 static struct blob_buf b;
27 static struct ubus_context *ctx;
28 static struct ubus_object main_object;
29
30 static void
31 service_instance_add(struct service *s, struct blob_attr *attr)
32 {
33 struct service_instance *in;
34
35 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
36 return;
37
38 in = calloc(1, sizeof(*in));
39 if (!in)
40 return;
41
42 instance_init(in, s, attr);
43 vlist_add(&s->instances, &in->node, (void *) in->name);
44 }
45
46 static void
47 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
48 struct vlist_node *node_old)
49 {
50 struct service_instance *in_o = NULL, *in_n = NULL;
51
52 if (node_old)
53 in_o = container_of(node_old, struct service_instance, node);
54
55 if (node_new)
56 in_n = container_of(node_new, struct service_instance, node);
57
58 if (in_o && in_n) {
59 DEBUG(2, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
60 instance_update(in_o, in_n);
61 instance_free(in_n);
62 } else if (in_o) {
63 DEBUG(2, "Stop instance %s::%s\n", in_o->srv->name, in_o->name);
64 instance_stop(in_o, true);
65 } else if (in_n && in_n->srv->autostart) {
66 DEBUG(2, "Start instance %s::%s\n", in_n->srv->name, in_n->name);
67 instance_start(in_n);
68 }
69 blob_buf_init(&b, 0);
70 trigger_event("instance.update", b.head);
71 }
72
73 static struct service *
74 service_alloc(const char *name)
75 {
76 struct service *s;
77 char *new_name;
78
79 s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
80 strcpy(new_name, name);
81
82 vlist_init(&s->instances, avl_strcmp, service_instance_update);
83 s->instances.no_delete = true;
84 s->name = new_name;
85 s->avl.key = s->name;
86 INIT_LIST_HEAD(&s->validators);
87 blobmsg_list_simple_init(&s->data_blob);
88
89 return s;
90 }
91
92 enum {
93 SERVICE_SET_NAME,
94 SERVICE_SET_SCRIPT,
95 SERVICE_SET_INSTANCES,
96 SERVICE_SET_TRIGGER,
97 SERVICE_SET_VALIDATE,
98 SERVICE_SET_AUTOSTART,
99 SERVICE_SET_DATA,
100 __SERVICE_SET_MAX
101 };
102
103 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
104 [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
105 [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
106 [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
107 [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
108 [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
109 [SERVICE_SET_AUTOSTART] = { "autostart", BLOBMSG_TYPE_BOOL },
110 [SERVICE_SET_DATA] = { "data", BLOBMSG_TYPE_TABLE },
111 };
112
113 static int
114 service_update(struct service *s, struct blob_attr **tb, bool add)
115 {
116 struct blob_attr *cur;
117 int rem;
118
119 if (s->trigger) {
120 trigger_del(s);
121 free(s->trigger);
122 s->trigger = NULL;
123 }
124
125 if (s->data) {
126 blobmsg_list_free(&s->data_blob);
127 free(s->data);
128 s->data = NULL;
129 }
130
131 service_validate_del(s);
132
133 if (tb[SERVICE_SET_AUTOSTART] && !blobmsg_get_bool(tb[SERVICE_SET_AUTOSTART]))
134 s->autostart = false;
135 else
136 s->autostart = true;
137
138 if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
139 s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
140 if (!s->trigger)
141 return -1;
142 trigger_add(s->trigger, s);
143 }
144
145 if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
146 blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
147 service_validate_add(s, cur);
148 }
149
150 if (tb[SERVICE_SET_INSTANCES]) {
151 if (!add)
152 vlist_update(&s->instances);
153 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
154 service_instance_add(s, cur);
155 }
156 if (!add)
157 vlist_flush(&s->instances);
158 }
159
160 if (tb[SERVICE_SET_DATA] && blobmsg_data_len(tb[SERVICE_SET_DATA])) {
161 s->data = blob_memdup(tb[SERVICE_SET_DATA]);
162 if (!s->data)
163 return -1;
164 blobmsg_list_fill(&s->data_blob, blobmsg_data(s->data),
165 blobmsg_data_len(s->data), false);
166 }
167
168 s->deleted = false;
169
170 rc(s->name, "running");
171
172 return 0;
173 }
174
175 static void
176 service_delete(struct service *s)
177 {
178 blobmsg_list_free(&s->data_blob);
179 free(s->data);
180 vlist_flush_all(&s->instances);
181 s->deleted = true;
182 service_stopped(s);
183 }
184
185 enum {
186 SERVICE_ATTR_NAME,
187 __SERVICE_ATTR_MAX,
188 };
189
190 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
191 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
192 };
193
194 enum {
195 SERVICE_DEL_ATTR_NAME,
196 SERVICE_DEL_ATTR_INSTANCE,
197 __SERVICE_DEL_ATTR_MAX,
198 };
199
200 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
201 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
202 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
203 };
204
205 enum {
206 SERVICE_LIST_ATTR_NAME,
207 SERVICE_LIST_ATTR_VERBOSE,
208 __SERVICE_LIST_ATTR_MAX,
209 };
210
211 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
212 [SERVICE_LIST_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
213 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
214 };
215
216 enum {
217 SERVICE_SIGNAL_ATTR_NAME,
218 SERVICE_SIGNAL_ATTR_INSTANCE,
219 SERVICE_SIGNAL_ATTR_SIGNAL,
220 __SERVICE_SIGNAL_ATTR_MAX,
221 };
222
223 static const struct blobmsg_policy service_signal_attrs[__SERVICE_SIGNAL_ATTR_MAX] = {
224 [SERVICE_SIGNAL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
225 [SERVICE_SIGNAL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
226 [SERVICE_SIGNAL_ATTR_SIGNAL] = { "signal", BLOBMSG_TYPE_INT32 },
227 };
228
229 enum {
230 SERVICE_STATE_ATTR_SPAWN,
231 SERVICE_STATE_ATTR_NAME,
232 __SERVICE_STATE_ATTR_MAX,
233 };
234
235 static const struct blobmsg_policy service_state_attrs[__SERVICE_STATE_ATTR_MAX] = {
236 [SERVICE_STATE_ATTR_SPAWN] = { "spawn", BLOBMSG_TYPE_BOOL },
237 [SERVICE_STATE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
238 };
239
240 enum {
241 EVENT_TYPE,
242 EVENT_DATA,
243 __EVENT_MAX
244 };
245
246 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
247 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
248 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
249 };
250
251 enum {
252 VALIDATE_PACKAGE,
253 VALIDATE_TYPE,
254 VALIDATE_SERVICE,
255 __VALIDATE_MAX
256 };
257
258 static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
259 [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
260 [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
261 [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
262 };
263
264 enum {
265 DATA_NAME,
266 DATA_INSTANCE,
267 DATA_TYPE,
268 __DATA_MAX
269 };
270
271 static const struct blobmsg_policy get_data_policy[] = {
272 [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING },
273 [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
274 [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING },
275 };
276
277 enum {
278 SERVICE_CONSOLE_NAME,
279 SERVICE_CONSOLE_INSTANCE,
280 __SERVICE_CONSOLE_MAX,
281 };
282
283 static const struct blobmsg_policy service_console_policy[__SERVICE_CONSOLE_MAX] = {
284 [SERVICE_CONSOLE_NAME] = { "name", BLOBMSG_TYPE_STRING },
285 [SERVICE_CONSOLE_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
286 };
287
288 static int
289 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
290 struct ubus_request_data *req, const char *method,
291 struct blob_attr *msg)
292 {
293 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
294 struct service *s = NULL;
295 const char *name;
296 bool add = !strcmp(method, "add");
297 int ret;
298
299 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
300 cur = tb[SERVICE_SET_NAME];
301 if (!cur)
302 return UBUS_STATUS_INVALID_ARGUMENT;
303
304 name = blobmsg_data(cur);
305
306 s = avl_find_element(&services, name, s, avl);
307 if (s) {
308 DEBUG(2, "Update service %s\n", name);
309 return service_update(s, tb, add);
310 }
311
312 DEBUG(2, "Create service %s\n", name);
313 s = service_alloc(name);
314 if (!s)
315 return UBUS_STATUS_UNKNOWN_ERROR;
316
317 ret = service_update(s, tb, add);
318 if (ret)
319 return ret;
320
321 avl_insert(&services, &s->avl);
322
323 service_event("service.start", s->name, NULL);
324
325 return 0;
326 }
327
328 static void
329 service_dump(struct service *s, bool verbose)
330 {
331 struct service_instance *in;
332 void *c, *i;
333
334 c = blobmsg_open_table(&b, s->name);
335
336 if (!s->autostart)
337 blobmsg_add_u8(&b, "autostart", false);
338
339 if (!avl_is_empty(&s->data_blob.avl)) {
340 struct blobmsg_list_node *var;
341 i = blobmsg_open_table(&b, "data");
342 blobmsg_list_for_each(&s->data_blob, var)
343 blobmsg_add_blob(&b, var->data);
344 blobmsg_close_table(&b, i);
345 }
346
347 if (!avl_is_empty(&s->instances.avl)) {
348 i = blobmsg_open_table(&b, "instances");
349 vlist_for_each_element(&s->instances, in, node)
350 instance_dump(&b, in, verbose);
351 blobmsg_close_table(&b, i);
352 }
353 if (verbose && s->trigger)
354 blobmsg_add_blob(&b, s->trigger);
355 if (verbose && !list_empty(&s->validators))
356 service_validate_dump(&b, s);
357 blobmsg_close_table(&b, c);
358 }
359
360 static int
361 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
362 struct ubus_request_data *req, const char *method,
363 struct blob_attr *msg)
364 {
365 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
366 struct service *s;
367 const char *name = NULL;
368 bool verbose = false;
369
370 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
371
372 if (tb[SERVICE_LIST_ATTR_VERBOSE])
373 verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
374 if (tb[SERVICE_LIST_ATTR_NAME])
375 name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
376
377 blob_buf_init(&b, 0);
378 avl_for_each_element(&services, s, avl) {
379 if (name && strcmp(s->name, name) != 0)
380 continue;
381
382 service_dump(s, verbose);
383 }
384
385 ubus_send_reply(ctx, req, b.head);
386
387 return 0;
388 }
389
390 static int
391 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
392 struct ubus_request_data *req, const char *method,
393 struct blob_attr *msg)
394 {
395 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
396 struct service *s;
397 struct service_instance *in;
398
399 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
400
401 cur = tb[SERVICE_DEL_ATTR_NAME];
402 if (!cur)
403 return UBUS_STATUS_NOT_FOUND;
404
405 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
406 if (!s)
407 return UBUS_STATUS_NOT_FOUND;
408
409 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
410 if (!cur) {
411 service_delete(s);
412 return 0;
413 }
414
415 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
416 if (!in) {
417 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
418 return UBUS_STATUS_NOT_FOUND;
419 }
420
421 vlist_delete(&s->instances, &in->node);
422
423 return 0;
424 }
425
426 static int
427 service_handle_kill(struct service_instance *in, int sig)
428 {
429 if (kill(in->proc.pid, sig) == 0)
430 return 0;
431
432 switch (errno) {
433 case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
434 case EPERM: return UBUS_STATUS_PERMISSION_DENIED;
435 case ESRCH: return UBUS_STATUS_NOT_FOUND;
436 }
437
438 return UBUS_STATUS_UNKNOWN_ERROR;
439 }
440
441 static int
442 service_handle_signal(struct ubus_context *ctx, struct ubus_object *obj,
443 struct ubus_request_data *req, const char *method,
444 struct blob_attr *msg)
445 {
446 struct blob_attr *tb[__SERVICE_SIGNAL_ATTR_MAX], *cur;
447 struct service *s;
448 struct service_instance *in;
449 int sig = SIGHUP;
450 int rv = 0;
451
452 blobmsg_parse(service_signal_attrs, __SERVICE_SIGNAL_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
453
454 cur = tb[SERVICE_SIGNAL_ATTR_SIGNAL];
455 if (cur)
456 sig = blobmsg_get_u32(cur);
457
458 cur = tb[SERVICE_SIGNAL_ATTR_NAME];
459 if (!cur)
460 return UBUS_STATUS_NOT_FOUND;
461
462 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
463 if (!s)
464 return UBUS_STATUS_NOT_FOUND;
465
466 cur = tb[SERVICE_SIGNAL_ATTR_INSTANCE];
467 if (!cur) {
468 vlist_for_each_element(&s->instances, in, node)
469 rv = service_handle_kill(in, sig);
470
471 return rv;
472 }
473
474 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
475 if (!in) {
476 ERROR("instance %s not found\n", blobmsg_get_string(cur));
477 return UBUS_STATUS_NOT_FOUND;
478 }
479
480 return service_handle_kill(in, sig);
481 }
482
483 static int
484 service_handle_state(struct ubus_context *ctx, struct ubus_object *obj,
485 struct ubus_request_data *req, const char *method,
486 struct blob_attr *msg)
487 {
488 struct blob_attr *tb[__SERVICE_STATE_ATTR_MAX];
489 struct service *s;
490 struct service_instance *in;
491 int spawn;
492
493 blobmsg_parse(service_state_attrs, __SERVICE_STATE_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
494
495 if (!tb[SERVICE_STATE_ATTR_SPAWN])
496 return UBUS_STATUS_INVALID_ARGUMENT;
497
498 if (!tb[SERVICE_STATE_ATTR_NAME])
499 return UBUS_STATUS_NOT_FOUND;
500
501 s = avl_find_element(&services, blobmsg_data(tb[SERVICE_STATE_ATTR_NAME]), s, avl);
502 if (!s)
503 return UBUS_STATUS_NOT_FOUND;
504
505 spawn = !!blobmsg_get_u8(tb[SERVICE_STATE_ATTR_SPAWN]);
506 vlist_for_each_element(&s->instances, in, node) {
507 if (!!in->proc.pending == !!spawn)
508 continue;
509 else if (!in->proc.pending)
510 instance_start(in);
511 else
512 instance_stop(in, false);
513 }
514
515 return UBUS_STATUS_OK;
516 }
517
518 static int
519 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
520 struct ubus_request_data *req, const char *method,
521 struct blob_attr *msg)
522 {
523 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
524 struct service *s;
525
526 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
527
528 cur = tb[SERVICE_SET_NAME];
529 if (!cur)
530 return UBUS_STATUS_INVALID_ARGUMENT;
531
532 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
533 if (!s)
534 return UBUS_STATUS_NOT_FOUND;
535
536 if (!strcmp(method, "update_start"))
537 vlist_update(&s->instances);
538 else
539 vlist_flush(&s->instances);
540
541 return 0;
542 }
543
544 static void ubus_event_bcast(const char *type, const char *param1, const char *val1,
545 const char *param2, const char *val2)
546 {
547 if (!ctx)
548 return;
549
550 blob_buf_init(&b, 0);
551 if (param1 && val1)
552 blobmsg_add_string(&b, param1, val1);
553 if (param2 && val2)
554 blobmsg_add_string(&b, param2, val2);
555 ubus_notify(ctx, &main_object, type, b.head, -1);
556 }
557
558 static int
559 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
560 struct ubus_request_data *req, const char *method,
561 struct blob_attr *msg)
562 {
563 struct blob_attr *tb[__EVENT_MAX];
564 const char *event;
565
566 if (!msg)
567 return UBUS_STATUS_INVALID_ARGUMENT;
568
569 blobmsg_parse(event_policy, __EVENT_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
570 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
571 return UBUS_STATUS_INVALID_ARGUMENT;
572
573 event = blobmsg_get_string(tb[EVENT_TYPE]);
574 trigger_event(event, tb[EVENT_DATA]);
575
576 if (!strcmp(event, "config.change")) {
577 struct blob_attr *tb2[__VALIDATE_MAX];
578
579 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb2,
580 blobmsg_data(tb[EVENT_DATA]), blobmsg_data_len(tb[EVENT_DATA]));
581 if (tb2[VALIDATE_PACKAGE])
582 ubus_event_bcast("config.change", "config",
583 blobmsg_get_string(tb2[VALIDATE_PACKAGE]), NULL, NULL);
584 }
585 return 0;
586 }
587
588 static int
589 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
590 struct ubus_request_data *req, const char *method,
591 struct blob_attr *msg)
592 {
593 struct blob_attr *tb[__VALIDATE_MAX];
594 char *p = NULL, *t = NULL;
595
596 if (!msg)
597 return UBUS_STATUS_INVALID_ARGUMENT;
598
599 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
600 if (tb[VALIDATE_SERVICE]) {
601 return 0;
602 }
603 if (tb[VALIDATE_PACKAGE])
604 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
605
606 if (tb[VALIDATE_TYPE])
607 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
608
609 blob_buf_init(&b, 0);
610 service_validate_dump_all(&b, p, t);
611 ubus_send_reply(ctx, req, b.head);
612
613 return 0;
614 }
615
616 static int
617 service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
618 struct ubus_request_data *req, const char *method,
619 struct blob_attr *msg)
620 {
621 struct service_instance *in;
622 struct service *s;
623 struct blob_attr *tb[__DATA_MAX];
624 const char *name = NULL;
625 const char *instance = NULL;
626 const char *type = NULL;
627
628 blobmsg_parse(get_data_policy, __DATA_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
629 if (tb[DATA_NAME])
630 name = blobmsg_data(tb[DATA_NAME]);
631 if (tb[DATA_INSTANCE])
632 instance = blobmsg_data(tb[DATA_INSTANCE]);
633 if (tb[DATA_TYPE])
634 type = blobmsg_data(tb[DATA_TYPE]);
635
636 blob_buf_init(&b, 0);
637 avl_for_each_element(&services, s, avl) {
638 void *cs = NULL;
639 void *ci = NULL;
640 struct blobmsg_list_node *var;
641
642 if (name && strcmp(name, s->name))
643 continue;
644
645 blobmsg_list_for_each(&s->data_blob, var) {
646 if (type && strcmp(blobmsg_name(var->data), type))
647 continue;
648
649 if (!cs)
650 cs = blobmsg_open_table(&b, s->name);
651
652 blobmsg_add_blob(&b, var->data);
653 }
654
655 vlist_for_each_element(&s->instances, in, node) {
656 ci = NULL;
657
658 if (instance && strcmp(instance, in->name))
659 continue;
660
661 blobmsg_list_for_each(&in->data, var) {
662 if (type &&
663 strcmp(blobmsg_name(var->data), type))
664 continue;
665
666 if (!cs)
667 cs = blobmsg_open_table(&b, s->name);
668 if (!ci)
669 ci = blobmsg_open_table(&b, in->name);
670
671 blobmsg_add_blob(&b, var->data);
672 }
673
674 if (ci)
675 blobmsg_close_table(&b, ci);
676 }
677
678 if (cs)
679 blobmsg_close_table(&b, cs);
680 }
681
682 ubus_send_reply(ctx, req, b.head);
683 return 0;
684 }
685
686 static int
687 service_handle_console(struct ubus_context *ctx, struct ubus_object *obj,
688 struct ubus_request_data *req, const char *method,
689 struct blob_attr *msg)
690 {
691 bool attach = !strcmp(method, "console_attach");
692 struct blob_attr *tb[__SERVICE_CONSOLE_MAX];
693 struct service *s;
694 struct service_instance *in;
695 int console_fd = -1;
696
697 console_fd = ubus_request_get_caller_fd(req);
698 if (console_fd < 0)
699 return UBUS_STATUS_INVALID_ARGUMENT;
700
701 if (!msg)
702 goto err_console_fd;
703
704 blobmsg_parse(service_console_policy, __SERVICE_CONSOLE_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
705 if (!tb[SERVICE_CONSOLE_NAME])
706 goto err_console_fd;
707
708 s = avl_find_element(&services, blobmsg_data(tb[SERVICE_CONSOLE_NAME]), s, avl);
709 if (!s)
710 goto err_console_fd;
711
712 if (tb[SERVICE_CONSOLE_INSTANCE]) {
713 in = vlist_find(&s->instances, blobmsg_data(tb[SERVICE_CONSOLE_INSTANCE]), in, node);
714 } else {
715 /* use first element in instances list */
716 vlist_for_each_element(&s->instances, in, node)
717 break;
718 }
719 if (!in)
720 goto err_console_fd;
721
722 if (attach) {
723 if (in->console.fd.fd < 0) {
724 close(console_fd);
725 return UBUS_STATUS_NOT_SUPPORTED;
726 }
727
728 /* close and replace existing attached console */
729 if (in->console_client.fd.fd > -1)
730 close(in->console_client.fd.fd);
731
732 ustream_fd_init(&in->console_client, console_fd);
733 } else {
734 ustream_fd_init(&in->console, console_fd);
735 }
736
737 return UBUS_STATUS_OK;
738 err_console_fd:
739 close(console_fd);
740 return UBUS_STATUS_INVALID_ARGUMENT;
741 }
742
743
744 static struct ubus_method main_object_methods[] = {
745 UBUS_METHOD("set", service_handle_set, service_set_attrs),
746 UBUS_METHOD("add", service_handle_set, service_set_attrs),
747 UBUS_METHOD("list", service_handle_list, service_list_attrs),
748 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
749 UBUS_METHOD("signal", service_handle_signal, service_signal_attrs),
750 UBUS_METHOD("update_start", service_handle_update, service_attrs),
751 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
752 UBUS_METHOD("event", service_handle_event, event_policy),
753 UBUS_METHOD("validate", service_handle_validate, validate_policy),
754 UBUS_METHOD("get_data", service_get_data, get_data_policy),
755 UBUS_METHOD("state", service_handle_state, service_state_attrs),
756 UBUS_METHOD("console_set", service_handle_console, service_console_policy),
757 UBUS_METHOD("console_attach", service_handle_console, service_console_policy),
758 };
759
760 static struct ubus_object_type main_object_type =
761 UBUS_OBJECT_TYPE("service", main_object_methods);
762
763 static struct ubus_object main_object = {
764 .name = "service",
765 .type = &main_object_type,
766 .methods = main_object_methods,
767 .n_methods = ARRAY_SIZE(main_object_methods),
768 };
769
770 int
771 service_start_early(char *name, char *cmdline)
772 {
773 void *instances, *instance, *command, *respawn;
774 char *t;
775
776 blob_buf_init(&b, 0);
777 blobmsg_add_string(&b, "name", name);
778 instances = blobmsg_open_table(&b, "instances");
779 instance = blobmsg_open_table(&b, "instance1");
780 command = blobmsg_open_array(&b, "command");
781 t = strtok(cmdline, " ");
782 while (t) {
783 blobmsg_add_string(&b, NULL, t);
784 t = strtok(NULL, " ");
785 }
786 blobmsg_close_array(&b, command);
787 respawn = blobmsg_open_array(&b, "respawn");
788 blobmsg_add_string(&b, NULL, "3600");
789 blobmsg_add_string(&b, NULL, "1");
790 blobmsg_add_string(&b, NULL, "0");
791 blobmsg_close_array(&b, respawn);
792 blobmsg_close_table(&b, instance);
793 blobmsg_close_table(&b, instances);
794
795 return service_handle_set(NULL, NULL, NULL, "add", b.head);
796 }
797
798 void service_stopped(struct service *s)
799 {
800 if (s->deleted && avl_is_empty(&s->instances.avl)) {
801 service_event("service.stop", s->name, NULL);
802 avl_delete(&services, &s->avl);
803 trigger_del(s);
804 service_validate_del(s);
805 free(s->trigger);
806 free(s);
807 }
808 }
809
810 void service_event(const char *type, const char *service, const char *instance)
811 {
812 ubus_event_bcast(type, "service", service, "instance", instance);
813 }
814
815 void ubus_init_service(struct ubus_context *_ctx)
816 {
817 ctx = _ctx;
818 ubus_add_object(ctx, &main_object);
819 }