service: don't use stdio log channel
[project/procd.git] / service / instance.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 <sys/resource.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <net/if.h>
19 #include <unistd.h>
20 #include <stdint.h>
21 #include <fcntl.h>
22 #include <pwd.h>
23 #include <libgen.h>
24
25 #include <libubox/md5.h>
26
27 #include "../procd.h"
28
29 #include "service.h"
30 #include "instance.h"
31
32
33 enum {
34         INSTANCE_ATTR_COMMAND,
35         INSTANCE_ATTR_ENV,
36         INSTANCE_ATTR_DATA,
37         INSTANCE_ATTR_NETDEV,
38         INSTANCE_ATTR_FILE,
39         INSTANCE_ATTR_TRIGGER,
40         INSTANCE_ATTR_RESPAWN,
41         INSTANCE_ATTR_NICE,
42         INSTANCE_ATTR_LIMITS,
43         INSTANCE_ATTR_WATCH,
44         INSTANCE_ATTR_ERROR,
45         INSTANCE_ATTR_USER,
46         INSTANCE_ATTR_STDOUT,
47         INSTANCE_ATTR_STDERR,
48         __INSTANCE_ATTR_MAX
49 };
50
51 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
52         [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
53         [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
54         [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
55         [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
56         [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
57         [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
58         [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
59         [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
60         [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
61         [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
62         [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
63         [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
64         [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
65         [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
66 };
67
68 struct instance_netdev {
69         struct blobmsg_list_node node;
70         int ifindex;
71 };
72
73 struct instance_file {
74         struct blobmsg_list_node node;
75         uint32_t md5[4];
76 };
77
78 struct rlimit_name {
79         const char *name;
80         int resource;
81 };
82
83 static const struct rlimit_name rlimit_names[] = {
84         { "as", RLIMIT_AS },
85         { "core", RLIMIT_CORE },
86         { "cpu", RLIMIT_CPU },
87         { "data", RLIMIT_DATA },
88         { "fsize", RLIMIT_FSIZE },
89         { "memlock", RLIMIT_MEMLOCK },
90         { "msgqueue", RLIMIT_MSGQUEUE },
91         { "nice", RLIMIT_NICE },
92         { "nofile", RLIMIT_NOFILE },
93         { "nproc", RLIMIT_NPROC },
94         { "rss", RLIMIT_RSS },
95         { "rtprio", RLIMIT_RTPRIO },
96         { "sigpending", RLIMIT_SIGPENDING },
97         { "stack", RLIMIT_STACK },
98         { NULL, 0 }
99 };
100
101 static void closefd(int fd)
102 {
103         if (fd > STDERR_FILENO)
104                 close(fd);
105 }
106
107 static void
108 instance_limits(const char *limit, const char *value)
109 {
110         int i;
111         struct rlimit rlim;
112         unsigned long cur, max;
113
114         for (i = 0; rlimit_names[i].name != NULL; i++) {
115                 if (strcmp(rlimit_names[i].name, limit))
116                         continue;
117                 if (!strcmp(value, "unlimited")) {
118                         rlim.rlim_cur = RLIM_INFINITY;
119                         rlim.rlim_max = RLIM_INFINITY;
120                 } else {
121                         if (getrlimit(rlimit_names[i].resource, &rlim))
122                                 return;
123
124                         cur = rlim.rlim_cur;
125                         max = rlim.rlim_max;
126
127                         if (sscanf(value, "%lu %lu", &cur, &max) < 1)
128                                 return;
129
130                         rlim.rlim_cur = cur;
131                         rlim.rlim_max = max;
132                 }
133
134                 setrlimit(rlimit_names[i].resource, &rlim);
135                 return;
136         }
137 }
138
139 static void
140 instance_run(struct service_instance *in, int stdout, int stderr)
141 {
142         struct blobmsg_list_node *var;
143         struct blob_attr *cur;
144         char **argv;
145         int argc = 1; /* NULL terminated */
146         int rem, stdin;
147
148         if (in->nice)
149                 setpriority(PRIO_PROCESS, 0, in->nice);
150
151         blobmsg_for_each_attr(cur, in->command, rem)
152                 argc++;
153
154         blobmsg_list_for_each(&in->env, var)
155                 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
156
157         blobmsg_list_for_each(&in->limits, var)
158                 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
159
160         argv = alloca(sizeof(char *) * argc);
161         argc = 0;
162
163         blobmsg_for_each_attr(cur, in->command, rem)
164                 argv[argc++] = blobmsg_data(cur);
165
166         argv[argc] = NULL;
167
168         stdin = open("/dev/null", O_RDONLY);
169
170         if (stdout == -1)
171                 stdout = open("/dev/null", O_WRONLY);
172
173         if (stderr == -1)
174                 stderr = open("/dev/null", O_WRONLY);
175
176         if (stdin > -1) {
177                 dup2(stdin, STDIN_FILENO);
178                 closefd(stdin);
179         }
180         if (stdout > -1) {
181                 dup2(stdout, STDOUT_FILENO);
182                 closefd(stdout);
183         }
184         if (stderr > -1) {
185                 dup2(stderr, STDERR_FILENO);
186                 closefd(stderr);
187         }
188
189         if (in->uid || in->gid) {
190                 setuid(in->uid);
191                 setgid(in->gid);
192         }
193         execvp(argv[0], argv);
194         exit(127);
195 }
196
197 void
198 instance_start(struct service_instance *in)
199 {
200         int pid;
201         int opipe[2] = { -1, -1 };
202         int epipe[2] = { -1, -1 };
203
204         if (!avl_is_empty(&in->errors.avl)) {
205                 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
206                 return;
207         }
208
209         if (in->proc.pending)
210                 return;
211
212         if (in->stdout.fd.fd > -2) {
213                 if (pipe(opipe)) {
214                         ULOG_WARN("pipe() failed: %d (%s)\n", errno, strerror(errno));
215                         opipe[0] = opipe[1] = -1;
216                 }
217         }
218
219         if (in->stderr.fd.fd > -2) {
220                 if (pipe(epipe)) {
221                         ULOG_WARN("pipe() failed: %d (%s)\n", errno, strerror(errno));
222                         epipe[0] = epipe[1] = -1;
223                 }
224         }
225
226         in->restart = false;
227         in->halt = !in->respawn;
228
229         if (!in->valid)
230                 return;
231
232         pid = fork();
233         if (pid < 0)
234                 return;
235
236         if (!pid) {
237                 uloop_done();
238                 closefd(opipe[0]);
239                 closefd(epipe[0]);
240                 instance_run(in, opipe[1], epipe[1]);
241                 return;
242         }
243
244         DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name);
245         in->proc.pid = pid;
246         clock_gettime(CLOCK_MONOTONIC, &in->start);
247         uloop_process_add(&in->proc);
248
249         if (opipe[0] > -1) {
250                 ustream_fd_init(&in->stdout, opipe[0]);
251                 closefd(opipe[1]);
252         }
253
254         if (epipe[0] > -1) {
255                 ustream_fd_init(&in->stderr, epipe[0]);
256                 closefd(epipe[1]);
257         }
258
259         service_event("instance.start", in->srv->name, in->name);
260 }
261
262 static void
263 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
264 {
265         char *newline, *str, *arg0, ident[32];
266         int len;
267
268         do {
269                 str = ustream_get_read_buf(s, NULL);
270                 if (!str)
271                         break;
272
273                 newline = strchr(str, '\n');
274                 if (!newline)
275                         break;
276
277                 *newline = 0;
278                 len = newline + 1 - str;
279
280                 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
281                 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
282
283                 ulog_open(ULOG_SYSLOG, LOG_DAEMON, ident);
284                 ulog(prio, "%s\n", str);
285                 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
286
287                 ustream_consume(s, len);
288         } while (1);
289 }
290
291 static void
292 instance_stdout(struct ustream *s, int bytes)
293 {
294         instance_stdio(s, LOG_INFO,
295                        container_of(s, struct service_instance, stdout.stream));
296 }
297
298 static void
299 instance_stderr(struct ustream *s, int bytes)
300 {
301         instance_stdio(s, LOG_ERR,
302                        container_of(s, struct service_instance, stderr.stream));
303 }
304
305 static void
306 instance_timeout(struct uloop_timeout *t)
307 {
308         struct service_instance *in;
309
310         in = container_of(t, struct service_instance, timeout);
311
312         if (!in->halt && (in->restart || in->respawn))
313                 instance_start(in);
314 }
315
316 static void
317 instance_exit(struct uloop_process *p, int ret)
318 {
319         struct service_instance *in;
320         struct timespec tp;
321         long runtime;
322
323         in = container_of(p, struct service_instance, proc);
324
325         clock_gettime(CLOCK_MONOTONIC, &tp);
326         runtime = tp.tv_sec - in->start.tv_sec;
327
328         DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
329         if (upgrade_running)
330                 return;
331
332         uloop_timeout_cancel(&in->timeout);
333         if (in->halt) {
334                 /* no action */
335         } else if (in->restart) {
336                 instance_start(in);
337         } else if (in->respawn) {
338                 if (runtime < in->respawn_threshold)
339                         in->respawn_count++;
340                 else
341                         in->respawn_count = 0;
342                 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
343                         LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
344                                                                 in->srv->name, in->name, in->respawn_count, runtime);
345                         in->restart = in->respawn = 0;
346                         in->halt = 1;
347                 } else {
348                         uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
349                 }
350         }
351         service_event("instance.stop", in->srv->name, in->name);
352 }
353
354 void
355 instance_stop(struct service_instance *in)
356 {
357         if (!in->proc.pending)
358                 return;
359         in->halt = true;
360         in->restart = in->respawn = false;
361         kill(in->proc.pid, SIGTERM);
362 }
363
364 static void
365 instance_restart(struct service_instance *in)
366 {
367         if (!in->proc.pending)
368                 return;
369         in->halt = false;
370         in->restart = true;
371         kill(in->proc.pid, SIGTERM);
372 }
373
374 static bool
375 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
376 {
377         if (!in->valid)
378                 return true;
379
380         if (!blob_attr_equal(in->command, in_new->command))
381                 return true;
382
383         if (!blobmsg_list_equal(&in->env, &in_new->env))
384                 return true;
385
386         if (!blobmsg_list_equal(&in->data, &in_new->data))
387                 return true;
388
389         if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
390                 return true;
391
392         if (!blobmsg_list_equal(&in->file, &in_new->file))
393                 return true;
394
395         if (in->nice != in_new->nice)
396                 return true;
397
398         if (in->uid != in_new->uid)
399                 return true;
400
401         if (in->gid != in_new->gid)
402                 return true;
403
404         if (!blobmsg_list_equal(&in->limits, &in_new->limits))
405                 return true;
406
407         if (!blobmsg_list_equal(&in->errors, &in_new->errors))
408                 return true;
409
410         return false;
411 }
412
413 static bool
414 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
415 {
416         struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
417         struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
418
419         return n1->ifindex == n2->ifindex;
420 }
421
422 static void
423 instance_netdev_update(struct blobmsg_list_node *l)
424 {
425         struct instance_netdev *n = container_of(l, struct instance_netdev, node);
426
427         n->ifindex = if_nametoindex(n->node.avl.key);
428 }
429
430 static bool
431 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
432 {
433         struct instance_file *f1 = container_of(l1, struct instance_file, node);
434         struct instance_file *f2 = container_of(l2, struct instance_file, node);
435
436         return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
437 }
438
439 static void
440 instance_file_update(struct blobmsg_list_node *l)
441 {
442         struct instance_file *f = container_of(l, struct instance_file, node);
443         md5_ctx_t md5;
444         char buf[256];
445         int len, fd;
446
447         memset(f->md5, 0, sizeof(f->md5));
448
449         fd = open(l->avl.key, O_RDONLY);
450         if (fd < 0)
451                 return;
452
453         md5_begin(&md5);
454         do {
455                 len = read(fd, buf, sizeof(buf));
456                 if (len < 0) {
457                         if (errno == EINTR)
458                                 continue;
459
460                         break;
461                 }
462                 if (!len)
463                         break;
464
465                 md5_hash(buf, len, &md5);
466         } while(1);
467
468         md5_end(f->md5, &md5);
469         close(fd);
470 }
471
472 static void
473 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
474 {
475         if (!cur)
476                 return;
477
478         blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
479 }
480
481 static bool
482 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
483 {
484         struct blobmsg_list_node *node;
485
486         if (!cur)
487                 return true;
488
489         if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
490                 return false;
491
492         blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
493         if (cb) {
494                 blobmsg_list_for_each(l, node)
495                         cb(node);
496         }
497         return true;
498 }
499
500 static bool
501 instance_config_parse(struct service_instance *in)
502 {
503         struct blob_attr *tb[__INSTANCE_ATTR_MAX];
504         struct blob_attr *cur, *cur2;
505         int argc = 0;
506         int rem;
507
508         blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
509                 blobmsg_data(in->config), blobmsg_data_len(in->config));
510
511         cur = tb[INSTANCE_ATTR_COMMAND];
512         if (!cur)
513                 return false;
514
515         if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
516                 return false;
517
518         blobmsg_for_each_attr(cur2, cur, rem) {
519                 argc++;
520                 break;
521         }
522         if (!argc)
523                 return false;
524
525         in->command = cur;
526
527         if (tb[INSTANCE_ATTR_RESPAWN]) {
528                 int i = 0;
529                 uint32_t vals[3] = { 3600, 5, 5};
530
531                 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
532                         if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
533                                 continue;
534                         vals[i] = atoi(blobmsg_get_string(cur2));
535                         i++;
536                 }
537                 in->respawn = true;
538                 in->respawn_count = 0;
539                 in->respawn_threshold = vals[0];
540                 in->respawn_timeout = vals[1];
541                 in->respawn_retry = vals[2];
542         }
543         if (tb[INSTANCE_ATTR_TRIGGER]) {
544                 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
545                 trigger_add(in->trigger, in);
546         }
547
548         if (tb[INSTANCE_ATTR_WATCH]) {
549                 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
550                         if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
551                                 continue;
552                         DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
553                         watch_add(blobmsg_get_string(cur2), in);
554                 }
555         }
556
557         if ((cur = tb[INSTANCE_ATTR_NICE])) {
558                 in->nice = (int8_t) blobmsg_get_u32(cur);
559                 if (in->nice < -20 || in->nice > 20)
560                         return false;
561         }
562
563         if (tb[INSTANCE_ATTR_USER]) {
564                 struct passwd *p = getpwnam(blobmsg_get_string(tb[INSTANCE_ATTR_USER]));
565                 if (p) {
566                         in->uid = p->pw_uid;
567                         in->gid = p->pw_gid;
568                 }
569         }
570
571         if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
572                 in->stdout.fd.fd = -1;
573
574         if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
575                 in->stderr.fd.fd = -1;
576
577         instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
578
579         if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
580                 return false;
581
582         if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
583                 return false;
584
585         if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
586                 return false;
587
588         if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
589                 return false;
590
591         if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
592                 return false;
593
594         return true;
595 }
596
597 static void
598 instance_config_cleanup(struct service_instance *in)
599 {
600         blobmsg_list_free(&in->env);
601         blobmsg_list_free(&in->data);
602         blobmsg_list_free(&in->netdev);
603         blobmsg_list_free(&in->file);
604         blobmsg_list_free(&in->limits);
605         blobmsg_list_free(&in->errors);
606 }
607
608 static void
609 instance_config_move(struct service_instance *in, struct service_instance *in_src)
610 {
611         instance_config_cleanup(in);
612         blobmsg_list_move(&in->env, &in_src->env);
613         blobmsg_list_move(&in->data, &in_src->data);
614         blobmsg_list_move(&in->netdev, &in_src->netdev);
615         blobmsg_list_move(&in->file, &in_src->file);
616         blobmsg_list_move(&in->limits, &in_src->limits);
617         blobmsg_list_move(&in->errors, &in_src->errors);
618         in->trigger = in_src->trigger;
619         in->command = in_src->command;
620         in->name = in_src->name;
621         in->node.avl.key = in_src->node.avl.key;
622
623         free(in->config);
624         in->config = in_src->config;
625         in_src->config = NULL;
626 }
627
628 bool
629 instance_update(struct service_instance *in, struct service_instance *in_new)
630 {
631         bool changed = instance_config_changed(in, in_new);
632         bool running = in->proc.pending;
633
634         if (!changed && running)
635                 return false;
636
637         if (!running) {
638                 if (changed)
639                         instance_config_move(in, in_new);
640                 instance_start(in);
641         } else {
642                 instance_restart(in);
643                 instance_config_move(in, in_new);
644                 /* restart happens in the child callback handler */
645         }
646         return true;
647 }
648
649 void
650 instance_free(struct service_instance *in)
651 {
652         if (in->stdout.fd.fd > -1) {
653                 ustream_free(&in->stdout.stream);
654                 close(in->stdout.fd.fd);
655         }
656
657         if (in->stderr.fd.fd > -1) {
658                 ustream_free(&in->stderr.stream);
659                 close(in->stderr.fd.fd);
660         }
661
662         uloop_process_delete(&in->proc);
663         uloop_timeout_cancel(&in->timeout);
664         trigger_del(in);
665         watch_del(in);
666         instance_config_cleanup(in);
667         free(in->config);
668         free(in);
669 }
670
671 void
672 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
673 {
674         config = blob_memdup(config);
675         in->srv = s;
676         in->name = blobmsg_name(config);
677         in->config = config;
678         in->timeout.cb = instance_timeout;
679         in->proc.cb = instance_exit;
680
681         in->stdout.fd.fd = -2;
682         in->stdout.stream.string_data = true;
683         in->stdout.stream.notify_read = instance_stdout;
684
685         in->stderr.fd.fd = -2;
686         in->stderr.stream.string_data = true;
687         in->stderr.stream.notify_read = instance_stderr;
688
689         blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
690         blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
691         blobmsg_list_simple_init(&in->env);
692         blobmsg_list_simple_init(&in->data);
693         blobmsg_list_simple_init(&in->limits);
694         blobmsg_list_simple_init(&in->errors);
695         in->valid = instance_config_parse(in);
696 }
697
698 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
699 {
700         void *i;
701
702         i = blobmsg_open_table(b, in->name);
703         blobmsg_add_u8(b, "running", in->proc.pending);
704         if (in->proc.pending)
705                 blobmsg_add_u32(b, "pid", in->proc.pid);
706         blobmsg_add_blob(b, in->command);
707
708         if (!avl_is_empty(&in->errors.avl)) {
709                 struct blobmsg_list_node *var;
710                 void *e = blobmsg_open_array(b, "errors");
711                 blobmsg_list_for_each(&in->errors, var)
712                         blobmsg_add_string(b, NULL, blobmsg_data(var->data));
713                 blobmsg_close_table(b, e);
714         }
715
716         if (!avl_is_empty(&in->env.avl)) {
717                 struct blobmsg_list_node *var;
718                 void *e = blobmsg_open_table(b, "env");
719                 blobmsg_list_for_each(&in->env, var)
720                         blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
721                 blobmsg_close_table(b, e);
722         }
723
724         if (!avl_is_empty(&in->data.avl)) {
725                 struct blobmsg_list_node *var;
726                 void *e = blobmsg_open_table(b, "data");
727                 blobmsg_list_for_each(&in->data, var)
728                         blobmsg_add_blob(b, var->data);
729                 blobmsg_close_table(b, e);
730         }
731
732         if (!avl_is_empty(&in->limits.avl)) {
733                 struct blobmsg_list_node *var;
734                 void *e = blobmsg_open_table(b, "limits");
735                 blobmsg_list_for_each(&in->limits, var)
736                         blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
737                 blobmsg_close_table(b, e);
738         }
739
740         if (in->respawn) {
741                 void *r = blobmsg_open_table(b, "respawn");
742                 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
743                 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
744                 blobmsg_add_u32(b, "retry", in->respawn_retry);
745                 blobmsg_close_table(b, r);
746         }
747
748         if (verbose && in->trigger)
749                 blobmsg_add_blob(b, in->trigger);
750
751         blobmsg_close_table(b, i);
752 }