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