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
,
71 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
72 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
73 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
74 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
75 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
76 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
77 [INSTANCE_ATTR_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
78 [INSTANCE_ATTR_RESPAWN
] = { "respawn", BLOBMSG_TYPE_ARRAY
},
79 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
80 [INSTANCE_ATTR_LIMITS
] = { "limits", BLOBMSG_TYPE_TABLE
},
81 [INSTANCE_ATTR_WATCH
] = { "watch", BLOBMSG_TYPE_ARRAY
},
82 [INSTANCE_ATTR_ERROR
] = { "error", BLOBMSG_TYPE_ARRAY
},
83 [INSTANCE_ATTR_USER
] = { "user", BLOBMSG_TYPE_STRING
},
84 [INSTANCE_ATTR_GROUP
] = { "group", BLOBMSG_TYPE_STRING
},
85 [INSTANCE_ATTR_STDOUT
] = { "stdout", BLOBMSG_TYPE_BOOL
},
86 [INSTANCE_ATTR_STDERR
] = { "stderr", BLOBMSG_TYPE_BOOL
},
87 [INSTANCE_ATTR_NO_NEW_PRIVS
] = { "no_new_privs", BLOBMSG_TYPE_BOOL
},
88 [INSTANCE_ATTR_JAIL
] = { "jail", BLOBMSG_TYPE_TABLE
},
89 [INSTANCE_ATTR_TRACE
] = { "trace", BLOBMSG_TYPE_BOOL
},
90 [INSTANCE_ATTR_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_STRING
},
91 [INSTANCE_ATTR_PIDFILE
] = { "pidfile", BLOBMSG_TYPE_STRING
},
92 [INSTANCE_ATTR_RELOADSIG
] = { "reload_signal", BLOBMSG_TYPE_INT32
},
93 [INSTANCE_ATTR_TERMTIMEOUT
] = { "term_timeout", BLOBMSG_TYPE_INT32
},
94 [INSTANCE_ATTR_FACILITY
] = { "facility", BLOBMSG_TYPE_STRING
},
95 [INSTANCE_ATTR_EXTROOT
] = { "extroot", BLOBMSG_TYPE_STRING
},
96 [INSTANCE_ATTR_OVERLAYDIR
] = { "overlaydir", BLOBMSG_TYPE_STRING
},
97 [INSTANCE_ATTR_TMPOVERLAYSIZE
] = { "tmpoverlaysize", BLOBMSG_TYPE_STRING
},
113 JAIL_ATTR_REQUIREJAIL
,
117 static const struct blobmsg_policy jail_attr
[__JAIL_ATTR_MAX
] = {
118 [JAIL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
119 [JAIL_ATTR_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
120 [JAIL_ATTR_PROCFS
] = { "procfs", BLOBMSG_TYPE_BOOL
},
121 [JAIL_ATTR_SYSFS
] = { "sysfs", BLOBMSG_TYPE_BOOL
},
122 [JAIL_ATTR_UBUS
] = { "ubus", BLOBMSG_TYPE_BOOL
},
123 [JAIL_ATTR_LOG
] = { "log", BLOBMSG_TYPE_BOOL
},
124 [JAIL_ATTR_RONLY
] = { "ronly", BLOBMSG_TYPE_BOOL
},
125 [JAIL_ATTR_MOUNT
] = { "mount", BLOBMSG_TYPE_TABLE
},
126 [JAIL_ATTR_NETNS
] = { "netns", BLOBMSG_TYPE_BOOL
},
127 [JAIL_ATTR_USERNS
] = { "userns", BLOBMSG_TYPE_BOOL
},
128 [JAIL_ATTR_CGROUPSNS
] = { "cgroupsns", BLOBMSG_TYPE_BOOL
},
129 [JAIL_ATTR_CONSOLE
] = { "console", BLOBMSG_TYPE_BOOL
},
130 [JAIL_ATTR_REQUIREJAIL
] = { "requirejail", BLOBMSG_TYPE_BOOL
},
133 struct instance_netdev
{
134 struct blobmsg_list_node node
;
138 struct instance_file
{
139 struct blobmsg_list_node node
;
148 static const struct rlimit_name rlimit_names
[] = {
150 { "core", RLIMIT_CORE
},
151 { "cpu", RLIMIT_CPU
},
152 { "data", RLIMIT_DATA
},
153 { "fsize", RLIMIT_FSIZE
},
154 { "memlock", RLIMIT_MEMLOCK
},
155 { "nofile", RLIMIT_NOFILE
},
156 { "nproc", RLIMIT_NPROC
},
157 { "rss", RLIMIT_RSS
},
158 { "stack", RLIMIT_STACK
},
160 { "nice", RLIMIT_NICE
},
161 { "rtprio", RLIMIT_RTPRIO
},
162 { "msgqueue", RLIMIT_MSGQUEUE
},
163 { "sigpending", RLIMIT_SIGPENDING
},
168 static void closefd(int fd
)
170 if (fd
> STDERR_FILENO
)
174 /* convert a string into numeric syslog facility or return -1 if no match found */
176 syslog_facility_str_to_int(const char *facility
)
178 CODE
*p
= facilitynames
;
180 while (p
->c_name
&& strcasecmp(p
->c_name
, facility
))
187 instance_limits(const char *limit
, const char *value
)
191 unsigned long cur
, max
;
193 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
194 if (strcmp(rlimit_names
[i
].name
, limit
))
196 if (!strcmp(value
, "unlimited")) {
197 rlim
.rlim_cur
= RLIM_INFINITY
;
198 rlim
.rlim_max
= RLIM_INFINITY
;
200 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
206 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
213 setrlimit(rlimit_names
[i
].resource
, &rlim
);
219 jail_run(struct service_instance
*in
, char **argv
)
221 struct blobmsg_list_node
*var
;
222 struct jail
*jail
= &in
->jail
;
225 argv
[argc
++] = UJAIL_BIN_PATH
;
229 argv
[argc
++] = jail
->name
;
232 if (jail
->hostname
) {
234 argv
[argc
++] = jail
->hostname
;
239 argv
[argc
++] = in
->seccomp
;
244 argv
[argc
++] = in
->user
;
249 argv
[argc
++] = in
->group
;
252 if (in
->no_new_privs
)
284 argv
[argc
++] = in
->extroot
;
287 if (in
->overlaydir
) {
289 argv
[argc
++] = in
->overlaydir
;
292 if (in
->tmpoverlaysize
) {
294 argv
[argc
++] = in
->tmpoverlaysize
;
297 if (in
->require_jail
)
300 blobmsg_list_for_each(&jail
->mount
, var
) {
301 const char *type
= blobmsg_data(var
->data
);
307 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
316 instance_removepid(struct service_instance
*in
) {
319 if (unlink(in
->pidfile
)) {
320 ERROR("Failed to remove pidfile: %s: %m\n", in
->pidfile
);
327 instance_writepid(struct service_instance
*in
)
334 _pidfile
= fopen(in
->pidfile
, "w");
335 if (_pidfile
== NULL
) {
336 ERROR("failed to open pidfile for writing: %s: %m", in
->pidfile
);
339 if (fprintf(_pidfile
, "%d\n", in
->proc
.pid
) < 0) {
340 ERROR("failed to write pidfile: %s: %m", in
->pidfile
);
344 if (fclose(_pidfile
)) {
345 ERROR("failed to close pidfile: %s: %m", in
->pidfile
);
353 instance_run(struct service_instance
*in
, int _stdout
, int _stderr
)
355 struct blobmsg_list_node
*var
;
356 struct blob_attr
*cur
;
358 int argc
= 1; /* NULL terminated */
360 bool seccomp
= !in
->trace
&& !in
->has_jail
&& in
->seccomp
;
361 bool setlbf
= _stdout
>= 0;
364 setpriority(PRIO_PROCESS
, 0, in
->nice
);
366 blobmsg_for_each_attr(cur
, in
->command
, rem
)
369 blobmsg_list_for_each(&in
->env
, var
)
370 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
373 setenv("SECCOMP_FILE", in
->seccomp
, 1);
376 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
378 blobmsg_list_for_each(&in
->limits
, var
)
379 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
381 if (in
->trace
|| seccomp
)
384 argv
= alloca(sizeof(char *) * (argc
+ in
->jail
.argc
));
387 #ifdef SECCOMP_SUPPORT
389 argv
[argc
++] = "/sbin/utrace";
391 argv
[argc
++] = "/sbin/seccomp-trace";
393 if (in
->trace
|| seccomp
)
394 ULOG_WARN("Seccomp support for %s::%s not available\n", in
->srv
->name
, in
->name
);
398 argc
= jail_run(in
, argv
);
399 if (argc
!= in
->jail
.argc
)
400 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
401 in
->jail
.argc
, argc
, in
->srv
->name
, in
->name
);
404 blobmsg_for_each_attr(cur
, in
->command
, rem
)
405 argv
[argc
++] = blobmsg_data(cur
);
409 _stdin
= open("/dev/null", O_RDONLY
);
412 _stdout
= open("/dev/null", O_WRONLY
);
415 _stderr
= open("/dev/null", O_WRONLY
);
418 dup2(_stdin
, STDIN_FILENO
);
422 dup2(_stdout
, STDOUT_FILENO
);
426 dup2(_stderr
, STDERR_FILENO
);
430 if (!in
->has_jail
&& in
->user
&& in
->pw_gid
&& initgroups(in
->user
, in
->pw_gid
)) {
431 ERROR("failed to initgroups() for user %s: %m\n", in
->user
);
434 if (!in
->has_jail
&& in
->gr_gid
&& setgid(in
->gr_gid
)) {
435 ERROR("failed to set group id %d: %m\n", in
->gr_gid
);
438 if (!in
->has_jail
&& in
->uid
&& setuid(in
->uid
)) {
439 ERROR("failed to set user id %d: %m\n", in
->uid
);
443 execvp(argv
[0], argv
);
448 instance_free_stdio(struct service_instance
*in
)
450 if (in
->_stdout
.fd
.fd
> -1) {
451 ustream_free(&in
->_stdout
.stream
);
452 close(in
->_stdout
.fd
.fd
);
453 in
->_stdout
.fd
.fd
= -1;
456 if (in
->_stderr
.fd
.fd
> -1) {
457 ustream_free(&in
->_stderr
.stream
);
458 close(in
->_stderr
.fd
.fd
);
459 in
->_stderr
.fd
.fd
= -1;
462 if (in
->console
.fd
.fd
> -1) {
463 ustream_free(&in
->console
.stream
);
464 close(in
->console
.fd
.fd
);
465 in
->console
.fd
.fd
= -1;
468 if (in
->console_client
.fd
.fd
> -1) {
469 ustream_free(&in
->console_client
.stream
);
470 close(in
->console_client
.fd
.fd
);
471 in
->console_client
.fd
.fd
= -1;
476 instance_start(struct service_instance
*in
)
479 int opipe
[2] = { -1, -1 };
480 int epipe
[2] = { -1, -1 };
482 if (!avl_is_empty(&in
->errors
.avl
)) {
483 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
488 LOG("Not starting instance %s::%s, command not set\n", in
->srv
->name
, in
->name
);
492 if (in
->proc
.pending
) {
498 instance_free_stdio(in
);
499 if (in
->_stdout
.fd
.fd
> -2) {
501 ULOG_WARN("pipe() failed: %m\n");
502 opipe
[0] = opipe
[1] = -1;
506 if (in
->_stderr
.fd
.fd
> -2) {
508 ULOG_WARN("pipe() failed: %m\n");
509 epipe
[0] = epipe
[1] = -1;
527 instance_run(in
, opipe
[1], epipe
[1]);
531 DEBUG(2, "Started instance %s::%s[%d]\n", in
->srv
->name
, in
->name
, pid
);
533 instance_writepid(in
);
534 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
535 uloop_process_add(&in
->proc
);
538 ustream_fd_init(&in
->_stdout
, opipe
[0]);
540 fcntl(opipe
[0], F_SETFD
, FD_CLOEXEC
);
544 ustream_fd_init(&in
->_stderr
, epipe
[0]);
546 fcntl(epipe
[0], F_SETFD
, FD_CLOEXEC
);
549 service_event("instance.start", in
->srv
->name
, in
->name
);
553 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
555 char *newline
, *str
, *arg0
, ident
[32];
558 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
559 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
560 ulog_open(ULOG_SYSLOG
, in
->syslog_facility
, ident
);
563 str
= ustream_get_read_buf(s
, &len
);
567 newline
= memchr(str
, '\n', len
);
568 if (!newline
&& (s
->r
.buffer_len
!= len
))
573 len
= newline
+ 1 - str
;
575 ulog(prio
, "%s\n", str
);
577 ustream_consume(s
, len
);
580 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
584 instance_stdout(struct ustream
*s
, int bytes
)
586 instance_stdio(s
, LOG_INFO
,
587 container_of(s
, struct service_instance
, _stdout
.stream
));
591 instance_console(struct ustream
*s
, int bytes
)
593 struct service_instance
*in
= container_of(s
, struct service_instance
, console
.stream
);
598 buf
= ustream_get_read_buf(s
, &len
);
602 ulog(LOG_INFO
, "out: %s\n", buf
);
604 /* test if console client is attached */
605 if (in
->console_client
.fd
.fd
> -1)
606 ustream_write(&in
->console_client
.stream
, buf
, len
, false);
608 ustream_consume(s
, len
);
613 instance_console_client(struct ustream
*s
, int bytes
)
615 struct service_instance
*in
= container_of(s
, struct service_instance
, console_client
.stream
);
620 buf
= ustream_get_read_buf(s
, &len
);
624 ulog(LOG_INFO
, "in: %s\n", buf
);
625 ustream_write(&in
->console
.stream
, buf
, len
, false);
626 ustream_consume(s
, len
);
631 instance_stderr(struct ustream
*s
, int bytes
)
633 instance_stdio(s
, LOG_ERR
,
634 container_of(s
, struct service_instance
, _stderr
.stream
));
638 instance_timeout(struct uloop_timeout
*t
)
640 struct service_instance
*in
;
642 in
= container_of(t
, struct service_instance
, timeout
);
645 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
646 in
->srv
->name
, in
->name
, in
->proc
.pid
);
647 kill(in
->proc
.pid
, SIGKILL
);
648 } else if (in
->restart
|| in
->respawn
)
653 instance_delete(struct service_instance
*in
)
655 struct service
*s
= in
->srv
;
657 avl_delete(&s
->instances
.avl
, &in
->node
.avl
);
663 instance_exit_code(int ret
)
665 if (WIFEXITED(ret
)) {
666 return WEXITSTATUS(ret
);
669 if (WIFSIGNALED(ret
)) {
670 return SIGNALLED_OFFSET
+ WTERMSIG(ret
);
673 if (WIFSTOPPED(ret
)) {
674 return WSTOPSIG(ret
);
681 instance_exit(struct uloop_process
*p
, int ret
)
683 struct service_instance
*in
;
687 in
= container_of(p
, struct service_instance
, proc
);
689 clock_gettime(CLOCK_MONOTONIC
, &tp
);
690 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
692 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
694 in
->exit_code
= instance_exit_code(ret
);
695 uloop_timeout_cancel(&in
->timeout
);
696 service_event("instance.stop", in
->srv
->name
, in
->name
);
699 instance_removepid(in
);
704 } else if (in
->restart
) {
706 } else if (in
->respawn
) {
707 if (runtime
< in
->respawn_threshold
)
710 in
->respawn_count
= 0;
711 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
712 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
713 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
714 in
->restart
= in
->respawn
= 0;
716 service_event("instance.fail", in
->srv
->name
, in
->name
);
718 service_event("instance.respawn", in
->srv
->name
, in
->name
);
719 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
725 instance_stop(struct service_instance
*in
, bool halt
)
727 if (!in
->proc
.pending
) {
733 in
->restart
= in
->respawn
= false;
734 kill(in
->proc
.pid
, SIGTERM
);
735 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
739 instance_restart(struct service_instance
*in
)
741 if (!in
->proc
.pending
)
744 if (in
->reload_signal
) {
745 kill(in
->proc
.pid
, in
->reload_signal
);
751 kill(in
->proc
.pid
, SIGTERM
);
752 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
755 static bool string_changed(const char *a
, const char *b
)
757 return !((!a
&& !b
) || (a
&& b
&& !strcmp(a
, b
)));
761 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
766 if (!blob_attr_equal(in
->command
, in_new
->command
))
769 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
772 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
775 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
778 if (in
->nice
!= in_new
->nice
)
781 if (in
->syslog_facility
!= in_new
->syslog_facility
)
784 if (string_changed(in
->user
, in_new
->user
))
787 if (string_changed(in
->group
, in_new
->group
))
790 if (in
->uid
!= in_new
->uid
)
793 if (in
->pw_gid
!= in_new
->pw_gid
)
796 if (string_changed(in
->pidfile
, in_new
->pidfile
))
799 if (in
->respawn_retry
!= in_new
->respawn_retry
)
801 if (in
->respawn_threshold
!= in_new
->respawn_threshold
)
803 if (in
->respawn_timeout
!= in_new
->respawn_timeout
)
806 if ((!in
->seccomp
&& in_new
->seccomp
) ||
807 (in
->seccomp
&& !in_new
->seccomp
) ||
808 (in
->seccomp
&& in_new
->seccomp
&& strcmp(in
->seccomp
, in_new
->seccomp
)))
811 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
814 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
817 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
824 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
826 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
827 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
829 return n1
->ifindex
== n2
->ifindex
;
833 instance_netdev_update(struct blobmsg_list_node
*l
)
835 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
837 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
841 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
843 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
844 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
846 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
850 instance_file_update(struct blobmsg_list_node
*l
)
852 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
857 memset(f
->md5
, 0, sizeof(f
->md5
));
859 fd
= open(l
->avl
.key
, O_RDONLY
);
865 len
= read(fd
, buf
, sizeof(buf
));
875 md5_hash(buf
, len
, &md5
);
878 md5_end(f
->md5
, &md5
);
883 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
888 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
892 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
894 struct blobmsg_list_node
*node
;
899 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
902 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
904 blobmsg_list_for_each(l
, node
)
911 instance_jail_parse(struct service_instance
*in
, struct blob_attr
*attr
)
913 struct blob_attr
*tb
[__JAIL_ATTR_MAX
];
914 struct jail
*jail
= &in
->jail
;
916 blobmsg_parse(jail_attr
, __JAIL_ATTR_MAX
, tb
,
917 blobmsg_data(attr
), blobmsg_data_len(attr
));
921 if (tb
[JAIL_ATTR_REQUIREJAIL
]) {
922 in
->require_jail
= true;
925 if (tb
[JAIL_ATTR_NAME
]) {
926 jail
->name
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_NAME
]));
929 if (tb
[JAIL_ATTR_HOSTNAME
]) {
930 jail
->hostname
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_HOSTNAME
]));
933 if (tb
[JAIL_ATTR_PROCFS
]) {
934 jail
->procfs
= blobmsg_get_bool(tb
[JAIL_ATTR_PROCFS
]);
937 if (tb
[JAIL_ATTR_SYSFS
]) {
938 jail
->sysfs
= blobmsg_get_bool(tb
[JAIL_ATTR_SYSFS
]);
941 if (tb
[JAIL_ATTR_UBUS
]) {
942 jail
->ubus
= blobmsg_get_bool(tb
[JAIL_ATTR_UBUS
]);
945 if (tb
[JAIL_ATTR_LOG
]) {
946 jail
->log
= blobmsg_get_bool(tb
[JAIL_ATTR_LOG
]);
949 if (tb
[JAIL_ATTR_RONLY
]) {
950 jail
->ronly
= blobmsg_get_bool(tb
[JAIL_ATTR_RONLY
]);
953 if (tb
[JAIL_ATTR_NETNS
]) {
954 jail
->netns
= blobmsg_get_bool(tb
[JAIL_ATTR_NETNS
]);
957 if (tb
[JAIL_ATTR_USERNS
]) {
958 jail
->userns
= blobmsg_get_bool(tb
[JAIL_ATTR_USERNS
]);
961 if (tb
[JAIL_ATTR_CGROUPSNS
]) {
962 jail
->cgroupsns
= blobmsg_get_bool(tb
[JAIL_ATTR_CGROUPSNS
]);
965 if (tb
[JAIL_ATTR_CONSOLE
]) {
966 jail
->console
= blobmsg_get_bool(tb
[JAIL_ATTR_CONSOLE
]);
970 if (tb
[JAIL_ATTR_MOUNT
]) {
971 struct blob_attr
*cur
;
974 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_MOUNT
], rem
)
976 instance_fill_array(&jail
->mount
, tb
[JAIL_ATTR_MOUNT
], NULL
, false);
993 if (in
->tmpoverlaysize
)
996 if (in
->no_new_privs
)
1003 instance_config_parse_command(struct service_instance
*in
, struct blob_attr
**tb
)
1005 struct blob_attr
*cur
, *cur2
;
1009 cur
= tb
[INSTANCE_ATTR_COMMAND
];
1015 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
1018 blobmsg_for_each_attr(cur2
, cur
, rem
) {
1028 instance_config_parse(struct service_instance
*in
)
1030 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
1031 struct blob_attr
*cur
, *cur2
;
1035 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
1036 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
1038 if (!instance_config_parse_command(in
, tb
))
1041 if (tb
[INSTANCE_ATTR_TERMTIMEOUT
])
1042 in
->term_timeout
= blobmsg_get_u32(tb
[INSTANCE_ATTR_TERMTIMEOUT
]);
1043 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
1045 uint32_t vals
[3] = { 3600, 5, 5};
1047 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
1048 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
1050 vals
[i
] = atoi(blobmsg_get_string(cur2
));
1054 in
->respawn_count
= 0;
1055 in
->respawn_threshold
= vals
[0];
1056 in
->respawn_timeout
= vals
[1];
1057 in
->respawn_retry
= vals
[2];
1059 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
1060 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
1061 trigger_add(in
->trigger
, in
);
1064 if (tb
[INSTANCE_ATTR_WATCH
]) {
1065 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
1066 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
1068 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
1069 watch_add(blobmsg_get_string(cur2
), in
);
1073 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
1074 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
1075 if (in
->nice
< -20 || in
->nice
> 20)
1079 if (tb
[INSTANCE_ATTR_USER
]) {
1080 const char *user
= blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]);
1081 struct passwd
*p
= getpwnam(user
);
1083 in
->user
= strdup(user
);
1084 in
->uid
= p
->pw_uid
;
1085 in
->gr_gid
= in
->pw_gid
= p
->pw_gid
;
1089 if (tb
[INSTANCE_ATTR_GROUP
]) {
1090 const char *group
= blobmsg_get_string(tb
[INSTANCE_ATTR_GROUP
]);
1091 struct group
*p
= getgrnam(group
);
1093 in
->group
= strdup(group
);
1094 in
->gr_gid
= p
->gr_gid
;
1098 if (tb
[INSTANCE_ATTR_TRACE
])
1099 in
->trace
= blobmsg_get_bool(tb
[INSTANCE_ATTR_TRACE
]);
1101 if (tb
[INSTANCE_ATTR_NO_NEW_PRIVS
])
1102 in
->no_new_privs
= blobmsg_get_bool(tb
[INSTANCE_ATTR_NO_NEW_PRIVS
]);
1104 if (!in
->trace
&& tb
[INSTANCE_ATTR_SECCOMP
])
1105 in
->seccomp
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_SECCOMP
]));
1107 if (tb
[INSTANCE_ATTR_EXTROOT
])
1108 in
->extroot
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_EXTROOT
]));
1110 if (tb
[INSTANCE_ATTR_OVERLAYDIR
])
1111 in
->overlaydir
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_OVERLAYDIR
]));
1113 if (tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
])
1114 in
->tmpoverlaysize
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
]));
1116 if (tb
[INSTANCE_ATTR_PIDFILE
]) {
1117 char *pidfile
= blobmsg_get_string(tb
[INSTANCE_ATTR_PIDFILE
]);
1119 in
->pidfile
= strdup(pidfile
);
1122 if (tb
[INSTANCE_ATTR_RELOADSIG
])
1123 in
->reload_signal
= blobmsg_get_u32(tb
[INSTANCE_ATTR_RELOADSIG
]);
1125 if (!in
->trace
&& tb
[INSTANCE_ATTR_JAIL
])
1126 in
->has_jail
= instance_jail_parse(in
, tb
[INSTANCE_ATTR_JAIL
]);
1129 r
= stat(UJAIL_BIN_PATH
, &s
);
1131 if (in
->require_jail
) {
1132 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1133 in
->srv
->name
, in
->name
, UJAIL_BIN_PATH
, r
);
1136 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH
, r
);
1137 in
->has_jail
= false;
1141 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
1142 in
->_stdout
.fd
.fd
= -1;
1144 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
1145 in
->_stderr
.fd
.fd
= -1;
1147 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
1149 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
1152 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
1155 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
1158 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
1161 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
1164 if (tb
[INSTANCE_ATTR_FACILITY
]) {
1165 int facility
= syslog_facility_str_to_int(blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1166 if (facility
!= -1) {
1167 in
->syslog_facility
= facility
;
1168 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1170 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1177 instance_config_cleanup(struct service_instance
*in
)
1179 blobmsg_list_free(&in
->env
);
1180 blobmsg_list_free(&in
->data
);
1181 blobmsg_list_free(&in
->netdev
);
1182 blobmsg_list_free(&in
->file
);
1183 blobmsg_list_free(&in
->limits
);
1184 blobmsg_list_free(&in
->errors
);
1185 blobmsg_list_free(&in
->jail
.mount
);
1189 instance_config_move_strdup(char **dst
, char *src
)
1203 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
1205 instance_config_cleanup(in
);
1206 blobmsg_list_move(&in
->env
, &in_src
->env
);
1207 blobmsg_list_move(&in
->data
, &in_src
->data
);
1208 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
1209 blobmsg_list_move(&in
->file
, &in_src
->file
);
1210 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
1211 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
1212 blobmsg_list_move(&in
->jail
.mount
, &in_src
->jail
.mount
);
1213 in
->trigger
= in_src
->trigger
;
1214 in
->command
= in_src
->command
;
1215 in
->respawn
= in_src
->respawn
;
1216 in
->respawn_retry
= in_src
->respawn_retry
;
1217 in
->respawn_threshold
= in_src
->respawn_threshold
;
1218 in
->respawn_timeout
= in_src
->respawn_timeout
;
1219 in
->name
= in_src
->name
;
1220 in
->trace
= in_src
->trace
;
1221 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
1222 in
->syslog_facility
= in_src
->syslog_facility
;
1224 instance_config_move_strdup(&in
->pidfile
, in_src
->pidfile
);
1225 instance_config_move_strdup(&in
->seccomp
, in_src
->seccomp
);
1226 instance_config_move_strdup(&in
->jail
.name
, in_src
->jail
.name
);
1227 instance_config_move_strdup(&in
->jail
.hostname
, in_src
->jail
.hostname
);
1230 in
->config
= in_src
->config
;
1231 in_src
->config
= NULL
;
1235 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
1237 bool changed
= instance_config_changed(in
, in_new
);
1238 bool running
= in
->proc
.pending
;
1239 bool stopping
= in
->halt
;
1241 if (!running
|| stopping
) {
1242 instance_config_move(in
, in_new
);
1246 instance_restart(in
);
1247 instance_config_move(in
, in_new
);
1248 /* restart happens in the child callback handler */
1253 instance_free(struct service_instance
*in
)
1255 instance_free_stdio(in
);
1256 uloop_process_delete(&in
->proc
);
1257 uloop_timeout_cancel(&in
->timeout
);
1260 instance_config_cleanup(in
);
1265 free(in
->overlaydir
);
1266 free(in
->tmpoverlaysize
);
1267 free(in
->jail
.name
);
1268 free(in
->jail
.hostname
);
1275 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
1277 config
= blob_memdup(config
);
1279 in
->name
= blobmsg_name(config
);
1280 in
->config
= config
;
1281 in
->timeout
.cb
= instance_timeout
;
1282 in
->proc
.cb
= instance_exit
;
1283 in
->term_timeout
= 5;
1284 in
->syslog_facility
= LOG_DAEMON
;
1286 in
->require_jail
= false;
1288 in
->_stdout
.fd
.fd
= -2;
1289 in
->_stdout
.stream
.string_data
= true;
1290 in
->_stdout
.stream
.notify_read
= instance_stdout
;
1292 in
->_stderr
.fd
.fd
= -2;
1293 in
->_stderr
.stream
.string_data
= true;
1294 in
->_stderr
.stream
.notify_read
= instance_stderr
;
1296 in
->console
.fd
.fd
= -2;
1297 in
->console
.stream
.string_data
= true;
1298 in
->console
.stream
.notify_read
= instance_console
;
1300 in
->console_client
.fd
.fd
= -2;
1301 in
->console_client
.stream
.string_data
= true;
1302 in
->console_client
.stream
.notify_read
= instance_console_client
;
1304 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
1305 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
1306 blobmsg_list_simple_init(&in
->env
);
1307 blobmsg_list_simple_init(&in
->data
);
1308 blobmsg_list_simple_init(&in
->limits
);
1309 blobmsg_list_simple_init(&in
->errors
);
1310 blobmsg_list_simple_init(&in
->jail
.mount
);
1311 in
->valid
= instance_config_parse(in
);
1314 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
1321 i
= blobmsg_open_table(b
, in
->name
);
1322 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
1323 if (in
->proc
.pending
)
1324 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
1326 blobmsg_add_blob(b
, in
->command
);
1327 blobmsg_add_u32(b
, "term_timeout", in
->term_timeout
);
1328 if (!in
->proc
.pending
)
1329 blobmsg_add_u32(b
, "exit_code", in
->exit_code
);
1331 if (!avl_is_empty(&in
->errors
.avl
)) {
1332 struct blobmsg_list_node
*var
;
1333 void *e
= blobmsg_open_array(b
, "errors");
1334 blobmsg_list_for_each(&in
->errors
, var
)
1335 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
1336 blobmsg_close_table(b
, e
);
1339 if (!avl_is_empty(&in
->env
.avl
)) {
1340 struct blobmsg_list_node
*var
;
1341 void *e
= blobmsg_open_table(b
, "env");
1342 blobmsg_list_for_each(&in
->env
, var
)
1343 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1344 blobmsg_close_table(b
, e
);
1347 if (!avl_is_empty(&in
->data
.avl
)) {
1348 struct blobmsg_list_node
*var
;
1349 void *e
= blobmsg_open_table(b
, "data");
1350 blobmsg_list_for_each(&in
->data
, var
)
1351 blobmsg_add_blob(b
, var
->data
);
1352 blobmsg_close_table(b
, e
);
1355 if (!avl_is_empty(&in
->limits
.avl
)) {
1356 struct blobmsg_list_node
*var
;
1357 void *e
= blobmsg_open_table(b
, "limits");
1358 blobmsg_list_for_each(&in
->limits
, var
)
1359 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1360 blobmsg_close_table(b
, e
);
1363 if (in
->reload_signal
)
1364 blobmsg_add_u32(b
, "reload_signal", in
->reload_signal
);
1367 void *r
= blobmsg_open_table(b
, "respawn");
1368 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
1369 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
1370 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
1371 blobmsg_close_table(b
, r
);
1375 blobmsg_add_u8(b
, "trace", true);
1377 if (in
->no_new_privs
)
1378 blobmsg_add_u8(b
, "no_new_privs", true);
1381 blobmsg_add_string(b
, "seccomp", in
->seccomp
);
1384 blobmsg_add_string(b
, "pidfile", in
->pidfile
);
1387 blobmsg_add_string(b
, "user", in
->user
);
1390 blobmsg_add_string(b
, "group", in
->group
);
1393 void *r
= blobmsg_open_table(b
, "jail");
1395 blobmsg_add_string(b
, "name", in
->jail
.name
);
1396 if (in
->jail
.hostname
)
1397 blobmsg_add_string(b
, "hostname", in
->jail
.hostname
);
1399 blobmsg_add_u8(b
, "procfs", in
->jail
.procfs
);
1400 blobmsg_add_u8(b
, "sysfs", in
->jail
.sysfs
);
1401 blobmsg_add_u8(b
, "ubus", in
->jail
.ubus
);
1402 blobmsg_add_u8(b
, "log", in
->jail
.log
);
1403 blobmsg_add_u8(b
, "ronly", in
->jail
.ronly
);
1404 blobmsg_add_u8(b
, "netns", in
->jail
.netns
);
1405 blobmsg_add_u8(b
, "userns", in
->jail
.userns
);
1406 blobmsg_add_u8(b
, "cgroupsns", in
->jail
.cgroupsns
);
1407 blobmsg_add_u8(b
, "console", (in
->console
.fd
.fd
> -1));
1408 blobmsg_close_table(b
, r
);
1409 if (!avl_is_empty(&in
->jail
.mount
.avl
)) {
1410 struct blobmsg_list_node
*var
;
1411 void *e
= blobmsg_open_table(b
, "mount");
1412 blobmsg_list_for_each(&in
->jail
.mount
, var
)
1413 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1414 blobmsg_close_table(b
, e
);
1419 blobmsg_add_string(b
, "extroot", in
->extroot
);
1421 blobmsg_add_string(b
, "overlaydir", in
->overlaydir
);
1422 if (in
->tmpoverlaysize
)
1423 blobmsg_add_string(b
, "tmpoverlaysize", in
->tmpoverlaysize
);
1425 if (verbose
&& in
->trigger
)
1426 blobmsg_add_blob(b
, in
->trigger
);
1428 blobmsg_close_table(b
, i
);