2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
16 #include <sys/resource.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
32 #include <libubox/md5.h>
39 #define UJAIL_BIN_PATH "/sbin/ujail"
40 #define CGROUP_BASEDIR "/sys/fs/cgroup/services"
43 INSTANCE_ATTR_COMMAND
,
48 INSTANCE_ATTR_TRIGGER
,
49 INSTANCE_ATTR_RESPAWN
,
58 INSTANCE_ATTR_NO_NEW_PRIVS
,
61 INSTANCE_ATTR_SECCOMP
,
62 INSTANCE_ATTR_PIDFILE
,
63 INSTANCE_ATTR_RELOADSIG
,
64 INSTANCE_ATTR_TERMTIMEOUT
,
65 INSTANCE_ATTR_FACILITY
,
66 INSTANCE_ATTR_EXTROOT
,
67 INSTANCE_ATTR_OVERLAYDIR
,
68 INSTANCE_ATTR_TMPOVERLAYSIZE
,
70 INSTANCE_ATTR_WATCHDOG
,
74 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
75 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
76 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
77 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
78 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
79 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
80 [INSTANCE_ATTR_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
81 [INSTANCE_ATTR_RESPAWN
] = { "respawn", BLOBMSG_TYPE_ARRAY
},
82 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
83 [INSTANCE_ATTR_LIMITS
] = { "limits", BLOBMSG_TYPE_TABLE
},
84 [INSTANCE_ATTR_WATCH
] = { "watch", BLOBMSG_TYPE_ARRAY
},
85 [INSTANCE_ATTR_ERROR
] = { "error", BLOBMSG_TYPE_ARRAY
},
86 [INSTANCE_ATTR_USER
] = { "user", BLOBMSG_TYPE_STRING
},
87 [INSTANCE_ATTR_GROUP
] = { "group", BLOBMSG_TYPE_STRING
},
88 [INSTANCE_ATTR_STDOUT
] = { "stdout", BLOBMSG_TYPE_BOOL
},
89 [INSTANCE_ATTR_STDERR
] = { "stderr", BLOBMSG_TYPE_BOOL
},
90 [INSTANCE_ATTR_NO_NEW_PRIVS
] = { "no_new_privs", BLOBMSG_TYPE_BOOL
},
91 [INSTANCE_ATTR_JAIL
] = { "jail", BLOBMSG_TYPE_TABLE
},
92 [INSTANCE_ATTR_TRACE
] = { "trace", BLOBMSG_TYPE_BOOL
},
93 [INSTANCE_ATTR_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_STRING
},
94 [INSTANCE_ATTR_PIDFILE
] = { "pidfile", BLOBMSG_TYPE_STRING
},
95 [INSTANCE_ATTR_RELOADSIG
] = { "reload_signal", BLOBMSG_TYPE_INT32
},
96 [INSTANCE_ATTR_TERMTIMEOUT
] = { "term_timeout", BLOBMSG_TYPE_INT32
},
97 [INSTANCE_ATTR_FACILITY
] = { "facility", BLOBMSG_TYPE_STRING
},
98 [INSTANCE_ATTR_EXTROOT
] = { "extroot", BLOBMSG_TYPE_STRING
},
99 [INSTANCE_ATTR_OVERLAYDIR
] = { "overlaydir", BLOBMSG_TYPE_STRING
},
100 [INSTANCE_ATTR_TMPOVERLAYSIZE
] = { "tmpoverlaysize", BLOBMSG_TYPE_STRING
},
101 [INSTANCE_ATTR_BUNDLE
] = { "bundle", BLOBMSG_TYPE_STRING
},
102 [INSTANCE_ATTR_WATCHDOG
] = { "watchdog", BLOBMSG_TYPE_ARRAY
},
118 JAIL_ATTR_REQUIREJAIL
,
119 JAIL_ATTR_IMMEDIATELY
,
123 static const struct blobmsg_policy jail_attr
[__JAIL_ATTR_MAX
] = {
124 [JAIL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
125 [JAIL_ATTR_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
126 [JAIL_ATTR_PROCFS
] = { "procfs", BLOBMSG_TYPE_BOOL
},
127 [JAIL_ATTR_SYSFS
] = { "sysfs", BLOBMSG_TYPE_BOOL
},
128 [JAIL_ATTR_UBUS
] = { "ubus", BLOBMSG_TYPE_BOOL
},
129 [JAIL_ATTR_LOG
] = { "log", BLOBMSG_TYPE_BOOL
},
130 [JAIL_ATTR_RONLY
] = { "ronly", BLOBMSG_TYPE_BOOL
},
131 [JAIL_ATTR_MOUNT
] = { "mount", BLOBMSG_TYPE_TABLE
},
132 [JAIL_ATTR_NETNS
] = { "netns", BLOBMSG_TYPE_BOOL
},
133 [JAIL_ATTR_USERNS
] = { "userns", BLOBMSG_TYPE_BOOL
},
134 [JAIL_ATTR_CGROUPSNS
] = { "cgroupsns", BLOBMSG_TYPE_BOOL
},
135 [JAIL_ATTR_CONSOLE
] = { "console", BLOBMSG_TYPE_BOOL
},
136 [JAIL_ATTR_REQUIREJAIL
] = { "requirejail", BLOBMSG_TYPE_BOOL
},
137 [JAIL_ATTR_IMMEDIATELY
] = { "immediately", BLOBMSG_TYPE_BOOL
},
140 struct instance_netdev
{
141 struct blobmsg_list_node node
;
145 struct instance_file
{
146 struct blobmsg_list_node node
;
155 static const struct rlimit_name rlimit_names
[] = {
157 { "core", RLIMIT_CORE
},
158 { "cpu", RLIMIT_CPU
},
159 { "data", RLIMIT_DATA
},
160 { "fsize", RLIMIT_FSIZE
},
161 { "memlock", RLIMIT_MEMLOCK
},
162 { "nofile", RLIMIT_NOFILE
},
163 { "nproc", RLIMIT_NPROC
},
164 { "rss", RLIMIT_RSS
},
165 { "stack", RLIMIT_STACK
},
167 { "nice", RLIMIT_NICE
},
168 { "rtprio", RLIMIT_RTPRIO
},
169 { "msgqueue", RLIMIT_MSGQUEUE
},
170 { "sigpending", RLIMIT_SIGPENDING
},
175 static void closefd(int fd
)
177 if (fd
> STDERR_FILENO
)
181 /* convert a string into numeric syslog facility or return -1 if no match found */
183 syslog_facility_str_to_int(const char *facility
)
185 CODE
*p
= facilitynames
;
187 while (p
->c_name
&& strcasecmp(p
->c_name
, facility
))
194 instance_limits(const char *limit
, const char *value
)
198 unsigned long cur
, max
;
200 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
201 if (strcmp(rlimit_names
[i
].name
, limit
))
203 if (!strcmp(value
, "unlimited")) {
204 rlim
.rlim_cur
= RLIM_INFINITY
;
205 rlim
.rlim_max
= RLIM_INFINITY
;
207 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
213 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
220 setrlimit(rlimit_names
[i
].resource
, &rlim
);
226 jail_run(struct service_instance
*in
, char **argv
)
228 struct blobmsg_list_node
*var
;
229 struct jail
*jail
= &in
->jail
;
232 argv
[argc
++] = UJAIL_BIN_PATH
;
236 argv
[argc
++] = jail
->name
;
239 if (jail
->hostname
) {
241 argv
[argc
++] = jail
->hostname
;
246 argv
[argc
++] = in
->seccomp
;
251 argv
[argc
++] = in
->user
;
256 argv
[argc
++] = in
->group
;
259 if (in
->no_new_privs
)
291 argv
[argc
++] = in
->extroot
;
294 if (in
->overlaydir
) {
296 argv
[argc
++] = in
->overlaydir
;
299 if (in
->tmpoverlaysize
) {
301 argv
[argc
++] = in
->tmpoverlaysize
;
309 argv
[argc
++] = in
->bundle
;
312 if (in
->require_jail
)
315 blobmsg_list_for_each(&jail
->mount
, var
) {
316 const char *type
= blobmsg_data(var
->data
);
322 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
331 instance_removepid(struct service_instance
*in
) {
334 if (unlink(in
->pidfile
)) {
335 ERROR("Failed to remove pidfile: %s: %m\n", in
->pidfile
);
342 instance_writepid(struct service_instance
*in
)
349 _pidfile
= fopen(in
->pidfile
, "w");
350 if (_pidfile
== NULL
) {
351 ERROR("failed to open pidfile for writing: %s: %m", in
->pidfile
);
354 if (fprintf(_pidfile
, "%d\n", in
->proc
.pid
) < 0) {
355 ERROR("failed to write pidfile: %s: %m", in
->pidfile
);
359 if (fclose(_pidfile
)) {
360 ERROR("failed to close pidfile: %s: %m", in
->pidfile
);
368 instance_run(struct service_instance
*in
, int _stdout
, int _stderr
)
370 struct blobmsg_list_node
*var
;
371 struct blob_attr
*cur
;
373 int argc
= 1; /* NULL terminated */
375 bool seccomp
= !in
->trace
&& !in
->has_jail
&& in
->seccomp
;
376 bool setlbf
= _stdout
>= 0;
379 setpriority(PRIO_PROCESS
, 0, in
->nice
);
381 blobmsg_for_each_attr(cur
, in
->command
, rem
)
384 blobmsg_list_for_each(&in
->env
, var
)
385 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
388 setenv("SECCOMP_FILE", in
->seccomp
, 1);
391 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
393 blobmsg_list_for_each(&in
->limits
, var
)
394 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
396 if (in
->trace
|| seccomp
)
399 argv
= alloca(sizeof(char *) * (argc
+ in
->jail
.argc
));
402 #ifdef SECCOMP_SUPPORT
404 argv
[argc
++] = "/sbin/utrace";
406 argv
[argc
++] = "/sbin/seccomp-trace";
408 if (in
->trace
|| seccomp
)
409 ULOG_WARN("Seccomp support for %s::%s not available\n", in
->srv
->name
, in
->name
);
413 argc
= jail_run(in
, argv
);
414 if (argc
!= in
->jail
.argc
)
415 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
416 in
->jail
.argc
, argc
, in
->srv
->name
, in
->name
);
419 blobmsg_for_each_attr(cur
, in
->command
, rem
)
420 argv
[argc
++] = blobmsg_data(cur
);
424 _stdin
= open("/dev/null", O_RDONLY
);
427 _stdout
= open("/dev/null", O_WRONLY
);
430 _stderr
= open("/dev/null", O_WRONLY
);
433 dup2(_stdin
, STDIN_FILENO
);
437 dup2(_stdout
, STDOUT_FILENO
);
441 dup2(_stderr
, STDERR_FILENO
);
445 if (!in
->has_jail
&& in
->user
&& in
->pw_gid
&& initgroups(in
->user
, in
->pw_gid
)) {
446 ERROR("failed to initgroups() for user %s: %m\n", in
->user
);
449 if (!in
->has_jail
&& in
->gr_gid
&& setgid(in
->gr_gid
)) {
450 ERROR("failed to set group id %d: %m\n", in
->gr_gid
);
453 if (!in
->has_jail
&& in
->uid
&& setuid(in
->uid
)) {
454 ERROR("failed to set user id %d: %m\n", in
->uid
);
458 execvp(argv
[0], argv
);
463 instance_add_cgroup(const char *service
, const char *instance
)
469 if (stat("/sys/fs/cgroup/cgroup.subtree_control", &sb
))
472 mkdir(CGROUP_BASEDIR
, 0700);
474 snprintf(cgnamebuf
, sizeof(cgnamebuf
), "%s/%s", CGROUP_BASEDIR
, service
);
475 mkdir(cgnamebuf
, 0700);
476 snprintf(cgnamebuf
, sizeof(cgnamebuf
), "%s/%s/%s", CGROUP_BASEDIR
, service
, instance
);
477 mkdir(cgnamebuf
, 0700);
478 strcat(cgnamebuf
, "/cgroup.procs");
480 fd
= open(cgnamebuf
, O_WRONLY
);
484 dprintf(fd
, "%d", getpid());
489 instance_free_stdio(struct service_instance
*in
)
491 if (in
->_stdout
.fd
.fd
> -1) {
492 ustream_free(&in
->_stdout
.stream
);
493 close(in
->_stdout
.fd
.fd
);
494 in
->_stdout
.fd
.fd
= -1;
497 if (in
->_stderr
.fd
.fd
> -1) {
498 ustream_free(&in
->_stderr
.stream
);
499 close(in
->_stderr
.fd
.fd
);
500 in
->_stderr
.fd
.fd
= -1;
503 if (in
->console
.fd
.fd
> -1) {
504 ustream_free(&in
->console
.stream
);
505 close(in
->console
.fd
.fd
);
506 in
->console
.fd
.fd
= -1;
509 if (in
->console_client
.fd
.fd
> -1) {
510 ustream_free(&in
->console_client
.stream
);
511 close(in
->console_client
.fd
.fd
);
512 in
->console_client
.fd
.fd
= -1;
517 instance_start(struct service_instance
*in
)
520 int opipe
[2] = { -1, -1 };
521 int epipe
[2] = { -1, -1 };
523 if (!avl_is_empty(&in
->errors
.avl
)) {
524 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
528 if (!in
->bundle
&& !in
->command
) {
529 LOG("Not starting instance %s::%s, command not set\n", in
->srv
->name
, in
->name
);
533 if (in
->proc
.pending
) {
539 instance_free_stdio(in
);
540 if (in
->_stdout
.fd
.fd
> -2) {
542 ULOG_WARN("pipe() failed: %m\n");
543 opipe
[0] = opipe
[1] = -1;
547 if (in
->_stderr
.fd
.fd
> -2) {
549 ULOG_WARN("pipe() failed: %m\n");
550 epipe
[0] = epipe
[1] = -1;
568 instance_add_cgroup(in
->srv
->name
, in
->name
);
569 instance_run(in
, opipe
[1], epipe
[1]);
573 DEBUG(2, "Started instance %s::%s[%d]\n", in
->srv
->name
, in
->name
, pid
);
575 instance_writepid(in
);
576 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
577 uloop_process_add(&in
->proc
);
580 ustream_fd_init(&in
->_stdout
, opipe
[0]);
582 fcntl(opipe
[0], F_SETFD
, FD_CLOEXEC
);
586 ustream_fd_init(&in
->_stderr
, epipe
[0]);
588 fcntl(epipe
[0], F_SETFD
, FD_CLOEXEC
);
591 if (in
->watchdog
.mode
!= INSTANCE_WATCHDOG_MODE_DISABLED
) {
592 uloop_timeout_set(&in
->watchdog
.timeout
, in
->watchdog
.freq
* 1000);
593 DEBUG(2, "Started instance %s::%s watchdog timer : timeout = %d\n", in
->srv
->name
, in
->name
, in
->watchdog
.freq
);
596 service_event("instance.start", in
->srv
->name
, in
->name
);
600 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
602 char *newline
, *str
, *arg0
, ident
[32];
605 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
606 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
607 ulog_open(ULOG_SYSLOG
, in
->syslog_facility
, ident
);
610 str
= ustream_get_read_buf(s
, &len
);
614 newline
= memchr(str
, '\n', len
);
615 if (!newline
&& (s
->r
.buffer_len
!= len
))
620 len
= newline
+ 1 - str
;
622 ulog(prio
, "%s\n", str
);
624 ustream_consume(s
, len
);
627 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
631 instance_stdout(struct ustream
*s
, int bytes
)
633 instance_stdio(s
, LOG_INFO
,
634 container_of(s
, struct service_instance
, _stdout
.stream
));
638 instance_console(struct ustream
*s
, int bytes
)
640 struct service_instance
*in
= container_of(s
, struct service_instance
, console
.stream
);
645 buf
= ustream_get_read_buf(s
, &len
);
649 ulog(LOG_INFO
, "out: %s\n", buf
);
651 /* test if console client is attached */
652 if (in
->console_client
.fd
.fd
> -1)
653 ustream_write(&in
->console_client
.stream
, buf
, len
, false);
655 ustream_consume(s
, len
);
660 instance_console_client(struct ustream
*s
, int bytes
)
662 struct service_instance
*in
= container_of(s
, struct service_instance
, console_client
.stream
);
667 buf
= ustream_get_read_buf(s
, &len
);
671 ulog(LOG_INFO
, "in: %s\n", buf
);
672 ustream_write(&in
->console
.stream
, buf
, len
, false);
673 ustream_consume(s
, len
);
678 instance_stderr(struct ustream
*s
, int bytes
)
680 instance_stdio(s
, LOG_ERR
,
681 container_of(s
, struct service_instance
, _stderr
.stream
));
685 instance_timeout(struct uloop_timeout
*t
)
687 struct service_instance
*in
;
689 in
= container_of(t
, struct service_instance
, timeout
);
692 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
693 in
->srv
->name
, in
->name
, in
->proc
.pid
);
694 kill(in
->proc
.pid
, SIGKILL
);
695 } else if (in
->restart
|| in
->respawn
)
700 instance_delete(struct service_instance
*in
)
702 struct service
*s
= in
->srv
;
704 avl_delete(&s
->instances
.avl
, &in
->node
.avl
);
710 instance_exit_code(int ret
)
712 if (WIFEXITED(ret
)) {
713 return WEXITSTATUS(ret
);
716 if (WIFSIGNALED(ret
)) {
717 return SIGNALLED_OFFSET
+ WTERMSIG(ret
);
720 if (WIFSTOPPED(ret
)) {
721 return WSTOPSIG(ret
);
728 instance_exit(struct uloop_process
*p
, int ret
)
730 struct service_instance
*in
;
734 in
= container_of(p
, struct service_instance
, proc
);
736 clock_gettime(CLOCK_MONOTONIC
, &tp
);
737 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
739 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
741 in
->exit_code
= instance_exit_code(ret
);
742 uloop_timeout_cancel(&in
->timeout
);
743 uloop_timeout_cancel(&in
->watchdog
.timeout
);
744 service_event("instance.stop", in
->srv
->name
, in
->name
);
747 instance_removepid(in
);
752 } else if (in
->restart
) {
754 } else if (in
->respawn
) {
755 if (runtime
< in
->respawn_threshold
)
758 in
->respawn_count
= 0;
759 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
760 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
761 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
762 in
->restart
= in
->respawn
= 0;
764 service_event("instance.fail", in
->srv
->name
, in
->name
);
766 service_event("instance.respawn", in
->srv
->name
, in
->name
);
767 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
773 instance_stop(struct service_instance
*in
, bool halt
)
775 if (!in
->proc
.pending
) {
781 in
->restart
= in
->respawn
= false;
782 kill(in
->proc
.pid
, SIGTERM
);
783 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
787 instance_restart(struct service_instance
*in
)
789 if (!in
->proc
.pending
)
792 if (in
->reload_signal
) {
793 kill(in
->proc
.pid
, in
->reload_signal
);
799 kill(in
->proc
.pid
, SIGTERM
);
800 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
804 instance_watchdog(struct uloop_timeout
*t
)
806 struct service_instance
*in
= container_of(t
, struct service_instance
, watchdog
.timeout
);
808 DEBUG(3, "instance %s::%s watchdog timer expired\n", in
->srv
->name
, in
->name
);
811 instance_restart(in
);
813 instance_stop(in
, true);
816 static bool string_changed(const char *a
, const char *b
)
818 return !((!a
&& !b
) || (a
&& b
&& !strcmp(a
, b
)));
822 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
827 if (!blob_attr_equal(in
->command
, in_new
->command
))
830 if (string_changed(in
->bundle
, in_new
->bundle
))
833 if (string_changed(in
->extroot
, in_new
->extroot
))
836 if (string_changed(in
->overlaydir
, in_new
->overlaydir
))
839 if (string_changed(in
->tmpoverlaysize
, in_new
->tmpoverlaysize
))
842 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
845 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
848 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
851 if (in
->nice
!= in_new
->nice
)
854 if (in
->syslog_facility
!= in_new
->syslog_facility
)
857 if (string_changed(in
->user
, in_new
->user
))
860 if (string_changed(in
->group
, in_new
->group
))
863 if (in
->uid
!= in_new
->uid
)
866 if (in
->pw_gid
!= in_new
->pw_gid
)
869 if (in
->gr_gid
!= in_new
->gr_gid
)
872 if (string_changed(in
->pidfile
, in_new
->pidfile
))
875 if (in
->respawn_retry
!= in_new
->respawn_retry
)
877 if (in
->respawn_threshold
!= in_new
->respawn_threshold
)
879 if (in
->respawn_timeout
!= in_new
->respawn_timeout
)
882 if (in
->reload_signal
!= in_new
->reload_signal
)
885 if (in
->term_timeout
!= in_new
->term_timeout
)
888 if (string_changed(in
->seccomp
, in_new
->seccomp
))
891 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
894 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
897 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
900 if (in
->has_jail
!= in_new
->has_jail
)
903 if (in
->trace
!= in_new
->trace
)
906 if (in
->require_jail
!= in_new
->require_jail
)
909 if (in
->immediately
!= in_new
->immediately
)
912 if (in
->no_new_privs
!= in_new
->no_new_privs
)
915 if (string_changed(in
->jail
.name
, in_new
->jail
.name
))
918 if (string_changed(in
->jail
.hostname
, in_new
->jail
.hostname
))
921 if (in
->jail
.procfs
!= in_new
->jail
.procfs
)
924 if (in
->jail
.sysfs
!= in_new
->jail
.sysfs
)
927 if (in
->jail
.ubus
!= in_new
->jail
.ubus
)
930 if (in
->jail
.log
!= in_new
->jail
.log
)
933 if (in
->jail
.ronly
!= in_new
->jail
.ronly
)
936 if (in
->jail
.netns
!= in_new
->jail
.netns
)
939 if (in
->jail
.userns
!= in_new
->jail
.userns
)
942 if (in
->jail
.cgroupsns
!= in_new
->jail
.cgroupsns
)
945 if (in
->jail
.console
!= in_new
->jail
.console
)
948 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
951 if (in
->watchdog
.mode
!= in_new
->watchdog
.mode
)
954 if (in
->watchdog
.freq
!= in_new
->watchdog
.freq
)
961 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
963 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
964 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
966 return n1
->ifindex
== n2
->ifindex
;
970 instance_netdev_update(struct blobmsg_list_node
*l
)
972 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
974 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
978 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
980 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
981 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
983 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
987 instance_file_update(struct blobmsg_list_node
*l
)
989 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
994 memset(f
->md5
, 0, sizeof(f
->md5
));
996 fd
= open(l
->avl
.key
, O_RDONLY
);
1002 len
= read(fd
, buf
, sizeof(buf
));
1012 md5_hash(buf
, len
, &md5
);
1015 md5_end(f
->md5
, &md5
);
1020 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
1025 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
1029 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
1031 struct blobmsg_list_node
*node
;
1036 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
1039 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
1041 blobmsg_list_for_each(l
, node
)
1048 instance_jail_parse(struct service_instance
*in
, struct blob_attr
*attr
)
1050 struct blob_attr
*tb
[__JAIL_ATTR_MAX
];
1051 struct jail
*jail
= &in
->jail
;
1053 blobmsg_parse(jail_attr
, __JAIL_ATTR_MAX
, tb
,
1054 blobmsg_data(attr
), blobmsg_data_len(attr
));
1058 if (tb
[JAIL_ATTR_REQUIREJAIL
] && blobmsg_get_bool(tb
[JAIL_ATTR_REQUIREJAIL
])) {
1059 in
->require_jail
= true;
1062 if (tb
[JAIL_ATTR_IMMEDIATELY
] && blobmsg_get_bool(tb
[JAIL_ATTR_IMMEDIATELY
])) {
1063 in
->immediately
= true;
1066 if (tb
[JAIL_ATTR_NAME
]) {
1067 jail
->name
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_NAME
]));
1070 if (tb
[JAIL_ATTR_HOSTNAME
]) {
1071 jail
->hostname
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_HOSTNAME
]));
1074 if (tb
[JAIL_ATTR_PROCFS
] && blobmsg_get_bool(tb
[JAIL_ATTR_PROCFS
])) {
1075 jail
->procfs
= true;
1078 if (tb
[JAIL_ATTR_SYSFS
] && blobmsg_get_bool(tb
[JAIL_ATTR_SYSFS
])) {
1082 if (tb
[JAIL_ATTR_UBUS
] && blobmsg_get_bool(tb
[JAIL_ATTR_UBUS
])) {
1086 if (tb
[JAIL_ATTR_LOG
] && blobmsg_get_bool(tb
[JAIL_ATTR_LOG
])) {
1090 if (tb
[JAIL_ATTR_RONLY
] && blobmsg_get_bool(tb
[JAIL_ATTR_RONLY
])) {
1094 if (tb
[JAIL_ATTR_NETNS
] && blobmsg_get_bool(tb
[JAIL_ATTR_NETNS
])) {
1098 if (tb
[JAIL_ATTR_USERNS
] && blobmsg_get_bool(tb
[JAIL_ATTR_USERNS
])) {
1099 jail
->userns
= true;
1102 if (tb
[JAIL_ATTR_CGROUPSNS
] && blobmsg_get_bool(tb
[JAIL_ATTR_CGROUPSNS
])) {
1103 jail
->cgroupsns
= true;
1106 if (tb
[JAIL_ATTR_CONSOLE
] && blobmsg_get_bool(tb
[JAIL_ATTR_CONSOLE
])) {
1107 jail
->console
= true;
1111 if (tb
[JAIL_ATTR_MOUNT
]) {
1112 struct blob_attr
*cur
;
1115 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_MOUNT
], rem
)
1117 instance_fill_array(&jail
->mount
, tb
[JAIL_ATTR_MOUNT
], NULL
, false);
1134 if (in
->tmpoverlaysize
)
1137 if (in
->no_new_privs
)
1147 instance_config_parse_command(struct service_instance
*in
, struct blob_attr
**tb
)
1149 struct blob_attr
*cur
, *cur2
;
1153 cur
= tb
[INSTANCE_ATTR_COMMAND
];
1159 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
1162 blobmsg_for_each_attr(cur2
, cur
, rem
) {
1172 instance_config_parse(struct service_instance
*in
)
1174 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
1175 struct blob_attr
*cur
, *cur2
;
1179 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
1180 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
1182 if (!tb
[INSTANCE_ATTR_BUNDLE
] && !instance_config_parse_command(in
, tb
))
1185 if (tb
[INSTANCE_ATTR_TERMTIMEOUT
])
1186 in
->term_timeout
= blobmsg_get_u32(tb
[INSTANCE_ATTR_TERMTIMEOUT
]);
1187 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
1189 uint32_t vals
[3] = { 3600, 5, 5};
1191 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
1192 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
1194 vals
[i
] = atoi(blobmsg_get_string(cur2
));
1198 in
->respawn_count
= 0;
1199 in
->respawn_threshold
= vals
[0];
1200 in
->respawn_timeout
= vals
[1];
1201 in
->respawn_retry
= vals
[2];
1203 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
1204 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
1205 trigger_add(in
->trigger
, in
);
1208 if (tb
[INSTANCE_ATTR_WATCH
]) {
1209 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
1210 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
1212 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
1213 watch_add(blobmsg_get_string(cur2
), in
);
1217 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
1218 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
1219 if (in
->nice
< -20 || in
->nice
> 20)
1223 if (tb
[INSTANCE_ATTR_USER
]) {
1224 const char *user
= blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]);
1225 struct passwd
*p
= getpwnam(user
);
1227 in
->user
= strdup(user
);
1228 in
->uid
= p
->pw_uid
;
1229 in
->gr_gid
= in
->pw_gid
= p
->pw_gid
;
1233 if (tb
[INSTANCE_ATTR_GROUP
]) {
1234 const char *group
= blobmsg_get_string(tb
[INSTANCE_ATTR_GROUP
]);
1235 struct group
*p
= getgrnam(group
);
1237 in
->group
= strdup(group
);
1238 in
->gr_gid
= p
->gr_gid
;
1242 if (tb
[INSTANCE_ATTR_TRACE
])
1243 in
->trace
= blobmsg_get_bool(tb
[INSTANCE_ATTR_TRACE
]);
1245 if (tb
[INSTANCE_ATTR_NO_NEW_PRIVS
])
1246 in
->no_new_privs
= blobmsg_get_bool(tb
[INSTANCE_ATTR_NO_NEW_PRIVS
]);
1248 if (!in
->trace
&& tb
[INSTANCE_ATTR_SECCOMP
])
1249 in
->seccomp
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_SECCOMP
]));
1251 if (tb
[INSTANCE_ATTR_EXTROOT
])
1252 in
->extroot
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_EXTROOT
]));
1254 if (tb
[INSTANCE_ATTR_OVERLAYDIR
])
1255 in
->overlaydir
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_OVERLAYDIR
]));
1257 if (tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
])
1258 in
->tmpoverlaysize
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
]));
1260 if (tb
[INSTANCE_ATTR_BUNDLE
])
1261 in
->bundle
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_BUNDLE
]));
1263 if (tb
[INSTANCE_ATTR_PIDFILE
]) {
1264 char *pidfile
= blobmsg_get_string(tb
[INSTANCE_ATTR_PIDFILE
]);
1266 in
->pidfile
= strdup(pidfile
);
1269 if (tb
[INSTANCE_ATTR_RELOADSIG
])
1270 in
->reload_signal
= blobmsg_get_u32(tb
[INSTANCE_ATTR_RELOADSIG
]);
1272 if (!in
->trace
&& tb
[INSTANCE_ATTR_JAIL
])
1273 in
->has_jail
= instance_jail_parse(in
, tb
[INSTANCE_ATTR_JAIL
]);
1276 r
= stat(UJAIL_BIN_PATH
, &s
);
1278 if (in
->require_jail
) {
1279 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1280 in
->srv
->name
, in
->name
, UJAIL_BIN_PATH
, r
);
1283 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH
, r
);
1284 in
->has_jail
= false;
1288 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
1289 in
->_stdout
.fd
.fd
= -1;
1291 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
1292 in
->_stderr
.fd
.fd
= -1;
1294 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
1296 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
1299 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
1302 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
1305 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
1308 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
1311 if (tb
[INSTANCE_ATTR_FACILITY
]) {
1312 int facility
= syslog_facility_str_to_int(blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1313 if (facility
!= -1) {
1314 in
->syslog_facility
= facility
;
1315 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1317 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1320 if (tb
[INSTANCE_ATTR_WATCHDOG
]) {
1322 uint32_t vals
[2] = { 0, 30 };
1324 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCHDOG
], rem
) {
1328 vals
[i
] = atoi(blobmsg_get_string(cur2
));
1332 if (vals
[0] >= 0 && vals
[0] < __INSTANCE_WATCHDOG_MODE_MAX
) {
1333 in
->watchdog
.mode
= vals
[0];
1334 DEBUG(3, "setting watchdog mode (%d)\n", vals
[0]);
1336 in
->watchdog
.mode
= 0;
1337 DEBUG(3, "unknown watchdog mode (%d) given, using default (0)\n", vals
[0]);
1341 in
->watchdog
.freq
= vals
[1];
1342 DEBUG(3, "setting watchdog timeout (%d)\n", vals
[0]);
1344 in
->watchdog
.freq
= 30;
1345 DEBUG(3, "invalid watchdog timeout (%d) given, using default (30)\n", vals
[1]);
1353 instance_config_cleanup(struct service_instance
*in
)
1355 blobmsg_list_free(&in
->env
);
1356 blobmsg_list_free(&in
->data
);
1357 blobmsg_list_free(&in
->netdev
);
1358 blobmsg_list_free(&in
->file
);
1359 blobmsg_list_free(&in
->limits
);
1360 blobmsg_list_free(&in
->errors
);
1361 blobmsg_list_free(&in
->jail
.mount
);
1365 instance_config_move_strdup(char **dst
, char *src
)
1379 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
1381 instance_config_cleanup(in
);
1382 blobmsg_list_move(&in
->env
, &in_src
->env
);
1383 blobmsg_list_move(&in
->data
, &in_src
->data
);
1384 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
1385 blobmsg_list_move(&in
->file
, &in_src
->file
);
1386 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
1387 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
1388 blobmsg_list_move(&in
->jail
.mount
, &in_src
->jail
.mount
);
1389 in
->trigger
= in_src
->trigger
;
1390 in
->command
= in_src
->command
;
1391 in
->respawn
= in_src
->respawn
;
1392 in
->respawn_retry
= in_src
->respawn_retry
;
1393 in
->respawn_threshold
= in_src
->respawn_threshold
;
1394 in
->respawn_timeout
= in_src
->respawn_timeout
;
1395 in
->reload_signal
= in_src
->reload_signal
;
1396 in
->term_timeout
= in_src
->term_timeout
;
1397 in
->watchdog
.mode
= in_src
->watchdog
.mode
;
1398 in
->watchdog
.freq
= in_src
->watchdog
.freq
;
1399 in
->watchdog
.timeout
= in_src
->watchdog
.timeout
;
1400 in
->name
= in_src
->name
;
1401 in
->nice
= in_src
->nice
;
1402 in
->trace
= in_src
->trace
;
1403 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
1404 in
->syslog_facility
= in_src
->syslog_facility
;
1405 in
->require_jail
= in_src
->require_jail
;
1406 in
->no_new_privs
= in_src
->no_new_privs
;
1407 in
->immediately
= in_src
->immediately
;
1408 in
->uid
= in_src
->uid
;
1409 in
->pw_gid
= in_src
->pw_gid
;
1410 in
->gr_gid
= in_src
->gr_gid
;
1412 in
->has_jail
= in_src
->has_jail
;
1413 in
->jail
.procfs
= in_src
->jail
.procfs
;
1414 in
->jail
.sysfs
= in_src
->jail
.sysfs
;
1415 in
->jail
.ubus
= in_src
->jail
.ubus
;
1416 in
->jail
.log
= in_src
->jail
.log
;
1417 in
->jail
.ronly
= in_src
->jail
.ronly
;
1418 in
->jail
.netns
= in_src
->jail
.netns
;
1419 in
->jail
.cgroupsns
= in_src
->jail
.cgroupsns
;
1420 in
->jail
.console
= in_src
->jail
.console
;
1421 in
->jail
.argc
= in_src
->jail
.argc
;
1423 instance_config_move_strdup(&in
->pidfile
, in_src
->pidfile
);
1424 instance_config_move_strdup(&in
->seccomp
, in_src
->seccomp
);
1425 instance_config_move_strdup(&in
->bundle
, in_src
->bundle
);
1426 instance_config_move_strdup(&in
->extroot
, in_src
->extroot
);
1427 instance_config_move_strdup(&in
->overlaydir
, in_src
->overlaydir
);
1428 instance_config_move_strdup(&in
->tmpoverlaysize
, in_src
->tmpoverlaysize
);
1429 instance_config_move_strdup(&in
->user
, in_src
->user
);
1430 instance_config_move_strdup(&in
->group
, in_src
->group
);
1431 instance_config_move_strdup(&in
->jail
.name
, in_src
->jail
.name
);
1432 instance_config_move_strdup(&in
->jail
.hostname
, in_src
->jail
.hostname
);
1435 in
->config
= in_src
->config
;
1436 in_src
->config
= NULL
;
1440 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
1442 bool changed
= instance_config_changed(in
, in_new
);
1443 bool running
= in
->proc
.pending
;
1444 bool stopping
= in
->halt
;
1446 if (!running
|| stopping
) {
1447 instance_config_move(in
, in_new
);
1451 instance_restart(in
);
1452 instance_config_move(in
, in_new
);
1453 /* restart happens in the child callback handler */
1458 instance_free(struct service_instance
*in
)
1460 instance_free_stdio(in
);
1461 uloop_process_delete(&in
->proc
);
1462 uloop_timeout_cancel(&in
->timeout
);
1463 uloop_timeout_cancel(&in
->watchdog
.timeout
);
1466 instance_config_cleanup(in
);
1471 free(in
->overlaydir
);
1472 free(in
->tmpoverlaysize
);
1474 free(in
->jail
.name
);
1475 free(in
->jail
.hostname
);
1482 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
1484 config
= blob_memdup(config
);
1486 in
->name
= blobmsg_name(config
);
1487 in
->config
= config
;
1488 in
->timeout
.cb
= instance_timeout
;
1489 in
->proc
.cb
= instance_exit
;
1490 in
->term_timeout
= 5;
1491 in
->syslog_facility
= LOG_DAEMON
;
1493 in
->require_jail
= false;
1494 in
->immediately
= false;
1496 in
->_stdout
.fd
.fd
= -2;
1497 in
->_stdout
.stream
.string_data
= true;
1498 in
->_stdout
.stream
.notify_read
= instance_stdout
;
1500 in
->_stderr
.fd
.fd
= -2;
1501 in
->_stderr
.stream
.string_data
= true;
1502 in
->_stderr
.stream
.notify_read
= instance_stderr
;
1504 in
->console
.fd
.fd
= -2;
1505 in
->console
.stream
.string_data
= true;
1506 in
->console
.stream
.notify_read
= instance_console
;
1508 in
->console_client
.fd
.fd
= -2;
1509 in
->console_client
.stream
.string_data
= true;
1510 in
->console_client
.stream
.notify_read
= instance_console_client
;
1512 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
1513 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
1514 blobmsg_list_simple_init(&in
->env
);
1515 blobmsg_list_simple_init(&in
->data
);
1516 blobmsg_list_simple_init(&in
->limits
);
1517 blobmsg_list_simple_init(&in
->errors
);
1518 blobmsg_list_simple_init(&in
->jail
.mount
);
1520 in
->watchdog
.timeout
.cb
= instance_watchdog
;
1522 in
->valid
= instance_config_parse(in
);
1525 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
1532 i
= blobmsg_open_table(b
, in
->name
);
1533 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
1534 if (in
->proc
.pending
)
1535 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
1537 blobmsg_add_blob(b
, in
->command
);
1539 blobmsg_add_string(b
, "bundle", in
->bundle
);
1540 blobmsg_add_u32(b
, "term_timeout", in
->term_timeout
);
1541 if (!in
->proc
.pending
)
1542 blobmsg_add_u32(b
, "exit_code", in
->exit_code
);
1544 if (!avl_is_empty(&in
->errors
.avl
)) {
1545 struct blobmsg_list_node
*var
;
1546 void *e
= blobmsg_open_array(b
, "errors");
1547 blobmsg_list_for_each(&in
->errors
, var
)
1548 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
1549 blobmsg_close_table(b
, e
);
1552 if (!avl_is_empty(&in
->env
.avl
)) {
1553 struct blobmsg_list_node
*var
;
1554 void *e
= blobmsg_open_table(b
, "env");
1555 blobmsg_list_for_each(&in
->env
, var
)
1556 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1557 blobmsg_close_table(b
, e
);
1560 if (!avl_is_empty(&in
->data
.avl
)) {
1561 struct blobmsg_list_node
*var
;
1562 void *e
= blobmsg_open_table(b
, "data");
1563 blobmsg_list_for_each(&in
->data
, var
)
1564 blobmsg_add_blob(b
, var
->data
);
1565 blobmsg_close_table(b
, e
);
1568 if (!avl_is_empty(&in
->limits
.avl
)) {
1569 struct blobmsg_list_node
*var
;
1570 void *e
= blobmsg_open_table(b
, "limits");
1571 blobmsg_list_for_each(&in
->limits
, var
)
1572 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1573 blobmsg_close_table(b
, e
);
1576 if (in
->reload_signal
)
1577 blobmsg_add_u32(b
, "reload_signal", in
->reload_signal
);
1580 void *r
= blobmsg_open_table(b
, "respawn");
1581 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
1582 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
1583 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
1584 blobmsg_close_table(b
, r
);
1588 blobmsg_add_u8(b
, "trace", true);
1590 if (in
->no_new_privs
)
1591 blobmsg_add_u8(b
, "no_new_privs", true);
1594 blobmsg_add_string(b
, "seccomp", in
->seccomp
);
1597 blobmsg_add_string(b
, "pidfile", in
->pidfile
);
1600 blobmsg_add_string(b
, "user", in
->user
);
1603 blobmsg_add_string(b
, "group", in
->group
);
1606 void *r
= blobmsg_open_table(b
, "jail");
1608 blobmsg_add_string(b
, "name", in
->jail
.name
);
1610 if (in
->jail
.hostname
)
1611 blobmsg_add_string(b
, "hostname", in
->jail
.hostname
);
1613 blobmsg_add_u8(b
, "procfs", in
->jail
.procfs
);
1614 blobmsg_add_u8(b
, "sysfs", in
->jail
.sysfs
);
1615 blobmsg_add_u8(b
, "ubus", in
->jail
.ubus
);
1616 blobmsg_add_u8(b
, "log", in
->jail
.log
);
1617 blobmsg_add_u8(b
, "ronly", in
->jail
.ronly
);
1618 blobmsg_add_u8(b
, "netns", in
->jail
.netns
);
1619 blobmsg_add_u8(b
, "userns", in
->jail
.userns
);
1620 blobmsg_add_u8(b
, "cgroupsns", in
->jail
.cgroupsns
);
1622 blobmsg_add_u8(b
, "immediately", in
->immediately
);
1624 blobmsg_add_u8(b
, "console", (in
->console
.fd
.fd
> -1));
1625 blobmsg_close_table(b
, r
);
1626 if (!avl_is_empty(&in
->jail
.mount
.avl
)) {
1627 struct blobmsg_list_node
*var
;
1628 void *e
= blobmsg_open_table(b
, "mount");
1629 blobmsg_list_for_each(&in
->jail
.mount
, var
)
1630 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1631 blobmsg_close_table(b
, e
);
1636 blobmsg_add_string(b
, "extroot", in
->extroot
);
1638 blobmsg_add_string(b
, "overlaydir", in
->overlaydir
);
1639 if (in
->tmpoverlaysize
)
1640 blobmsg_add_string(b
, "tmpoverlaysize", in
->tmpoverlaysize
);
1642 if (verbose
&& in
->trigger
)
1643 blobmsg_add_blob(b
, in
->trigger
);
1645 if (in
->watchdog
.mode
!= INSTANCE_WATCHDOG_MODE_DISABLED
) {
1646 void *r
= blobmsg_open_table(b
, "watchdog");
1647 blobmsg_add_u32(b
, "mode", in
->watchdog
.mode
);
1648 blobmsg_add_u32(b
, "timeout", in
->watchdog
.freq
);
1649 blobmsg_close_table(b
, r
);
1652 blobmsg_close_table(b
, i
);