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 },
106 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
107 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
108 [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
109 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
110 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
111 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
112 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
113 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
114 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
117 struct instance_netdev {
118 struct blobmsg_list_node node;
122 struct instance_file {
123 struct blobmsg_list_node node;
132 static const struct rlimit_name rlimit_names[] = {
134 { "core", RLIMIT_CORE },
135 { "cpu", RLIMIT_CPU },
136 { "data", RLIMIT_DATA },
137 { "fsize", RLIMIT_FSIZE },
138 { "memlock", RLIMIT_MEMLOCK },
139 { "nofile", RLIMIT_NOFILE },
140 { "nproc", RLIMIT_NPROC },
141 { "rss", RLIMIT_RSS },
142 { "stack", RLIMIT_STACK },
144 { "nice", RLIMIT_NICE },
145 { "rtprio", RLIMIT_RTPRIO },
146 { "msgqueue", RLIMIT_MSGQUEUE },
147 { "sigpending", RLIMIT_SIGPENDING },
152 static void closefd(int fd)
154 if (fd > STDERR_FILENO)
158 /* convert a string into numeric syslog facility or return -1 if no match found */
160 syslog_facility_str_to_int(const char *facility)
162 CODE *p = facilitynames;
164 while (p->c_name && strcasecmp(p->c_name, facility))
171 instance_limits(const char *limit, const char *value)
175 unsigned long cur, max;
177 for (i = 0; rlimit_names[i].name != NULL; i++) {
178 if (strcmp(rlimit_names[i].name, limit))
180 if (!strcmp(value, "unlimited")) {
181 rlim.rlim_cur = RLIM_INFINITY;
182 rlim.rlim_max = RLIM_INFINITY;
184 if (getrlimit(rlimit_names[i].resource, &rlim))
190 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
197 setrlimit(rlimit_names[i].resource, &rlim);
203 jail_run(struct service_instance *in, char **argv)
205 struct blobmsg_list_node *var;
206 struct jail *jail = &in->jail;
209 argv[argc++] = UJAIL_BIN_PATH;
213 argv[argc++] = jail->name;
216 if (jail->hostname) {
218 argv[argc++] = jail->hostname;
223 argv[argc++] = in->seccomp;
228 argv[argc++] = in->user;
233 argv[argc++] = in->group;
236 if (in->no_new_privs)
254 blobmsg_list_for_each(&jail->mount, var) {
255 const char *type = blobmsg_data(var->data);
261 argv[argc++] = (char *) blobmsg_name(var->data);
270 instance_removepid(struct service_instance *in) {
273 if (unlink(in->pidfile)) {
274 ERROR("Failed to remove pidfile: %s: %m\n", in->pidfile);
281 instance_writepid(struct service_instance *in)
288 _pidfile = fopen(in->pidfile, "w");
289 if (_pidfile == NULL) {
290 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
293 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
294 ERROR("failed to write pidfile: %s: %m", in->pidfile);
298 if (fclose(_pidfile)) {
299 ERROR("failed to close pidfile: %s: %m", in->pidfile);
307 instance_run(struct service_instance *in, int _stdout, int _stderr)
309 struct blobmsg_list_node *var;
310 struct blob_attr *cur;
312 int argc = 1; /* NULL terminated */
314 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
315 bool setlbf = _stdout >= 0;
318 setpriority(PRIO_PROCESS, 0, in->nice);
320 blobmsg_for_each_attr(cur, in->command, rem)
323 blobmsg_list_for_each(&in->env, var)
324 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
327 setenv("SECCOMP_FILE", in->seccomp, 1);
330 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
332 blobmsg_list_for_each(&in->limits, var)
333 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
335 if (in->trace || seccomp)
338 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
341 #ifdef SECCOMP_SUPPORT
343 argv[argc++] = "/sbin/utrace";
345 argv[argc++] = "/sbin/seccomp-trace";
347 if (in->trace || seccomp)
348 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
352 argc = jail_run(in, argv);
353 if (argc != in->jail.argc)
354 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
355 in->jail.argc, argc, in->srv->name, in->name);
358 blobmsg_for_each_attr(cur, in->command, rem)
359 argv[argc++] = blobmsg_data(cur);
363 _stdin = open("/dev/null", O_RDONLY);
366 _stdout = open("/dev/null", O_WRONLY);
369 _stderr = open("/dev/null", O_WRONLY);
372 dup2(_stdin, STDIN_FILENO);
376 dup2(_stdout, STDOUT_FILENO);
380 dup2(_stderr, STDERR_FILENO);
384 if (!in->has_jail && in->user && in->pw_gid && initgroups(in->user, in->pw_gid)) {
385 ERROR("failed to initgroups() for user %s: %m\n", in->user);
388 if (!in->has_jail && in->gr_gid && setgid(in->gr_gid)) {
389 ERROR("failed to set group id %d: %m\n", in->gr_gid);
392 if (!in->has_jail && in->uid && setuid(in->uid)) {
393 ERROR("failed to set user id %d: %m\n", in->uid);
397 execvp(argv[0], argv);
402 instance_free_stdio(struct service_instance *in)
404 if (in->_stdout.fd.fd > -1) {
405 ustream_free(&in->_stdout.stream);
406 close(in->_stdout.fd.fd);
407 in->_stdout.fd.fd = -1;
410 if (in->_stderr.fd.fd > -1) {
411 ustream_free(&in->_stderr.stream);
412 close(in->_stderr.fd.fd);
413 in->_stderr.fd.fd = -1;
418 instance_start(struct service_instance *in)
421 int opipe[2] = { -1, -1 };
422 int epipe[2] = { -1, -1 };
424 if (!avl_is_empty(&in->errors.avl)) {
425 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
430 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
434 if (in->proc.pending) {
440 instance_free_stdio(in);
441 if (in->_stdout.fd.fd > -2) {
443 ULOG_WARN("pipe() failed: %m\n");
444 opipe[0] = opipe[1] = -1;
448 if (in->_stderr.fd.fd > -2) {
450 ULOG_WARN("pipe() failed: %m\n");
451 epipe[0] = epipe[1] = -1;
469 instance_run(in, opipe[1], epipe[1]);
473 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
475 instance_writepid(in);
476 clock_gettime(CLOCK_MONOTONIC, &in->start);
477 uloop_process_add(&in->proc);
480 ustream_fd_init(&in->_stdout, opipe[0]);
482 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
486 ustream_fd_init(&in->_stderr, epipe[0]);
488 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
491 service_event("instance.start", in->srv->name, in->name);
495 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
497 char *newline, *str, *arg0, ident[32];
500 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
501 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
502 ulog_open(ULOG_SYSLOG, in->syslog_facility, ident);
505 str = ustream_get_read_buf(s, &len);
509 newline = memchr(str, '\n', len);
510 if (!newline && (s->r.buffer_len != len))
515 len = newline + 1 - str;
517 ulog(prio, "%s\n", str);
519 ustream_consume(s, len);
522 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
526 instance_stdout(struct ustream *s, int bytes)
528 instance_stdio(s, LOG_INFO,
529 container_of(s, struct service_instance, _stdout.stream));
533 instance_stderr(struct ustream *s, int bytes)
535 instance_stdio(s, LOG_ERR,
536 container_of(s, struct service_instance, _stderr.stream));
540 instance_timeout(struct uloop_timeout *t)
542 struct service_instance *in;
544 in = container_of(t, struct service_instance, timeout);
547 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
548 in->srv->name, in->name, in->proc.pid);
549 kill(in->proc.pid, SIGKILL);
550 } else if (in->restart || in->respawn)
555 instance_delete(struct service_instance *in)
557 struct service *s = in->srv;
559 avl_delete(&s->instances.avl, &in->node.avl);
565 instance_exit(struct uloop_process *p, int ret)
567 struct service_instance *in;
571 in = container_of(p, struct service_instance, proc);
573 clock_gettime(CLOCK_MONOTONIC, &tp);
574 runtime = tp.tv_sec - in->start.tv_sec;
576 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
578 uloop_timeout_cancel(&in->timeout);
579 service_event("instance.stop", in->srv->name, in->name);
582 instance_removepid(in);
587 } else if (in->restart) {
589 } else if (in->respawn) {
590 if (runtime < in->respawn_threshold)
593 in->respawn_count = 0;
594 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
595 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
596 in->srv->name, in->name, in->respawn_count, runtime);
597 in->restart = in->respawn = 0;
599 service_event("instance.fail", in->srv->name, in->name);
601 service_event("instance.respawn", in->srv->name, in->name);
602 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
608 instance_stop(struct service_instance *in, bool halt)
610 if (!in->proc.pending) {
616 in->restart = in->respawn = false;
617 kill(in->proc.pid, SIGTERM);
618 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
622 instance_restart(struct service_instance *in)
624 if (!in->proc.pending)
627 if (in->reload_signal) {
628 kill(in->proc.pid, in->reload_signal);
634 kill(in->proc.pid, SIGTERM);
635 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
638 static bool string_changed(const char *a, const char *b)
640 return !((!a && !b) || (a && b && !strcmp(a, b)));
644 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
649 if (!blob_attr_equal(in->command, in_new->command))
652 if (!blobmsg_list_equal(&in->env, &in_new->env))
655 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
658 if (!blobmsg_list_equal(&in->file, &in_new->file))
661 if (in->nice != in_new->nice)
664 if (in->syslog_facility != in_new->syslog_facility)
667 if (string_changed(in->user, in_new->user))
670 if (string_changed(in->group, in_new->group))
673 if (in->uid != in_new->uid)
676 if (in->pw_gid != in_new->pw_gid)
679 if (string_changed(in->pidfile, in_new->pidfile))
682 if (in->respawn_retry != in_new->respawn_retry)
684 if (in->respawn_threshold != in_new->respawn_threshold)
686 if (in->respawn_timeout != in_new->respawn_timeout)
689 if ((!in->seccomp && in_new->seccomp) ||
690 (in->seccomp && !in_new->seccomp) ||
691 (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
694 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
697 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
700 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
707 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
709 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
710 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
712 return n1->ifindex == n2->ifindex;
716 instance_netdev_update(struct blobmsg_list_node *l)
718 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
720 n->ifindex = if_nametoindex(n->node.avl.key);
724 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
726 struct instance_file *f1 = container_of(l1, struct instance_file, node);
727 struct instance_file *f2 = container_of(l2, struct instance_file, node);
729 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
733 instance_file_update(struct blobmsg_list_node *l)
735 struct instance_file *f = container_of(l, struct instance_file, node);
740 memset(f->md5, 0, sizeof(f->md5));
742 fd = open(l->avl.key, O_RDONLY);
748 len = read(fd, buf, sizeof(buf));
758 md5_hash(buf, len, &md5);
761 md5_end(f->md5, &md5);
766 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
771 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
775 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
777 struct blobmsg_list_node *node;
782 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
785 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
787 blobmsg_list_for_each(l, node)
794 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
796 struct blob_attr *tb[__JAIL_ATTR_MAX];
797 struct jail *jail = &in->jail;
801 r = stat(UJAIL_BIN_PATH, &s);
803 ERROR("unable to find %s: %m (%d)\n", UJAIL_BIN_PATH, r);
807 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
808 blobmsg_data(attr), blobmsg_data_len(attr));
812 if (tb[JAIL_ATTR_NAME]) {
813 jail->name = strdup(blobmsg_get_string(tb[JAIL_ATTR_NAME]));
816 if (tb[JAIL_ATTR_HOSTNAME]) {
817 jail->hostname = strdup(blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]));
820 if (tb[JAIL_ATTR_PROCFS]) {
821 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
824 if (tb[JAIL_ATTR_SYSFS]) {
825 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
828 if (tb[JAIL_ATTR_UBUS]) {
829 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
832 if (tb[JAIL_ATTR_LOG]) {
833 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
836 if (tb[JAIL_ATTR_RONLY]) {
837 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
840 if (tb[JAIL_ATTR_MOUNT]) {
841 struct blob_attr *cur;
844 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
846 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
857 if (in->no_new_privs)
864 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
866 struct blob_attr *cur, *cur2;
870 cur = tb[INSTANCE_ATTR_COMMAND];
876 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
879 blobmsg_for_each_attr(cur2, cur, rem) {
889 instance_config_parse(struct service_instance *in)
891 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
892 struct blob_attr *cur, *cur2;
895 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
896 blobmsg_data(in->config), blobmsg_data_len(in->config));
898 if (!instance_config_parse_command(in, tb))
901 if (tb[INSTANCE_ATTR_TERMTIMEOUT])
902 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
903 if (tb[INSTANCE_ATTR_RESPAWN]) {
905 uint32_t vals[3] = { 3600, 5, 5};
907 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
908 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
910 vals[i] = atoi(blobmsg_get_string(cur2));
914 in->respawn_count = 0;
915 in->respawn_threshold = vals[0];
916 in->respawn_timeout = vals[1];
917 in->respawn_retry = vals[2];
919 if (tb[INSTANCE_ATTR_TRIGGER]) {
920 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
921 trigger_add(in->trigger, in);
924 if (tb[INSTANCE_ATTR_WATCH]) {
925 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
926 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
928 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
929 watch_add(blobmsg_get_string(cur2), in);
933 if ((cur = tb[INSTANCE_ATTR_NICE])) {
934 in->nice = (int8_t) blobmsg_get_u32(cur);
935 if (in->nice < -20 || in->nice > 20)
939 if (tb[INSTANCE_ATTR_USER]) {
940 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
941 struct passwd *p = getpwnam(user);
943 in->user = strdup(user);
945 in->gr_gid = in->pw_gid = p->pw_gid;
949 if (tb[INSTANCE_ATTR_GROUP]) {
950 const char *group = blobmsg_get_string(tb[INSTANCE_ATTR_GROUP]);
951 struct group *p = getgrnam(group);
953 in->group = strdup(group);
954 in->gr_gid = p->gr_gid;
958 if (tb[INSTANCE_ATTR_TRACE])
959 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
961 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
962 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
964 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
965 in->seccomp = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]));
967 if (tb[INSTANCE_ATTR_PIDFILE]) {
968 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
970 in->pidfile = strdup(pidfile);
973 if (tb[INSTANCE_ATTR_RELOADSIG])
974 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
976 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
977 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
979 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
980 in->_stdout.fd.fd = -1;
982 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
983 in->_stderr.fd.fd = -1;
985 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
987 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
990 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
993 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
996 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
999 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
1002 if (tb[INSTANCE_ATTR_FACILITY]) {
1003 int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1004 if (facility != -1) {
1005 in->syslog_facility = facility;
1006 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1008 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1015 instance_config_cleanup(struct service_instance *in)
1017 blobmsg_list_free(&in->env);
1018 blobmsg_list_free(&in->data);
1019 blobmsg_list_free(&in->netdev);
1020 blobmsg_list_free(&in->file);
1021 blobmsg_list_free(&in->limits);
1022 blobmsg_list_free(&in->errors);
1023 blobmsg_list_free(&in->jail.mount);
1027 instance_config_move_strdup(char **dst, char *src)
1041 instance_config_move(struct service_instance *in, struct service_instance *in_src)
1043 instance_config_cleanup(in);
1044 blobmsg_list_move(&in->env, &in_src->env);
1045 blobmsg_list_move(&in->data, &in_src->data);
1046 blobmsg_list_move(&in->netdev, &in_src->netdev);
1047 blobmsg_list_move(&in->file, &in_src->file);
1048 blobmsg_list_move(&in->limits, &in_src->limits);
1049 blobmsg_list_move(&in->errors, &in_src->errors);
1050 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
1051 in->trigger = in_src->trigger;
1052 in->command = in_src->command;
1053 in->respawn = in_src->respawn;
1054 in->respawn_retry = in_src->respawn_retry;
1055 in->respawn_threshold = in_src->respawn_threshold;
1056 in->respawn_timeout = in_src->respawn_timeout;
1057 in->name = in_src->name;
1058 in->trace = in_src->trace;
1059 in->node.avl.key = in_src->node.avl.key;
1060 in->syslog_facility = in_src->syslog_facility;
1062 instance_config_move_strdup(&in->pidfile, in_src->pidfile);
1063 instance_config_move_strdup(&in->seccomp, in_src->seccomp);
1064 instance_config_move_strdup(&in->jail.name, in_src->jail.name);
1065 instance_config_move_strdup(&in->jail.hostname, in_src->jail.hostname);
1068 in->config = in_src->config;
1069 in_src->config = NULL;
1073 instance_update(struct service_instance *in, struct service_instance *in_new)
1075 bool changed = instance_config_changed(in, in_new);
1076 bool running = in->proc.pending;
1077 bool stopping = in->halt;
1079 if (!running || stopping) {
1080 instance_config_move(in, in_new);
1084 instance_restart(in);
1085 instance_config_move(in, in_new);
1086 /* restart happens in the child callback handler */
1091 instance_free(struct service_instance *in)
1093 instance_free_stdio(in);
1094 uloop_process_delete(&in->proc);
1095 uloop_timeout_cancel(&in->timeout);
1098 instance_config_cleanup(in);
1102 free(in->jail.name);
1103 free(in->jail.hostname);
1110 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1112 config = blob_memdup(config);
1114 in->name = blobmsg_name(config);
1115 in->config = config;
1116 in->timeout.cb = instance_timeout;
1117 in->proc.cb = instance_exit;
1118 in->term_timeout = 5;
1119 in->syslog_facility = LOG_DAEMON;
1121 in->_stdout.fd.fd = -2;
1122 in->_stdout.stream.string_data = true;
1123 in->_stdout.stream.notify_read = instance_stdout;
1125 in->_stderr.fd.fd = -2;
1126 in->_stderr.stream.string_data = true;
1127 in->_stderr.stream.notify_read = instance_stderr;
1129 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1130 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1131 blobmsg_list_simple_init(&in->env);
1132 blobmsg_list_simple_init(&in->data);
1133 blobmsg_list_simple_init(&in->limits);
1134 blobmsg_list_simple_init(&in->errors);
1135 blobmsg_list_simple_init(&in->jail.mount);
1136 in->valid = instance_config_parse(in);
1139 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1146 i = blobmsg_open_table(b, in->name);
1147 blobmsg_add_u8(b, "running", in->proc.pending);
1148 if (in->proc.pending)
1149 blobmsg_add_u32(b, "pid", in->proc.pid);
1151 blobmsg_add_blob(b, in->command);
1152 blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1154 if (!avl_is_empty(&in->errors.avl)) {
1155 struct blobmsg_list_node *var;
1156 void *e = blobmsg_open_array(b, "errors");
1157 blobmsg_list_for_each(&in->errors, var)
1158 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1159 blobmsg_close_table(b, e);
1162 if (!avl_is_empty(&in->env.avl)) {
1163 struct blobmsg_list_node *var;
1164 void *e = blobmsg_open_table(b, "env");
1165 blobmsg_list_for_each(&in->env, var)
1166 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1167 blobmsg_close_table(b, e);
1170 if (!avl_is_empty(&in->data.avl)) {
1171 struct blobmsg_list_node *var;
1172 void *e = blobmsg_open_table(b, "data");
1173 blobmsg_list_for_each(&in->data, var)
1174 blobmsg_add_blob(b, var->data);
1175 blobmsg_close_table(b, e);
1178 if (!avl_is_empty(&in->limits.avl)) {
1179 struct blobmsg_list_node *var;
1180 void *e = blobmsg_open_table(b, "limits");
1181 blobmsg_list_for_each(&in->limits, var)
1182 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1183 blobmsg_close_table(b, e);
1186 if (in->reload_signal)
1187 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1190 void *r = blobmsg_open_table(b, "respawn");
1191 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1192 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1193 blobmsg_add_u32(b, "retry", in->respawn_retry);
1194 blobmsg_close_table(b, r);
1198 blobmsg_add_u8(b, "trace", true);
1200 if (in->no_new_privs)
1201 blobmsg_add_u8(b, "no_new_privs", true);
1204 blobmsg_add_string(b, "seccomp", in->seccomp);
1207 blobmsg_add_string(b, "pidfile", in->pidfile);
1210 blobmsg_add_string(b, "user", in->user);
1213 blobmsg_add_string(b, "group", in->group);
1216 void *r = blobmsg_open_table(b, "jail");
1218 blobmsg_add_string(b, "name", in->jail.name);
1219 if (in->jail.hostname)
1220 blobmsg_add_string(b, "hostname", in->jail.hostname);
1221 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1222 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1223 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1224 blobmsg_add_u8(b, "log", in->jail.log);
1225 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1226 blobmsg_close_table(b, r);
1227 if (!avl_is_empty(&in->jail.mount.avl)) {
1228 struct blobmsg_list_node *var;
1229 void *e = blobmsg_open_table(b, "mount");
1230 blobmsg_list_for_each(&in->jail.mount, var)
1231 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1232 blobmsg_close_table(b, e);
1236 if (verbose && in->trigger)
1237 blobmsg_add_blob(b, in->trigger);
1239 blobmsg_close_table(b, i);