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>
29 #include <libubox/md5.h>
38 INSTANCE_ATTR_COMMAND,
43 INSTANCE_ATTR_TRIGGER,
44 INSTANCE_ATTR_RESPAWN,
52 INSTANCE_ATTR_NO_NEW_PRIVS,
55 INSTANCE_ATTR_SECCOMP,
56 INSTANCE_ATTR_PIDFILE,
57 INSTANCE_ATTR_RELOADSIG,
58 INSTANCE_ATTR_TERMTIMEOUT,
62 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
63 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
64 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
65 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
66 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
67 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
68 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
69 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
70 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
71 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
72 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
73 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
74 [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
75 [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
76 [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
77 [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
78 [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
79 [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
80 [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
81 [INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
82 [INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
83 [INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
98 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
99 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
100 [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
101 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
102 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
103 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
104 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
105 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
106 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
109 struct instance_netdev {
110 struct blobmsg_list_node node;
114 struct instance_file {
115 struct blobmsg_list_node node;
124 static const struct rlimit_name rlimit_names[] = {
126 { "core", RLIMIT_CORE },
127 { "cpu", RLIMIT_CPU },
128 { "data", RLIMIT_DATA },
129 { "fsize", RLIMIT_FSIZE },
130 { "memlock", RLIMIT_MEMLOCK },
131 { "nofile", RLIMIT_NOFILE },
132 { "nproc", RLIMIT_NPROC },
133 { "rss", RLIMIT_RSS },
134 { "stack", RLIMIT_STACK },
136 { "nice", RLIMIT_NICE },
137 { "rtprio", RLIMIT_RTPRIO },
138 { "msgqueue", RLIMIT_MSGQUEUE },
139 { "sigpending", RLIMIT_SIGPENDING },
144 static void closefd(int fd)
146 if (fd > STDERR_FILENO)
151 instance_limits(const char *limit, const char *value)
155 unsigned long cur, max;
157 for (i = 0; rlimit_names[i].name != NULL; i++) {
158 if (strcmp(rlimit_names[i].name, limit))
160 if (!strcmp(value, "unlimited")) {
161 rlim.rlim_cur = RLIM_INFINITY;
162 rlim.rlim_max = RLIM_INFINITY;
164 if (getrlimit(rlimit_names[i].resource, &rlim))
170 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
177 setrlimit(rlimit_names[i].resource, &rlim);
183 jail_run(struct service_instance *in, char **argv)
185 struct blobmsg_list_node *var;
186 struct jail *jail = &in->jail;
189 argv[argc++] = "/sbin/ujail";
193 argv[argc++] = jail->name;
196 if (jail->hostname) {
198 argv[argc++] = jail->hostname;
203 argv[argc++] = in->seccomp;
206 if (in->no_new_privs)
224 blobmsg_list_for_each(&jail->mount, var) {
225 const char *type = blobmsg_data(var->data);
231 argv[argc++] = (char *) blobmsg_name(var->data);
240 instance_removepid(struct service_instance *in) {
243 if (unlink(in->pidfile)) {
244 ERROR("Failed to removed pidfile: %s: %m\n", in->pidfile);
251 instance_writepid(struct service_instance *in)
258 _pidfile = fopen(in->pidfile, "w");
259 if (_pidfile == NULL) {
260 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
263 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
264 ERROR("failed to write pidfile: %s: %m", in->pidfile);
268 if (fclose(_pidfile)) {
269 ERROR("failed to close pidfile: %s: %m", in->pidfile);
277 instance_run(struct service_instance *in, int _stdout, int _stderr)
279 struct blobmsg_list_node *var;
280 struct blob_attr *cur;
282 int argc = 1; /* NULL terminated */
284 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
285 bool setlbf = _stdout >= 0;
288 setpriority(PRIO_PROCESS, 0, in->nice);
290 blobmsg_for_each_attr(cur, in->command, rem)
293 blobmsg_list_for_each(&in->env, var)
294 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
297 setenv("SECCOMP_FILE", in->seccomp, 1);
300 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
302 blobmsg_list_for_each(&in->limits, var)
303 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
305 if (in->trace || seccomp)
308 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
311 #ifdef SECCOMP_SUPPORT
313 argv[argc++] = "/sbin/utrace";
315 argv[argc++] = "/sbin/seccomp-trace";
317 if (in->trace || seccomp)
318 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
322 argc = jail_run(in, argv);
324 blobmsg_for_each_attr(cur, in->command, rem)
325 argv[argc++] = blobmsg_data(cur);
329 _stdin = open("/dev/null", O_RDONLY);
332 _stdout = open("/dev/null", O_WRONLY);
335 _stderr = open("/dev/null", O_WRONLY);
338 dup2(_stdin, STDIN_FILENO);
342 dup2(_stdout, STDOUT_FILENO);
346 dup2(_stderr, STDERR_FILENO);
350 if (in->gid && setgid(in->gid)) {
351 ERROR("failed to set group id %d: %m\n", in->gid);
354 if (in->uid && setuid(in->uid)) {
355 ERROR("failed to set user id %d: %m\n", in->uid);
359 execvp(argv[0], argv);
364 instance_free_stdio(struct service_instance *in)
366 if (in->_stdout.fd.fd > -1) {
367 ustream_free(&in->_stdout.stream);
368 close(in->_stdout.fd.fd);
369 in->_stdout.fd.fd = -1;
372 if (in->_stderr.fd.fd > -1) {
373 ustream_free(&in->_stderr.stream);
374 close(in->_stderr.fd.fd);
375 in->_stderr.fd.fd = -1;
380 instance_start(struct service_instance *in)
383 int opipe[2] = { -1, -1 };
384 int epipe[2] = { -1, -1 };
386 if (!avl_is_empty(&in->errors.avl)) {
387 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
392 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
396 if (in->proc.pending) {
402 instance_free_stdio(in);
403 if (in->_stdout.fd.fd > -2) {
405 ULOG_WARN("pipe() failed: %m\n");
406 opipe[0] = opipe[1] = -1;
410 if (in->_stderr.fd.fd > -2) {
412 ULOG_WARN("pipe() failed: %m\n");
413 epipe[0] = epipe[1] = -1;
431 instance_run(in, opipe[1], epipe[1]);
435 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
437 instance_writepid(in);
438 clock_gettime(CLOCK_MONOTONIC, &in->start);
439 uloop_process_add(&in->proc);
442 ustream_fd_init(&in->_stdout, opipe[0]);
444 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
448 ustream_fd_init(&in->_stderr, epipe[0]);
450 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
453 service_event("instance.start", in->srv->name, in->name);
457 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
459 char *newline, *str, *arg0, ident[32];
462 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
463 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
464 ulog_open(ULOG_SYSLOG, LOG_DAEMON, ident);
467 str = ustream_get_read_buf(s, NULL);
471 newline = strchr(str, '\n');
476 ulog(prio, "%s\n", str);
478 len = newline + 1 - str;
479 ustream_consume(s, len);
482 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
486 instance_stdout(struct ustream *s, int bytes)
488 instance_stdio(s, LOG_INFO,
489 container_of(s, struct service_instance, _stdout.stream));
493 instance_stderr(struct ustream *s, int bytes)
495 instance_stdio(s, LOG_ERR,
496 container_of(s, struct service_instance, _stderr.stream));
500 instance_timeout(struct uloop_timeout *t)
502 struct service_instance *in;
504 in = container_of(t, struct service_instance, timeout);
507 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
508 in->srv->name, in->name, in->proc.pid);
509 kill(in->proc.pid, SIGKILL);
510 } else if (in->restart || in->respawn)
515 instance_exit(struct uloop_process *p, int ret)
517 struct service_instance *in;
521 in = container_of(p, struct service_instance, proc);
523 clock_gettime(CLOCK_MONOTONIC, &tp);
524 runtime = tp.tv_sec - in->start.tv_sec;
526 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
528 uloop_timeout_cancel(&in->timeout);
529 service_event("instance.stop", in->srv->name, in->name);
532 instance_removepid(in);
536 struct service *s = in->srv;
538 avl_delete(&s->instances.avl, &in->node.avl);
542 } else if (in->restart) {
544 } else if (in->respawn) {
545 if (runtime < in->respawn_threshold)
548 in->respawn_count = 0;
549 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
550 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
551 in->srv->name, in->name, in->respawn_count, runtime);
552 in->restart = in->respawn = 0;
554 service_event("instance.fail", in->srv->name, in->name);
556 service_event("instance.respawn", in->srv->name, in->name);
557 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
563 instance_stop(struct service_instance *in, bool halt)
565 if (!in->proc.pending)
568 in->restart = in->respawn = false;
569 kill(in->proc.pid, SIGTERM);
570 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
574 instance_restart(struct service_instance *in)
576 if (!in->proc.pending)
579 if (in->reload_signal) {
580 kill(in->proc.pid, in->reload_signal);
586 kill(in->proc.pid, SIGTERM);
587 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
590 static bool string_changed(const char *a, const char *b)
592 return !((!a && !b) || (a && b && !strcmp(a, b)));
596 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
601 if (!blob_attr_equal(in->command, in_new->command))
604 if (!blobmsg_list_equal(&in->env, &in_new->env))
607 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
610 if (!blobmsg_list_equal(&in->file, &in_new->file))
613 if (in->nice != in_new->nice)
616 if (in->uid != in_new->uid)
619 if (in->gid != in_new->gid)
622 if (string_changed(in->pidfile, in_new->pidfile))
625 if (in->respawn_retry != in_new->respawn_retry)
627 if (in->respawn_threshold != in_new->respawn_threshold)
629 if (in->respawn_timeout != in_new->respawn_timeout)
632 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
635 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
638 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
645 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
647 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
648 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
650 return n1->ifindex == n2->ifindex;
654 instance_netdev_update(struct blobmsg_list_node *l)
656 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
658 n->ifindex = if_nametoindex(n->node.avl.key);
662 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
664 struct instance_file *f1 = container_of(l1, struct instance_file, node);
665 struct instance_file *f2 = container_of(l2, struct instance_file, node);
667 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
671 instance_file_update(struct blobmsg_list_node *l)
673 struct instance_file *f = container_of(l, struct instance_file, node);
678 memset(f->md5, 0, sizeof(f->md5));
680 fd = open(l->avl.key, O_RDONLY);
686 len = read(fd, buf, sizeof(buf));
696 md5_hash(buf, len, &md5);
699 md5_end(f->md5, &md5);
704 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
709 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
713 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
715 struct blobmsg_list_node *node;
720 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
723 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
725 blobmsg_list_for_each(l, node)
732 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
734 struct blob_attr *tb[__JAIL_ATTR_MAX];
735 struct jail *jail = &in->jail;
738 if (stat("/sbin/ujail", &s))
741 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
742 blobmsg_data(attr), blobmsg_data_len(attr));
746 if (tb[JAIL_ATTR_NAME]) {
747 jail->name = blobmsg_get_string(tb[JAIL_ATTR_NAME]);
750 if (tb[JAIL_ATTR_HOSTNAME]) {
751 jail->hostname = blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]);
754 if (tb[JAIL_ATTR_PROCFS]) {
755 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
758 if (tb[JAIL_ATTR_SYSFS]) {
759 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
762 if (tb[JAIL_ATTR_UBUS]) {
763 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
766 if (tb[JAIL_ATTR_LOG]) {
767 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
770 if (tb[JAIL_ATTR_RONLY]) {
771 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
774 if (tb[JAIL_ATTR_MOUNT]) {
775 struct blob_attr *cur;
778 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
780 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
789 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
791 struct blob_attr *cur, *cur2;
795 cur = tb[INSTANCE_ATTR_COMMAND];
801 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
804 blobmsg_for_each_attr(cur2, cur, rem) {
814 instance_config_parse(struct service_instance *in)
816 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
817 struct blob_attr *cur, *cur2;
820 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
821 blobmsg_data(in->config), blobmsg_data_len(in->config));
823 if (!instance_config_parse_command(in, tb))
826 if (tb[INSTANCE_ATTR_TERMTIMEOUT])
827 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
828 if (tb[INSTANCE_ATTR_RESPAWN]) {
830 uint32_t vals[3] = { 3600, 5, 5};
832 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
833 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
835 vals[i] = atoi(blobmsg_get_string(cur2));
839 in->respawn_count = 0;
840 in->respawn_threshold = vals[0];
841 in->respawn_timeout = vals[1];
842 in->respawn_retry = vals[2];
844 if (tb[INSTANCE_ATTR_TRIGGER]) {
845 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
846 trigger_add(in->trigger, in);
849 if (tb[INSTANCE_ATTR_WATCH]) {
850 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
851 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
853 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
854 watch_add(blobmsg_get_string(cur2), in);
858 if ((cur = tb[INSTANCE_ATTR_NICE])) {
859 in->nice = (int8_t) blobmsg_get_u32(cur);
860 if (in->nice < -20 || in->nice > 20)
864 if (tb[INSTANCE_ATTR_USER]) {
865 struct passwd *p = getpwnam(blobmsg_get_string(tb[INSTANCE_ATTR_USER]));
872 if (tb[INSTANCE_ATTR_TRACE])
873 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
875 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
876 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
878 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
879 in->seccomp = blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]);
881 if (tb[INSTANCE_ATTR_PIDFILE]) {
882 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
884 in->pidfile = pidfile;
887 if (tb[INSTANCE_ATTR_RELOADSIG])
888 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
890 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
891 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
893 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
894 in->_stdout.fd.fd = -1;
896 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
897 in->_stderr.fd.fd = -1;
899 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
901 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
904 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
907 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
910 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
913 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
920 instance_config_cleanup(struct service_instance *in)
922 blobmsg_list_free(&in->env);
923 blobmsg_list_free(&in->data);
924 blobmsg_list_free(&in->netdev);
925 blobmsg_list_free(&in->file);
926 blobmsg_list_free(&in->limits);
927 blobmsg_list_free(&in->errors);
928 blobmsg_list_free(&in->jail.mount);
932 instance_config_move(struct service_instance *in, struct service_instance *in_src)
934 instance_config_cleanup(in);
935 blobmsg_list_move(&in->env, &in_src->env);
936 blobmsg_list_move(&in->data, &in_src->data);
937 blobmsg_list_move(&in->netdev, &in_src->netdev);
938 blobmsg_list_move(&in->file, &in_src->file);
939 blobmsg_list_move(&in->limits, &in_src->limits);
940 blobmsg_list_move(&in->errors, &in_src->errors);
941 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
942 in->trigger = in_src->trigger;
943 in->command = in_src->command;
944 in->pidfile = in_src->pidfile;
945 in->respawn_retry = in_src->respawn_retry;
946 in->respawn_threshold = in_src->respawn_threshold;
947 in->respawn_timeout = in_src->respawn_timeout;
948 in->name = in_src->name;
949 in->trace = in_src->trace;
950 in->node.avl.key = in_src->node.avl.key;
953 in->config = in_src->config;
954 in_src->config = NULL;
958 instance_update(struct service_instance *in, struct service_instance *in_new)
960 bool changed = instance_config_changed(in, in_new);
961 bool running = in->proc.pending;
962 bool stopping = in->halt;
964 if (!running || stopping) {
965 instance_config_move(in, in_new);
969 instance_restart(in);
970 instance_config_move(in, in_new);
971 /* restart happens in the child callback handler */
976 instance_free(struct service_instance *in)
978 instance_free_stdio(in);
979 uloop_process_delete(&in->proc);
980 uloop_timeout_cancel(&in->timeout);
983 instance_config_cleanup(in);
989 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
991 config = blob_memdup(config);
993 in->name = blobmsg_name(config);
995 in->timeout.cb = instance_timeout;
996 in->proc.cb = instance_exit;
997 in->term_timeout = 5;
999 in->_stdout.fd.fd = -2;
1000 in->_stdout.stream.string_data = true;
1001 in->_stdout.stream.notify_read = instance_stdout;
1003 in->_stderr.fd.fd = -2;
1004 in->_stderr.stream.string_data = true;
1005 in->_stderr.stream.notify_read = instance_stderr;
1007 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1008 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1009 blobmsg_list_simple_init(&in->env);
1010 blobmsg_list_simple_init(&in->data);
1011 blobmsg_list_simple_init(&in->limits);
1012 blobmsg_list_simple_init(&in->errors);
1013 blobmsg_list_simple_init(&in->jail.mount);
1014 in->valid = instance_config_parse(in);
1017 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1024 i = blobmsg_open_table(b, in->name);
1025 blobmsg_add_u8(b, "running", in->proc.pending);
1026 if (in->proc.pending)
1027 blobmsg_add_u32(b, "pid", in->proc.pid);
1029 blobmsg_add_blob(b, in->command);
1030 blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1032 if (!avl_is_empty(&in->errors.avl)) {
1033 struct blobmsg_list_node *var;
1034 void *e = blobmsg_open_array(b, "errors");
1035 blobmsg_list_for_each(&in->errors, var)
1036 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1037 blobmsg_close_table(b, e);
1040 if (!avl_is_empty(&in->env.avl)) {
1041 struct blobmsg_list_node *var;
1042 void *e = blobmsg_open_table(b, "env");
1043 blobmsg_list_for_each(&in->env, var)
1044 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1045 blobmsg_close_table(b, e);
1048 if (!avl_is_empty(&in->data.avl)) {
1049 struct blobmsg_list_node *var;
1050 void *e = blobmsg_open_table(b, "data");
1051 blobmsg_list_for_each(&in->data, var)
1052 blobmsg_add_blob(b, var->data);
1053 blobmsg_close_table(b, e);
1056 if (!avl_is_empty(&in->limits.avl)) {
1057 struct blobmsg_list_node *var;
1058 void *e = blobmsg_open_table(b, "limits");
1059 blobmsg_list_for_each(&in->limits, var)
1060 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1061 blobmsg_close_table(b, e);
1064 if (in->reload_signal)
1065 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1068 void *r = blobmsg_open_table(b, "respawn");
1069 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1070 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1071 blobmsg_add_u32(b, "retry", in->respawn_retry);
1072 blobmsg_close_table(b, r);
1076 blobmsg_add_u8(b, "trace", true);
1078 if (in->no_new_privs)
1079 blobmsg_add_u8(b, "no_new_privs", true);
1082 blobmsg_add_string(b, "seccomp", in->seccomp);
1085 blobmsg_add_string(b, "pidfile", in->pidfile);
1088 void *r = blobmsg_open_table(b, "jail");
1090 blobmsg_add_string(b, "name", in->jail.name);
1091 if (in->jail.hostname)
1092 blobmsg_add_string(b, "hostname", in->jail.hostname);
1093 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1094 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1095 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1096 blobmsg_add_u8(b, "log", in->jail.log);
1097 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1098 blobmsg_close_table(b, r);
1099 if (!avl_is_empty(&in->jail.mount.avl)) {
1100 struct blobmsg_list_node *var;
1101 void *e = blobmsg_open_table(b, "mount");
1102 blobmsg_list_for_each(&in->jail.mount, var)
1103 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1104 blobmsg_close_table(b, e);
1108 if (verbose && in->trigger)
1109 blobmsg_add_blob(b, in->trigger);
1111 blobmsg_close_table(b, i);