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
,
69 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
70 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
71 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
72 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
73 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
74 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
75 [INSTANCE_ATTR_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
76 [INSTANCE_ATTR_RESPAWN
] = { "respawn", BLOBMSG_TYPE_ARRAY
},
77 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
78 [INSTANCE_ATTR_LIMITS
] = { "limits", BLOBMSG_TYPE_TABLE
},
79 [INSTANCE_ATTR_WATCH
] = { "watch", BLOBMSG_TYPE_ARRAY
},
80 [INSTANCE_ATTR_ERROR
] = { "error", BLOBMSG_TYPE_ARRAY
},
81 [INSTANCE_ATTR_USER
] = { "user", BLOBMSG_TYPE_STRING
},
82 [INSTANCE_ATTR_GROUP
] = { "group", BLOBMSG_TYPE_STRING
},
83 [INSTANCE_ATTR_STDOUT
] = { "stdout", BLOBMSG_TYPE_BOOL
},
84 [INSTANCE_ATTR_STDERR
] = { "stderr", BLOBMSG_TYPE_BOOL
},
85 [INSTANCE_ATTR_NO_NEW_PRIVS
] = { "no_new_privs", BLOBMSG_TYPE_BOOL
},
86 [INSTANCE_ATTR_JAIL
] = { "jail", BLOBMSG_TYPE_TABLE
},
87 [INSTANCE_ATTR_TRACE
] = { "trace", BLOBMSG_TYPE_BOOL
},
88 [INSTANCE_ATTR_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_STRING
},
89 [INSTANCE_ATTR_PIDFILE
] = { "pidfile", BLOBMSG_TYPE_STRING
},
90 [INSTANCE_ATTR_RELOADSIG
] = { "reload_signal", BLOBMSG_TYPE_INT32
},
91 [INSTANCE_ATTR_TERMTIMEOUT
] = { "term_timeout", BLOBMSG_TYPE_INT32
},
92 [INSTANCE_ATTR_FACILITY
] = { "facility", BLOBMSG_TYPE_STRING
},
93 [INSTANCE_ATTR_EXTROOT
] = { "extroot", BLOBMSG_TYPE_STRING
},
108 JAIL_ATTR_REQUIREJAIL
,
112 static const struct blobmsg_policy jail_attr
[__JAIL_ATTR_MAX
] = {
113 [JAIL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
114 [JAIL_ATTR_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
115 [JAIL_ATTR_PROCFS
] = { "procfs", BLOBMSG_TYPE_BOOL
},
116 [JAIL_ATTR_SYSFS
] = { "sysfs", BLOBMSG_TYPE_BOOL
},
117 [JAIL_ATTR_UBUS
] = { "ubus", BLOBMSG_TYPE_BOOL
},
118 [JAIL_ATTR_LOG
] = { "log", BLOBMSG_TYPE_BOOL
},
119 [JAIL_ATTR_RONLY
] = { "ronly", BLOBMSG_TYPE_BOOL
},
120 [JAIL_ATTR_MOUNT
] = { "mount", BLOBMSG_TYPE_TABLE
},
121 [JAIL_ATTR_NETNS
] = { "netns", BLOBMSG_TYPE_BOOL
},
122 [JAIL_ATTR_USERNS
] = { "userns", BLOBMSG_TYPE_BOOL
},
123 [JAIL_ATTR_CGROUPSNS
] = { "cgroupsns", BLOBMSG_TYPE_BOOL
},
124 [JAIL_ATTR_REQUIREJAIL
] = { "requirejail", BLOBMSG_TYPE_BOOL
},
127 struct instance_netdev
{
128 struct blobmsg_list_node node
;
132 struct instance_file
{
133 struct blobmsg_list_node node
;
142 static const struct rlimit_name rlimit_names
[] = {
144 { "core", RLIMIT_CORE
},
145 { "cpu", RLIMIT_CPU
},
146 { "data", RLIMIT_DATA
},
147 { "fsize", RLIMIT_FSIZE
},
148 { "memlock", RLIMIT_MEMLOCK
},
149 { "nofile", RLIMIT_NOFILE
},
150 { "nproc", RLIMIT_NPROC
},
151 { "rss", RLIMIT_RSS
},
152 { "stack", RLIMIT_STACK
},
154 { "nice", RLIMIT_NICE
},
155 { "rtprio", RLIMIT_RTPRIO
},
156 { "msgqueue", RLIMIT_MSGQUEUE
},
157 { "sigpending", RLIMIT_SIGPENDING
},
162 static void closefd(int fd
)
164 if (fd
> STDERR_FILENO
)
168 /* convert a string into numeric syslog facility or return -1 if no match found */
170 syslog_facility_str_to_int(const char *facility
)
172 CODE
*p
= facilitynames
;
174 while (p
->c_name
&& strcasecmp(p
->c_name
, facility
))
181 instance_limits(const char *limit
, const char *value
)
185 unsigned long cur
, max
;
187 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
188 if (strcmp(rlimit_names
[i
].name
, limit
))
190 if (!strcmp(value
, "unlimited")) {
191 rlim
.rlim_cur
= RLIM_INFINITY
;
192 rlim
.rlim_max
= RLIM_INFINITY
;
194 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
200 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
207 setrlimit(rlimit_names
[i
].resource
, &rlim
);
213 jail_run(struct service_instance
*in
, char **argv
)
215 struct blobmsg_list_node
*var
;
216 struct jail
*jail
= &in
->jail
;
219 argv
[argc
++] = UJAIL_BIN_PATH
;
223 argv
[argc
++] = jail
->name
;
226 if (jail
->hostname
) {
228 argv
[argc
++] = jail
->hostname
;
233 argv
[argc
++] = in
->seccomp
;
238 argv
[argc
++] = in
->user
;
243 argv
[argc
++] = in
->group
;
246 if (in
->no_new_privs
)
275 argv
[argc
++] = in
->extroot
;
278 blobmsg_list_for_each(&jail
->mount
, var
) {
279 const char *type
= blobmsg_data(var
->data
);
285 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
294 instance_removepid(struct service_instance
*in
) {
297 if (unlink(in
->pidfile
)) {
298 ERROR("Failed to remove pidfile: %s: %m\n", in
->pidfile
);
305 instance_writepid(struct service_instance
*in
)
312 _pidfile
= fopen(in
->pidfile
, "w");
313 if (_pidfile
== NULL
) {
314 ERROR("failed to open pidfile for writing: %s: %m", in
->pidfile
);
317 if (fprintf(_pidfile
, "%d\n", in
->proc
.pid
) < 0) {
318 ERROR("failed to write pidfile: %s: %m", in
->pidfile
);
322 if (fclose(_pidfile
)) {
323 ERROR("failed to close pidfile: %s: %m", in
->pidfile
);
331 instance_run(struct service_instance
*in
, int _stdout
, int _stderr
)
333 struct blobmsg_list_node
*var
;
334 struct blob_attr
*cur
;
336 int argc
= 1; /* NULL terminated */
338 bool seccomp
= !in
->trace
&& !in
->has_jail
&& in
->seccomp
;
339 bool setlbf
= _stdout
>= 0;
342 setpriority(PRIO_PROCESS
, 0, in
->nice
);
344 blobmsg_for_each_attr(cur
, in
->command
, rem
)
347 blobmsg_list_for_each(&in
->env
, var
)
348 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
351 setenv("SECCOMP_FILE", in
->seccomp
, 1);
354 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
356 blobmsg_list_for_each(&in
->limits
, var
)
357 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
359 if (in
->trace
|| seccomp
)
362 argv
= alloca(sizeof(char *) * (argc
+ in
->jail
.argc
));
365 #ifdef SECCOMP_SUPPORT
367 argv
[argc
++] = "/sbin/utrace";
369 argv
[argc
++] = "/sbin/seccomp-trace";
371 if (in
->trace
|| seccomp
)
372 ULOG_WARN("Seccomp support for %s::%s not available\n", in
->srv
->name
, in
->name
);
376 argc
= jail_run(in
, argv
);
377 if (argc
!= in
->jail
.argc
)
378 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
379 in
->jail
.argc
, argc
, in
->srv
->name
, in
->name
);
382 blobmsg_for_each_attr(cur
, in
->command
, rem
)
383 argv
[argc
++] = blobmsg_data(cur
);
387 _stdin
= open("/dev/null", O_RDONLY
);
390 _stdout
= open("/dev/null", O_WRONLY
);
393 _stderr
= open("/dev/null", O_WRONLY
);
396 dup2(_stdin
, STDIN_FILENO
);
400 dup2(_stdout
, STDOUT_FILENO
);
404 dup2(_stderr
, STDERR_FILENO
);
408 if (!in
->has_jail
&& in
->user
&& in
->pw_gid
&& initgroups(in
->user
, in
->pw_gid
)) {
409 ERROR("failed to initgroups() for user %s: %m\n", in
->user
);
412 if (!in
->has_jail
&& in
->gr_gid
&& setgid(in
->gr_gid
)) {
413 ERROR("failed to set group id %d: %m\n", in
->gr_gid
);
416 if (!in
->has_jail
&& in
->uid
&& setuid(in
->uid
)) {
417 ERROR("failed to set user id %d: %m\n", in
->uid
);
421 execvp(argv
[0], argv
);
426 instance_free_stdio(struct service_instance
*in
)
428 if (in
->_stdout
.fd
.fd
> -1) {
429 ustream_free(&in
->_stdout
.stream
);
430 close(in
->_stdout
.fd
.fd
);
431 in
->_stdout
.fd
.fd
= -1;
434 if (in
->_stderr
.fd
.fd
> -1) {
435 ustream_free(&in
->_stderr
.stream
);
436 close(in
->_stderr
.fd
.fd
);
437 in
->_stderr
.fd
.fd
= -1;
442 instance_start(struct service_instance
*in
)
445 int opipe
[2] = { -1, -1 };
446 int epipe
[2] = { -1, -1 };
448 if (!avl_is_empty(&in
->errors
.avl
)) {
449 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
454 LOG("Not starting instance %s::%s, command not set\n", in
->srv
->name
, in
->name
);
458 if (in
->proc
.pending
) {
464 instance_free_stdio(in
);
465 if (in
->_stdout
.fd
.fd
> -2) {
467 ULOG_WARN("pipe() failed: %m\n");
468 opipe
[0] = opipe
[1] = -1;
472 if (in
->_stderr
.fd
.fd
> -2) {
474 ULOG_WARN("pipe() failed: %m\n");
475 epipe
[0] = epipe
[1] = -1;
493 instance_run(in
, opipe
[1], epipe
[1]);
497 DEBUG(2, "Started instance %s::%s[%d]\n", in
->srv
->name
, in
->name
, pid
);
499 instance_writepid(in
);
500 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
501 uloop_process_add(&in
->proc
);
504 ustream_fd_init(&in
->_stdout
, opipe
[0]);
506 fcntl(opipe
[0], F_SETFD
, FD_CLOEXEC
);
510 ustream_fd_init(&in
->_stderr
, epipe
[0]);
512 fcntl(epipe
[0], F_SETFD
, FD_CLOEXEC
);
515 service_event("instance.start", in
->srv
->name
, in
->name
);
519 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
521 char *newline
, *str
, *arg0
, ident
[32];
524 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
525 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
526 ulog_open(ULOG_SYSLOG
, in
->syslog_facility
, ident
);
529 str
= ustream_get_read_buf(s
, &len
);
533 newline
= memchr(str
, '\n', len
);
534 if (!newline
&& (s
->r
.buffer_len
!= len
))
539 len
= newline
+ 1 - str
;
541 ulog(prio
, "%s\n", str
);
543 ustream_consume(s
, len
);
546 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
550 instance_stdout(struct ustream
*s
, int bytes
)
552 instance_stdio(s
, LOG_INFO
,
553 container_of(s
, struct service_instance
, _stdout
.stream
));
557 instance_stderr(struct ustream
*s
, int bytes
)
559 instance_stdio(s
, LOG_ERR
,
560 container_of(s
, struct service_instance
, _stderr
.stream
));
564 instance_timeout(struct uloop_timeout
*t
)
566 struct service_instance
*in
;
568 in
= container_of(t
, struct service_instance
, timeout
);
571 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
572 in
->srv
->name
, in
->name
, in
->proc
.pid
);
573 kill(in
->proc
.pid
, SIGKILL
);
574 } else if (in
->restart
|| in
->respawn
)
579 instance_delete(struct service_instance
*in
)
581 struct service
*s
= in
->srv
;
583 avl_delete(&s
->instances
.avl
, &in
->node
.avl
);
589 instance_exit_code(int ret
)
591 if (WIFEXITED(ret
)) {
592 return WEXITSTATUS(ret
);
595 if (WIFSIGNALED(ret
)) {
596 return SIGNALLED_OFFSET
+ WTERMSIG(ret
);
599 if (WIFSTOPPED(ret
)) {
600 return WSTOPSIG(ret
);
607 instance_exit(struct uloop_process
*p
, int ret
)
609 struct service_instance
*in
;
613 in
= container_of(p
, struct service_instance
, proc
);
615 clock_gettime(CLOCK_MONOTONIC
, &tp
);
616 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
618 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
620 in
->exit_code
= instance_exit_code(ret
);
621 uloop_timeout_cancel(&in
->timeout
);
622 service_event("instance.stop", in
->srv
->name
, in
->name
);
625 instance_removepid(in
);
630 } else if (in
->restart
) {
632 } else if (in
->respawn
) {
633 if (runtime
< in
->respawn_threshold
)
636 in
->respawn_count
= 0;
637 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
638 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
639 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
640 in
->restart
= in
->respawn
= 0;
642 service_event("instance.fail", in
->srv
->name
, in
->name
);
644 service_event("instance.respawn", in
->srv
->name
, in
->name
);
645 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
651 instance_stop(struct service_instance
*in
, bool halt
)
653 if (!in
->proc
.pending
) {
659 in
->restart
= in
->respawn
= false;
660 kill(in
->proc
.pid
, SIGTERM
);
661 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
665 instance_restart(struct service_instance
*in
)
667 if (!in
->proc
.pending
)
670 if (in
->reload_signal
) {
671 kill(in
->proc
.pid
, in
->reload_signal
);
677 kill(in
->proc
.pid
, SIGTERM
);
678 uloop_timeout_set(&in
->timeout
, in
->term_timeout
* 1000);
681 static bool string_changed(const char *a
, const char *b
)
683 return !((!a
&& !b
) || (a
&& b
&& !strcmp(a
, b
)));
687 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
692 if (!blob_attr_equal(in
->command
, in_new
->command
))
695 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
698 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
701 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
704 if (in
->nice
!= in_new
->nice
)
707 if (in
->syslog_facility
!= in_new
->syslog_facility
)
710 if (string_changed(in
->user
, in_new
->user
))
713 if (string_changed(in
->group
, in_new
->group
))
716 if (in
->uid
!= in_new
->uid
)
719 if (in
->pw_gid
!= in_new
->pw_gid
)
722 if (string_changed(in
->pidfile
, in_new
->pidfile
))
725 if (in
->respawn_retry
!= in_new
->respawn_retry
)
727 if (in
->respawn_threshold
!= in_new
->respawn_threshold
)
729 if (in
->respawn_timeout
!= in_new
->respawn_timeout
)
732 if ((!in
->seccomp
&& in_new
->seccomp
) ||
733 (in
->seccomp
&& !in_new
->seccomp
) ||
734 (in
->seccomp
&& in_new
->seccomp
&& strcmp(in
->seccomp
, in_new
->seccomp
)))
737 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
740 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
743 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
750 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
752 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
753 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
755 return n1
->ifindex
== n2
->ifindex
;
759 instance_netdev_update(struct blobmsg_list_node
*l
)
761 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
763 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
767 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
769 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
770 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
772 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
776 instance_file_update(struct blobmsg_list_node
*l
)
778 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
783 memset(f
->md5
, 0, sizeof(f
->md5
));
785 fd
= open(l
->avl
.key
, O_RDONLY
);
791 len
= read(fd
, buf
, sizeof(buf
));
801 md5_hash(buf
, len
, &md5
);
804 md5_end(f
->md5
, &md5
);
809 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
814 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
818 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
820 struct blobmsg_list_node
*node
;
825 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
828 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
830 blobmsg_list_for_each(l
, node
)
837 instance_jail_parse(struct service_instance
*in
, struct blob_attr
*attr
)
839 struct blob_attr
*tb
[__JAIL_ATTR_MAX
];
840 struct jail
*jail
= &in
->jail
;
842 blobmsg_parse(jail_attr
, __JAIL_ATTR_MAX
, tb
,
843 blobmsg_data(attr
), blobmsg_data_len(attr
));
847 if (tb
[JAIL_ATTR_REQUIREJAIL
]) {
848 in
->require_jail
= true;
851 if (tb
[JAIL_ATTR_NAME
]) {
852 jail
->name
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_NAME
]));
855 if (tb
[JAIL_ATTR_HOSTNAME
]) {
856 jail
->hostname
= strdup(blobmsg_get_string(tb
[JAIL_ATTR_HOSTNAME
]));
859 if (tb
[JAIL_ATTR_PROCFS
]) {
860 jail
->procfs
= blobmsg_get_bool(tb
[JAIL_ATTR_PROCFS
]);
863 if (tb
[JAIL_ATTR_SYSFS
]) {
864 jail
->sysfs
= blobmsg_get_bool(tb
[JAIL_ATTR_SYSFS
]);
867 if (tb
[JAIL_ATTR_UBUS
]) {
868 jail
->ubus
= blobmsg_get_bool(tb
[JAIL_ATTR_UBUS
]);
871 if (tb
[JAIL_ATTR_LOG
]) {
872 jail
->log
= blobmsg_get_bool(tb
[JAIL_ATTR_LOG
]);
875 if (tb
[JAIL_ATTR_RONLY
]) {
876 jail
->ronly
= blobmsg_get_bool(tb
[JAIL_ATTR_RONLY
]);
879 if (tb
[JAIL_ATTR_NETNS
]) {
880 jail
->netns
= blobmsg_get_bool(tb
[JAIL_ATTR_NETNS
]);
883 if (tb
[JAIL_ATTR_USERNS
]) {
884 jail
->userns
= blobmsg_get_bool(tb
[JAIL_ATTR_USERNS
]);
887 if (tb
[JAIL_ATTR_CGROUPSNS
]) {
888 jail
->cgroupsns
= blobmsg_get_bool(tb
[JAIL_ATTR_CGROUPSNS
]);
892 if (tb
[JAIL_ATTR_MOUNT
]) {
893 struct blob_attr
*cur
;
896 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_MOUNT
], rem
)
898 instance_fill_array(&jail
->mount
, tb
[JAIL_ATTR_MOUNT
], NULL
, false);
913 if (in
->no_new_privs
)
920 instance_config_parse_command(struct service_instance
*in
, struct blob_attr
**tb
)
922 struct blob_attr
*cur
, *cur2
;
926 cur
= tb
[INSTANCE_ATTR_COMMAND
];
932 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
935 blobmsg_for_each_attr(cur2
, cur
, rem
) {
945 instance_config_parse(struct service_instance
*in
)
947 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
948 struct blob_attr
*cur
, *cur2
;
952 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
953 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
955 if (!instance_config_parse_command(in
, tb
))
958 if (tb
[INSTANCE_ATTR_TERMTIMEOUT
])
959 in
->term_timeout
= blobmsg_get_u32(tb
[INSTANCE_ATTR_TERMTIMEOUT
]);
960 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
962 uint32_t vals
[3] = { 3600, 5, 5};
964 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
965 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
967 vals
[i
] = atoi(blobmsg_get_string(cur2
));
971 in
->respawn_count
= 0;
972 in
->respawn_threshold
= vals
[0];
973 in
->respawn_timeout
= vals
[1];
974 in
->respawn_retry
= vals
[2];
976 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
977 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
978 trigger_add(in
->trigger
, in
);
981 if (tb
[INSTANCE_ATTR_WATCH
]) {
982 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
983 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
985 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
986 watch_add(blobmsg_get_string(cur2
), in
);
990 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
991 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
992 if (in
->nice
< -20 || in
->nice
> 20)
996 if (tb
[INSTANCE_ATTR_USER
]) {
997 const char *user
= blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]);
998 struct passwd
*p
= getpwnam(user
);
1000 in
->user
= strdup(user
);
1001 in
->uid
= p
->pw_uid
;
1002 in
->gr_gid
= in
->pw_gid
= p
->pw_gid
;
1006 if (tb
[INSTANCE_ATTR_GROUP
]) {
1007 const char *group
= blobmsg_get_string(tb
[INSTANCE_ATTR_GROUP
]);
1008 struct group
*p
= getgrnam(group
);
1010 in
->group
= strdup(group
);
1011 in
->gr_gid
= p
->gr_gid
;
1015 if (tb
[INSTANCE_ATTR_TRACE
])
1016 in
->trace
= blobmsg_get_bool(tb
[INSTANCE_ATTR_TRACE
]);
1018 if (tb
[INSTANCE_ATTR_NO_NEW_PRIVS
])
1019 in
->no_new_privs
= blobmsg_get_bool(tb
[INSTANCE_ATTR_NO_NEW_PRIVS
]);
1021 if (!in
->trace
&& tb
[INSTANCE_ATTR_SECCOMP
])
1022 in
->seccomp
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_SECCOMP
]));
1024 if (tb
[INSTANCE_ATTR_EXTROOT
])
1025 in
->extroot
= strdup(blobmsg_get_string(tb
[INSTANCE_ATTR_EXTROOT
]));
1027 if (tb
[INSTANCE_ATTR_PIDFILE
]) {
1028 char *pidfile
= blobmsg_get_string(tb
[INSTANCE_ATTR_PIDFILE
]);
1030 in
->pidfile
= strdup(pidfile
);
1033 if (tb
[INSTANCE_ATTR_RELOADSIG
])
1034 in
->reload_signal
= blobmsg_get_u32(tb
[INSTANCE_ATTR_RELOADSIG
]);
1036 if (!in
->trace
&& tb
[INSTANCE_ATTR_JAIL
])
1037 in
->has_jail
= instance_jail_parse(in
, tb
[INSTANCE_ATTR_JAIL
]);
1040 r
= stat(UJAIL_BIN_PATH
, &s
);
1042 if (in
->require_jail
) {
1043 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1044 in
->srv
->name
, in
->name
, UJAIL_BIN_PATH
, r
);
1047 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH
, r
);
1048 in
->has_jail
= false;
1052 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
1053 in
->_stdout
.fd
.fd
= -1;
1055 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
1056 in
->_stderr
.fd
.fd
= -1;
1058 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
1060 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
1063 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
1066 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
1069 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
1072 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
1075 if (tb
[INSTANCE_ATTR_FACILITY
]) {
1076 int facility
= syslog_facility_str_to_int(blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1077 if (facility
!= -1) {
1078 in
->syslog_facility
= facility
;
1079 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1081 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb
[INSTANCE_ATTR_FACILITY
]));
1088 instance_config_cleanup(struct service_instance
*in
)
1090 blobmsg_list_free(&in
->env
);
1091 blobmsg_list_free(&in
->data
);
1092 blobmsg_list_free(&in
->netdev
);
1093 blobmsg_list_free(&in
->file
);
1094 blobmsg_list_free(&in
->limits
);
1095 blobmsg_list_free(&in
->errors
);
1096 blobmsg_list_free(&in
->jail
.mount
);
1100 instance_config_move_strdup(char **dst
, char *src
)
1114 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
1116 instance_config_cleanup(in
);
1117 blobmsg_list_move(&in
->env
, &in_src
->env
);
1118 blobmsg_list_move(&in
->data
, &in_src
->data
);
1119 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
1120 blobmsg_list_move(&in
->file
, &in_src
->file
);
1121 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
1122 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
1123 blobmsg_list_move(&in
->jail
.mount
, &in_src
->jail
.mount
);
1124 in
->trigger
= in_src
->trigger
;
1125 in
->command
= in_src
->command
;
1126 in
->respawn
= in_src
->respawn
;
1127 in
->respawn_retry
= in_src
->respawn_retry
;
1128 in
->respawn_threshold
= in_src
->respawn_threshold
;
1129 in
->respawn_timeout
= in_src
->respawn_timeout
;
1130 in
->name
= in_src
->name
;
1131 in
->trace
= in_src
->trace
;
1132 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
1133 in
->syslog_facility
= in_src
->syslog_facility
;
1135 instance_config_move_strdup(&in
->pidfile
, in_src
->pidfile
);
1136 instance_config_move_strdup(&in
->seccomp
, in_src
->seccomp
);
1137 instance_config_move_strdup(&in
->jail
.name
, in_src
->jail
.name
);
1138 instance_config_move_strdup(&in
->jail
.hostname
, in_src
->jail
.hostname
);
1141 in
->config
= in_src
->config
;
1142 in_src
->config
= NULL
;
1146 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
1148 bool changed
= instance_config_changed(in
, in_new
);
1149 bool running
= in
->proc
.pending
;
1150 bool stopping
= in
->halt
;
1152 if (!running
|| stopping
) {
1153 instance_config_move(in
, in_new
);
1157 instance_restart(in
);
1158 instance_config_move(in
, in_new
);
1159 /* restart happens in the child callback handler */
1164 instance_free(struct service_instance
*in
)
1166 instance_free_stdio(in
);
1167 uloop_process_delete(&in
->proc
);
1168 uloop_timeout_cancel(&in
->timeout
);
1171 instance_config_cleanup(in
);
1176 free(in
->jail
.name
);
1177 free(in
->jail
.hostname
);
1184 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
1186 config
= blob_memdup(config
);
1188 in
->name
= blobmsg_name(config
);
1189 in
->config
= config
;
1190 in
->timeout
.cb
= instance_timeout
;
1191 in
->proc
.cb
= instance_exit
;
1192 in
->term_timeout
= 5;
1193 in
->syslog_facility
= LOG_DAEMON
;
1195 in
->require_jail
= false;
1197 in
->_stdout
.fd
.fd
= -2;
1198 in
->_stdout
.stream
.string_data
= true;
1199 in
->_stdout
.stream
.notify_read
= instance_stdout
;
1201 in
->_stderr
.fd
.fd
= -2;
1202 in
->_stderr
.stream
.string_data
= true;
1203 in
->_stderr
.stream
.notify_read
= instance_stderr
;
1205 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
1206 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
1207 blobmsg_list_simple_init(&in
->env
);
1208 blobmsg_list_simple_init(&in
->data
);
1209 blobmsg_list_simple_init(&in
->limits
);
1210 blobmsg_list_simple_init(&in
->errors
);
1211 blobmsg_list_simple_init(&in
->jail
.mount
);
1212 in
->valid
= instance_config_parse(in
);
1215 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
1222 i
= blobmsg_open_table(b
, in
->name
);
1223 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
1224 if (in
->proc
.pending
)
1225 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
1227 blobmsg_add_blob(b
, in
->command
);
1228 blobmsg_add_u32(b
, "term_timeout", in
->term_timeout
);
1229 if (!in
->proc
.pending
)
1230 blobmsg_add_u32(b
, "exit_code", in
->exit_code
);
1232 if (!avl_is_empty(&in
->errors
.avl
)) {
1233 struct blobmsg_list_node
*var
;
1234 void *e
= blobmsg_open_array(b
, "errors");
1235 blobmsg_list_for_each(&in
->errors
, var
)
1236 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
1237 blobmsg_close_table(b
, e
);
1240 if (!avl_is_empty(&in
->env
.avl
)) {
1241 struct blobmsg_list_node
*var
;
1242 void *e
= blobmsg_open_table(b
, "env");
1243 blobmsg_list_for_each(&in
->env
, var
)
1244 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1245 blobmsg_close_table(b
, e
);
1248 if (!avl_is_empty(&in
->data
.avl
)) {
1249 struct blobmsg_list_node
*var
;
1250 void *e
= blobmsg_open_table(b
, "data");
1251 blobmsg_list_for_each(&in
->data
, var
)
1252 blobmsg_add_blob(b
, var
->data
);
1253 blobmsg_close_table(b
, e
);
1256 if (!avl_is_empty(&in
->limits
.avl
)) {
1257 struct blobmsg_list_node
*var
;
1258 void *e
= blobmsg_open_table(b
, "limits");
1259 blobmsg_list_for_each(&in
->limits
, var
)
1260 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1261 blobmsg_close_table(b
, e
);
1264 if (in
->reload_signal
)
1265 blobmsg_add_u32(b
, "reload_signal", in
->reload_signal
);
1268 void *r
= blobmsg_open_table(b
, "respawn");
1269 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
1270 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
1271 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
1272 blobmsg_close_table(b
, r
);
1276 blobmsg_add_u8(b
, "trace", true);
1278 if (in
->no_new_privs
)
1279 blobmsg_add_u8(b
, "no_new_privs", true);
1282 blobmsg_add_string(b
, "seccomp", in
->seccomp
);
1285 blobmsg_add_string(b
, "pidfile", in
->pidfile
);
1288 blobmsg_add_string(b
, "user", in
->user
);
1291 blobmsg_add_string(b
, "group", in
->group
);
1294 void *r
= blobmsg_open_table(b
, "jail");
1296 blobmsg_add_string(b
, "name", in
->jail
.name
);
1297 if (in
->jail
.hostname
)
1298 blobmsg_add_string(b
, "hostname", in
->jail
.hostname
);
1300 blobmsg_add_string(b
, "extroot", in
->extroot
);
1301 blobmsg_add_u8(b
, "procfs", in
->jail
.procfs
);
1302 blobmsg_add_u8(b
, "sysfs", in
->jail
.sysfs
);
1303 blobmsg_add_u8(b
, "ubus", in
->jail
.ubus
);
1304 blobmsg_add_u8(b
, "log", in
->jail
.log
);
1305 blobmsg_add_u8(b
, "ronly", in
->jail
.ronly
);
1306 blobmsg_add_u8(b
, "netns", in
->jail
.netns
);
1307 blobmsg_add_u8(b
, "userns", in
->jail
.userns
);
1308 blobmsg_add_u8(b
, "cgroupsns", in
->jail
.cgroupsns
);
1309 blobmsg_close_table(b
, r
);
1310 if (!avl_is_empty(&in
->jail
.mount
.avl
)) {
1311 struct blobmsg_list_node
*var
;
1312 void *e
= blobmsg_open_table(b
, "mount");
1313 blobmsg_list_for_each(&in
->jail
.mount
, var
)
1314 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
1315 blobmsg_close_table(b
, e
);
1319 if (verbose
&& in
->trigger
)
1320 blobmsg_add_blob(b
, in
->trigger
);
1322 blobmsg_close_table(b
, i
);