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,
55 INSTANCE_ATTR_NO_NEW_PRIVS,
58 INSTANCE_ATTR_SECCOMP,
59 INSTANCE_ATTR_PIDFILE,
60 INSTANCE_ATTR_RELOADSIG,
61 INSTANCE_ATTR_TERMTIMEOUT,
62 INSTANCE_ATTR_FACILITY,
66 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
67 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
68 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
69 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
70 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
71 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
72 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
73 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
74 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
75 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
76 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
77 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
78 [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
79 [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
80 [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
81 [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
82 [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
83 [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
84 [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
85 [INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
86 [INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
87 [INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
88 [INSTANCE_ATTR_FACILITY] = { "facility", BLOBMSG_TYPE_STRING },
103 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
104 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
105 [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
106 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
107 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
108 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
109 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
110 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
111 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
114 struct instance_netdev {
115 struct blobmsg_list_node node;
119 struct instance_file {
120 struct blobmsg_list_node node;
129 static const struct rlimit_name rlimit_names[] = {
131 { "core", RLIMIT_CORE },
132 { "cpu", RLIMIT_CPU },
133 { "data", RLIMIT_DATA },
134 { "fsize", RLIMIT_FSIZE },
135 { "memlock", RLIMIT_MEMLOCK },
136 { "nofile", RLIMIT_NOFILE },
137 { "nproc", RLIMIT_NPROC },
138 { "rss", RLIMIT_RSS },
139 { "stack", RLIMIT_STACK },
141 { "nice", RLIMIT_NICE },
142 { "rtprio", RLIMIT_RTPRIO },
143 { "msgqueue", RLIMIT_MSGQUEUE },
144 { "sigpending", RLIMIT_SIGPENDING },
149 static void closefd(int fd)
151 if (fd > STDERR_FILENO)
155 /* convert a string into numeric syslog facility or return -1 if no match found */
157 syslog_facility_str_to_int(const char *facility)
159 CODE *p = facilitynames;
161 while (p->c_name && strcasecmp(p->c_name, facility))
168 instance_limits(const char *limit, const char *value)
172 unsigned long cur, max;
174 for (i = 0; rlimit_names[i].name != NULL; i++) {
175 if (strcmp(rlimit_names[i].name, limit))
177 if (!strcmp(value, "unlimited")) {
178 rlim.rlim_cur = RLIM_INFINITY;
179 rlim.rlim_max = RLIM_INFINITY;
181 if (getrlimit(rlimit_names[i].resource, &rlim))
187 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
194 setrlimit(rlimit_names[i].resource, &rlim);
200 jail_run(struct service_instance *in, char **argv)
202 struct blobmsg_list_node *var;
203 struct jail *jail = &in->jail;
206 argv[argc++] = "/sbin/ujail";
210 argv[argc++] = jail->name;
213 if (jail->hostname) {
215 argv[argc++] = jail->hostname;
220 argv[argc++] = in->seccomp;
223 if (in->no_new_privs)
241 blobmsg_list_for_each(&jail->mount, var) {
242 const char *type = blobmsg_data(var->data);
248 argv[argc++] = (char *) blobmsg_name(var->data);
257 instance_removepid(struct service_instance *in) {
260 if (unlink(in->pidfile)) {
261 ERROR("Failed to removed pidfile: %s: %m\n", in->pidfile);
268 instance_writepid(struct service_instance *in)
275 _pidfile = fopen(in->pidfile, "w");
276 if (_pidfile == NULL) {
277 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
280 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
281 ERROR("failed to write pidfile: %s: %m", in->pidfile);
285 if (fclose(_pidfile)) {
286 ERROR("failed to close pidfile: %s: %m", in->pidfile);
294 instance_run(struct service_instance *in, int _stdout, int _stderr)
296 struct blobmsg_list_node *var;
297 struct blob_attr *cur;
299 int argc = 1; /* NULL terminated */
301 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
302 bool setlbf = _stdout >= 0;
305 setpriority(PRIO_PROCESS, 0, in->nice);
307 blobmsg_for_each_attr(cur, in->command, rem)
310 blobmsg_list_for_each(&in->env, var)
311 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
314 setenv("SECCOMP_FILE", in->seccomp, 1);
317 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
319 blobmsg_list_for_each(&in->limits, var)
320 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
322 if (in->trace || seccomp)
325 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
328 #ifdef SECCOMP_SUPPORT
330 argv[argc++] = "/sbin/utrace";
332 argv[argc++] = "/sbin/seccomp-trace";
334 if (in->trace || seccomp)
335 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
339 argc = jail_run(in, argv);
341 blobmsg_for_each_attr(cur, in->command, rem)
342 argv[argc++] = blobmsg_data(cur);
346 _stdin = open("/dev/null", O_RDONLY);
349 _stdout = open("/dev/null", O_WRONLY);
352 _stderr = open("/dev/null", O_WRONLY);
355 dup2(_stdin, STDIN_FILENO);
359 dup2(_stdout, STDOUT_FILENO);
363 dup2(_stderr, STDERR_FILENO);
367 if (in->user && in->gid && initgroups(in->user, in->gid)) {
368 ERROR("failed to initgroups() for user %s: %m\n", in->user);
371 if (in->gid && setgid(in->gid)) {
372 ERROR("failed to set group id %d: %m\n", in->gid);
375 if (in->uid && setuid(in->uid)) {
376 ERROR("failed to set user id %d: %m\n", in->uid);
380 execvp(argv[0], argv);
385 instance_free_stdio(struct service_instance *in)
387 if (in->_stdout.fd.fd > -1) {
388 ustream_free(&in->_stdout.stream);
389 close(in->_stdout.fd.fd);
390 in->_stdout.fd.fd = -1;
393 if (in->_stderr.fd.fd > -1) {
394 ustream_free(&in->_stderr.stream);
395 close(in->_stderr.fd.fd);
396 in->_stderr.fd.fd = -1;
401 instance_start(struct service_instance *in)
404 int opipe[2] = { -1, -1 };
405 int epipe[2] = { -1, -1 };
407 if (!avl_is_empty(&in->errors.avl)) {
408 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
413 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
417 if (in->proc.pending) {
423 instance_free_stdio(in);
424 if (in->_stdout.fd.fd > -2) {
426 ULOG_WARN("pipe() failed: %m\n");
427 opipe[0] = opipe[1] = -1;
431 if (in->_stderr.fd.fd > -2) {
433 ULOG_WARN("pipe() failed: %m\n");
434 epipe[0] = epipe[1] = -1;
452 instance_run(in, opipe[1], epipe[1]);
456 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
458 instance_writepid(in);
459 clock_gettime(CLOCK_MONOTONIC, &in->start);
460 uloop_process_add(&in->proc);
463 ustream_fd_init(&in->_stdout, opipe[0]);
465 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
469 ustream_fd_init(&in->_stderr, epipe[0]);
471 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
474 service_event("instance.start", in->srv->name, in->name);
478 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
480 char *newline, *str, *arg0, ident[32];
483 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
484 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
485 ulog_open(ULOG_SYSLOG, in->syslog_facility, ident);
488 str = ustream_get_read_buf(s, &len);
492 newline = memchr(str, '\n', len);
493 if (!newline && (s->r.buffer_len != len))
498 len = newline + 1 - str;
500 ulog(prio, "%s\n", str);
502 ustream_consume(s, len);
505 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
509 instance_stdout(struct ustream *s, int bytes)
511 instance_stdio(s, LOG_INFO,
512 container_of(s, struct service_instance, _stdout.stream));
516 instance_stderr(struct ustream *s, int bytes)
518 instance_stdio(s, LOG_ERR,
519 container_of(s, struct service_instance, _stderr.stream));
523 instance_timeout(struct uloop_timeout *t)
525 struct service_instance *in;
527 in = container_of(t, struct service_instance, timeout);
530 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
531 in->srv->name, in->name, in->proc.pid);
532 kill(in->proc.pid, SIGKILL);
533 } else if (in->restart || in->respawn)
538 instance_delete(struct service_instance *in)
540 struct service *s = in->srv;
542 avl_delete(&s->instances.avl, &in->node.avl);
548 instance_exit(struct uloop_process *p, int ret)
550 struct service_instance *in;
554 in = container_of(p, struct service_instance, proc);
556 clock_gettime(CLOCK_MONOTONIC, &tp);
557 runtime = tp.tv_sec - in->start.tv_sec;
559 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
561 uloop_timeout_cancel(&in->timeout);
562 service_event("instance.stop", in->srv->name, in->name);
565 instance_removepid(in);
570 } else if (in->restart) {
572 } else if (in->respawn) {
573 if (runtime < in->respawn_threshold)
576 in->respawn_count = 0;
577 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
578 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
579 in->srv->name, in->name, in->respawn_count, runtime);
580 in->restart = in->respawn = 0;
582 service_event("instance.fail", in->srv->name, in->name);
584 service_event("instance.respawn", in->srv->name, in->name);
585 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
591 instance_stop(struct service_instance *in, bool halt)
593 if (!in->proc.pending) {
599 in->restart = in->respawn = false;
600 kill(in->proc.pid, SIGTERM);
601 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
605 instance_restart(struct service_instance *in)
607 if (!in->proc.pending)
610 if (in->reload_signal) {
611 kill(in->proc.pid, in->reload_signal);
617 kill(in->proc.pid, SIGTERM);
618 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
621 static bool string_changed(const char *a, const char *b)
623 return !((!a && !b) || (a && b && !strcmp(a, b)));
627 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
632 if (!blob_attr_equal(in->command, in_new->command))
635 if (!blobmsg_list_equal(&in->env, &in_new->env))
638 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
641 if (!blobmsg_list_equal(&in->file, &in_new->file))
644 if (in->nice != in_new->nice)
647 if (in->syslog_facility != in_new->syslog_facility)
650 if (string_changed(in->user, in_new->user))
653 if (in->uid != in_new->uid)
656 if (in->gid != in_new->gid)
659 if (string_changed(in->pidfile, in_new->pidfile))
662 if (in->respawn_retry != in_new->respawn_retry)
664 if (in->respawn_threshold != in_new->respawn_threshold)
666 if (in->respawn_timeout != in_new->respawn_timeout)
669 if ((!in->seccomp && in_new->seccomp) ||
670 (in->seccomp && !in_new->seccomp) ||
671 (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
674 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
677 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
680 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
687 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
689 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
690 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
692 return n1->ifindex == n2->ifindex;
696 instance_netdev_update(struct blobmsg_list_node *l)
698 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
700 n->ifindex = if_nametoindex(n->node.avl.key);
704 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
706 struct instance_file *f1 = container_of(l1, struct instance_file, node);
707 struct instance_file *f2 = container_of(l2, struct instance_file, node);
709 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
713 instance_file_update(struct blobmsg_list_node *l)
715 struct instance_file *f = container_of(l, struct instance_file, node);
720 memset(f->md5, 0, sizeof(f->md5));
722 fd = open(l->avl.key, O_RDONLY);
728 len = read(fd, buf, sizeof(buf));
738 md5_hash(buf, len, &md5);
741 md5_end(f->md5, &md5);
746 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
751 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
755 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
757 struct blobmsg_list_node *node;
762 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
765 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
767 blobmsg_list_for_each(l, node)
774 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
776 struct blob_attr *tb[__JAIL_ATTR_MAX];
777 struct jail *jail = &in->jail;
780 if (stat("/sbin/ujail", &s))
783 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
784 blobmsg_data(attr), blobmsg_data_len(attr));
788 if (tb[JAIL_ATTR_NAME]) {
789 jail->name = blobmsg_get_string(tb[JAIL_ATTR_NAME]);
792 if (tb[JAIL_ATTR_HOSTNAME]) {
793 jail->hostname = blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]);
796 if (tb[JAIL_ATTR_PROCFS]) {
797 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
800 if (tb[JAIL_ATTR_SYSFS]) {
801 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
804 if (tb[JAIL_ATTR_UBUS]) {
805 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
808 if (tb[JAIL_ATTR_LOG]) {
809 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
812 if (tb[JAIL_ATTR_RONLY]) {
813 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
816 if (tb[JAIL_ATTR_MOUNT]) {
817 struct blob_attr *cur;
820 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
822 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
831 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
833 struct blob_attr *cur, *cur2;
837 cur = tb[INSTANCE_ATTR_COMMAND];
843 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
846 blobmsg_for_each_attr(cur2, cur, rem) {
856 instance_config_parse(struct service_instance *in)
858 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
859 struct blob_attr *cur, *cur2;
862 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
863 blobmsg_data(in->config), blobmsg_data_len(in->config));
865 if (!instance_config_parse_command(in, tb))
868 if (tb[INSTANCE_ATTR_TERMTIMEOUT])
869 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
870 if (tb[INSTANCE_ATTR_RESPAWN]) {
872 uint32_t vals[3] = { 3600, 5, 5};
874 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
875 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
877 vals[i] = atoi(blobmsg_get_string(cur2));
881 in->respawn_count = 0;
882 in->respawn_threshold = vals[0];
883 in->respawn_timeout = vals[1];
884 in->respawn_retry = vals[2];
886 if (tb[INSTANCE_ATTR_TRIGGER]) {
887 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
888 trigger_add(in->trigger, in);
891 if (tb[INSTANCE_ATTR_WATCH]) {
892 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
893 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
895 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
896 watch_add(blobmsg_get_string(cur2), in);
900 if ((cur = tb[INSTANCE_ATTR_NICE])) {
901 in->nice = (int8_t) blobmsg_get_u32(cur);
902 if (in->nice < -20 || in->nice > 20)
906 if (tb[INSTANCE_ATTR_USER]) {
907 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
908 struct passwd *p = getpwnam(user);
910 in->user = strdup(user);
916 if (tb[INSTANCE_ATTR_TRACE])
917 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
919 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
920 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
922 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
923 in->seccomp = blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]);
925 if (tb[INSTANCE_ATTR_PIDFILE]) {
926 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
928 in->pidfile = pidfile;
931 if (tb[INSTANCE_ATTR_RELOADSIG])
932 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
934 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
935 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
937 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
938 in->_stdout.fd.fd = -1;
940 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
941 in->_stderr.fd.fd = -1;
943 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
945 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
948 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
951 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
954 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
957 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
960 if (tb[INSTANCE_ATTR_FACILITY]) {
961 int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
962 if (facility != -1) {
963 in->syslog_facility = facility;
964 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
966 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
973 instance_config_cleanup(struct service_instance *in)
975 blobmsg_list_free(&in->env);
976 blobmsg_list_free(&in->data);
977 blobmsg_list_free(&in->netdev);
978 blobmsg_list_free(&in->file);
979 blobmsg_list_free(&in->limits);
980 blobmsg_list_free(&in->errors);
981 blobmsg_list_free(&in->jail.mount);
985 instance_config_move(struct service_instance *in, struct service_instance *in_src)
987 instance_config_cleanup(in);
988 blobmsg_list_move(&in->env, &in_src->env);
989 blobmsg_list_move(&in->data, &in_src->data);
990 blobmsg_list_move(&in->netdev, &in_src->netdev);
991 blobmsg_list_move(&in->file, &in_src->file);
992 blobmsg_list_move(&in->limits, &in_src->limits);
993 blobmsg_list_move(&in->errors, &in_src->errors);
994 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
995 in->trigger = in_src->trigger;
996 in->command = in_src->command;
997 in->pidfile = in_src->pidfile;
998 in->respawn = in_src->respawn;
999 in->respawn_retry = in_src->respawn_retry;
1000 in->respawn_threshold = in_src->respawn_threshold;
1001 in->respawn_timeout = in_src->respawn_timeout;
1002 in->name = in_src->name;
1003 in->trace = in_src->trace;
1004 in->seccomp = in_src->seccomp;
1005 in->node.avl.key = in_src->node.avl.key;
1006 in->syslog_facility = in_src->syslog_facility;
1009 in->config = in_src->config;
1010 in_src->config = NULL;
1014 instance_update(struct service_instance *in, struct service_instance *in_new)
1016 bool changed = instance_config_changed(in, in_new);
1017 bool running = in->proc.pending;
1018 bool stopping = in->halt;
1020 if (!running || stopping) {
1021 instance_config_move(in, in_new);
1025 instance_restart(in);
1026 instance_config_move(in, in_new);
1027 /* restart happens in the child callback handler */
1032 instance_free(struct service_instance *in)
1034 instance_free_stdio(in);
1035 uloop_process_delete(&in->proc);
1036 uloop_timeout_cancel(&in->timeout);
1039 instance_config_cleanup(in);
1046 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1048 config = blob_memdup(config);
1050 in->name = blobmsg_name(config);
1051 in->config = config;
1052 in->timeout.cb = instance_timeout;
1053 in->proc.cb = instance_exit;
1054 in->term_timeout = 5;
1055 in->syslog_facility = LOG_DAEMON;
1057 in->_stdout.fd.fd = -2;
1058 in->_stdout.stream.string_data = true;
1059 in->_stdout.stream.notify_read = instance_stdout;
1061 in->_stderr.fd.fd = -2;
1062 in->_stderr.stream.string_data = true;
1063 in->_stderr.stream.notify_read = instance_stderr;
1065 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1066 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1067 blobmsg_list_simple_init(&in->env);
1068 blobmsg_list_simple_init(&in->data);
1069 blobmsg_list_simple_init(&in->limits);
1070 blobmsg_list_simple_init(&in->errors);
1071 blobmsg_list_simple_init(&in->jail.mount);
1072 in->valid = instance_config_parse(in);
1075 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1082 i = blobmsg_open_table(b, in->name);
1083 blobmsg_add_u8(b, "running", in->proc.pending);
1084 if (in->proc.pending)
1085 blobmsg_add_u32(b, "pid", in->proc.pid);
1087 blobmsg_add_blob(b, in->command);
1088 blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1090 if (!avl_is_empty(&in->errors.avl)) {
1091 struct blobmsg_list_node *var;
1092 void *e = blobmsg_open_array(b, "errors");
1093 blobmsg_list_for_each(&in->errors, var)
1094 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1095 blobmsg_close_table(b, e);
1098 if (!avl_is_empty(&in->env.avl)) {
1099 struct blobmsg_list_node *var;
1100 void *e = blobmsg_open_table(b, "env");
1101 blobmsg_list_for_each(&in->env, var)
1102 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1103 blobmsg_close_table(b, e);
1106 if (!avl_is_empty(&in->data.avl)) {
1107 struct blobmsg_list_node *var;
1108 void *e = blobmsg_open_table(b, "data");
1109 blobmsg_list_for_each(&in->data, var)
1110 blobmsg_add_blob(b, var->data);
1111 blobmsg_close_table(b, e);
1114 if (!avl_is_empty(&in->limits.avl)) {
1115 struct blobmsg_list_node *var;
1116 void *e = blobmsg_open_table(b, "limits");
1117 blobmsg_list_for_each(&in->limits, var)
1118 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1119 blobmsg_close_table(b, e);
1122 if (in->reload_signal)
1123 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1126 void *r = blobmsg_open_table(b, "respawn");
1127 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1128 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1129 blobmsg_add_u32(b, "retry", in->respawn_retry);
1130 blobmsg_close_table(b, r);
1134 blobmsg_add_u8(b, "trace", true);
1136 if (in->no_new_privs)
1137 blobmsg_add_u8(b, "no_new_privs", true);
1140 blobmsg_add_string(b, "seccomp", in->seccomp);
1143 blobmsg_add_string(b, "pidfile", in->pidfile);
1146 void *r = blobmsg_open_table(b, "jail");
1148 blobmsg_add_string(b, "name", in->jail.name);
1149 if (in->jail.hostname)
1150 blobmsg_add_string(b, "hostname", in->jail.hostname);
1151 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1152 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1153 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1154 blobmsg_add_u8(b, "log", in->jail.log);
1155 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1156 blobmsg_close_table(b, r);
1157 if (!avl_is_empty(&in->jail.mount.avl)) {
1158 struct blobmsg_list_node *var;
1159 void *e = blobmsg_open_table(b, "mount");
1160 blobmsg_list_for_each(&in->jail.mount, var)
1161 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1162 blobmsg_close_table(b, e);
1166 if (verbose && in->trigger)
1167 blobmsg_add_blob(b, in->trigger);
1169 blobmsg_close_table(b, i);