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;
530 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
533 service_event("instance.stop", in->srv->name, in->name);
537 instance_stop(struct service_instance *in)
539 if (!in->proc.pending)
542 in->restart = in->respawn = false;
543 kill(in->proc.pid, SIGTERM);
547 instance_restart(struct service_instance *in)
549 if (!in->proc.pending)
553 kill(in->proc.pid, SIGTERM);
554 instance_removepid(in);
558 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
563 if (!blob_attr_equal(in->command, in_new->command))
566 if (!blobmsg_list_equal(&in->env, &in_new->env))
569 if (!blobmsg_list_equal(&in->data, &in_new->data))
572 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
575 if (!blobmsg_list_equal(&in->file, &in_new->file))
578 if (in->nice != in_new->nice)
581 if (in->uid != in_new->uid)
584 if (in->gid != in_new->gid)
587 if (in->pidfile && in_new->pidfile)
588 if (strcmp(in->pidfile, in_new->pidfile))
591 if (in->pidfile && !in_new->pidfile)
594 if (!in->pidfile && in_new->pidfile)
597 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
600 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
603 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
610 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
612 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
613 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
615 return n1->ifindex == n2->ifindex;
619 instance_netdev_update(struct blobmsg_list_node *l)
621 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
623 n->ifindex = if_nametoindex(n->node.avl.key);
627 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
629 struct instance_file *f1 = container_of(l1, struct instance_file, node);
630 struct instance_file *f2 = container_of(l2, struct instance_file, node);
632 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
636 instance_file_update(struct blobmsg_list_node *l)
638 struct instance_file *f = container_of(l, struct instance_file, node);
643 memset(f->md5, 0, sizeof(f->md5));
645 fd = open(l->avl.key, O_RDONLY);
651 len = read(fd, buf, sizeof(buf));
661 md5_hash(buf, len, &md5);
664 md5_end(f->md5, &md5);
669 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
674 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
678 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
680 struct blobmsg_list_node *node;
685 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
688 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
690 blobmsg_list_for_each(l, node)
697 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
699 struct blob_attr *tb[__JAIL_ATTR_MAX];
700 struct jail *jail = &in->jail;
703 if (stat("/sbin/ujail", &s))
706 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
707 blobmsg_data(attr), blobmsg_data_len(attr));
711 if (tb[JAIL_ATTR_NAME]) {
712 jail->name = blobmsg_get_string(tb[JAIL_ATTR_NAME]);
715 if (tb[JAIL_ATTR_HOSTNAME]) {
716 jail->hostname = blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]);
719 if (tb[JAIL_ATTR_PROCFS]) {
720 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
723 if (tb[JAIL_ATTR_SYSFS]) {
724 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
727 if (tb[JAIL_ATTR_UBUS]) {
728 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
731 if (tb[JAIL_ATTR_LOG]) {
732 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
735 if (tb[JAIL_ATTR_RONLY]) {
736 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
739 if (tb[JAIL_ATTR_MOUNT]) {
740 struct blob_attr *cur;
743 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
745 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
754 instance_config_parse(struct service_instance *in)
756 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
757 struct blob_attr *cur, *cur2;
761 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
762 blobmsg_data(in->config), blobmsg_data_len(in->config));
764 cur = tb[INSTANCE_ATTR_COMMAND];
768 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
771 blobmsg_for_each_attr(cur2, cur, rem) {
780 if (tb[INSTANCE_ATTR_RESPAWN]) {
782 uint32_t vals[3] = { 3600, 5, 5};
784 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
785 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
787 vals[i] = atoi(blobmsg_get_string(cur2));
791 in->respawn_count = 0;
792 in->respawn_threshold = vals[0];
793 in->respawn_timeout = vals[1];
794 in->respawn_retry = vals[2];
796 if (tb[INSTANCE_ATTR_TRIGGER]) {
797 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
798 trigger_add(in->trigger, in);
801 if (tb[INSTANCE_ATTR_WATCH]) {
802 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
803 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
805 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
806 watch_add(blobmsg_get_string(cur2), in);
810 if ((cur = tb[INSTANCE_ATTR_NICE])) {
811 in->nice = (int8_t) blobmsg_get_u32(cur);
812 if (in->nice < -20 || in->nice > 20)
816 if (tb[INSTANCE_ATTR_USER]) {
817 struct passwd *p = getpwnam(blobmsg_get_string(tb[INSTANCE_ATTR_USER]));
824 if (tb[INSTANCE_ATTR_TRACE])
825 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
827 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
828 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
830 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP]) {
831 char *seccomp = blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]);
834 if (stat(seccomp, &s))
835 ERROR("%s: not starting seccomp as %s is missing\n", in->name, seccomp);
837 in->seccomp = seccomp;
840 if (tb[INSTANCE_ATTR_PIDFILE]) {
841 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
843 in->pidfile = pidfile;
846 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
847 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
849 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
850 in->_stdout.fd.fd = -1;
852 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
853 in->_stderr.fd.fd = -1;
855 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
857 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
860 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
863 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
866 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
869 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
876 instance_config_cleanup(struct service_instance *in)
878 blobmsg_list_free(&in->env);
879 blobmsg_list_free(&in->data);
880 blobmsg_list_free(&in->netdev);
881 blobmsg_list_free(&in->file);
882 blobmsg_list_free(&in->limits);
883 blobmsg_list_free(&in->errors);
884 blobmsg_list_free(&in->jail.mount);
888 instance_config_move(struct service_instance *in, struct service_instance *in_src)
890 instance_config_cleanup(in);
891 blobmsg_list_move(&in->env, &in_src->env);
892 blobmsg_list_move(&in->data, &in_src->data);
893 blobmsg_list_move(&in->netdev, &in_src->netdev);
894 blobmsg_list_move(&in->file, &in_src->file);
895 blobmsg_list_move(&in->limits, &in_src->limits);
896 blobmsg_list_move(&in->errors, &in_src->errors);
897 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
898 in->trigger = in_src->trigger;
899 in->command = in_src->command;
900 in->pidfile = in_src->pidfile;
901 in->name = in_src->name;
902 in->node.avl.key = in_src->node.avl.key;
905 in->config = in_src->config;
906 in_src->config = NULL;
910 instance_update(struct service_instance *in, struct service_instance *in_new)
912 bool changed = instance_config_changed(in, in_new);
913 bool running = in->proc.pending;
915 if (!changed && running)
920 instance_config_move(in, in_new);
923 instance_restart(in);
924 instance_config_move(in, in_new);
925 /* restart happens in the child callback handler */
931 instance_free(struct service_instance *in)
933 instance_free_stdio(in);
934 uloop_process_delete(&in->proc);
935 uloop_timeout_cancel(&in->timeout);
938 instance_config_cleanup(in);
944 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
946 config = blob_memdup(config);
948 in->name = blobmsg_name(config);
950 in->timeout.cb = instance_timeout;
951 in->proc.cb = instance_exit;
953 in->_stdout.fd.fd = -2;
954 in->_stdout.stream.string_data = true;
955 in->_stdout.stream.notify_read = instance_stdout;
957 in->_stderr.fd.fd = -2;
958 in->_stderr.stream.string_data = true;
959 in->_stderr.stream.notify_read = instance_stderr;
961 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
962 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
963 blobmsg_list_simple_init(&in->env);
964 blobmsg_list_simple_init(&in->data);
965 blobmsg_list_simple_init(&in->limits);
966 blobmsg_list_simple_init(&in->errors);
967 blobmsg_list_simple_init(&in->jail.mount);
968 in->valid = instance_config_parse(in);
971 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
978 i = blobmsg_open_table(b, in->name);
979 blobmsg_add_u8(b, "running", in->proc.pending);
980 if (in->proc.pending)
981 blobmsg_add_u32(b, "pid", in->proc.pid);
982 blobmsg_add_blob(b, in->command);
984 if (!avl_is_empty(&in->errors.avl)) {
985 struct blobmsg_list_node *var;
986 void *e = blobmsg_open_array(b, "errors");
987 blobmsg_list_for_each(&in->errors, var)
988 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
989 blobmsg_close_table(b, e);
992 if (!avl_is_empty(&in->env.avl)) {
993 struct blobmsg_list_node *var;
994 void *e = blobmsg_open_table(b, "env");
995 blobmsg_list_for_each(&in->env, var)
996 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
997 blobmsg_close_table(b, e);
1000 if (!avl_is_empty(&in->data.avl)) {
1001 struct blobmsg_list_node *var;
1002 void *e = blobmsg_open_table(b, "data");
1003 blobmsg_list_for_each(&in->data, var)
1004 blobmsg_add_blob(b, var->data);
1005 blobmsg_close_table(b, e);
1008 if (!avl_is_empty(&in->limits.avl)) {
1009 struct blobmsg_list_node *var;
1010 void *e = blobmsg_open_table(b, "limits");
1011 blobmsg_list_for_each(&in->limits, var)
1012 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1013 blobmsg_close_table(b, e);
1017 void *r = blobmsg_open_table(b, "respawn");
1018 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1019 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1020 blobmsg_add_u32(b, "retry", in->respawn_retry);
1021 blobmsg_close_table(b, r);
1025 blobmsg_add_u8(b, "trace", true);
1027 if (in->no_new_privs)
1028 blobmsg_add_u8(b, "no_new_privs", true);
1031 blobmsg_add_string(b, "seccomp", in->seccomp);
1034 blobmsg_add_string(b, "pidfile", in->pidfile);
1037 void *r = blobmsg_open_table(b, "jail");
1039 blobmsg_add_string(b, "name", in->jail.name);
1040 if (in->jail.hostname)
1041 blobmsg_add_string(b, "hostname", in->jail.hostname);
1042 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1043 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1044 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1045 blobmsg_add_u8(b, "log", in->jail.log);
1046 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1047 blobmsg_close_table(b, r);
1048 if (!avl_is_empty(&in->jail.mount.avl)) {
1049 struct blobmsg_list_node *var;
1050 void *e = blobmsg_open_table(b, "mount");
1051 blobmsg_list_for_each(&in->jail.mount, var)
1052 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1053 blobmsg_close_table(b, e);
1057 if (verbose && in->trigger)
1058 blobmsg_add_blob(b, in->trigger);
1060 blobmsg_close_table(b, i);