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>
41 INSTANCE_ATTR_COMMAND,
46 INSTANCE_ATTR_TRIGGER,
47 INSTANCE_ATTR_RESPAWN,
56 INSTANCE_ATTR_NO_NEW_PRIVS,
59 INSTANCE_ATTR_SECCOMP,
60 INSTANCE_ATTR_PIDFILE,
61 INSTANCE_ATTR_RELOADSIG,
62 INSTANCE_ATTR_TERMTIMEOUT,
63 INSTANCE_ATTR_FACILITY,
67 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
68 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
69 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
70 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
71 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
72 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
73 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
74 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
75 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
76 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
77 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
78 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
79 [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
80 [INSTANCE_ATTR_GROUP] = { "group", BLOBMSG_TYPE_STRING },
81 [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
82 [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
83 [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
84 [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
85 [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
86 [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
87 [INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
88 [INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
89 [INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
90 [INSTANCE_ATTR_FACILITY] = { "facility", BLOBMSG_TYPE_STRING },
106 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
107 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
108 [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
109 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
110 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
111 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
112 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
113 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
114 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
115 [JAIL_ATTR_NETNS] = { "netns", BLOBMSG_TYPE_BOOL },
118 struct instance_netdev {
119 struct blobmsg_list_node node;
123 struct instance_file {
124 struct blobmsg_list_node node;
133 static const struct rlimit_name rlimit_names[] = {
135 { "core", RLIMIT_CORE },
136 { "cpu", RLIMIT_CPU },
137 { "data", RLIMIT_DATA },
138 { "fsize", RLIMIT_FSIZE },
139 { "memlock", RLIMIT_MEMLOCK },
140 { "nofile", RLIMIT_NOFILE },
141 { "nproc", RLIMIT_NPROC },
142 { "rss", RLIMIT_RSS },
143 { "stack", RLIMIT_STACK },
145 { "nice", RLIMIT_NICE },
146 { "rtprio", RLIMIT_RTPRIO },
147 { "msgqueue", RLIMIT_MSGQUEUE },
148 { "sigpending", RLIMIT_SIGPENDING },
153 static void closefd(int fd)
155 if (fd > STDERR_FILENO)
159 /* convert a string into numeric syslog facility or return -1 if no match found */
161 syslog_facility_str_to_int(const char *facility)
163 CODE *p = facilitynames;
165 while (p->c_name && strcasecmp(p->c_name, facility))
172 instance_limits(const char *limit, const char *value)
176 unsigned long cur, max;
178 for (i = 0; rlimit_names[i].name != NULL; i++) {
179 if (strcmp(rlimit_names[i].name, limit))
181 if (!strcmp(value, "unlimited")) {
182 rlim.rlim_cur = RLIM_INFINITY;
183 rlim.rlim_max = RLIM_INFINITY;
185 if (getrlimit(rlimit_names[i].resource, &rlim))
191 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
198 setrlimit(rlimit_names[i].resource, &rlim);
204 jail_run(struct service_instance *in, char **argv)
206 struct blobmsg_list_node *var;
207 struct jail *jail = &in->jail;
210 argv[argc++] = "/sbin/ujail";
214 argv[argc++] = jail->name;
217 if (jail->hostname) {
219 argv[argc++] = jail->hostname;
224 argv[argc++] = in->seccomp;
229 argv[argc++] = in->user;
234 argv[argc++] = in->group;
237 if (in->no_new_privs)
258 blobmsg_list_for_each(&jail->mount, var) {
259 const char *type = blobmsg_data(var->data);
265 argv[argc++] = (char *) blobmsg_name(var->data);
274 instance_removepid(struct service_instance *in) {
277 if (unlink(in->pidfile)) {
278 ERROR("Failed to remove pidfile: %s: %m\n", in->pidfile);
285 instance_writepid(struct service_instance *in)
292 _pidfile = fopen(in->pidfile, "w");
293 if (_pidfile == NULL) {
294 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
297 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
298 ERROR("failed to write pidfile: %s: %m", in->pidfile);
302 if (fclose(_pidfile)) {
303 ERROR("failed to close pidfile: %s: %m", in->pidfile);
311 instance_run(struct service_instance *in, int _stdout, int _stderr)
313 struct blobmsg_list_node *var;
314 struct blob_attr *cur;
316 int argc = 1; /* NULL terminated */
318 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
319 bool setlbf = _stdout >= 0;
322 setpriority(PRIO_PROCESS, 0, in->nice);
324 blobmsg_for_each_attr(cur, in->command, rem)
327 blobmsg_list_for_each(&in->env, var)
328 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
331 setenv("SECCOMP_FILE", in->seccomp, 1);
334 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
336 blobmsg_list_for_each(&in->limits, var)
337 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
339 if (in->trace || seccomp)
342 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
345 #ifdef SECCOMP_SUPPORT
347 argv[argc++] = "/sbin/utrace";
349 argv[argc++] = "/sbin/seccomp-trace";
351 if (in->trace || seccomp)
352 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
356 argc = jail_run(in, argv);
357 if (argc != in->jail.argc)
358 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
359 in->jail.argc, argc, in->srv->name, in->name);
362 blobmsg_for_each_attr(cur, in->command, rem)
363 argv[argc++] = blobmsg_data(cur);
367 _stdin = open("/dev/null", O_RDONLY);
370 _stdout = open("/dev/null", O_WRONLY);
373 _stderr = open("/dev/null", O_WRONLY);
376 dup2(_stdin, STDIN_FILENO);
380 dup2(_stdout, STDOUT_FILENO);
384 dup2(_stderr, STDERR_FILENO);
388 if (!in->has_jail && in->user && in->pw_gid && initgroups(in->user, in->pw_gid)) {
389 ERROR("failed to initgroups() for user %s: %m\n", in->user);
392 if (!in->has_jail && in->gr_gid && setgid(in->gr_gid)) {
393 ERROR("failed to set group id %d: %m\n", in->gr_gid);
396 if (!in->has_jail && in->uid && setuid(in->uid)) {
397 ERROR("failed to set user id %d: %m\n", in->uid);
401 execvp(argv[0], argv);
406 instance_free_stdio(struct service_instance *in)
408 if (in->_stdout.fd.fd > -1) {
409 ustream_free(&in->_stdout.stream);
410 close(in->_stdout.fd.fd);
411 in->_stdout.fd.fd = -1;
414 if (in->_stderr.fd.fd > -1) {
415 ustream_free(&in->_stderr.stream);
416 close(in->_stderr.fd.fd);
417 in->_stderr.fd.fd = -1;
422 instance_start(struct service_instance *in)
425 int opipe[2] = { -1, -1 };
426 int epipe[2] = { -1, -1 };
428 if (!avl_is_empty(&in->errors.avl)) {
429 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
434 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
438 if (in->proc.pending) {
444 instance_free_stdio(in);
445 if (in->_stdout.fd.fd > -2) {
447 ULOG_WARN("pipe() failed: %m\n");
448 opipe[0] = opipe[1] = -1;
452 if (in->_stderr.fd.fd > -2) {
454 ULOG_WARN("pipe() failed: %m\n");
455 epipe[0] = epipe[1] = -1;
473 instance_run(in, opipe[1], epipe[1]);
477 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
479 instance_writepid(in);
480 clock_gettime(CLOCK_MONOTONIC, &in->start);
481 uloop_process_add(&in->proc);
484 ustream_fd_init(&in->_stdout, opipe[0]);
486 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
490 ustream_fd_init(&in->_stderr, epipe[0]);
492 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
495 service_event("instance.start", in->srv->name, in->name);
499 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
501 char *newline, *str, *arg0, ident[32];
504 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
505 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
506 ulog_open(ULOG_SYSLOG, in->syslog_facility, ident);
509 str = ustream_get_read_buf(s, &len);
513 newline = memchr(str, '\n', len);
514 if (!newline && (s->r.buffer_len != len))
519 len = newline + 1 - str;
521 ulog(prio, "%s\n", str);
523 ustream_consume(s, len);
526 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
530 instance_stdout(struct ustream *s, int bytes)
532 instance_stdio(s, LOG_INFO,
533 container_of(s, struct service_instance, _stdout.stream));
537 instance_stderr(struct ustream *s, int bytes)
539 instance_stdio(s, LOG_ERR,
540 container_of(s, struct service_instance, _stderr.stream));
544 instance_timeout(struct uloop_timeout *t)
546 struct service_instance *in;
548 in = container_of(t, struct service_instance, timeout);
551 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
552 in->srv->name, in->name, in->proc.pid);
553 kill(in->proc.pid, SIGKILL);
554 } else if (in->restart || in->respawn)
559 instance_delete(struct service_instance *in)
561 struct service *s = in->srv;
563 avl_delete(&s->instances.avl, &in->node.avl);
569 instance_exit(struct uloop_process *p, int ret)
571 struct service_instance *in;
575 in = container_of(p, struct service_instance, proc);
577 clock_gettime(CLOCK_MONOTONIC, &tp);
578 runtime = tp.tv_sec - in->start.tv_sec;
580 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
582 uloop_timeout_cancel(&in->timeout);
583 service_event("instance.stop", in->srv->name, in->name);
586 instance_removepid(in);
591 } else if (in->restart) {
593 } else if (in->respawn) {
594 if (runtime < in->respawn_threshold)
597 in->respawn_count = 0;
598 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
599 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
600 in->srv->name, in->name, in->respawn_count, runtime);
601 in->restart = in->respawn = 0;
603 service_event("instance.fail", in->srv->name, in->name);
605 service_event("instance.respawn", in->srv->name, in->name);
606 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
612 instance_stop(struct service_instance *in, bool halt)
614 if (!in->proc.pending) {
620 in->restart = in->respawn = false;
621 kill(in->proc.pid, SIGTERM);
622 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
626 instance_restart(struct service_instance *in)
628 if (!in->proc.pending)
631 if (in->reload_signal) {
632 kill(in->proc.pid, in->reload_signal);
638 kill(in->proc.pid, SIGTERM);
639 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
642 static bool string_changed(const char *a, const char *b)
644 return !((!a && !b) || (a && b && !strcmp(a, b)));
648 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
653 if (!blob_attr_equal(in->command, in_new->command))
656 if (!blobmsg_list_equal(&in->env, &in_new->env))
659 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
662 if (!blobmsg_list_equal(&in->file, &in_new->file))
665 if (in->nice != in_new->nice)
668 if (in->syslog_facility != in_new->syslog_facility)
671 if (string_changed(in->user, in_new->user))
674 if (string_changed(in->group, in_new->group))
677 if (in->uid != in_new->uid)
680 if (in->pw_gid != in_new->pw_gid)
683 if (string_changed(in->pidfile, in_new->pidfile))
686 if (in->respawn_retry != in_new->respawn_retry)
688 if (in->respawn_threshold != in_new->respawn_threshold)
690 if (in->respawn_timeout != in_new->respawn_timeout)
693 if ((!in->seccomp && in_new->seccomp) ||
694 (in->seccomp && !in_new->seccomp) ||
695 (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
698 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
701 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
704 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
711 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
713 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
714 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
716 return n1->ifindex == n2->ifindex;
720 instance_netdev_update(struct blobmsg_list_node *l)
722 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
724 n->ifindex = if_nametoindex(n->node.avl.key);
728 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
730 struct instance_file *f1 = container_of(l1, struct instance_file, node);
731 struct instance_file *f2 = container_of(l2, struct instance_file, node);
733 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
737 instance_file_update(struct blobmsg_list_node *l)
739 struct instance_file *f = container_of(l, struct instance_file, node);
744 memset(f->md5, 0, sizeof(f->md5));
746 fd = open(l->avl.key, O_RDONLY);
752 len = read(fd, buf, sizeof(buf));
762 md5_hash(buf, len, &md5);
765 md5_end(f->md5, &md5);
770 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
775 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
779 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
781 struct blobmsg_list_node *node;
786 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
789 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
791 blobmsg_list_for_each(l, node)
798 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
800 struct blob_attr *tb[__JAIL_ATTR_MAX];
801 struct jail *jail = &in->jail;
804 if (stat("/sbin/ujail", &s))
807 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
808 blobmsg_data(attr), blobmsg_data_len(attr));
812 if (tb[JAIL_ATTR_NAME]) {
813 jail->name = strdup(blobmsg_get_string(tb[JAIL_ATTR_NAME]));
816 if (tb[JAIL_ATTR_HOSTNAME]) {
817 jail->hostname = strdup(blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]));
820 if (tb[JAIL_ATTR_PROCFS]) {
821 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
824 if (tb[JAIL_ATTR_SYSFS]) {
825 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
828 if (tb[JAIL_ATTR_UBUS]) {
829 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
832 if (tb[JAIL_ATTR_LOG]) {
833 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
836 if (tb[JAIL_ATTR_RONLY]) {
837 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
840 if (tb[JAIL_ATTR_NETNS]) {
841 jail->netns = blobmsg_get_bool(tb[JAIL_ATTR_NETNS]);
844 if (tb[JAIL_ATTR_MOUNT]) {
845 struct blob_attr *cur;
848 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
850 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
861 if (in->no_new_privs)
868 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
870 struct blob_attr *cur, *cur2;
874 cur = tb[INSTANCE_ATTR_COMMAND];
880 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
883 blobmsg_for_each_attr(cur2, cur, rem) {
893 instance_config_parse(struct service_instance *in)
895 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
896 struct blob_attr *cur, *cur2;
899 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
900 blobmsg_data(in->config), blobmsg_data_len(in->config));
902 if (!instance_config_parse_command(in, tb))
905 if (tb[INSTANCE_ATTR_TERMTIMEOUT])
906 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
907 if (tb[INSTANCE_ATTR_RESPAWN]) {
909 uint32_t vals[3] = { 3600, 5, 5};
911 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
912 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
914 vals[i] = atoi(blobmsg_get_string(cur2));
918 in->respawn_count = 0;
919 in->respawn_threshold = vals[0];
920 in->respawn_timeout = vals[1];
921 in->respawn_retry = vals[2];
923 if (tb[INSTANCE_ATTR_TRIGGER]) {
924 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
925 trigger_add(in->trigger, in);
928 if (tb[INSTANCE_ATTR_WATCH]) {
929 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
930 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
932 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
933 watch_add(blobmsg_get_string(cur2), in);
937 if ((cur = tb[INSTANCE_ATTR_NICE])) {
938 in->nice = (int8_t) blobmsg_get_u32(cur);
939 if (in->nice < -20 || in->nice > 20)
943 if (tb[INSTANCE_ATTR_USER]) {
944 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
945 struct passwd *p = getpwnam(user);
947 in->user = strdup(user);
949 in->gr_gid = in->pw_gid = p->pw_gid;
953 if (tb[INSTANCE_ATTR_GROUP]) {
954 const char *group = blobmsg_get_string(tb[INSTANCE_ATTR_GROUP]);
955 struct group *p = getgrnam(group);
957 in->group = strdup(group);
958 in->gr_gid = p->gr_gid;
962 if (tb[INSTANCE_ATTR_TRACE])
963 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
965 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
966 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
968 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
969 in->seccomp = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]));
971 if (tb[INSTANCE_ATTR_PIDFILE]) {
972 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
974 in->pidfile = strdup(pidfile);
977 if (tb[INSTANCE_ATTR_RELOADSIG])
978 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
980 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
981 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
983 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
984 in->_stdout.fd.fd = -1;
986 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
987 in->_stderr.fd.fd = -1;
989 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
991 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
994 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
997 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
1000 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
1003 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
1006 if (tb[INSTANCE_ATTR_FACILITY]) {
1007 int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1008 if (facility != -1) {
1009 in->syslog_facility = facility;
1010 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1012 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1019 instance_config_cleanup(struct service_instance *in)
1021 blobmsg_list_free(&in->env);
1022 blobmsg_list_free(&in->data);
1023 blobmsg_list_free(&in->netdev);
1024 blobmsg_list_free(&in->file);
1025 blobmsg_list_free(&in->limits);
1026 blobmsg_list_free(&in->errors);
1027 blobmsg_list_free(&in->jail.mount);
1031 instance_config_move_strdup(char **dst, char *src)
1045 instance_config_move(struct service_instance *in, struct service_instance *in_src)
1047 instance_config_cleanup(in);
1048 blobmsg_list_move(&in->env, &in_src->env);
1049 blobmsg_list_move(&in->data, &in_src->data);
1050 blobmsg_list_move(&in->netdev, &in_src->netdev);
1051 blobmsg_list_move(&in->file, &in_src->file);
1052 blobmsg_list_move(&in->limits, &in_src->limits);
1053 blobmsg_list_move(&in->errors, &in_src->errors);
1054 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
1055 in->trigger = in_src->trigger;
1056 in->command = in_src->command;
1057 in->respawn = in_src->respawn;
1058 in->respawn_retry = in_src->respawn_retry;
1059 in->respawn_threshold = in_src->respawn_threshold;
1060 in->respawn_timeout = in_src->respawn_timeout;
1061 in->name = in_src->name;
1062 in->trace = in_src->trace;
1063 in->node.avl.key = in_src->node.avl.key;
1064 in->syslog_facility = in_src->syslog_facility;
1066 instance_config_move_strdup(&in->pidfile, in_src->pidfile);
1067 instance_config_move_strdup(&in->seccomp, in_src->seccomp);
1068 instance_config_move_strdup(&in->jail.name, in_src->jail.name);
1069 instance_config_move_strdup(&in->jail.hostname, in_src->jail.hostname);
1072 in->config = in_src->config;
1073 in_src->config = NULL;
1077 instance_update(struct service_instance *in, struct service_instance *in_new)
1079 bool changed = instance_config_changed(in, in_new);
1080 bool running = in->proc.pending;
1081 bool stopping = in->halt;
1083 if (!running || stopping) {
1084 instance_config_move(in, in_new);
1088 instance_restart(in);
1089 instance_config_move(in, in_new);
1090 /* restart happens in the child callback handler */
1095 instance_free(struct service_instance *in)
1097 instance_free_stdio(in);
1098 uloop_process_delete(&in->proc);
1099 uloop_timeout_cancel(&in->timeout);
1102 instance_config_cleanup(in);
1106 free(in->jail.name);
1107 free(in->jail.hostname);
1114 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1116 config = blob_memdup(config);
1118 in->name = blobmsg_name(config);
1119 in->config = config;
1120 in->timeout.cb = instance_timeout;
1121 in->proc.cb = instance_exit;
1122 in->term_timeout = 5;
1123 in->syslog_facility = LOG_DAEMON;
1125 in->_stdout.fd.fd = -2;
1126 in->_stdout.stream.string_data = true;
1127 in->_stdout.stream.notify_read = instance_stdout;
1129 in->_stderr.fd.fd = -2;
1130 in->_stderr.stream.string_data = true;
1131 in->_stderr.stream.notify_read = instance_stderr;
1133 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1134 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1135 blobmsg_list_simple_init(&in->env);
1136 blobmsg_list_simple_init(&in->data);
1137 blobmsg_list_simple_init(&in->limits);
1138 blobmsg_list_simple_init(&in->errors);
1139 blobmsg_list_simple_init(&in->jail.mount);
1140 in->valid = instance_config_parse(in);
1143 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1150 i = blobmsg_open_table(b, in->name);
1151 blobmsg_add_u8(b, "running", in->proc.pending);
1152 if (in->proc.pending)
1153 blobmsg_add_u32(b, "pid", in->proc.pid);
1155 blobmsg_add_blob(b, in->command);
1156 blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1158 if (!avl_is_empty(&in->errors.avl)) {
1159 struct blobmsg_list_node *var;
1160 void *e = blobmsg_open_array(b, "errors");
1161 blobmsg_list_for_each(&in->errors, var)
1162 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1163 blobmsg_close_table(b, e);
1166 if (!avl_is_empty(&in->env.avl)) {
1167 struct blobmsg_list_node *var;
1168 void *e = blobmsg_open_table(b, "env");
1169 blobmsg_list_for_each(&in->env, var)
1170 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1171 blobmsg_close_table(b, e);
1174 if (!avl_is_empty(&in->data.avl)) {
1175 struct blobmsg_list_node *var;
1176 void *e = blobmsg_open_table(b, "data");
1177 blobmsg_list_for_each(&in->data, var)
1178 blobmsg_add_blob(b, var->data);
1179 blobmsg_close_table(b, e);
1182 if (!avl_is_empty(&in->limits.avl)) {
1183 struct blobmsg_list_node *var;
1184 void *e = blobmsg_open_table(b, "limits");
1185 blobmsg_list_for_each(&in->limits, var)
1186 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1187 blobmsg_close_table(b, e);
1190 if (in->reload_signal)
1191 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1194 void *r = blobmsg_open_table(b, "respawn");
1195 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1196 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1197 blobmsg_add_u32(b, "retry", in->respawn_retry);
1198 blobmsg_close_table(b, r);
1202 blobmsg_add_u8(b, "trace", true);
1204 if (in->no_new_privs)
1205 blobmsg_add_u8(b, "no_new_privs", true);
1208 blobmsg_add_string(b, "seccomp", in->seccomp);
1211 blobmsg_add_string(b, "pidfile", in->pidfile);
1214 blobmsg_add_string(b, "user", in->user);
1217 blobmsg_add_string(b, "group", in->group);
1220 void *r = blobmsg_open_table(b, "jail");
1222 blobmsg_add_string(b, "name", in->jail.name);
1223 if (in->jail.hostname)
1224 blobmsg_add_string(b, "hostname", in->jail.hostname);
1225 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1226 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1227 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1228 blobmsg_add_u8(b, "log", in->jail.log);
1229 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1230 blobmsg_add_u8(b, "netns", in->jail.netns);
1231 blobmsg_close_table(b, r);
1232 if (!avl_is_empty(&in->jail.mount.avl)) {
1233 struct blobmsg_list_node *var;
1234 void *e = blobmsg_open_table(b, "mount");
1235 blobmsg_list_for_each(&in->jail.mount, var)
1236 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1237 blobmsg_close_table(b, e);
1241 if (verbose && in->trigger)
1242 blobmsg_add_blob(b, in->trigger);
1244 blobmsg_close_table(b, i);