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