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
,
127 static const struct blobmsg_policy jail_attr
[__JAIL_ATTR_MAX
] = {
128 [JAIL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
129 [JAIL_ATTR_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
130 [JAIL_ATTR_PROCFS
] = { "procfs", BLOBMSG_TYPE_BOOL
},
131 [JAIL_ATTR_SYSFS
] = { "sysfs", BLOBMSG_TYPE_BOOL
},
132 [JAIL_ATTR_UBUS
] = { "ubus", BLOBMSG_TYPE_BOOL
},
133 [JAIL_ATTR_LOG
] = { "log", BLOBMSG_TYPE_BOOL
},
134 [JAIL_ATTR_RONLY
] = { "ronly", BLOBMSG_TYPE_BOOL
},
135 [JAIL_ATTR_MOUNT
] = { "mount", BLOBMSG_TYPE_TABLE
},
136 [JAIL_ATTR_NETNS
] = { "netns", BLOBMSG_TYPE_BOOL
},
137 [JAIL_ATTR_USERNS
] = { "userns", BLOBMSG_TYPE_BOOL
},
138 [JAIL_ATTR_CGROUPSNS
] = { "cgroupsns", BLOBMSG_TYPE_BOOL
},
139 [JAIL_ATTR_CONSOLE
] = { "console", BLOBMSG_TYPE_BOOL
},
140 [JAIL_ATTR_REQUIREJAIL
] = { "requirejail", BLOBMSG_TYPE_BOOL
},
141 [JAIL_ATTR_IMMEDIATELY
] = { "immediately", BLOBMSG_TYPE_BOOL
},
142 [JAIL_ATTR_PIDFILE
] = { "pidfile", BLOBMSG_TYPE_STRING
},
143 [JAIL_ATTR_SETNS
] = { "setns", BLOBMSG_TYPE_ARRAY
},
149 __JAIL_SETNS_ATTR_MAX
,
152 static const struct blobmsg_policy jail_setns_attr
[__JAIL_SETNS_ATTR_MAX
] = {
153 [JAIL_SETNS_ATTR_PID
] = { "pid", BLOBMSG_TYPE_INT32
},
154 [JAIL_SETNS_ATTR_NS
] = { "namespaces", BLOBMSG_TYPE_ARRAY
},
157 struct instance_netdev
{
158 struct blobmsg_list_node node
;
162 struct instance_file
{
163 struct blobmsg_list_node node
;
172 static const struct rlimit_name rlimit_names
[] = {
174 { "core", RLIMIT_CORE
},
175 { "cpu", RLIMIT_CPU
},
176 { "data", RLIMIT_DATA
},
177 { "fsize", RLIMIT_FSIZE
},
178 { "memlock", RLIMIT_MEMLOCK
},
179 { "nofile", RLIMIT_NOFILE
},
180 { "nproc", RLIMIT_NPROC
},
181 { "rss", RLIMIT_RSS
},
182 { "stack", RLIMIT_STACK
},
184 { "nice", RLIMIT_NICE
},
185 { "rtprio", RLIMIT_RTPRIO
},
186 { "msgqueue", RLIMIT_MSGQUEUE
},
187 { "sigpending", RLIMIT_SIGPENDING
},
192 static void closefd(int fd
)
194 if (fd
> STDERR_FILENO
)
198 /* convert a string into numeric syslog facility or return -1 if no match found */
200 syslog_facility_str_to_int(const char *facility
)
202 CODE
*p
= facilitynames
;
204 while (p
->c_name
&& strcasecmp(p
->c_name
, facility
))
211 instance_limits(const char *limit
, const char *value
)
215 unsigned long cur
, max
;
217 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
218 if (strcmp(rlimit_names
[i
].name
, limit
))
220 if (!strcmp(value
, "unlimited")) {
221 rlim
.rlim_cur
= RLIM_INFINITY
;
222 rlim
.rlim_max
= RLIM_INFINITY
;
224 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
230 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
237 setrlimit(rlimit_names
[i
].resource
, &rlim
);
243 instance_gen_setns_argstr(struct blob_attr
*attr
)
245 struct blob_attr
*tb
[__JAIL_SETNS_ATTR_MAX
];
246 struct blob_attr
*cur
;
250 blobmsg_parse(jail_setns_attr
, __JAIL_SETNS_ATTR_MAX
, tb
,
251 blobmsg_data(attr
), blobmsg_data_len(attr
));
253 if (!tb
[JAIL_SETNS_ATTR_PID
] || !tb
[JAIL_SETNS_ATTR_NS
])
256 len
= snprintf(NULL
, 0, "%d:", blobmsg_get_u32(tb
[JAIL_SETNS_ATTR_PID
]));
258 blobmsg_for_each_attr(cur
, tb
[JAIL_SETNS_ATTR_NS
], rem
) {
261 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
264 tmp
= blobmsg_get_string(cur
);
268 len
+= strlen(tmp
) + 1;
276 len
= snprintf(ret
, total
, "%d:", blobmsg_get_u32(tb
[JAIL_SETNS_ATTR_PID
]));
278 blobmsg_for_each_attr(cur
, tb
[JAIL_SETNS_ATTR_NS
], rem
) {
279 strncpy(&ret
[len
], blobmsg_get_string(cur
), total
- len
);
280 len
+= strlen(blobmsg_get_string(cur
));
283 ret
[total
- 1] = '\0';
289 jail_run(struct service_instance
*in
, char **argv
)
291 char *term_timeout_str
;
292 struct blobmsg_list_node
*var
;
293 struct jail
*jail
= &in
->jail
;
296 argv
[argc
++] = UJAIL_BIN_PATH
;
298 asprintf(&term_timeout_str
, "%d", in
->term_timeout
);
300 argv
[argc
++] = term_timeout_str
;
304 argv
[argc
++] = jail
->name
;
307 if (jail
->hostname
) {
309 argv
[argc
++] = jail
->hostname
;
314 argv
[argc
++] = in
->seccomp
;
319 argv
[argc
++] = in
->user
;
324 argv
[argc
++] = in
->group
;
327 if (in
->capabilities
) {
329 argv
[argc
++] = in
->capabilities
;
332 if (in
->no_new_privs
)
364 argv
[argc
++] = in
->extroot
;
367 if (in
->overlaydir
) {
369 argv
[argc
++] = in
->overlaydir
;
372 if (in
->tmpoverlaysize
) {
374 argv
[argc
++] = in
->tmpoverlaysize
;
382 argv
[argc
++] = jail
->pidfile
;
387 argv
[argc
++] = in
->bundle
;
390 if (in
->require_jail
)
393 blobmsg_list_for_each(&in
->env
, var
) {
395 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
398 blobmsg_list_for_each(&jail
->mount
, var
) {
399 const char *type
= blobmsg_data(var
->data
);
405 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
408 blobmsg_list_for_each(&jail
->setns
, var
) {
409 char *setns_arg
= instance_gen_setns_argstr(var
->data
);
413 argv
[argc
++] = setns_arg
;
423 instance_removepid(struct service_instance
*in
) {
426 if (unlink(in
->pidfile
)) {
427 ERROR("Failed to remove pidfile: %s: %m\n", in
->pidfile
);
434 instance_writepid(struct service_instance
*in
)
441 _pidfile
= fopen(in
->pidfile
, "w");
442 if (_pidfile
== NULL
) {
443 ERROR("failed to open pidfile for writing: %s: %m", in
->pidfile
);
446 if (fprintf(_pidfile
, "%d\n", in
->proc
.pid
) < 0) {
447 ERROR("failed to write pidfile: %s: %m", in
->pidfile
);
451 if (fclose(_pidfile
)) {
452 ERROR("failed to close pidfile: %s: %m", in
->pidfile
);
460 instance_run(struct service_instance
*in
, int _stdout
, int _stderr
)
462 struct blobmsg_list_node
*var
;
463 struct blob_attr
*cur
;
465 int argc
= 1; /* NULL terminated */
467 bool seccomp
= !in
->trace
&& !in
->has_jail
&& in
->seccomp
;
468 bool setlbf
= _stdout
>= 0;
471 setpriority(PRIO_PROCESS
, 0, in
->nice
);
473 blobmsg_for_each_attr(cur
, in
->command
, rem
)
476 blobmsg_list_for_each(&in
->env
, var
)
477 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
480 setenv("SECCOMP_FILE", in
->seccomp
, 1);
483 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
485 blobmsg_list_for_each(&in
->limits
, var
)
486 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
488 if (in
->trace
|| seccomp
)
491 argv
= alloca(sizeof(char *) * (argc
+ in
->jail
.argc
));
494 #ifdef SECCOMP_SUPPORT
496 argv
[argc
++] = "/sbin/utrace";
498 argv
[argc
++] = "/sbin/seccomp-trace";
500 if (in
->trace
|| seccomp
)
501 ULOG_WARN("Seccomp support for %s::%s not available\n", in
->srv
->name
, in
->name
);
505 argc
= jail_run(in
, argv
);
506 if (argc
!= in
->jail
.argc
)
507 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
508 in
->jail
.argc
, argc
, in
->srv
->name
, in
->name
);
511 blobmsg_for_each_attr(cur
, in
->command
, rem
)
512 argv
[argc
++] = blobmsg_data(cur
);
516 _stdin
= open("/dev/null", O_RDONLY
);
519 _stdout
= open("/dev/null", O_WRONLY
);
522 _stderr
= open("/dev/null", O_WRONLY
);
525 dup2(_stdin
, STDIN_FILENO
);
529 dup2(_stdout
, STDOUT_FILENO
);
533 dup2(_stderr
, STDERR_FILENO
);
537 if (!in
->has_jail
&& in
->user
&& in
->pw_gid
&& initgroups(in
->user
, in
->pw_gid
)) {
538 ERROR("failed to initgroups() for user %s: %m\n", in
->user
);
541 if (!in
->has_jail
&& in
->gr_gid
&& setgid(in
->gr_gid
)) {
542 ERROR("failed to set group id %d: %m\n", in
->gr_gid
);
545 if (!in
->has_jail
&& in
->uid
&& setuid(in
->uid
)) {
546 ERROR("failed to set user id %d: %m\n", in
->uid
);
550 execvp(argv
[0], argv
);
555 instance_add_cgroup(const char *service
, const char *instance
)
561 if (stat("/sys/fs/cgroup/cgroup.subtree_control", &sb
))
564 mkdir(CGROUP_BASEDIR
, 0700);
566 snprintf(cgnamebuf
, sizeof(cgnamebuf
), "%s/%s", CGROUP_BASEDIR
, service
);
567 mkdir(cgnamebuf
, 0700);
568 snprintf(cgnamebuf
, sizeof(cgnamebuf
), "%s/%s/%s", CGROUP_BASEDIR
, service
, instance
);
569 mkdir(cgnamebuf
, 0700);
570 strcat(cgnamebuf
, "/cgroup.procs");
572 fd
= open(cgnamebuf
, O_WRONLY
);
576 dprintf(fd
, "%d", getpid());
581 instance_free_stdio(struct service_instance
*in
)
583 if (in
->_stdout
.fd
.fd
> -1) {
584 ustream_free(&in
->_stdout
.stream
);
585 close(in
->_stdout
.fd
.fd
);
586 in
->_stdout
.fd
.fd
= -1;
589 if (in
->_stderr
.fd
.fd
> -1) {
590 ustream_free(&in
->_stderr
.stream
);
591 close(in
->_stderr
.fd
.fd
);
592 in
->_stderr
.fd
.fd
= -1;
595 if (in
->console
.fd
.fd
> -1) {
596 ustream_free(&in
->console
.stream
);
597 close(in
->console
.fd
.fd
);
598 in
->console
.fd
.fd
= -1;
601 if (in
->console_client
.fd
.fd
> -1) {
602 ustream_free(&in
->console_client
.stream
);
603 close(in
->console_client
.fd
.fd
);
604 in
->console_client
.fd
.fd
= -1;
609 instance_start(struct service_instance
*in
)
612 int opipe
[2] = { -1, -1 };
613 int epipe
[2] = { -1, -1 };
615 if (!avl_is_empty(&in
->errors
.avl
)) {
616 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
620 if (!in
->bundle
&& !in
->command
) {
621 LOG("Not starting instance %s::%s, command not set\n", in
->srv
->name
, in
->name
);
625 if (in
->proc
.pending
) {
631 instance_free_stdio(in
);
632 if (in
->_stdout
.fd
.fd
> -2) {
634 ULOG_WARN("pipe() failed: %m\n");
635 opipe
[0] = opipe
[1] = -1;
639 if (in
->_stderr
.fd
.fd
> -2) {
641 ULOG_WARN("pipe() failed: %m\n");
642 epipe
[0] = epipe
[1] = -1;
660 instance_add_cgroup(in
->srv
->name
, in
->name
);
661 instance_run(in
, opipe
[1], epipe
[1]);
665 DEBUG(2, "Started instance %s::%s[%d]\n", in
->srv
->name
, in
->name
, pid
);
667 instance_writepid(in
);
668 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
669 uloop_process_add(&in
->proc
);
672 ustream_fd_init(&in
->_stdout
, opipe
[0]);
674 fcntl(opipe
[0], F_SETFD
, FD_CLOEXEC
);
678 ustream_fd_init(&in
->_stderr
, epipe
[0]);
680 fcntl(epipe
[0], F_SETFD
, FD_CLOEXEC
);
683 if (in
->watchdog
.mode
!= INSTANCE_WATCHDOG_MODE_DISABLED
) {
684 uloop_timeout_set(&in
->watchdog
.timeout
, in
->watchdog
.freq
* 1000);
685 DEBUG(2, "Started instance %s::%s watchdog timer : timeout = %d\n", in
->srv
->name
, in
->name
, in
->watchdog
.freq
);
688 service_event("instance.start", in
->srv
->name
, in
->name
);
692 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
694 char *newline
, *str
, *arg0
, ident
[32];
697 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
698 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
699 ulog_open(ULOG_SYSLOG
, in
->syslog_facility
, ident
);
702 str
= ustream_get_read_buf(s
, &len
);
706 newline
= memchr(str
, '\n', len
);
707 if (!newline
&& (s
->r
.buffer_len
!= len
))
712 len
= newline
+ 1 - str
;
714 ulog(prio
, "%s\n", str
);
716 ustream_consume(s
, len
);
719 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
723 instance_stdout(struct ustream
*s
, int bytes
)
725 instance_stdio(s
, LOG_INFO
,
726 container_of(s
, struct service_instance
, _stdout
.stream
));
730 instance_console(struct ustream
*s
, int bytes
)
732 struct service_instance
*in
= container_of(s
, struct service_instance
, console
.stream
);
737 buf
= ustream_get_read_buf(s
, &len
);
741 ulog(LOG_INFO
, "out: %s\n", buf
);
743 /* test if console client is attached */
744 if (in
->console_client
.fd
.fd
> -1)
745 ustream_write(&in
->console_client
.stream
, buf
, len
, false);
747 ustream_consume(s
, len
);
752 instance_console_client(struct ustream
*s
, int bytes
)
754 struct service_instance
*in
= container_of(s
, struct service_instance
, console_client
.stream
);
759 buf
= ustream_get_read_buf(s
, &len
);
763 ulog(LOG_INFO
, "in: %s\n", buf
);
764 ustream_write(&in
->console
.stream
, buf
, len
, false);
765 ustream_consume(s
, len
);
770 instance_stderr(struct ustream
*s
, int bytes
)
772 instance_stdio(s
, LOG_ERR
,
773 container_of(s
, struct service_instance
, _stderr
.stream
));
777 instance_timeout(struct uloop_timeout
*t
)
779 struct service_instance
*in
;
781 in
= container_of(t
, struct service_instance
, timeout
);
784 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
785 in
->srv
->name
, in
->name
, in
->proc
.pid
);
786 kill(in
->proc
.pid
, SIGKILL
);
787 } else if (in
->restart
|| in
->respawn
)
792 instance_delete(struct service_instance
*in
)
794 struct service
*s
= in
->srv
;
796 avl_delete(&s
->instances
.avl
, &in
->node
.avl
);
802 instance_exit_code(int ret
)
804 if (WIFEXITED(ret
)) {
805 return WEXITSTATUS(ret
);
808 if (WIFSIGNALED(ret
)) {
809 return SIGNALLED_OFFSET
+ WTERMSIG(ret
);
812 if (WIFSTOPPED(ret
)) {
813 return WSTOPSIG(ret
);
820 instance_exit(struct uloop_process
*p
, int ret
)
822 struct service_instance
*in
;
826 in
= container_of(p
, struct service_instance
, proc
);
828 clock_gettime(CLOCK_MONOTONIC
, &tp
);
829 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
831 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
833 in
->exit_code
= instance_exit_code(ret
);
834 uloop_timeout_cancel(&in
->timeout
);
835 uloop_timeout_cancel(&in
->watchdog
.timeout
);
836 service_event("instance.stop", in
->srv
->name
, in
->name
);
839 instance_removepid(in
);
844 } else if (in
->restart
) {
846 } else if (in
->respawn
) {
847 if (runtime
< in
->respawn_threshold
)
850 in
->respawn_count
= 0;
851 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
852 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
853 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
854 in
->restart
= in
->respawn
= 0;
856 service_event("instance.fail", in
->srv
->name
, in
->name
);
858 service_event("instance.respawn", in
->srv
->name
, in
->name
);
859 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
865 instance_stop(struct service_instance
*in
, bool halt
)
867 if (!in
->proc
.pending
) {
873 in
->restart
= in
->respawn
= false;
874 kill(in
->proc
.pid
, SIGTERM
);
876 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
880 instance_restart(struct service_instance
*in
)
882 if (!in
->proc
.pending
)
885 if (in
->reload_signal
) {
886 kill(in
->proc
.pid
, in
->reload_signal
);
892 kill(in
->proc
.pid
, SIGTERM
);
894 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
898 instance_watchdog(struct uloop_timeout
*t
)
900 struct service_instance
*in
= container_of(t
, struct service_instance
, watchdog
.timeout
);
902 DEBUG(3, "instance %s::%s watchdog timer expired\n", in
->srv
->name
, in
->name
);
905 instance_restart(in
);
907 instance_stop(in
, true);
910 static bool string_changed(const char *a
, const char *b
)
912 return !((!a
&& !b
) || (a
&& b
&& !strcmp(a
, b
)));
916 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
921 if (!blob_attr_equal(in
->command
, in_new
->command
))
924 if (string_changed(in
->bundle
, in_new
->bundle
))
927 if (string_changed(in
->extroot
, in_new
->extroot
))
930 if (string_changed(in
->overlaydir
, in_new
->overlaydir
))
933 if (string_changed(in
->tmpoverlaysize
, in_new
->tmpoverlaysize
))
936 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
939 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
942 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
945 if (in
->nice
!= in_new
->nice
)
948 if (in
->syslog_facility
!= in_new
->syslog_facility
)
951 if (string_changed(in
->user
, in_new
->user
))
954 if (string_changed(in
->group
, in_new
->group
))
957 if (in
->uid
!= in_new
->uid
)
960 if (in
->pw_gid
!= in_new
->pw_gid
)
963 if (in
->gr_gid
!= in_new
->gr_gid
)
966 if (string_changed(in
->pidfile
, in_new
->pidfile
))
969 if (in
->respawn_retry
!= in_new
->respawn_retry
)
971 if (in
->respawn_threshold
!= in_new
->respawn_threshold
)
973 if (in
->respawn_timeout
!= in_new
->respawn_timeout
)
976 if (in
->reload_signal
!= in_new
->reload_signal
)
979 if (in
->term_timeout
!= in_new
->term_timeout
)
982 if (string_changed(in
->seccomp
, in_new
->seccomp
))
985 if (string_changed(in
->capabilities
, in_new
->capabilities
))
988 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
991 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
994 if (!blobmsg_list_equal(&in
->jail
.setns
, &in_new
->jail
.setns
))
997 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
1000 if (in
->has_jail
!= in_new
->has_jail
)
1003 if (in
->trace
!= in_new
->trace
)
1006 if (in
->require_jail
!= in_new
->require_jail
)
1009 if (in
->immediately
!= in_new
->immediately
)
1012 if (in
->no_new_privs
!= in_new
->no_new_privs
)
1015 if (string_changed(in
->jail
.name
, in_new
->jail
.name
))
1018 if (string_changed(in
->jail
.hostname
, in_new
->jail
.hostname
))
1021 if (string_changed(in
->jail
.pidfile
, in_new
->jail
.pidfile
))
1024 if (in
->jail
.procfs
!= in_new
->jail
.procfs
)
1027 if (in
->jail
.sysfs
!= in_new
->jail
.sysfs
)
1030 if (in
->jail
.ubus
!= in_new
->jail
.ubus
)
1033 if (in
->jail
.log
!= in_new
->jail
.log
)
1036 if (in
->jail
.ronly
!= in_new
->jail
.ronly
)
1039 if (in
->jail
.netns
!= in_new
->jail
.netns
)
1042 if (in
->jail
.userns
!= in_new
->jail
.userns
)
1045 if (in
->jail
.cgroupsns
!= in_new
->jail
.cgroupsns
)
1048 if (in
->jail
.console
!= in_new
->jail
.console
)
1051 if (in
->watchdog
.mode
!= in_new
->watchdog
.mode
)
1054 if (in
->watchdog
.freq
!= in_new
->watchdog
.freq
)
1061 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
1063 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
1064 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
1066 return n1
->ifindex
== n2
->ifindex
;
1070 instance_netdev_update(struct blobmsg_list_node
*l
)
1072 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
1074 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
1078 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
1080 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
1081 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
1083 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
1087 instance_file_update(struct blobmsg_list_node
*l
)
1089 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
1094 memset(f
->md5
, 0, sizeof(f
->md5
));
1096 fd
= open(l
->avl
.key
, O_RDONLY
);
1102 len
= read(fd
, buf
, sizeof(buf
));
1112 md5_hash(buf
, len
, &md5
);
1115 md5_end(f
->md5
, &md5
);
1120 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
1125 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
1129 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
1131 struct blobmsg_list_node
*node
;
1136 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
1139 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
1141 blobmsg_list_for_each(l
, node
)
1148 instance_jail_parse(struct service_instance
*in
, struct blob_attr
*attr
)
1150 struct blob_attr
*tb
[__JAIL_ATTR_MAX
];
1151 struct jail
*jail
= &in
->jail
;
1152 struct blobmsg_list_node
*var
;
1154 blobmsg_parse(jail_attr
, __JAIL_ATTR_MAX
, tb
,
1155 blobmsg_data(attr
), blobmsg_data_len(attr
));
1159 if (tb
[JAIL_ATTR_REQUIREJAIL
] && blobmsg_get_bool(tb
[JAIL_ATTR_REQUIREJAIL
])) {
1160 in
->require_jail
= true;
1163 if (tb
[JAIL_ATTR_IMMEDIATELY
] && blobmsg_get_bool(tb
[JAIL_ATTR_IMMEDIATELY
])) {
1164 in
->immediately
= true;
1167 if (tb
[JAIL_ATTR_NAME
]) {
1168 jail
->name
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_NAME
]));
1171 if (tb
[JAIL_ATTR_HOSTNAME
]) {
1172 jail
->hostname
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_HOSTNAME
]));
1175 if (tb
[JAIL_ATTR_PROCFS
] && blobmsg_get_bool(tb
[JAIL_ATTR_PROCFS
])) {
1176 jail
->procfs
= true;
1179 if (tb
[JAIL_ATTR_SYSFS
] && blobmsg_get_bool(tb
[JAIL_ATTR_SYSFS
])) {
1183 if (tb
[JAIL_ATTR_UBUS
] && blobmsg_get_bool(tb
[JAIL_ATTR_UBUS
])) {
1187 if (tb
[JAIL_ATTR_LOG
] && blobmsg_get_bool(tb
[JAIL_ATTR_LOG
])) {
1191 if (tb
[JAIL_ATTR_RONLY
] && blobmsg_get_bool(tb
[JAIL_ATTR_RONLY
])) {
1195 if (tb
[JAIL_ATTR_NETNS
] && blobmsg_get_bool(tb
[JAIL_ATTR_NETNS
])) {
1199 if (tb
[JAIL_ATTR_USERNS
] && blobmsg_get_bool(tb
[JAIL_ATTR_USERNS
])) {
1200 jail
->userns
= true;
1203 if (tb
[JAIL_ATTR_CGROUPSNS
] && blobmsg_get_bool(tb
[JAIL_ATTR_CGROUPSNS
])) {
1204 jail
->cgroupsns
= true;
1207 if (tb
[JAIL_ATTR_CONSOLE
] && blobmsg_get_bool(tb
[JAIL_ATTR_CONSOLE
])) {
1208 jail
->console
= true;
1211 if (tb
[JAIL_ATTR_PIDFILE
]) {
1212 jail
->pidfile
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_PIDFILE
]));
1216 if (tb
[JAIL_ATTR_SETNS
]) {
1217 struct blob_attr
*cur
;
1220 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_SETNS
], rem
)
1222 blobmsg_list_fill(&jail
->setns
, blobmsg_data(tb
[JAIL_ATTR_SETNS
]),
1223 blobmsg_data_len(tb
[JAIL_ATTR_SETNS
]), true);
1226 if (tb
[JAIL_ATTR_MOUNT
]) {
1227 struct blob_attr
*cur
;
1230 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_MOUNT
], rem
)
1232 instance_fill_array(&jail
->mount
, tb
[JAIL_ATTR_MOUNT
], NULL
, false);
1235 blobmsg_list_for_each(&in
->env
, var
)
1241 if (in
->capabilities
)
1256 if (in
->tmpoverlaysize
)
1259 if (in
->no_new_privs
)
1269 instance_config_parse_command(struct service_instance
*in
, struct blob_attr
**tb
)
1271 struct blob_attr
*cur
, *cur2
;
1275 cur
= tb
[INSTANCE_ATTR_COMMAND
];
1281 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
1284 blobmsg_for_each_attr(cur2
, cur
, rem
) {
1294 instance_config_parse(struct service_instance
*in
)
1296 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
1297 struct blob_attr
*cur
, *cur2
;
1301 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
1302 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
1304 if (!tb
[INSTANCE_ATTR_BUNDLE
] && !instance_config_parse_command(in
, tb
))
1307 if (tb
[INSTANCE_ATTR_TERMTIMEOUT
])
1308 in
->term_timeout
= blobmsg_get_u32(tb
[INSTANCE_ATTR_TERMTIMEOUT
]);
1309 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
1311 uint32_t vals
[3] = { 3600, 5, 5};
1313 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
1314 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
1316 vals
[i
] = atoi(blobmsg_get_string(cur2
));
1320 in
->respawn_count
= 0;
1321 in
->respawn_threshold
= vals
[0];
1322 in
->respawn_timeout
= vals
[1];
1323 in
->respawn_retry
= vals
[2];
1325 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
1326 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
1327 trigger_add(in
->trigger
, in
);
1330 if (tb
[INSTANCE_ATTR_WATCH
]) {
1331 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
1332 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
1334 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
1335 watch_add(blobmsg_get_string(cur2
), in
);
1339 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
1340 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
1341 if (in
->nice
< -20 || in
->nice
> 20)
1345 if (tb
[INSTANCE_ATTR_USER
]) {
1346 const char *user
= blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]);
1347 struct passwd
*p
= getpwnam(user
);
1349 in
->user
= strdup(user
);
1350 in
->uid
= p
->pw_uid
;
1351 in
->gr_gid
= in
->pw_gid
= p
->pw_gid
;
1355 if (tb
[INSTANCE_ATTR_GROUP
]) {
1356 const char *group
= blobmsg_get_string(tb
[INSTANCE_ATTR_GROUP
]);
1357 struct group
*p
= getgrnam(group
);
1359 in
->group
= strdup(group
);
1360 in
->gr_gid
= p
->gr_gid
;
1364 if (tb
[INSTANCE_ATTR_TRACE
])
1365 in
->trace
= blobmsg_get_bool(tb
[INSTANCE_ATTR_TRACE
]);
1367 if (tb
[INSTANCE_ATTR_NO_NEW_PRIVS
])
1368 in
->no_new_privs
= blobmsg_get_bool(tb
[INSTANCE_ATTR_NO_NEW_PRIVS
]);
1370 if (!in
->trace
&& tb
[INSTANCE_ATTR_SECCOMP
])
1371 in
->seccomp
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_SECCOMP
]));
1373 if (tb
[INSTANCE_ATTR_CAPABILITIES
])
1374 in
->capabilities
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_CAPABILITIES
]));
1376 if (tb
[INSTANCE_ATTR_EXTROOT
])
1377 in
->extroot
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_EXTROOT
]));
1379 if (tb
[INSTANCE_ATTR_OVERLAYDIR
])
1380 in
->overlaydir
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_OVERLAYDIR
]));
1382 if (tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
])
1383 in
->tmpoverlaysize
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
]));
1385 if (tb
[INSTANCE_ATTR_BUNDLE
])
1386 in
->bundle
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_BUNDLE
]));
1388 if (tb
[INSTANCE_ATTR_PIDFILE
]) {
1389 char *pidfile
= blobmsg_get_string(tb
[INSTANCE_ATTR_PIDFILE
]);
1391 in
->pidfile
= strdup(pidfile
);
1394 if (tb
[INSTANCE_ATTR_RELOADSIG
])
1395 in
->reload_signal
= blobmsg_get_u32(tb
[INSTANCE_ATTR_RELOADSIG
]);
1397 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
1398 in
->_stdout
.fd
.fd
= -1;
1400 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
1401 in
->_stderr
.fd
.fd
= -1;
1403 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
1405 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
1408 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
1411 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
1414 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
1417 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
1420 if (tb
[INSTANCE_ATTR_FACILITY
]) {
1421 int facility
= syslog_facility_str_to_int(blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1422 if (facility
!= -1) {
1423 in
->syslog_facility
= facility
;
1424 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1426 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1429 if (tb
[INSTANCE_ATTR_WATCHDOG
]) {
1431 uint32_t vals
[2] = { 0, 30 };
1433 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCHDOG
], rem
) {
1437 vals
[i
] = atoi(blobmsg_get_string(cur2
));
1441 if (vals
[0] >= 0 && vals
[0] < __INSTANCE_WATCHDOG_MODE_MAX
) {
1442 in
->watchdog
.mode
= vals
[0];
1443 DEBUG(3, "setting watchdog mode (%d)\n", vals
[0]);
1445 in
->watchdog
.mode
= 0;
1446 DEBUG(3, "unknown watchdog mode (%d) given, using default (0)\n", vals
[0]);
1450 in
->watchdog
.freq
= vals
[1];
1451 DEBUG(3, "setting watchdog timeout (%d)\n", vals
[0]);
1453 in
->watchdog
.freq
= 30;
1454 DEBUG(3, "invalid watchdog timeout (%d) given, using default (30)\n", vals
[1]);
1458 if (!in
->trace
&& tb
[INSTANCE_ATTR_JAIL
])
1459 in
->has_jail
= instance_jail_parse(in
, tb
[INSTANCE_ATTR_JAIL
]);
1462 r
= stat(UJAIL_BIN_PATH
, &s
);
1464 if (in
->require_jail
) {
1465 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1466 in
->srv
->name
, in
->name
, UJAIL_BIN_PATH
, r
);
1469 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH
, r
);
1470 in
->has_jail
= false;
1478 instance_config_cleanup(struct service_instance
*in
)
1480 blobmsg_list_free(&in
->env
);
1481 blobmsg_list_free(&in
->data
);
1482 blobmsg_list_free(&in
->netdev
);
1483 blobmsg_list_free(&in
->file
);
1484 blobmsg_list_free(&in
->limits
);
1485 blobmsg_list_free(&in
->errors
);
1486 blobmsg_list_free(&in
->jail
.mount
);
1487 blobmsg_list_free(&in
->jail
.setns
);
1491 instance_config_move_strdup(char **dst
, char *src
)
1505 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
1507 instance_config_cleanup(in
);
1508 blobmsg_list_move(&in
->env
, &in_src
->env
);
1509 blobmsg_list_move(&in
->data
, &in_src
->data
);
1510 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
1511 blobmsg_list_move(&in
->file
, &in_src
->file
);
1512 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
1513 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
1514 blobmsg_list_move(&in
->jail
.mount
, &in_src
->jail
.mount
);
1515 blobmsg_list_move(&in
->jail
.setns
, &in_src
->jail
.setns
);
1516 in
->trigger
= in_src
->trigger
;
1517 in
->command
= in_src
->command
;
1518 in
->respawn
= in_src
->respawn
;
1519 in
->respawn_retry
= in_src
->respawn_retry
;
1520 in
->respawn_threshold
= in_src
->respawn_threshold
;
1521 in
->respawn_timeout
= in_src
->respawn_timeout
;
1522 in
->reload_signal
= in_src
->reload_signal
;
1523 in
->term_timeout
= in_src
->term_timeout
;
1524 in
->watchdog
.mode
= in_src
->watchdog
.mode
;
1525 in
->watchdog
.freq
= in_src
->watchdog
.freq
;
1526 in
->watchdog
.timeout
= in_src
->watchdog
.timeout
;
1527 in
->name
= in_src
->name
;
1528 in
->nice
= in_src
->nice
;
1529 in
->trace
= in_src
->trace
;
1530 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
1531 in
->syslog_facility
= in_src
->syslog_facility
;
1532 in
->require_jail
= in_src
->require_jail
;
1533 in
->no_new_privs
= in_src
->no_new_privs
;
1534 in
->immediately
= in_src
->immediately
;
1535 in
->uid
= in_src
->uid
;
1536 in
->pw_gid
= in_src
->pw_gid
;
1537 in
->gr_gid
= in_src
->gr_gid
;
1539 in
->has_jail
= in_src
->has_jail
;
1540 in
->jail
.procfs
= in_src
->jail
.procfs
;
1541 in
->jail
.sysfs
= in_src
->jail
.sysfs
;
1542 in
->jail
.ubus
= in_src
->jail
.ubus
;
1543 in
->jail
.log
= in_src
->jail
.log
;
1544 in
->jail
.ronly
= in_src
->jail
.ronly
;
1545 in
->jail
.netns
= in_src
->jail
.netns
;
1546 in
->jail
.cgroupsns
= in_src
->jail
.cgroupsns
;
1547 in
->jail
.console
= in_src
->jail
.console
;
1548 in
->jail
.argc
= in_src
->jail
.argc
;
1550 instance_config_move_strdup(&in
->pidfile
, in_src
->pidfile
);
1551 instance_config_move_strdup(&in
->seccomp
, in_src
->seccomp
);
1552 instance_config_move_strdup(&in
->capabilities
, in_src
->capabilities
);
1553 instance_config_move_strdup(&in
->bundle
, in_src
->bundle
);
1554 instance_config_move_strdup(&in
->extroot
, in_src
->extroot
);
1555 instance_config_move_strdup(&in
->overlaydir
, in_src
->overlaydir
);
1556 instance_config_move_strdup(&in
->tmpoverlaysize
, in_src
->tmpoverlaysize
);
1557 instance_config_move_strdup(&in
->user
, in_src
->user
);
1558 instance_config_move_strdup(&in
->group
, in_src
->group
);
1559 instance_config_move_strdup(&in
->jail
.name
, in_src
->jail
.name
);
1560 instance_config_move_strdup(&in
->jail
.hostname
, in_src
->jail
.hostname
);
1561 instance_config_move_strdup(&in
->jail
.pidfile
, in_src
->jail
.pidfile
);
1564 in
->config
= in_src
->config
;
1565 in_src
->config
= NULL
;
1569 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
1571 bool changed
= instance_config_changed(in
, in_new
);
1572 bool running
= in
->proc
.pending
;
1573 bool stopping
= in
->halt
;
1575 if (!running
|| stopping
) {
1576 instance_config_move(in
, in_new
);
1580 instance_restart(in
);
1581 instance_config_move(in
, in_new
);
1582 /* restart happens in the child callback handler */
1587 instance_free(struct service_instance
*in
)
1589 instance_free_stdio(in
);
1590 uloop_process_delete(&in
->proc
);
1591 uloop_timeout_cancel(&in
->timeout
);
1592 uloop_timeout_cancel(&in
->watchdog
.timeout
);
1595 instance_config_cleanup(in
);
1600 free(in
->overlaydir
);
1601 free(in
->tmpoverlaysize
);
1603 free(in
->jail
.name
);
1604 free(in
->jail
.hostname
);
1605 free(in
->jail
.pidfile
);
1607 free(in
->capabilities
);
1613 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
1615 config
= blob_memdup(config
);
1617 in
->name
= blobmsg_name(config
);
1618 in
->config
= config
;
1619 in
->timeout
.cb
= instance_timeout
;
1620 in
->proc
.cb
= instance_exit
;
1621 in
->term_timeout
= 5;
1622 in
->syslog_facility
= LOG_DAEMON
;
1624 in
->require_jail
= false;
1625 in
->immediately
= false;
1627 in
->_stdout
.fd
.fd
= -2;
1628 in
->_stdout
.stream
.string_data
= true;
1629 in
->_stdout
.stream
.notify_read
= instance_stdout
;
1631 in
->_stderr
.fd
.fd
= -2;
1632 in
->_stderr
.stream
.string_data
= true;
1633 in
->_stderr
.stream
.notify_read
= instance_stderr
;
1635 in
->console
.fd
.fd
= -2;
1636 in
->console
.stream
.string_data
= true;
1637 in
->console
.stream
.notify_read
= instance_console
;
1639 in
->console_client
.fd
.fd
= -2;
1640 in
->console_client
.stream
.string_data
= true;
1641 in
->console_client
.stream
.notify_read
= instance_console_client
;
1643 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
1644 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
1645 blobmsg_list_simple_init(&in
->env
);
1646 blobmsg_list_simple_init(&in
->data
);
1647 blobmsg_list_simple_init(&in
->limits
);
1648 blobmsg_list_simple_init(&in
->errors
);
1649 blobmsg_list_simple_init(&in
->jail
.mount
);
1650 blobmsg_list_simple_init(&in
->jail
.setns
);
1652 in
->watchdog
.timeout
.cb
= instance_watchdog
;
1654 in
->valid
= instance_config_parse(in
);
1657 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
1664 i
= blobmsg_open_table(b
, in
->name
);
1665 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
1666 if (in
->proc
.pending
)
1667 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
1669 blobmsg_add_blob(b
, in
->command
);
1671 blobmsg_add_string(b
, "bundle", in
->bundle
);
1672 blobmsg_add_u32(b
, "term_timeout", in
->term_timeout
);
1673 if (!in
->proc
.pending
)
1674 blobmsg_add_u32(b
, "exit_code", in
->exit_code
);
1676 if (!avl_is_empty(&in
->errors
.avl
)) {
1677 struct blobmsg_list_node
*var
;
1678 void *e
= blobmsg_open_array(b
, "errors");
1679 blobmsg_list_for_each(&in
->errors
, var
)
1680 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
1681 blobmsg_close_table(b
, e
);
1684 if (!avl_is_empty(&in
->env
.avl
)) {
1685 struct blobmsg_list_node
*var
;
1686 void *e
= blobmsg_open_table(b
, "env");
1687 blobmsg_list_for_each(&in
->env
, var
)
1688 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1689 blobmsg_close_table(b
, e
);
1692 if (!avl_is_empty(&in
->data
.avl
)) {
1693 struct blobmsg_list_node
*var
;
1694 void *e
= blobmsg_open_table(b
, "data");
1695 blobmsg_list_for_each(&in
->data
, var
)
1696 blobmsg_add_blob(b
, var
->data
);
1697 blobmsg_close_table(b
, e
);
1700 if (!avl_is_empty(&in
->limits
.avl
)) {
1701 struct blobmsg_list_node
*var
;
1702 void *e
= blobmsg_open_table(b
, "limits");
1703 blobmsg_list_for_each(&in
->limits
, var
)
1704 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1705 blobmsg_close_table(b
, e
);
1708 if (in
->reload_signal
)
1709 blobmsg_add_u32(b
, "reload_signal", in
->reload_signal
);
1712 void *r
= blobmsg_open_table(b
, "respawn");
1713 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
1714 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
1715 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
1716 blobmsg_close_table(b
, r
);
1720 blobmsg_add_u8(b
, "trace", true);
1722 if (in
->no_new_privs
)
1723 blobmsg_add_u8(b
, "no_new_privs", true);
1726 blobmsg_add_string(b
, "seccomp", in
->seccomp
);
1728 if (in
->capabilities
)
1729 blobmsg_add_string(b
, "capabilities", in
->capabilities
);
1732 blobmsg_add_string(b
, "pidfile", in
->pidfile
);
1735 blobmsg_add_string(b
, "user", in
->user
);
1738 blobmsg_add_string(b
, "group", in
->group
);
1741 void *r
= blobmsg_open_table(b
, "jail");
1743 blobmsg_add_string(b
, "name", in
->jail
.name
);
1745 if (in
->jail
.hostname
)
1746 blobmsg_add_string(b
, "hostname", in
->jail
.hostname
);
1748 blobmsg_add_u8(b
, "procfs", in
->jail
.procfs
);
1749 blobmsg_add_u8(b
, "sysfs", in
->jail
.sysfs
);
1750 blobmsg_add_u8(b
, "ubus", in
->jail
.ubus
);
1751 blobmsg_add_u8(b
, "log", in
->jail
.log
);
1752 blobmsg_add_u8(b
, "ronly", in
->jail
.ronly
);
1753 blobmsg_add_u8(b
, "netns", in
->jail
.netns
);
1754 blobmsg_add_u8(b
, "userns", in
->jail
.userns
);
1755 blobmsg_add_u8(b
, "cgroupsns", in
->jail
.cgroupsns
);
1757 if (in
->jail
.pidfile
)
1758 blobmsg_add_string(b
, "pidfile", in
->jail
.pidfile
);
1760 blobmsg_add_u8(b
, "immediately", in
->immediately
);
1762 blobmsg_add_u8(b
, "console", (in
->console
.fd
.fd
> -1));
1763 blobmsg_close_table(b
, r
);
1764 if (!avl_is_empty(&in
->jail
.mount
.avl
)) {
1765 struct blobmsg_list_node
*var
;
1766 void *e
= blobmsg_open_table(b
, "mount");
1767 blobmsg_list_for_each(&in
->jail
.mount
, var
)
1768 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1769 blobmsg_close_table(b
, e
);
1772 if (!avl_is_empty(&in
->jail
.setns
.avl
)) {
1773 struct blobmsg_list_node
*var
;
1774 void *s
= blobmsg_open_array(b
, "setns");
1775 blobmsg_list_for_each(&in
->jail
.setns
, var
)
1776 blobmsg_add_blob(b
, var
->data
);
1777 blobmsg_close_array(b
, s
);
1782 blobmsg_add_string(b
, "extroot", in
->extroot
);
1784 blobmsg_add_string(b
, "overlaydir", in
->overlaydir
);
1785 if (in
->tmpoverlaysize
)
1786 blobmsg_add_string(b
, "tmpoverlaysize", in
->tmpoverlaysize
);
1788 if (verbose
&& in
->trigger
)
1789 blobmsg_add_blob(b
, in
->trigger
);
1791 if (in
->watchdog
.mode
!= INSTANCE_WATCHDOG_MODE_DISABLED
) {
1792 void *r
= blobmsg_open_table(b
, "watchdog");
1793 blobmsg_add_u32(b
, "mode", in
->watchdog
.mode
);
1794 blobmsg_add_u32(b
, "timeout", in
->watchdog
.freq
);
1795 blobmsg_close_table(b
, r
);
1798 blobmsg_close_table(b
, i
);