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,
108 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
109 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
110 [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
111 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
112 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
113 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
114 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
115 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
116 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
117 [JAIL_ATTR_NETNS] = { "netns", BLOBMSG_TYPE_BOOL },
118 [JAIL_ATTR_REQUIREJAIL] = { "requirejail", BLOBMSG_TYPE_BOOL },
121 struct instance_netdev {
122 struct blobmsg_list_node node;
126 struct instance_file {
127 struct blobmsg_list_node node;
136 static const struct rlimit_name rlimit_names[] = {
138 { "core", RLIMIT_CORE },
139 { "cpu", RLIMIT_CPU },
140 { "data", RLIMIT_DATA },
141 { "fsize", RLIMIT_FSIZE },
142 { "memlock", RLIMIT_MEMLOCK },
143 { "nofile", RLIMIT_NOFILE },
144 { "nproc", RLIMIT_NPROC },
145 { "rss", RLIMIT_RSS },
146 { "stack", RLIMIT_STACK },
148 { "nice", RLIMIT_NICE },
149 { "rtprio", RLIMIT_RTPRIO },
150 { "msgqueue", RLIMIT_MSGQUEUE },
151 { "sigpending", RLIMIT_SIGPENDING },
156 static void closefd(int fd)
158 if (fd > STDERR_FILENO)
162 /* convert a string into numeric syslog facility or return -1 if no match found */
164 syslog_facility_str_to_int(const char *facility)
166 CODE *p = facilitynames;
168 while (p->c_name && strcasecmp(p->c_name, facility))
175 instance_limits(const char *limit, const char *value)
179 unsigned long cur, max;
181 for (i = 0; rlimit_names[i].name != NULL; i++) {
182 if (strcmp(rlimit_names[i].name, limit))
184 if (!strcmp(value, "unlimited")) {
185 rlim.rlim_cur = RLIM_INFINITY;
186 rlim.rlim_max = RLIM_INFINITY;
188 if (getrlimit(rlimit_names[i].resource, &rlim))
194 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
201 setrlimit(rlimit_names[i].resource, &rlim);
207 jail_run(struct service_instance *in, char **argv)
209 struct blobmsg_list_node *var;
210 struct jail *jail = &in->jail;
213 argv[argc++] = UJAIL_BIN_PATH;
217 argv[argc++] = jail->name;
220 if (jail->hostname) {
222 argv[argc++] = jail->hostname;
227 argv[argc++] = in->seccomp;
232 argv[argc++] = in->user;
237 argv[argc++] = in->group;
240 if (in->no_new_privs)
261 blobmsg_list_for_each(&jail->mount, var) {
262 const char *type = blobmsg_data(var->data);
268 argv[argc++] = (char *) blobmsg_name(var->data);
277 instance_removepid(struct service_instance *in) {
280 if (unlink(in->pidfile)) {
281 ERROR("Failed to remove pidfile: %s: %m\n", in->pidfile);
288 instance_writepid(struct service_instance *in)
295 _pidfile = fopen(in->pidfile, "w");
296 if (_pidfile == NULL) {
297 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
300 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
301 ERROR("failed to write pidfile: %s: %m", in->pidfile);
305 if (fclose(_pidfile)) {
306 ERROR("failed to close pidfile: %s: %m", in->pidfile);
314 instance_run(struct service_instance *in, int _stdout, int _stderr)
316 struct blobmsg_list_node *var;
317 struct blob_attr *cur;
319 int argc = 1; /* NULL terminated */
321 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
322 bool setlbf = _stdout >= 0;
325 setpriority(PRIO_PROCESS, 0, in->nice);
327 blobmsg_for_each_attr(cur, in->command, rem)
330 blobmsg_list_for_each(&in->env, var)
331 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
334 setenv("SECCOMP_FILE", in->seccomp, 1);
337 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
339 blobmsg_list_for_each(&in->limits, var)
340 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
342 if (in->trace || seccomp)
345 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
348 #ifdef SECCOMP_SUPPORT
350 argv[argc++] = "/sbin/utrace";
352 argv[argc++] = "/sbin/seccomp-trace";
354 if (in->trace || seccomp)
355 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
359 argc = jail_run(in, argv);
360 if (argc != in->jail.argc)
361 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
362 in->jail.argc, argc, in->srv->name, in->name);
365 blobmsg_for_each_attr(cur, in->command, rem)
366 argv[argc++] = blobmsg_data(cur);
370 _stdin = open("/dev/null", O_RDONLY);
373 _stdout = open("/dev/null", O_WRONLY);
376 _stderr = open("/dev/null", O_WRONLY);
379 dup2(_stdin, STDIN_FILENO);
383 dup2(_stdout, STDOUT_FILENO);
387 dup2(_stderr, STDERR_FILENO);
391 if (!in->has_jail && in->user && in->pw_gid && initgroups(in->user, in->pw_gid)) {
392 ERROR("failed to initgroups() for user %s: %m\n", in->user);
395 if (!in->has_jail && in->gr_gid && setgid(in->gr_gid)) {
396 ERROR("failed to set group id %d: %m\n", in->gr_gid);
399 if (!in->has_jail && in->uid && setuid(in->uid)) {
400 ERROR("failed to set user id %d: %m\n", in->uid);
404 execvp(argv[0], argv);
409 instance_free_stdio(struct service_instance *in)
411 if (in->_stdout.fd.fd > -1) {
412 ustream_free(&in->_stdout.stream);
413 close(in->_stdout.fd.fd);
414 in->_stdout.fd.fd = -1;
417 if (in->_stderr.fd.fd > -1) {
418 ustream_free(&in->_stderr.stream);
419 close(in->_stderr.fd.fd);
420 in->_stderr.fd.fd = -1;
425 instance_start(struct service_instance *in)
428 int opipe[2] = { -1, -1 };
429 int epipe[2] = { -1, -1 };
431 if (!avl_is_empty(&in->errors.avl)) {
432 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
437 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
441 if (in->proc.pending) {
447 instance_free_stdio(in);
448 if (in->_stdout.fd.fd > -2) {
450 ULOG_WARN("pipe() failed: %m\n");
451 opipe[0] = opipe[1] = -1;
455 if (in->_stderr.fd.fd > -2) {
457 ULOG_WARN("pipe() failed: %m\n");
458 epipe[0] = epipe[1] = -1;
476 instance_run(in, opipe[1], epipe[1]);
480 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
482 instance_writepid(in);
483 clock_gettime(CLOCK_MONOTONIC, &in->start);
484 uloop_process_add(&in->proc);
487 ustream_fd_init(&in->_stdout, opipe[0]);
489 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
493 ustream_fd_init(&in->_stderr, epipe[0]);
495 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
498 service_event("instance.start", in->srv->name, in->name);
502 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
504 char *newline, *str, *arg0, ident[32];
507 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
508 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
509 ulog_open(ULOG_SYSLOG, in->syslog_facility, ident);
512 str = ustream_get_read_buf(s, &len);
516 newline = memchr(str, '\n', len);
517 if (!newline && (s->r.buffer_len != len))
522 len = newline + 1 - str;
524 ulog(prio, "%s\n", str);
526 ustream_consume(s, len);
529 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
533 instance_stdout(struct ustream *s, int bytes)
535 instance_stdio(s, LOG_INFO,
536 container_of(s, struct service_instance, _stdout.stream));
540 instance_stderr(struct ustream *s, int bytes)
542 instance_stdio(s, LOG_ERR,
543 container_of(s, struct service_instance, _stderr.stream));
547 instance_timeout(struct uloop_timeout *t)
549 struct service_instance *in;
551 in = container_of(t, struct service_instance, timeout);
554 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
555 in->srv->name, in->name, in->proc.pid);
556 kill(in->proc.pid, SIGKILL);
557 } else if (in->restart || in->respawn)
562 instance_delete(struct service_instance *in)
564 struct service *s = in->srv;
566 avl_delete(&s->instances.avl, &in->node.avl);
572 instance_exit_code(int ret)
574 if (WIFEXITED(ret)) {
575 return WEXITSTATUS(ret);
578 if (WIFSIGNALED(ret)) {
579 return SIGNALLED_OFFSET + WTERMSIG(ret);
582 if (WIFSTOPPED(ret)) {
583 return WSTOPSIG(ret);
590 instance_exit(struct uloop_process *p, int ret)
592 struct service_instance *in;
596 in = container_of(p, struct service_instance, proc);
598 clock_gettime(CLOCK_MONOTONIC, &tp);
599 runtime = tp.tv_sec - in->start.tv_sec;
601 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
603 in->exit_code = instance_exit_code(ret);
604 uloop_timeout_cancel(&in->timeout);
605 service_event("instance.stop", in->srv->name, in->name);
608 instance_removepid(in);
613 } else if (in->restart) {
615 } else if (in->respawn) {
616 if (runtime < in->respawn_threshold)
619 in->respawn_count = 0;
620 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
621 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
622 in->srv->name, in->name, in->respawn_count, runtime);
623 in->restart = in->respawn = 0;
625 service_event("instance.fail", in->srv->name, in->name);
627 service_event("instance.respawn", in->srv->name, in->name);
628 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
634 instance_stop(struct service_instance *in, bool halt)
636 if (!in->proc.pending) {
642 in->restart = in->respawn = false;
643 kill(in->proc.pid, SIGTERM);
644 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
648 instance_restart(struct service_instance *in)
650 if (!in->proc.pending)
653 if (in->reload_signal) {
654 kill(in->proc.pid, in->reload_signal);
660 kill(in->proc.pid, SIGTERM);
661 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
664 static bool string_changed(const char *a, const char *b)
666 return !((!a && !b) || (a && b && !strcmp(a, b)));
670 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
675 if (!blob_attr_equal(in->command, in_new->command))
678 if (!blobmsg_list_equal(&in->env, &in_new->env))
681 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
684 if (!blobmsg_list_equal(&in->file, &in_new->file))
687 if (in->nice != in_new->nice)
690 if (in->syslog_facility != in_new->syslog_facility)
693 if (string_changed(in->user, in_new->user))
696 if (string_changed(in->group, in_new->group))
699 if (in->uid != in_new->uid)
702 if (in->pw_gid != in_new->pw_gid)
705 if (string_changed(in->pidfile, in_new->pidfile))
708 if (in->respawn_retry != in_new->respawn_retry)
710 if (in->respawn_threshold != in_new->respawn_threshold)
712 if (in->respawn_timeout != in_new->respawn_timeout)
715 if ((!in->seccomp && in_new->seccomp) ||
716 (in->seccomp && !in_new->seccomp) ||
717 (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
720 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
723 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
726 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
733 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
735 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
736 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
738 return n1->ifindex == n2->ifindex;
742 instance_netdev_update(struct blobmsg_list_node *l)
744 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
746 n->ifindex = if_nametoindex(n->node.avl.key);
750 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
752 struct instance_file *f1 = container_of(l1, struct instance_file, node);
753 struct instance_file *f2 = container_of(l2, struct instance_file, node);
755 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
759 instance_file_update(struct blobmsg_list_node *l)
761 struct instance_file *f = container_of(l, struct instance_file, node);
766 memset(f->md5, 0, sizeof(f->md5));
768 fd = open(l->avl.key, O_RDONLY);
774 len = read(fd, buf, sizeof(buf));
784 md5_hash(buf, len, &md5);
787 md5_end(f->md5, &md5);
792 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
797 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
801 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
803 struct blobmsg_list_node *node;
808 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
811 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
813 blobmsg_list_for_each(l, node)
820 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
822 struct blob_attr *tb[__JAIL_ATTR_MAX];
823 struct jail *jail = &in->jail;
825 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
826 blobmsg_data(attr), blobmsg_data_len(attr));
830 if (tb[JAIL_ATTR_REQUIREJAIL]) {
831 in->require_jail = true;
834 if (tb[JAIL_ATTR_NAME]) {
835 jail->name = strdup(blobmsg_get_string(tb[JAIL_ATTR_NAME]));
838 if (tb[JAIL_ATTR_HOSTNAME]) {
839 jail->hostname = strdup(blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]));
842 if (tb[JAIL_ATTR_PROCFS]) {
843 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
846 if (tb[JAIL_ATTR_SYSFS]) {
847 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
850 if (tb[JAIL_ATTR_UBUS]) {
851 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
854 if (tb[JAIL_ATTR_LOG]) {
855 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
858 if (tb[JAIL_ATTR_RONLY]) {
859 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
862 if (tb[JAIL_ATTR_NETNS]) {
863 jail->netns = blobmsg_get_bool(tb[JAIL_ATTR_NETNS]);
866 if (tb[JAIL_ATTR_MOUNT]) {
867 struct blob_attr *cur;
870 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
872 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
883 if (in->no_new_privs)
890 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
892 struct blob_attr *cur, *cur2;
896 cur = tb[INSTANCE_ATTR_COMMAND];
902 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
905 blobmsg_for_each_attr(cur2, cur, rem) {
915 instance_config_parse(struct service_instance *in)
917 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
918 struct blob_attr *cur, *cur2;
922 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
923 blobmsg_data(in->config), blobmsg_data_len(in->config));
925 if (!instance_config_parse_command(in, tb))
928 if (tb[INSTANCE_ATTR_TERMTIMEOUT])
929 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
930 if (tb[INSTANCE_ATTR_RESPAWN]) {
932 uint32_t vals[3] = { 3600, 5, 5};
934 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
935 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
937 vals[i] = atoi(blobmsg_get_string(cur2));
941 in->respawn_count = 0;
942 in->respawn_threshold = vals[0];
943 in->respawn_timeout = vals[1];
944 in->respawn_retry = vals[2];
946 if (tb[INSTANCE_ATTR_TRIGGER]) {
947 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
948 trigger_add(in->trigger, in);
951 if (tb[INSTANCE_ATTR_WATCH]) {
952 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
953 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
955 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
956 watch_add(blobmsg_get_string(cur2), in);
960 if ((cur = tb[INSTANCE_ATTR_NICE])) {
961 in->nice = (int8_t) blobmsg_get_u32(cur);
962 if (in->nice < -20 || in->nice > 20)
966 if (tb[INSTANCE_ATTR_USER]) {
967 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
968 struct passwd *p = getpwnam(user);
970 in->user = strdup(user);
972 in->gr_gid = in->pw_gid = p->pw_gid;
976 if (tb[INSTANCE_ATTR_GROUP]) {
977 const char *group = blobmsg_get_string(tb[INSTANCE_ATTR_GROUP]);
978 struct group *p = getgrnam(group);
980 in->group = strdup(group);
981 in->gr_gid = p->gr_gid;
985 if (tb[INSTANCE_ATTR_TRACE])
986 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
988 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
989 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
991 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
992 in->seccomp = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]));
994 if (tb[INSTANCE_ATTR_PIDFILE]) {
995 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
997 in->pidfile = strdup(pidfile);
1000 if (tb[INSTANCE_ATTR_RELOADSIG])
1001 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
1003 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
1004 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
1007 r = stat(UJAIL_BIN_PATH, &s);
1009 if (in->require_jail) {
1010 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1011 in->srv->name, in->name, UJAIL_BIN_PATH, r);
1014 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH, r);
1015 in->has_jail = false;
1019 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
1020 in->_stdout.fd.fd = -1;
1022 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
1023 in->_stderr.fd.fd = -1;
1025 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
1027 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
1030 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
1033 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
1036 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
1039 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
1042 if (tb[INSTANCE_ATTR_FACILITY]) {
1043 int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1044 if (facility != -1) {
1045 in->syslog_facility = facility;
1046 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1048 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1055 instance_config_cleanup(struct service_instance *in)
1057 blobmsg_list_free(&in->env);
1058 blobmsg_list_free(&in->data);
1059 blobmsg_list_free(&in->netdev);
1060 blobmsg_list_free(&in->file);
1061 blobmsg_list_free(&in->limits);
1062 blobmsg_list_free(&in->errors);
1063 blobmsg_list_free(&in->jail.mount);
1067 instance_config_move_strdup(char **dst, char *src)
1081 instance_config_move(struct service_instance *in, struct service_instance *in_src)
1083 instance_config_cleanup(in);
1084 blobmsg_list_move(&in->env, &in_src->env);
1085 blobmsg_list_move(&in->data, &in_src->data);
1086 blobmsg_list_move(&in->netdev, &in_src->netdev);
1087 blobmsg_list_move(&in->file, &in_src->file);
1088 blobmsg_list_move(&in->limits, &in_src->limits);
1089 blobmsg_list_move(&in->errors, &in_src->errors);
1090 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
1091 in->trigger = in_src->trigger;
1092 in->command = in_src->command;
1093 in->respawn = in_src->respawn;
1094 in->respawn_retry = in_src->respawn_retry;
1095 in->respawn_threshold = in_src->respawn_threshold;
1096 in->respawn_timeout = in_src->respawn_timeout;
1097 in->name = in_src->name;
1098 in->trace = in_src->trace;
1099 in->node.avl.key = in_src->node.avl.key;
1100 in->syslog_facility = in_src->syslog_facility;
1102 instance_config_move_strdup(&in->pidfile, in_src->pidfile);
1103 instance_config_move_strdup(&in->seccomp, in_src->seccomp);
1104 instance_config_move_strdup(&in->jail.name, in_src->jail.name);
1105 instance_config_move_strdup(&in->jail.hostname, in_src->jail.hostname);
1108 in->config = in_src->config;
1109 in_src->config = NULL;
1113 instance_update(struct service_instance *in, struct service_instance *in_new)
1115 bool changed = instance_config_changed(in, in_new);
1116 bool running = in->proc.pending;
1117 bool stopping = in->halt;
1119 if (!running || stopping) {
1120 instance_config_move(in, in_new);
1124 instance_restart(in);
1125 instance_config_move(in, in_new);
1126 /* restart happens in the child callback handler */
1131 instance_free(struct service_instance *in)
1133 instance_free_stdio(in);
1134 uloop_process_delete(&in->proc);
1135 uloop_timeout_cancel(&in->timeout);
1138 instance_config_cleanup(in);
1142 free(in->jail.name);
1143 free(in->jail.hostname);
1150 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1152 config = blob_memdup(config);
1154 in->name = blobmsg_name(config);
1155 in->config = config;
1156 in->timeout.cb = instance_timeout;
1157 in->proc.cb = instance_exit;
1158 in->term_timeout = 5;
1159 in->syslog_facility = LOG_DAEMON;
1161 in->require_jail = false;
1163 in->_stdout.fd.fd = -2;
1164 in->_stdout.stream.string_data = true;
1165 in->_stdout.stream.notify_read = instance_stdout;
1167 in->_stderr.fd.fd = -2;
1168 in->_stderr.stream.string_data = true;
1169 in->_stderr.stream.notify_read = instance_stderr;
1171 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1172 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1173 blobmsg_list_simple_init(&in->env);
1174 blobmsg_list_simple_init(&in->data);
1175 blobmsg_list_simple_init(&in->limits);
1176 blobmsg_list_simple_init(&in->errors);
1177 blobmsg_list_simple_init(&in->jail.mount);
1178 in->valid = instance_config_parse(in);
1181 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1188 i = blobmsg_open_table(b, in->name);
1189 blobmsg_add_u8(b, "running", in->proc.pending);
1190 if (in->proc.pending)
1191 blobmsg_add_u32(b, "pid", in->proc.pid);
1193 blobmsg_add_blob(b, in->command);
1194 blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1195 if (!in->proc.pending)
1196 blobmsg_add_u32(b, "exit_code", in->exit_code);
1198 if (!avl_is_empty(&in->errors.avl)) {
1199 struct blobmsg_list_node *var;
1200 void *e = blobmsg_open_array(b, "errors");
1201 blobmsg_list_for_each(&in->errors, var)
1202 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1203 blobmsg_close_table(b, e);
1206 if (!avl_is_empty(&in->env.avl)) {
1207 struct blobmsg_list_node *var;
1208 void *e = blobmsg_open_table(b, "env");
1209 blobmsg_list_for_each(&in->env, var)
1210 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1211 blobmsg_close_table(b, e);
1214 if (!avl_is_empty(&in->data.avl)) {
1215 struct blobmsg_list_node *var;
1216 void *e = blobmsg_open_table(b, "data");
1217 blobmsg_list_for_each(&in->data, var)
1218 blobmsg_add_blob(b, var->data);
1219 blobmsg_close_table(b, e);
1222 if (!avl_is_empty(&in->limits.avl)) {
1223 struct blobmsg_list_node *var;
1224 void *e = blobmsg_open_table(b, "limits");
1225 blobmsg_list_for_each(&in->limits, var)
1226 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1227 blobmsg_close_table(b, e);
1230 if (in->reload_signal)
1231 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1234 void *r = blobmsg_open_table(b, "respawn");
1235 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1236 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1237 blobmsg_add_u32(b, "retry", in->respawn_retry);
1238 blobmsg_close_table(b, r);
1242 blobmsg_add_u8(b, "trace", true);
1244 if (in->no_new_privs)
1245 blobmsg_add_u8(b, "no_new_privs", true);
1248 blobmsg_add_string(b, "seccomp", in->seccomp);
1251 blobmsg_add_string(b, "pidfile", in->pidfile);
1254 blobmsg_add_string(b, "user", in->user);
1257 blobmsg_add_string(b, "group", in->group);
1260 void *r = blobmsg_open_table(b, "jail");
1262 blobmsg_add_string(b, "name", in->jail.name);
1263 if (in->jail.hostname)
1264 blobmsg_add_string(b, "hostname", in->jail.hostname);
1265 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1266 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1267 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1268 blobmsg_add_u8(b, "log", in->jail.log);
1269 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1270 blobmsg_add_u8(b, "netns", in->jail.netns);
1271 blobmsg_close_table(b, r);
1272 if (!avl_is_empty(&in->jail.mount.avl)) {
1273 struct blobmsg_list_node *var;
1274 void *e = blobmsg_open_table(b, "mount");
1275 blobmsg_list_for_each(&in->jail.mount, var)
1276 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1277 blobmsg_close_table(b, e);
1281 if (verbose && in->trigger)
1282 blobmsg_add_blob(b, in->trigger);
1284 blobmsg_close_table(b, i);