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 blobmsg_list_for_each(&jail->mount, var) {
293 const char *type = blobmsg_data(var->data);
299 argv[argc++] = (char *) blobmsg_name(var->data);
308 instance_removepid(struct service_instance *in) {
311 if (unlink(in->pidfile)) {
312 ERROR("Failed to remove pidfile: %s: %m\n", in->pidfile);
319 instance_writepid(struct service_instance *in)
326 _pidfile = fopen(in->pidfile, "w");
327 if (_pidfile == NULL) {
328 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
331 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
332 ERROR("failed to write pidfile: %s: %m", in->pidfile);
336 if (fclose(_pidfile)) {
337 ERROR("failed to close pidfile: %s: %m", in->pidfile);
345 instance_run(struct service_instance *in, int _stdout, int _stderr)
347 struct blobmsg_list_node *var;
348 struct blob_attr *cur;
350 int argc = 1; /* NULL terminated */
352 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
353 bool setlbf = _stdout >= 0;
356 setpriority(PRIO_PROCESS, 0, in->nice);
358 blobmsg_for_each_attr(cur, in->command, rem)
361 blobmsg_list_for_each(&in->env, var)
362 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
365 setenv("SECCOMP_FILE", in->seccomp, 1);
368 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
370 blobmsg_list_for_each(&in->limits, var)
371 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
373 if (in->trace || seccomp)
376 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
379 #ifdef SECCOMP_SUPPORT
381 argv[argc++] = "/sbin/utrace";
383 argv[argc++] = "/sbin/seccomp-trace";
385 if (in->trace || seccomp)
386 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
390 argc = jail_run(in, argv);
391 if (argc != in->jail.argc)
392 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
393 in->jail.argc, argc, in->srv->name, in->name);
396 blobmsg_for_each_attr(cur, in->command, rem)
397 argv[argc++] = blobmsg_data(cur);
401 _stdin = open("/dev/null", O_RDONLY);
404 _stdout = open("/dev/null", O_WRONLY);
407 _stderr = open("/dev/null", O_WRONLY);
410 dup2(_stdin, STDIN_FILENO);
414 dup2(_stdout, STDOUT_FILENO);
418 dup2(_stderr, STDERR_FILENO);
422 if (!in->has_jail && in->user && in->pw_gid && initgroups(in->user, in->pw_gid)) {
423 ERROR("failed to initgroups() for user %s: %m\n", in->user);
426 if (!in->has_jail && in->gr_gid && setgid(in->gr_gid)) {
427 ERROR("failed to set group id %d: %m\n", in->gr_gid);
430 if (!in->has_jail && in->uid && setuid(in->uid)) {
431 ERROR("failed to set user id %d: %m\n", in->uid);
435 execvp(argv[0], argv);
440 instance_free_stdio(struct service_instance *in)
442 if (in->_stdout.fd.fd > -1) {
443 ustream_free(&in->_stdout.stream);
444 close(in->_stdout.fd.fd);
445 in->_stdout.fd.fd = -1;
448 if (in->_stderr.fd.fd > -1) {
449 ustream_free(&in->_stderr.stream);
450 close(in->_stderr.fd.fd);
451 in->_stderr.fd.fd = -1;
456 instance_start(struct service_instance *in)
459 int opipe[2] = { -1, -1 };
460 int epipe[2] = { -1, -1 };
462 if (!avl_is_empty(&in->errors.avl)) {
463 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
468 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
472 if (in->proc.pending) {
478 instance_free_stdio(in);
479 if (in->_stdout.fd.fd > -2) {
481 ULOG_WARN("pipe() failed: %m\n");
482 opipe[0] = opipe[1] = -1;
486 if (in->_stderr.fd.fd > -2) {
488 ULOG_WARN("pipe() failed: %m\n");
489 epipe[0] = epipe[1] = -1;
507 instance_run(in, opipe[1], epipe[1]);
511 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
513 instance_writepid(in);
514 clock_gettime(CLOCK_MONOTONIC, &in->start);
515 uloop_process_add(&in->proc);
518 ustream_fd_init(&in->_stdout, opipe[0]);
520 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
524 ustream_fd_init(&in->_stderr, epipe[0]);
526 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
529 service_event("instance.start", in->srv->name, in->name);
533 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
535 char *newline, *str, *arg0, ident[32];
538 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
539 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
540 ulog_open(ULOG_SYSLOG, in->syslog_facility, ident);
543 str = ustream_get_read_buf(s, &len);
547 newline = memchr(str, '\n', len);
548 if (!newline && (s->r.buffer_len != len))
553 len = newline + 1 - str;
555 ulog(prio, "%s\n", str);
557 ustream_consume(s, len);
560 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
564 instance_stdout(struct ustream *s, int bytes)
566 instance_stdio(s, LOG_INFO,
567 container_of(s, struct service_instance, _stdout.stream));
571 instance_stderr(struct ustream *s, int bytes)
573 instance_stdio(s, LOG_ERR,
574 container_of(s, struct service_instance, _stderr.stream));
578 instance_timeout(struct uloop_timeout *t)
580 struct service_instance *in;
582 in = container_of(t, struct service_instance, timeout);
585 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
586 in->srv->name, in->name, in->proc.pid);
587 kill(in->proc.pid, SIGKILL);
588 } else if (in->restart || in->respawn)
593 instance_delete(struct service_instance *in)
595 struct service *s = in->srv;
597 avl_delete(&s->instances.avl, &in->node.avl);
603 instance_exit_code(int ret)
605 if (WIFEXITED(ret)) {
606 return WEXITSTATUS(ret);
609 if (WIFSIGNALED(ret)) {
610 return SIGNALLED_OFFSET + WTERMSIG(ret);
613 if (WIFSTOPPED(ret)) {
614 return WSTOPSIG(ret);
621 instance_exit(struct uloop_process *p, int ret)
623 struct service_instance *in;
627 in = container_of(p, struct service_instance, proc);
629 clock_gettime(CLOCK_MONOTONIC, &tp);
630 runtime = tp.tv_sec - in->start.tv_sec;
632 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
634 in->exit_code = instance_exit_code(ret);
635 uloop_timeout_cancel(&in->timeout);
636 service_event("instance.stop", in->srv->name, in->name);
639 instance_removepid(in);
644 } else if (in->restart) {
646 } else if (in->respawn) {
647 if (runtime < in->respawn_threshold)
650 in->respawn_count = 0;
651 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
652 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
653 in->srv->name, in->name, in->respawn_count, runtime);
654 in->restart = in->respawn = 0;
656 service_event("instance.fail", in->srv->name, in->name);
658 service_event("instance.respawn", in->srv->name, in->name);
659 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
665 instance_stop(struct service_instance *in, bool halt)
667 if (!in->proc.pending) {
673 in->restart = in->respawn = false;
674 kill(in->proc.pid, SIGTERM);
675 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
679 instance_restart(struct service_instance *in)
681 if (!in->proc.pending)
684 if (in->reload_signal) {
685 kill(in->proc.pid, in->reload_signal);
691 kill(in->proc.pid, SIGTERM);
692 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
695 static bool string_changed(const char *a, const char *b)
697 return !((!a && !b) || (a && b && !strcmp(a, b)));
701 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
706 if (!blob_attr_equal(in->command, in_new->command))
709 if (!blobmsg_list_equal(&in->env, &in_new->env))
712 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
715 if (!blobmsg_list_equal(&in->file, &in_new->file))
718 if (in->nice != in_new->nice)
721 if (in->syslog_facility != in_new->syslog_facility)
724 if (string_changed(in->user, in_new->user))
727 if (string_changed(in->group, in_new->group))
730 if (in->uid != in_new->uid)
733 if (in->pw_gid != in_new->pw_gid)
736 if (string_changed(in->pidfile, in_new->pidfile))
739 if (in->respawn_retry != in_new->respawn_retry)
741 if (in->respawn_threshold != in_new->respawn_threshold)
743 if (in->respawn_timeout != in_new->respawn_timeout)
746 if ((!in->seccomp && in_new->seccomp) ||
747 (in->seccomp && !in_new->seccomp) ||
748 (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
751 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
754 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
757 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
764 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
766 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
767 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
769 return n1->ifindex == n2->ifindex;
773 instance_netdev_update(struct blobmsg_list_node *l)
775 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
777 n->ifindex = if_nametoindex(n->node.avl.key);
781 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
783 struct instance_file *f1 = container_of(l1, struct instance_file, node);
784 struct instance_file *f2 = container_of(l2, struct instance_file, node);
786 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
790 instance_file_update(struct blobmsg_list_node *l)
792 struct instance_file *f = container_of(l, struct instance_file, node);
797 memset(f->md5, 0, sizeof(f->md5));
799 fd = open(l->avl.key, O_RDONLY);
805 len = read(fd, buf, sizeof(buf));
815 md5_hash(buf, len, &md5);
818 md5_end(f->md5, &md5);
823 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
828 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
832 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
834 struct blobmsg_list_node *node;
839 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
842 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
844 blobmsg_list_for_each(l, node)
851 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
853 struct blob_attr *tb[__JAIL_ATTR_MAX];
854 struct jail *jail = &in->jail;
856 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
857 blobmsg_data(attr), blobmsg_data_len(attr));
861 if (tb[JAIL_ATTR_REQUIREJAIL]) {
862 in->require_jail = true;
865 if (tb[JAIL_ATTR_NAME]) {
866 jail->name = strdup(blobmsg_get_string(tb[JAIL_ATTR_NAME]));
869 if (tb[JAIL_ATTR_HOSTNAME]) {
870 jail->hostname = strdup(blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]));
873 if (tb[JAIL_ATTR_PROCFS]) {
874 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
877 if (tb[JAIL_ATTR_SYSFS]) {
878 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
881 if (tb[JAIL_ATTR_UBUS]) {
882 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
885 if (tb[JAIL_ATTR_LOG]) {
886 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
889 if (tb[JAIL_ATTR_RONLY]) {
890 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
893 if (tb[JAIL_ATTR_NETNS]) {
894 jail->netns = blobmsg_get_bool(tb[JAIL_ATTR_NETNS]);
897 if (tb[JAIL_ATTR_USERNS]) {
898 jail->userns = blobmsg_get_bool(tb[JAIL_ATTR_USERNS]);
901 if (tb[JAIL_ATTR_CGROUPSNS]) {
902 jail->cgroupsns = blobmsg_get_bool(tb[JAIL_ATTR_CGROUPSNS]);
906 if (tb[JAIL_ATTR_MOUNT]) {
907 struct blob_attr *cur;
910 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
912 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
929 if (in->tmpoverlaysize)
932 if (in->no_new_privs)
939 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
941 struct blob_attr *cur, *cur2;
945 cur = tb[INSTANCE_ATTR_COMMAND];
951 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
954 blobmsg_for_each_attr(cur2, cur, rem) {
964 instance_config_parse(struct service_instance *in)
966 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
967 struct blob_attr *cur, *cur2;
971 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
972 blobmsg_data(in->config), blobmsg_data_len(in->config));
974 if (!instance_config_parse_command(in, tb))
977 if (tb[INSTANCE_ATTR_TERMTIMEOUT])
978 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
979 if (tb[INSTANCE_ATTR_RESPAWN]) {
981 uint32_t vals[3] = { 3600, 5, 5};
983 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
984 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
986 vals[i] = atoi(blobmsg_get_string(cur2));
990 in->respawn_count = 0;
991 in->respawn_threshold = vals[0];
992 in->respawn_timeout = vals[1];
993 in->respawn_retry = vals[2];
995 if (tb[INSTANCE_ATTR_TRIGGER]) {
996 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
997 trigger_add(in->trigger, in);
1000 if (tb[INSTANCE_ATTR_WATCH]) {
1001 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
1002 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
1004 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
1005 watch_add(blobmsg_get_string(cur2), in);
1009 if ((cur = tb[INSTANCE_ATTR_NICE])) {
1010 in->nice = (int8_t) blobmsg_get_u32(cur);
1011 if (in->nice < -20 || in->nice > 20)
1015 if (tb[INSTANCE_ATTR_USER]) {
1016 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
1017 struct passwd *p = getpwnam(user);
1019 in->user = strdup(user);
1020 in->uid = p->pw_uid;
1021 in->gr_gid = in->pw_gid = p->pw_gid;
1025 if (tb[INSTANCE_ATTR_GROUP]) {
1026 const char *group = blobmsg_get_string(tb[INSTANCE_ATTR_GROUP]);
1027 struct group *p = getgrnam(group);
1029 in->group = strdup(group);
1030 in->gr_gid = p->gr_gid;
1034 if (tb[INSTANCE_ATTR_TRACE])
1035 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
1037 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
1038 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
1040 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
1041 in->seccomp = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]));
1043 if (tb[INSTANCE_ATTR_EXTROOT])
1044 in->extroot = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_EXTROOT]));
1046 if (tb[INSTANCE_ATTR_OVERLAYDIR])
1047 in->overlaydir = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_OVERLAYDIR]));
1049 if (tb[INSTANCE_ATTR_TMPOVERLAYSIZE])
1050 in->tmpoverlaysize = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_TMPOVERLAYSIZE]));
1052 if (tb[INSTANCE_ATTR_PIDFILE]) {
1053 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
1055 in->pidfile = strdup(pidfile);
1058 if (tb[INSTANCE_ATTR_RELOADSIG])
1059 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
1061 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
1062 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
1065 r = stat(UJAIL_BIN_PATH, &s);
1067 if (in->require_jail) {
1068 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1069 in->srv->name, in->name, UJAIL_BIN_PATH, r);
1072 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH, r);
1073 in->has_jail = false;
1077 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
1078 in->_stdout.fd.fd = -1;
1080 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
1081 in->_stderr.fd.fd = -1;
1083 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
1085 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
1088 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
1091 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
1094 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
1097 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
1100 if (tb[INSTANCE_ATTR_FACILITY]) {
1101 int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1102 if (facility != -1) {
1103 in->syslog_facility = facility;
1104 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1106 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1113 instance_config_cleanup(struct service_instance *in)
1115 blobmsg_list_free(&in->env);
1116 blobmsg_list_free(&in->data);
1117 blobmsg_list_free(&in->netdev);
1118 blobmsg_list_free(&in->file);
1119 blobmsg_list_free(&in->limits);
1120 blobmsg_list_free(&in->errors);
1121 blobmsg_list_free(&in->jail.mount);
1125 instance_config_move_strdup(char **dst, char *src)
1139 instance_config_move(struct service_instance *in, struct service_instance *in_src)
1141 instance_config_cleanup(in);
1142 blobmsg_list_move(&in->env, &in_src->env);
1143 blobmsg_list_move(&in->data, &in_src->data);
1144 blobmsg_list_move(&in->netdev, &in_src->netdev);
1145 blobmsg_list_move(&in->file, &in_src->file);
1146 blobmsg_list_move(&in->limits, &in_src->limits);
1147 blobmsg_list_move(&in->errors, &in_src->errors);
1148 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
1149 in->trigger = in_src->trigger;
1150 in->command = in_src->command;
1151 in->respawn = in_src->respawn;
1152 in->respawn_retry = in_src->respawn_retry;
1153 in->respawn_threshold = in_src->respawn_threshold;
1154 in->respawn_timeout = in_src->respawn_timeout;
1155 in->name = in_src->name;
1156 in->trace = in_src->trace;
1157 in->node.avl.key = in_src->node.avl.key;
1158 in->syslog_facility = in_src->syslog_facility;
1160 instance_config_move_strdup(&in->pidfile, in_src->pidfile);
1161 instance_config_move_strdup(&in->seccomp, in_src->seccomp);
1162 instance_config_move_strdup(&in->jail.name, in_src->jail.name);
1163 instance_config_move_strdup(&in->jail.hostname, in_src->jail.hostname);
1166 in->config = in_src->config;
1167 in_src->config = NULL;
1171 instance_update(struct service_instance *in, struct service_instance *in_new)
1173 bool changed = instance_config_changed(in, in_new);
1174 bool running = in->proc.pending;
1175 bool stopping = in->halt;
1177 if (!running || stopping) {
1178 instance_config_move(in, in_new);
1182 instance_restart(in);
1183 instance_config_move(in, in_new);
1184 /* restart happens in the child callback handler */
1189 instance_free(struct service_instance *in)
1191 instance_free_stdio(in);
1192 uloop_process_delete(&in->proc);
1193 uloop_timeout_cancel(&in->timeout);
1196 instance_config_cleanup(in);
1201 free(in->overlaydir);
1202 free(in->tmpoverlaysize);
1203 free(in->jail.name);
1204 free(in->jail.hostname);
1211 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1213 config = blob_memdup(config);
1215 in->name = blobmsg_name(config);
1216 in->config = config;
1217 in->timeout.cb = instance_timeout;
1218 in->proc.cb = instance_exit;
1219 in->term_timeout = 5;
1220 in->syslog_facility = LOG_DAEMON;
1222 in->require_jail = false;
1224 in->_stdout.fd.fd = -2;
1225 in->_stdout.stream.string_data = true;
1226 in->_stdout.stream.notify_read = instance_stdout;
1228 in->_stderr.fd.fd = -2;
1229 in->_stderr.stream.string_data = true;
1230 in->_stderr.stream.notify_read = instance_stderr;
1232 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1233 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1234 blobmsg_list_simple_init(&in->env);
1235 blobmsg_list_simple_init(&in->data);
1236 blobmsg_list_simple_init(&in->limits);
1237 blobmsg_list_simple_init(&in->errors);
1238 blobmsg_list_simple_init(&in->jail.mount);
1239 in->valid = instance_config_parse(in);
1242 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1249 i = blobmsg_open_table(b, in->name);
1250 blobmsg_add_u8(b, "running", in->proc.pending);
1251 if (in->proc.pending)
1252 blobmsg_add_u32(b, "pid", in->proc.pid);
1254 blobmsg_add_blob(b, in->command);
1255 blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1256 if (!in->proc.pending)
1257 blobmsg_add_u32(b, "exit_code", in->exit_code);
1259 if (!avl_is_empty(&in->errors.avl)) {
1260 struct blobmsg_list_node *var;
1261 void *e = blobmsg_open_array(b, "errors");
1262 blobmsg_list_for_each(&in->errors, var)
1263 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1264 blobmsg_close_table(b, e);
1267 if (!avl_is_empty(&in->env.avl)) {
1268 struct blobmsg_list_node *var;
1269 void *e = blobmsg_open_table(b, "env");
1270 blobmsg_list_for_each(&in->env, var)
1271 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1272 blobmsg_close_table(b, e);
1275 if (!avl_is_empty(&in->data.avl)) {
1276 struct blobmsg_list_node *var;
1277 void *e = blobmsg_open_table(b, "data");
1278 blobmsg_list_for_each(&in->data, var)
1279 blobmsg_add_blob(b, var->data);
1280 blobmsg_close_table(b, e);
1283 if (!avl_is_empty(&in->limits.avl)) {
1284 struct blobmsg_list_node *var;
1285 void *e = blobmsg_open_table(b, "limits");
1286 blobmsg_list_for_each(&in->limits, var)
1287 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1288 blobmsg_close_table(b, e);
1291 if (in->reload_signal)
1292 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1295 void *r = blobmsg_open_table(b, "respawn");
1296 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1297 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1298 blobmsg_add_u32(b, "retry", in->respawn_retry);
1299 blobmsg_close_table(b, r);
1303 blobmsg_add_u8(b, "trace", true);
1305 if (in->no_new_privs)
1306 blobmsg_add_u8(b, "no_new_privs", true);
1309 blobmsg_add_string(b, "seccomp", in->seccomp);
1312 blobmsg_add_string(b, "pidfile", in->pidfile);
1315 blobmsg_add_string(b, "user", in->user);
1318 blobmsg_add_string(b, "group", in->group);
1321 void *r = blobmsg_open_table(b, "jail");
1323 blobmsg_add_string(b, "name", in->jail.name);
1324 if (in->jail.hostname)
1325 blobmsg_add_string(b, "hostname", in->jail.hostname);
1327 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1328 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1329 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1330 blobmsg_add_u8(b, "log", in->jail.log);
1331 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1332 blobmsg_add_u8(b, "netns", in->jail.netns);
1333 blobmsg_add_u8(b, "userns", in->jail.userns);
1334 blobmsg_add_u8(b, "cgroupsns", in->jail.cgroupsns);
1335 blobmsg_close_table(b, r);
1336 if (!avl_is_empty(&in->jail.mount.avl)) {
1337 struct blobmsg_list_node *var;
1338 void *e = blobmsg_open_table(b, "mount");
1339 blobmsg_list_for_each(&in->jail.mount, var)
1340 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1341 blobmsg_close_table(b, e);
1346 blobmsg_add_string(b, "extroot", in->extroot);
1348 blobmsg_add_string(b, "overlaydir", in->overlaydir);
1349 if (in->tmpoverlaysize)
1350 blobmsg_add_string(b, "tmpoverlaysize", in->tmpoverlaysize);
1352 if (verbose && in->trigger)
1353 blobmsg_add_blob(b, in->trigger);
1355 blobmsg_close_table(b, i);