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
,
54 INSTANCE_ATTR_SECCOMP
,
58 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
59 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
60 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
61 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
62 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
63 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
64 [INSTANCE_ATTR_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
65 [INSTANCE_ATTR_RESPAWN
] = { "respawn", BLOBMSG_TYPE_ARRAY
},
66 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
67 [INSTANCE_ATTR_LIMITS
] = { "limits", BLOBMSG_TYPE_TABLE
},
68 [INSTANCE_ATTR_WATCH
] = { "watch", BLOBMSG_TYPE_ARRAY
},
69 [INSTANCE_ATTR_ERROR
] = { "error", BLOBMSG_TYPE_ARRAY
},
70 [INSTANCE_ATTR_USER
] = { "user", BLOBMSG_TYPE_STRING
},
71 [INSTANCE_ATTR_STDOUT
] = { "stdout", BLOBMSG_TYPE_BOOL
},
72 [INSTANCE_ATTR_STDERR
] = { "stderr", BLOBMSG_TYPE_BOOL
},
73 [INSTANCE_ATTR_JAIL
] = { "jail", BLOBMSG_TYPE_TABLE
},
74 [INSTANCE_ATTR_TRACE
] = { "trace", BLOBMSG_TYPE_BOOL
},
75 [INSTANCE_ATTR_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_STRING
},
89 static const struct blobmsg_policy jail_attr
[__JAIL_ATTR_MAX
] = {
90 [JAIL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
91 [JAIL_ATTR_PROCFS
] = { "procfs", BLOBMSG_TYPE_BOOL
},
92 [JAIL_ATTR_SYSFS
] = { "sysfs", BLOBMSG_TYPE_BOOL
},
93 [JAIL_ATTR_UBUS
] = { "ubus", BLOBMSG_TYPE_BOOL
},
94 [JAIL_ATTR_LOG
] = { "log", BLOBMSG_TYPE_BOOL
},
95 [JAIL_ATTR_RONLY
] = { "ronly", BLOBMSG_TYPE_BOOL
},
96 [JAIL_ATTR_MOUNT
] = { "mount", BLOBMSG_TYPE_TABLE
},
99 struct instance_netdev
{
100 struct blobmsg_list_node node
;
104 struct instance_file
{
105 struct blobmsg_list_node node
;
114 static const struct rlimit_name rlimit_names
[] = {
116 { "core", RLIMIT_CORE
},
117 { "cpu", RLIMIT_CPU
},
118 { "data", RLIMIT_DATA
},
119 { "fsize", RLIMIT_FSIZE
},
120 { "memlock", RLIMIT_MEMLOCK
},
121 { "msgqueue", RLIMIT_MSGQUEUE
},
122 { "nice", RLIMIT_NICE
},
123 { "nofile", RLIMIT_NOFILE
},
124 { "nproc", RLIMIT_NPROC
},
125 { "rss", RLIMIT_RSS
},
126 { "rtprio", RLIMIT_RTPRIO
},
127 { "sigpending", RLIMIT_SIGPENDING
},
128 { "stack", RLIMIT_STACK
},
132 static char trace
[] = "/sbin/utrace";
134 static void closefd(int fd
)
136 if (fd
> STDERR_FILENO
)
141 instance_limits(const char *limit
, const char *value
)
145 unsigned long cur
, max
;
147 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
148 if (strcmp(rlimit_names
[i
].name
, limit
))
150 if (!strcmp(value
, "unlimited")) {
151 rlim
.rlim_cur
= RLIM_INFINITY
;
152 rlim
.rlim_max
= RLIM_INFINITY
;
154 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
160 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
167 setrlimit(rlimit_names
[i
].resource
, &rlim
);
173 jail_run(struct service_instance
*in
, char **argv
)
175 struct blobmsg_list_node
*var
;
176 struct jail
*jail
= &in
->jail
;
179 argv
[argc
++] = "/sbin/ujail";
183 argv
[argc
++] = jail
->name
;
188 argv
[argc
++] = in
->seccomp
;
206 blobmsg_list_for_each(&jail
->mount
, var
) {
207 const char *type
= blobmsg_data(var
->data
);
213 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
222 instance_run(struct service_instance
*in
, int _stdout
, int _stderr
)
224 struct blobmsg_list_node
*var
;
225 struct blob_attr
*cur
;
228 int argc
= 1; /* NULL terminated */
230 bool seccomp
= !in
->trace
&& !in
->has_jail
&& in
->seccomp
;
231 bool setlbf
= _stdout
>= 0;
234 setpriority(PRIO_PROCESS
, 0, in
->nice
);
236 blobmsg_for_each_attr(cur
, in
->command
, rem
)
239 blobmsg_list_for_each(&in
->env
, var
)
240 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
243 setenv("SECCOMP_FILE", in
->seccomp
, 1);
245 if ((seccomp
|| setlbf
) && asprintf(&ld_preload
, "LD_PRELOAD=%s%s%s",
246 seccomp
? "/lib/libpreload-seccomp.so" : "",
247 seccomp
&& setlbf
? ":" : "",
248 setlbf
? "/lib/libsetlbf.so" : "") > 0)
251 blobmsg_list_for_each(&in
->limits
, var
)
252 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
257 argv
= alloca(sizeof(char *) * (argc
+ in
->jail
.argc
));
261 argv
[argc
++] = trace
;
264 argc
= jail_run(in
, argv
);
266 blobmsg_for_each_attr(cur
, in
->command
, rem
)
267 argv
[argc
++] = blobmsg_data(cur
);
271 _stdin
= open("/dev/null", O_RDONLY
);
274 _stdout
= open("/dev/null", O_WRONLY
);
277 _stderr
= open("/dev/null", O_WRONLY
);
280 dup2(_stdin
, STDIN_FILENO
);
284 dup2(_stdout
, STDOUT_FILENO
);
288 dup2(_stderr
, STDERR_FILENO
);
292 if (in
->gid
&& setgid(in
->gid
)) {
293 ERROR("failed to set group id %d: %d (%s)\n", in
->gid
, errno
, strerror(errno
));
296 if (in
->uid
&& setuid(in
->uid
)) {
297 ERROR("failed to set user id %d: %d (%s)\n", in
->uid
, errno
, strerror(errno
));
301 execvp(argv
[0], argv
);
306 instance_free_stdio(struct service_instance
*in
)
308 if (in
->_stdout
.fd
.fd
> -1) {
309 ustream_free(&in
->_stdout
.stream
);
310 close(in
->_stdout
.fd
.fd
);
311 in
->_stdout
.fd
.fd
= -1;
314 if (in
->_stderr
.fd
.fd
> -1) {
315 ustream_free(&in
->_stderr
.stream
);
316 close(in
->_stderr
.fd
.fd
);
317 in
->_stderr
.fd
.fd
= -1;
322 instance_start(struct service_instance
*in
)
325 int opipe
[2] = { -1, -1 };
326 int epipe
[2] = { -1, -1 };
328 if (!avl_is_empty(&in
->errors
.avl
)) {
329 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
333 if (in
->proc
.pending
)
336 instance_free_stdio(in
);
337 if (in
->_stdout
.fd
.fd
> -2) {
339 ULOG_WARN("pipe() failed: %d (%s)\n", errno
, strerror(errno
));
340 opipe
[0] = opipe
[1] = -1;
344 if (in
->_stderr
.fd
.fd
> -2) {
346 ULOG_WARN("pipe() failed: %d (%s)\n", errno
, strerror(errno
));
347 epipe
[0] = epipe
[1] = -1;
352 in
->halt
= !in
->respawn
;
365 instance_run(in
, opipe
[1], epipe
[1]);
369 DEBUG(2, "Started instance %s::%s\n", in
->srv
->name
, in
->name
);
371 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
372 uloop_process_add(&in
->proc
);
375 ustream_fd_init(&in
->_stdout
, opipe
[0]);
380 ustream_fd_init(&in
->_stderr
, epipe
[0]);
384 service_event("instance.start", in
->srv
->name
, in
->name
);
388 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
390 char *newline
, *str
, *arg0
, ident
[32];
393 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
394 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
395 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, ident
);
398 str
= ustream_get_read_buf(s
, NULL
);
402 newline
= strchr(str
, '\n');
407 ulog(prio
, "%s\n", str
);
409 len
= newline
+ 1 - str
;
410 ustream_consume(s
, len
);
413 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
417 instance_stdout(struct ustream
*s
, int bytes
)
419 instance_stdio(s
, LOG_INFO
,
420 container_of(s
, struct service_instance
, _stdout
.stream
));
424 instance_stderr(struct ustream
*s
, int bytes
)
426 instance_stdio(s
, LOG_ERR
,
427 container_of(s
, struct service_instance
, _stderr
.stream
));
431 instance_timeout(struct uloop_timeout
*t
)
433 struct service_instance
*in
;
435 in
= container_of(t
, struct service_instance
, timeout
);
437 if (!in
->halt
&& (in
->restart
|| in
->respawn
))
442 instance_exit(struct uloop_process
*p
, int ret
)
444 struct service_instance
*in
;
448 in
= container_of(p
, struct service_instance
, proc
);
450 clock_gettime(CLOCK_MONOTONIC
, &tp
);
451 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
453 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
457 uloop_timeout_cancel(&in
->timeout
);
460 } else if (in
->restart
) {
462 } else if (in
->respawn
) {
463 if (runtime
< in
->respawn_threshold
)
466 in
->respawn_count
= 0;
467 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
468 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
469 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
470 in
->restart
= in
->respawn
= 0;
473 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
476 service_event("instance.stop", in
->srv
->name
, in
->name
);
480 instance_stop(struct service_instance
*in
)
482 if (!in
->proc
.pending
)
485 in
->restart
= in
->respawn
= false;
486 kill(in
->proc
.pid
, SIGTERM
);
490 instance_restart(struct service_instance
*in
)
492 if (!in
->proc
.pending
)
496 kill(in
->proc
.pid
, SIGTERM
);
500 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
505 if (!blob_attr_equal(in
->command
, in_new
->command
))
508 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
511 if (!blobmsg_list_equal(&in
->data
, &in_new
->data
))
514 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
517 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
520 if (in
->nice
!= in_new
->nice
)
523 if (in
->uid
!= in_new
->uid
)
526 if (in
->gid
!= in_new
->gid
)
529 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
532 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
535 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
542 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
544 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
545 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
547 return n1
->ifindex
== n2
->ifindex
;
551 instance_netdev_update(struct blobmsg_list_node
*l
)
553 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
555 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
559 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
561 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
562 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
564 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
568 instance_file_update(struct blobmsg_list_node
*l
)
570 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
575 memset(f
->md5
, 0, sizeof(f
->md5
));
577 fd
= open(l
->avl
.key
, O_RDONLY
);
583 len
= read(fd
, buf
, sizeof(buf
));
593 md5_hash(buf
, len
, &md5
);
596 md5_end(f
->md5
, &md5
);
601 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
606 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
610 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
612 struct blobmsg_list_node
*node
;
617 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
620 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
622 blobmsg_list_for_each(l
, node
)
629 instance_jail_parse(struct service_instance
*in
, struct blob_attr
*attr
)
631 struct blob_attr
*tb
[__JAIL_ATTR_MAX
];
632 struct jail
*jail
= &in
->jail
;
635 if (stat("/sbin/ujail", &s
))
638 blobmsg_parse(jail_attr
, __JAIL_ATTR_MAX
, tb
,
639 blobmsg_data(attr
), blobmsg_data_len(attr
));
643 if (tb
[JAIL_ATTR_NAME
]) {
644 jail
->name
= blobmsg_get_string(tb
[JAIL_ATTR_NAME
]);
647 if (tb
[JAIL_ATTR_PROCFS
]) {
648 jail
->procfs
= blobmsg_get_bool(tb
[JAIL_ATTR_PROCFS
]);
651 if (tb
[JAIL_ATTR_SYSFS
]) {
652 jail
->sysfs
= blobmsg_get_bool(tb
[JAIL_ATTR_SYSFS
]);
655 if (tb
[JAIL_ATTR_UBUS
]) {
656 jail
->ubus
= blobmsg_get_bool(tb
[JAIL_ATTR_UBUS
]);
659 if (tb
[JAIL_ATTR_LOG
]) {
660 jail
->log
= blobmsg_get_bool(tb
[JAIL_ATTR_LOG
]);
663 if (tb
[JAIL_ATTR_RONLY
]) {
664 jail
->ronly
= blobmsg_get_bool(tb
[JAIL_ATTR_RONLY
]);
667 if (tb
[JAIL_ATTR_MOUNT
]) {
668 struct blob_attr
*cur
;
671 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_MOUNT
], rem
)
673 instance_fill_array(&jail
->mount
, tb
[JAIL_ATTR_MOUNT
], NULL
, false);
682 instance_config_parse(struct service_instance
*in
)
684 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
685 struct blob_attr
*cur
, *cur2
;
689 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
690 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
692 cur
= tb
[INSTANCE_ATTR_COMMAND
];
696 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
699 blobmsg_for_each_attr(cur2
, cur
, rem
) {
708 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
710 uint32_t vals
[3] = { 3600, 5, 5};
712 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
713 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
715 vals
[i
] = atoi(blobmsg_get_string(cur2
));
719 in
->respawn_count
= 0;
720 in
->respawn_threshold
= vals
[0];
721 in
->respawn_timeout
= vals
[1];
722 in
->respawn_retry
= vals
[2];
724 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
725 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
726 trigger_add(in
->trigger
, in
);
729 if (tb
[INSTANCE_ATTR_WATCH
]) {
730 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
731 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
733 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
734 watch_add(blobmsg_get_string(cur2
), in
);
738 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
739 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
740 if (in
->nice
< -20 || in
->nice
> 20)
744 if (tb
[INSTANCE_ATTR_USER
]) {
745 struct passwd
*p
= getpwnam(blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]));
752 if (tb
[INSTANCE_ATTR_TRACE
])
753 in
->trace
= blobmsg_get_bool(tb
[INSTANCE_ATTR_TRACE
]);
755 if (!in
->trace
&& tb
[INSTANCE_ATTR_SECCOMP
]) {
756 char *seccomp
= blobmsg_get_string(tb
[INSTANCE_ATTR_SECCOMP
]);
759 if (stat(seccomp
, &s
))
760 ERROR("%s: not starting seccomp as %s is missing\n", in
->name
, seccomp
);
762 in
->seccomp
= seccomp
;
764 if (!in
->trace
&& tb
[INSTANCE_ATTR_JAIL
])
765 in
->has_jail
= instance_jail_parse(in
, tb
[INSTANCE_ATTR_JAIL
]);
767 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
768 in
->_stdout
.fd
.fd
= -1;
770 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
771 in
->_stderr
.fd
.fd
= -1;
773 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
775 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
778 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
781 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
784 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
787 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
794 instance_config_cleanup(struct service_instance
*in
)
796 blobmsg_list_free(&in
->env
);
797 blobmsg_list_free(&in
->data
);
798 blobmsg_list_free(&in
->netdev
);
799 blobmsg_list_free(&in
->file
);
800 blobmsg_list_free(&in
->limits
);
801 blobmsg_list_free(&in
->errors
);
802 blobmsg_list_free(&in
->jail
.mount
);
806 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
808 instance_config_cleanup(in
);
809 blobmsg_list_move(&in
->env
, &in_src
->env
);
810 blobmsg_list_move(&in
->data
, &in_src
->data
);
811 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
812 blobmsg_list_move(&in
->file
, &in_src
->file
);
813 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
814 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
815 blobmsg_list_move(&in
->jail
.mount
, &in_src
->jail
.mount
);
816 in
->trigger
= in_src
->trigger
;
817 in
->command
= in_src
->command
;
818 in
->name
= in_src
->name
;
819 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
822 in
->config
= in_src
->config
;
823 in_src
->config
= NULL
;
827 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
829 bool changed
= instance_config_changed(in
, in_new
);
830 bool running
= in
->proc
.pending
;
832 if (!changed
&& running
)
837 instance_config_move(in
, in_new
);
840 instance_restart(in
);
841 instance_config_move(in
, in_new
);
842 /* restart happens in the child callback handler */
848 instance_free(struct service_instance
*in
)
850 instance_free_stdio(in
);
851 uloop_process_delete(&in
->proc
);
852 uloop_timeout_cancel(&in
->timeout
);
855 instance_config_cleanup(in
);
861 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
863 config
= blob_memdup(config
);
865 in
->name
= blobmsg_name(config
);
867 in
->timeout
.cb
= instance_timeout
;
868 in
->proc
.cb
= instance_exit
;
870 in
->_stdout
.fd
.fd
= -2;
871 in
->_stdout
.stream
.string_data
= true;
872 in
->_stdout
.stream
.notify_read
= instance_stdout
;
874 in
->_stderr
.fd
.fd
= -2;
875 in
->_stderr
.stream
.string_data
= true;
876 in
->_stderr
.stream
.notify_read
= instance_stderr
;
878 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
879 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
880 blobmsg_list_simple_init(&in
->env
);
881 blobmsg_list_simple_init(&in
->data
);
882 blobmsg_list_simple_init(&in
->limits
);
883 blobmsg_list_simple_init(&in
->errors
);
884 blobmsg_list_simple_init(&in
->jail
.mount
);
885 in
->valid
= instance_config_parse(in
);
888 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
895 i
= blobmsg_open_table(b
, in
->name
);
896 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
897 if (in
->proc
.pending
)
898 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
899 blobmsg_add_blob(b
, in
->command
);
901 if (!avl_is_empty(&in
->errors
.avl
)) {
902 struct blobmsg_list_node
*var
;
903 void *e
= blobmsg_open_array(b
, "errors");
904 blobmsg_list_for_each(&in
->errors
, var
)
905 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
906 blobmsg_close_table(b
, e
);
909 if (!avl_is_empty(&in
->env
.avl
)) {
910 struct blobmsg_list_node
*var
;
911 void *e
= blobmsg_open_table(b
, "env");
912 blobmsg_list_for_each(&in
->env
, var
)
913 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
914 blobmsg_close_table(b
, e
);
917 if (!avl_is_empty(&in
->data
.avl
)) {
918 struct blobmsg_list_node
*var
;
919 void *e
= blobmsg_open_table(b
, "data");
920 blobmsg_list_for_each(&in
->data
, var
)
921 blobmsg_add_blob(b
, var
->data
);
922 blobmsg_close_table(b
, e
);
925 if (!avl_is_empty(&in
->limits
.avl
)) {
926 struct blobmsg_list_node
*var
;
927 void *e
= blobmsg_open_table(b
, "limits");
928 blobmsg_list_for_each(&in
->limits
, var
)
929 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
930 blobmsg_close_table(b
, e
);
934 void *r
= blobmsg_open_table(b
, "respawn");
935 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
936 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
937 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
938 blobmsg_close_table(b
, r
);
942 blobmsg_add_u8(b
, "trace", true);
945 blobmsg_add_string(b
, "seccomp", in
->seccomp
);
948 void *r
= blobmsg_open_table(b
, "jail");
950 blobmsg_add_string(b
, "name", in
->jail
.name
);
951 blobmsg_add_u8(b
, "procfs", in
->jail
.procfs
);
952 blobmsg_add_u8(b
, "sysfs", in
->jail
.sysfs
);
953 blobmsg_add_u8(b
, "ubus", in
->jail
.ubus
);
954 blobmsg_add_u8(b
, "log", in
->jail
.log
);
955 blobmsg_add_u8(b
, "ronly", in
->jail
.ronly
);
956 blobmsg_close_table(b
, r
);
957 if (!avl_is_empty(&in
->jail
.mount
.avl
)) {
958 struct blobmsg_list_node
*var
;
959 void *e
= blobmsg_open_table(b
, "mount");
960 blobmsg_list_for_each(&in
->jail
.mount
, var
)
961 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
962 blobmsg_close_table(b
, e
);
966 if (verbose
&& in
->trigger
)
967 blobmsg_add_blob(b
, in
->trigger
);
969 blobmsg_close_table(b
, i
);