9c798aae67c97f1ef6c75ffba8d215d6dcad185e
[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
88 return s;
89 }
90
91 enum {
92 SERVICE_SET_NAME,
93 SERVICE_SET_SCRIPT,
94 SERVICE_SET_INSTANCES,
95 SERVICE_SET_TRIGGER,
96 SERVICE_SET_VALIDATE,
97 SERVICE_SET_AUTOSTART,
98 __SERVICE_SET_MAX
99 };
100
101 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
102 [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
103 [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
104 [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
105 [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
106 [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
107 [SERVICE_SET_AUTOSTART] = { "autostart", BLOBMSG_TYPE_BOOL },
108 };
109
110 static int
111 service_update(struct service *s, struct blob_attr **tb, bool add)
112 {
113 struct blob_attr *cur;
114 int rem;
115
116 if (s->trigger) {
117 trigger_del(s);
118 free(s->trigger);
119 s->trigger = NULL;
120 }
121
122 service_validate_del(s);
123
124 if (tb[SERVICE_SET_AUTOSTART] && !blobmsg_get_bool(tb[SERVICE_SET_AUTOSTART]))
125 s->autostart = false;
126 else
127 s->autostart = true;
128
129 if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
130 s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
131 if (!s->trigger)
132 return -1;
133 trigger_add(s->trigger, s);
134 }
135
136 if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
137 blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
138 service_validate_add(s, cur);
139 }
140
141 if (tb[SERVICE_SET_INSTANCES]) {
142 if (!add)
143 vlist_update(&s->instances);
144 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
145 service_instance_add(s, cur);
146 }
147 if (!add)
148 vlist_flush(&s->instances);
149 }
150
151 s->deleted = false;
152
153 rc(s->name, "running");
154
155 return 0;
156 }
157
158 static void
159 service_delete(struct service *s)
160 {
161 vlist_flush_all(&s->instances);
162 s->deleted = true;
163 service_stopped(s);
164 }
165
166 enum {
167 SERVICE_ATTR_NAME,
168 __SERVICE_ATTR_MAX,
169 };
170
171 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
172 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
173 };
174
175 enum {
176 SERVICE_DEL_ATTR_NAME,
177 SERVICE_DEL_ATTR_INSTANCE,
178 __SERVICE_DEL_ATTR_MAX,
179 };
180
181 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
182 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
183 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
184 };
185
186 enum {
187 SERVICE_LIST_ATTR_NAME,
188 SERVICE_LIST_ATTR_VERBOSE,
189 __SERVICE_LIST_ATTR_MAX,
190 };
191
192 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
193 [SERVICE_LIST_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
194 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
195 };
196
197 enum {
198 SERVICE_SIGNAL_ATTR_NAME,
199 SERVICE_SIGNAL_ATTR_INSTANCE,
200 SERVICE_SIGNAL_ATTR_SIGNAL,
201 __SERVICE_SIGNAL_ATTR_MAX,
202 };
203
204 static const struct blobmsg_policy service_signal_attrs[__SERVICE_SIGNAL_ATTR_MAX] = {
205 [SERVICE_SIGNAL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
206 [SERVICE_SIGNAL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
207 [SERVICE_SIGNAL_ATTR_SIGNAL] = { "signal", BLOBMSG_TYPE_INT32 },
208 };
209
210 enum {
211 SERVICE_STATE_ATTR_SPAWN,
212 SERVICE_STATE_ATTR_NAME,
213 __SERVICE_STATE_ATTR_MAX,
214 };
215
216 static const struct blobmsg_policy service_state_attrs[__SERVICE_STATE_ATTR_MAX] = {
217 [SERVICE_STATE_ATTR_SPAWN] = { "spawn", BLOBMSG_TYPE_BOOL },
218 [SERVICE_STATE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
219 };
220
221 enum {
222 EVENT_TYPE,
223 EVENT_DATA,
224 __EVENT_MAX
225 };
226
227 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
228 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
229 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
230 };
231
232 enum {
233 VALIDATE_PACKAGE,
234 VALIDATE_TYPE,
235 VALIDATE_SERVICE,
236 __VALIDATE_MAX
237 };
238
239 static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
240 [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
241 [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
242 [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
243 };
244
245 enum {
246 DATA_NAME,
247 DATA_INSTANCE,
248 DATA_TYPE,
249 __DATA_MAX
250 };
251
252 static const struct blobmsg_policy get_data_policy[] = {
253 [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING },
254 [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
255 [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING },
256 };
257
258 static int
259 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
260 struct ubus_request_data *req, const char *method,
261 struct blob_attr *msg)
262 {
263 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
264 struct service *s = NULL;
265 const char *name;
266 bool add = !strcmp(method, "add");
267 int ret;
268
269 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
270 cur = tb[SERVICE_SET_NAME];
271 if (!cur)
272 return UBUS_STATUS_INVALID_ARGUMENT;
273
274 name = blobmsg_data(cur);
275
276 s = avl_find_element(&services, name, s, avl);
277 if (s) {
278 DEBUG(2, "Update service %s\n", name);
279 return service_update(s, tb, add);
280 }
281
282 DEBUG(2, "Create service %s\n", name);
283 s = service_alloc(name);
284 if (!s)
285 return UBUS_STATUS_UNKNOWN_ERROR;
286
287 ret = service_update(s, tb, add);
288 if (ret)
289 return ret;
290
291 avl_insert(&services, &s->avl);
292
293 service_event("service.start", s->name, NULL);
294
295 return 0;
296 }
297
298 static void
299 service_dump(struct service *s, bool verbose)
300 {
301 struct service_instance *in;
302 void *c, *i;
303
304 c = blobmsg_open_table(&b, s->name);
305
306 if (!s->autostart)
307 blobmsg_add_u8(&b, "autostart", false);
308
309 if (!avl_is_empty(&s->instances.avl)) {
310 i = blobmsg_open_table(&b, "instances");
311 vlist_for_each_element(&s->instances, in, node)
312 instance_dump(&b, in, verbose);
313 blobmsg_close_table(&b, i);
314 }
315 if (verbose && s->trigger)
316 blobmsg_add_blob(&b, s->trigger);
317 if (verbose && !list_empty(&s->validators))
318 service_validate_dump(&b, s);
319 blobmsg_close_table(&b, c);
320 }
321
322 static int
323 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
324 struct ubus_request_data *req, const char *method,
325 struct blob_attr *msg)
326 {
327 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
328 struct service *s;
329 const char *name = NULL;
330 bool verbose = false;
331
332 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
333
334 if (tb[SERVICE_LIST_ATTR_VERBOSE])
335 verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
336 if (tb[SERVICE_LIST_ATTR_NAME])
337 name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
338
339 blob_buf_init(&b, 0);
340 avl_for_each_element(&services, s, avl) {
341 if (name && strcmp(s->name, name) != 0)
342 continue;
343
344 service_dump(s, verbose);
345 }
346
347 ubus_send_reply(ctx, req, b.head);
348
349 return 0;
350 }
351
352 static int
353 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
354 struct ubus_request_data *req, const char *method,
355 struct blob_attr *msg)
356 {
357 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
358 struct service *s;
359 struct service_instance *in;
360
361 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
362
363 cur = tb[SERVICE_DEL_ATTR_NAME];
364 if (!cur)
365 return UBUS_STATUS_NOT_FOUND;
366
367 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
368 if (!s)
369 return UBUS_STATUS_NOT_FOUND;
370
371 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
372 if (!cur) {
373 service_delete(s);
374 return 0;
375 }
376
377 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
378 if (!in) {
379 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
380 return UBUS_STATUS_NOT_FOUND;
381 }
382
383 vlist_delete(&s->instances, &in->node);
384
385 return 0;
386 }
387
388 static int
389 service_handle_kill(struct service_instance *in, int sig)
390 {
391 if (kill(in->proc.pid, sig) == 0)
392 return 0;
393
394 switch (errno) {
395 case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
396 case EPERM: return UBUS_STATUS_PERMISSION_DENIED;
397 case ESRCH: return UBUS_STATUS_NOT_FOUND;
398 }
399
400 return UBUS_STATUS_UNKNOWN_ERROR;
401 }
402
403 static int
404 service_handle_signal(struct ubus_context *ctx, struct ubus_object *obj,
405 struct ubus_request_data *req, const char *method,
406 struct blob_attr *msg)
407 {
408 struct blob_attr *tb[__SERVICE_SIGNAL_ATTR_MAX], *cur;
409 struct service *s;
410 struct service_instance *in;
411 int sig = SIGHUP;
412 int rv = 0;
413
414 blobmsg_parse(service_signal_attrs, __SERVICE_SIGNAL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
415
416 cur = tb[SERVICE_SIGNAL_ATTR_SIGNAL];
417 if (cur)
418 sig = blobmsg_get_u32(cur);
419
420 cur = tb[SERVICE_SIGNAL_ATTR_NAME];
421 if (!cur)
422 return UBUS_STATUS_NOT_FOUND;
423
424 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
425 if (!s)
426 return UBUS_STATUS_NOT_FOUND;
427
428 cur = tb[SERVICE_SIGNAL_ATTR_INSTANCE];
429 if (!cur) {
430 vlist_for_each_element(&s->instances, in, node)
431 rv = service_handle_kill(in, sig);
432
433 return rv;
434 }
435
436 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
437 if (!in) {
438 ERROR("instance %s not found\n", blobmsg_get_string(cur));
439 return UBUS_STATUS_NOT_FOUND;
440 }
441
442 return service_handle_kill(in, sig);
443 }
444
445 static int
446 service_handle_state(struct ubus_context *ctx, struct ubus_object *obj,
447 struct ubus_request_data *req, const char *method,
448 struct blob_attr *msg)
449 {
450 struct blob_attr *tb[__SERVICE_STATE_ATTR_MAX];
451 struct service *s;
452 struct service_instance *in;
453 int spawn;
454
455 blobmsg_parse(service_state_attrs, __SERVICE_STATE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
456
457 if (!tb[SERVICE_STATE_ATTR_SPAWN])
458 return UBUS_STATUS_INVALID_ARGUMENT;
459
460 if (!tb[SERVICE_STATE_ATTR_NAME])
461 return UBUS_STATUS_NOT_FOUND;
462
463 s = avl_find_element(&services, blobmsg_data(tb[SERVICE_STATE_ATTR_NAME]), s, avl);
464 if (!s)
465 return UBUS_STATUS_NOT_FOUND;
466
467 spawn = !!blobmsg_get_u8(tb[SERVICE_STATE_ATTR_SPAWN]);
468 vlist_for_each_element(&s->instances, in, node) {
469 if (!!in->proc.pending == !!spawn)
470 continue;
471 else if (!in->proc.pending)
472 instance_start(in);
473 else
474 instance_stop(in, false);
475 }
476
477 return UBUS_STATUS_OK;
478 }
479
480 static int
481 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
482 struct ubus_request_data *req, const char *method,
483 struct blob_attr *msg)
484 {
485 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
486 struct service *s;
487
488 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
489
490 cur = tb[SERVICE_SET_NAME];
491 if (!cur)
492 return UBUS_STATUS_INVALID_ARGUMENT;
493
494 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
495 if (!s)
496 return UBUS_STATUS_NOT_FOUND;
497
498 if (!strcmp(method, "update_start"))
499 vlist_update(&s->instances);
500 else
501 vlist_flush(&s->instances);
502
503 return 0;
504 }
505
506 static void ubus_event_bcast(const char *type, const char *param1, const char *val1,
507 const char *param2, const char *val2)
508 {
509 if (!ctx)
510 return;
511
512 blob_buf_init(&b, 0);
513 if (param1 && val1)
514 blobmsg_add_string(&b, param1, val1);
515 if (param2 && val2)
516 blobmsg_add_string(&b, param2, val2);
517 ubus_notify(ctx, &main_object, type, b.head, -1);
518 }
519
520 static int
521 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
522 struct ubus_request_data *req, const char *method,
523 struct blob_attr *msg)
524 {
525 struct blob_attr *tb[__EVENT_MAX];
526 const char *event;
527
528 if (!msg)
529 return UBUS_STATUS_INVALID_ARGUMENT;
530
531 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
532 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
533 return UBUS_STATUS_INVALID_ARGUMENT;
534
535 event = blobmsg_get_string(tb[EVENT_TYPE]);
536 trigger_event(event, tb[EVENT_DATA]);
537
538 if (!strcmp(event, "config.change")) {
539 struct blob_attr *tb2[__DATA_MAX];
540
541 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb2,
542 blobmsg_data(tb[EVENT_DATA]), blobmsg_len(tb[EVENT_DATA]));
543 if (tb2[VALIDATE_PACKAGE])
544 ubus_event_bcast("config.change", "config",
545 blobmsg_get_string(tb2[VALIDATE_PACKAGE]), NULL, NULL);
546 }
547 return 0;
548 }
549
550 static int
551 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
552 struct ubus_request_data *req, const char *method,
553 struct blob_attr *msg)
554 {
555 struct blob_attr *tb[__VALIDATE_MAX];
556 char *p = NULL, *t = NULL;
557
558 if (!msg)
559 return UBUS_STATUS_INVALID_ARGUMENT;
560
561 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
562 if (tb[VALIDATE_SERVICE]) {
563 return 0;
564 }
565 if (tb[VALIDATE_PACKAGE])
566 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
567
568 if (tb[VALIDATE_TYPE])
569 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
570
571 blob_buf_init(&b, 0);
572 service_validate_dump_all(&b, p, t);
573 ubus_send_reply(ctx, req, b.head);
574
575 return 0;
576 }
577
578 static int
579 service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
580 struct ubus_request_data *req, const char *method,
581 struct blob_attr *msg)
582 {
583 struct service_instance *in;
584 struct service *s;
585 struct blob_attr *tb[__DATA_MAX];
586 const char *name = NULL;
587 const char *instance = NULL;
588 const char *type = NULL;
589
590 blobmsg_parse(get_data_policy, __DATA_MAX, tb, blob_data(msg), blob_len(msg));
591 if (tb[DATA_NAME])
592 name = blobmsg_data(tb[DATA_NAME]);
593 if (tb[DATA_INSTANCE])
594 instance = blobmsg_data(tb[DATA_INSTANCE]);
595 if (tb[DATA_TYPE])
596 type = blobmsg_data(tb[DATA_TYPE]);
597
598 blob_buf_init(&b, 0);
599 avl_for_each_element(&services, s, avl) {
600 void *cs = NULL;
601
602 if (name && strcmp(name, s->name))
603 continue;
604
605 vlist_for_each_element(&s->instances, in, node) {
606 struct blobmsg_list_node *var;
607 void *ci = NULL;
608
609 if (instance && strcmp(instance, in->name))
610 continue;
611
612 blobmsg_list_for_each(&in->data, var) {
613 if (type &&
614 strcmp(blobmsg_name(var->data), type))
615 continue;
616
617 if (!cs)
618 cs = blobmsg_open_table(&b, s->name);
619 if (!ci)
620 ci = blobmsg_open_table(&b, in->name);
621
622 blobmsg_add_blob(&b, var->data);
623 }
624
625 if (ci)
626 blobmsg_close_table(&b, ci);
627 }
628
629 if (cs)
630 blobmsg_close_table(&b, cs);
631 }
632
633 ubus_send_reply(ctx, req, b.head);
634 return 0;
635 }
636
637 static struct ubus_method main_object_methods[] = {
638 UBUS_METHOD("set", service_handle_set, service_set_attrs),
639 UBUS_METHOD("add", service_handle_set, service_set_attrs),
640 UBUS_METHOD("list", service_handle_list, service_list_attrs),
641 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
642 UBUS_METHOD("signal", service_handle_signal, service_signal_attrs),
643 UBUS_METHOD("update_start", service_handle_update, service_attrs),
644 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
645 UBUS_METHOD("event", service_handle_event, event_policy),
646 UBUS_METHOD("validate", service_handle_validate, validate_policy),
647 UBUS_METHOD("get_data", service_get_data, get_data_policy),
648 UBUS_METHOD("state", service_handle_state, service_state_attrs),
649 };
650
651 static struct ubus_object_type main_object_type =
652 UBUS_OBJECT_TYPE("service", main_object_methods);
653
654 static struct ubus_object main_object = {
655 .name = "service",
656 .type = &main_object_type,
657 .methods = main_object_methods,
658 .n_methods = ARRAY_SIZE(main_object_methods),
659 };
660
661 int
662 service_start_early(char *name, char *cmdline)
663 {
664 void *instances, *instance, *command, *respawn;
665 char *t;
666
667 blob_buf_init(&b, 0);
668 blobmsg_add_string(&b, "name", name);
669 instances = blobmsg_open_table(&b, "instances");
670 instance = blobmsg_open_table(&b, "instance1");
671 command = blobmsg_open_array(&b, "command");
672 t = strtok(cmdline, " ");
673 while (t) {
674 blobmsg_add_string(&b, NULL, t);
675 t = strtok(NULL, " ");
676 }
677 blobmsg_close_array(&b, command);
678 respawn = blobmsg_open_array(&b, "respawn");
679 blobmsg_add_string(&b, NULL, "3600");
680 blobmsg_add_string(&b, NULL, "1");
681 blobmsg_add_string(&b, NULL, "0");
682 blobmsg_close_array(&b, respawn);
683 blobmsg_close_table(&b, instance);
684 blobmsg_close_table(&b, instances);
685
686 return service_handle_set(NULL, NULL, NULL, "add", b.head);
687 }
688
689 void service_stopped(struct service *s)
690 {
691 if (s->deleted && avl_is_empty(&s->instances.avl)) {
692 service_event("service.stop", s->name, NULL);
693 avl_delete(&services, &s->avl);
694 trigger_del(s);
695 service_validate_del(s);
696 free(s->trigger);
697 free(s);
698 }
699 }
700
701 void service_event(const char *type, const char *service, const char *instance)
702 {
703 ubus_event_bcast(type, "service", service, "instance", instance);
704 }
705
706 void ubus_init_service(struct ubus_context *_ctx)
707 {
708 ctx = _ctx;
709 ubus_add_object(ctx, &main_object);
710 }