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,
68 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
69 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
70 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
71 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
72 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
73 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
74 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
75 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
76 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
77 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
78 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
79 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
80 [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
81 [INSTANCE_ATTR_GROUP] = { "group", BLOBMSG_TYPE_STRING },
82 [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
83 [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
84 [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
85 [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
86 [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
87 [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
88 [INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
89 [INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
90 [INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
91 [INSTANCE_ATTR_FACILITY] = { "facility", BLOBMSG_TYPE_STRING },
104 JAIL_ATTR_REQUIREJAIL,
109 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
110 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
111 [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
112 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
113 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
114 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
115 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
116 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
117 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
118 [JAIL_ATTR_NETNS] = { "netns", BLOBMSG_TYPE_BOOL },
119 [JAIL_ATTR_REQUIREJAIL] = { "requirejail", BLOBMSG_TYPE_BOOL },
120 [JAIL_ATTR_EXTROOT] = { "extroot", BLOBMSG_TYPE_STRING },
123 struct instance_netdev {
124 struct blobmsg_list_node node;
128 struct instance_file {
129 struct blobmsg_list_node node;
138 static const struct rlimit_name rlimit_names[] = {
140 { "core", RLIMIT_CORE },
141 { "cpu", RLIMIT_CPU },
142 { "data", RLIMIT_DATA },
143 { "fsize", RLIMIT_FSIZE },
144 { "memlock", RLIMIT_MEMLOCK },
145 { "nofile", RLIMIT_NOFILE },
146 { "nproc", RLIMIT_NPROC },
147 { "rss", RLIMIT_RSS },
148 { "stack", RLIMIT_STACK },
150 { "nice", RLIMIT_NICE },
151 { "rtprio", RLIMIT_RTPRIO },
152 { "msgqueue", RLIMIT_MSGQUEUE },
153 { "sigpending", RLIMIT_SIGPENDING },
158 static void closefd(int fd)
160 if (fd > STDERR_FILENO)
164 /* convert a string into numeric syslog facility or return -1 if no match found */
166 syslog_facility_str_to_int(const char *facility)
168 CODE *p = facilitynames;
170 while (p->c_name && strcasecmp(p->c_name, facility))
177 instance_limits(const char *limit, const char *value)
181 unsigned long cur, max;
183 for (i = 0; rlimit_names[i].name != NULL; i++) {
184 if (strcmp(rlimit_names[i].name, limit))
186 if (!strcmp(value, "unlimited")) {
187 rlim.rlim_cur = RLIM_INFINITY;
188 rlim.rlim_max = RLIM_INFINITY;
190 if (getrlimit(rlimit_names[i].resource, &rlim))
196 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
203 setrlimit(rlimit_names[i].resource, &rlim);
209 jail_run(struct service_instance *in, char **argv)
211 struct blobmsg_list_node *var;
212 struct jail *jail = &in->jail;
215 argv[argc++] = UJAIL_BIN_PATH;
219 argv[argc++] = jail->name;
222 if (jail->hostname) {
224 argv[argc++] = jail->hostname;
229 argv[argc++] = in->seccomp;
234 argv[argc++] = in->user;
239 argv[argc++] = in->group;
242 if (in->no_new_privs)
265 argv[argc++] = jail->extroot;
268 blobmsg_list_for_each(&jail->mount, var) {
269 const char *type = blobmsg_data(var->data);
275 argv[argc++] = (char *) blobmsg_name(var->data);
284 instance_removepid(struct service_instance *in) {
287 if (unlink(in->pidfile)) {
288 ERROR("Failed to remove pidfile: %s: %m\n", in->pidfile);
295 instance_writepid(struct service_instance *in)
302 _pidfile = fopen(in->pidfile, "w");
303 if (_pidfile == NULL) {
304 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
307 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
308 ERROR("failed to write pidfile: %s: %m", in->pidfile);
312 if (fclose(_pidfile)) {
313 ERROR("failed to close pidfile: %s: %m", in->pidfile);
321 instance_run(struct service_instance *in, int _stdout, int _stderr)
323 struct blobmsg_list_node *var;
324 struct blob_attr *cur;
326 int argc = 1; /* NULL terminated */
328 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
329 bool setlbf = _stdout >= 0;
332 setpriority(PRIO_PROCESS, 0, in->nice);
334 blobmsg_for_each_attr(cur, in->command, rem)
337 blobmsg_list_for_each(&in->env, var)
338 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
341 setenv("SECCOMP_FILE", in->seccomp, 1);
344 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
346 blobmsg_list_for_each(&in->limits, var)
347 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
349 if (in->trace || seccomp)
352 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
355 #ifdef SECCOMP_SUPPORT
357 argv[argc++] = "/sbin/utrace";
359 argv[argc++] = "/sbin/seccomp-trace";
361 if (in->trace || seccomp)
362 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
366 argc = jail_run(in, argv);
367 if (argc != in->jail.argc)
368 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
369 in->jail.argc, argc, in->srv->name, in->name);
372 blobmsg_for_each_attr(cur, in->command, rem)
373 argv[argc++] = blobmsg_data(cur);
377 _stdin = open("/dev/null", O_RDONLY);
380 _stdout = open("/dev/null", O_WRONLY);
383 _stderr = open("/dev/null", O_WRONLY);
386 dup2(_stdin, STDIN_FILENO);
390 dup2(_stdout, STDOUT_FILENO);
394 dup2(_stderr, STDERR_FILENO);
398 if (!in->has_jail && in->user && in->pw_gid && initgroups(in->user, in->pw_gid)) {
399 ERROR("failed to initgroups() for user %s: %m\n", in->user);
402 if (!in->has_jail && in->gr_gid && setgid(in->gr_gid)) {
403 ERROR("failed to set group id %d: %m\n", in->gr_gid);
406 if (!in->has_jail && in->uid && setuid(in->uid)) {
407 ERROR("failed to set user id %d: %m\n", in->uid);
411 execvp(argv[0], argv);
416 instance_free_stdio(struct service_instance *in)
418 if (in->_stdout.fd.fd > -1) {
419 ustream_free(&in->_stdout.stream);
420 close(in->_stdout.fd.fd);
421 in->_stdout.fd.fd = -1;
424 if (in->_stderr.fd.fd > -1) {
425 ustream_free(&in->_stderr.stream);
426 close(in->_stderr.fd.fd);
427 in->_stderr.fd.fd = -1;
432 instance_start(struct service_instance *in)
435 int opipe[2] = { -1, -1 };
436 int epipe[2] = { -1, -1 };
438 if (!avl_is_empty(&in->errors.avl)) {
439 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
444 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
448 if (in->proc.pending) {
454 instance_free_stdio(in);
455 if (in->_stdout.fd.fd > -2) {
457 ULOG_WARN("pipe() failed: %m\n");
458 opipe[0] = opipe[1] = -1;
462 if (in->_stderr.fd.fd > -2) {
464 ULOG_WARN("pipe() failed: %m\n");
465 epipe[0] = epipe[1] = -1;
483 instance_run(in, opipe[1], epipe[1]);
487 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
489 instance_writepid(in);
490 clock_gettime(CLOCK_MONOTONIC, &in->start);
491 uloop_process_add(&in->proc);
494 ustream_fd_init(&in->_stdout, opipe[0]);
496 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
500 ustream_fd_init(&in->_stderr, epipe[0]);
502 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
505 service_event("instance.start", in->srv->name, in->name);
509 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
511 char *newline, *str, *arg0, ident[32];
514 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
515 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
516 ulog_open(ULOG_SYSLOG, in->syslog_facility, ident);
519 str = ustream_get_read_buf(s, &len);
523 newline = memchr(str, '\n', len);
524 if (!newline && (s->r.buffer_len != len))
529 len = newline + 1 - str;
531 ulog(prio, "%s\n", str);
533 ustream_consume(s, len);
536 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
540 instance_stdout(struct ustream *s, int bytes)
542 instance_stdio(s, LOG_INFO,
543 container_of(s, struct service_instance, _stdout.stream));
547 instance_stderr(struct ustream *s, int bytes)
549 instance_stdio(s, LOG_ERR,
550 container_of(s, struct service_instance, _stderr.stream));
554 instance_timeout(struct uloop_timeout *t)
556 struct service_instance *in;
558 in = container_of(t, struct service_instance, timeout);
561 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
562 in->srv->name, in->name, in->proc.pid);
563 kill(in->proc.pid, SIGKILL);
564 } else if (in->restart || in->respawn)
569 instance_delete(struct service_instance *in)
571 struct service *s = in->srv;
573 avl_delete(&s->instances.avl, &in->node.avl);
579 instance_exit_code(int ret)
581 if (WIFEXITED(ret)) {
582 return WEXITSTATUS(ret);
585 if (WIFSIGNALED(ret)) {
586 return SIGNALLED_OFFSET + WTERMSIG(ret);
589 if (WIFSTOPPED(ret)) {
590 return WSTOPSIG(ret);
597 instance_exit(struct uloop_process *p, int ret)
599 struct service_instance *in;
603 in = container_of(p, struct service_instance, proc);
605 clock_gettime(CLOCK_MONOTONIC, &tp);
606 runtime = tp.tv_sec - in->start.tv_sec;
608 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
610 in->exit_code = instance_exit_code(ret);
611 uloop_timeout_cancel(&in->timeout);
612 service_event("instance.stop", in->srv->name, in->name);
615 instance_removepid(in);
620 } else if (in->restart) {
622 } else if (in->respawn) {
623 if (runtime < in->respawn_threshold)
626 in->respawn_count = 0;
627 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
628 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
629 in->srv->name, in->name, in->respawn_count, runtime);
630 in->restart = in->respawn = 0;
632 service_event("instance.fail", in->srv->name, in->name);
634 service_event("instance.respawn", in->srv->name, in->name);
635 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
641 instance_stop(struct service_instance *in, bool halt)
643 if (!in->proc.pending) {
649 in->restart = in->respawn = false;
650 kill(in->proc.pid, SIGTERM);
651 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
655 instance_restart(struct service_instance *in)
657 if (!in->proc.pending)
660 if (in->reload_signal) {
661 kill(in->proc.pid, in->reload_signal);
667 kill(in->proc.pid, SIGTERM);
668 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
671 static bool string_changed(const char *a, const char *b)
673 return !((!a && !b) || (a && b && !strcmp(a, b)));
677 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
682 if (!blob_attr_equal(in->command, in_new->command))
685 if (!blobmsg_list_equal(&in->env, &in_new->env))
688 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
691 if (!blobmsg_list_equal(&in->file, &in_new->file))
694 if (in->nice != in_new->nice)
697 if (in->syslog_facility != in_new->syslog_facility)
700 if (string_changed(in->user, in_new->user))
703 if (string_changed(in->group, in_new->group))
706 if (in->uid != in_new->uid)
709 if (in->pw_gid != in_new->pw_gid)
712 if (string_changed(in->pidfile, in_new->pidfile))
715 if (in->respawn_retry != in_new->respawn_retry)
717 if (in->respawn_threshold != in_new->respawn_threshold)
719 if (in->respawn_timeout != in_new->respawn_timeout)
722 if ((!in->seccomp && in_new->seccomp) ||
723 (in->seccomp && !in_new->seccomp) ||
724 (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
727 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
730 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
733 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
740 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
742 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
743 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
745 return n1->ifindex == n2->ifindex;
749 instance_netdev_update(struct blobmsg_list_node *l)
751 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
753 n->ifindex = if_nametoindex(n->node.avl.key);
757 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
759 struct instance_file *f1 = container_of(l1, struct instance_file, node);
760 struct instance_file *f2 = container_of(l2, struct instance_file, node);
762 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
766 instance_file_update(struct blobmsg_list_node *l)
768 struct instance_file *f = container_of(l, struct instance_file, node);
773 memset(f->md5, 0, sizeof(f->md5));
775 fd = open(l->avl.key, O_RDONLY);
781 len = read(fd, buf, sizeof(buf));
791 md5_hash(buf, len, &md5);
794 md5_end(f->md5, &md5);
799 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
804 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
808 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
810 struct blobmsg_list_node *node;
815 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
818 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
820 blobmsg_list_for_each(l, node)
827 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
829 struct blob_attr *tb[__JAIL_ATTR_MAX];
830 struct jail *jail = &in->jail;
832 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
833 blobmsg_data(attr), blobmsg_data_len(attr));
837 if (tb[JAIL_ATTR_REQUIREJAIL]) {
838 in->require_jail = true;
841 if (tb[JAIL_ATTR_NAME]) {
842 jail->name = strdup(blobmsg_get_string(tb[JAIL_ATTR_NAME]));
845 if (tb[JAIL_ATTR_HOSTNAME]) {
846 jail->hostname = strdup(blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]));
849 if (tb[JAIL_ATTR_PROCFS]) {
850 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
853 if (tb[JAIL_ATTR_SYSFS]) {
854 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
857 if (tb[JAIL_ATTR_UBUS]) {
858 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
861 if (tb[JAIL_ATTR_LOG]) {
862 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
865 if (tb[JAIL_ATTR_RONLY]) {
866 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
869 if (tb[JAIL_ATTR_NETNS]) {
870 jail->netns = blobmsg_get_bool(tb[JAIL_ATTR_NETNS]);
873 if (tb[JAIL_ATTR_EXTROOT]) {
874 jail->extroot = strdup(blobmsg_get_string(tb[JAIL_ATTR_EXTROOT]));
878 if (tb[JAIL_ATTR_MOUNT]) {
879 struct blob_attr *cur;
882 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
884 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
895 if (in->no_new_privs)
902 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
904 struct blob_attr *cur, *cur2;
908 cur = tb[INSTANCE_ATTR_COMMAND];
914 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
917 blobmsg_for_each_attr(cur2, cur, rem) {
927 instance_config_parse(struct service_instance *in)
929 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
930 struct blob_attr *cur, *cur2;
934 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
935 blobmsg_data(in->config), blobmsg_data_len(in->config));
937 if (!instance_config_parse_command(in, tb))
940 if (tb[INSTANCE_ATTR_TERMTIMEOUT])
941 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
942 if (tb[INSTANCE_ATTR_RESPAWN]) {
944 uint32_t vals[3] = { 3600, 5, 5};
946 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
947 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
949 vals[i] = atoi(blobmsg_get_string(cur2));
953 in->respawn_count = 0;
954 in->respawn_threshold = vals[0];
955 in->respawn_timeout = vals[1];
956 in->respawn_retry = vals[2];
958 if (tb[INSTANCE_ATTR_TRIGGER]) {
959 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
960 trigger_add(in->trigger, in);
963 if (tb[INSTANCE_ATTR_WATCH]) {
964 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
965 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
967 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
968 watch_add(blobmsg_get_string(cur2), in);
972 if ((cur = tb[INSTANCE_ATTR_NICE])) {
973 in->nice = (int8_t) blobmsg_get_u32(cur);
974 if (in->nice < -20 || in->nice > 20)
978 if (tb[INSTANCE_ATTR_USER]) {
979 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
980 struct passwd *p = getpwnam(user);
982 in->user = strdup(user);
984 in->gr_gid = in->pw_gid = p->pw_gid;
988 if (tb[INSTANCE_ATTR_GROUP]) {
989 const char *group = blobmsg_get_string(tb[INSTANCE_ATTR_GROUP]);
990 struct group *p = getgrnam(group);
992 in->group = strdup(group);
993 in->gr_gid = p->gr_gid;
997 if (tb[INSTANCE_ATTR_TRACE])
998 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
1000 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
1001 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
1003 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
1004 in->seccomp = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]));
1006 if (tb[INSTANCE_ATTR_PIDFILE]) {
1007 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
1009 in->pidfile = strdup(pidfile);
1012 if (tb[INSTANCE_ATTR_RELOADSIG])
1013 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
1015 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
1016 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
1019 r = stat(UJAIL_BIN_PATH, &s);
1021 if (in->require_jail) {
1022 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1023 in->srv->name, in->name, UJAIL_BIN_PATH, r);
1026 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH, r);
1027 in->has_jail = false;
1031 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
1032 in->_stdout.fd.fd = -1;
1034 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
1035 in->_stderr.fd.fd = -1;
1037 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
1039 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
1042 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
1045 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
1048 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
1051 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
1054 if (tb[INSTANCE_ATTR_FACILITY]) {
1055 int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1056 if (facility != -1) {
1057 in->syslog_facility = facility;
1058 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1060 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1067 instance_config_cleanup(struct service_instance *in)
1069 blobmsg_list_free(&in->env);
1070 blobmsg_list_free(&in->data);
1071 blobmsg_list_free(&in->netdev);
1072 blobmsg_list_free(&in->file);
1073 blobmsg_list_free(&in->limits);
1074 blobmsg_list_free(&in->errors);
1075 blobmsg_list_free(&in->jail.mount);
1079 instance_config_move_strdup(char **dst, char *src)
1093 instance_config_move(struct service_instance *in, struct service_instance *in_src)
1095 instance_config_cleanup(in);
1096 blobmsg_list_move(&in->env, &in_src->env);
1097 blobmsg_list_move(&in->data, &in_src->data);
1098 blobmsg_list_move(&in->netdev, &in_src->netdev);
1099 blobmsg_list_move(&in->file, &in_src->file);
1100 blobmsg_list_move(&in->limits, &in_src->limits);
1101 blobmsg_list_move(&in->errors, &in_src->errors);
1102 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
1103 in->trigger = in_src->trigger;
1104 in->command = in_src->command;
1105 in->respawn = in_src->respawn;
1106 in->respawn_retry = in_src->respawn_retry;
1107 in->respawn_threshold = in_src->respawn_threshold;
1108 in->respawn_timeout = in_src->respawn_timeout;
1109 in->name = in_src->name;
1110 in->trace = in_src->trace;
1111 in->node.avl.key = in_src->node.avl.key;
1112 in->syslog_facility = in_src->syslog_facility;
1114 instance_config_move_strdup(&in->pidfile, in_src->pidfile);
1115 instance_config_move_strdup(&in->seccomp, in_src->seccomp);
1116 instance_config_move_strdup(&in->jail.name, in_src->jail.name);
1117 instance_config_move_strdup(&in->jail.hostname, in_src->jail.hostname);
1120 in->config = in_src->config;
1121 in_src->config = NULL;
1125 instance_update(struct service_instance *in, struct service_instance *in_new)
1127 bool changed = instance_config_changed(in, in_new);
1128 bool running = in->proc.pending;
1129 bool stopping = in->halt;
1131 if (!running || stopping) {
1132 instance_config_move(in, in_new);
1136 instance_restart(in);
1137 instance_config_move(in, in_new);
1138 /* restart happens in the child callback handler */
1143 instance_free(struct service_instance *in)
1145 instance_free_stdio(in);
1146 uloop_process_delete(&in->proc);
1147 uloop_timeout_cancel(&in->timeout);
1150 instance_config_cleanup(in);
1154 free(in->jail.extroot);
1155 free(in->jail.name);
1156 free(in->jail.hostname);
1163 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1165 config = blob_memdup(config);
1167 in->name = blobmsg_name(config);
1168 in->config = config;
1169 in->timeout.cb = instance_timeout;
1170 in->proc.cb = instance_exit;
1171 in->term_timeout = 5;
1172 in->syslog_facility = LOG_DAEMON;
1174 in->require_jail = false;
1176 in->_stdout.fd.fd = -2;
1177 in->_stdout.stream.string_data = true;
1178 in->_stdout.stream.notify_read = instance_stdout;
1180 in->_stderr.fd.fd = -2;
1181 in->_stderr.stream.string_data = true;
1182 in->_stderr.stream.notify_read = instance_stderr;
1184 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1185 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1186 blobmsg_list_simple_init(&in->env);
1187 blobmsg_list_simple_init(&in->data);
1188 blobmsg_list_simple_init(&in->limits);
1189 blobmsg_list_simple_init(&in->errors);
1190 blobmsg_list_simple_init(&in->jail.mount);
1191 in->valid = instance_config_parse(in);
1194 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1201 i = blobmsg_open_table(b, in->name);
1202 blobmsg_add_u8(b, "running", in->proc.pending);
1203 if (in->proc.pending)
1204 blobmsg_add_u32(b, "pid", in->proc.pid);
1206 blobmsg_add_blob(b, in->command);
1207 blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1208 if (!in->proc.pending)
1209 blobmsg_add_u32(b, "exit_code", in->exit_code);
1211 if (!avl_is_empty(&in->errors.avl)) {
1212 struct blobmsg_list_node *var;
1213 void *e = blobmsg_open_array(b, "errors");
1214 blobmsg_list_for_each(&in->errors, var)
1215 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1216 blobmsg_close_table(b, e);
1219 if (!avl_is_empty(&in->env.avl)) {
1220 struct blobmsg_list_node *var;
1221 void *e = blobmsg_open_table(b, "env");
1222 blobmsg_list_for_each(&in->env, var)
1223 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1224 blobmsg_close_table(b, e);
1227 if (!avl_is_empty(&in->data.avl)) {
1228 struct blobmsg_list_node *var;
1229 void *e = blobmsg_open_table(b, "data");
1230 blobmsg_list_for_each(&in->data, var)
1231 blobmsg_add_blob(b, var->data);
1232 blobmsg_close_table(b, e);
1235 if (!avl_is_empty(&in->limits.avl)) {
1236 struct blobmsg_list_node *var;
1237 void *e = blobmsg_open_table(b, "limits");
1238 blobmsg_list_for_each(&in->limits, var)
1239 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1240 blobmsg_close_table(b, e);
1243 if (in->reload_signal)
1244 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1247 void *r = blobmsg_open_table(b, "respawn");
1248 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1249 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1250 blobmsg_add_u32(b, "retry", in->respawn_retry);
1251 blobmsg_close_table(b, r);
1255 blobmsg_add_u8(b, "trace", true);
1257 if (in->no_new_privs)
1258 blobmsg_add_u8(b, "no_new_privs", true);
1261 blobmsg_add_string(b, "seccomp", in->seccomp);
1264 blobmsg_add_string(b, "pidfile", in->pidfile);
1267 blobmsg_add_string(b, "user", in->user);
1270 blobmsg_add_string(b, "group", in->group);
1273 void *r = blobmsg_open_table(b, "jail");
1275 blobmsg_add_string(b, "name", in->jail.name);
1276 if (in->jail.hostname)
1277 blobmsg_add_string(b, "hostname", in->jail.hostname);
1278 if (in->jail.extroot)
1279 blobmsg_add_string(b, "extroot", in->jail.extroot);
1280 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1281 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1282 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1283 blobmsg_add_u8(b, "log", in->jail.log);
1284 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1285 blobmsg_add_u8(b, "netns", in->jail.netns);
1286 blobmsg_close_table(b, r);
1287 if (!avl_is_empty(&in->jail.mount.avl)) {
1288 struct blobmsg_list_node *var;
1289 void *e = blobmsg_open_table(b, "mount");
1290 blobmsg_list_for_each(&in->jail.mount, var)
1291 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1292 blobmsg_close_table(b, e);
1296 if (verbose && in->trigger)
1297 blobmsg_add_blob(b, in->trigger);
1299 blobmsg_close_table(b, i);