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
,
59 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
60 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
61 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
62 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
63 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
64 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
65 [INSTANCE_ATTR_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
66 [INSTANCE_ATTR_RESPAWN
] = { "respawn", BLOBMSG_TYPE_ARRAY
},
67 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
68 [INSTANCE_ATTR_LIMITS
] = { "limits", BLOBMSG_TYPE_TABLE
},
69 [INSTANCE_ATTR_WATCH
] = { "watch", BLOBMSG_TYPE_ARRAY
},
70 [INSTANCE_ATTR_ERROR
] = { "error", BLOBMSG_TYPE_ARRAY
},
71 [INSTANCE_ATTR_USER
] = { "user", BLOBMSG_TYPE_STRING
},
72 [INSTANCE_ATTR_STDOUT
] = { "stdout", BLOBMSG_TYPE_BOOL
},
73 [INSTANCE_ATTR_STDERR
] = { "stderr", BLOBMSG_TYPE_BOOL
},
74 [INSTANCE_ATTR_NO_NEW_PRIVS
] = { "no_new_privs", BLOBMSG_TYPE_BOOL
},
75 [INSTANCE_ATTR_JAIL
] = { "jail", BLOBMSG_TYPE_TABLE
},
76 [INSTANCE_ATTR_TRACE
] = { "trace", BLOBMSG_TYPE_BOOL
},
77 [INSTANCE_ATTR_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_STRING
},
92 static const struct blobmsg_policy jail_attr
[__JAIL_ATTR_MAX
] = {
93 [JAIL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
94 [JAIL_ATTR_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
95 [JAIL_ATTR_PROCFS
] = { "procfs", BLOBMSG_TYPE_BOOL
},
96 [JAIL_ATTR_SYSFS
] = { "sysfs", BLOBMSG_TYPE_BOOL
},
97 [JAIL_ATTR_UBUS
] = { "ubus", BLOBMSG_TYPE_BOOL
},
98 [JAIL_ATTR_LOG
] = { "log", BLOBMSG_TYPE_BOOL
},
99 [JAIL_ATTR_RONLY
] = { "ronly", BLOBMSG_TYPE_BOOL
},
100 [JAIL_ATTR_MOUNT
] = { "mount", BLOBMSG_TYPE_TABLE
},
103 struct instance_netdev
{
104 struct blobmsg_list_node node
;
108 struct instance_file
{
109 struct blobmsg_list_node node
;
118 static const struct rlimit_name rlimit_names
[] = {
120 { "core", RLIMIT_CORE
},
121 { "cpu", RLIMIT_CPU
},
122 { "data", RLIMIT_DATA
},
123 { "fsize", RLIMIT_FSIZE
},
124 { "memlock", RLIMIT_MEMLOCK
},
125 { "msgqueue", RLIMIT_MSGQUEUE
},
126 { "nice", RLIMIT_NICE
},
127 { "nofile", RLIMIT_NOFILE
},
128 { "nproc", RLIMIT_NPROC
},
129 { "rss", RLIMIT_RSS
},
130 { "rtprio", RLIMIT_RTPRIO
},
131 { "sigpending", RLIMIT_SIGPENDING
},
132 { "stack", RLIMIT_STACK
},
136 static char trace
[] = "/sbin/utrace";
138 static void closefd(int fd
)
140 if (fd
> STDERR_FILENO
)
145 instance_limits(const char *limit
, const char *value
)
149 unsigned long cur
, max
;
151 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
152 if (strcmp(rlimit_names
[i
].name
, limit
))
154 if (!strcmp(value
, "unlimited")) {
155 rlim
.rlim_cur
= RLIM_INFINITY
;
156 rlim
.rlim_max
= RLIM_INFINITY
;
158 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
164 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
171 setrlimit(rlimit_names
[i
].resource
, &rlim
);
177 jail_run(struct service_instance
*in
, char **argv
)
179 struct blobmsg_list_node
*var
;
180 struct jail
*jail
= &in
->jail
;
183 argv
[argc
++] = "/sbin/ujail";
187 argv
[argc
++] = jail
->name
;
190 if (jail
->hostname
) {
192 argv
[argc
++] = jail
->hostname
;
197 argv
[argc
++] = in
->seccomp
;
200 if (in
->no_new_privs
)
218 blobmsg_list_for_each(&jail
->mount
, var
) {
219 const char *type
= blobmsg_data(var
->data
);
225 argv
[argc
++] = (char *) blobmsg_name(var
->data
);
234 instance_run(struct service_instance
*in
, int _stdout
, int _stderr
)
236 struct blobmsg_list_node
*var
;
237 struct blob_attr
*cur
;
240 int argc
= 1; /* NULL terminated */
242 bool seccomp
= !in
->trace
&& !in
->has_jail
&& in
->seccomp
;
243 bool setlbf
= _stdout
>= 0;
246 setpriority(PRIO_PROCESS
, 0, in
->nice
);
248 blobmsg_for_each_attr(cur
, in
->command
, rem
)
251 blobmsg_list_for_each(&in
->env
, var
)
252 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
255 setenv("SECCOMP_FILE", in
->seccomp
, 1);
257 if ((seccomp
|| setlbf
) && asprintf(&ld_preload
, "LD_PRELOAD=%s%s%s",
258 seccomp
? "/lib/libpreload-seccomp.so" : "",
259 seccomp
&& setlbf
? ":" : "",
260 setlbf
? "/lib/libsetlbf.so" : "") > 0)
263 blobmsg_list_for_each(&in
->limits
, var
)
264 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
269 argv
= alloca(sizeof(char *) * (argc
+ in
->jail
.argc
));
273 argv
[argc
++] = trace
;
276 argc
= jail_run(in
, argv
);
278 blobmsg_for_each_attr(cur
, in
->command
, rem
)
279 argv
[argc
++] = blobmsg_data(cur
);
283 _stdin
= open("/dev/null", O_RDONLY
);
286 _stdout
= open("/dev/null", O_WRONLY
);
289 _stderr
= open("/dev/null", O_WRONLY
);
292 dup2(_stdin
, STDIN_FILENO
);
296 dup2(_stdout
, STDOUT_FILENO
);
300 dup2(_stderr
, STDERR_FILENO
);
304 if (in
->gid
&& setgid(in
->gid
)) {
305 ERROR("failed to set group id %d: %d (%s)\n", in
->gid
, errno
, strerror(errno
));
308 if (in
->uid
&& setuid(in
->uid
)) {
309 ERROR("failed to set user id %d: %d (%s)\n", in
->uid
, errno
, strerror(errno
));
313 execvp(argv
[0], argv
);
318 instance_free_stdio(struct service_instance
*in
)
320 if (in
->_stdout
.fd
.fd
> -1) {
321 ustream_free(&in
->_stdout
.stream
);
322 close(in
->_stdout
.fd
.fd
);
323 in
->_stdout
.fd
.fd
= -1;
326 if (in
->_stderr
.fd
.fd
> -1) {
327 ustream_free(&in
->_stderr
.stream
);
328 close(in
->_stderr
.fd
.fd
);
329 in
->_stderr
.fd
.fd
= -1;
334 instance_start(struct service_instance
*in
)
337 int opipe
[2] = { -1, -1 };
338 int epipe
[2] = { -1, -1 };
340 if (!avl_is_empty(&in
->errors
.avl
)) {
341 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
345 if (in
->proc
.pending
)
348 instance_free_stdio(in
);
349 if (in
->_stdout
.fd
.fd
> -2) {
351 ULOG_WARN("pipe() failed: %d (%s)\n", errno
, strerror(errno
));
352 opipe
[0] = opipe
[1] = -1;
356 if (in
->_stderr
.fd
.fd
> -2) {
358 ULOG_WARN("pipe() failed: %d (%s)\n", errno
, strerror(errno
));
359 epipe
[0] = epipe
[1] = -1;
364 in
->halt
= !in
->respawn
;
377 instance_run(in
, opipe
[1], epipe
[1]);
381 DEBUG(2, "Started instance %s::%s\n", in
->srv
->name
, in
->name
);
383 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
384 uloop_process_add(&in
->proc
);
387 ustream_fd_init(&in
->_stdout
, opipe
[0]);
392 ustream_fd_init(&in
->_stderr
, epipe
[0]);
396 service_event("instance.start", in
->srv
->name
, in
->name
);
400 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
402 char *newline
, *str
, *arg0
, ident
[32];
405 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
406 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
407 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, ident
);
410 str
= ustream_get_read_buf(s
, NULL
);
414 newline
= strchr(str
, '\n');
419 ulog(prio
, "%s\n", str
);
421 len
= newline
+ 1 - str
;
422 ustream_consume(s
, len
);
425 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
429 instance_stdout(struct ustream
*s
, int bytes
)
431 instance_stdio(s
, LOG_INFO
,
432 container_of(s
, struct service_instance
, _stdout
.stream
));
436 instance_stderr(struct ustream
*s
, int bytes
)
438 instance_stdio(s
, LOG_ERR
,
439 container_of(s
, struct service_instance
, _stderr
.stream
));
443 instance_timeout(struct uloop_timeout
*t
)
445 struct service_instance
*in
;
447 in
= container_of(t
, struct service_instance
, timeout
);
449 if (!in
->halt
&& (in
->restart
|| in
->respawn
))
454 instance_exit(struct uloop_process
*p
, int ret
)
456 struct service_instance
*in
;
460 in
= container_of(p
, struct service_instance
, proc
);
462 clock_gettime(CLOCK_MONOTONIC
, &tp
);
463 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
465 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
469 uloop_timeout_cancel(&in
->timeout
);
472 } else if (in
->restart
) {
474 } else if (in
->respawn
) {
475 if (runtime
< in
->respawn_threshold
)
478 in
->respawn_count
= 0;
479 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
480 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
481 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
482 in
->restart
= in
->respawn
= 0;
485 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
488 service_event("instance.stop", in
->srv
->name
, in
->name
);
492 instance_stop(struct service_instance
*in
)
494 if (!in
->proc
.pending
)
497 in
->restart
= in
->respawn
= false;
498 kill(in
->proc
.pid
, SIGTERM
);
502 instance_restart(struct service_instance
*in
)
504 if (!in
->proc
.pending
)
508 kill(in
->proc
.pid
, SIGTERM
);
512 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
517 if (!blob_attr_equal(in
->command
, in_new
->command
))
520 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
523 if (!blobmsg_list_equal(&in
->data
, &in_new
->data
))
526 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
529 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
532 if (in
->nice
!= in_new
->nice
)
535 if (in
->uid
!= in_new
->uid
)
538 if (in
->gid
!= in_new
->gid
)
541 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
544 if (!blobmsg_list_equal(&in
->jail
.mount
, &in_new
->jail
.mount
))
547 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
554 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
556 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
557 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
559 return n1
->ifindex
== n2
->ifindex
;
563 instance_netdev_update(struct blobmsg_list_node
*l
)
565 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
567 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
571 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
573 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
574 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
576 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
580 instance_file_update(struct blobmsg_list_node
*l
)
582 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
587 memset(f
->md5
, 0, sizeof(f
->md5
));
589 fd
= open(l
->avl
.key
, O_RDONLY
);
595 len
= read(fd
, buf
, sizeof(buf
));
605 md5_hash(buf
, len
, &md5
);
608 md5_end(f
->md5
, &md5
);
613 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
618 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
622 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
624 struct blobmsg_list_node
*node
;
629 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
632 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
634 blobmsg_list_for_each(l
, node
)
641 instance_jail_parse(struct service_instance
*in
, struct blob_attr
*attr
)
643 struct blob_attr
*tb
[__JAIL_ATTR_MAX
];
644 struct jail
*jail
= &in
->jail
;
647 if (stat("/sbin/ujail", &s
))
650 blobmsg_parse(jail_attr
, __JAIL_ATTR_MAX
, tb
,
651 blobmsg_data(attr
), blobmsg_data_len(attr
));
655 if (tb
[JAIL_ATTR_NAME
]) {
656 jail
->name
= blobmsg_get_string(tb
[JAIL_ATTR_NAME
]);
659 if (tb
[JAIL_ATTR_HOSTNAME
]) {
660 jail
->hostname
= blobmsg_get_string(tb
[JAIL_ATTR_HOSTNAME
]);
663 if (tb
[JAIL_ATTR_PROCFS
]) {
664 jail
->procfs
= blobmsg_get_bool(tb
[JAIL_ATTR_PROCFS
]);
667 if (tb
[JAIL_ATTR_SYSFS
]) {
668 jail
->sysfs
= blobmsg_get_bool(tb
[JAIL_ATTR_SYSFS
]);
671 if (tb
[JAIL_ATTR_UBUS
]) {
672 jail
->ubus
= blobmsg_get_bool(tb
[JAIL_ATTR_UBUS
]);
675 if (tb
[JAIL_ATTR_LOG
]) {
676 jail
->log
= blobmsg_get_bool(tb
[JAIL_ATTR_LOG
]);
679 if (tb
[JAIL_ATTR_RONLY
]) {
680 jail
->ronly
= blobmsg_get_bool(tb
[JAIL_ATTR_RONLY
]);
683 if (tb
[JAIL_ATTR_MOUNT
]) {
684 struct blob_attr
*cur
;
687 blobmsg_for_each_attr(cur
, tb
[JAIL_ATTR_MOUNT
], rem
)
689 instance_fill_array(&jail
->mount
, tb
[JAIL_ATTR_MOUNT
], NULL
, false);
698 instance_config_parse(struct service_instance
*in
)
700 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
701 struct blob_attr
*cur
, *cur2
;
705 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
706 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
708 cur
= tb
[INSTANCE_ATTR_COMMAND
];
712 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
715 blobmsg_for_each_attr(cur2
, cur
, rem
) {
724 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
726 uint32_t vals
[3] = { 3600, 5, 5};
728 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
729 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
731 vals
[i
] = atoi(blobmsg_get_string(cur2
));
735 in
->respawn_count
= 0;
736 in
->respawn_threshold
= vals
[0];
737 in
->respawn_timeout
= vals
[1];
738 in
->respawn_retry
= vals
[2];
740 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
741 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
742 trigger_add(in
->trigger
, in
);
745 if (tb
[INSTANCE_ATTR_WATCH
]) {
746 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
747 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
749 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
750 watch_add(blobmsg_get_string(cur2
), in
);
754 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
755 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
756 if (in
->nice
< -20 || in
->nice
> 20)
760 if (tb
[INSTANCE_ATTR_USER
]) {
761 struct passwd
*p
= getpwnam(blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]));
768 if (tb
[INSTANCE_ATTR_TRACE
])
769 in
->trace
= blobmsg_get_bool(tb
[INSTANCE_ATTR_TRACE
]);
771 if (tb
[INSTANCE_ATTR_NO_NEW_PRIVS
])
772 in
->no_new_privs
= blobmsg_get_bool(tb
[INSTANCE_ATTR_NO_NEW_PRIVS
]);
774 if (!in
->trace
&& tb
[INSTANCE_ATTR_SECCOMP
]) {
775 char *seccomp
= blobmsg_get_string(tb
[INSTANCE_ATTR_SECCOMP
]);
778 if (stat(seccomp
, &s
))
779 ERROR("%s: not starting seccomp as %s is missing\n", in
->name
, seccomp
);
781 in
->seccomp
= seccomp
;
783 if (!in
->trace
&& tb
[INSTANCE_ATTR_JAIL
])
784 in
->has_jail
= instance_jail_parse(in
, tb
[INSTANCE_ATTR_JAIL
]);
786 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
787 in
->_stdout
.fd
.fd
= -1;
789 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
790 in
->_stderr
.fd
.fd
= -1;
792 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
794 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
797 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
800 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
803 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
806 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
813 instance_config_cleanup(struct service_instance
*in
)
815 blobmsg_list_free(&in
->env
);
816 blobmsg_list_free(&in
->data
);
817 blobmsg_list_free(&in
->netdev
);
818 blobmsg_list_free(&in
->file
);
819 blobmsg_list_free(&in
->limits
);
820 blobmsg_list_free(&in
->errors
);
821 blobmsg_list_free(&in
->jail
.mount
);
825 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
827 instance_config_cleanup(in
);
828 blobmsg_list_move(&in
->env
, &in_src
->env
);
829 blobmsg_list_move(&in
->data
, &in_src
->data
);
830 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
831 blobmsg_list_move(&in
->file
, &in_src
->file
);
832 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
833 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
834 blobmsg_list_move(&in
->jail
.mount
, &in_src
->jail
.mount
);
835 in
->trigger
= in_src
->trigger
;
836 in
->command
= in_src
->command
;
837 in
->name
= in_src
->name
;
838 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
841 in
->config
= in_src
->config
;
842 in_src
->config
= NULL
;
846 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
848 bool changed
= instance_config_changed(in
, in_new
);
849 bool running
= in
->proc
.pending
;
851 if (!changed
&& running
)
856 instance_config_move(in
, in_new
);
859 instance_restart(in
);
860 instance_config_move(in
, in_new
);
861 /* restart happens in the child callback handler */
867 instance_free(struct service_instance
*in
)
869 instance_free_stdio(in
);
870 uloop_process_delete(&in
->proc
);
871 uloop_timeout_cancel(&in
->timeout
);
874 instance_config_cleanup(in
);
880 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
882 config
= blob_memdup(config
);
884 in
->name
= blobmsg_name(config
);
886 in
->timeout
.cb
= instance_timeout
;
887 in
->proc
.cb
= instance_exit
;
889 in
->_stdout
.fd
.fd
= -2;
890 in
->_stdout
.stream
.string_data
= true;
891 in
->_stdout
.stream
.notify_read
= instance_stdout
;
893 in
->_stderr
.fd
.fd
= -2;
894 in
->_stderr
.stream
.string_data
= true;
895 in
->_stderr
.stream
.notify_read
= instance_stderr
;
897 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
898 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
899 blobmsg_list_simple_init(&in
->env
);
900 blobmsg_list_simple_init(&in
->data
);
901 blobmsg_list_simple_init(&in
->limits
);
902 blobmsg_list_simple_init(&in
->errors
);
903 blobmsg_list_simple_init(&in
->jail
.mount
);
904 in
->valid
= instance_config_parse(in
);
907 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
914 i
= blobmsg_open_table(b
, in
->name
);
915 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
916 if (in
->proc
.pending
)
917 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
918 blobmsg_add_blob(b
, in
->command
);
920 if (!avl_is_empty(&in
->errors
.avl
)) {
921 struct blobmsg_list_node
*var
;
922 void *e
= blobmsg_open_array(b
, "errors");
923 blobmsg_list_for_each(&in
->errors
, var
)
924 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
925 blobmsg_close_table(b
, e
);
928 if (!avl_is_empty(&in
->env
.avl
)) {
929 struct blobmsg_list_node
*var
;
930 void *e
= blobmsg_open_table(b
, "env");
931 blobmsg_list_for_each(&in
->env
, var
)
932 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
933 blobmsg_close_table(b
, e
);
936 if (!avl_is_empty(&in
->data
.avl
)) {
937 struct blobmsg_list_node
*var
;
938 void *e
= blobmsg_open_table(b
, "data");
939 blobmsg_list_for_each(&in
->data
, var
)
940 blobmsg_add_blob(b
, var
->data
);
941 blobmsg_close_table(b
, e
);
944 if (!avl_is_empty(&in
->limits
.avl
)) {
945 struct blobmsg_list_node
*var
;
946 void *e
= blobmsg_open_table(b
, "limits");
947 blobmsg_list_for_each(&in
->limits
, var
)
948 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
949 blobmsg_close_table(b
, e
);
953 void *r
= blobmsg_open_table(b
, "respawn");
954 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
955 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
956 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
957 blobmsg_close_table(b
, r
);
961 blobmsg_add_u8(b
, "trace", true);
963 if (in
->no_new_privs
)
964 blobmsg_add_u8(b
, "no_new_privs", true);
967 blobmsg_add_string(b
, "seccomp", in
->seccomp
);
970 void *r
= blobmsg_open_table(b
, "jail");
972 blobmsg_add_string(b
, "name", in
->jail
.name
);
973 if (in
->jail
.hostname
)
974 blobmsg_add_string(b
, "hostname", in
->jail
.hostname
);
975 blobmsg_add_u8(b
, "procfs", in
->jail
.procfs
);
976 blobmsg_add_u8(b
, "sysfs", in
->jail
.sysfs
);
977 blobmsg_add_u8(b
, "ubus", in
->jail
.ubus
);
978 blobmsg_add_u8(b
, "log", in
->jail
.log
);
979 blobmsg_add_u8(b
, "ronly", in
->jail
.ronly
);
980 blobmsg_close_table(b
, r
);
981 if (!avl_is_empty(&in
->jail
.mount
.avl
)) {
982 struct blobmsg_list_node
*var
;
983 void *e
= blobmsg_open_table(b
, "mount");
984 blobmsg_list_for_each(&in
->jail
.mount
, var
)
985 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
986 blobmsg_close_table(b
, e
);
990 if (verbose
&& in
->trigger
)
991 blobmsg_add_blob(b
, in
->trigger
);
993 blobmsg_close_table(b
, i
);