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
},
88 static const struct blobmsg_policy jail_attr
[__JAIL_ATTR_MAX
] = {
89 [JAIL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
90 [JAIL_ATTR_PROCFS
] = { "procfs", BLOBMSG_TYPE_BOOL
},
91 [JAIL_ATTR_SYSFS
] = { "sysfs", BLOBMSG_TYPE_BOOL
},
92 [JAIL_ATTR_UBUS
] = { "ubus", BLOBMSG_TYPE_BOOL
},
93 [JAIL_ATTR_LOG
] = { "log", BLOBMSG_TYPE_BOOL
},
94 [JAIL_ATTR_MOUNT
] = { "mount", BLOBMSG_TYPE_TABLE
},
97 struct instance_netdev
{
98 struct blobmsg_list_node node
;
102 struct instance_file
{
103 struct blobmsg_list_node node
;
112 static const struct rlimit_name rlimit_names
[] = {
114 { "core", RLIMIT_CORE
},
115 { "cpu", RLIMIT_CPU
},
116 { "data", RLIMIT_DATA
},
117 { "fsize", RLIMIT_FSIZE
},
118 { "memlock", RLIMIT_MEMLOCK
},
119 { "msgqueue", RLIMIT_MSGQUEUE
},
120 { "nice", RLIMIT_NICE
},
121 { "nofile", RLIMIT_NOFILE
},
122 { "nproc", RLIMIT_NPROC
},
123 { "rss", RLIMIT_RSS
},
124 { "rtprio", RLIMIT_RTPRIO
},
125 { "sigpending", RLIMIT_SIGPENDING
},
126 { "stack", RLIMIT_STACK
},
130 static char trace
[] = "/sbin/utrace";
132 static void closefd(int fd
)
134 if (fd
> STDERR_FILENO
)
139 instance_limits(const char *limit
, const char *value
)
143 unsigned long cur
, max
;
145 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
146 if (strcmp(rlimit_names
[i
].name
, limit
))
148 if (!strcmp(value
, "unlimited")) {
149 rlim
.rlim_cur
= RLIM_INFINITY
;
150 rlim
.rlim_max
= RLIM_INFINITY
;
152 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
158 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
165 setrlimit(rlimit_names
[i
].resource
, &rlim
);
171 jail_run(struct service_instance
*in
, char **argv
)
173 struct blobmsg_list_node
*var
;
174 struct jail
*jail
= &in
->jail
;
177 argv
[argc
++] = "/sbin/ujail";
181 argv
[argc
++] = jail
->name
;
186 argv
[argc
++] = in
->seccomp
;
201 blobmsg_list_for_each(&jail
->mount
, var
) {
202 const char *type
= blobmsg_data(var
->data
);
208 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
217 instance_run(struct service_instance
*in
, int _stdout
, int _stderr
)
219 struct blobmsg_list_node
*var
;
220 struct blob_attr
*cur
;
223 int argc
= 1; /* NULL terminated */
225 bool seccomp
= !in
->trace
&& !in
->has_jail
&& in
->seccomp
;
226 bool setlbf
= _stdout
>= 0;
229 setpriority(PRIO_PROCESS
, 0, in
->nice
);
231 blobmsg_for_each_attr(cur
, in
->command
, rem
)
234 blobmsg_list_for_each(&in
->env
, var
)
235 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
238 setenv("SECCOMP_FILE", in
->seccomp
, 1);
240 if ((seccomp
|| setlbf
) && asprintf(&ld_preload
, "LD_PRELOAD=%s%s%s",
241 seccomp
? "/lib/libpreload-seccomp.so" : "",
242 seccomp
&& setlbf
? ":" : "",
243 setlbf
? "/lib/libsetlbf.so" : "") > 0)
246 blobmsg_list_for_each(&in
->limits
, var
)
247 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
252 argv
= alloca(sizeof(char *) * (argc
+ in
->jail
.argc
));
256 argv
[argc
++] = trace
;
259 argc
= jail_run(in
, argv
);
261 blobmsg_for_each_attr(cur
, in
->command
, rem
)
262 argv
[argc
++] = blobmsg_data(cur
);
266 _stdin
= open("/dev/null", O_RDONLY
);
269 _stdout
= open("/dev/null", O_WRONLY
);
272 _stderr
= open("/dev/null", O_WRONLY
);
275 dup2(_stdin
, STDIN_FILENO
);
279 dup2(_stdout
, STDOUT_FILENO
);
283 dup2(_stderr
, STDERR_FILENO
);
287 if (in
->gid
&& setgid(in
->gid
)) {
288 ERROR("failed to set group id %d: %d (%s)\n", in
->gid
, errno
, strerror(errno
));
291 if (in
->uid
&& setuid(in
->uid
)) {
292 ERROR("failed to set user id %d: %d (%s)\n", in
->uid
, errno
, strerror(errno
));
296 execvp(argv
[0], argv
);
301 instance_free_stdio(struct service_instance
*in
)
303 if (in
->_stdout
.fd
.fd
> -1) {
304 ustream_free(&in
->_stdout
.stream
);
305 close(in
->_stdout
.fd
.fd
);
306 in
->_stdout
.fd
.fd
= -1;
309 if (in
->_stderr
.fd
.fd
> -1) {
310 ustream_free(&in
->_stderr
.stream
);
311 close(in
->_stderr
.fd
.fd
);
312 in
->_stderr
.fd
.fd
= -1;
317 instance_start(struct service_instance
*in
)
320 int opipe
[2] = { -1, -1 };
321 int epipe
[2] = { -1, -1 };
323 if (!avl_is_empty(&in
->errors
.avl
)) {
324 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
328 if (in
->proc
.pending
)
331 instance_free_stdio(in
);
332 if (in
->_stdout
.fd
.fd
> -2) {
334 ULOG_WARN("pipe() failed: %d (%s)\n", errno
, strerror(errno
));
335 opipe
[0] = opipe
[1] = -1;
339 if (in
->_stderr
.fd
.fd
> -2) {
341 ULOG_WARN("pipe() failed: %d (%s)\n", errno
, strerror(errno
));
342 epipe
[0] = epipe
[1] = -1;
347 in
->halt
= !in
->respawn
;
360 instance_run(in
, opipe
[1], epipe
[1]);
364 DEBUG(2, "Started instance %s::%s\n", in
->srv
->name
, in
->name
);
366 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
367 uloop_process_add(&in
->proc
);
370 ustream_fd_init(&in
->_stdout
, opipe
[0]);
375 ustream_fd_init(&in
->_stderr
, epipe
[0]);
379 service_event("instance.start", in
->srv
->name
, in
->name
);
383 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
385 char *newline
, *str
, *arg0
, ident
[32];
388 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
389 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
390 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, ident
);
393 str
= ustream_get_read_buf(s
, NULL
);
397 newline
= strchr(str
, '\n');
402 ulog(prio
, "%s\n", str
);
404 len
= newline
+ 1 - str
;
405 ustream_consume(s
, len
);
408 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
412 instance_stdout(struct ustream
*s
, int bytes
)
414 instance_stdio(s
, LOG_INFO
,
415 container_of(s
, struct service_instance
, _stdout
.stream
));
419 instance_stderr(struct ustream
*s
, int bytes
)
421 instance_stdio(s
, LOG_ERR
,
422 container_of(s
, struct service_instance
, _stderr
.stream
));
426 instance_timeout(struct uloop_timeout
*t
)
428 struct service_instance
*in
;
430 in
= container_of(t
, struct service_instance
, timeout
);
432 if (!in
->halt
&& (in
->restart
|| in
->respawn
))
437 instance_exit(struct uloop_process
*p
, int ret
)
439 struct service_instance
*in
;
443 in
= container_of(p
, struct service_instance
, proc
);
445 clock_gettime(CLOCK_MONOTONIC
, &tp
);
446 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
448 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
452 uloop_timeout_cancel(&in
->timeout
);
455 } else if (in
->restart
) {
457 } else if (in
->respawn
) {
458 if (runtime
< in
->respawn_threshold
)
461 in
->respawn_count
= 0;
462 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
463 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
464 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
465 in
->restart
= in
->respawn
= 0;
468 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
471 service_event("instance.stop", in
->srv
->name
, in
->name
);
475 instance_stop(struct service_instance
*in
)
477 if (!in
->proc
.pending
)
480 in
->restart
= in
->respawn
= false;
481 kill(in
->proc
.pid
, SIGTERM
);
485 instance_restart(struct service_instance
*in
)
487 if (!in
->proc
.pending
)
491 kill(in
->proc
.pid
, SIGTERM
);
495 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
500 if (!blob_attr_equal(in
->command
, in_new
->command
))
503 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
506 if (!blobmsg_list_equal(&in
->data
, &in_new
->data
))
509 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
512 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
515 if (in
->nice
!= in_new
->nice
)
518 if (in
->uid
!= in_new
->uid
)
521 if (in
->gid
!= in_new
->gid
)
524 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
527 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
530 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
537 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
539 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
540 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
542 return n1
->ifindex
== n2
->ifindex
;
546 instance_netdev_update(struct blobmsg_list_node
*l
)
548 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
550 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
554 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
556 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
557 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
559 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
563 instance_file_update(struct blobmsg_list_node
*l
)
565 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
570 memset(f
->md5
, 0, sizeof(f
->md5
));
572 fd
= open(l
->avl
.key
, O_RDONLY
);
578 len
= read(fd
, buf
, sizeof(buf
));
588 md5_hash(buf
, len
, &md5
);
591 md5_end(f
->md5
, &md5
);
596 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
601 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
605 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
607 struct blobmsg_list_node
*node
;
612 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
615 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
617 blobmsg_list_for_each(l
, node
)
624 instance_jail_parse(struct service_instance
*in
, struct blob_attr
*attr
)
626 struct blob_attr
*tb
[__JAIL_ATTR_MAX
];
627 struct jail
*jail
= &in
->jail
;
630 if (stat("/sbin/ujail", &s
))
633 blobmsg_parse(jail_attr
, __JAIL_ATTR_MAX
, tb
,
634 blobmsg_data(attr
), blobmsg_data_len(attr
));
638 if (tb
[JAIL_ATTR_NAME
]) {
639 jail
->name
= blobmsg_get_string(tb
[JAIL_ATTR_NAME
]);
642 if (tb
[JAIL_ATTR_PROCFS
]) {
643 jail
->procfs
= blobmsg_get_bool(tb
[JAIL_ATTR_PROCFS
]);
646 if (tb
[JAIL_ATTR_SYSFS
]) {
647 jail
->sysfs
= blobmsg_get_bool(tb
[JAIL_ATTR_SYSFS
]);
650 if (tb
[JAIL_ATTR_UBUS
]) {
651 jail
->ubus
= blobmsg_get_bool(tb
[JAIL_ATTR_UBUS
]);
654 if (tb
[JAIL_ATTR_LOG
]) {
655 jail
->log
= blobmsg_get_bool(tb
[JAIL_ATTR_LOG
]);
658 if (tb
[JAIL_ATTR_MOUNT
]) {
659 struct blob_attr
*cur
;
662 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_MOUNT
], rem
)
664 instance_fill_array(&jail
->mount
, tb
[JAIL_ATTR_MOUNT
], NULL
, false);
673 instance_config_parse(struct service_instance
*in
)
675 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
676 struct blob_attr
*cur
, *cur2
;
680 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
681 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
683 cur
= tb
[INSTANCE_ATTR_COMMAND
];
687 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
690 blobmsg_for_each_attr(cur2
, cur
, rem
) {
699 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
701 uint32_t vals
[3] = { 3600, 5, 5};
703 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
704 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
706 vals
[i
] = atoi(blobmsg_get_string(cur2
));
710 in
->respawn_count
= 0;
711 in
->respawn_threshold
= vals
[0];
712 in
->respawn_timeout
= vals
[1];
713 in
->respawn_retry
= vals
[2];
715 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
716 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
717 trigger_add(in
->trigger
, in
);
720 if (tb
[INSTANCE_ATTR_WATCH
]) {
721 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
722 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
724 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
725 watch_add(blobmsg_get_string(cur2
), in
);
729 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
730 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
731 if (in
->nice
< -20 || in
->nice
> 20)
735 if (tb
[INSTANCE_ATTR_USER
]) {
736 struct passwd
*p
= getpwnam(blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]));
743 if (tb
[INSTANCE_ATTR_TRACE
])
744 in
->trace
= blobmsg_get_bool(tb
[INSTANCE_ATTR_TRACE
]);
746 if (!in
->trace
&& tb
[INSTANCE_ATTR_SECCOMP
]) {
747 char *seccomp
= blobmsg_get_string(tb
[INSTANCE_ATTR_SECCOMP
]);
750 if (stat(seccomp
, &s
))
751 ERROR("%s: not starting seccomp as %s is missing\n", in
->name
, seccomp
);
753 in
->seccomp
= seccomp
;
755 if (!in
->trace
&& tb
[INSTANCE_ATTR_JAIL
])
756 in
->has_jail
= instance_jail_parse(in
, tb
[INSTANCE_ATTR_JAIL
]);
758 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
759 in
->_stdout
.fd
.fd
= -1;
761 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
762 in
->_stderr
.fd
.fd
= -1;
764 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
766 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
769 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
772 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
775 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
778 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
785 instance_config_cleanup(struct service_instance
*in
)
787 blobmsg_list_free(&in
->env
);
788 blobmsg_list_free(&in
->data
);
789 blobmsg_list_free(&in
->netdev
);
790 blobmsg_list_free(&in
->file
);
791 blobmsg_list_free(&in
->limits
);
792 blobmsg_list_free(&in
->errors
);
793 blobmsg_list_free(&in
->jail
.mount
);
797 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
799 instance_config_cleanup(in
);
800 blobmsg_list_move(&in
->env
, &in_src
->env
);
801 blobmsg_list_move(&in
->data
, &in_src
->data
);
802 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
803 blobmsg_list_move(&in
->file
, &in_src
->file
);
804 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
805 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
806 blobmsg_list_move(&in
->jail
.mount
, &in_src
->jail
.mount
);
807 in
->trigger
= in_src
->trigger
;
808 in
->command
= in_src
->command
;
809 in
->name
= in_src
->name
;
810 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
813 in
->config
= in_src
->config
;
814 in_src
->config
= NULL
;
818 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
820 bool changed
= instance_config_changed(in
, in_new
);
821 bool running
= in
->proc
.pending
;
823 if (!changed
&& running
)
828 instance_config_move(in
, in_new
);
831 instance_restart(in
);
832 instance_config_move(in
, in_new
);
833 /* restart happens in the child callback handler */
839 instance_free(struct service_instance
*in
)
841 instance_free_stdio(in
);
842 uloop_process_delete(&in
->proc
);
843 uloop_timeout_cancel(&in
->timeout
);
846 instance_config_cleanup(in
);
852 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
854 config
= blob_memdup(config
);
856 in
->name
= blobmsg_name(config
);
858 in
->timeout
.cb
= instance_timeout
;
859 in
->proc
.cb
= instance_exit
;
861 in
->_stdout
.fd
.fd
= -2;
862 in
->_stdout
.stream
.string_data
= true;
863 in
->_stdout
.stream
.notify_read
= instance_stdout
;
865 in
->_stderr
.fd
.fd
= -2;
866 in
->_stderr
.stream
.string_data
= true;
867 in
->_stderr
.stream
.notify_read
= instance_stderr
;
869 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
870 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
871 blobmsg_list_simple_init(&in
->env
);
872 blobmsg_list_simple_init(&in
->data
);
873 blobmsg_list_simple_init(&in
->limits
);
874 blobmsg_list_simple_init(&in
->errors
);
875 blobmsg_list_simple_init(&in
->jail
.mount
);
876 in
->valid
= instance_config_parse(in
);
879 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
886 i
= blobmsg_open_table(b
, in
->name
);
887 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
888 if (in
->proc
.pending
)
889 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
890 blobmsg_add_blob(b
, in
->command
);
892 if (!avl_is_empty(&in
->errors
.avl
)) {
893 struct blobmsg_list_node
*var
;
894 void *e
= blobmsg_open_array(b
, "errors");
895 blobmsg_list_for_each(&in
->errors
, var
)
896 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
897 blobmsg_close_table(b
, e
);
900 if (!avl_is_empty(&in
->env
.avl
)) {
901 struct blobmsg_list_node
*var
;
902 void *e
= blobmsg_open_table(b
, "env");
903 blobmsg_list_for_each(&in
->env
, var
)
904 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
905 blobmsg_close_table(b
, e
);
908 if (!avl_is_empty(&in
->data
.avl
)) {
909 struct blobmsg_list_node
*var
;
910 void *e
= blobmsg_open_table(b
, "data");
911 blobmsg_list_for_each(&in
->data
, var
)
912 blobmsg_add_blob(b
, var
->data
);
913 blobmsg_close_table(b
, e
);
916 if (!avl_is_empty(&in
->limits
.avl
)) {
917 struct blobmsg_list_node
*var
;
918 void *e
= blobmsg_open_table(b
, "limits");
919 blobmsg_list_for_each(&in
->limits
, var
)
920 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
921 blobmsg_close_table(b
, e
);
925 void *r
= blobmsg_open_table(b
, "respawn");
926 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
927 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
928 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
929 blobmsg_close_table(b
, r
);
933 blobmsg_add_u8(b
, "trace", true);
936 blobmsg_add_string(b
, "seccomp", in
->seccomp
);
939 void *r
= blobmsg_open_table(b
, "jail");
941 blobmsg_add_string(b
, "name", in
->jail
.name
);
942 blobmsg_add_u8(b
, "procfs", in
->jail
.procfs
);
943 blobmsg_add_u8(b
, "sysfs", in
->jail
.sysfs
);
944 blobmsg_add_u8(b
, "ubus", in
->jail
.ubus
);
945 blobmsg_add_u8(b
, "log", in
->jail
.log
);
946 blobmsg_close_table(b
, r
);
947 if (!avl_is_empty(&in
->jail
.mount
.avl
)) {
948 struct blobmsg_list_node
*var
;
949 void *e
= blobmsg_open_table(b
, "mount");
950 blobmsg_list_for_each(&in
->jail
.mount
, var
)
951 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
952 blobmsg_close_table(b
, e
);
956 if (verbose
&& in
->trigger
)
957 blobmsg_add_blob(b
, in
->trigger
);
959 blobmsg_close_table(b
, i
);