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>
30 #include <libubox/md5.h>
39 INSTANCE_ATTR_COMMAND,
44 INSTANCE_ATTR_TRIGGER,
45 INSTANCE_ATTR_RESPAWN,
53 INSTANCE_ATTR_NO_NEW_PRIVS,
56 INSTANCE_ATTR_SECCOMP,
57 INSTANCE_ATTR_PIDFILE,
58 INSTANCE_ATTR_RELOADSIG,
59 INSTANCE_ATTR_TERMTIMEOUT,
63 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
64 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
65 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
66 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
67 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
68 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
69 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
70 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
71 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
72 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
73 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
74 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
75 [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
76 [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
77 [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
78 [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
79 [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
80 [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
81 [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
82 [INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
83 [INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
84 [INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
99 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
100 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
101 [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
102 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
103 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
104 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
105 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
106 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
107 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
110 struct instance_netdev {
111 struct blobmsg_list_node node;
115 struct instance_file {
116 struct blobmsg_list_node node;
125 static const struct rlimit_name rlimit_names[] = {
127 { "core", RLIMIT_CORE },
128 { "cpu", RLIMIT_CPU },
129 { "data", RLIMIT_DATA },
130 { "fsize", RLIMIT_FSIZE },
131 { "memlock", RLIMIT_MEMLOCK },
132 { "nofile", RLIMIT_NOFILE },
133 { "nproc", RLIMIT_NPROC },
134 { "rss", RLIMIT_RSS },
135 { "stack", RLIMIT_STACK },
137 { "nice", RLIMIT_NICE },
138 { "rtprio", RLIMIT_RTPRIO },
139 { "msgqueue", RLIMIT_MSGQUEUE },
140 { "sigpending", RLIMIT_SIGPENDING },
145 static void closefd(int fd)
147 if (fd > STDERR_FILENO)
152 instance_limits(const char *limit, const char *value)
156 unsigned long cur, max;
158 for (i = 0; rlimit_names[i].name != NULL; i++) {
159 if (strcmp(rlimit_names[i].name, limit))
161 if (!strcmp(value, "unlimited")) {
162 rlim.rlim_cur = RLIM_INFINITY;
163 rlim.rlim_max = RLIM_INFINITY;
165 if (getrlimit(rlimit_names[i].resource, &rlim))
171 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
178 setrlimit(rlimit_names[i].resource, &rlim);
184 jail_run(struct service_instance *in, char **argv)
186 struct blobmsg_list_node *var;
187 struct jail *jail = &in->jail;
190 argv[argc++] = "/sbin/ujail";
194 argv[argc++] = jail->name;
197 if (jail->hostname) {
199 argv[argc++] = jail->hostname;
204 argv[argc++] = in->seccomp;
207 if (in->no_new_privs)
225 blobmsg_list_for_each(&jail->mount, var) {
226 const char *type = blobmsg_data(var->data);
232 argv[argc++] = (char *) blobmsg_name(var->data);
241 instance_removepid(struct service_instance *in) {
244 if (unlink(in->pidfile)) {
245 ERROR("Failed to removed pidfile: %s: %m\n", in->pidfile);
252 instance_writepid(struct service_instance *in)
259 _pidfile = fopen(in->pidfile, "w");
260 if (_pidfile == NULL) {
261 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
264 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
265 ERROR("failed to write pidfile: %s: %m", in->pidfile);
269 if (fclose(_pidfile)) {
270 ERROR("failed to close pidfile: %s: %m", in->pidfile);
278 instance_run(struct service_instance *in, int _stdout, int _stderr)
280 struct blobmsg_list_node *var;
281 struct blob_attr *cur;
283 int argc = 1; /* NULL terminated */
285 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
286 bool setlbf = _stdout >= 0;
289 setpriority(PRIO_PROCESS, 0, in->nice);
291 blobmsg_for_each_attr(cur, in->command, rem)
294 blobmsg_list_for_each(&in->env, var)
295 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
298 setenv("SECCOMP_FILE", in->seccomp, 1);
301 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
303 blobmsg_list_for_each(&in->limits, var)
304 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
306 if (in->trace || seccomp)
309 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
312 #ifdef SECCOMP_SUPPORT
314 argv[argc++] = "/sbin/utrace";
316 argv[argc++] = "/sbin/seccomp-trace";
318 if (in->trace || seccomp)
319 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
323 argc = jail_run(in, argv);
325 blobmsg_for_each_attr(cur, in->command, rem)
326 argv[argc++] = blobmsg_data(cur);
330 _stdin = open("/dev/null", O_RDONLY);
333 _stdout = open("/dev/null", O_WRONLY);
336 _stderr = open("/dev/null", O_WRONLY);
339 dup2(_stdin, STDIN_FILENO);
343 dup2(_stdout, STDOUT_FILENO);
347 dup2(_stderr, STDERR_FILENO);
351 if (in->user && in->gid && initgroups(in->user, in->gid)) {
352 ERROR("failed to initgroups() for user %s: %m\n", in->user);
355 if (in->gid && setgid(in->gid)) {
356 ERROR("failed to set group id %d: %m\n", in->gid);
359 if (in->uid && setuid(in->uid)) {
360 ERROR("failed to set user id %d: %m\n", in->uid);
364 execvp(argv[0], argv);
369 instance_free_stdio(struct service_instance *in)
371 if (in->_stdout.fd.fd > -1) {
372 ustream_free(&in->_stdout.stream);
373 close(in->_stdout.fd.fd);
374 in->_stdout.fd.fd = -1;
377 if (in->_stderr.fd.fd > -1) {
378 ustream_free(&in->_stderr.stream);
379 close(in->_stderr.fd.fd);
380 in->_stderr.fd.fd = -1;
385 instance_start(struct service_instance *in)
388 int opipe[2] = { -1, -1 };
389 int epipe[2] = { -1, -1 };
391 if (!avl_is_empty(&in->errors.avl)) {
392 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
397 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
401 if (in->proc.pending) {
407 instance_free_stdio(in);
408 if (in->_stdout.fd.fd > -2) {
410 ULOG_WARN("pipe() failed: %m\n");
411 opipe[0] = opipe[1] = -1;
415 if (in->_stderr.fd.fd > -2) {
417 ULOG_WARN("pipe() failed: %m\n");
418 epipe[0] = epipe[1] = -1;
436 instance_run(in, opipe[1], epipe[1]);
440 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
442 instance_writepid(in);
443 clock_gettime(CLOCK_MONOTONIC, &in->start);
444 uloop_process_add(&in->proc);
447 ustream_fd_init(&in->_stdout, opipe[0]);
449 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
453 ustream_fd_init(&in->_stderr, epipe[0]);
455 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
458 service_event("instance.start", in->srv->name, in->name);
462 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
464 char *newline, *str, *arg0, ident[32];
467 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
468 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
469 ulog_open(ULOG_SYSLOG, LOG_DAEMON, ident);
472 str = ustream_get_read_buf(s, &len);
476 newline = memchr(str, '\n', len);
477 if (!newline && (s->r.buffer_len != len))
482 len = newline + 1 - str;
484 ulog(prio, "%s\n", str);
486 ustream_consume(s, len);
489 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
493 instance_stdout(struct ustream *s, int bytes)
495 instance_stdio(s, LOG_INFO,
496 container_of(s, struct service_instance, _stdout.stream));
500 instance_stderr(struct ustream *s, int bytes)
502 instance_stdio(s, LOG_ERR,
503 container_of(s, struct service_instance, _stderr.stream));
507 instance_timeout(struct uloop_timeout *t)
509 struct service_instance *in;
511 in = container_of(t, struct service_instance, timeout);
514 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
515 in->srv->name, in->name, in->proc.pid);
516 kill(in->proc.pid, SIGKILL);
517 } else if (in->restart || in->respawn)
522 instance_delete(struct service_instance *in)
524 struct service *s = in->srv;
526 avl_delete(&s->instances.avl, &in->node.avl);
532 instance_exit(struct uloop_process *p, int ret)
534 struct service_instance *in;
538 in = container_of(p, struct service_instance, proc);
540 clock_gettime(CLOCK_MONOTONIC, &tp);
541 runtime = tp.tv_sec - in->start.tv_sec;
543 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
545 uloop_timeout_cancel(&in->timeout);
546 service_event("instance.stop", in->srv->name, in->name);
549 instance_removepid(in);
554 } else if (in->restart) {
556 } else if (in->respawn) {
557 if (runtime < in->respawn_threshold)
560 in->respawn_count = 0;
561 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
562 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
563 in->srv->name, in->name, in->respawn_count, runtime);
564 in->restart = in->respawn = 0;
566 service_event("instance.fail", in->srv->name, in->name);
568 service_event("instance.respawn", in->srv->name, in->name);
569 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
575 instance_stop(struct service_instance *in, bool halt)
577 if (!in->proc.pending) {
583 in->restart = in->respawn = false;
584 kill(in->proc.pid, SIGTERM);
585 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
589 instance_restart(struct service_instance *in)
591 if (!in->proc.pending)
594 if (in->reload_signal) {
595 kill(in->proc.pid, in->reload_signal);
601 kill(in->proc.pid, SIGTERM);
602 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
605 static bool string_changed(const char *a, const char *b)
607 return !((!a && !b) || (a && b && !strcmp(a, b)));
611 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
616 if (!blob_attr_equal(in->command, in_new->command))
619 if (!blobmsg_list_equal(&in->env, &in_new->env))
622 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
625 if (!blobmsg_list_equal(&in->file, &in_new->file))
628 if (in->nice != in_new->nice)
631 if (string_changed(in->user, in_new->user))
634 if (in->uid != in_new->uid)
637 if (in->gid != in_new->gid)
640 if (string_changed(in->pidfile, in_new->pidfile))
643 if (in->respawn_retry != in_new->respawn_retry)
645 if (in->respawn_threshold != in_new->respawn_threshold)
647 if (in->respawn_timeout != in_new->respawn_timeout)
650 if ((!in->seccomp && in_new->seccomp) ||
651 (in->seccomp && !in_new->seccomp) ||
652 (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
655 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
658 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
661 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
668 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
670 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
671 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
673 return n1->ifindex == n2->ifindex;
677 instance_netdev_update(struct blobmsg_list_node *l)
679 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
681 n->ifindex = if_nametoindex(n->node.avl.key);
685 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
687 struct instance_file *f1 = container_of(l1, struct instance_file, node);
688 struct instance_file *f2 = container_of(l2, struct instance_file, node);
690 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
694 instance_file_update(struct blobmsg_list_node *l)
696 struct instance_file *f = container_of(l, struct instance_file, node);
701 memset(f->md5, 0, sizeof(f->md5));
703 fd = open(l->avl.key, O_RDONLY);
709 len = read(fd, buf, sizeof(buf));
719 md5_hash(buf, len, &md5);
722 md5_end(f->md5, &md5);
727 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
732 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
736 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
738 struct blobmsg_list_node *node;
743 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
746 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
748 blobmsg_list_for_each(l, node)
755 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
757 struct blob_attr *tb[__JAIL_ATTR_MAX];
758 struct jail *jail = &in->jail;
761 if (stat("/sbin/ujail", &s))
764 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
765 blobmsg_data(attr), blobmsg_data_len(attr));
769 if (tb[JAIL_ATTR_NAME]) {
770 jail->name = blobmsg_get_string(tb[JAIL_ATTR_NAME]);
773 if (tb[JAIL_ATTR_HOSTNAME]) {
774 jail->hostname = blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]);
777 if (tb[JAIL_ATTR_PROCFS]) {
778 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
781 if (tb[JAIL_ATTR_SYSFS]) {
782 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
785 if (tb[JAIL_ATTR_UBUS]) {
786 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
789 if (tb[JAIL_ATTR_LOG]) {
790 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
793 if (tb[JAIL_ATTR_RONLY]) {
794 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
797 if (tb[JAIL_ATTR_MOUNT]) {
798 struct blob_attr *cur;
801 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
803 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
812 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
814 struct blob_attr *cur, *cur2;
818 cur = tb[INSTANCE_ATTR_COMMAND];
824 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
827 blobmsg_for_each_attr(cur2, cur, rem) {
837 instance_config_parse(struct service_instance *in)
839 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
840 struct blob_attr *cur, *cur2;
843 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
844 blobmsg_data(in->config), blobmsg_data_len(in->config));
846 if (!instance_config_parse_command(in, tb))
849 if (tb[INSTANCE_ATTR_TERMTIMEOUT])
850 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
851 if (tb[INSTANCE_ATTR_RESPAWN]) {
853 uint32_t vals[3] = { 3600, 5, 5};
855 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
856 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
858 vals[i] = atoi(blobmsg_get_string(cur2));
862 in->respawn_count = 0;
863 in->respawn_threshold = vals[0];
864 in->respawn_timeout = vals[1];
865 in->respawn_retry = vals[2];
867 if (tb[INSTANCE_ATTR_TRIGGER]) {
868 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
869 trigger_add(in->trigger, in);
872 if (tb[INSTANCE_ATTR_WATCH]) {
873 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
874 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
876 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
877 watch_add(blobmsg_get_string(cur2), in);
881 if ((cur = tb[INSTANCE_ATTR_NICE])) {
882 in->nice = (int8_t) blobmsg_get_u32(cur);
883 if (in->nice < -20 || in->nice > 20)
887 if (tb[INSTANCE_ATTR_USER]) {
888 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
889 struct passwd *p = getpwnam(user);
891 in->user = strdup(user);
897 if (tb[INSTANCE_ATTR_TRACE])
898 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
900 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
901 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
903 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
904 in->seccomp = blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]);
906 if (tb[INSTANCE_ATTR_PIDFILE]) {
907 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
909 in->pidfile = pidfile;
912 if (tb[INSTANCE_ATTR_RELOADSIG])
913 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
915 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
916 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
918 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
919 in->_stdout.fd.fd = -1;
921 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
922 in->_stderr.fd.fd = -1;
924 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
926 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
929 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
932 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
935 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
938 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
945 instance_config_cleanup(struct service_instance *in)
947 blobmsg_list_free(&in->env);
948 blobmsg_list_free(&in->data);
949 blobmsg_list_free(&in->netdev);
950 blobmsg_list_free(&in->file);
951 blobmsg_list_free(&in->limits);
952 blobmsg_list_free(&in->errors);
953 blobmsg_list_free(&in->jail.mount);
957 instance_config_move(struct service_instance *in, struct service_instance *in_src)
959 instance_config_cleanup(in);
960 blobmsg_list_move(&in->env, &in_src->env);
961 blobmsg_list_move(&in->data, &in_src->data);
962 blobmsg_list_move(&in->netdev, &in_src->netdev);
963 blobmsg_list_move(&in->file, &in_src->file);
964 blobmsg_list_move(&in->limits, &in_src->limits);
965 blobmsg_list_move(&in->errors, &in_src->errors);
966 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
967 in->trigger = in_src->trigger;
968 in->command = in_src->command;
969 in->pidfile = in_src->pidfile;
970 in->respawn_retry = in_src->respawn_retry;
971 in->respawn_threshold = in_src->respawn_threshold;
972 in->respawn_timeout = in_src->respawn_timeout;
973 in->name = in_src->name;
974 in->trace = in_src->trace;
975 in->seccomp = in_src->seccomp;
976 in->node.avl.key = in_src->node.avl.key;
979 in->config = in_src->config;
980 in_src->config = NULL;
984 instance_update(struct service_instance *in, struct service_instance *in_new)
986 bool changed = instance_config_changed(in, in_new);
987 bool running = in->proc.pending;
988 bool stopping = in->halt;
990 if (!running || stopping) {
991 instance_config_move(in, in_new);
995 instance_restart(in);
996 instance_config_move(in, in_new);
997 /* restart happens in the child callback handler */
1002 instance_free(struct service_instance *in)
1004 instance_free_stdio(in);
1005 uloop_process_delete(&in->proc);
1006 uloop_timeout_cancel(&in->timeout);
1009 instance_config_cleanup(in);
1016 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1018 config = blob_memdup(config);
1020 in->name = blobmsg_name(config);
1021 in->config = config;
1022 in->timeout.cb = instance_timeout;
1023 in->proc.cb = instance_exit;
1024 in->term_timeout = 5;
1026 in->_stdout.fd.fd = -2;
1027 in->_stdout.stream.string_data = true;
1028 in->_stdout.stream.notify_read = instance_stdout;
1030 in->_stderr.fd.fd = -2;
1031 in->_stderr.stream.string_data = true;
1032 in->_stderr.stream.notify_read = instance_stderr;
1034 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1035 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1036 blobmsg_list_simple_init(&in->env);
1037 blobmsg_list_simple_init(&in->data);
1038 blobmsg_list_simple_init(&in->limits);
1039 blobmsg_list_simple_init(&in->errors);
1040 blobmsg_list_simple_init(&in->jail.mount);
1041 in->valid = instance_config_parse(in);
1044 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1051 i = blobmsg_open_table(b, in->name);
1052 blobmsg_add_u8(b, "running", in->proc.pending);
1053 if (in->proc.pending)
1054 blobmsg_add_u32(b, "pid", in->proc.pid);
1056 blobmsg_add_blob(b, in->command);
1057 blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1059 if (!avl_is_empty(&in->errors.avl)) {
1060 struct blobmsg_list_node *var;
1061 void *e = blobmsg_open_array(b, "errors");
1062 blobmsg_list_for_each(&in->errors, var)
1063 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1064 blobmsg_close_table(b, e);
1067 if (!avl_is_empty(&in->env.avl)) {
1068 struct blobmsg_list_node *var;
1069 void *e = blobmsg_open_table(b, "env");
1070 blobmsg_list_for_each(&in->env, var)
1071 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1072 blobmsg_close_table(b, e);
1075 if (!avl_is_empty(&in->data.avl)) {
1076 struct blobmsg_list_node *var;
1077 void *e = blobmsg_open_table(b, "data");
1078 blobmsg_list_for_each(&in->data, var)
1079 blobmsg_add_blob(b, var->data);
1080 blobmsg_close_table(b, e);
1083 if (!avl_is_empty(&in->limits.avl)) {
1084 struct blobmsg_list_node *var;
1085 void *e = blobmsg_open_table(b, "limits");
1086 blobmsg_list_for_each(&in->limits, var)
1087 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1088 blobmsg_close_table(b, e);
1091 if (in->reload_signal)
1092 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1095 void *r = blobmsg_open_table(b, "respawn");
1096 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1097 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1098 blobmsg_add_u32(b, "retry", in->respawn_retry);
1099 blobmsg_close_table(b, r);
1103 blobmsg_add_u8(b, "trace", true);
1105 if (in->no_new_privs)
1106 blobmsg_add_u8(b, "no_new_privs", true);
1109 blobmsg_add_string(b, "seccomp", in->seccomp);
1112 blobmsg_add_string(b, "pidfile", in->pidfile);
1115 void *r = blobmsg_open_table(b, "jail");
1117 blobmsg_add_string(b, "name", in->jail.name);
1118 if (in->jail.hostname)
1119 blobmsg_add_string(b, "hostname", in->jail.hostname);
1120 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1121 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1122 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1123 blobmsg_add_u8(b, "log", in->jail.log);
1124 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1125 blobmsg_close_table(b, r);
1126 if (!avl_is_empty(&in->jail.mount.avl)) {
1127 struct blobmsg_list_node *var;
1128 void *e = blobmsg_open_table(b, "mount");
1129 blobmsg_list_for_each(&in->jail.mount, var)
1130 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1131 blobmsg_close_table(b, e);
1135 if (verbose && in->trigger)
1136 blobmsg_add_blob(b, in->trigger);
1138 blobmsg_close_table(b, i);