procd: add timing to start/stop logging
[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 static int
278 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
279                    struct ubus_request_data *req, const char *method,
280                    struct blob_attr *msg)
281 {
282         struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
283         struct service *s = NULL;
284         const char *name;
285         bool add = !strcmp(method, "add");
286         int ret;
287
288         blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
289         cur = tb[SERVICE_SET_NAME];
290         if (!cur)
291                 return UBUS_STATUS_INVALID_ARGUMENT;
292
293         name = blobmsg_data(cur);
294
295         s = avl_find_element(&services, name, s, avl);
296         if (s) {
297                 DEBUG(2, "Update service %s\n", name);
298                 return service_update(s, tb, add);
299         }
300
301         DEBUG(2, "Create service %s\n", name);
302         s = service_alloc(name);
303         if (!s)
304                 return UBUS_STATUS_UNKNOWN_ERROR;
305
306         ret = service_update(s, tb, add);
307         if (ret)
308                 return ret;
309
310         avl_insert(&services, &s->avl);
311
312         service_event("service.start", s->name, NULL);
313
314         return 0;
315 }
316
317 static void
318 service_dump(struct service *s, bool verbose)
319 {
320         struct service_instance *in;
321         void *c, *i;
322
323         c = blobmsg_open_table(&b, s->name);
324
325         if (!s->autostart)
326                 blobmsg_add_u8(&b, "autostart", false);
327
328         if (!avl_is_empty(&s->data_blob.avl)) {
329                 struct blobmsg_list_node *var;
330                 i = blobmsg_open_table(&b, "data");
331                 blobmsg_list_for_each(&s->data_blob, var)
332                         blobmsg_add_blob(&b, var->data);
333                 blobmsg_close_table(&b, i);
334         }
335
336         if (!avl_is_empty(&s->instances.avl)) {
337                 i = blobmsg_open_table(&b, "instances");
338                 vlist_for_each_element(&s->instances, in, node)
339                         instance_dump(&b, in, verbose);
340                 blobmsg_close_table(&b, i);
341         }
342         if (verbose && s->trigger)
343                 blobmsg_add_blob(&b, s->trigger);
344         if (verbose && !list_empty(&s->validators))
345                 service_validate_dump(&b, s);
346         blobmsg_close_table(&b, c);
347 }
348
349 static int
350 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
351                     struct ubus_request_data *req, const char *method,
352                     struct blob_attr *msg)
353 {
354         struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
355         struct service *s;
356         const char *name = NULL;
357         bool verbose = false;
358
359         blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
360
361         if (tb[SERVICE_LIST_ATTR_VERBOSE])
362                 verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
363         if (tb[SERVICE_LIST_ATTR_NAME])
364                 name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
365
366         blob_buf_init(&b, 0);
367         avl_for_each_element(&services, s, avl) {
368                 if (name && strcmp(s->name, name) != 0)
369                         continue;
370
371                 service_dump(s, verbose);
372         }
373
374         ubus_send_reply(ctx, req, b.head);
375
376         return 0;
377 }
378
379 static int
380 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
381                     struct ubus_request_data *req, const char *method,
382                     struct blob_attr *msg)
383 {
384         struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
385         struct service *s;
386         struct service_instance *in;
387
388         blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
389
390         cur = tb[SERVICE_DEL_ATTR_NAME];
391         if (!cur)
392                 return UBUS_STATUS_NOT_FOUND;
393
394         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
395         if (!s)
396                 return UBUS_STATUS_NOT_FOUND;
397
398         cur = tb[SERVICE_DEL_ATTR_INSTANCE];
399         if (!cur) {
400                 service_delete(s);
401                 return 0;
402         }
403
404         in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
405         if (!in) {
406                 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
407                 return UBUS_STATUS_NOT_FOUND;
408         }
409
410         vlist_delete(&s->instances, &in->node);
411
412         return 0;
413 }
414
415 static int
416 service_handle_kill(struct service_instance *in, int sig)
417 {
418         if (kill(in->proc.pid, sig) == 0)
419                 return 0;
420
421         switch (errno) {
422         case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
423         case EPERM:  return UBUS_STATUS_PERMISSION_DENIED;
424         case ESRCH:  return UBUS_STATUS_NOT_FOUND;
425         }
426
427         return UBUS_STATUS_UNKNOWN_ERROR;
428 }
429
430 static int
431 service_handle_signal(struct ubus_context *ctx, struct ubus_object *obj,
432                     struct ubus_request_data *req, const char *method,
433                     struct blob_attr *msg)
434 {
435         struct blob_attr *tb[__SERVICE_SIGNAL_ATTR_MAX], *cur;
436         struct service *s;
437         struct service_instance *in;
438         int sig = SIGHUP;
439         int rv = 0;
440
441         blobmsg_parse(service_signal_attrs, __SERVICE_SIGNAL_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
442
443         cur = tb[SERVICE_SIGNAL_ATTR_SIGNAL];
444         if (cur)
445                 sig = blobmsg_get_u32(cur);
446
447         cur = tb[SERVICE_SIGNAL_ATTR_NAME];
448         if (!cur)
449                 return UBUS_STATUS_NOT_FOUND;
450
451         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
452         if (!s)
453                 return UBUS_STATUS_NOT_FOUND;
454
455         cur = tb[SERVICE_SIGNAL_ATTR_INSTANCE];
456         if (!cur) {
457                 vlist_for_each_element(&s->instances, in, node)
458                         rv = service_handle_kill(in, sig);
459
460                 return rv;
461         }
462
463         in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
464         if (!in) {
465                 ERROR("instance %s not found\n", blobmsg_get_string(cur));
466                 return UBUS_STATUS_NOT_FOUND;
467         }
468
469         return service_handle_kill(in, sig);
470 }
471
472 static int
473 service_handle_state(struct ubus_context *ctx, struct ubus_object *obj,
474                      struct ubus_request_data *req, const char *method,
475                      struct blob_attr *msg)
476 {
477         struct blob_attr *tb[__SERVICE_STATE_ATTR_MAX];
478         struct service *s;
479         struct service_instance *in;
480         int spawn;
481
482         blobmsg_parse(service_state_attrs, __SERVICE_STATE_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
483
484         if (!tb[SERVICE_STATE_ATTR_SPAWN])
485                 return UBUS_STATUS_INVALID_ARGUMENT;
486
487         if (!tb[SERVICE_STATE_ATTR_NAME])
488                 return UBUS_STATUS_NOT_FOUND;
489
490         s = avl_find_element(&services, blobmsg_data(tb[SERVICE_STATE_ATTR_NAME]), s, avl);
491         if (!s)
492                 return UBUS_STATUS_NOT_FOUND;
493
494         spawn = !!blobmsg_get_u8(tb[SERVICE_STATE_ATTR_SPAWN]);
495         vlist_for_each_element(&s->instances, in, node) {
496                 if (!!in->proc.pending == !!spawn)
497                         continue;
498                 else if (!in->proc.pending)
499                         instance_start(in);
500                 else
501                         instance_stop(in, false);
502         }
503
504         return UBUS_STATUS_OK;
505 }
506
507 static int
508 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
509                       struct ubus_request_data *req, const char *method,
510                       struct blob_attr *msg)
511 {
512         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
513         struct service *s;
514
515         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
516
517         cur = tb[SERVICE_SET_NAME];
518         if (!cur)
519                 return UBUS_STATUS_INVALID_ARGUMENT;
520
521         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
522         if (!s)
523                 return UBUS_STATUS_NOT_FOUND;
524
525         if (!strcmp(method, "update_start"))
526                 vlist_update(&s->instances);
527         else
528                 vlist_flush(&s->instances);
529
530         return 0;
531 }
532
533 static void ubus_event_bcast(const char *type, const char *param1, const char *val1,
534                              const char *param2, const char *val2)
535 {
536         if (!ctx)
537                 return;
538
539         blob_buf_init(&b, 0);
540         if (param1 && val1)
541                 blobmsg_add_string(&b, param1, val1);
542         if (param2 && val2)
543                 blobmsg_add_string(&b, param2, val2);
544         ubus_notify(ctx, &main_object, type, b.head, -1);
545 }
546
547 static int
548 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
549                         struct ubus_request_data *req, const char *method,
550                         struct blob_attr *msg)
551 {
552         struct blob_attr *tb[__EVENT_MAX];
553         const char *event;
554
555         if (!msg)
556                 return UBUS_STATUS_INVALID_ARGUMENT;
557
558         blobmsg_parse(event_policy, __EVENT_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
559         if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
560                 return UBUS_STATUS_INVALID_ARGUMENT;
561
562         event = blobmsg_get_string(tb[EVENT_TYPE]);
563         trigger_event(event, tb[EVENT_DATA]);
564
565         if (!strcmp(event, "config.change")) {
566                 struct blob_attr *tb2[__VALIDATE_MAX];
567
568                 blobmsg_parse(validate_policy, __VALIDATE_MAX, tb2,
569                               blobmsg_data(tb[EVENT_DATA]), blobmsg_data_len(tb[EVENT_DATA]));
570                 if (tb2[VALIDATE_PACKAGE])
571                         ubus_event_bcast("config.change", "config",
572                                          blobmsg_get_string(tb2[VALIDATE_PACKAGE]), NULL, NULL);
573         }
574         return 0;
575 }
576
577 static int
578 service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
579                         struct ubus_request_data *req, const char *method,
580                         struct blob_attr *msg)
581 {
582         struct blob_attr *tb[__VALIDATE_MAX];
583         char *p = NULL, *t = NULL;
584
585         if (!msg)
586                 return UBUS_STATUS_INVALID_ARGUMENT;
587
588         blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
589         if (tb[VALIDATE_SERVICE]) {
590                 return 0;
591         }
592         if (tb[VALIDATE_PACKAGE])
593                 p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
594
595         if (tb[VALIDATE_TYPE])
596                 t = blobmsg_get_string(tb[VALIDATE_TYPE]);
597
598         blob_buf_init(&b, 0);
599         service_validate_dump_all(&b, p, t);
600         ubus_send_reply(ctx, req, b.head);
601
602         return 0;
603 }
604
605 static int
606 service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
607                  struct ubus_request_data *req, const char *method,
608                  struct blob_attr *msg)
609 {
610         struct service_instance *in;
611         struct service *s;
612         struct blob_attr *tb[__DATA_MAX];
613         const char *name = NULL;
614         const char *instance = NULL;
615         const char *type = NULL;
616
617         blobmsg_parse(get_data_policy, __DATA_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
618         if (tb[DATA_NAME])
619                 name = blobmsg_data(tb[DATA_NAME]);
620         if (tb[DATA_INSTANCE])
621                 instance = blobmsg_data(tb[DATA_INSTANCE]);
622         if (tb[DATA_TYPE])
623                 type = blobmsg_data(tb[DATA_TYPE]);
624
625         blob_buf_init(&b, 0);
626         avl_for_each_element(&services, s, avl) {
627                 void *cs = NULL;
628                 void *ci = NULL;
629                 struct blobmsg_list_node *var;
630
631                 if (name && strcmp(name, s->name))
632                         continue;
633
634                 blobmsg_list_for_each(&s->data_blob, var) {
635                         if (type && strcmp(blobmsg_name(var->data), type))
636                                 continue;
637
638                         if (!cs)
639                                 cs = blobmsg_open_table(&b, s->name);
640
641                         blobmsg_add_blob(&b, var->data);
642                 }
643
644                 vlist_for_each_element(&s->instances, in, node) {
645                         ci = NULL;
646
647                         if (instance && strcmp(instance, in->name))
648                                 continue;
649
650                         blobmsg_list_for_each(&in->data, var) {
651                                 if (type &&
652                                     strcmp(blobmsg_name(var->data), type))
653                                         continue;
654
655                                 if (!cs)
656                                         cs = blobmsg_open_table(&b, s->name);
657                                 if (!ci)
658                                         ci = blobmsg_open_table(&b, in->name);
659
660                                 blobmsg_add_blob(&b, var->data);
661                         }
662
663                         if (ci)
664                                 blobmsg_close_table(&b, ci);
665                 }
666
667                 if (cs)
668                         blobmsg_close_table(&b, cs);
669         }
670
671         ubus_send_reply(ctx, req, b.head);
672         return 0;
673 }
674
675 static struct ubus_method main_object_methods[] = {
676         UBUS_METHOD("set", service_handle_set, service_set_attrs),
677         UBUS_METHOD("add", service_handle_set, service_set_attrs),
678         UBUS_METHOD("list", service_handle_list, service_list_attrs),
679         UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
680         UBUS_METHOD("signal", service_handle_signal, service_signal_attrs),
681         UBUS_METHOD("update_start", service_handle_update, service_attrs),
682         UBUS_METHOD("update_complete", service_handle_update, service_attrs),
683         UBUS_METHOD("event", service_handle_event, event_policy),
684         UBUS_METHOD("validate", service_handle_validate, validate_policy),
685         UBUS_METHOD("get_data", service_get_data, get_data_policy),
686         UBUS_METHOD("state", service_handle_state, service_state_attrs),
687 };
688
689 static struct ubus_object_type main_object_type =
690         UBUS_OBJECT_TYPE("service", main_object_methods);
691
692 static struct ubus_object main_object = {
693         .name = "service",
694         .type = &main_object_type,
695         .methods = main_object_methods,
696         .n_methods = ARRAY_SIZE(main_object_methods),
697 };
698
699 int
700 service_start_early(char *name, char *cmdline)
701 {
702         void *instances, *instance, *command, *respawn;
703         char *t;
704
705         blob_buf_init(&b, 0);
706         blobmsg_add_string(&b, "name", name);
707         instances = blobmsg_open_table(&b, "instances");
708         instance = blobmsg_open_table(&b, "instance1");
709         command = blobmsg_open_array(&b, "command");
710         t = strtok(cmdline, " ");
711         while (t) {
712                 blobmsg_add_string(&b, NULL, t);
713                 t = strtok(NULL, " ");
714         }
715         blobmsg_close_array(&b, command);
716         respawn = blobmsg_open_array(&b, "respawn");
717         blobmsg_add_string(&b, NULL, "3600");
718         blobmsg_add_string(&b, NULL, "1");
719         blobmsg_add_string(&b, NULL, "0");
720         blobmsg_close_array(&b, respawn);
721         blobmsg_close_table(&b, instance);
722         blobmsg_close_table(&b, instances);
723
724         return service_handle_set(NULL, NULL, NULL, "add", b.head);
725 }
726
727 void service_stopped(struct service *s)
728 {
729         if (s->deleted && avl_is_empty(&s->instances.avl)) {
730                 service_event("service.stop", s->name, NULL);
731                 avl_delete(&services, &s->avl);
732                 trigger_del(s);
733                 service_validate_del(s);
734                 free(s->trigger);
735                 free(s);
736         }
737 }
738
739 void service_event(const char *type, const char *service, const char *instance)
740 {
741         ubus_event_bcast(type, "service", service, "instance", instance);
742 }
743
744 void ubus_init_service(struct ubus_context *_ctx)
745 {
746         ctx = _ctx;
747         ubus_add_object(ctx, &main_object);
748 }