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,
60 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
61 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
62 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
63 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
64 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
65 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
66 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
67 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
68 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
69 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
70 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
71 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
72 [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
73 [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
74 [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
75 [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
76 [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
77 [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
78 [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
79 [INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
94 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
95 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
96 [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
97 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
98 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
99 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
100 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
101 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
102 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
105 struct instance_netdev {
106 struct blobmsg_list_node node;
110 struct instance_file {
111 struct blobmsg_list_node node;
120 static const struct rlimit_name rlimit_names[] = {
122 { "core", RLIMIT_CORE },
123 { "cpu", RLIMIT_CPU },
124 { "data", RLIMIT_DATA },
125 { "fsize", RLIMIT_FSIZE },
126 { "memlock", RLIMIT_MEMLOCK },
127 { "nofile", RLIMIT_NOFILE },
128 { "nproc", RLIMIT_NPROC },
129 { "rss", RLIMIT_RSS },
130 { "stack", RLIMIT_STACK },
132 { "nice", RLIMIT_NICE },
133 { "rtprio", RLIMIT_RTPRIO },
134 { "msgqueue", RLIMIT_MSGQUEUE },
135 { "sigpending", RLIMIT_SIGPENDING },
140 static char trace[] = "/sbin/utrace";
142 static void closefd(int fd)
144 if (fd > STDERR_FILENO)
149 instance_limits(const char *limit, const char *value)
153 unsigned long cur, max;
155 for (i = 0; rlimit_names[i].name != NULL; i++) {
156 if (strcmp(rlimit_names[i].name, limit))
158 if (!strcmp(value, "unlimited")) {
159 rlim.rlim_cur = RLIM_INFINITY;
160 rlim.rlim_max = RLIM_INFINITY;
162 if (getrlimit(rlimit_names[i].resource, &rlim))
168 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
175 setrlimit(rlimit_names[i].resource, &rlim);
181 jail_run(struct service_instance *in, char **argv)
183 struct blobmsg_list_node *var;
184 struct jail *jail = &in->jail;
187 argv[argc++] = "/sbin/ujail";
191 argv[argc++] = jail->name;
194 if (jail->hostname) {
196 argv[argc++] = jail->hostname;
201 argv[argc++] = in->seccomp;
204 if (in->no_new_privs)
222 blobmsg_list_for_each(&jail->mount, var) {
223 const char *type = blobmsg_data(var->data);
229 argv[argc++] = (char *) blobmsg_name(var->data);
238 instance_removepid(struct service_instance *in) {
241 if (unlink(in->pidfile)) {
242 ERROR("Failed to removed pidfile: %s: %d - %s\n",
243 in->pidfile, errno, strerror(errno));
250 instance_writepid(struct service_instance *in)
257 _pidfile = fopen(in->pidfile, "w");
258 if (_pidfile == NULL) {
259 ERROR("failed to open pidfile for writing: %s: %d (%s)",
260 in->pidfile, errno, strerror(errno));
263 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
264 ERROR("failed to write pidfile: %s: %d (%s)",
265 in->pidfile, errno, strerror(errno));
268 if (fclose(_pidfile)) {
269 ERROR("failed to close pidfile: %s: %d (%s)",
270 in->pidfile, errno, strerror(errno));
278 instance_run(struct service_instance *in, int _stdout, int _stderr)
280 struct blobmsg_list_node *var;
281 struct blob_attr *cur;
284 int argc = 1; /* NULL terminated */
286 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
287 bool setlbf = _stdout >= 0;
290 setpriority(PRIO_PROCESS, 0, in->nice);
292 blobmsg_for_each_attr(cur, in->command, rem)
295 blobmsg_list_for_each(&in->env, var)
296 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
299 setenv("SECCOMP_FILE", in->seccomp, 1);
301 if ((seccomp || setlbf) && asprintf(&ld_preload, "LD_PRELOAD=%s%s%s",
302 seccomp ? "/lib/libpreload-seccomp.so" : "",
303 seccomp && setlbf ? ":" : "",
304 setlbf ? "/lib/libsetlbf.so" : "") > 0)
307 blobmsg_list_for_each(&in->limits, var)
308 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
313 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
317 argv[argc++] = trace;
320 argc = jail_run(in, argv);
322 blobmsg_for_each_attr(cur, in->command, rem)
323 argv[argc++] = blobmsg_data(cur);
327 _stdin = open("/dev/null", O_RDONLY);
330 _stdout = open("/dev/null", O_WRONLY);
333 _stderr = open("/dev/null", O_WRONLY);
336 dup2(_stdin, STDIN_FILENO);
340 dup2(_stdout, STDOUT_FILENO);
344 dup2(_stderr, STDERR_FILENO);
348 if (in->gid && setgid(in->gid)) {
349 ERROR("failed to set group id %d: %d (%s)\n", in->gid, errno, strerror(errno));
352 if (in->uid && setuid(in->uid)) {
353 ERROR("failed to set user id %d: %d (%s)\n", in->uid, errno, strerror(errno));
357 execvp(argv[0], argv);
362 instance_free_stdio(struct service_instance *in)
364 if (in->_stdout.fd.fd > -1) {
365 ustream_free(&in->_stdout.stream);
366 close(in->_stdout.fd.fd);
367 in->_stdout.fd.fd = -1;
370 if (in->_stderr.fd.fd > -1) {
371 ustream_free(&in->_stderr.stream);
372 close(in->_stderr.fd.fd);
373 in->_stderr.fd.fd = -1;
378 instance_start(struct service_instance *in)
381 int opipe[2] = { -1, -1 };
382 int epipe[2] = { -1, -1 };
384 if (!avl_is_empty(&in->errors.avl)) {
385 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
389 if (in->proc.pending)
392 instance_free_stdio(in);
393 if (in->_stdout.fd.fd > -2) {
395 ULOG_WARN("pipe() failed: %d (%s)\n", errno, strerror(errno));
396 opipe[0] = opipe[1] = -1;
400 if (in->_stderr.fd.fd > -2) {
402 ULOG_WARN("pipe() failed: %d (%s)\n", errno, strerror(errno));
403 epipe[0] = epipe[1] = -1;
408 in->halt = !in->respawn;
421 instance_run(in, opipe[1], epipe[1]);
425 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
427 instance_writepid(in);
428 clock_gettime(CLOCK_MONOTONIC, &in->start);
429 uloop_process_add(&in->proc);
432 ustream_fd_init(&in->_stdout, opipe[0]);
437 ustream_fd_init(&in->_stderr, epipe[0]);
441 service_event("instance.start", in->srv->name, in->name);
445 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
447 char *newline, *str, *arg0, ident[32];
450 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
451 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
452 ulog_open(ULOG_SYSLOG, LOG_DAEMON, ident);
455 str = ustream_get_read_buf(s, NULL);
459 newline = strchr(str, '\n');
464 ulog(prio, "%s\n", str);
466 len = newline + 1 - str;
467 ustream_consume(s, len);
470 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
474 instance_stdout(struct ustream *s, int bytes)
476 instance_stdio(s, LOG_INFO,
477 container_of(s, struct service_instance, _stdout.stream));
481 instance_stderr(struct ustream *s, int bytes)
483 instance_stdio(s, LOG_ERR,
484 container_of(s, struct service_instance, _stderr.stream));
488 instance_timeout(struct uloop_timeout *t)
490 struct service_instance *in;
492 in = container_of(t, struct service_instance, timeout);
494 if (!in->halt && (in->restart || in->respawn))
499 instance_exit(struct uloop_process *p, int ret)
501 struct service_instance *in;
505 in = container_of(p, struct service_instance, proc);
507 clock_gettime(CLOCK_MONOTONIC, &tp);
508 runtime = tp.tv_sec - in->start.tv_sec;
510 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
514 uloop_timeout_cancel(&in->timeout);
516 instance_removepid(in);
517 } else if (in->restart) {
519 } else if (in->respawn) {
520 if (runtime < in->respawn_threshold)
523 in->respawn_count = 0;
524 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
525 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
526 in->srv->name, in->name, in->respawn_count, runtime);
527 in->restart = in->respawn = 0;
529 service_event("instance.fail", in->srv->name, in->name);
531 service_event("instance.respawn", in->srv->name, in->name);
532 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
535 service_event("instance.stop", in->srv->name, in->name);
539 instance_stop(struct service_instance *in)
541 if (!in->proc.pending)
544 in->restart = in->respawn = false;
545 kill(in->proc.pid, SIGTERM);
549 instance_restart(struct service_instance *in)
551 if (!in->proc.pending)
555 kill(in->proc.pid, SIGTERM);
556 instance_removepid(in);
560 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
565 if (!blob_attr_equal(in->command, in_new->command))
568 if (!blobmsg_list_equal(&in->env, &in_new->env))
571 if (!blobmsg_list_equal(&in->data, &in_new->data))
574 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
577 if (!blobmsg_list_equal(&in->file, &in_new->file))
580 if (in->nice != in_new->nice)
583 if (in->uid != in_new->uid)
586 if (in->gid != in_new->gid)
589 if (in->pidfile && in_new->pidfile)
590 if (strcmp(in->pidfile, in_new->pidfile))
593 if (in->pidfile && !in_new->pidfile)
596 if (!in->pidfile && in_new->pidfile)
599 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
602 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
605 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
612 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
614 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
615 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
617 return n1->ifindex == n2->ifindex;
621 instance_netdev_update(struct blobmsg_list_node *l)
623 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
625 n->ifindex = if_nametoindex(n->node.avl.key);
629 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
631 struct instance_file *f1 = container_of(l1, struct instance_file, node);
632 struct instance_file *f2 = container_of(l2, struct instance_file, node);
634 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
638 instance_file_update(struct blobmsg_list_node *l)
640 struct instance_file *f = container_of(l, struct instance_file, node);
645 memset(f->md5, 0, sizeof(f->md5));
647 fd = open(l->avl.key, O_RDONLY);
653 len = read(fd, buf, sizeof(buf));
663 md5_hash(buf, len, &md5);
666 md5_end(f->md5, &md5);
671 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
676 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
680 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
682 struct blobmsg_list_node *node;
687 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
690 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
692 blobmsg_list_for_each(l, node)
699 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
701 struct blob_attr *tb[__JAIL_ATTR_MAX];
702 struct jail *jail = &in->jail;
705 if (stat("/sbin/ujail", &s))
708 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
709 blobmsg_data(attr), blobmsg_data_len(attr));
713 if (tb[JAIL_ATTR_NAME]) {
714 jail->name = blobmsg_get_string(tb[JAIL_ATTR_NAME]);
717 if (tb[JAIL_ATTR_HOSTNAME]) {
718 jail->hostname = blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]);
721 if (tb[JAIL_ATTR_PROCFS]) {
722 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
725 if (tb[JAIL_ATTR_SYSFS]) {
726 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
729 if (tb[JAIL_ATTR_UBUS]) {
730 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
733 if (tb[JAIL_ATTR_LOG]) {
734 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
737 if (tb[JAIL_ATTR_RONLY]) {
738 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
741 if (tb[JAIL_ATTR_MOUNT]) {
742 struct blob_attr *cur;
745 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
747 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
756 instance_config_parse(struct service_instance *in)
758 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
759 struct blob_attr *cur, *cur2;
763 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
764 blobmsg_data(in->config), blobmsg_data_len(in->config));
766 cur = tb[INSTANCE_ATTR_COMMAND];
770 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
773 blobmsg_for_each_attr(cur2, cur, rem) {
782 if (tb[INSTANCE_ATTR_RESPAWN]) {
784 uint32_t vals[3] = { 3600, 5, 5};
786 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
787 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
789 vals[i] = atoi(blobmsg_get_string(cur2));
793 in->respawn_count = 0;
794 in->respawn_threshold = vals[0];
795 in->respawn_timeout = vals[1];
796 in->respawn_retry = vals[2];
798 if (tb[INSTANCE_ATTR_TRIGGER]) {
799 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
800 trigger_add(in->trigger, in);
803 if (tb[INSTANCE_ATTR_WATCH]) {
804 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
805 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
807 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
808 watch_add(blobmsg_get_string(cur2), in);
812 if ((cur = tb[INSTANCE_ATTR_NICE])) {
813 in->nice = (int8_t) blobmsg_get_u32(cur);
814 if (in->nice < -20 || in->nice > 20)
818 if (tb[INSTANCE_ATTR_USER]) {
819 struct passwd *p = getpwnam(blobmsg_get_string(tb[INSTANCE_ATTR_USER]));
826 if (tb[INSTANCE_ATTR_TRACE])
827 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
829 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
830 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
832 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP]) {
833 char *seccomp = blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]);
836 if (stat(seccomp, &s))
837 ERROR("%s: not starting seccomp as %s is missing\n", in->name, seccomp);
839 in->seccomp = seccomp;
842 if (tb[INSTANCE_ATTR_PIDFILE]) {
843 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
845 in->pidfile = pidfile;
848 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
849 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
851 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
852 in->_stdout.fd.fd = -1;
854 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
855 in->_stderr.fd.fd = -1;
857 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
859 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
862 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
865 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
868 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
871 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
878 instance_config_cleanup(struct service_instance *in)
880 blobmsg_list_free(&in->env);
881 blobmsg_list_free(&in->data);
882 blobmsg_list_free(&in->netdev);
883 blobmsg_list_free(&in->file);
884 blobmsg_list_free(&in->limits);
885 blobmsg_list_free(&in->errors);
886 blobmsg_list_free(&in->jail.mount);
890 instance_config_move(struct service_instance *in, struct service_instance *in_src)
892 instance_config_cleanup(in);
893 blobmsg_list_move(&in->env, &in_src->env);
894 blobmsg_list_move(&in->data, &in_src->data);
895 blobmsg_list_move(&in->netdev, &in_src->netdev);
896 blobmsg_list_move(&in->file, &in_src->file);
897 blobmsg_list_move(&in->limits, &in_src->limits);
898 blobmsg_list_move(&in->errors, &in_src->errors);
899 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
900 in->trigger = in_src->trigger;
901 in->command = in_src->command;
902 in->pidfile = in_src->pidfile;
903 in->name = in_src->name;
904 in->node.avl.key = in_src->node.avl.key;
907 in->config = in_src->config;
908 in_src->config = NULL;
912 instance_update(struct service_instance *in, struct service_instance *in_new)
914 bool changed = instance_config_changed(in, in_new);
915 bool running = in->proc.pending;
917 if (!changed && running)
922 instance_config_move(in, in_new);
925 instance_restart(in);
926 instance_config_move(in, in_new);
927 /* restart happens in the child callback handler */
933 instance_free(struct service_instance *in)
935 instance_free_stdio(in);
936 uloop_process_delete(&in->proc);
937 uloop_timeout_cancel(&in->timeout);
940 instance_config_cleanup(in);
946 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
948 config = blob_memdup(config);
950 in->name = blobmsg_name(config);
952 in->timeout.cb = instance_timeout;
953 in->proc.cb = instance_exit;
955 in->_stdout.fd.fd = -2;
956 in->_stdout.stream.string_data = true;
957 in->_stdout.stream.notify_read = instance_stdout;
959 in->_stderr.fd.fd = -2;
960 in->_stderr.stream.string_data = true;
961 in->_stderr.stream.notify_read = instance_stderr;
963 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
964 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
965 blobmsg_list_simple_init(&in->env);
966 blobmsg_list_simple_init(&in->data);
967 blobmsg_list_simple_init(&in->limits);
968 blobmsg_list_simple_init(&in->errors);
969 blobmsg_list_simple_init(&in->jail.mount);
970 in->valid = instance_config_parse(in);
973 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
980 i = blobmsg_open_table(b, in->name);
981 blobmsg_add_u8(b, "running", in->proc.pending);
982 if (in->proc.pending)
983 blobmsg_add_u32(b, "pid", in->proc.pid);
984 blobmsg_add_blob(b, in->command);
986 if (!avl_is_empty(&in->errors.avl)) {
987 struct blobmsg_list_node *var;
988 void *e = blobmsg_open_array(b, "errors");
989 blobmsg_list_for_each(&in->errors, var)
990 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
991 blobmsg_close_table(b, e);
994 if (!avl_is_empty(&in->env.avl)) {
995 struct blobmsg_list_node *var;
996 void *e = blobmsg_open_table(b, "env");
997 blobmsg_list_for_each(&in->env, var)
998 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
999 blobmsg_close_table(b, e);
1002 if (!avl_is_empty(&in->data.avl)) {
1003 struct blobmsg_list_node *var;
1004 void *e = blobmsg_open_table(b, "data");
1005 blobmsg_list_for_each(&in->data, var)
1006 blobmsg_add_blob(b, var->data);
1007 blobmsg_close_table(b, e);
1010 if (!avl_is_empty(&in->limits.avl)) {
1011 struct blobmsg_list_node *var;
1012 void *e = blobmsg_open_table(b, "limits");
1013 blobmsg_list_for_each(&in->limits, var)
1014 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1015 blobmsg_close_table(b, e);
1019 void *r = blobmsg_open_table(b, "respawn");
1020 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1021 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1022 blobmsg_add_u32(b, "retry", in->respawn_retry);
1023 blobmsg_close_table(b, r);
1027 blobmsg_add_u8(b, "trace", true);
1029 if (in->no_new_privs)
1030 blobmsg_add_u8(b, "no_new_privs", true);
1033 blobmsg_add_string(b, "seccomp", in->seccomp);
1036 blobmsg_add_string(b, "pidfile", in->pidfile);
1039 void *r = blobmsg_open_table(b, "jail");
1041 blobmsg_add_string(b, "name", in->jail.name);
1042 if (in->jail.hostname)
1043 blobmsg_add_string(b, "hostname", in->jail.hostname);
1044 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1045 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1046 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1047 blobmsg_add_u8(b, "log", in->jail.log);
1048 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1049 blobmsg_close_table(b, r);
1050 if (!avl_is_empty(&in->jail.mount.avl)) {
1051 struct blobmsg_list_node *var;
1052 void *e = blobmsg_open_table(b, "mount");
1053 blobmsg_list_for_each(&in->jail.mount, var)
1054 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1055 blobmsg_close_table(b, e);
1059 if (verbose && in->trigger)
1060 blobmsg_add_blob(b, in->trigger);
1062 blobmsg_close_table(b, i);