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>
32 #include <libubox/md5.h>
39 #define UJAIL_BIN_PATH "/sbin/ujail"
42 INSTANCE_ATTR_COMMAND,
47 INSTANCE_ATTR_TRIGGER,
48 INSTANCE_ATTR_RESPAWN,
57 INSTANCE_ATTR_NO_NEW_PRIVS,
60 INSTANCE_ATTR_SECCOMP,
61 INSTANCE_ATTR_PIDFILE,
62 INSTANCE_ATTR_RELOADSIG,
63 INSTANCE_ATTR_TERMTIMEOUT,
64 INSTANCE_ATTR_FACILITY,
68 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
69 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
70 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
71 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
72 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
73 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
74 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
75 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
76 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
77 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
78 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
79 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
80 [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
81 [INSTANCE_ATTR_GROUP] = { "group", BLOBMSG_TYPE_STRING },
82 [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
83 [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
84 [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
85 [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
86 [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
87 [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
88 [INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
89 [INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
90 [INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
91 [INSTANCE_ATTR_FACILITY] = { "facility", BLOBMSG_TYPE_STRING },
107 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
108 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
109 [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
110 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
111 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
112 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
113 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
114 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
115 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
116 [JAIL_ATTR_NETNS] = { "netns", BLOBMSG_TYPE_BOOL },
119 struct instance_netdev {
120 struct blobmsg_list_node node;
124 struct instance_file {
125 struct blobmsg_list_node node;
134 static const struct rlimit_name rlimit_names[] = {
136 { "core", RLIMIT_CORE },
137 { "cpu", RLIMIT_CPU },
138 { "data", RLIMIT_DATA },
139 { "fsize", RLIMIT_FSIZE },
140 { "memlock", RLIMIT_MEMLOCK },
141 { "nofile", RLIMIT_NOFILE },
142 { "nproc", RLIMIT_NPROC },
143 { "rss", RLIMIT_RSS },
144 { "stack", RLIMIT_STACK },
146 { "nice", RLIMIT_NICE },
147 { "rtprio", RLIMIT_RTPRIO },
148 { "msgqueue", RLIMIT_MSGQUEUE },
149 { "sigpending", RLIMIT_SIGPENDING },
154 static void closefd(int fd)
156 if (fd > STDERR_FILENO)
160 /* convert a string into numeric syslog facility or return -1 if no match found */
162 syslog_facility_str_to_int(const char *facility)
164 CODE *p = facilitynames;
166 while (p->c_name && strcasecmp(p->c_name, facility))
173 instance_limits(const char *limit, const char *value)
177 unsigned long cur, max;
179 for (i = 0; rlimit_names[i].name != NULL; i++) {
180 if (strcmp(rlimit_names[i].name, limit))
182 if (!strcmp(value, "unlimited")) {
183 rlim.rlim_cur = RLIM_INFINITY;
184 rlim.rlim_max = RLIM_INFINITY;
186 if (getrlimit(rlimit_names[i].resource, &rlim))
192 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
199 setrlimit(rlimit_names[i].resource, &rlim);
205 jail_run(struct service_instance *in, char **argv)
207 struct blobmsg_list_node *var;
208 struct jail *jail = &in->jail;
211 argv[argc++] = UJAIL_BIN_PATH;
215 argv[argc++] = jail->name;
218 if (jail->hostname) {
220 argv[argc++] = jail->hostname;
225 argv[argc++] = in->seccomp;
230 argv[argc++] = in->user;
235 argv[argc++] = in->group;
238 if (in->no_new_privs)
259 blobmsg_list_for_each(&jail->mount, var) {
260 const char *type = blobmsg_data(var->data);
266 argv[argc++] = (char *) blobmsg_name(var->data);
275 instance_removepid(struct service_instance *in) {
278 if (unlink(in->pidfile)) {
279 ERROR("Failed to remove pidfile: %s: %m\n", in->pidfile);
286 instance_writepid(struct service_instance *in)
293 _pidfile = fopen(in->pidfile, "w");
294 if (_pidfile == NULL) {
295 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
298 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
299 ERROR("failed to write pidfile: %s: %m", in->pidfile);
303 if (fclose(_pidfile)) {
304 ERROR("failed to close pidfile: %s: %m", in->pidfile);
312 instance_run(struct service_instance *in, int _stdout, int _stderr)
314 struct blobmsg_list_node *var;
315 struct blob_attr *cur;
317 int argc = 1; /* NULL terminated */
319 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
320 bool setlbf = _stdout >= 0;
323 setpriority(PRIO_PROCESS, 0, in->nice);
325 blobmsg_for_each_attr(cur, in->command, rem)
328 blobmsg_list_for_each(&in->env, var)
329 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
332 setenv("SECCOMP_FILE", in->seccomp, 1);
335 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
337 blobmsg_list_for_each(&in->limits, var)
338 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
340 if (in->trace || seccomp)
343 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
346 #ifdef SECCOMP_SUPPORT
348 argv[argc++] = "/sbin/utrace";
350 argv[argc++] = "/sbin/seccomp-trace";
352 if (in->trace || seccomp)
353 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
357 argc = jail_run(in, argv);
358 if (argc != in->jail.argc)
359 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
360 in->jail.argc, argc, in->srv->name, in->name);
363 blobmsg_for_each_attr(cur, in->command, rem)
364 argv[argc++] = blobmsg_data(cur);
368 _stdin = open("/dev/null", O_RDONLY);
371 _stdout = open("/dev/null", O_WRONLY);
374 _stderr = open("/dev/null", O_WRONLY);
377 dup2(_stdin, STDIN_FILENO);
381 dup2(_stdout, STDOUT_FILENO);
385 dup2(_stderr, STDERR_FILENO);
389 if (!in->has_jail && in->user && in->pw_gid && initgroups(in->user, in->pw_gid)) {
390 ERROR("failed to initgroups() for user %s: %m\n", in->user);
393 if (!in->has_jail && in->gr_gid && setgid(in->gr_gid)) {
394 ERROR("failed to set group id %d: %m\n", in->gr_gid);
397 if (!in->has_jail && in->uid && setuid(in->uid)) {
398 ERROR("failed to set user id %d: %m\n", in->uid);
402 execvp(argv[0], argv);
407 instance_free_stdio(struct service_instance *in)
409 if (in->_stdout.fd.fd > -1) {
410 ustream_free(&in->_stdout.stream);
411 close(in->_stdout.fd.fd);
412 in->_stdout.fd.fd = -1;
415 if (in->_stderr.fd.fd > -1) {
416 ustream_free(&in->_stderr.stream);
417 close(in->_stderr.fd.fd);
418 in->_stderr.fd.fd = -1;
423 instance_start(struct service_instance *in)
426 int opipe[2] = { -1, -1 };
427 int epipe[2] = { -1, -1 };
429 if (!avl_is_empty(&in->errors.avl)) {
430 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
435 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
439 if (in->proc.pending) {
445 instance_free_stdio(in);
446 if (in->_stdout.fd.fd > -2) {
448 ULOG_WARN("pipe() failed: %m\n");
449 opipe[0] = opipe[1] = -1;
453 if (in->_stderr.fd.fd > -2) {
455 ULOG_WARN("pipe() failed: %m\n");
456 epipe[0] = epipe[1] = -1;
474 instance_run(in, opipe[1], epipe[1]);
478 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
480 instance_writepid(in);
481 clock_gettime(CLOCK_MONOTONIC, &in->start);
482 uloop_process_add(&in->proc);
485 ustream_fd_init(&in->_stdout, opipe[0]);
487 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
491 ustream_fd_init(&in->_stderr, epipe[0]);
493 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
496 service_event("instance.start", in->srv->name, in->name);
500 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
502 char *newline, *str, *arg0, ident[32];
505 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
506 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
507 ulog_open(ULOG_SYSLOG, in->syslog_facility, ident);
510 str = ustream_get_read_buf(s, &len);
514 newline = memchr(str, '\n', len);
515 if (!newline && (s->r.buffer_len != len))
520 len = newline + 1 - str;
522 ulog(prio, "%s\n", str);
524 ustream_consume(s, len);
527 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
531 instance_stdout(struct ustream *s, int bytes)
533 instance_stdio(s, LOG_INFO,
534 container_of(s, struct service_instance, _stdout.stream));
538 instance_stderr(struct ustream *s, int bytes)
540 instance_stdio(s, LOG_ERR,
541 container_of(s, struct service_instance, _stderr.stream));
545 instance_timeout(struct uloop_timeout *t)
547 struct service_instance *in;
549 in = container_of(t, struct service_instance, timeout);
552 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
553 in->srv->name, in->name, in->proc.pid);
554 kill(in->proc.pid, SIGKILL);
555 } else if (in->restart || in->respawn)
560 instance_delete(struct service_instance *in)
562 struct service *s = in->srv;
564 avl_delete(&s->instances.avl, &in->node.avl);
570 instance_exit_code(int ret)
572 if (WIFEXITED(ret)) {
573 return WEXITSTATUS(ret);
576 if (WIFSIGNALED(ret)) {
577 return SIGNALLED_OFFSET + WTERMSIG(ret);
580 if (WIFSTOPPED(ret)) {
581 return WSTOPSIG(ret);
588 instance_exit(struct uloop_process *p, int ret)
590 struct service_instance *in;
594 in = container_of(p, struct service_instance, proc);
596 clock_gettime(CLOCK_MONOTONIC, &tp);
597 runtime = tp.tv_sec - in->start.tv_sec;
599 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
601 in->exit_code = instance_exit_code(ret);
602 uloop_timeout_cancel(&in->timeout);
603 service_event("instance.stop", in->srv->name, in->name);
606 instance_removepid(in);
611 } else if (in->restart) {
613 } else if (in->respawn) {
614 if (runtime < in->respawn_threshold)
617 in->respawn_count = 0;
618 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
619 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
620 in->srv->name, in->name, in->respawn_count, runtime);
621 in->restart = in->respawn = 0;
623 service_event("instance.fail", in->srv->name, in->name);
625 service_event("instance.respawn", in->srv->name, in->name);
626 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
632 instance_stop(struct service_instance *in, bool halt)
634 if (!in->proc.pending) {
640 in->restart = in->respawn = false;
641 kill(in->proc.pid, SIGTERM);
642 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
646 instance_restart(struct service_instance *in)
648 if (!in->proc.pending)
651 if (in->reload_signal) {
652 kill(in->proc.pid, in->reload_signal);
658 kill(in->proc.pid, SIGTERM);
659 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
662 static bool string_changed(const char *a, const char *b)
664 return !((!a && !b) || (a && b && !strcmp(a, b)));
668 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
673 if (!blob_attr_equal(in->command, in_new->command))
676 if (!blobmsg_list_equal(&in->env, &in_new->env))
679 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
682 if (!blobmsg_list_equal(&in->file, &in_new->file))
685 if (in->nice != in_new->nice)
688 if (in->syslog_facility != in_new->syslog_facility)
691 if (string_changed(in->user, in_new->user))
694 if (string_changed(in->group, in_new->group))
697 if (in->uid != in_new->uid)
700 if (in->pw_gid != in_new->pw_gid)
703 if (string_changed(in->pidfile, in_new->pidfile))
706 if (in->respawn_retry != in_new->respawn_retry)
708 if (in->respawn_threshold != in_new->respawn_threshold)
710 if (in->respawn_timeout != in_new->respawn_timeout)
713 if ((!in->seccomp && in_new->seccomp) ||
714 (in->seccomp && !in_new->seccomp) ||
715 (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
718 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
721 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
724 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
731 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
733 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
734 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
736 return n1->ifindex == n2->ifindex;
740 instance_netdev_update(struct blobmsg_list_node *l)
742 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
744 n->ifindex = if_nametoindex(n->node.avl.key);
748 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
750 struct instance_file *f1 = container_of(l1, struct instance_file, node);
751 struct instance_file *f2 = container_of(l2, struct instance_file, node);
753 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
757 instance_file_update(struct blobmsg_list_node *l)
759 struct instance_file *f = container_of(l, struct instance_file, node);
764 memset(f->md5, 0, sizeof(f->md5));
766 fd = open(l->avl.key, O_RDONLY);
772 len = read(fd, buf, sizeof(buf));
782 md5_hash(buf, len, &md5);
785 md5_end(f->md5, &md5);
790 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
795 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
799 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
801 struct blobmsg_list_node *node;
806 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
809 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
811 blobmsg_list_for_each(l, node)
818 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
820 struct blob_attr *tb[__JAIL_ATTR_MAX];
821 struct jail *jail = &in->jail;
825 r = stat(UJAIL_BIN_PATH, &s);
827 ERROR("unable to find %s: %m (%d)\n", UJAIL_BIN_PATH, r);
831 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
832 blobmsg_data(attr), blobmsg_data_len(attr));
836 if (tb[JAIL_ATTR_NAME]) {
837 jail->name = strdup(blobmsg_get_string(tb[JAIL_ATTR_NAME]));
840 if (tb[JAIL_ATTR_HOSTNAME]) {
841 jail->hostname = strdup(blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]));
844 if (tb[JAIL_ATTR_PROCFS]) {
845 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
848 if (tb[JAIL_ATTR_SYSFS]) {
849 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
852 if (tb[JAIL_ATTR_UBUS]) {
853 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
856 if (tb[JAIL_ATTR_LOG]) {
857 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
860 if (tb[JAIL_ATTR_RONLY]) {
861 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
864 if (tb[JAIL_ATTR_NETNS]) {
865 jail->netns = blobmsg_get_bool(tb[JAIL_ATTR_NETNS]);
868 if (tb[JAIL_ATTR_MOUNT]) {
869 struct blob_attr *cur;
872 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
874 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
885 if (in->no_new_privs)
892 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
894 struct blob_attr *cur, *cur2;
898 cur = tb[INSTANCE_ATTR_COMMAND];
904 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
907 blobmsg_for_each_attr(cur2, cur, rem) {
917 instance_config_parse(struct service_instance *in)
919 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
920 struct blob_attr *cur, *cur2;
923 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
924 blobmsg_data(in->config), blobmsg_data_len(in->config));
926 if (!instance_config_parse_command(in, tb))
929 if (tb[INSTANCE_ATTR_TERMTIMEOUT])
930 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
931 if (tb[INSTANCE_ATTR_RESPAWN]) {
933 uint32_t vals[3] = { 3600, 5, 5};
935 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
936 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
938 vals[i] = atoi(blobmsg_get_string(cur2));
942 in->respawn_count = 0;
943 in->respawn_threshold = vals[0];
944 in->respawn_timeout = vals[1];
945 in->respawn_retry = vals[2];
947 if (tb[INSTANCE_ATTR_TRIGGER]) {
948 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
949 trigger_add(in->trigger, in);
952 if (tb[INSTANCE_ATTR_WATCH]) {
953 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
954 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
956 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
957 watch_add(blobmsg_get_string(cur2), in);
961 if ((cur = tb[INSTANCE_ATTR_NICE])) {
962 in->nice = (int8_t) blobmsg_get_u32(cur);
963 if (in->nice < -20 || in->nice > 20)
967 if (tb[INSTANCE_ATTR_USER]) {
968 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
969 struct passwd *p = getpwnam(user);
971 in->user = strdup(user);
973 in->gr_gid = in->pw_gid = p->pw_gid;
977 if (tb[INSTANCE_ATTR_GROUP]) {
978 const char *group = blobmsg_get_string(tb[INSTANCE_ATTR_GROUP]);
979 struct group *p = getgrnam(group);
981 in->group = strdup(group);
982 in->gr_gid = p->gr_gid;
986 if (tb[INSTANCE_ATTR_TRACE])
987 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
989 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
990 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
992 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
993 in->seccomp = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]));
995 if (tb[INSTANCE_ATTR_PIDFILE]) {
996 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
998 in->pidfile = strdup(pidfile);
1001 if (tb[INSTANCE_ATTR_RELOADSIG])
1002 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
1004 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
1005 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
1007 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
1008 in->_stdout.fd.fd = -1;
1010 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
1011 in->_stderr.fd.fd = -1;
1013 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
1015 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
1018 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
1021 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
1024 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
1027 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
1030 if (tb[INSTANCE_ATTR_FACILITY]) {
1031 int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1032 if (facility != -1) {
1033 in->syslog_facility = facility;
1034 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1036 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1043 instance_config_cleanup(struct service_instance *in)
1045 blobmsg_list_free(&in->env);
1046 blobmsg_list_free(&in->data);
1047 blobmsg_list_free(&in->netdev);
1048 blobmsg_list_free(&in->file);
1049 blobmsg_list_free(&in->limits);
1050 blobmsg_list_free(&in->errors);
1051 blobmsg_list_free(&in->jail.mount);
1055 instance_config_move_strdup(char **dst, char *src)
1069 instance_config_move(struct service_instance *in, struct service_instance *in_src)
1071 instance_config_cleanup(in);
1072 blobmsg_list_move(&in->env, &in_src->env);
1073 blobmsg_list_move(&in->data, &in_src->data);
1074 blobmsg_list_move(&in->netdev, &in_src->netdev);
1075 blobmsg_list_move(&in->file, &in_src->file);
1076 blobmsg_list_move(&in->limits, &in_src->limits);
1077 blobmsg_list_move(&in->errors, &in_src->errors);
1078 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
1079 in->trigger = in_src->trigger;
1080 in->command = in_src->command;
1081 in->respawn = in_src->respawn;
1082 in->respawn_retry = in_src->respawn_retry;
1083 in->respawn_threshold = in_src->respawn_threshold;
1084 in->respawn_timeout = in_src->respawn_timeout;
1085 in->name = in_src->name;
1086 in->trace = in_src->trace;
1087 in->node.avl.key = in_src->node.avl.key;
1088 in->syslog_facility = in_src->syslog_facility;
1090 instance_config_move_strdup(&in->pidfile, in_src->pidfile);
1091 instance_config_move_strdup(&in->seccomp, in_src->seccomp);
1092 instance_config_move_strdup(&in->jail.name, in_src->jail.name);
1093 instance_config_move_strdup(&in->jail.hostname, in_src->jail.hostname);
1096 in->config = in_src->config;
1097 in_src->config = NULL;
1101 instance_update(struct service_instance *in, struct service_instance *in_new)
1103 bool changed = instance_config_changed(in, in_new);
1104 bool running = in->proc.pending;
1105 bool stopping = in->halt;
1107 if (!running || stopping) {
1108 instance_config_move(in, in_new);
1112 instance_restart(in);
1113 instance_config_move(in, in_new);
1114 /* restart happens in the child callback handler */
1119 instance_free(struct service_instance *in)
1121 instance_free_stdio(in);
1122 uloop_process_delete(&in->proc);
1123 uloop_timeout_cancel(&in->timeout);
1126 instance_config_cleanup(in);
1130 free(in->jail.name);
1131 free(in->jail.hostname);
1138 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1140 config = blob_memdup(config);
1142 in->name = blobmsg_name(config);
1143 in->config = config;
1144 in->timeout.cb = instance_timeout;
1145 in->proc.cb = instance_exit;
1146 in->term_timeout = 5;
1147 in->syslog_facility = LOG_DAEMON;
1150 in->_stdout.fd.fd = -2;
1151 in->_stdout.stream.string_data = true;
1152 in->_stdout.stream.notify_read = instance_stdout;
1154 in->_stderr.fd.fd = -2;
1155 in->_stderr.stream.string_data = true;
1156 in->_stderr.stream.notify_read = instance_stderr;
1158 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1159 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1160 blobmsg_list_simple_init(&in->env);
1161 blobmsg_list_simple_init(&in->data);
1162 blobmsg_list_simple_init(&in->limits);
1163 blobmsg_list_simple_init(&in->errors);
1164 blobmsg_list_simple_init(&in->jail.mount);
1165 in->valid = instance_config_parse(in);
1168 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1175 i = blobmsg_open_table(b, in->name);
1176 blobmsg_add_u8(b, "running", in->proc.pending);
1177 if (in->proc.pending)
1178 blobmsg_add_u32(b, "pid", in->proc.pid);
1180 blobmsg_add_blob(b, in->command);
1181 blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1182 if (!in->proc.pending)
1183 blobmsg_add_u32(b, "exit_code", in->exit_code);
1185 if (!avl_is_empty(&in->errors.avl)) {
1186 struct blobmsg_list_node *var;
1187 void *e = blobmsg_open_array(b, "errors");
1188 blobmsg_list_for_each(&in->errors, var)
1189 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1190 blobmsg_close_table(b, e);
1193 if (!avl_is_empty(&in->env.avl)) {
1194 struct blobmsg_list_node *var;
1195 void *e = blobmsg_open_table(b, "env");
1196 blobmsg_list_for_each(&in->env, var)
1197 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1198 blobmsg_close_table(b, e);
1201 if (!avl_is_empty(&in->data.avl)) {
1202 struct blobmsg_list_node *var;
1203 void *e = blobmsg_open_table(b, "data");
1204 blobmsg_list_for_each(&in->data, var)
1205 blobmsg_add_blob(b, var->data);
1206 blobmsg_close_table(b, e);
1209 if (!avl_is_empty(&in->limits.avl)) {
1210 struct blobmsg_list_node *var;
1211 void *e = blobmsg_open_table(b, "limits");
1212 blobmsg_list_for_each(&in->limits, var)
1213 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1214 blobmsg_close_table(b, e);
1217 if (in->reload_signal)
1218 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1221 void *r = blobmsg_open_table(b, "respawn");
1222 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1223 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1224 blobmsg_add_u32(b, "retry", in->respawn_retry);
1225 blobmsg_close_table(b, r);
1229 blobmsg_add_u8(b, "trace", true);
1231 if (in->no_new_privs)
1232 blobmsg_add_u8(b, "no_new_privs", true);
1235 blobmsg_add_string(b, "seccomp", in->seccomp);
1238 blobmsg_add_string(b, "pidfile", in->pidfile);
1241 blobmsg_add_string(b, "user", in->user);
1244 blobmsg_add_string(b, "group", in->group);
1247 void *r = blobmsg_open_table(b, "jail");
1249 blobmsg_add_string(b, "name", in->jail.name);
1250 if (in->jail.hostname)
1251 blobmsg_add_string(b, "hostname", in->jail.hostname);
1252 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1253 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1254 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1255 blobmsg_add_u8(b, "log", in->jail.log);
1256 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1257 blobmsg_add_u8(b, "netns", in->jail.netns);
1258 blobmsg_close_table(b, r);
1259 if (!avl_is_empty(&in->jail.mount.avl)) {
1260 struct blobmsg_list_node *var;
1261 void *e = blobmsg_open_table(b, "mount");
1262 blobmsg_list_for_each(&in->jail.mount, var)
1263 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1264 blobmsg_close_table(b, e);
1268 if (verbose && in->trigger)
1269 blobmsg_add_blob(b, in->trigger);
1271 blobmsg_close_table(b, i);