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"
42 INSTANCE_ATTR_COMMAND
,
47 INSTANCE_ATTR_TRIGGER
,
48 INSTANCE_ATTR_RESPAWN
,
57 INSTANCE_ATTR_NO_NEW_PRIVS
,
60 INSTANCE_ATTR_SECCOMP
,
61 INSTANCE_ATTR_PIDFILE
,
62 INSTANCE_ATTR_RELOADSIG
,
63 INSTANCE_ATTR_TERMTIMEOUT
,
64 INSTANCE_ATTR_FACILITY
,
65 INSTANCE_ATTR_EXTROOT
,
66 INSTANCE_ATTR_OVERLAYDIR
,
67 INSTANCE_ATTR_TMPOVERLAYSIZE
,
69 INSTANCE_ATTR_WATCHDOG
,
73 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
74 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
75 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
76 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
77 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
78 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
79 [INSTANCE_ATTR_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
80 [INSTANCE_ATTR_RESPAWN
] = { "respawn", BLOBMSG_TYPE_ARRAY
},
81 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
82 [INSTANCE_ATTR_LIMITS
] = { "limits", BLOBMSG_TYPE_TABLE
},
83 [INSTANCE_ATTR_WATCH
] = { "watch", BLOBMSG_TYPE_ARRAY
},
84 [INSTANCE_ATTR_ERROR
] = { "error", BLOBMSG_TYPE_ARRAY
},
85 [INSTANCE_ATTR_USER
] = { "user", BLOBMSG_TYPE_STRING
},
86 [INSTANCE_ATTR_GROUP
] = { "group", BLOBMSG_TYPE_STRING
},
87 [INSTANCE_ATTR_STDOUT
] = { "stdout", BLOBMSG_TYPE_BOOL
},
88 [INSTANCE_ATTR_STDERR
] = { "stderr", BLOBMSG_TYPE_BOOL
},
89 [INSTANCE_ATTR_NO_NEW_PRIVS
] = { "no_new_privs", BLOBMSG_TYPE_BOOL
},
90 [INSTANCE_ATTR_JAIL
] = { "jail", BLOBMSG_TYPE_TABLE
},
91 [INSTANCE_ATTR_TRACE
] = { "trace", BLOBMSG_TYPE_BOOL
},
92 [INSTANCE_ATTR_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_STRING
},
93 [INSTANCE_ATTR_PIDFILE
] = { "pidfile", BLOBMSG_TYPE_STRING
},
94 [INSTANCE_ATTR_RELOADSIG
] = { "reload_signal", BLOBMSG_TYPE_INT32
},
95 [INSTANCE_ATTR_TERMTIMEOUT
] = { "term_timeout", BLOBMSG_TYPE_INT32
},
96 [INSTANCE_ATTR_FACILITY
] = { "facility", BLOBMSG_TYPE_STRING
},
97 [INSTANCE_ATTR_EXTROOT
] = { "extroot", BLOBMSG_TYPE_STRING
},
98 [INSTANCE_ATTR_OVERLAYDIR
] = { "overlaydir", BLOBMSG_TYPE_STRING
},
99 [INSTANCE_ATTR_TMPOVERLAYSIZE
] = { "tmpoverlaysize", BLOBMSG_TYPE_STRING
},
100 [INSTANCE_ATTR_BUNDLE
] = { "bundle", BLOBMSG_TYPE_STRING
},
101 [INSTANCE_ATTR_WATCHDOG
] = { "watchdog", BLOBMSG_TYPE_ARRAY
},
117 JAIL_ATTR_REQUIREJAIL
,
118 JAIL_ATTR_IMMEDIATELY
,
122 static const struct blobmsg_policy jail_attr
[__JAIL_ATTR_MAX
] = {
123 [JAIL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
124 [JAIL_ATTR_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
125 [JAIL_ATTR_PROCFS
] = { "procfs", BLOBMSG_TYPE_BOOL
},
126 [JAIL_ATTR_SYSFS
] = { "sysfs", BLOBMSG_TYPE_BOOL
},
127 [JAIL_ATTR_UBUS
] = { "ubus", BLOBMSG_TYPE_BOOL
},
128 [JAIL_ATTR_LOG
] = { "log", BLOBMSG_TYPE_BOOL
},
129 [JAIL_ATTR_RONLY
] = { "ronly", BLOBMSG_TYPE_BOOL
},
130 [JAIL_ATTR_MOUNT
] = { "mount", BLOBMSG_TYPE_TABLE
},
131 [JAIL_ATTR_NETNS
] = { "netns", BLOBMSG_TYPE_BOOL
},
132 [JAIL_ATTR_USERNS
] = { "userns", BLOBMSG_TYPE_BOOL
},
133 [JAIL_ATTR_CGROUPSNS
] = { "cgroupsns", BLOBMSG_TYPE_BOOL
},
134 [JAIL_ATTR_CONSOLE
] = { "console", BLOBMSG_TYPE_BOOL
},
135 [JAIL_ATTR_REQUIREJAIL
] = { "requirejail", BLOBMSG_TYPE_BOOL
},
136 [JAIL_ATTR_IMMEDIATELY
] = { "immediately", BLOBMSG_TYPE_BOOL
},
139 struct instance_netdev
{
140 struct blobmsg_list_node node
;
144 struct instance_file
{
145 struct blobmsg_list_node node
;
154 static const struct rlimit_name rlimit_names
[] = {
156 { "core", RLIMIT_CORE
},
157 { "cpu", RLIMIT_CPU
},
158 { "data", RLIMIT_DATA
},
159 { "fsize", RLIMIT_FSIZE
},
160 { "memlock", RLIMIT_MEMLOCK
},
161 { "nofile", RLIMIT_NOFILE
},
162 { "nproc", RLIMIT_NPROC
},
163 { "rss", RLIMIT_RSS
},
164 { "stack", RLIMIT_STACK
},
166 { "nice", RLIMIT_NICE
},
167 { "rtprio", RLIMIT_RTPRIO
},
168 { "msgqueue", RLIMIT_MSGQUEUE
},
169 { "sigpending", RLIMIT_SIGPENDING
},
174 static void closefd(int fd
)
176 if (fd
> STDERR_FILENO
)
180 /* convert a string into numeric syslog facility or return -1 if no match found */
182 syslog_facility_str_to_int(const char *facility
)
184 CODE
*p
= facilitynames
;
186 while (p
->c_name
&& strcasecmp(p
->c_name
, facility
))
193 instance_limits(const char *limit
, const char *value
)
197 unsigned long cur
, max
;
199 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
200 if (strcmp(rlimit_names
[i
].name
, limit
))
202 if (!strcmp(value
, "unlimited")) {
203 rlim
.rlim_cur
= RLIM_INFINITY
;
204 rlim
.rlim_max
= RLIM_INFINITY
;
206 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
212 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
219 setrlimit(rlimit_names
[i
].resource
, &rlim
);
225 jail_run(struct service_instance
*in
, char **argv
)
227 struct blobmsg_list_node
*var
;
228 struct jail
*jail
= &in
->jail
;
231 argv
[argc
++] = UJAIL_BIN_PATH
;
235 argv
[argc
++] = jail
->name
;
238 if (jail
->hostname
) {
240 argv
[argc
++] = jail
->hostname
;
245 argv
[argc
++] = in
->seccomp
;
250 argv
[argc
++] = in
->user
;
255 argv
[argc
++] = in
->group
;
258 if (in
->no_new_privs
)
290 argv
[argc
++] = in
->extroot
;
293 if (in
->overlaydir
) {
295 argv
[argc
++] = in
->overlaydir
;
298 if (in
->tmpoverlaysize
) {
300 argv
[argc
++] = in
->tmpoverlaysize
;
308 argv
[argc
++] = in
->bundle
;
311 if (in
->require_jail
)
314 blobmsg_list_for_each(&jail
->mount
, var
) {
315 const char *type
= blobmsg_data(var
->data
);
321 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
330 instance_removepid(struct service_instance
*in
) {
333 if (unlink(in
->pidfile
)) {
334 ERROR("Failed to remove pidfile: %s: %m\n", in
->pidfile
);
341 instance_writepid(struct service_instance
*in
)
348 _pidfile
= fopen(in
->pidfile
, "w");
349 if (_pidfile
== NULL
) {
350 ERROR("failed to open pidfile for writing: %s: %m", in
->pidfile
);
353 if (fprintf(_pidfile
, "%d\n", in
->proc
.pid
) < 0) {
354 ERROR("failed to write pidfile: %s: %m", in
->pidfile
);
358 if (fclose(_pidfile
)) {
359 ERROR("failed to close pidfile: %s: %m", in
->pidfile
);
367 instance_run(struct service_instance
*in
, int _stdout
, int _stderr
)
369 struct blobmsg_list_node
*var
;
370 struct blob_attr
*cur
;
372 int argc
= 1; /* NULL terminated */
374 bool seccomp
= !in
->trace
&& !in
->has_jail
&& in
->seccomp
;
375 bool setlbf
= _stdout
>= 0;
378 setpriority(PRIO_PROCESS
, 0, in
->nice
);
380 blobmsg_for_each_attr(cur
, in
->command
, rem
)
383 blobmsg_list_for_each(&in
->env
, var
)
384 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
387 setenv("SECCOMP_FILE", in
->seccomp
, 1);
390 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
392 blobmsg_list_for_each(&in
->limits
, var
)
393 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
395 if (in
->trace
|| seccomp
)
398 argv
= alloca(sizeof(char *) * (argc
+ in
->jail
.argc
));
401 #ifdef SECCOMP_SUPPORT
403 argv
[argc
++] = "/sbin/utrace";
405 argv
[argc
++] = "/sbin/seccomp-trace";
407 if (in
->trace
|| seccomp
)
408 ULOG_WARN("Seccomp support for %s::%s not available\n", in
->srv
->name
, in
->name
);
412 argc
= jail_run(in
, argv
);
413 if (argc
!= in
->jail
.argc
)
414 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
415 in
->jail
.argc
, argc
, in
->srv
->name
, in
->name
);
418 blobmsg_for_each_attr(cur
, in
->command
, rem
)
419 argv
[argc
++] = blobmsg_data(cur
);
423 _stdin
= open("/dev/null", O_RDONLY
);
426 _stdout
= open("/dev/null", O_WRONLY
);
429 _stderr
= open("/dev/null", O_WRONLY
);
432 dup2(_stdin
, STDIN_FILENO
);
436 dup2(_stdout
, STDOUT_FILENO
);
440 dup2(_stderr
, STDERR_FILENO
);
444 if (!in
->has_jail
&& in
->user
&& in
->pw_gid
&& initgroups(in
->user
, in
->pw_gid
)) {
445 ERROR("failed to initgroups() for user %s: %m\n", in
->user
);
448 if (!in
->has_jail
&& in
->gr_gid
&& setgid(in
->gr_gid
)) {
449 ERROR("failed to set group id %d: %m\n", in
->gr_gid
);
452 if (!in
->has_jail
&& in
->uid
&& setuid(in
->uid
)) {
453 ERROR("failed to set user id %d: %m\n", in
->uid
);
457 execvp(argv
[0], argv
);
462 instance_free_stdio(struct service_instance
*in
)
464 if (in
->_stdout
.fd
.fd
> -1) {
465 ustream_free(&in
->_stdout
.stream
);
466 close(in
->_stdout
.fd
.fd
);
467 in
->_stdout
.fd
.fd
= -1;
470 if (in
->_stderr
.fd
.fd
> -1) {
471 ustream_free(&in
->_stderr
.stream
);
472 close(in
->_stderr
.fd
.fd
);
473 in
->_stderr
.fd
.fd
= -1;
476 if (in
->console
.fd
.fd
> -1) {
477 ustream_free(&in
->console
.stream
);
478 close(in
->console
.fd
.fd
);
479 in
->console
.fd
.fd
= -1;
482 if (in
->console_client
.fd
.fd
> -1) {
483 ustream_free(&in
->console_client
.stream
);
484 close(in
->console_client
.fd
.fd
);
485 in
->console_client
.fd
.fd
= -1;
490 instance_start(struct service_instance
*in
)
493 int opipe
[2] = { -1, -1 };
494 int epipe
[2] = { -1, -1 };
496 if (!avl_is_empty(&in
->errors
.avl
)) {
497 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
501 if (!in
->bundle
&& !in
->command
) {
502 LOG("Not starting instance %s::%s, command not set\n", in
->srv
->name
, in
->name
);
506 if (in
->proc
.pending
) {
512 instance_free_stdio(in
);
513 if (in
->_stdout
.fd
.fd
> -2) {
515 ULOG_WARN("pipe() failed: %m\n");
516 opipe
[0] = opipe
[1] = -1;
520 if (in
->_stderr
.fd
.fd
> -2) {
522 ULOG_WARN("pipe() failed: %m\n");
523 epipe
[0] = epipe
[1] = -1;
541 instance_run(in
, opipe
[1], epipe
[1]);
545 DEBUG(2, "Started instance %s::%s[%d]\n", in
->srv
->name
, in
->name
, pid
);
547 instance_writepid(in
);
548 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
549 uloop_process_add(&in
->proc
);
552 ustream_fd_init(&in
->_stdout
, opipe
[0]);
554 fcntl(opipe
[0], F_SETFD
, FD_CLOEXEC
);
558 ustream_fd_init(&in
->_stderr
, epipe
[0]);
560 fcntl(epipe
[0], F_SETFD
, FD_CLOEXEC
);
563 if (in
->watchdog
.mode
!= INSTANCE_WATCHDOG_MODE_DISABLED
) {
564 uloop_timeout_set(&in
->watchdog
.timeout
, in
->watchdog
.freq
* 1000);
565 DEBUG(2, "Started instance %s::%s watchdog timer : timeout = %d\n", in
->srv
->name
, in
->name
, in
->watchdog
.freq
);
568 service_event("instance.start", in
->srv
->name
, in
->name
);
572 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
574 char *newline
, *str
, *arg0
, ident
[32];
577 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
578 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
579 ulog_open(ULOG_SYSLOG
, in
->syslog_facility
, ident
);
582 str
= ustream_get_read_buf(s
, &len
);
586 newline
= memchr(str
, '\n', len
);
587 if (!newline
&& (s
->r
.buffer_len
!= len
))
592 len
= newline
+ 1 - str
;
594 ulog(prio
, "%s\n", str
);
596 ustream_consume(s
, len
);
599 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
603 instance_stdout(struct ustream
*s
, int bytes
)
605 instance_stdio(s
, LOG_INFO
,
606 container_of(s
, struct service_instance
, _stdout
.stream
));
610 instance_console(struct ustream
*s
, int bytes
)
612 struct service_instance
*in
= container_of(s
, struct service_instance
, console
.stream
);
617 buf
= ustream_get_read_buf(s
, &len
);
621 ulog(LOG_INFO
, "out: %s\n", buf
);
623 /* test if console client is attached */
624 if (in
->console_client
.fd
.fd
> -1)
625 ustream_write(&in
->console_client
.stream
, buf
, len
, false);
627 ustream_consume(s
, len
);
632 instance_console_client(struct ustream
*s
, int bytes
)
634 struct service_instance
*in
= container_of(s
, struct service_instance
, console_client
.stream
);
639 buf
= ustream_get_read_buf(s
, &len
);
643 ulog(LOG_INFO
, "in: %s\n", buf
);
644 ustream_write(&in
->console
.stream
, buf
, len
, false);
645 ustream_consume(s
, len
);
650 instance_stderr(struct ustream
*s
, int bytes
)
652 instance_stdio(s
, LOG_ERR
,
653 container_of(s
, struct service_instance
, _stderr
.stream
));
657 instance_timeout(struct uloop_timeout
*t
)
659 struct service_instance
*in
;
661 in
= container_of(t
, struct service_instance
, timeout
);
664 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
665 in
->srv
->name
, in
->name
, in
->proc
.pid
);
666 kill(in
->proc
.pid
, SIGKILL
);
667 } else if (in
->restart
|| in
->respawn
)
672 instance_delete(struct service_instance
*in
)
674 struct service
*s
= in
->srv
;
676 avl_delete(&s
->instances
.avl
, &in
->node
.avl
);
682 instance_exit_code(int ret
)
684 if (WIFEXITED(ret
)) {
685 return WEXITSTATUS(ret
);
688 if (WIFSIGNALED(ret
)) {
689 return SIGNALLED_OFFSET
+ WTERMSIG(ret
);
692 if (WIFSTOPPED(ret
)) {
693 return WSTOPSIG(ret
);
700 instance_exit(struct uloop_process
*p
, int ret
)
702 struct service_instance
*in
;
706 in
= container_of(p
, struct service_instance
, proc
);
708 clock_gettime(CLOCK_MONOTONIC
, &tp
);
709 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
711 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
713 in
->exit_code
= instance_exit_code(ret
);
714 uloop_timeout_cancel(&in
->timeout
);
715 uloop_timeout_cancel(&in
->watchdog
.timeout
);
716 service_event("instance.stop", in
->srv
->name
, in
->name
);
719 instance_removepid(in
);
724 } else if (in
->restart
) {
726 } else if (in
->respawn
) {
727 if (runtime
< in
->respawn_threshold
)
730 in
->respawn_count
= 0;
731 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
732 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
733 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
734 in
->restart
= in
->respawn
= 0;
736 service_event("instance.fail", in
->srv
->name
, in
->name
);
738 service_event("instance.respawn", in
->srv
->name
, in
->name
);
739 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
745 instance_stop(struct service_instance
*in
, bool halt
)
747 if (!in
->proc
.pending
) {
753 in
->restart
= in
->respawn
= false;
754 kill(in
->proc
.pid
, SIGTERM
);
755 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
759 instance_restart(struct service_instance
*in
)
761 if (!in
->proc
.pending
)
764 if (in
->reload_signal
) {
765 kill(in
->proc
.pid
, in
->reload_signal
);
771 kill(in
->proc
.pid
, SIGTERM
);
772 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
776 instance_watchdog(struct uloop_timeout
*t
)
778 struct service_instance
*in
= container_of(t
, struct service_instance
, watchdog
.timeout
);
780 DEBUG(3, "instance %s::%s watchdog timer expired\n", in
->srv
->name
, in
->name
);
783 instance_restart(in
);
785 instance_stop(in
, true);
788 static bool string_changed(const char *a
, const char *b
)
790 return !((!a
&& !b
) || (a
&& b
&& !strcmp(a
, b
)));
794 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
799 if (!blob_attr_equal(in
->command
, in_new
->command
))
802 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
805 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
808 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
811 if (in
->nice
!= in_new
->nice
)
814 if (in
->syslog_facility
!= in_new
->syslog_facility
)
817 if (string_changed(in
->user
, in_new
->user
))
820 if (string_changed(in
->group
, in_new
->group
))
823 if (in
->uid
!= in_new
->uid
)
826 if (in
->pw_gid
!= in_new
->pw_gid
)
829 if (string_changed(in
->pidfile
, in_new
->pidfile
))
832 if (in
->respawn_retry
!= in_new
->respawn_retry
)
834 if (in
->respawn_threshold
!= in_new
->respawn_threshold
)
836 if (in
->respawn_timeout
!= in_new
->respawn_timeout
)
838 if (in
->bundle
&& in_new
->bundle
&& strcmp(in
->bundle
, in_new
->bundle
))
840 if ((!in
->seccomp
&& in_new
->seccomp
) ||
841 (in
->seccomp
&& !in_new
->seccomp
) ||
842 (in
->seccomp
&& in_new
->seccomp
&& strcmp(in
->seccomp
, in_new
->seccomp
)))
845 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
848 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
851 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
854 if (in
->watchdog
.mode
!= in_new
->watchdog
.mode
)
857 if (in
->watchdog
.freq
!= in_new
->watchdog
.freq
)
864 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
866 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
867 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
869 return n1
->ifindex
== n2
->ifindex
;
873 instance_netdev_update(struct blobmsg_list_node
*l
)
875 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
877 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
881 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
883 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
884 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
886 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
890 instance_file_update(struct blobmsg_list_node
*l
)
892 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
897 memset(f
->md5
, 0, sizeof(f
->md5
));
899 fd
= open(l
->avl
.key
, O_RDONLY
);
905 len
= read(fd
, buf
, sizeof(buf
));
915 md5_hash(buf
, len
, &md5
);
918 md5_end(f
->md5
, &md5
);
923 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
928 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
932 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
934 struct blobmsg_list_node
*node
;
939 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
942 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
944 blobmsg_list_for_each(l
, node
)
951 instance_jail_parse(struct service_instance
*in
, struct blob_attr
*attr
)
953 struct blob_attr
*tb
[__JAIL_ATTR_MAX
];
954 struct jail
*jail
= &in
->jail
;
956 blobmsg_parse(jail_attr
, __JAIL_ATTR_MAX
, tb
,
957 blobmsg_data(attr
), blobmsg_data_len(attr
));
961 if (tb
[JAIL_ATTR_REQUIREJAIL
]) {
962 in
->require_jail
= true;
965 if (tb
[JAIL_ATTR_IMMEDIATELY
] && blobmsg_get_bool(tb
[JAIL_ATTR_IMMEDIATELY
])) {
966 in
->immediately
= true;
969 if (tb
[JAIL_ATTR_NAME
]) {
970 jail
->name
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_NAME
]));
973 if (tb
[JAIL_ATTR_HOSTNAME
]) {
974 jail
->hostname
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_HOSTNAME
]));
977 if (tb
[JAIL_ATTR_PROCFS
]) {
978 jail
->procfs
= blobmsg_get_bool(tb
[JAIL_ATTR_PROCFS
]);
981 if (tb
[JAIL_ATTR_SYSFS
]) {
982 jail
->sysfs
= blobmsg_get_bool(tb
[JAIL_ATTR_SYSFS
]);
985 if (tb
[JAIL_ATTR_UBUS
]) {
986 jail
->ubus
= blobmsg_get_bool(tb
[JAIL_ATTR_UBUS
]);
989 if (tb
[JAIL_ATTR_LOG
]) {
990 jail
->log
= blobmsg_get_bool(tb
[JAIL_ATTR_LOG
]);
993 if (tb
[JAIL_ATTR_RONLY
]) {
994 jail
->ronly
= blobmsg_get_bool(tb
[JAIL_ATTR_RONLY
]);
997 if (tb
[JAIL_ATTR_NETNS
]) {
998 jail
->netns
= blobmsg_get_bool(tb
[JAIL_ATTR_NETNS
]);
1001 if (tb
[JAIL_ATTR_USERNS
]) {
1002 jail
->userns
= blobmsg_get_bool(tb
[JAIL_ATTR_USERNS
]);
1005 if (tb
[JAIL_ATTR_CGROUPSNS
]) {
1006 jail
->cgroupsns
= blobmsg_get_bool(tb
[JAIL_ATTR_CGROUPSNS
]);
1009 if (tb
[JAIL_ATTR_CONSOLE
]) {
1010 jail
->console
= blobmsg_get_bool(tb
[JAIL_ATTR_CONSOLE
]);
1014 if (tb
[JAIL_ATTR_MOUNT
]) {
1015 struct blob_attr
*cur
;
1018 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_MOUNT
], rem
)
1020 instance_fill_array(&jail
->mount
, tb
[JAIL_ATTR_MOUNT
], NULL
, false);
1037 if (in
->tmpoverlaysize
)
1040 if (in
->no_new_privs
)
1050 instance_config_parse_command(struct service_instance
*in
, struct blob_attr
**tb
)
1052 struct blob_attr
*cur
, *cur2
;
1056 cur
= tb
[INSTANCE_ATTR_COMMAND
];
1062 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
1065 blobmsg_for_each_attr(cur2
, cur
, rem
) {
1075 instance_config_parse(struct service_instance
*in
)
1077 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
1078 struct blob_attr
*cur
, *cur2
;
1082 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
1083 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
1085 if (!tb
[INSTANCE_ATTR_BUNDLE
] && !instance_config_parse_command(in
, tb
))
1088 if (tb
[INSTANCE_ATTR_TERMTIMEOUT
])
1089 in
->term_timeout
= blobmsg_get_u32(tb
[INSTANCE_ATTR_TERMTIMEOUT
]);
1090 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
1092 uint32_t vals
[3] = { 3600, 5, 5};
1094 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
1095 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
1097 vals
[i
] = atoi(blobmsg_get_string(cur2
));
1101 in
->respawn_count
= 0;
1102 in
->respawn_threshold
= vals
[0];
1103 in
->respawn_timeout
= vals
[1];
1104 in
->respawn_retry
= vals
[2];
1106 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
1107 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
1108 trigger_add(in
->trigger
, in
);
1111 if (tb
[INSTANCE_ATTR_WATCH
]) {
1112 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
1113 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
1115 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
1116 watch_add(blobmsg_get_string(cur2
), in
);
1120 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
1121 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
1122 if (in
->nice
< -20 || in
->nice
> 20)
1126 if (tb
[INSTANCE_ATTR_USER
]) {
1127 const char *user
= blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]);
1128 struct passwd
*p
= getpwnam(user
);
1130 in
->user
= strdup(user
);
1131 in
->uid
= p
->pw_uid
;
1132 in
->gr_gid
= in
->pw_gid
= p
->pw_gid
;
1136 if (tb
[INSTANCE_ATTR_GROUP
]) {
1137 const char *group
= blobmsg_get_string(tb
[INSTANCE_ATTR_GROUP
]);
1138 struct group
*p
= getgrnam(group
);
1140 in
->group
= strdup(group
);
1141 in
->gr_gid
= p
->gr_gid
;
1145 if (tb
[INSTANCE_ATTR_TRACE
])
1146 in
->trace
= blobmsg_get_bool(tb
[INSTANCE_ATTR_TRACE
]);
1148 if (tb
[INSTANCE_ATTR_NO_NEW_PRIVS
])
1149 in
->no_new_privs
= blobmsg_get_bool(tb
[INSTANCE_ATTR_NO_NEW_PRIVS
]);
1151 if (!in
->trace
&& tb
[INSTANCE_ATTR_SECCOMP
])
1152 in
->seccomp
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_SECCOMP
]));
1154 if (tb
[INSTANCE_ATTR_EXTROOT
])
1155 in
->extroot
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_EXTROOT
]));
1157 if (tb
[INSTANCE_ATTR_OVERLAYDIR
])
1158 in
->overlaydir
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_OVERLAYDIR
]));
1160 if (tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
])
1161 in
->tmpoverlaysize
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
]));
1163 if (tb
[INSTANCE_ATTR_BUNDLE
])
1164 in
->bundle
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_BUNDLE
]));
1166 if (tb
[INSTANCE_ATTR_PIDFILE
]) {
1167 char *pidfile
= blobmsg_get_string(tb
[INSTANCE_ATTR_PIDFILE
]);
1169 in
->pidfile
= strdup(pidfile
);
1172 if (tb
[INSTANCE_ATTR_RELOADSIG
])
1173 in
->reload_signal
= blobmsg_get_u32(tb
[INSTANCE_ATTR_RELOADSIG
]);
1175 if (!in
->trace
&& tb
[INSTANCE_ATTR_JAIL
])
1176 in
->has_jail
= instance_jail_parse(in
, tb
[INSTANCE_ATTR_JAIL
]);
1179 r
= stat(UJAIL_BIN_PATH
, &s
);
1181 if (in
->require_jail
) {
1182 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1183 in
->srv
->name
, in
->name
, UJAIL_BIN_PATH
, r
);
1186 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH
, r
);
1187 in
->has_jail
= false;
1191 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
1192 in
->_stdout
.fd
.fd
= -1;
1194 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
1195 in
->_stderr
.fd
.fd
= -1;
1197 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
1199 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
1202 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
1205 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
1208 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
1211 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
1214 if (tb
[INSTANCE_ATTR_FACILITY
]) {
1215 int facility
= syslog_facility_str_to_int(blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1216 if (facility
!= -1) {
1217 in
->syslog_facility
= facility
;
1218 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1220 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1223 if (tb
[INSTANCE_ATTR_WATCHDOG
]) {
1225 uint32_t vals
[2] = { 0, 30 };
1227 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCHDOG
], rem
) {
1231 vals
[i
] = atoi(blobmsg_get_string(cur2
));
1235 if (vals
[0] >= 0 && vals
[0] < __INSTANCE_WATCHDOG_MODE_MAX
) {
1236 in
->watchdog
.mode
= vals
[0];
1237 DEBUG(3, "setting watchdog mode (%d)\n", vals
[0]);
1239 in
->watchdog
.mode
= 0;
1240 DEBUG(3, "unknown watchdog mode (%d) given, using default (0)\n", vals
[0]);
1244 in
->watchdog
.freq
= vals
[1];
1245 DEBUG(3, "setting watchdog timeout (%d)\n", vals
[0]);
1247 in
->watchdog
.freq
= 30;
1248 DEBUG(3, "invalid watchdog timeout (%d) given, using default (30)\n", vals
[1]);
1256 instance_config_cleanup(struct service_instance
*in
)
1258 blobmsg_list_free(&in
->env
);
1259 blobmsg_list_free(&in
->data
);
1260 blobmsg_list_free(&in
->netdev
);
1261 blobmsg_list_free(&in
->file
);
1262 blobmsg_list_free(&in
->limits
);
1263 blobmsg_list_free(&in
->errors
);
1264 blobmsg_list_free(&in
->jail
.mount
);
1268 instance_config_move_strdup(char **dst
, char *src
)
1282 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
1284 instance_config_cleanup(in
);
1285 blobmsg_list_move(&in
->env
, &in_src
->env
);
1286 blobmsg_list_move(&in
->data
, &in_src
->data
);
1287 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
1288 blobmsg_list_move(&in
->file
, &in_src
->file
);
1289 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
1290 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
1291 blobmsg_list_move(&in
->jail
.mount
, &in_src
->jail
.mount
);
1292 in
->trigger
= in_src
->trigger
;
1293 in
->command
= in_src
->command
;
1294 in
->respawn
= in_src
->respawn
;
1295 in
->respawn_retry
= in_src
->respawn_retry
;
1296 in
->respawn_threshold
= in_src
->respawn_threshold
;
1297 in
->respawn_timeout
= in_src
->respawn_timeout
;
1298 in
->name
= in_src
->name
;
1299 in
->trace
= in_src
->trace
;
1300 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
1301 in
->syslog_facility
= in_src
->syslog_facility
;
1303 instance_config_move_strdup(&in
->pidfile
, in_src
->pidfile
);
1304 instance_config_move_strdup(&in
->seccomp
, in_src
->seccomp
);
1305 instance_config_move_strdup(&in
->jail
.name
, in_src
->jail
.name
);
1306 instance_config_move_strdup(&in
->jail
.hostname
, in_src
->jail
.hostname
);
1309 in
->config
= in_src
->config
;
1310 in_src
->config
= NULL
;
1314 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
1316 bool changed
= instance_config_changed(in
, in_new
);
1317 bool running
= in
->proc
.pending
;
1318 bool stopping
= in
->halt
;
1320 if (!running
|| stopping
) {
1321 instance_config_move(in
, in_new
);
1325 instance_restart(in
);
1326 instance_config_move(in
, in_new
);
1327 /* restart happens in the child callback handler */
1332 instance_free(struct service_instance
*in
)
1334 instance_free_stdio(in
);
1335 uloop_process_delete(&in
->proc
);
1336 uloop_timeout_cancel(&in
->timeout
);
1337 uloop_timeout_cancel(&in
->watchdog
.timeout
);
1340 instance_config_cleanup(in
);
1345 free(in
->overlaydir
);
1346 free(in
->tmpoverlaysize
);
1348 free(in
->jail
.name
);
1349 free(in
->jail
.hostname
);
1356 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
1358 config
= blob_memdup(config
);
1360 in
->name
= blobmsg_name(config
);
1361 in
->config
= config
;
1362 in
->timeout
.cb
= instance_timeout
;
1363 in
->proc
.cb
= instance_exit
;
1364 in
->term_timeout
= 5;
1365 in
->syslog_facility
= LOG_DAEMON
;
1367 in
->require_jail
= false;
1368 in
->immediately
= false;
1370 in
->_stdout
.fd
.fd
= -2;
1371 in
->_stdout
.stream
.string_data
= true;
1372 in
->_stdout
.stream
.notify_read
= instance_stdout
;
1374 in
->_stderr
.fd
.fd
= -2;
1375 in
->_stderr
.stream
.string_data
= true;
1376 in
->_stderr
.stream
.notify_read
= instance_stderr
;
1378 in
->console
.fd
.fd
= -2;
1379 in
->console
.stream
.string_data
= true;
1380 in
->console
.stream
.notify_read
= instance_console
;
1382 in
->console_client
.fd
.fd
= -2;
1383 in
->console_client
.stream
.string_data
= true;
1384 in
->console_client
.stream
.notify_read
= instance_console_client
;
1386 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
1387 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
1388 blobmsg_list_simple_init(&in
->env
);
1389 blobmsg_list_simple_init(&in
->data
);
1390 blobmsg_list_simple_init(&in
->limits
);
1391 blobmsg_list_simple_init(&in
->errors
);
1392 blobmsg_list_simple_init(&in
->jail
.mount
);
1394 in
->watchdog
.timeout
.cb
= instance_watchdog
;
1396 in
->valid
= instance_config_parse(in
);
1399 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
1406 i
= blobmsg_open_table(b
, in
->name
);
1407 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
1408 if (in
->proc
.pending
)
1409 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
1411 blobmsg_add_blob(b
, in
->command
);
1413 blobmsg_add_string(b
, "bundle", in
->bundle
);
1414 blobmsg_add_u32(b
, "term_timeout", in
->term_timeout
);
1415 if (!in
->proc
.pending
)
1416 blobmsg_add_u32(b
, "exit_code", in
->exit_code
);
1418 if (!avl_is_empty(&in
->errors
.avl
)) {
1419 struct blobmsg_list_node
*var
;
1420 void *e
= blobmsg_open_array(b
, "errors");
1421 blobmsg_list_for_each(&in
->errors
, var
)
1422 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
1423 blobmsg_close_table(b
, e
);
1426 if (!avl_is_empty(&in
->env
.avl
)) {
1427 struct blobmsg_list_node
*var
;
1428 void *e
= blobmsg_open_table(b
, "env");
1429 blobmsg_list_for_each(&in
->env
, var
)
1430 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1431 blobmsg_close_table(b
, e
);
1434 if (!avl_is_empty(&in
->data
.avl
)) {
1435 struct blobmsg_list_node
*var
;
1436 void *e
= blobmsg_open_table(b
, "data");
1437 blobmsg_list_for_each(&in
->data
, var
)
1438 blobmsg_add_blob(b
, var
->data
);
1439 blobmsg_close_table(b
, e
);
1442 if (!avl_is_empty(&in
->limits
.avl
)) {
1443 struct blobmsg_list_node
*var
;
1444 void *e
= blobmsg_open_table(b
, "limits");
1445 blobmsg_list_for_each(&in
->limits
, var
)
1446 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1447 blobmsg_close_table(b
, e
);
1450 if (in
->reload_signal
)
1451 blobmsg_add_u32(b
, "reload_signal", in
->reload_signal
);
1454 void *r
= blobmsg_open_table(b
, "respawn");
1455 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
1456 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
1457 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
1458 blobmsg_close_table(b
, r
);
1462 blobmsg_add_u8(b
, "trace", true);
1464 if (in
->no_new_privs
)
1465 blobmsg_add_u8(b
, "no_new_privs", true);
1468 blobmsg_add_string(b
, "seccomp", in
->seccomp
);
1471 blobmsg_add_string(b
, "pidfile", in
->pidfile
);
1474 blobmsg_add_string(b
, "user", in
->user
);
1477 blobmsg_add_string(b
, "group", in
->group
);
1480 void *r
= blobmsg_open_table(b
, "jail");
1482 blobmsg_add_string(b
, "name", in
->jail
.name
);
1484 if (in
->jail
.hostname
)
1485 blobmsg_add_string(b
, "hostname", in
->jail
.hostname
);
1487 blobmsg_add_u8(b
, "procfs", in
->jail
.procfs
);
1488 blobmsg_add_u8(b
, "sysfs", in
->jail
.sysfs
);
1489 blobmsg_add_u8(b
, "ubus", in
->jail
.ubus
);
1490 blobmsg_add_u8(b
, "log", in
->jail
.log
);
1491 blobmsg_add_u8(b
, "ronly", in
->jail
.ronly
);
1492 blobmsg_add_u8(b
, "netns", in
->jail
.netns
);
1493 blobmsg_add_u8(b
, "userns", in
->jail
.userns
);
1494 blobmsg_add_u8(b
, "cgroupsns", in
->jail
.cgroupsns
);
1496 blobmsg_add_u8(b
, "console", (in
->console
.fd
.fd
> -1));
1497 blobmsg_close_table(b
, r
);
1498 if (!avl_is_empty(&in
->jail
.mount
.avl
)) {
1499 struct blobmsg_list_node
*var
;
1500 void *e
= blobmsg_open_table(b
, "mount");
1501 blobmsg_list_for_each(&in
->jail
.mount
, var
)
1502 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1503 blobmsg_close_table(b
, e
);
1508 blobmsg_add_string(b
, "extroot", in
->extroot
);
1510 blobmsg_add_string(b
, "overlaydir", in
->overlaydir
);
1511 if (in
->tmpoverlaysize
)
1512 blobmsg_add_string(b
, "tmpoverlaysize", in
->tmpoverlaysize
);
1514 if (verbose
&& in
->trigger
)
1515 blobmsg_add_blob(b
, in
->trigger
);
1517 if (in
->watchdog
.mode
!= INSTANCE_WATCHDOG_MODE_DISABLED
) {
1518 void *r
= blobmsg_open_table(b
, "watchdog");
1519 blobmsg_add_u32(b
, "mode", in
->watchdog
.mode
);
1520 blobmsg_add_u32(b
, "timeout", in
->watchdog
.freq
);
1521 blobmsg_close_table(b
, r
);
1524 blobmsg_close_table(b
, i
);