instance: harmonize instance API
[oweals/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 #define _GNU_SOURCE
16 #include <sys/resource.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20 #include <grp.h>
21 #include <net/if.h>
22 #include <unistd.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <pwd.h>
27 #include <libgen.h>
28 #include <unistd.h>
29 #define SYSLOG_NAMES
30 #include <syslog.h>
31
32 #include <libubox/md5.h>
33
34 #include "../procd.h"
35
36 #include "service.h"
37 #include "instance.h"
38
39 #define UJAIL_BIN_PATH "/sbin/ujail"
40
41 enum {
42         INSTANCE_ATTR_COMMAND,
43         INSTANCE_ATTR_ENV,
44         INSTANCE_ATTR_DATA,
45         INSTANCE_ATTR_NETDEV,
46         INSTANCE_ATTR_FILE,
47         INSTANCE_ATTR_TRIGGER,
48         INSTANCE_ATTR_RESPAWN,
49         INSTANCE_ATTR_NICE,
50         INSTANCE_ATTR_LIMITS,
51         INSTANCE_ATTR_WATCH,
52         INSTANCE_ATTR_ERROR,
53         INSTANCE_ATTR_USER,
54         INSTANCE_ATTR_GROUP,
55         INSTANCE_ATTR_STDOUT,
56         INSTANCE_ATTR_STDERR,
57         INSTANCE_ATTR_NO_NEW_PRIVS,
58         INSTANCE_ATTR_JAIL,
59         INSTANCE_ATTR_TRACE,
60         INSTANCE_ATTR_SECCOMP,
61         INSTANCE_ATTR_PIDFILE,
62         INSTANCE_ATTR_RELOADSIG,
63         INSTANCE_ATTR_TERMTIMEOUT,
64         INSTANCE_ATTR_FACILITY,
65         INSTANCE_ATTR_EXTROOT,
66         INSTANCE_ATTR_OVERLAYDIR,
67         INSTANCE_ATTR_TMPOVERLAYSIZE,
68         __INSTANCE_ATTR_MAX
69 };
70
71 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
72         [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
73         [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
74         [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
75         [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
76         [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
77         [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
78         [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
79         [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
80         [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
81         [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
82         [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
83         [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
84         [INSTANCE_ATTR_GROUP] = { "group", BLOBMSG_TYPE_STRING },
85         [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
86         [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
87         [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
88         [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
89         [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
90         [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
91         [INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
92         [INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
93         [INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
94         [INSTANCE_ATTR_FACILITY] = { "facility", BLOBMSG_TYPE_STRING },
95         [INSTANCE_ATTR_EXTROOT] = { "extroot", BLOBMSG_TYPE_STRING },
96         [INSTANCE_ATTR_OVERLAYDIR] = { "overlaydir", BLOBMSG_TYPE_STRING },
97         [INSTANCE_ATTR_TMPOVERLAYSIZE] = { "tmpoverlaysize", BLOBMSG_TYPE_STRING },
98 };
99
100 enum {
101         JAIL_ATTR_NAME,
102         JAIL_ATTR_HOSTNAME,
103         JAIL_ATTR_PROCFS,
104         JAIL_ATTR_SYSFS,
105         JAIL_ATTR_UBUS,
106         JAIL_ATTR_LOG,
107         JAIL_ATTR_RONLY,
108         JAIL_ATTR_MOUNT,
109         JAIL_ATTR_NETNS,
110         JAIL_ATTR_USERNS,
111         JAIL_ATTR_CGROUPSNS,
112         JAIL_ATTR_REQUIREJAIL,
113         __JAIL_ATTR_MAX,
114 };
115
116 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
117         [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
118         [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
119         [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
120         [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
121         [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
122         [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
123         [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
124         [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
125         [JAIL_ATTR_NETNS] = { "netns", BLOBMSG_TYPE_BOOL },
126         [JAIL_ATTR_USERNS] = { "userns", BLOBMSG_TYPE_BOOL },
127         [JAIL_ATTR_CGROUPSNS] = { "cgroupsns", BLOBMSG_TYPE_BOOL },
128         [JAIL_ATTR_REQUIREJAIL] = { "requirejail", BLOBMSG_TYPE_BOOL },
129 };
130
131 struct instance_netdev {
132         struct blobmsg_list_node node;
133         int ifindex;
134 };
135
136 struct instance_file {
137         struct blobmsg_list_node node;
138         uint32_t md5[4];
139 };
140
141 struct rlimit_name {
142         const char *name;
143         int resource;
144 };
145
146 static const struct rlimit_name rlimit_names[] = {
147         { "as", RLIMIT_AS },
148         { "core", RLIMIT_CORE },
149         { "cpu", RLIMIT_CPU },
150         { "data", RLIMIT_DATA },
151         { "fsize", RLIMIT_FSIZE },
152         { "memlock", RLIMIT_MEMLOCK },
153         { "nofile", RLIMIT_NOFILE },
154         { "nproc", RLIMIT_NPROC },
155         { "rss", RLIMIT_RSS },
156         { "stack", RLIMIT_STACK },
157 #ifdef linux
158         { "nice", RLIMIT_NICE },
159         { "rtprio", RLIMIT_RTPRIO },
160         { "msgqueue", RLIMIT_MSGQUEUE },
161         { "sigpending", RLIMIT_SIGPENDING },
162 #endif
163         { NULL, 0 }
164 };
165
166 static void closefd(int fd)
167 {
168         if (fd > STDERR_FILENO)
169                 close(fd);
170 }
171
172 /* convert a string into numeric syslog facility or return -1 if no match found */
173 static int
174 syslog_facility_str_to_int(const char *facility)
175 {
176         CODE *p = facilitynames;
177
178         while (p->c_name && strcasecmp(p->c_name, facility))
179                 p++;
180
181         return p->c_val;
182 }
183
184 static void
185 instance_limits(const char *limit, const char *value)
186 {
187         int i;
188         struct rlimit rlim;
189         unsigned long cur, max;
190
191         for (i = 0; rlimit_names[i].name != NULL; i++) {
192                 if (strcmp(rlimit_names[i].name, limit))
193                         continue;
194                 if (!strcmp(value, "unlimited")) {
195                         rlim.rlim_cur = RLIM_INFINITY;
196                         rlim.rlim_max = RLIM_INFINITY;
197                 } else {
198                         if (getrlimit(rlimit_names[i].resource, &rlim))
199                                 return;
200
201                         cur = rlim.rlim_cur;
202                         max = rlim.rlim_max;
203
204                         if (sscanf(value, "%lu %lu", &cur, &max) < 1)
205                                 return;
206
207                         rlim.rlim_cur = cur;
208                         rlim.rlim_max = max;
209                 }
210
211                 setrlimit(rlimit_names[i].resource, &rlim);
212                 return;
213         }
214 }
215
216 static inline int
217 jail_run(struct service_instance *in, char **argv)
218 {
219         struct blobmsg_list_node *var;
220         struct jail *jail = &in->jail;
221         int argc = 0;
222
223         argv[argc++] = UJAIL_BIN_PATH;
224
225         if (jail->name) {
226                 argv[argc++] = "-n";
227                 argv[argc++] = jail->name;
228         }
229
230         if (jail->hostname) {
231                 argv[argc++] = "-h";
232                 argv[argc++] = jail->hostname;
233         }
234
235         if (in->seccomp) {
236                 argv[argc++] = "-S";
237                 argv[argc++] = in->seccomp;
238         }
239
240         if (in->user) {
241                 argv[argc++] = "-U";
242                 argv[argc++] = in->user;
243         }
244
245         if (in->group) {
246                 argv[argc++] = "-G";
247                 argv[argc++] = in->group;
248         }
249
250         if (in->no_new_privs)
251                 argv[argc++] = "-c";
252
253         if (jail->procfs)
254                 argv[argc++] = "-p";
255
256         if (jail->sysfs)
257                 argv[argc++] = "-s";
258
259         if (jail->ubus)
260                 argv[argc++] = "-u";
261
262         if (jail->log)
263                 argv[argc++] = "-l";
264
265         if (jail->ronly)
266                 argv[argc++] = "-o";
267
268         if (jail->netns)
269                 argv[argc++] = "-N";
270
271         if (jail->userns)
272                 argv[argc++] = "-f";
273
274         if (jail->cgroupsns)
275                 argv[argc++] = "-F";
276
277         if (in->extroot) {
278                 argv[argc++] = "-R";
279                 argv[argc++] = in->extroot;
280         }
281
282         if (in->overlaydir) {
283                 argv[argc++] = "-O";
284                 argv[argc++] = in->overlaydir;
285         }
286
287         if (in->tmpoverlaysize) {
288                 argv[argc++] = "-T";
289                 argv[argc++] = in->tmpoverlaysize;
290         }
291
292         blobmsg_list_for_each(&jail->mount, var) {
293                 const char *type = blobmsg_data(var->data);
294
295                 if (*type == '1')
296                         argv[argc++] = "-w";
297                 else
298                         argv[argc++] = "-r";
299                 argv[argc++] = (char *) blobmsg_name(var->data);
300         }
301
302         argv[argc++] = "--";
303
304         return argc;
305 }
306
307 static int
308 instance_removepid(struct service_instance *in) {
309         if (!in->pidfile)
310                 return 0;
311         if (unlink(in->pidfile)) {
312                 ERROR("Failed to remove pidfile: %s: %m\n", in->pidfile);
313                 return 1;
314         }
315         return 0;
316 }
317
318 static int
319 instance_writepid(struct service_instance *in)
320 {
321         FILE *_pidfile;
322
323         if (!in->pidfile) {
324                 return 0;
325         }
326         _pidfile = fopen(in->pidfile, "w");
327         if (_pidfile == NULL) {
328                 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
329                 return 1;
330         }
331         if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
332                 ERROR("failed to write pidfile: %s: %m", in->pidfile);
333                 fclose(_pidfile);
334                 return 2;
335         }
336         if (fclose(_pidfile)) {
337                 ERROR("failed to close pidfile: %s: %m", in->pidfile);
338                 return 3;
339         }
340
341         return 0;
342 }
343
344 static void
345 instance_run(struct service_instance *in, int _stdout, int _stderr)
346 {
347         struct blobmsg_list_node *var;
348         struct blob_attr *cur;
349         char **argv;
350         int argc = 1; /* NULL terminated */
351         int rem, _stdin;
352         bool seccomp = !in->trace && !in->has_jail && in->seccomp;
353         bool setlbf = _stdout >= 0;
354
355         if (in->nice)
356                 setpriority(PRIO_PROCESS, 0, in->nice);
357
358         blobmsg_for_each_attr(cur, in->command, rem)
359                 argc++;
360
361         blobmsg_list_for_each(&in->env, var)
362                 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
363
364         if (seccomp)
365                 setenv("SECCOMP_FILE", in->seccomp, 1);
366
367         if (setlbf)
368                 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
369
370         blobmsg_list_for_each(&in->limits, var)
371                 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
372
373         if (in->trace || seccomp)
374                 argc += 1;
375
376         argv = alloca(sizeof(char *) * (argc + in->jail.argc));
377         argc = 0;
378
379 #ifdef SECCOMP_SUPPORT
380         if (in->trace)
381                 argv[argc++] = "/sbin/utrace";
382         else if (seccomp)
383                 argv[argc++] = "/sbin/seccomp-trace";
384 #else
385         if (in->trace || seccomp)
386                 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
387 #endif
388
389         if (in->has_jail) {
390                 argc = jail_run(in, argv);
391                 if (argc != in->jail.argc)
392                         ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
393                                 in->jail.argc, argc, in->srv->name, in->name);
394         }
395
396         blobmsg_for_each_attr(cur, in->command, rem)
397                 argv[argc++] = blobmsg_data(cur);
398
399         argv[argc] = NULL;
400
401         _stdin = open("/dev/null", O_RDONLY);
402
403         if (_stdout == -1)
404                 _stdout = open("/dev/null", O_WRONLY);
405
406         if (_stderr == -1)
407                 _stderr = open("/dev/null", O_WRONLY);
408
409         if (_stdin > -1) {
410                 dup2(_stdin, STDIN_FILENO);
411                 closefd(_stdin);
412         }
413         if (_stdout > -1) {
414                 dup2(_stdout, STDOUT_FILENO);
415                 closefd(_stdout);
416         }
417         if (_stderr > -1) {
418                 dup2(_stderr, STDERR_FILENO);
419                 closefd(_stderr);
420         }
421
422         if (!in->has_jail && in->user && in->pw_gid && initgroups(in->user, in->pw_gid)) {
423                 ERROR("failed to initgroups() for user %s: %m\n", in->user);
424                 exit(127);
425         }
426         if (!in->has_jail && in->gr_gid && setgid(in->gr_gid)) {
427                 ERROR("failed to set group id %d: %m\n", in->gr_gid);
428                 exit(127);
429         }
430         if (!in->has_jail && in->uid && setuid(in->uid)) {
431                 ERROR("failed to set user id %d: %m\n", in->uid);
432                 exit(127);
433         }
434
435         execvp(argv[0], argv);
436         exit(127);
437 }
438
439 static void
440 instance_free_stdio(struct service_instance *in)
441 {
442         if (in->_stdout.fd.fd > -1) {
443                 ustream_free(&in->_stdout.stream);
444                 close(in->_stdout.fd.fd);
445                 in->_stdout.fd.fd = -1;
446         }
447
448         if (in->_stderr.fd.fd > -1) {
449                 ustream_free(&in->_stderr.stream);
450                 close(in->_stderr.fd.fd);
451                 in->_stderr.fd.fd = -1;
452         }
453 }
454
455 void
456 instance_start(struct service_instance *in)
457 {
458         int pid;
459         int opipe[2] = { -1, -1 };
460         int epipe[2] = { -1, -1 };
461
462         if (!avl_is_empty(&in->errors.avl)) {
463                 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
464                 return;
465         }
466
467         if (!in->command) {
468                 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
469                 return;
470         }
471
472         if (in->proc.pending) {
473                 if (in->halt)
474                         in->restart = true;
475                 return;
476         }
477
478         instance_free_stdio(in);
479         if (in->_stdout.fd.fd > -2) {
480                 if (pipe(opipe)) {
481                         ULOG_WARN("pipe() failed: %m\n");
482                         opipe[0] = opipe[1] = -1;
483                 }
484         }
485
486         if (in->_stderr.fd.fd > -2) {
487                 if (pipe(epipe)) {
488                         ULOG_WARN("pipe() failed: %m\n");
489                         epipe[0] = epipe[1] = -1;
490                 }
491         }
492
493         in->restart = false;
494         in->halt = false;
495
496         if (!in->valid)
497                 return;
498
499         pid = fork();
500         if (pid < 0)
501                 return;
502
503         if (!pid) {
504                 uloop_done();
505                 closefd(opipe[0]);
506                 closefd(epipe[0]);
507                 instance_run(in, opipe[1], epipe[1]);
508                 return;
509         }
510
511         DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
512         in->proc.pid = pid;
513         instance_writepid(in);
514         clock_gettime(CLOCK_MONOTONIC, &in->start);
515         uloop_process_add(&in->proc);
516
517         if (opipe[0] > -1) {
518                 ustream_fd_init(&in->_stdout, opipe[0]);
519                 closefd(opipe[1]);
520                 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
521         }
522
523         if (epipe[0] > -1) {
524                 ustream_fd_init(&in->_stderr, epipe[0]);
525                 closefd(epipe[1]);
526                 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
527         }
528
529         service_event("instance.start", in->srv->name, in->name);
530 }
531
532 static void
533 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
534 {
535         char *newline, *str, *arg0, ident[32];
536         int len;
537
538         arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
539         snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
540         ulog_open(ULOG_SYSLOG, in->syslog_facility, ident);
541
542         do {
543                 str = ustream_get_read_buf(s, &len);
544                 if (!str)
545                         break;
546
547                 newline = memchr(str, '\n', len);
548                 if (!newline && (s->r.buffer_len != len))
549                         break;
550
551                 if (newline) {
552                         *newline = 0;
553                         len = newline + 1 - str;
554                 }
555                 ulog(prio, "%s\n", str);
556
557                 ustream_consume(s, len);
558         } while (1);
559
560         ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
561 }
562
563 static void
564 instance_stdout(struct ustream *s, int bytes)
565 {
566         instance_stdio(s, LOG_INFO,
567                        container_of(s, struct service_instance, _stdout.stream));
568 }
569
570 static void
571 instance_stderr(struct ustream *s, int bytes)
572 {
573         instance_stdio(s, LOG_ERR,
574                        container_of(s, struct service_instance, _stderr.stream));
575 }
576
577 static void
578 instance_timeout(struct uloop_timeout *t)
579 {
580         struct service_instance *in;
581
582         in = container_of(t, struct service_instance, timeout);
583
584         if (in->halt) {
585                 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
586                                 in->srv->name, in->name, in->proc.pid);
587                 kill(in->proc.pid, SIGKILL);
588         } else if (in->restart || in->respawn)
589                 instance_start(in);
590 }
591
592 static void
593 instance_delete(struct service_instance *in)
594 {
595         struct service *s = in->srv;
596
597         avl_delete(&s->instances.avl, &in->node.avl);
598         instance_free(in);
599         service_stopped(s);
600 }
601
602 static int
603 instance_exit_code(int ret)
604 {
605         if (WIFEXITED(ret)) {
606                 return WEXITSTATUS(ret);
607         }
608
609         if (WIFSIGNALED(ret)) {
610                 return SIGNALLED_OFFSET + WTERMSIG(ret);
611         }
612
613         if (WIFSTOPPED(ret)) {
614                 return WSTOPSIG(ret);
615         }
616
617         return 1;
618 }
619
620 static void
621 instance_exit(struct uloop_process *p, int ret)
622 {
623         struct service_instance *in;
624         struct timespec tp;
625         long runtime;
626
627         in = container_of(p, struct service_instance, proc);
628
629         clock_gettime(CLOCK_MONOTONIC, &tp);
630         runtime = tp.tv_sec - in->start.tv_sec;
631
632         DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
633
634         in->exit_code = instance_exit_code(ret);
635         uloop_timeout_cancel(&in->timeout);
636         service_event("instance.stop", in->srv->name, in->name);
637
638         if (in->halt) {
639                 instance_removepid(in);
640                 if (in->restart)
641                         instance_start(in);
642                 else
643                         instance_delete(in);
644         } else if (in->restart) {
645                 instance_start(in);
646         } else if (in->respawn) {
647                 if (runtime < in->respawn_threshold)
648                         in->respawn_count++;
649                 else
650                         in->respawn_count = 0;
651                 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
652                         LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
653                                                                 in->srv->name, in->name, in->respawn_count, runtime);
654                         in->restart = in->respawn = 0;
655                         in->halt = 1;
656                         service_event("instance.fail", in->srv->name, in->name);
657                 } else {
658                         service_event("instance.respawn", in->srv->name, in->name);
659                         uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
660                 }
661         }
662 }
663
664 void
665 instance_stop(struct service_instance *in, bool halt)
666 {
667         if (!in->proc.pending) {
668                 if (halt)
669                         instance_delete(in);
670                 return;
671         }
672         in->halt = halt;
673         in->restart = in->respawn = false;
674         kill(in->proc.pid, SIGTERM);
675         uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
676 }
677
678 static void
679 instance_restart(struct service_instance *in)
680 {
681         if (!in->proc.pending)
682                 return;
683
684         if (in->reload_signal) {
685                 kill(in->proc.pid, in->reload_signal);
686                 return;
687         }
688
689         in->halt = true;
690         in->restart = true;
691         kill(in->proc.pid, SIGTERM);
692         uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
693 }
694
695 static bool string_changed(const char *a, const char *b)
696 {
697         return !((!a && !b) || (a && b && !strcmp(a, b)));
698 }
699
700 static bool
701 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
702 {
703         if (!in->valid)
704                 return true;
705
706         if (!blob_attr_equal(in->command, in_new->command))
707                 return true;
708
709         if (!blobmsg_list_equal(&in->env, &in_new->env))
710                 return true;
711
712         if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
713                 return true;
714
715         if (!blobmsg_list_equal(&in->file, &in_new->file))
716                 return true;
717
718         if (in->nice != in_new->nice)
719                 return true;
720
721         if (in->syslog_facility != in_new->syslog_facility)
722                 return true;
723
724         if (string_changed(in->user, in_new->user))
725                 return true;
726
727         if (string_changed(in->group, in_new->group))
728                 return true;
729
730         if (in->uid != in_new->uid)
731                 return true;
732
733         if (in->pw_gid != in_new->pw_gid)
734                 return true;
735
736         if (string_changed(in->pidfile, in_new->pidfile))
737                 return true;
738
739         if (in->respawn_retry != in_new->respawn_retry)
740                 return true;
741         if (in->respawn_threshold != in_new->respawn_threshold)
742                 return true;
743         if (in->respawn_timeout != in_new->respawn_timeout)
744                 return true;
745
746         if ((!in->seccomp && in_new->seccomp) ||
747             (in->seccomp && !in_new->seccomp) ||
748             (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
749                 return true;
750
751         if (!blobmsg_list_equal(&in->limits, &in_new->limits))
752                 return true;
753
754         if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
755                 return true;
756
757         if (!blobmsg_list_equal(&in->errors, &in_new->errors))
758                 return true;
759
760         return false;
761 }
762
763 static bool
764 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
765 {
766         struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
767         struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
768
769         return n1->ifindex == n2->ifindex;
770 }
771
772 static void
773 instance_netdev_update(struct blobmsg_list_node *l)
774 {
775         struct instance_netdev *n = container_of(l, struct instance_netdev, node);
776
777         n->ifindex = if_nametoindex(n->node.avl.key);
778 }
779
780 static bool
781 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
782 {
783         struct instance_file *f1 = container_of(l1, struct instance_file, node);
784         struct instance_file *f2 = container_of(l2, struct instance_file, node);
785
786         return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
787 }
788
789 static void
790 instance_file_update(struct blobmsg_list_node *l)
791 {
792         struct instance_file *f = container_of(l, struct instance_file, node);
793         md5_ctx_t md5;
794         char buf[256];
795         int len, fd;
796
797         memset(f->md5, 0, sizeof(f->md5));
798
799         fd = open(l->avl.key, O_RDONLY);
800         if (fd < 0)
801                 return;
802
803         md5_begin(&md5);
804         do {
805                 len = read(fd, buf, sizeof(buf));
806                 if (len < 0) {
807                         if (errno == EINTR)
808                                 continue;
809
810                         break;
811                 }
812                 if (!len)
813                         break;
814
815                 md5_hash(buf, len, &md5);
816         } while(1);
817
818         md5_end(f->md5, &md5);
819         close(fd);
820 }
821
822 static void
823 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
824 {
825         if (!cur)
826                 return;
827
828         blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
829 }
830
831 static bool
832 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
833 {
834         struct blobmsg_list_node *node;
835
836         if (!cur)
837                 return true;
838
839         if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
840                 return false;
841
842         blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
843         if (cb) {
844                 blobmsg_list_for_each(l, node)
845                         cb(node);
846         }
847         return true;
848 }
849
850 static int
851 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
852 {
853         struct blob_attr *tb[__JAIL_ATTR_MAX];
854         struct jail *jail = &in->jail;
855
856         blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
857                 blobmsg_data(attr), blobmsg_data_len(attr));
858
859         jail->argc = 2;
860
861         if (tb[JAIL_ATTR_REQUIREJAIL]) {
862                 in->require_jail = true;
863                 jail->argc++;
864         }
865         if (tb[JAIL_ATTR_NAME]) {
866                 jail->name = strdup(blobmsg_get_string(tb[JAIL_ATTR_NAME]));
867                 jail->argc += 2;
868         }
869         if (tb[JAIL_ATTR_HOSTNAME]) {
870                 jail->hostname = strdup(blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]));
871                 jail->argc += 2;
872         }
873         if (tb[JAIL_ATTR_PROCFS]) {
874                 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
875                 jail->argc++;
876         }
877         if (tb[JAIL_ATTR_SYSFS]) {
878                 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
879                 jail->argc++;
880         }
881         if (tb[JAIL_ATTR_UBUS]) {
882                 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
883                 jail->argc++;
884         }
885         if (tb[JAIL_ATTR_LOG]) {
886                 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
887                 jail->argc++;
888         }
889         if (tb[JAIL_ATTR_RONLY]) {
890                 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
891                 jail->argc++;
892         }
893         if (tb[JAIL_ATTR_NETNS]) {
894                 jail->netns = blobmsg_get_bool(tb[JAIL_ATTR_NETNS]);
895                 jail->argc++;
896         }
897         if (tb[JAIL_ATTR_USERNS]) {
898                 jail->userns = blobmsg_get_bool(tb[JAIL_ATTR_USERNS]);
899                 jail->argc++;
900         }
901         if (tb[JAIL_ATTR_CGROUPSNS]) {
902                 jail->cgroupsns = blobmsg_get_bool(tb[JAIL_ATTR_CGROUPSNS]);
903                 jail->argc++;
904         }
905
906         if (tb[JAIL_ATTR_MOUNT]) {
907                 struct blob_attr *cur;
908                 int rem;
909
910                 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
911                         jail->argc += 2;
912                 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
913         }
914         if (in->seccomp)
915                 jail->argc += 2;
916
917         if (in->user)
918                 jail->argc += 2;
919
920         if (in->group)
921                 jail->argc += 2;
922
923         if (in->extroot)
924                 jail->argc += 2;
925
926         if (in->overlaydir)
927                 jail->argc += 2;
928
929         if (in->tmpoverlaysize)
930                 jail->argc += 2;
931
932         if (in->no_new_privs)
933                 jail->argc++;
934
935         return true;
936 }
937
938 static bool
939 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
940 {
941         struct blob_attr *cur, *cur2;
942         bool ret = false;
943         int rem;
944
945         cur = tb[INSTANCE_ATTR_COMMAND];
946         if (!cur) {
947                 in->command = NULL;
948                 return true;
949         }
950
951         if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
952                 return false;
953
954         blobmsg_for_each_attr(cur2, cur, rem) {
955                 ret = true;
956                 break;
957         }
958
959         in->command = cur;
960         return ret;
961 }
962
963 static bool
964 instance_config_parse(struct service_instance *in)
965 {
966         struct blob_attr *tb[__INSTANCE_ATTR_MAX];
967         struct blob_attr *cur, *cur2;
968         struct stat s;
969         int rem, r;
970
971         blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
972                 blobmsg_data(in->config), blobmsg_data_len(in->config));
973
974         if (!instance_config_parse_command(in, tb))
975                 return false;
976
977         if (tb[INSTANCE_ATTR_TERMTIMEOUT])
978                 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
979         if (tb[INSTANCE_ATTR_RESPAWN]) {
980                 int i = 0;
981                 uint32_t vals[3] = { 3600, 5, 5};
982
983                 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
984                         if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
985                                 continue;
986                         vals[i] = atoi(blobmsg_get_string(cur2));
987                         i++;
988                 }
989                 in->respawn = true;
990                 in->respawn_count = 0;
991                 in->respawn_threshold = vals[0];
992                 in->respawn_timeout = vals[1];
993                 in->respawn_retry = vals[2];
994         }
995         if (tb[INSTANCE_ATTR_TRIGGER]) {
996                 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
997                 trigger_add(in->trigger, in);
998         }
999
1000         if (tb[INSTANCE_ATTR_WATCH]) {
1001                 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
1002                         if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
1003                                 continue;
1004                         DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
1005                         watch_add(blobmsg_get_string(cur2), in);
1006                 }
1007         }
1008
1009         if ((cur = tb[INSTANCE_ATTR_NICE])) {
1010                 in->nice = (int8_t) blobmsg_get_u32(cur);
1011                 if (in->nice < -20 || in->nice > 20)
1012                         return false;
1013         }
1014
1015         if (tb[INSTANCE_ATTR_USER]) {
1016                 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
1017                 struct passwd *p = getpwnam(user);
1018                 if (p) {
1019                         in->user = strdup(user);
1020                         in->uid = p->pw_uid;
1021                         in->gr_gid = in->pw_gid = p->pw_gid;
1022                 }
1023         }
1024
1025         if (tb[INSTANCE_ATTR_GROUP]) {
1026                 const char *group = blobmsg_get_string(tb[INSTANCE_ATTR_GROUP]);
1027                 struct group *p = getgrnam(group);
1028                 if (p) {
1029                         in->group = strdup(group);
1030                         in->gr_gid = p->gr_gid;
1031                 }
1032         }
1033
1034         if (tb[INSTANCE_ATTR_TRACE])
1035                 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
1036
1037         if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
1038                 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
1039
1040         if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
1041                 in->seccomp = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]));
1042
1043         if (tb[INSTANCE_ATTR_EXTROOT])
1044                 in->extroot = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_EXTROOT]));
1045
1046         if (tb[INSTANCE_ATTR_OVERLAYDIR])
1047                 in->overlaydir = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_OVERLAYDIR]));
1048
1049         if (tb[INSTANCE_ATTR_TMPOVERLAYSIZE])
1050                 in->tmpoverlaysize = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_TMPOVERLAYSIZE]));
1051
1052         if (tb[INSTANCE_ATTR_PIDFILE]) {
1053                 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
1054                 if (pidfile)
1055                         in->pidfile = strdup(pidfile);
1056         }
1057
1058         if (tb[INSTANCE_ATTR_RELOADSIG])
1059                 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
1060
1061         if (!in->trace && tb[INSTANCE_ATTR_JAIL])
1062                 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
1063
1064         if (in->has_jail) {
1065                 r = stat(UJAIL_BIN_PATH, &s);
1066                 if (r < 0) {
1067                         if (in->require_jail) {
1068                                 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1069                                                 in->srv->name, in->name, UJAIL_BIN_PATH, r);
1070                                 return false;
1071                         }
1072                         DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH, r);
1073                         in->has_jail = false;
1074                 }
1075         }
1076
1077         if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
1078                 in->_stdout.fd.fd = -1;
1079
1080         if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
1081                 in->_stderr.fd.fd = -1;
1082
1083         instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
1084
1085         if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
1086                 return false;
1087
1088         if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
1089                 return false;
1090
1091         if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
1092                 return false;
1093
1094         if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
1095                 return false;
1096
1097         if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
1098                 return false;
1099
1100         if (tb[INSTANCE_ATTR_FACILITY]) {
1101                 int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1102                 if (facility != -1) {
1103                         in->syslog_facility = facility;
1104                         DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1105                 } else
1106                         DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1107         }
1108
1109         return true;
1110 }
1111
1112 static void
1113 instance_config_cleanup(struct service_instance *in)
1114 {
1115         blobmsg_list_free(&in->env);
1116         blobmsg_list_free(&in->data);
1117         blobmsg_list_free(&in->netdev);
1118         blobmsg_list_free(&in->file);
1119         blobmsg_list_free(&in->limits);
1120         blobmsg_list_free(&in->errors);
1121         blobmsg_list_free(&in->jail.mount);
1122 }
1123
1124 static void
1125 instance_config_move_strdup(char **dst, char *src)
1126 {
1127         if (*dst) {
1128                 free(*dst);
1129                 *dst = NULL;
1130         }
1131
1132         if (!src)
1133                 return;
1134
1135         *dst = strdup(src);
1136 }
1137
1138 static void
1139 instance_config_move(struct service_instance *in, struct service_instance *in_src)
1140 {
1141         instance_config_cleanup(in);
1142         blobmsg_list_move(&in->env, &in_src->env);
1143         blobmsg_list_move(&in->data, &in_src->data);
1144         blobmsg_list_move(&in->netdev, &in_src->netdev);
1145         blobmsg_list_move(&in->file, &in_src->file);
1146         blobmsg_list_move(&in->limits, &in_src->limits);
1147         blobmsg_list_move(&in->errors, &in_src->errors);
1148         blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
1149         in->trigger = in_src->trigger;
1150         in->command = in_src->command;
1151         in->respawn = in_src->respawn;
1152         in->respawn_retry = in_src->respawn_retry;
1153         in->respawn_threshold = in_src->respawn_threshold;
1154         in->respawn_timeout = in_src->respawn_timeout;
1155         in->name = in_src->name;
1156         in->trace = in_src->trace;
1157         in->node.avl.key = in_src->node.avl.key;
1158         in->syslog_facility = in_src->syslog_facility;
1159
1160         instance_config_move_strdup(&in->pidfile, in_src->pidfile);
1161         instance_config_move_strdup(&in->seccomp, in_src->seccomp);
1162         instance_config_move_strdup(&in->jail.name, in_src->jail.name);
1163         instance_config_move_strdup(&in->jail.hostname, in_src->jail.hostname);
1164
1165         free(in->config);
1166         in->config = in_src->config;
1167         in_src->config = NULL;
1168 }
1169
1170 void
1171 instance_update(struct service_instance *in, struct service_instance *in_new)
1172 {
1173         bool changed = instance_config_changed(in, in_new);
1174         bool running = in->proc.pending;
1175         bool stopping = in->halt;
1176
1177         if (!running || stopping) {
1178                 instance_config_move(in, in_new);
1179                 instance_start(in);
1180         } else {
1181                 if (changed)
1182                         instance_restart(in);
1183                 instance_config_move(in, in_new);
1184                 /* restart happens in the child callback handler */
1185         }
1186 }
1187
1188 void
1189 instance_free(struct service_instance *in)
1190 {
1191         instance_free_stdio(in);
1192         uloop_process_delete(&in->proc);
1193         uloop_timeout_cancel(&in->timeout);
1194         trigger_del(in);
1195         watch_del(in);
1196         instance_config_cleanup(in);
1197         free(in->config);
1198         free(in->user);
1199         free(in->group);
1200         free(in->extroot);
1201         free(in->overlaydir);
1202         free(in->tmpoverlaysize);
1203         free(in->jail.name);
1204         free(in->jail.hostname);
1205         free(in->seccomp);
1206         free(in->pidfile);
1207         free(in);
1208 }
1209
1210 void
1211 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1212 {
1213         config = blob_memdup(config);
1214         in->srv = s;
1215         in->name = blobmsg_name(config);
1216         in->config = config;
1217         in->timeout.cb = instance_timeout;
1218         in->proc.cb = instance_exit;
1219         in->term_timeout = 5;
1220         in->syslog_facility = LOG_DAEMON;
1221         in->exit_code = 0;
1222         in->require_jail = false;
1223
1224         in->_stdout.fd.fd = -2;
1225         in->_stdout.stream.string_data = true;
1226         in->_stdout.stream.notify_read = instance_stdout;
1227
1228         in->_stderr.fd.fd = -2;
1229         in->_stderr.stream.string_data = true;
1230         in->_stderr.stream.notify_read = instance_stderr;
1231
1232         blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1233         blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1234         blobmsg_list_simple_init(&in->env);
1235         blobmsg_list_simple_init(&in->data);
1236         blobmsg_list_simple_init(&in->limits);
1237         blobmsg_list_simple_init(&in->errors);
1238         blobmsg_list_simple_init(&in->jail.mount);
1239         in->valid = instance_config_parse(in);
1240 }
1241
1242 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1243 {
1244         void *i;
1245
1246         if (!in->valid)
1247                 return;
1248
1249         i = blobmsg_open_table(b, in->name);
1250         blobmsg_add_u8(b, "running", in->proc.pending);
1251         if (in->proc.pending)
1252                 blobmsg_add_u32(b, "pid", in->proc.pid);
1253         if (in->command)
1254                 blobmsg_add_blob(b, in->command);
1255         blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1256         if (!in->proc.pending)
1257                 blobmsg_add_u32(b, "exit_code", in->exit_code);
1258
1259         if (!avl_is_empty(&in->errors.avl)) {
1260                 struct blobmsg_list_node *var;
1261                 void *e = blobmsg_open_array(b, "errors");
1262                 blobmsg_list_for_each(&in->errors, var)
1263                         blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1264                 blobmsg_close_table(b, e);
1265         }
1266
1267         if (!avl_is_empty(&in->env.avl)) {
1268                 struct blobmsg_list_node *var;
1269                 void *e = blobmsg_open_table(b, "env");
1270                 blobmsg_list_for_each(&in->env, var)
1271                         blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1272                 blobmsg_close_table(b, e);
1273         }
1274
1275         if (!avl_is_empty(&in->data.avl)) {
1276                 struct blobmsg_list_node *var;
1277                 void *e = blobmsg_open_table(b, "data");
1278                 blobmsg_list_for_each(&in->data, var)
1279                         blobmsg_add_blob(b, var->data);
1280                 blobmsg_close_table(b, e);
1281         }
1282
1283         if (!avl_is_empty(&in->limits.avl)) {
1284                 struct blobmsg_list_node *var;
1285                 void *e = blobmsg_open_table(b, "limits");
1286                 blobmsg_list_for_each(&in->limits, var)
1287                         blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1288                 blobmsg_close_table(b, e);
1289         }
1290
1291         if (in->reload_signal)
1292                 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1293
1294         if (in->respawn) {
1295                 void *r = blobmsg_open_table(b, "respawn");
1296                 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1297                 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1298                 blobmsg_add_u32(b, "retry", in->respawn_retry);
1299                 blobmsg_close_table(b, r);
1300         }
1301
1302         if (in->trace)
1303                 blobmsg_add_u8(b, "trace", true);
1304
1305         if (in->no_new_privs)
1306                 blobmsg_add_u8(b, "no_new_privs", true);
1307
1308         if (in->seccomp)
1309                 blobmsg_add_string(b, "seccomp", in->seccomp);
1310
1311         if (in->pidfile)
1312                 blobmsg_add_string(b, "pidfile", in->pidfile);
1313
1314         if (in->user)
1315                 blobmsg_add_string(b, "user", in->user);
1316
1317         if (in->group)
1318                 blobmsg_add_string(b, "group", in->group);
1319
1320         if (in->has_jail) {
1321                 void *r = blobmsg_open_table(b, "jail");
1322                 if (in->jail.name)
1323                         blobmsg_add_string(b, "name", in->jail.name);
1324                 if (in->jail.hostname)
1325                         blobmsg_add_string(b, "hostname", in->jail.hostname);
1326
1327                 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1328                 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1329                 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1330                 blobmsg_add_u8(b, "log", in->jail.log);
1331                 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1332                 blobmsg_add_u8(b, "netns", in->jail.netns);
1333                 blobmsg_add_u8(b, "userns", in->jail.userns);
1334                 blobmsg_add_u8(b, "cgroupsns", in->jail.cgroupsns);
1335                 blobmsg_close_table(b, r);
1336                 if (!avl_is_empty(&in->jail.mount.avl)) {
1337                         struct blobmsg_list_node *var;
1338                         void *e = blobmsg_open_table(b, "mount");
1339                         blobmsg_list_for_each(&in->jail.mount, var)
1340                                 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1341                         blobmsg_close_table(b, e);
1342                 }
1343         }
1344
1345         if (in->extroot)
1346                 blobmsg_add_string(b, "extroot", in->extroot);
1347         if (in->overlaydir)
1348                 blobmsg_add_string(b, "overlaydir", in->overlaydir);
1349         if (in->tmpoverlaysize)
1350                 blobmsg_add_string(b, "tmpoverlaysize", in->tmpoverlaysize);
1351
1352         if (verbose && in->trigger)
1353                 blobmsg_add_blob(b, in->trigger);
1354
1355         blobmsg_close_table(b, i);
1356 }