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_CAPABILITIES
,
63 INSTANCE_ATTR_PIDFILE
,
64 INSTANCE_ATTR_RELOADSIG
,
65 INSTANCE_ATTR_TERMTIMEOUT
,
66 INSTANCE_ATTR_FACILITY
,
67 INSTANCE_ATTR_EXTROOT
,
68 INSTANCE_ATTR_OVERLAYDIR
,
69 INSTANCE_ATTR_TMPOVERLAYSIZE
,
71 INSTANCE_ATTR_WATCHDOG
,
75 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
76 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
77 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
78 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
79 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
80 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
81 [INSTANCE_ATTR_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
82 [INSTANCE_ATTR_RESPAWN
] = { "respawn", BLOBMSG_TYPE_ARRAY
},
83 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
84 [INSTANCE_ATTR_LIMITS
] = { "limits", BLOBMSG_TYPE_TABLE
},
85 [INSTANCE_ATTR_WATCH
] = { "watch", BLOBMSG_TYPE_ARRAY
},
86 [INSTANCE_ATTR_ERROR
] = { "error", BLOBMSG_TYPE_ARRAY
},
87 [INSTANCE_ATTR_USER
] = { "user", BLOBMSG_TYPE_STRING
},
88 [INSTANCE_ATTR_GROUP
] = { "group", BLOBMSG_TYPE_STRING
},
89 [INSTANCE_ATTR_STDOUT
] = { "stdout", BLOBMSG_TYPE_BOOL
},
90 [INSTANCE_ATTR_STDERR
] = { "stderr", BLOBMSG_TYPE_BOOL
},
91 [INSTANCE_ATTR_NO_NEW_PRIVS
] = { "no_new_privs", BLOBMSG_TYPE_BOOL
},
92 [INSTANCE_ATTR_JAIL
] = { "jail", BLOBMSG_TYPE_TABLE
},
93 [INSTANCE_ATTR_TRACE
] = { "trace", BLOBMSG_TYPE_BOOL
},
94 [INSTANCE_ATTR_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_STRING
},
95 [INSTANCE_ATTR_CAPABILITIES
] = { "capabilities", BLOBMSG_TYPE_STRING
},
96 [INSTANCE_ATTR_PIDFILE
] = { "pidfile", BLOBMSG_TYPE_STRING
},
97 [INSTANCE_ATTR_RELOADSIG
] = { "reload_signal", BLOBMSG_TYPE_INT32
},
98 [INSTANCE_ATTR_TERMTIMEOUT
] = { "term_timeout", BLOBMSG_TYPE_INT32
},
99 [INSTANCE_ATTR_FACILITY
] = { "facility", BLOBMSG_TYPE_STRING
},
100 [INSTANCE_ATTR_EXTROOT
] = { "extroot", BLOBMSG_TYPE_STRING
},
101 [INSTANCE_ATTR_OVERLAYDIR
] = { "overlaydir", BLOBMSG_TYPE_STRING
},
102 [INSTANCE_ATTR_TMPOVERLAYSIZE
] = { "tmpoverlaysize", BLOBMSG_TYPE_STRING
},
103 [INSTANCE_ATTR_BUNDLE
] = { "bundle", BLOBMSG_TYPE_STRING
},
104 [INSTANCE_ATTR_WATCHDOG
] = { "watchdog", BLOBMSG_TYPE_ARRAY
},
120 JAIL_ATTR_REQUIREJAIL
,
121 JAIL_ATTR_IMMEDIATELY
,
126 static const struct blobmsg_policy jail_attr
[__JAIL_ATTR_MAX
] = {
127 [JAIL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
128 [JAIL_ATTR_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
129 [JAIL_ATTR_PROCFS
] = { "procfs", BLOBMSG_TYPE_BOOL
},
130 [JAIL_ATTR_SYSFS
] = { "sysfs", BLOBMSG_TYPE_BOOL
},
131 [JAIL_ATTR_UBUS
] = { "ubus", BLOBMSG_TYPE_BOOL
},
132 [JAIL_ATTR_LOG
] = { "log", BLOBMSG_TYPE_BOOL
},
133 [JAIL_ATTR_RONLY
] = { "ronly", BLOBMSG_TYPE_BOOL
},
134 [JAIL_ATTR_MOUNT
] = { "mount", BLOBMSG_TYPE_TABLE
},
135 [JAIL_ATTR_NETNS
] = { "netns", BLOBMSG_TYPE_BOOL
},
136 [JAIL_ATTR_USERNS
] = { "userns", BLOBMSG_TYPE_BOOL
},
137 [JAIL_ATTR_CGROUPSNS
] = { "cgroupsns", BLOBMSG_TYPE_BOOL
},
138 [JAIL_ATTR_CONSOLE
] = { "console", BLOBMSG_TYPE_BOOL
},
139 [JAIL_ATTR_REQUIREJAIL
] = { "requirejail", BLOBMSG_TYPE_BOOL
},
140 [JAIL_ATTR_IMMEDIATELY
] = { "immediately", BLOBMSG_TYPE_BOOL
},
141 [JAIL_ATTR_PIDFILE
] = { "pidfile", BLOBMSG_TYPE_STRING
},
144 struct instance_netdev
{
145 struct blobmsg_list_node node
;
149 struct instance_file
{
150 struct blobmsg_list_node node
;
159 static const struct rlimit_name rlimit_names
[] = {
161 { "core", RLIMIT_CORE
},
162 { "cpu", RLIMIT_CPU
},
163 { "data", RLIMIT_DATA
},
164 { "fsize", RLIMIT_FSIZE
},
165 { "memlock", RLIMIT_MEMLOCK
},
166 { "nofile", RLIMIT_NOFILE
},
167 { "nproc", RLIMIT_NPROC
},
168 { "rss", RLIMIT_RSS
},
169 { "stack", RLIMIT_STACK
},
171 { "nice", RLIMIT_NICE
},
172 { "rtprio", RLIMIT_RTPRIO
},
173 { "msgqueue", RLIMIT_MSGQUEUE
},
174 { "sigpending", RLIMIT_SIGPENDING
},
179 static void closefd(int fd
)
181 if (fd
> STDERR_FILENO
)
185 /* convert a string into numeric syslog facility or return -1 if no match found */
187 syslog_facility_str_to_int(const char *facility
)
189 CODE
*p
= facilitynames
;
191 while (p
->c_name
&& strcasecmp(p
->c_name
, facility
))
198 instance_limits(const char *limit
, const char *value
)
202 unsigned long cur
, max
;
204 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
205 if (strcmp(rlimit_names
[i
].name
, limit
))
207 if (!strcmp(value
, "unlimited")) {
208 rlim
.rlim_cur
= RLIM_INFINITY
;
209 rlim
.rlim_max
= RLIM_INFINITY
;
211 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
217 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
224 setrlimit(rlimit_names
[i
].resource
, &rlim
);
230 jail_run(struct service_instance
*in
, char **argv
)
232 struct blobmsg_list_node
*var
;
233 struct jail
*jail
= &in
->jail
;
236 argv
[argc
++] = UJAIL_BIN_PATH
;
240 argv
[argc
++] = jail
->name
;
243 if (jail
->hostname
) {
245 argv
[argc
++] = jail
->hostname
;
250 argv
[argc
++] = in
->seccomp
;
255 argv
[argc
++] = in
->user
;
260 argv
[argc
++] = in
->group
;
263 if (in
->capabilities
) {
265 argv
[argc
++] = in
->capabilities
;
268 if (in
->no_new_privs
)
300 argv
[argc
++] = in
->extroot
;
303 if (in
->overlaydir
) {
305 argv
[argc
++] = in
->overlaydir
;
308 if (in
->tmpoverlaysize
) {
310 argv
[argc
++] = in
->tmpoverlaysize
;
318 argv
[argc
++] = jail
->pidfile
;
323 argv
[argc
++] = in
->bundle
;
326 if (in
->require_jail
)
329 blobmsg_list_for_each(&jail
->mount
, var
) {
330 const char *type
= blobmsg_data(var
->data
);
336 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
345 instance_removepid(struct service_instance
*in
) {
348 if (unlink(in
->pidfile
)) {
349 ERROR("Failed to remove pidfile: %s: %m\n", in
->pidfile
);
356 instance_writepid(struct service_instance
*in
)
363 _pidfile
= fopen(in
->pidfile
, "w");
364 if (_pidfile
== NULL
) {
365 ERROR("failed to open pidfile for writing: %s: %m", in
->pidfile
);
368 if (fprintf(_pidfile
, "%d\n", in
->proc
.pid
) < 0) {
369 ERROR("failed to write pidfile: %s: %m", in
->pidfile
);
373 if (fclose(_pidfile
)) {
374 ERROR("failed to close pidfile: %s: %m", in
->pidfile
);
382 instance_run(struct service_instance
*in
, int _stdout
, int _stderr
)
384 struct blobmsg_list_node
*var
;
385 struct blob_attr
*cur
;
387 int argc
= 1; /* NULL terminated */
389 bool seccomp
= !in
->trace
&& !in
->has_jail
&& in
->seccomp
;
390 bool setlbf
= _stdout
>= 0;
393 setpriority(PRIO_PROCESS
, 0, in
->nice
);
395 blobmsg_for_each_attr(cur
, in
->command
, rem
)
398 blobmsg_list_for_each(&in
->env
, var
)
399 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
402 setenv("SECCOMP_FILE", in
->seccomp
, 1);
405 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
407 blobmsg_list_for_each(&in
->limits
, var
)
408 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
410 if (in
->trace
|| seccomp
)
413 argv
= alloca(sizeof(char *) * (argc
+ in
->jail
.argc
));
416 #ifdef SECCOMP_SUPPORT
418 argv
[argc
++] = "/sbin/utrace";
420 argv
[argc
++] = "/sbin/seccomp-trace";
422 if (in
->trace
|| seccomp
)
423 ULOG_WARN("Seccomp support for %s::%s not available\n", in
->srv
->name
, in
->name
);
427 argc
= jail_run(in
, argv
);
428 if (argc
!= in
->jail
.argc
)
429 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
430 in
->jail
.argc
, argc
, in
->srv
->name
, in
->name
);
433 blobmsg_for_each_attr(cur
, in
->command
, rem
)
434 argv
[argc
++] = blobmsg_data(cur
);
438 _stdin
= open("/dev/null", O_RDONLY
);
441 _stdout
= open("/dev/null", O_WRONLY
);
444 _stderr
= open("/dev/null", O_WRONLY
);
447 dup2(_stdin
, STDIN_FILENO
);
451 dup2(_stdout
, STDOUT_FILENO
);
455 dup2(_stderr
, STDERR_FILENO
);
459 if (!in
->has_jail
&& in
->user
&& in
->pw_gid
&& initgroups(in
->user
, in
->pw_gid
)) {
460 ERROR("failed to initgroups() for user %s: %m\n", in
->user
);
463 if (!in
->has_jail
&& in
->gr_gid
&& setgid(in
->gr_gid
)) {
464 ERROR("failed to set group id %d: %m\n", in
->gr_gid
);
467 if (!in
->has_jail
&& in
->uid
&& setuid(in
->uid
)) {
468 ERROR("failed to set user id %d: %m\n", in
->uid
);
472 execvp(argv
[0], argv
);
477 instance_add_cgroup(const char *service
, const char *instance
)
483 if (stat("/sys/fs/cgroup/cgroup.subtree_control", &sb
))
486 mkdir(CGROUP_BASEDIR
, 0700);
488 snprintf(cgnamebuf
, sizeof(cgnamebuf
), "%s/%s", CGROUP_BASEDIR
, service
);
489 mkdir(cgnamebuf
, 0700);
490 snprintf(cgnamebuf
, sizeof(cgnamebuf
), "%s/%s/%s", CGROUP_BASEDIR
, service
, instance
);
491 mkdir(cgnamebuf
, 0700);
492 strcat(cgnamebuf
, "/cgroup.procs");
494 fd
= open(cgnamebuf
, O_WRONLY
);
498 dprintf(fd
, "%d", getpid());
503 instance_free_stdio(struct service_instance
*in
)
505 if (in
->_stdout
.fd
.fd
> -1) {
506 ustream_free(&in
->_stdout
.stream
);
507 close(in
->_stdout
.fd
.fd
);
508 in
->_stdout
.fd
.fd
= -1;
511 if (in
->_stderr
.fd
.fd
> -1) {
512 ustream_free(&in
->_stderr
.stream
);
513 close(in
->_stderr
.fd
.fd
);
514 in
->_stderr
.fd
.fd
= -1;
517 if (in
->console
.fd
.fd
> -1) {
518 ustream_free(&in
->console
.stream
);
519 close(in
->console
.fd
.fd
);
520 in
->console
.fd
.fd
= -1;
523 if (in
->console_client
.fd
.fd
> -1) {
524 ustream_free(&in
->console_client
.stream
);
525 close(in
->console_client
.fd
.fd
);
526 in
->console_client
.fd
.fd
= -1;
531 instance_start(struct service_instance
*in
)
534 int opipe
[2] = { -1, -1 };
535 int epipe
[2] = { -1, -1 };
537 if (!avl_is_empty(&in
->errors
.avl
)) {
538 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
542 if (!in
->bundle
&& !in
->command
) {
543 LOG("Not starting instance %s::%s, command not set\n", in
->srv
->name
, in
->name
);
547 if (in
->proc
.pending
) {
553 instance_free_stdio(in
);
554 if (in
->_stdout
.fd
.fd
> -2) {
556 ULOG_WARN("pipe() failed: %m\n");
557 opipe
[0] = opipe
[1] = -1;
561 if (in
->_stderr
.fd
.fd
> -2) {
563 ULOG_WARN("pipe() failed: %m\n");
564 epipe
[0] = epipe
[1] = -1;
582 instance_add_cgroup(in
->srv
->name
, in
->name
);
583 instance_run(in
, opipe
[1], epipe
[1]);
587 DEBUG(2, "Started instance %s::%s[%d]\n", in
->srv
->name
, in
->name
, pid
);
589 instance_writepid(in
);
590 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
591 uloop_process_add(&in
->proc
);
594 ustream_fd_init(&in
->_stdout
, opipe
[0]);
596 fcntl(opipe
[0], F_SETFD
, FD_CLOEXEC
);
600 ustream_fd_init(&in
->_stderr
, epipe
[0]);
602 fcntl(epipe
[0], F_SETFD
, FD_CLOEXEC
);
605 if (in
->watchdog
.mode
!= INSTANCE_WATCHDOG_MODE_DISABLED
) {
606 uloop_timeout_set(&in
->watchdog
.timeout
, in
->watchdog
.freq
* 1000);
607 DEBUG(2, "Started instance %s::%s watchdog timer : timeout = %d\n", in
->srv
->name
, in
->name
, in
->watchdog
.freq
);
610 service_event("instance.start", in
->srv
->name
, in
->name
);
614 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
616 char *newline
, *str
, *arg0
, ident
[32];
619 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
620 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
621 ulog_open(ULOG_SYSLOG
, in
->syslog_facility
, ident
);
624 str
= ustream_get_read_buf(s
, &len
);
628 newline
= memchr(str
, '\n', len
);
629 if (!newline
&& (s
->r
.buffer_len
!= len
))
634 len
= newline
+ 1 - str
;
636 ulog(prio
, "%s\n", str
);
638 ustream_consume(s
, len
);
641 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
645 instance_stdout(struct ustream
*s
, int bytes
)
647 instance_stdio(s
, LOG_INFO
,
648 container_of(s
, struct service_instance
, _stdout
.stream
));
652 instance_console(struct ustream
*s
, int bytes
)
654 struct service_instance
*in
= container_of(s
, struct service_instance
, console
.stream
);
659 buf
= ustream_get_read_buf(s
, &len
);
663 ulog(LOG_INFO
, "out: %s\n", buf
);
665 /* test if console client is attached */
666 if (in
->console_client
.fd
.fd
> -1)
667 ustream_write(&in
->console_client
.stream
, buf
, len
, false);
669 ustream_consume(s
, len
);
674 instance_console_client(struct ustream
*s
, int bytes
)
676 struct service_instance
*in
= container_of(s
, struct service_instance
, console_client
.stream
);
681 buf
= ustream_get_read_buf(s
, &len
);
685 ulog(LOG_INFO
, "in: %s\n", buf
);
686 ustream_write(&in
->console
.stream
, buf
, len
, false);
687 ustream_consume(s
, len
);
692 instance_stderr(struct ustream
*s
, int bytes
)
694 instance_stdio(s
, LOG_ERR
,
695 container_of(s
, struct service_instance
, _stderr
.stream
));
699 instance_timeout(struct uloop_timeout
*t
)
701 struct service_instance
*in
;
703 in
= container_of(t
, struct service_instance
, timeout
);
706 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
707 in
->srv
->name
, in
->name
, in
->proc
.pid
);
708 kill(in
->proc
.pid
, SIGKILL
);
709 } else if (in
->restart
|| in
->respawn
)
714 instance_delete(struct service_instance
*in
)
716 struct service
*s
= in
->srv
;
718 avl_delete(&s
->instances
.avl
, &in
->node
.avl
);
724 instance_exit_code(int ret
)
726 if (WIFEXITED(ret
)) {
727 return WEXITSTATUS(ret
);
730 if (WIFSIGNALED(ret
)) {
731 return SIGNALLED_OFFSET
+ WTERMSIG(ret
);
734 if (WIFSTOPPED(ret
)) {
735 return WSTOPSIG(ret
);
742 instance_exit(struct uloop_process
*p
, int ret
)
744 struct service_instance
*in
;
748 in
= container_of(p
, struct service_instance
, proc
);
750 clock_gettime(CLOCK_MONOTONIC
, &tp
);
751 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
753 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
755 in
->exit_code
= instance_exit_code(ret
);
756 uloop_timeout_cancel(&in
->timeout
);
757 uloop_timeout_cancel(&in
->watchdog
.timeout
);
758 service_event("instance.stop", in
->srv
->name
, in
->name
);
761 instance_removepid(in
);
766 } else if (in
->restart
) {
768 } else if (in
->respawn
) {
769 if (runtime
< in
->respawn_threshold
)
772 in
->respawn_count
= 0;
773 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
774 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
775 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
776 in
->restart
= in
->respawn
= 0;
778 service_event("instance.fail", in
->srv
->name
, in
->name
);
780 service_event("instance.respawn", in
->srv
->name
, in
->name
);
781 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
787 instance_stop(struct service_instance
*in
, bool halt
)
789 if (!in
->proc
.pending
) {
795 in
->restart
= in
->respawn
= false;
796 kill(in
->proc
.pid
, SIGTERM
);
797 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
801 instance_restart(struct service_instance
*in
)
803 if (!in
->proc
.pending
)
806 if (in
->reload_signal
) {
807 kill(in
->proc
.pid
, in
->reload_signal
);
813 kill(in
->proc
.pid
, SIGTERM
);
814 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
818 instance_watchdog(struct uloop_timeout
*t
)
820 struct service_instance
*in
= container_of(t
, struct service_instance
, watchdog
.timeout
);
822 DEBUG(3, "instance %s::%s watchdog timer expired\n", in
->srv
->name
, in
->name
);
825 instance_restart(in
);
827 instance_stop(in
, true);
830 static bool string_changed(const char *a
, const char *b
)
832 return !((!a
&& !b
) || (a
&& b
&& !strcmp(a
, b
)));
836 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
841 if (!blob_attr_equal(in
->command
, in_new
->command
))
844 if (string_changed(in
->bundle
, in_new
->bundle
))
847 if (string_changed(in
->extroot
, in_new
->extroot
))
850 if (string_changed(in
->overlaydir
, in_new
->overlaydir
))
853 if (string_changed(in
->tmpoverlaysize
, in_new
->tmpoverlaysize
))
856 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
859 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
862 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
865 if (in
->nice
!= in_new
->nice
)
868 if (in
->syslog_facility
!= in_new
->syslog_facility
)
871 if (string_changed(in
->user
, in_new
->user
))
874 if (string_changed(in
->group
, in_new
->group
))
877 if (in
->uid
!= in_new
->uid
)
880 if (in
->pw_gid
!= in_new
->pw_gid
)
883 if (in
->gr_gid
!= in_new
->gr_gid
)
886 if (string_changed(in
->pidfile
, in_new
->pidfile
))
889 if (in
->respawn_retry
!= in_new
->respawn_retry
)
891 if (in
->respawn_threshold
!= in_new
->respawn_threshold
)
893 if (in
->respawn_timeout
!= in_new
->respawn_timeout
)
896 if (in
->reload_signal
!= in_new
->reload_signal
)
899 if (in
->term_timeout
!= in_new
->term_timeout
)
902 if (string_changed(in
->seccomp
, in_new
->seccomp
))
905 if (string_changed(in
->capabilities
, in_new
->capabilities
))
908 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
911 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
914 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
917 if (in
->has_jail
!= in_new
->has_jail
)
920 if (in
->trace
!= in_new
->trace
)
923 if (in
->require_jail
!= in_new
->require_jail
)
926 if (in
->immediately
!= in_new
->immediately
)
929 if (in
->no_new_privs
!= in_new
->no_new_privs
)
932 if (string_changed(in
->jail
.name
, in_new
->jail
.name
))
935 if (string_changed(in
->jail
.hostname
, in_new
->jail
.hostname
))
938 if (string_changed(in
->jail
.pidfile
, in_new
->jail
.pidfile
))
941 if (in
->jail
.procfs
!= in_new
->jail
.procfs
)
944 if (in
->jail
.sysfs
!= in_new
->jail
.sysfs
)
947 if (in
->jail
.ubus
!= in_new
->jail
.ubus
)
950 if (in
->jail
.log
!= in_new
->jail
.log
)
953 if (in
->jail
.ronly
!= in_new
->jail
.ronly
)
956 if (in
->jail
.netns
!= in_new
->jail
.netns
)
959 if (in
->jail
.userns
!= in_new
->jail
.userns
)
962 if (in
->jail
.cgroupsns
!= in_new
->jail
.cgroupsns
)
965 if (in
->jail
.console
!= in_new
->jail
.console
)
968 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
971 if (in
->watchdog
.mode
!= in_new
->watchdog
.mode
)
974 if (in
->watchdog
.freq
!= in_new
->watchdog
.freq
)
981 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
983 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
984 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
986 return n1
->ifindex
== n2
->ifindex
;
990 instance_netdev_update(struct blobmsg_list_node
*l
)
992 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
994 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
998 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
1000 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
1001 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
1003 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
1007 instance_file_update(struct blobmsg_list_node
*l
)
1009 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
1014 memset(f
->md5
, 0, sizeof(f
->md5
));
1016 fd
= open(l
->avl
.key
, O_RDONLY
);
1022 len
= read(fd
, buf
, sizeof(buf
));
1032 md5_hash(buf
, len
, &md5
);
1035 md5_end(f
->md5
, &md5
);
1040 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
1045 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
1049 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
1051 struct blobmsg_list_node
*node
;
1056 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
1059 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
1061 blobmsg_list_for_each(l
, node
)
1068 instance_jail_parse(struct service_instance
*in
, struct blob_attr
*attr
)
1070 struct blob_attr
*tb
[__JAIL_ATTR_MAX
];
1071 struct jail
*jail
= &in
->jail
;
1073 blobmsg_parse(jail_attr
, __JAIL_ATTR_MAX
, tb
,
1074 blobmsg_data(attr
), blobmsg_data_len(attr
));
1078 if (tb
[JAIL_ATTR_REQUIREJAIL
] && blobmsg_get_bool(tb
[JAIL_ATTR_REQUIREJAIL
])) {
1079 in
->require_jail
= true;
1082 if (tb
[JAIL_ATTR_IMMEDIATELY
] && blobmsg_get_bool(tb
[JAIL_ATTR_IMMEDIATELY
])) {
1083 in
->immediately
= true;
1086 if (tb
[JAIL_ATTR_NAME
]) {
1087 jail
->name
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_NAME
]));
1090 if (tb
[JAIL_ATTR_HOSTNAME
]) {
1091 jail
->hostname
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_HOSTNAME
]));
1094 if (tb
[JAIL_ATTR_PROCFS
] && blobmsg_get_bool(tb
[JAIL_ATTR_PROCFS
])) {
1095 jail
->procfs
= true;
1098 if (tb
[JAIL_ATTR_SYSFS
] && blobmsg_get_bool(tb
[JAIL_ATTR_SYSFS
])) {
1102 if (tb
[JAIL_ATTR_UBUS
] && blobmsg_get_bool(tb
[JAIL_ATTR_UBUS
])) {
1106 if (tb
[JAIL_ATTR_LOG
] && blobmsg_get_bool(tb
[JAIL_ATTR_LOG
])) {
1110 if (tb
[JAIL_ATTR_RONLY
] && blobmsg_get_bool(tb
[JAIL_ATTR_RONLY
])) {
1114 if (tb
[JAIL_ATTR_NETNS
] && blobmsg_get_bool(tb
[JAIL_ATTR_NETNS
])) {
1118 if (tb
[JAIL_ATTR_USERNS
] && blobmsg_get_bool(tb
[JAIL_ATTR_USERNS
])) {
1119 jail
->userns
= true;
1122 if (tb
[JAIL_ATTR_CGROUPSNS
] && blobmsg_get_bool(tb
[JAIL_ATTR_CGROUPSNS
])) {
1123 jail
->cgroupsns
= true;
1126 if (tb
[JAIL_ATTR_CONSOLE
] && blobmsg_get_bool(tb
[JAIL_ATTR_CONSOLE
])) {
1127 jail
->console
= true;
1130 if (tb
[JAIL_ATTR_PIDFILE
]) {
1131 jail
->pidfile
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_PIDFILE
]));
1135 if (tb
[JAIL_ATTR_MOUNT
]) {
1136 struct blob_attr
*cur
;
1139 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_MOUNT
], rem
)
1141 instance_fill_array(&jail
->mount
, tb
[JAIL_ATTR_MOUNT
], NULL
, false);
1146 if (in
->capabilities
)
1161 if (in
->tmpoverlaysize
)
1164 if (in
->no_new_privs
)
1174 instance_config_parse_command(struct service_instance
*in
, struct blob_attr
**tb
)
1176 struct blob_attr
*cur
, *cur2
;
1180 cur
= tb
[INSTANCE_ATTR_COMMAND
];
1186 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
1189 blobmsg_for_each_attr(cur2
, cur
, rem
) {
1199 instance_config_parse(struct service_instance
*in
)
1201 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
1202 struct blob_attr
*cur
, *cur2
;
1206 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
1207 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
1209 if (!tb
[INSTANCE_ATTR_BUNDLE
] && !instance_config_parse_command(in
, tb
))
1212 if (tb
[INSTANCE_ATTR_TERMTIMEOUT
])
1213 in
->term_timeout
= blobmsg_get_u32(tb
[INSTANCE_ATTR_TERMTIMEOUT
]);
1214 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
1216 uint32_t vals
[3] = { 3600, 5, 5};
1218 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
1219 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
1221 vals
[i
] = atoi(blobmsg_get_string(cur2
));
1225 in
->respawn_count
= 0;
1226 in
->respawn_threshold
= vals
[0];
1227 in
->respawn_timeout
= vals
[1];
1228 in
->respawn_retry
= vals
[2];
1230 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
1231 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
1232 trigger_add(in
->trigger
, in
);
1235 if (tb
[INSTANCE_ATTR_WATCH
]) {
1236 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
1237 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
1239 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
1240 watch_add(blobmsg_get_string(cur2
), in
);
1244 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
1245 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
1246 if (in
->nice
< -20 || in
->nice
> 20)
1250 if (tb
[INSTANCE_ATTR_USER
]) {
1251 const char *user
= blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]);
1252 struct passwd
*p
= getpwnam(user
);
1254 in
->user
= strdup(user
);
1255 in
->uid
= p
->pw_uid
;
1256 in
->gr_gid
= in
->pw_gid
= p
->pw_gid
;
1260 if (tb
[INSTANCE_ATTR_GROUP
]) {
1261 const char *group
= blobmsg_get_string(tb
[INSTANCE_ATTR_GROUP
]);
1262 struct group
*p
= getgrnam(group
);
1264 in
->group
= strdup(group
);
1265 in
->gr_gid
= p
->gr_gid
;
1269 if (tb
[INSTANCE_ATTR_TRACE
])
1270 in
->trace
= blobmsg_get_bool(tb
[INSTANCE_ATTR_TRACE
]);
1272 if (tb
[INSTANCE_ATTR_NO_NEW_PRIVS
])
1273 in
->no_new_privs
= blobmsg_get_bool(tb
[INSTANCE_ATTR_NO_NEW_PRIVS
]);
1275 if (!in
->trace
&& tb
[INSTANCE_ATTR_SECCOMP
])
1276 in
->seccomp
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_SECCOMP
]));
1278 if (tb
[INSTANCE_ATTR_CAPABILITIES
])
1279 in
->capabilities
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_CAPABILITIES
]));
1281 if (tb
[INSTANCE_ATTR_EXTROOT
])
1282 in
->extroot
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_EXTROOT
]));
1284 if (tb
[INSTANCE_ATTR_OVERLAYDIR
])
1285 in
->overlaydir
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_OVERLAYDIR
]));
1287 if (tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
])
1288 in
->tmpoverlaysize
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
]));
1290 if (tb
[INSTANCE_ATTR_BUNDLE
])
1291 in
->bundle
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_BUNDLE
]));
1293 if (tb
[INSTANCE_ATTR_PIDFILE
]) {
1294 char *pidfile
= blobmsg_get_string(tb
[INSTANCE_ATTR_PIDFILE
]);
1296 in
->pidfile
= strdup(pidfile
);
1299 if (tb
[INSTANCE_ATTR_RELOADSIG
])
1300 in
->reload_signal
= blobmsg_get_u32(tb
[INSTANCE_ATTR_RELOADSIG
]);
1302 if (!in
->trace
&& tb
[INSTANCE_ATTR_JAIL
])
1303 in
->has_jail
= instance_jail_parse(in
, tb
[INSTANCE_ATTR_JAIL
]);
1306 r
= stat(UJAIL_BIN_PATH
, &s
);
1308 if (in
->require_jail
) {
1309 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1310 in
->srv
->name
, in
->name
, UJAIL_BIN_PATH
, r
);
1313 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH
, r
);
1314 in
->has_jail
= false;
1318 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
1319 in
->_stdout
.fd
.fd
= -1;
1321 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
1322 in
->_stderr
.fd
.fd
= -1;
1324 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
1326 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
1329 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
1332 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
1335 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
1338 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
1341 if (tb
[INSTANCE_ATTR_FACILITY
]) {
1342 int facility
= syslog_facility_str_to_int(blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1343 if (facility
!= -1) {
1344 in
->syslog_facility
= facility
;
1345 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1347 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1350 if (tb
[INSTANCE_ATTR_WATCHDOG
]) {
1352 uint32_t vals
[2] = { 0, 30 };
1354 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCHDOG
], rem
) {
1358 vals
[i
] = atoi(blobmsg_get_string(cur2
));
1362 if (vals
[0] >= 0 && vals
[0] < __INSTANCE_WATCHDOG_MODE_MAX
) {
1363 in
->watchdog
.mode
= vals
[0];
1364 DEBUG(3, "setting watchdog mode (%d)\n", vals
[0]);
1366 in
->watchdog
.mode
= 0;
1367 DEBUG(3, "unknown watchdog mode (%d) given, using default (0)\n", vals
[0]);
1371 in
->watchdog
.freq
= vals
[1];
1372 DEBUG(3, "setting watchdog timeout (%d)\n", vals
[0]);
1374 in
->watchdog
.freq
= 30;
1375 DEBUG(3, "invalid watchdog timeout (%d) given, using default (30)\n", vals
[1]);
1383 instance_config_cleanup(struct service_instance
*in
)
1385 blobmsg_list_free(&in
->env
);
1386 blobmsg_list_free(&in
->data
);
1387 blobmsg_list_free(&in
->netdev
);
1388 blobmsg_list_free(&in
->file
);
1389 blobmsg_list_free(&in
->limits
);
1390 blobmsg_list_free(&in
->errors
);
1391 blobmsg_list_free(&in
->jail
.mount
);
1395 instance_config_move_strdup(char **dst
, char *src
)
1409 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
1411 instance_config_cleanup(in
);
1412 blobmsg_list_move(&in
->env
, &in_src
->env
);
1413 blobmsg_list_move(&in
->data
, &in_src
->data
);
1414 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
1415 blobmsg_list_move(&in
->file
, &in_src
->file
);
1416 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
1417 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
1418 blobmsg_list_move(&in
->jail
.mount
, &in_src
->jail
.mount
);
1419 in
->trigger
= in_src
->trigger
;
1420 in
->command
= in_src
->command
;
1421 in
->respawn
= in_src
->respawn
;
1422 in
->respawn_retry
= in_src
->respawn_retry
;
1423 in
->respawn_threshold
= in_src
->respawn_threshold
;
1424 in
->respawn_timeout
= in_src
->respawn_timeout
;
1425 in
->reload_signal
= in_src
->reload_signal
;
1426 in
->term_timeout
= in_src
->term_timeout
;
1427 in
->watchdog
.mode
= in_src
->watchdog
.mode
;
1428 in
->watchdog
.freq
= in_src
->watchdog
.freq
;
1429 in
->watchdog
.timeout
= in_src
->watchdog
.timeout
;
1430 in
->name
= in_src
->name
;
1431 in
->nice
= in_src
->nice
;
1432 in
->trace
= in_src
->trace
;
1433 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
1434 in
->syslog_facility
= in_src
->syslog_facility
;
1435 in
->require_jail
= in_src
->require_jail
;
1436 in
->no_new_privs
= in_src
->no_new_privs
;
1437 in
->immediately
= in_src
->immediately
;
1438 in
->uid
= in_src
->uid
;
1439 in
->pw_gid
= in_src
->pw_gid
;
1440 in
->gr_gid
= in_src
->gr_gid
;
1442 in
->has_jail
= in_src
->has_jail
;
1443 in
->jail
.procfs
= in_src
->jail
.procfs
;
1444 in
->jail
.sysfs
= in_src
->jail
.sysfs
;
1445 in
->jail
.ubus
= in_src
->jail
.ubus
;
1446 in
->jail
.log
= in_src
->jail
.log
;
1447 in
->jail
.ronly
= in_src
->jail
.ronly
;
1448 in
->jail
.netns
= in_src
->jail
.netns
;
1449 in
->jail
.cgroupsns
= in_src
->jail
.cgroupsns
;
1450 in
->jail
.console
= in_src
->jail
.console
;
1451 in
->jail
.argc
= in_src
->jail
.argc
;
1453 instance_config_move_strdup(&in
->pidfile
, in_src
->pidfile
);
1454 instance_config_move_strdup(&in
->seccomp
, in_src
->seccomp
);
1455 instance_config_move_strdup(&in
->capabilities
, in_src
->capabilities
);
1456 instance_config_move_strdup(&in
->bundle
, in_src
->bundle
);
1457 instance_config_move_strdup(&in
->extroot
, in_src
->extroot
);
1458 instance_config_move_strdup(&in
->overlaydir
, in_src
->overlaydir
);
1459 instance_config_move_strdup(&in
->tmpoverlaysize
, in_src
->tmpoverlaysize
);
1460 instance_config_move_strdup(&in
->user
, in_src
->user
);
1461 instance_config_move_strdup(&in
->group
, in_src
->group
);
1462 instance_config_move_strdup(&in
->jail
.name
, in_src
->jail
.name
);
1463 instance_config_move_strdup(&in
->jail
.hostname
, in_src
->jail
.hostname
);
1464 instance_config_move_strdup(&in
->jail
.pidfile
, in_src
->jail
.pidfile
);
1467 in
->config
= in_src
->config
;
1468 in_src
->config
= NULL
;
1472 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
1474 bool changed
= instance_config_changed(in
, in_new
);
1475 bool running
= in
->proc
.pending
;
1476 bool stopping
= in
->halt
;
1478 if (!running
|| stopping
) {
1479 instance_config_move(in
, in_new
);
1483 instance_restart(in
);
1484 instance_config_move(in
, in_new
);
1485 /* restart happens in the child callback handler */
1490 instance_free(struct service_instance
*in
)
1492 instance_free_stdio(in
);
1493 uloop_process_delete(&in
->proc
);
1494 uloop_timeout_cancel(&in
->timeout
);
1495 uloop_timeout_cancel(&in
->watchdog
.timeout
);
1498 instance_config_cleanup(in
);
1503 free(in
->overlaydir
);
1504 free(in
->tmpoverlaysize
);
1506 free(in
->jail
.name
);
1507 free(in
->jail
.hostname
);
1508 free(in
->jail
.pidfile
);
1510 free(in
->capabilities
);
1516 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
1518 config
= blob_memdup(config
);
1520 in
->name
= blobmsg_name(config
);
1521 in
->config
= config
;
1522 in
->timeout
.cb
= instance_timeout
;
1523 in
->proc
.cb
= instance_exit
;
1524 in
->term_timeout
= 5;
1525 in
->syslog_facility
= LOG_DAEMON
;
1527 in
->require_jail
= false;
1528 in
->immediately
= false;
1530 in
->_stdout
.fd
.fd
= -2;
1531 in
->_stdout
.stream
.string_data
= true;
1532 in
->_stdout
.stream
.notify_read
= instance_stdout
;
1534 in
->_stderr
.fd
.fd
= -2;
1535 in
->_stderr
.stream
.string_data
= true;
1536 in
->_stderr
.stream
.notify_read
= instance_stderr
;
1538 in
->console
.fd
.fd
= -2;
1539 in
->console
.stream
.string_data
= true;
1540 in
->console
.stream
.notify_read
= instance_console
;
1542 in
->console_client
.fd
.fd
= -2;
1543 in
->console_client
.stream
.string_data
= true;
1544 in
->console_client
.stream
.notify_read
= instance_console_client
;
1546 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
1547 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
1548 blobmsg_list_simple_init(&in
->env
);
1549 blobmsg_list_simple_init(&in
->data
);
1550 blobmsg_list_simple_init(&in
->limits
);
1551 blobmsg_list_simple_init(&in
->errors
);
1552 blobmsg_list_simple_init(&in
->jail
.mount
);
1554 in
->watchdog
.timeout
.cb
= instance_watchdog
;
1556 in
->valid
= instance_config_parse(in
);
1559 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
1566 i
= blobmsg_open_table(b
, in
->name
);
1567 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
1568 if (in
->proc
.pending
)
1569 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
1571 blobmsg_add_blob(b
, in
->command
);
1573 blobmsg_add_string(b
, "bundle", in
->bundle
);
1574 blobmsg_add_u32(b
, "term_timeout", in
->term_timeout
);
1575 if (!in
->proc
.pending
)
1576 blobmsg_add_u32(b
, "exit_code", in
->exit_code
);
1578 if (!avl_is_empty(&in
->errors
.avl
)) {
1579 struct blobmsg_list_node
*var
;
1580 void *e
= blobmsg_open_array(b
, "errors");
1581 blobmsg_list_for_each(&in
->errors
, var
)
1582 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
1583 blobmsg_close_table(b
, e
);
1586 if (!avl_is_empty(&in
->env
.avl
)) {
1587 struct blobmsg_list_node
*var
;
1588 void *e
= blobmsg_open_table(b
, "env");
1589 blobmsg_list_for_each(&in
->env
, var
)
1590 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1591 blobmsg_close_table(b
, e
);
1594 if (!avl_is_empty(&in
->data
.avl
)) {
1595 struct blobmsg_list_node
*var
;
1596 void *e
= blobmsg_open_table(b
, "data");
1597 blobmsg_list_for_each(&in
->data
, var
)
1598 blobmsg_add_blob(b
, var
->data
);
1599 blobmsg_close_table(b
, e
);
1602 if (!avl_is_empty(&in
->limits
.avl
)) {
1603 struct blobmsg_list_node
*var
;
1604 void *e
= blobmsg_open_table(b
, "limits");
1605 blobmsg_list_for_each(&in
->limits
, var
)
1606 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1607 blobmsg_close_table(b
, e
);
1610 if (in
->reload_signal
)
1611 blobmsg_add_u32(b
, "reload_signal", in
->reload_signal
);
1614 void *r
= blobmsg_open_table(b
, "respawn");
1615 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
1616 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
1617 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
1618 blobmsg_close_table(b
, r
);
1622 blobmsg_add_u8(b
, "trace", true);
1624 if (in
->no_new_privs
)
1625 blobmsg_add_u8(b
, "no_new_privs", true);
1628 blobmsg_add_string(b
, "seccomp", in
->seccomp
);
1630 if (in
->capabilities
)
1631 blobmsg_add_string(b
, "capabilities", in
->capabilities
);
1634 blobmsg_add_string(b
, "pidfile", in
->pidfile
);
1637 blobmsg_add_string(b
, "user", in
->user
);
1640 blobmsg_add_string(b
, "group", in
->group
);
1643 void *r
= blobmsg_open_table(b
, "jail");
1645 blobmsg_add_string(b
, "name", in
->jail
.name
);
1647 if (in
->jail
.hostname
)
1648 blobmsg_add_string(b
, "hostname", in
->jail
.hostname
);
1650 blobmsg_add_u8(b
, "procfs", in
->jail
.procfs
);
1651 blobmsg_add_u8(b
, "sysfs", in
->jail
.sysfs
);
1652 blobmsg_add_u8(b
, "ubus", in
->jail
.ubus
);
1653 blobmsg_add_u8(b
, "log", in
->jail
.log
);
1654 blobmsg_add_u8(b
, "ronly", in
->jail
.ronly
);
1655 blobmsg_add_u8(b
, "netns", in
->jail
.netns
);
1656 blobmsg_add_u8(b
, "userns", in
->jail
.userns
);
1657 blobmsg_add_u8(b
, "cgroupsns", in
->jail
.cgroupsns
);
1659 if (in
->jail
.pidfile
)
1660 blobmsg_add_string(b
, "pidfile", in
->jail
.pidfile
);
1662 blobmsg_add_u8(b
, "immediately", in
->immediately
);
1664 blobmsg_add_u8(b
, "console", (in
->console
.fd
.fd
> -1));
1665 blobmsg_close_table(b
, r
);
1666 if (!avl_is_empty(&in
->jail
.mount
.avl
)) {
1667 struct blobmsg_list_node
*var
;
1668 void *e
= blobmsg_open_table(b
, "mount");
1669 blobmsg_list_for_each(&in
->jail
.mount
, var
)
1670 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1671 blobmsg_close_table(b
, e
);
1676 blobmsg_add_string(b
, "extroot", in
->extroot
);
1678 blobmsg_add_string(b
, "overlaydir", in
->overlaydir
);
1679 if (in
->tmpoverlaysize
)
1680 blobmsg_add_string(b
, "tmpoverlaysize", in
->tmpoverlaysize
);
1682 if (verbose
&& in
->trigger
)
1683 blobmsg_add_blob(b
, in
->trigger
);
1685 if (in
->watchdog
.mode
!= INSTANCE_WATCHDOG_MODE_DISABLED
) {
1686 void *r
= blobmsg_open_table(b
, "watchdog");
1687 blobmsg_add_u32(b
, "mode", in
->watchdog
.mode
);
1688 blobmsg_add_u32(b
, "timeout", in
->watchdog
.freq
);
1689 blobmsg_close_table(b
, r
);
1692 blobmsg_close_table(b
, i
);