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
},
112 JAIL_ATTR_REQUIREJAIL
,
116 static const struct blobmsg_policy jail_attr
[__JAIL_ATTR_MAX
] = {
117 [JAIL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
118 [JAIL_ATTR_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
119 [JAIL_ATTR_PROCFS
] = { "procfs", BLOBMSG_TYPE_BOOL
},
120 [JAIL_ATTR_SYSFS
] = { "sysfs", BLOBMSG_TYPE_BOOL
},
121 [JAIL_ATTR_UBUS
] = { "ubus", BLOBMSG_TYPE_BOOL
},
122 [JAIL_ATTR_LOG
] = { "log", BLOBMSG_TYPE_BOOL
},
123 [JAIL_ATTR_RONLY
] = { "ronly", BLOBMSG_TYPE_BOOL
},
124 [JAIL_ATTR_MOUNT
] = { "mount", BLOBMSG_TYPE_TABLE
},
125 [JAIL_ATTR_NETNS
] = { "netns", BLOBMSG_TYPE_BOOL
},
126 [JAIL_ATTR_USERNS
] = { "userns", BLOBMSG_TYPE_BOOL
},
127 [JAIL_ATTR_CGROUPSNS
] = { "cgroupsns", BLOBMSG_TYPE_BOOL
},
128 [JAIL_ATTR_REQUIREJAIL
] = { "requirejail", BLOBMSG_TYPE_BOOL
},
131 struct instance_netdev
{
132 struct blobmsg_list_node node
;
136 struct instance_file
{
137 struct blobmsg_list_node node
;
146 static const struct rlimit_name rlimit_names
[] = {
148 { "core", RLIMIT_CORE
},
149 { "cpu", RLIMIT_CPU
},
150 { "data", RLIMIT_DATA
},
151 { "fsize", RLIMIT_FSIZE
},
152 { "memlock", RLIMIT_MEMLOCK
},
153 { "nofile", RLIMIT_NOFILE
},
154 { "nproc", RLIMIT_NPROC
},
155 { "rss", RLIMIT_RSS
},
156 { "stack", RLIMIT_STACK
},
158 { "nice", RLIMIT_NICE
},
159 { "rtprio", RLIMIT_RTPRIO
},
160 { "msgqueue", RLIMIT_MSGQUEUE
},
161 { "sigpending", RLIMIT_SIGPENDING
},
166 static void closefd(int fd
)
168 if (fd
> STDERR_FILENO
)
172 /* convert a string into numeric syslog facility or return -1 if no match found */
174 syslog_facility_str_to_int(const char *facility
)
176 CODE
*p
= facilitynames
;
178 while (p
->c_name
&& strcasecmp(p
->c_name
, facility
))
185 instance_limits(const char *limit
, const char *value
)
189 unsigned long cur
, max
;
191 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
192 if (strcmp(rlimit_names
[i
].name
, limit
))
194 if (!strcmp(value
, "unlimited")) {
195 rlim
.rlim_cur
= RLIM_INFINITY
;
196 rlim
.rlim_max
= RLIM_INFINITY
;
198 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
204 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
211 setrlimit(rlimit_names
[i
].resource
, &rlim
);
217 jail_run(struct service_instance
*in
, char **argv
)
219 struct blobmsg_list_node
*var
;
220 struct jail
*jail
= &in
->jail
;
223 argv
[argc
++] = UJAIL_BIN_PATH
;
227 argv
[argc
++] = jail
->name
;
230 if (jail
->hostname
) {
232 argv
[argc
++] = jail
->hostname
;
237 argv
[argc
++] = in
->seccomp
;
242 argv
[argc
++] = in
->user
;
247 argv
[argc
++] = in
->group
;
250 if (in
->no_new_privs
)
279 argv
[argc
++] = in
->extroot
;
282 if (in
->overlaydir
) {
284 argv
[argc
++] = in
->overlaydir
;
287 if (in
->tmpoverlaysize
) {
289 argv
[argc
++] = in
->tmpoverlaysize
;
292 if (in
->require_jail
)
295 blobmsg_list_for_each(&jail
->mount
, var
) {
296 const char *type
= blobmsg_data(var
->data
);
302 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
311 instance_removepid(struct service_instance
*in
) {
314 if (unlink(in
->pidfile
)) {
315 ERROR("Failed to remove pidfile: %s: %m\n", in
->pidfile
);
322 instance_writepid(struct service_instance
*in
)
329 _pidfile
= fopen(in
->pidfile
, "w");
330 if (_pidfile
== NULL
) {
331 ERROR("failed to open pidfile for writing: %s: %m", in
->pidfile
);
334 if (fprintf(_pidfile
, "%d\n", in
->proc
.pid
) < 0) {
335 ERROR("failed to write pidfile: %s: %m", in
->pidfile
);
339 if (fclose(_pidfile
)) {
340 ERROR("failed to close pidfile: %s: %m", in
->pidfile
);
348 instance_run(struct service_instance
*in
, int _stdout
, int _stderr
)
350 struct blobmsg_list_node
*var
;
351 struct blob_attr
*cur
;
353 int argc
= 1; /* NULL terminated */
355 bool seccomp
= !in
->trace
&& !in
->has_jail
&& in
->seccomp
;
356 bool setlbf
= _stdout
>= 0;
359 setpriority(PRIO_PROCESS
, 0, in
->nice
);
361 blobmsg_for_each_attr(cur
, in
->command
, rem
)
364 blobmsg_list_for_each(&in
->env
, var
)
365 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
368 setenv("SECCOMP_FILE", in
->seccomp
, 1);
371 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
373 blobmsg_list_for_each(&in
->limits
, var
)
374 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
376 if (in
->trace
|| seccomp
)
379 argv
= alloca(sizeof(char *) * (argc
+ in
->jail
.argc
));
382 #ifdef SECCOMP_SUPPORT
384 argv
[argc
++] = "/sbin/utrace";
386 argv
[argc
++] = "/sbin/seccomp-trace";
388 if (in
->trace
|| seccomp
)
389 ULOG_WARN("Seccomp support for %s::%s not available\n", in
->srv
->name
, in
->name
);
393 argc
= jail_run(in
, argv
);
394 if (argc
!= in
->jail
.argc
)
395 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
396 in
->jail
.argc
, argc
, in
->srv
->name
, in
->name
);
399 blobmsg_for_each_attr(cur
, in
->command
, rem
)
400 argv
[argc
++] = blobmsg_data(cur
);
404 _stdin
= open("/dev/null", O_RDONLY
);
407 _stdout
= open("/dev/null", O_WRONLY
);
410 _stderr
= open("/dev/null", O_WRONLY
);
413 dup2(_stdin
, STDIN_FILENO
);
417 dup2(_stdout
, STDOUT_FILENO
);
421 dup2(_stderr
, STDERR_FILENO
);
425 if (!in
->has_jail
&& in
->user
&& in
->pw_gid
&& initgroups(in
->user
, in
->pw_gid
)) {
426 ERROR("failed to initgroups() for user %s: %m\n", in
->user
);
429 if (!in
->has_jail
&& in
->gr_gid
&& setgid(in
->gr_gid
)) {
430 ERROR("failed to set group id %d: %m\n", in
->gr_gid
);
433 if (!in
->has_jail
&& in
->uid
&& setuid(in
->uid
)) {
434 ERROR("failed to set user id %d: %m\n", in
->uid
);
438 execvp(argv
[0], argv
);
443 instance_free_stdio(struct service_instance
*in
)
445 if (in
->_stdout
.fd
.fd
> -1) {
446 ustream_free(&in
->_stdout
.stream
);
447 close(in
->_stdout
.fd
.fd
);
448 in
->_stdout
.fd
.fd
= -1;
451 if (in
->_stderr
.fd
.fd
> -1) {
452 ustream_free(&in
->_stderr
.stream
);
453 close(in
->_stderr
.fd
.fd
);
454 in
->_stderr
.fd
.fd
= -1;
459 instance_start(struct service_instance
*in
)
462 int opipe
[2] = { -1, -1 };
463 int epipe
[2] = { -1, -1 };
465 if (!avl_is_empty(&in
->errors
.avl
)) {
466 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
471 LOG("Not starting instance %s::%s, command not set\n", in
->srv
->name
, in
->name
);
475 if (in
->proc
.pending
) {
481 instance_free_stdio(in
);
482 if (in
->_stdout
.fd
.fd
> -2) {
484 ULOG_WARN("pipe() failed: %m\n");
485 opipe
[0] = opipe
[1] = -1;
489 if (in
->_stderr
.fd
.fd
> -2) {
491 ULOG_WARN("pipe() failed: %m\n");
492 epipe
[0] = epipe
[1] = -1;
510 instance_run(in
, opipe
[1], epipe
[1]);
514 DEBUG(2, "Started instance %s::%s[%d]\n", in
->srv
->name
, in
->name
, pid
);
516 instance_writepid(in
);
517 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
518 uloop_process_add(&in
->proc
);
521 ustream_fd_init(&in
->_stdout
, opipe
[0]);
523 fcntl(opipe
[0], F_SETFD
, FD_CLOEXEC
);
527 ustream_fd_init(&in
->_stderr
, epipe
[0]);
529 fcntl(epipe
[0], F_SETFD
, FD_CLOEXEC
);
532 service_event("instance.start", in
->srv
->name
, in
->name
);
536 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
538 char *newline
, *str
, *arg0
, ident
[32];
541 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
542 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
543 ulog_open(ULOG_SYSLOG
, in
->syslog_facility
, ident
);
546 str
= ustream_get_read_buf(s
, &len
);
550 newline
= memchr(str
, '\n', len
);
551 if (!newline
&& (s
->r
.buffer_len
!= len
))
556 len
= newline
+ 1 - str
;
558 ulog(prio
, "%s\n", str
);
560 ustream_consume(s
, len
);
563 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
567 instance_stdout(struct ustream
*s
, int bytes
)
569 instance_stdio(s
, LOG_INFO
,
570 container_of(s
, struct service_instance
, _stdout
.stream
));
574 instance_stderr(struct ustream
*s
, int bytes
)
576 instance_stdio(s
, LOG_ERR
,
577 container_of(s
, struct service_instance
, _stderr
.stream
));
581 instance_timeout(struct uloop_timeout
*t
)
583 struct service_instance
*in
;
585 in
= container_of(t
, struct service_instance
, timeout
);
588 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
589 in
->srv
->name
, in
->name
, in
->proc
.pid
);
590 kill(in
->proc
.pid
, SIGKILL
);
591 } else if (in
->restart
|| in
->respawn
)
596 instance_delete(struct service_instance
*in
)
598 struct service
*s
= in
->srv
;
600 avl_delete(&s
->instances
.avl
, &in
->node
.avl
);
606 instance_exit_code(int ret
)
608 if (WIFEXITED(ret
)) {
609 return WEXITSTATUS(ret
);
612 if (WIFSIGNALED(ret
)) {
613 return SIGNALLED_OFFSET
+ WTERMSIG(ret
);
616 if (WIFSTOPPED(ret
)) {
617 return WSTOPSIG(ret
);
624 instance_exit(struct uloop_process
*p
, int ret
)
626 struct service_instance
*in
;
630 in
= container_of(p
, struct service_instance
, proc
);
632 clock_gettime(CLOCK_MONOTONIC
, &tp
);
633 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
635 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
637 in
->exit_code
= instance_exit_code(ret
);
638 uloop_timeout_cancel(&in
->timeout
);
639 service_event("instance.stop", in
->srv
->name
, in
->name
);
642 instance_removepid(in
);
647 } else if (in
->restart
) {
649 } else if (in
->respawn
) {
650 if (runtime
< in
->respawn_threshold
)
653 in
->respawn_count
= 0;
654 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
655 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
656 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
657 in
->restart
= in
->respawn
= 0;
659 service_event("instance.fail", in
->srv
->name
, in
->name
);
661 service_event("instance.respawn", in
->srv
->name
, in
->name
);
662 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
668 instance_stop(struct service_instance
*in
, bool halt
)
670 if (!in
->proc
.pending
) {
676 in
->restart
= in
->respawn
= false;
677 kill(in
->proc
.pid
, SIGTERM
);
678 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
682 instance_restart(struct service_instance
*in
)
684 if (!in
->proc
.pending
)
687 if (in
->reload_signal
) {
688 kill(in
->proc
.pid
, in
->reload_signal
);
694 kill(in
->proc
.pid
, SIGTERM
);
695 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
698 static bool string_changed(const char *a
, const char *b
)
700 return !((!a
&& !b
) || (a
&& b
&& !strcmp(a
, b
)));
704 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
709 if (!blob_attr_equal(in
->command
, in_new
->command
))
712 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
715 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
718 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
721 if (in
->nice
!= in_new
->nice
)
724 if (in
->syslog_facility
!= in_new
->syslog_facility
)
727 if (string_changed(in
->user
, in_new
->user
))
730 if (string_changed(in
->group
, in_new
->group
))
733 if (in
->uid
!= in_new
->uid
)
736 if (in
->pw_gid
!= in_new
->pw_gid
)
739 if (string_changed(in
->pidfile
, in_new
->pidfile
))
742 if (in
->respawn_retry
!= in_new
->respawn_retry
)
744 if (in
->respawn_threshold
!= in_new
->respawn_threshold
)
746 if (in
->respawn_timeout
!= in_new
->respawn_timeout
)
749 if ((!in
->seccomp
&& in_new
->seccomp
) ||
750 (in
->seccomp
&& !in_new
->seccomp
) ||
751 (in
->seccomp
&& in_new
->seccomp
&& strcmp(in
->seccomp
, in_new
->seccomp
)))
754 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
757 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
760 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
767 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
769 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
770 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
772 return n1
->ifindex
== n2
->ifindex
;
776 instance_netdev_update(struct blobmsg_list_node
*l
)
778 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
780 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
784 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
786 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
787 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
789 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
793 instance_file_update(struct blobmsg_list_node
*l
)
795 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
800 memset(f
->md5
, 0, sizeof(f
->md5
));
802 fd
= open(l
->avl
.key
, O_RDONLY
);
808 len
= read(fd
, buf
, sizeof(buf
));
818 md5_hash(buf
, len
, &md5
);
821 md5_end(f
->md5
, &md5
);
826 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
831 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
835 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
837 struct blobmsg_list_node
*node
;
842 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
845 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
847 blobmsg_list_for_each(l
, node
)
854 instance_jail_parse(struct service_instance
*in
, struct blob_attr
*attr
)
856 struct blob_attr
*tb
[__JAIL_ATTR_MAX
];
857 struct jail
*jail
= &in
->jail
;
859 blobmsg_parse(jail_attr
, __JAIL_ATTR_MAX
, tb
,
860 blobmsg_data(attr
), blobmsg_data_len(attr
));
864 if (tb
[JAIL_ATTR_REQUIREJAIL
]) {
865 in
->require_jail
= true;
868 if (tb
[JAIL_ATTR_NAME
]) {
869 jail
->name
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_NAME
]));
872 if (tb
[JAIL_ATTR_HOSTNAME
]) {
873 jail
->hostname
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_HOSTNAME
]));
876 if (tb
[JAIL_ATTR_PROCFS
]) {
877 jail
->procfs
= blobmsg_get_bool(tb
[JAIL_ATTR_PROCFS
]);
880 if (tb
[JAIL_ATTR_SYSFS
]) {
881 jail
->sysfs
= blobmsg_get_bool(tb
[JAIL_ATTR_SYSFS
]);
884 if (tb
[JAIL_ATTR_UBUS
]) {
885 jail
->ubus
= blobmsg_get_bool(tb
[JAIL_ATTR_UBUS
]);
888 if (tb
[JAIL_ATTR_LOG
]) {
889 jail
->log
= blobmsg_get_bool(tb
[JAIL_ATTR_LOG
]);
892 if (tb
[JAIL_ATTR_RONLY
]) {
893 jail
->ronly
= blobmsg_get_bool(tb
[JAIL_ATTR_RONLY
]);
896 if (tb
[JAIL_ATTR_NETNS
]) {
897 jail
->netns
= blobmsg_get_bool(tb
[JAIL_ATTR_NETNS
]);
900 if (tb
[JAIL_ATTR_USERNS
]) {
901 jail
->userns
= blobmsg_get_bool(tb
[JAIL_ATTR_USERNS
]);
904 if (tb
[JAIL_ATTR_CGROUPSNS
]) {
905 jail
->cgroupsns
= blobmsg_get_bool(tb
[JAIL_ATTR_CGROUPSNS
]);
909 if (tb
[JAIL_ATTR_MOUNT
]) {
910 struct blob_attr
*cur
;
913 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_MOUNT
], rem
)
915 instance_fill_array(&jail
->mount
, tb
[JAIL_ATTR_MOUNT
], NULL
, false);
932 if (in
->tmpoverlaysize
)
935 if (in
->no_new_privs
)
942 instance_config_parse_command(struct service_instance
*in
, struct blob_attr
**tb
)
944 struct blob_attr
*cur
, *cur2
;
948 cur
= tb
[INSTANCE_ATTR_COMMAND
];
954 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
957 blobmsg_for_each_attr(cur2
, cur
, rem
) {
967 instance_config_parse(struct service_instance
*in
)
969 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
970 struct blob_attr
*cur
, *cur2
;
974 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
975 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
977 if (!instance_config_parse_command(in
, tb
))
980 if (tb
[INSTANCE_ATTR_TERMTIMEOUT
])
981 in
->term_timeout
= blobmsg_get_u32(tb
[INSTANCE_ATTR_TERMTIMEOUT
]);
982 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
984 uint32_t vals
[3] = { 3600, 5, 5};
986 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
987 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
989 vals
[i
] = atoi(blobmsg_get_string(cur2
));
993 in
->respawn_count
= 0;
994 in
->respawn_threshold
= vals
[0];
995 in
->respawn_timeout
= vals
[1];
996 in
->respawn_retry
= vals
[2];
998 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
999 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
1000 trigger_add(in
->trigger
, in
);
1003 if (tb
[INSTANCE_ATTR_WATCH
]) {
1004 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
1005 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
1007 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
1008 watch_add(blobmsg_get_string(cur2
), in
);
1012 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
1013 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
1014 if (in
->nice
< -20 || in
->nice
> 20)
1018 if (tb
[INSTANCE_ATTR_USER
]) {
1019 const char *user
= blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]);
1020 struct passwd
*p
= getpwnam(user
);
1022 in
->user
= strdup(user
);
1023 in
->uid
= p
->pw_uid
;
1024 in
->gr_gid
= in
->pw_gid
= p
->pw_gid
;
1028 if (tb
[INSTANCE_ATTR_GROUP
]) {
1029 const char *group
= blobmsg_get_string(tb
[INSTANCE_ATTR_GROUP
]);
1030 struct group
*p
= getgrnam(group
);
1032 in
->group
= strdup(group
);
1033 in
->gr_gid
= p
->gr_gid
;
1037 if (tb
[INSTANCE_ATTR_TRACE
])
1038 in
->trace
= blobmsg_get_bool(tb
[INSTANCE_ATTR_TRACE
]);
1040 if (tb
[INSTANCE_ATTR_NO_NEW_PRIVS
])
1041 in
->no_new_privs
= blobmsg_get_bool(tb
[INSTANCE_ATTR_NO_NEW_PRIVS
]);
1043 if (!in
->trace
&& tb
[INSTANCE_ATTR_SECCOMP
])
1044 in
->seccomp
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_SECCOMP
]));
1046 if (tb
[INSTANCE_ATTR_EXTROOT
])
1047 in
->extroot
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_EXTROOT
]));
1049 if (tb
[INSTANCE_ATTR_OVERLAYDIR
])
1050 in
->overlaydir
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_OVERLAYDIR
]));
1052 if (tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
])
1053 in
->tmpoverlaysize
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_TMPOVERLAYSIZE
]));
1055 if (tb
[INSTANCE_ATTR_PIDFILE
]) {
1056 char *pidfile
= blobmsg_get_string(tb
[INSTANCE_ATTR_PIDFILE
]);
1058 in
->pidfile
= strdup(pidfile
);
1061 if (tb
[INSTANCE_ATTR_RELOADSIG
])
1062 in
->reload_signal
= blobmsg_get_u32(tb
[INSTANCE_ATTR_RELOADSIG
]);
1064 if (!in
->trace
&& tb
[INSTANCE_ATTR_JAIL
])
1065 in
->has_jail
= instance_jail_parse(in
, tb
[INSTANCE_ATTR_JAIL
]);
1068 r
= stat(UJAIL_BIN_PATH
, &s
);
1070 if (in
->require_jail
) {
1071 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1072 in
->srv
->name
, in
->name
, UJAIL_BIN_PATH
, r
);
1075 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH
, r
);
1076 in
->has_jail
= false;
1080 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
1081 in
->_stdout
.fd
.fd
= -1;
1083 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
1084 in
->_stderr
.fd
.fd
= -1;
1086 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
1088 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
1091 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
1094 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
1097 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
1100 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
1103 if (tb
[INSTANCE_ATTR_FACILITY
]) {
1104 int facility
= syslog_facility_str_to_int(blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1105 if (facility
!= -1) {
1106 in
->syslog_facility
= facility
;
1107 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1109 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1116 instance_config_cleanup(struct service_instance
*in
)
1118 blobmsg_list_free(&in
->env
);
1119 blobmsg_list_free(&in
->data
);
1120 blobmsg_list_free(&in
->netdev
);
1121 blobmsg_list_free(&in
->file
);
1122 blobmsg_list_free(&in
->limits
);
1123 blobmsg_list_free(&in
->errors
);
1124 blobmsg_list_free(&in
->jail
.mount
);
1128 instance_config_move_strdup(char **dst
, char *src
)
1142 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
1144 instance_config_cleanup(in
);
1145 blobmsg_list_move(&in
->env
, &in_src
->env
);
1146 blobmsg_list_move(&in
->data
, &in_src
->data
);
1147 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
1148 blobmsg_list_move(&in
->file
, &in_src
->file
);
1149 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
1150 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
1151 blobmsg_list_move(&in
->jail
.mount
, &in_src
->jail
.mount
);
1152 in
->trigger
= in_src
->trigger
;
1153 in
->command
= in_src
->command
;
1154 in
->respawn
= in_src
->respawn
;
1155 in
->respawn_retry
= in_src
->respawn_retry
;
1156 in
->respawn_threshold
= in_src
->respawn_threshold
;
1157 in
->respawn_timeout
= in_src
->respawn_timeout
;
1158 in
->name
= in_src
->name
;
1159 in
->trace
= in_src
->trace
;
1160 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
1161 in
->syslog_facility
= in_src
->syslog_facility
;
1163 instance_config_move_strdup(&in
->pidfile
, in_src
->pidfile
);
1164 instance_config_move_strdup(&in
->seccomp
, in_src
->seccomp
);
1165 instance_config_move_strdup(&in
->jail
.name
, in_src
->jail
.name
);
1166 instance_config_move_strdup(&in
->jail
.hostname
, in_src
->jail
.hostname
);
1169 in
->config
= in_src
->config
;
1170 in_src
->config
= NULL
;
1174 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
1176 bool changed
= instance_config_changed(in
, in_new
);
1177 bool running
= in
->proc
.pending
;
1178 bool stopping
= in
->halt
;
1180 if (!running
|| stopping
) {
1181 instance_config_move(in
, in_new
);
1185 instance_restart(in
);
1186 instance_config_move(in
, in_new
);
1187 /* restart happens in the child callback handler */
1192 instance_free(struct service_instance
*in
)
1194 instance_free_stdio(in
);
1195 uloop_process_delete(&in
->proc
);
1196 uloop_timeout_cancel(&in
->timeout
);
1199 instance_config_cleanup(in
);
1204 free(in
->overlaydir
);
1205 free(in
->tmpoverlaysize
);
1206 free(in
->jail
.name
);
1207 free(in
->jail
.hostname
);
1214 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
1216 config
= blob_memdup(config
);
1218 in
->name
= blobmsg_name(config
);
1219 in
->config
= config
;
1220 in
->timeout
.cb
= instance_timeout
;
1221 in
->proc
.cb
= instance_exit
;
1222 in
->term_timeout
= 5;
1223 in
->syslog_facility
= LOG_DAEMON
;
1225 in
->require_jail
= false;
1227 in
->_stdout
.fd
.fd
= -2;
1228 in
->_stdout
.stream
.string_data
= true;
1229 in
->_stdout
.stream
.notify_read
= instance_stdout
;
1231 in
->_stderr
.fd
.fd
= -2;
1232 in
->_stderr
.stream
.string_data
= true;
1233 in
->_stderr
.stream
.notify_read
= instance_stderr
;
1235 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
1236 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
1237 blobmsg_list_simple_init(&in
->env
);
1238 blobmsg_list_simple_init(&in
->data
);
1239 blobmsg_list_simple_init(&in
->limits
);
1240 blobmsg_list_simple_init(&in
->errors
);
1241 blobmsg_list_simple_init(&in
->jail
.mount
);
1242 in
->valid
= instance_config_parse(in
);
1245 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
1252 i
= blobmsg_open_table(b
, in
->name
);
1253 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
1254 if (in
->proc
.pending
)
1255 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
1257 blobmsg_add_blob(b
, in
->command
);
1258 blobmsg_add_u32(b
, "term_timeout", in
->term_timeout
);
1259 if (!in
->proc
.pending
)
1260 blobmsg_add_u32(b
, "exit_code", in
->exit_code
);
1262 if (!avl_is_empty(&in
->errors
.avl
)) {
1263 struct blobmsg_list_node
*var
;
1264 void *e
= blobmsg_open_array(b
, "errors");
1265 blobmsg_list_for_each(&in
->errors
, var
)
1266 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
1267 blobmsg_close_table(b
, e
);
1270 if (!avl_is_empty(&in
->env
.avl
)) {
1271 struct blobmsg_list_node
*var
;
1272 void *e
= blobmsg_open_table(b
, "env");
1273 blobmsg_list_for_each(&in
->env
, var
)
1274 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1275 blobmsg_close_table(b
, e
);
1278 if (!avl_is_empty(&in
->data
.avl
)) {
1279 struct blobmsg_list_node
*var
;
1280 void *e
= blobmsg_open_table(b
, "data");
1281 blobmsg_list_for_each(&in
->data
, var
)
1282 blobmsg_add_blob(b
, var
->data
);
1283 blobmsg_close_table(b
, e
);
1286 if (!avl_is_empty(&in
->limits
.avl
)) {
1287 struct blobmsg_list_node
*var
;
1288 void *e
= blobmsg_open_table(b
, "limits");
1289 blobmsg_list_for_each(&in
->limits
, var
)
1290 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1291 blobmsg_close_table(b
, e
);
1294 if (in
->reload_signal
)
1295 blobmsg_add_u32(b
, "reload_signal", in
->reload_signal
);
1298 void *r
= blobmsg_open_table(b
, "respawn");
1299 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
1300 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
1301 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
1302 blobmsg_close_table(b
, r
);
1306 blobmsg_add_u8(b
, "trace", true);
1308 if (in
->no_new_privs
)
1309 blobmsg_add_u8(b
, "no_new_privs", true);
1312 blobmsg_add_string(b
, "seccomp", in
->seccomp
);
1315 blobmsg_add_string(b
, "pidfile", in
->pidfile
);
1318 blobmsg_add_string(b
, "user", in
->user
);
1321 blobmsg_add_string(b
, "group", in
->group
);
1324 void *r
= blobmsg_open_table(b
, "jail");
1326 blobmsg_add_string(b
, "name", in
->jail
.name
);
1327 if (in
->jail
.hostname
)
1328 blobmsg_add_string(b
, "hostname", in
->jail
.hostname
);
1330 blobmsg_add_u8(b
, "procfs", in
->jail
.procfs
);
1331 blobmsg_add_u8(b
, "sysfs", in
->jail
.sysfs
);
1332 blobmsg_add_u8(b
, "ubus", in
->jail
.ubus
);
1333 blobmsg_add_u8(b
, "log", in
->jail
.log
);
1334 blobmsg_add_u8(b
, "ronly", in
->jail
.ronly
);
1335 blobmsg_add_u8(b
, "netns", in
->jail
.netns
);
1336 blobmsg_add_u8(b
, "userns", in
->jail
.userns
);
1337 blobmsg_add_u8(b
, "cgroupsns", in
->jail
.cgroupsns
);
1338 blobmsg_close_table(b
, r
);
1339 if (!avl_is_empty(&in
->jail
.mount
.avl
)) {
1340 struct blobmsg_list_node
*var
;
1341 void *e
= blobmsg_open_table(b
, "mount");
1342 blobmsg_list_for_each(&in
->jail
.mount
, var
)
1343 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1344 blobmsg_close_table(b
, e
);
1349 blobmsg_add_string(b
, "extroot", in
->extroot
);
1351 blobmsg_add_string(b
, "overlaydir", in
->overlaydir
);
1352 if (in
->tmpoverlaysize
)
1353 blobmsg_add_string(b
, "tmpoverlaysize", in
->tmpoverlaysize
);
1355 if (verbose
&& in
->trigger
)
1356 blobmsg_add_blob(b
, in
->trigger
);
1358 blobmsg_close_table(b
, i
);