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
,
72 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
73 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
74 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
75 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
76 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
77 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
78 [INSTANCE_ATTR_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
79 [INSTANCE_ATTR_RESPAWN
] = { "respawn", BLOBMSG_TYPE_ARRAY
},
80 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
81 [INSTANCE_ATTR_LIMITS
] = { "limits", BLOBMSG_TYPE_TABLE
},
82 [INSTANCE_ATTR_WATCH
] = { "watch", BLOBMSG_TYPE_ARRAY
},
83 [INSTANCE_ATTR_ERROR
] = { "error", BLOBMSG_TYPE_ARRAY
},
84 [INSTANCE_ATTR_USER
] = { "user", BLOBMSG_TYPE_STRING
},
85 [INSTANCE_ATTR_GROUP
] = { "group", BLOBMSG_TYPE_STRING
},
86 [INSTANCE_ATTR_STDOUT
] = { "stdout", BLOBMSG_TYPE_BOOL
},
87 [INSTANCE_ATTR_STDERR
] = { "stderr", BLOBMSG_TYPE_BOOL
},
88 [INSTANCE_ATTR_NO_NEW_PRIVS
] = { "no_new_privs", BLOBMSG_TYPE_BOOL
},
89 [INSTANCE_ATTR_JAIL
] = { "jail", BLOBMSG_TYPE_TABLE
},
90 [INSTANCE_ATTR_TRACE
] = { "trace", BLOBMSG_TYPE_BOOL
},
91 [INSTANCE_ATTR_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_STRING
},
92 [INSTANCE_ATTR_PIDFILE
] = { "pidfile", BLOBMSG_TYPE_STRING
},
93 [INSTANCE_ATTR_RELOADSIG
] = { "reload_signal", BLOBMSG_TYPE_INT32
},
94 [INSTANCE_ATTR_TERMTIMEOUT
] = { "term_timeout", BLOBMSG_TYPE_INT32
},
95 [INSTANCE_ATTR_FACILITY
] = { "facility", BLOBMSG_TYPE_STRING
},
96 [INSTANCE_ATTR_EXTROOT
] = { "extroot", BLOBMSG_TYPE_STRING
},
97 [INSTANCE_ATTR_OVERLAYDIR
] = { "overlaydir", BLOBMSG_TYPE_STRING
},
98 [INSTANCE_ATTR_TMPOVERLAYSIZE
] = { "tmpoverlaysize", BLOBMSG_TYPE_STRING
},
99 [INSTANCE_ATTR_BUNDLE
] = { "bundle", BLOBMSG_TYPE_STRING
},
115 JAIL_ATTR_REQUIREJAIL
,
119 static const struct blobmsg_policy jail_attr
[__JAIL_ATTR_MAX
] = {
120 [JAIL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
121 [JAIL_ATTR_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
122 [JAIL_ATTR_PROCFS
] = { "procfs", BLOBMSG_TYPE_BOOL
},
123 [JAIL_ATTR_SYSFS
] = { "sysfs", BLOBMSG_TYPE_BOOL
},
124 [JAIL_ATTR_UBUS
] = { "ubus", BLOBMSG_TYPE_BOOL
},
125 [JAIL_ATTR_LOG
] = { "log", BLOBMSG_TYPE_BOOL
},
126 [JAIL_ATTR_RONLY
] = { "ronly", BLOBMSG_TYPE_BOOL
},
127 [JAIL_ATTR_MOUNT
] = { "mount", BLOBMSG_TYPE_TABLE
},
128 [JAIL_ATTR_NETNS
] = { "netns", BLOBMSG_TYPE_BOOL
},
129 [JAIL_ATTR_USERNS
] = { "userns", BLOBMSG_TYPE_BOOL
},
130 [JAIL_ATTR_CGROUPSNS
] = { "cgroupsns", BLOBMSG_TYPE_BOOL
},
131 [JAIL_ATTR_CONSOLE
] = { "console", BLOBMSG_TYPE_BOOL
},
132 [JAIL_ATTR_REQUIREJAIL
] = { "requirejail", BLOBMSG_TYPE_BOOL
},
135 struct instance_netdev
{
136 struct blobmsg_list_node node
;
140 struct instance_file
{
141 struct blobmsg_list_node node
;
150 static const struct rlimit_name rlimit_names
[] = {
152 { "core", RLIMIT_CORE
},
153 { "cpu", RLIMIT_CPU
},
154 { "data", RLIMIT_DATA
},
155 { "fsize", RLIMIT_FSIZE
},
156 { "memlock", RLIMIT_MEMLOCK
},
157 { "nofile", RLIMIT_NOFILE
},
158 { "nproc", RLIMIT_NPROC
},
159 { "rss", RLIMIT_RSS
},
160 { "stack", RLIMIT_STACK
},
162 { "nice", RLIMIT_NICE
},
163 { "rtprio", RLIMIT_RTPRIO
},
164 { "msgqueue", RLIMIT_MSGQUEUE
},
165 { "sigpending", RLIMIT_SIGPENDING
},
170 static void closefd(int fd
)
172 if (fd
> STDERR_FILENO
)
176 /* convert a string into numeric syslog facility or return -1 if no match found */
178 syslog_facility_str_to_int(const char *facility
)
180 CODE
*p
= facilitynames
;
182 while (p
->c_name
&& strcasecmp(p
->c_name
, facility
))
189 instance_limits(const char *limit
, const char *value
)
193 unsigned long cur
, max
;
195 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
196 if (strcmp(rlimit_names
[i
].name
, limit
))
198 if (!strcmp(value
, "unlimited")) {
199 rlim
.rlim_cur
= RLIM_INFINITY
;
200 rlim
.rlim_max
= RLIM_INFINITY
;
202 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
208 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
215 setrlimit(rlimit_names
[i
].resource
, &rlim
);
221 jail_run(struct service_instance
*in
, char **argv
)
223 struct blobmsg_list_node
*var
;
224 struct jail
*jail
= &in
->jail
;
227 argv
[argc
++] = UJAIL_BIN_PATH
;
231 argv
[argc
++] = jail
->name
;
234 if (jail
->hostname
) {
236 argv
[argc
++] = jail
->hostname
;
241 argv
[argc
++] = in
->seccomp
;
246 argv
[argc
++] = in
->user
;
251 argv
[argc
++] = in
->group
;
254 if (in
->no_new_privs
)
286 argv
[argc
++] = in
->extroot
;
289 if (in
->overlaydir
) {
291 argv
[argc
++] = in
->overlaydir
;
294 if (in
->tmpoverlaysize
) {
296 argv
[argc
++] = in
->tmpoverlaysize
;
301 argv
[argc
++] = in
->bundle
;
304 if (in
->require_jail
)
307 blobmsg_list_for_each(&jail
->mount
, var
) {
308 const char *type
= blobmsg_data(var
->data
);
314 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
323 instance_removepid(struct service_instance
*in
) {
326 if (unlink(in
->pidfile
)) {
327 ERROR("Failed to remove pidfile: %s: %m\n", in
->pidfile
);
334 instance_writepid(struct service_instance
*in
)
341 _pidfile
= fopen(in
->pidfile
, "w");
342 if (_pidfile
== NULL
) {
343 ERROR("failed to open pidfile for writing: %s: %m", in
->pidfile
);
346 if (fprintf(_pidfile
, "%d\n", in
->proc
.pid
) < 0) {
347 ERROR("failed to write pidfile: %s: %m", in
->pidfile
);
351 if (fclose(_pidfile
)) {
352 ERROR("failed to close pidfile: %s: %m", in
->pidfile
);
360 instance_run(struct service_instance
*in
, int _stdout
, int _stderr
)
362 struct blobmsg_list_node
*var
;
363 struct blob_attr
*cur
;
365 int argc
= 1; /* NULL terminated */
367 bool seccomp
= !in
->trace
&& !in
->has_jail
&& in
->seccomp
;
368 bool setlbf
= _stdout
>= 0;
371 setpriority(PRIO_PROCESS
, 0, in
->nice
);
373 blobmsg_for_each_attr(cur
, in
->command
, rem
)
376 blobmsg_list_for_each(&in
->env
, var
)
377 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
380 setenv("SECCOMP_FILE", in
->seccomp
, 1);
383 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
385 blobmsg_list_for_each(&in
->limits
, var
)
386 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
388 if (in
->trace
|| seccomp
)
391 argv
= alloca(sizeof(char *) * (argc
+ in
->jail
.argc
));
394 #ifdef SECCOMP_SUPPORT
396 argv
[argc
++] = "/sbin/utrace";
398 argv
[argc
++] = "/sbin/seccomp-trace";
400 if (in
->trace
|| seccomp
)
401 ULOG_WARN("Seccomp support for %s::%s not available\n", in
->srv
->name
, in
->name
);
405 argc
= jail_run(in
, argv
);
406 if (argc
!= in
->jail
.argc
)
407 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
408 in
->jail
.argc
, argc
, in
->srv
->name
, in
->name
);
411 blobmsg_for_each_attr(cur
, in
->command
, rem
)
412 argv
[argc
++] = blobmsg_data(cur
);
416 _stdin
= open("/dev/null", O_RDONLY
);
419 _stdout
= open("/dev/null", O_WRONLY
);
422 _stderr
= open("/dev/null", O_WRONLY
);
425 dup2(_stdin
, STDIN_FILENO
);
429 dup2(_stdout
, STDOUT_FILENO
);
433 dup2(_stderr
, STDERR_FILENO
);
437 if (!in
->has_jail
&& in
->user
&& in
->pw_gid
&& initgroups(in
->user
, in
->pw_gid
)) {
438 ERROR("failed to initgroups() for user %s: %m\n", in
->user
);
441 if (!in
->has_jail
&& in
->gr_gid
&& setgid(in
->gr_gid
)) {
442 ERROR("failed to set group id %d: %m\n", in
->gr_gid
);
445 if (!in
->has_jail
&& in
->uid
&& setuid(in
->uid
)) {
446 ERROR("failed to set user id %d: %m\n", in
->uid
);
450 execvp(argv
[0], argv
);
455 instance_free_stdio(struct service_instance
*in
)
457 if (in
->_stdout
.fd
.fd
> -1) {
458 ustream_free(&in
->_stdout
.stream
);
459 close(in
->_stdout
.fd
.fd
);
460 in
->_stdout
.fd
.fd
= -1;
463 if (in
->_stderr
.fd
.fd
> -1) {
464 ustream_free(&in
->_stderr
.stream
);
465 close(in
->_stderr
.fd
.fd
);
466 in
->_stderr
.fd
.fd
= -1;
469 if (in
->console
.fd
.fd
> -1) {
470 ustream_free(&in
->console
.stream
);
471 close(in
->console
.fd
.fd
);
472 in
->console
.fd
.fd
= -1;
475 if (in
->console_client
.fd
.fd
> -1) {
476 ustream_free(&in
->console_client
.stream
);
477 close(in
->console_client
.fd
.fd
);
478 in
->console_client
.fd
.fd
= -1;
483 instance_start(struct service_instance
*in
)
486 int opipe
[2] = { -1, -1 };
487 int epipe
[2] = { -1, -1 };
489 if (!avl_is_empty(&in
->errors
.avl
)) {
490 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
494 if (!in
->bundle
&& !in
->command
) {
495 LOG("Not starting instance %s::%s, command not set\n", in
->srv
->name
, in
->name
);
499 if (in
->proc
.pending
) {
505 instance_free_stdio(in
);
506 if (in
->_stdout
.fd
.fd
> -2) {
508 ULOG_WARN("pipe() failed: %m\n");
509 opipe
[0] = opipe
[1] = -1;
513 if (in
->_stderr
.fd
.fd
> -2) {
515 ULOG_WARN("pipe() failed: %m\n");
516 epipe
[0] = epipe
[1] = -1;
534 instance_run(in
, opipe
[1], epipe
[1]);
538 DEBUG(2, "Started instance %s::%s[%d]\n", in
->srv
->name
, in
->name
, pid
);
540 instance_writepid(in
);
541 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
542 uloop_process_add(&in
->proc
);
545 ustream_fd_init(&in
->_stdout
, opipe
[0]);
547 fcntl(opipe
[0], F_SETFD
, FD_CLOEXEC
);
551 ustream_fd_init(&in
->_stderr
, epipe
[0]);
553 fcntl(epipe
[0], F_SETFD
, FD_CLOEXEC
);
556 service_event("instance.start", in
->srv
->name
, in
->name
);
560 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
562 char *newline
, *str
, *arg0
, ident
[32];
565 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
566 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
567 ulog_open(ULOG_SYSLOG
, in
->syslog_facility
, ident
);
570 str
= ustream_get_read_buf(s
, &len
);
574 newline
= memchr(str
, '\n', len
);
575 if (!newline
&& (s
->r
.buffer_len
!= len
))
580 len
= newline
+ 1 - str
;
582 ulog(prio
, "%s\n", str
);
584 ustream_consume(s
, len
);
587 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
591 instance_stdout(struct ustream
*s
, int bytes
)
593 instance_stdio(s
, LOG_INFO
,
594 container_of(s
, struct service_instance
, _stdout
.stream
));
598 instance_console(struct ustream
*s
, int bytes
)
600 struct service_instance
*in
= container_of(s
, struct service_instance
, console
.stream
);
605 buf
= ustream_get_read_buf(s
, &len
);
609 ulog(LOG_INFO
, "out: %s\n", buf
);
611 /* test if console client is attached */
612 if (in
->console_client
.fd
.fd
> -1)
613 ustream_write(&in
->console_client
.stream
, buf
, len
, false);
615 ustream_consume(s
, len
);
620 instance_console_client(struct ustream
*s
, int bytes
)
622 struct service_instance
*in
= container_of(s
, struct service_instance
, console_client
.stream
);
627 buf
= ustream_get_read_buf(s
, &len
);
631 ulog(LOG_INFO
, "in: %s\n", buf
);
632 ustream_write(&in
->console
.stream
, buf
, len
, false);
633 ustream_consume(s
, len
);
638 instance_stderr(struct ustream
*s
, int bytes
)
640 instance_stdio(s
, LOG_ERR
,
641 container_of(s
, struct service_instance
, _stderr
.stream
));
645 instance_timeout(struct uloop_timeout
*t
)
647 struct service_instance
*in
;
649 in
= container_of(t
, struct service_instance
, timeout
);
652 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
653 in
->srv
->name
, in
->name
, in
->proc
.pid
);
654 kill(in
->proc
.pid
, SIGKILL
);
655 } else if (in
->restart
|| in
->respawn
)
660 instance_delete(struct service_instance
*in
)
662 struct service
*s
= in
->srv
;
664 avl_delete(&s
->instances
.avl
, &in
->node
.avl
);
670 instance_exit_code(int ret
)
672 if (WIFEXITED(ret
)) {
673 return WEXITSTATUS(ret
);
676 if (WIFSIGNALED(ret
)) {
677 return SIGNALLED_OFFSET
+ WTERMSIG(ret
);
680 if (WIFSTOPPED(ret
)) {
681 return WSTOPSIG(ret
);
688 instance_exit(struct uloop_process
*p
, int ret
)
690 struct service_instance
*in
;
694 in
= container_of(p
, struct service_instance
, proc
);
696 clock_gettime(CLOCK_MONOTONIC
, &tp
);
697 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
699 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
701 in
->exit_code
= instance_exit_code(ret
);
702 uloop_timeout_cancel(&in
->timeout
);
703 service_event("instance.stop", in
->srv
->name
, in
->name
);
706 instance_removepid(in
);
711 } else if (in
->restart
) {
713 } else if (in
->respawn
) {
714 if (runtime
< in
->respawn_threshold
)
717 in
->respawn_count
= 0;
718 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
719 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
720 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
721 in
->restart
= in
->respawn
= 0;
723 service_event("instance.fail", in
->srv
->name
, in
->name
);
725 service_event("instance.respawn", in
->srv
->name
, in
->name
);
726 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
732 instance_stop(struct service_instance
*in
, bool halt
)
734 if (!in
->proc
.pending
) {
740 in
->restart
= in
->respawn
= false;
741 kill(in
->proc
.pid
, SIGTERM
);
742 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
746 instance_restart(struct service_instance
*in
)
748 if (!in
->proc
.pending
)
751 if (in
->reload_signal
) {
752 kill(in
->proc
.pid
, in
->reload_signal
);
758 kill(in
->proc
.pid
, SIGTERM
);
759 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
762 static bool string_changed(const char *a
, const char *b
)
764 return !((!a
&& !b
) || (a
&& b
&& !strcmp(a
, b
)));
768 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
773 if (!blob_attr_equal(in
->command
, in_new
->command
))
776 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
779 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
782 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
785 if (in
->nice
!= in_new
->nice
)
788 if (in
->syslog_facility
!= in_new
->syslog_facility
)
791 if (string_changed(in
->user
, in_new
->user
))
794 if (string_changed(in
->group
, in_new
->group
))
797 if (in
->uid
!= in_new
->uid
)
800 if (in
->pw_gid
!= in_new
->pw_gid
)
803 if (string_changed(in
->pidfile
, in_new
->pidfile
))
806 if (in
->respawn_retry
!= in_new
->respawn_retry
)
808 if (in
->respawn_threshold
!= in_new
->respawn_threshold
)
810 if (in
->respawn_timeout
!= in_new
->respawn_timeout
)
812 if (in
->bundle
&& in_new
->bundle
&& strcmp(in
->bundle
, in_new
->bundle
))
814 if ((!in
->seccomp
&& in_new
->seccomp
) ||
815 (in
->seccomp
&& !in_new
->seccomp
) ||
816 (in
->seccomp
&& in_new
->seccomp
&& strcmp(in
->seccomp
, in_new
->seccomp
)))
819 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
822 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
825 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
832 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
834 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
835 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
837 return n1
->ifindex
== n2
->ifindex
;
841 instance_netdev_update(struct blobmsg_list_node
*l
)
843 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
845 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
849 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
851 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
852 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
854 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
858 instance_file_update(struct blobmsg_list_node
*l
)
860 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
865 memset(f
->md5
, 0, sizeof(f
->md5
));
867 fd
= open(l
->avl
.key
, O_RDONLY
);
873 len
= read(fd
, buf
, sizeof(buf
));
883 md5_hash(buf
, len
, &md5
);
886 md5_end(f
->md5
, &md5
);
891 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
896 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
900 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
902 struct blobmsg_list_node
*node
;
907 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
910 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
912 blobmsg_list_for_each(l
, node
)
919 instance_jail_parse(struct service_instance
*in
, struct blob_attr
*attr
)
921 struct blob_attr
*tb
[__JAIL_ATTR_MAX
];
922 struct jail
*jail
= &in
->jail
;
924 blobmsg_parse(jail_attr
, __JAIL_ATTR_MAX
, tb
,
925 blobmsg_data(attr
), blobmsg_data_len(attr
));
929 if (tb
[JAIL_ATTR_REQUIREJAIL
]) {
930 in
->require_jail
= true;
933 if (tb
[JAIL_ATTR_NAME
]) {
934 jail
->name
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_NAME
]));
937 if (tb
[JAIL_ATTR_HOSTNAME
]) {
938 jail
->hostname
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_HOSTNAME
]));
941 if (tb
[JAIL_ATTR_PROCFS
]) {
942 jail
->procfs
= blobmsg_get_bool(tb
[JAIL_ATTR_PROCFS
]);
945 if (tb
[JAIL_ATTR_SYSFS
]) {
946 jail
->sysfs
= blobmsg_get_bool(tb
[JAIL_ATTR_SYSFS
]);
949 if (tb
[JAIL_ATTR_UBUS
]) {
950 jail
->ubus
= blobmsg_get_bool(tb
[JAIL_ATTR_UBUS
]);
953 if (tb
[JAIL_ATTR_LOG
]) {
954 jail
->log
= blobmsg_get_bool(tb
[JAIL_ATTR_LOG
]);
957 if (tb
[JAIL_ATTR_RONLY
]) {
958 jail
->ronly
= blobmsg_get_bool(tb
[JAIL_ATTR_RONLY
]);
961 if (tb
[JAIL_ATTR_NETNS
]) {
962 jail
->netns
= blobmsg_get_bool(tb
[JAIL_ATTR_NETNS
]);
965 if (tb
[JAIL_ATTR_USERNS
]) {
966 jail
->userns
= blobmsg_get_bool(tb
[JAIL_ATTR_USERNS
]);
969 if (tb
[JAIL_ATTR_CGROUPSNS
]) {
970 jail
->cgroupsns
= blobmsg_get_bool(tb
[JAIL_ATTR_CGROUPSNS
]);
973 if (tb
[JAIL_ATTR_CONSOLE
]) {
974 jail
->console
= blobmsg_get_bool(tb
[JAIL_ATTR_CONSOLE
]);
978 if (tb
[JAIL_ATTR_MOUNT
]) {
979 struct blob_attr
*cur
;
982 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_MOUNT
], rem
)
984 instance_fill_array(&jail
->mount
, tb
[JAIL_ATTR_MOUNT
], NULL
, false);
1001 if (in
->tmpoverlaysize
)
1004 if (in
->no_new_privs
)
1014 instance_config_parse_command(struct service_instance
*in
, struct blob_attr
**tb
)
1016 struct blob_attr
*cur
, *cur2
;
1020 cur
= tb
[INSTANCE_ATTR_COMMAND
];
1026 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
1029 blobmsg_for_each_attr(cur2
, cur
, rem
) {
1039 instance_config_parse(struct service_instance
*in
)
1041 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
1042 struct blob_attr
*cur
, *cur2
;
1046 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
1047 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
1049 if (!tb
[INSTANCE_ATTR_BUNDLE
] && !instance_config_parse_command(in
, tb
))
1052 if (tb
[INSTANCE_ATTR_TERMTIMEOUT
])
1053 in
->term_timeout
= blobmsg_get_u32(tb
[INSTANCE_ATTR_TERMTIMEOUT
]);
1054 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
1056 uint32_t vals
[3] = { 3600, 5, 5};
1058 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
1059 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
1061 vals
[i
] = atoi(blobmsg_get_string(cur2
));
1065 in
->respawn_count
= 0;
1066 in
->respawn_threshold
= vals
[0];
1067 in
->respawn_timeout
= vals
[1];
1068 in
->respawn_retry
= vals
[2];
1070 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
1071 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
1072 trigger_add(in
->trigger
, in
);
1075 if (tb
[INSTANCE_ATTR_WATCH
]) {
1076 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
1077 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
1079 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
1080 watch_add(blobmsg_get_string(cur2
), in
);
1084 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
1085 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
1086 if (in
->nice
< -20 || in
->nice
> 20)
1090 if (tb
[INSTANCE_ATTR_USER
]) {
1091 const char *user
= blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]);
1092 struct passwd
*p
= getpwnam(user
);
1094 in
->user
= strdup(user
);
1095 in
->uid
= p
->pw_uid
;
1096 in
->gr_gid
= in
->pw_gid
= p
->pw_gid
;
1100 if (tb
[INSTANCE_ATTR_GROUP
]) {
1101 const char *group
= blobmsg_get_string(tb
[INSTANCE_ATTR_GROUP
]);
1102 struct group
*p
= getgrnam(group
);
1104 in
->group
= strdup(group
);
1105 in
->gr_gid
= p
->gr_gid
;
1109 if (tb
[INSTANCE_ATTR_TRACE
])
1110 in
->trace
= blobmsg_get_bool(tb
[INSTANCE_ATTR_TRACE
]);
1112 if (tb
[INSTANCE_ATTR_NO_NEW_PRIVS
])
1113 in
->no_new_privs
= blobmsg_get_bool(tb
[INSTANCE_ATTR_NO_NEW_PRIVS
]);
1115 if (!in
->trace
&& tb
[INSTANCE_ATTR_SECCOMP
])
1116 in
->seccomp
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_SECCOMP
]));
1118 if (tb
[INSTANCE_ATTR_EXTROOT
])
1119 in
->extroot
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_EXTROOT
]));
1121 if (tb
[INSTANCE_ATTR_OVERLAYDIR
])
1122 in
->overlaydir
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_OVERLAYDIR
]));
1124 if (tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
])
1125 in
->tmpoverlaysize
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
]));
1127 if (tb
[INSTANCE_ATTR_BUNDLE
])
1128 in
->bundle
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_BUNDLE
]));
1130 if (tb
[INSTANCE_ATTR_PIDFILE
]) {
1131 char *pidfile
= blobmsg_get_string(tb
[INSTANCE_ATTR_PIDFILE
]);
1133 in
->pidfile
= strdup(pidfile
);
1136 if (tb
[INSTANCE_ATTR_RELOADSIG
])
1137 in
->reload_signal
= blobmsg_get_u32(tb
[INSTANCE_ATTR_RELOADSIG
]);
1139 if (!in
->trace
&& tb
[INSTANCE_ATTR_JAIL
])
1140 in
->has_jail
= instance_jail_parse(in
, tb
[INSTANCE_ATTR_JAIL
]);
1143 r
= stat(UJAIL_BIN_PATH
, &s
);
1145 if (in
->require_jail
) {
1146 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1147 in
->srv
->name
, in
->name
, UJAIL_BIN_PATH
, r
);
1150 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH
, r
);
1151 in
->has_jail
= false;
1155 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
1156 in
->_stdout
.fd
.fd
= -1;
1158 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
1159 in
->_stderr
.fd
.fd
= -1;
1161 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
1163 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
1166 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
1169 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
1172 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
1175 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
1178 if (tb
[INSTANCE_ATTR_FACILITY
]) {
1179 int facility
= syslog_facility_str_to_int(blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1180 if (facility
!= -1) {
1181 in
->syslog_facility
= facility
;
1182 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1184 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1191 instance_config_cleanup(struct service_instance
*in
)
1193 blobmsg_list_free(&in
->env
);
1194 blobmsg_list_free(&in
->data
);
1195 blobmsg_list_free(&in
->netdev
);
1196 blobmsg_list_free(&in
->file
);
1197 blobmsg_list_free(&in
->limits
);
1198 blobmsg_list_free(&in
->errors
);
1199 blobmsg_list_free(&in
->jail
.mount
);
1203 instance_config_move_strdup(char **dst
, char *src
)
1217 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
1219 instance_config_cleanup(in
);
1220 blobmsg_list_move(&in
->env
, &in_src
->env
);
1221 blobmsg_list_move(&in
->data
, &in_src
->data
);
1222 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
1223 blobmsg_list_move(&in
->file
, &in_src
->file
);
1224 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
1225 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
1226 blobmsg_list_move(&in
->jail
.mount
, &in_src
->jail
.mount
);
1227 in
->trigger
= in_src
->trigger
;
1228 in
->command
= in_src
->command
;
1229 in
->respawn
= in_src
->respawn
;
1230 in
->respawn_retry
= in_src
->respawn_retry
;
1231 in
->respawn_threshold
= in_src
->respawn_threshold
;
1232 in
->respawn_timeout
= in_src
->respawn_timeout
;
1233 in
->name
= in_src
->name
;
1234 in
->trace
= in_src
->trace
;
1235 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
1236 in
->syslog_facility
= in_src
->syslog_facility
;
1238 instance_config_move_strdup(&in
->pidfile
, in_src
->pidfile
);
1239 instance_config_move_strdup(&in
->seccomp
, in_src
->seccomp
);
1240 instance_config_move_strdup(&in
->jail
.name
, in_src
->jail
.name
);
1241 instance_config_move_strdup(&in
->jail
.hostname
, in_src
->jail
.hostname
);
1244 in
->config
= in_src
->config
;
1245 in_src
->config
= NULL
;
1249 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
1251 bool changed
= instance_config_changed(in
, in_new
);
1252 bool running
= in
->proc
.pending
;
1253 bool stopping
= in
->halt
;
1255 if (!running
|| stopping
) {
1256 instance_config_move(in
, in_new
);
1260 instance_restart(in
);
1261 instance_config_move(in
, in_new
);
1262 /* restart happens in the child callback handler */
1267 instance_free(struct service_instance
*in
)
1269 instance_free_stdio(in
);
1270 uloop_process_delete(&in
->proc
);
1271 uloop_timeout_cancel(&in
->timeout
);
1274 instance_config_cleanup(in
);
1279 free(in
->overlaydir
);
1280 free(in
->tmpoverlaysize
);
1282 free(in
->jail
.name
);
1283 free(in
->jail
.hostname
);
1290 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
1292 config
= blob_memdup(config
);
1294 in
->name
= blobmsg_name(config
);
1295 in
->config
= config
;
1296 in
->timeout
.cb
= instance_timeout
;
1297 in
->proc
.cb
= instance_exit
;
1298 in
->term_timeout
= 5;
1299 in
->syslog_facility
= LOG_DAEMON
;
1301 in
->require_jail
= false;
1303 in
->_stdout
.fd
.fd
= -2;
1304 in
->_stdout
.stream
.string_data
= true;
1305 in
->_stdout
.stream
.notify_read
= instance_stdout
;
1307 in
->_stderr
.fd
.fd
= -2;
1308 in
->_stderr
.stream
.string_data
= true;
1309 in
->_stderr
.stream
.notify_read
= instance_stderr
;
1311 in
->console
.fd
.fd
= -2;
1312 in
->console
.stream
.string_data
= true;
1313 in
->console
.stream
.notify_read
= instance_console
;
1315 in
->console_client
.fd
.fd
= -2;
1316 in
->console_client
.stream
.string_data
= true;
1317 in
->console_client
.stream
.notify_read
= instance_console_client
;
1319 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
1320 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
1321 blobmsg_list_simple_init(&in
->env
);
1322 blobmsg_list_simple_init(&in
->data
);
1323 blobmsg_list_simple_init(&in
->limits
);
1324 blobmsg_list_simple_init(&in
->errors
);
1325 blobmsg_list_simple_init(&in
->jail
.mount
);
1326 in
->valid
= instance_config_parse(in
);
1329 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
1336 i
= blobmsg_open_table(b
, in
->name
);
1337 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
1338 if (in
->proc
.pending
)
1339 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
1341 blobmsg_add_blob(b
, in
->command
);
1343 blobmsg_add_string(b
, "bundle", in
->bundle
);
1344 blobmsg_add_u32(b
, "term_timeout", in
->term_timeout
);
1345 if (!in
->proc
.pending
)
1346 blobmsg_add_u32(b
, "exit_code", in
->exit_code
);
1348 if (!avl_is_empty(&in
->errors
.avl
)) {
1349 struct blobmsg_list_node
*var
;
1350 void *e
= blobmsg_open_array(b
, "errors");
1351 blobmsg_list_for_each(&in
->errors
, var
)
1352 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
1353 blobmsg_close_table(b
, e
);
1356 if (!avl_is_empty(&in
->env
.avl
)) {
1357 struct blobmsg_list_node
*var
;
1358 void *e
= blobmsg_open_table(b
, "env");
1359 blobmsg_list_for_each(&in
->env
, var
)
1360 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1361 blobmsg_close_table(b
, e
);
1364 if (!avl_is_empty(&in
->data
.avl
)) {
1365 struct blobmsg_list_node
*var
;
1366 void *e
= blobmsg_open_table(b
, "data");
1367 blobmsg_list_for_each(&in
->data
, var
)
1368 blobmsg_add_blob(b
, var
->data
);
1369 blobmsg_close_table(b
, e
);
1372 if (!avl_is_empty(&in
->limits
.avl
)) {
1373 struct blobmsg_list_node
*var
;
1374 void *e
= blobmsg_open_table(b
, "limits");
1375 blobmsg_list_for_each(&in
->limits
, var
)
1376 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1377 blobmsg_close_table(b
, e
);
1380 if (in
->reload_signal
)
1381 blobmsg_add_u32(b
, "reload_signal", in
->reload_signal
);
1384 void *r
= blobmsg_open_table(b
, "respawn");
1385 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
1386 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
1387 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
1388 blobmsg_close_table(b
, r
);
1392 blobmsg_add_u8(b
, "trace", true);
1394 if (in
->no_new_privs
)
1395 blobmsg_add_u8(b
, "no_new_privs", true);
1398 blobmsg_add_string(b
, "seccomp", in
->seccomp
);
1401 blobmsg_add_string(b
, "pidfile", in
->pidfile
);
1404 blobmsg_add_string(b
, "user", in
->user
);
1407 blobmsg_add_string(b
, "group", in
->group
);
1410 void *r
= blobmsg_open_table(b
, "jail");
1412 blobmsg_add_string(b
, "name", in
->jail
.name
);
1414 if (in
->jail
.hostname
)
1415 blobmsg_add_string(b
, "hostname", in
->jail
.hostname
);
1417 blobmsg_add_u8(b
, "procfs", in
->jail
.procfs
);
1418 blobmsg_add_u8(b
, "sysfs", in
->jail
.sysfs
);
1419 blobmsg_add_u8(b
, "ubus", in
->jail
.ubus
);
1420 blobmsg_add_u8(b
, "log", in
->jail
.log
);
1421 blobmsg_add_u8(b
, "ronly", in
->jail
.ronly
);
1422 blobmsg_add_u8(b
, "netns", in
->jail
.netns
);
1423 blobmsg_add_u8(b
, "userns", in
->jail
.userns
);
1424 blobmsg_add_u8(b
, "cgroupsns", in
->jail
.cgroupsns
);
1426 blobmsg_add_u8(b
, "console", (in
->console
.fd
.fd
> -1));
1427 blobmsg_close_table(b
, r
);
1428 if (!avl_is_empty(&in
->jail
.mount
.avl
)) {
1429 struct blobmsg_list_node
*var
;
1430 void *e
= blobmsg_open_table(b
, "mount");
1431 blobmsg_list_for_each(&in
->jail
.mount
, var
)
1432 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1433 blobmsg_close_table(b
, e
);
1438 blobmsg_add_string(b
, "extroot", in
->extroot
);
1440 blobmsg_add_string(b
, "overlaydir", in
->overlaydir
);
1441 if (in
->tmpoverlaysize
)
1442 blobmsg_add_string(b
, "tmpoverlaysize", in
->tmpoverlaysize
);
1444 if (verbose
&& in
->trigger
)
1445 blobmsg_add_blob(b
, in
->trigger
);
1447 blobmsg_close_table(b
, i
);