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 },
105 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
106 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
107 [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
108 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
109 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
110 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
111 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
112 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
113 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
116 struct instance_netdev {
117 struct blobmsg_list_node node;
121 struct instance_file {
122 struct blobmsg_list_node node;
131 static const struct rlimit_name rlimit_names[] = {
133 { "core", RLIMIT_CORE },
134 { "cpu", RLIMIT_CPU },
135 { "data", RLIMIT_DATA },
136 { "fsize", RLIMIT_FSIZE },
137 { "memlock", RLIMIT_MEMLOCK },
138 { "nofile", RLIMIT_NOFILE },
139 { "nproc", RLIMIT_NPROC },
140 { "rss", RLIMIT_RSS },
141 { "stack", RLIMIT_STACK },
143 { "nice", RLIMIT_NICE },
144 { "rtprio", RLIMIT_RTPRIO },
145 { "msgqueue", RLIMIT_MSGQUEUE },
146 { "sigpending", RLIMIT_SIGPENDING },
151 static void closefd(int fd)
153 if (fd > STDERR_FILENO)
157 /* convert a string into numeric syslog facility or return -1 if no match found */
159 syslog_facility_str_to_int(const char *facility)
161 CODE *p = facilitynames;
163 while (p->c_name && strcasecmp(p->c_name, facility))
170 instance_limits(const char *limit, const char *value)
174 unsigned long cur, max;
176 for (i = 0; rlimit_names[i].name != NULL; i++) {
177 if (strcmp(rlimit_names[i].name, limit))
179 if (!strcmp(value, "unlimited")) {
180 rlim.rlim_cur = RLIM_INFINITY;
181 rlim.rlim_max = RLIM_INFINITY;
183 if (getrlimit(rlimit_names[i].resource, &rlim))
189 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
196 setrlimit(rlimit_names[i].resource, &rlim);
202 jail_run(struct service_instance *in, char **argv)
204 struct blobmsg_list_node *var;
205 struct jail *jail = &in->jail;
208 argv[argc++] = "/sbin/ujail";
212 argv[argc++] = jail->name;
215 if (jail->hostname) {
217 argv[argc++] = jail->hostname;
222 argv[argc++] = in->seccomp;
225 if (in->no_new_privs)
243 blobmsg_list_for_each(&jail->mount, var) {
244 const char *type = blobmsg_data(var->data);
250 argv[argc++] = (char *) blobmsg_name(var->data);
259 instance_removepid(struct service_instance *in) {
262 if (unlink(in->pidfile)) {
263 ERROR("Failed to removed pidfile: %s: %m\n", in->pidfile);
270 instance_writepid(struct service_instance *in)
277 _pidfile = fopen(in->pidfile, "w");
278 if (_pidfile == NULL) {
279 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
282 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
283 ERROR("failed to write pidfile: %s: %m", in->pidfile);
287 if (fclose(_pidfile)) {
288 ERROR("failed to close pidfile: %s: %m", in->pidfile);
296 instance_run(struct service_instance *in, int _stdout, int _stderr)
298 struct blobmsg_list_node *var;
299 struct blob_attr *cur;
301 int argc = 1; /* NULL terminated */
303 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
304 bool setlbf = _stdout >= 0;
307 setpriority(PRIO_PROCESS, 0, in->nice);
309 blobmsg_for_each_attr(cur, in->command, rem)
312 blobmsg_list_for_each(&in->env, var)
313 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
316 setenv("SECCOMP_FILE", in->seccomp, 1);
319 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
321 blobmsg_list_for_each(&in->limits, var)
322 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
324 if (in->trace || seccomp)
327 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
330 #ifdef SECCOMP_SUPPORT
332 argv[argc++] = "/sbin/utrace";
334 argv[argc++] = "/sbin/seccomp-trace";
336 if (in->trace || seccomp)
337 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
341 argc = jail_run(in, argv);
343 blobmsg_for_each_attr(cur, in->command, rem)
344 argv[argc++] = blobmsg_data(cur);
348 _stdin = open("/dev/null", O_RDONLY);
351 _stdout = open("/dev/null", O_WRONLY);
354 _stderr = open("/dev/null", O_WRONLY);
357 dup2(_stdin, STDIN_FILENO);
361 dup2(_stdout, STDOUT_FILENO);
365 dup2(_stderr, STDERR_FILENO);
369 if (in->user && in->pw_gid && initgroups(in->user, in->pw_gid)) {
370 ERROR("failed to initgroups() for user %s: %m\n", in->user);
373 if (in->gr_gid && setgid(in->gr_gid)) {
374 ERROR("failed to set group id %d: %m\n", in->gr_gid);
377 if (in->uid && setuid(in->uid)) {
378 ERROR("failed to set user id %d: %m\n", in->uid);
382 execvp(argv[0], argv);
387 instance_free_stdio(struct service_instance *in)
389 if (in->_stdout.fd.fd > -1) {
390 ustream_free(&in->_stdout.stream);
391 close(in->_stdout.fd.fd);
392 in->_stdout.fd.fd = -1;
395 if (in->_stderr.fd.fd > -1) {
396 ustream_free(&in->_stderr.stream);
397 close(in->_stderr.fd.fd);
398 in->_stderr.fd.fd = -1;
403 instance_start(struct service_instance *in)
406 int opipe[2] = { -1, -1 };
407 int epipe[2] = { -1, -1 };
409 if (!avl_is_empty(&in->errors.avl)) {
410 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
415 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
419 if (in->proc.pending) {
425 instance_free_stdio(in);
426 if (in->_stdout.fd.fd > -2) {
428 ULOG_WARN("pipe() failed: %m\n");
429 opipe[0] = opipe[1] = -1;
433 if (in->_stderr.fd.fd > -2) {
435 ULOG_WARN("pipe() failed: %m\n");
436 epipe[0] = epipe[1] = -1;
454 instance_run(in, opipe[1], epipe[1]);
458 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
460 instance_writepid(in);
461 clock_gettime(CLOCK_MONOTONIC, &in->start);
462 uloop_process_add(&in->proc);
465 ustream_fd_init(&in->_stdout, opipe[0]);
467 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
471 ustream_fd_init(&in->_stderr, epipe[0]);
473 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
476 service_event("instance.start", in->srv->name, in->name);
480 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
482 char *newline, *str, *arg0, ident[32];
485 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
486 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
487 ulog_open(ULOG_SYSLOG, in->syslog_facility, ident);
490 str = ustream_get_read_buf(s, &len);
494 newline = memchr(str, '\n', len);
495 if (!newline && (s->r.buffer_len != len))
500 len = newline + 1 - str;
502 ulog(prio, "%s\n", str);
504 ustream_consume(s, len);
507 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
511 instance_stdout(struct ustream *s, int bytes)
513 instance_stdio(s, LOG_INFO,
514 container_of(s, struct service_instance, _stdout.stream));
518 instance_stderr(struct ustream *s, int bytes)
520 instance_stdio(s, LOG_ERR,
521 container_of(s, struct service_instance, _stderr.stream));
525 instance_timeout(struct uloop_timeout *t)
527 struct service_instance *in;
529 in = container_of(t, struct service_instance, timeout);
532 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
533 in->srv->name, in->name, in->proc.pid);
534 kill(in->proc.pid, SIGKILL);
535 } else if (in->restart || in->respawn)
540 instance_delete(struct service_instance *in)
542 struct service *s = in->srv;
544 avl_delete(&s->instances.avl, &in->node.avl);
550 instance_exit(struct uloop_process *p, int ret)
552 struct service_instance *in;
556 in = container_of(p, struct service_instance, proc);
558 clock_gettime(CLOCK_MONOTONIC, &tp);
559 runtime = tp.tv_sec - in->start.tv_sec;
561 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
563 uloop_timeout_cancel(&in->timeout);
564 service_event("instance.stop", in->srv->name, in->name);
567 instance_removepid(in);
572 } else if (in->restart) {
574 } else if (in->respawn) {
575 if (runtime < in->respawn_threshold)
578 in->respawn_count = 0;
579 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
580 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
581 in->srv->name, in->name, in->respawn_count, runtime);
582 in->restart = in->respawn = 0;
584 service_event("instance.fail", in->srv->name, in->name);
586 service_event("instance.respawn", in->srv->name, in->name);
587 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
593 instance_stop(struct service_instance *in, bool halt)
595 if (!in->proc.pending) {
601 in->restart = in->respawn = false;
602 kill(in->proc.pid, SIGTERM);
603 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
607 instance_restart(struct service_instance *in)
609 if (!in->proc.pending)
612 if (in->reload_signal) {
613 kill(in->proc.pid, in->reload_signal);
619 kill(in->proc.pid, SIGTERM);
620 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
623 static bool string_changed(const char *a, const char *b)
625 return !((!a && !b) || (a && b && !strcmp(a, b)));
629 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
634 if (!blob_attr_equal(in->command, in_new->command))
637 if (!blobmsg_list_equal(&in->env, &in_new->env))
640 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
643 if (!blobmsg_list_equal(&in->file, &in_new->file))
646 if (in->nice != in_new->nice)
649 if (in->syslog_facility != in_new->syslog_facility)
652 if (string_changed(in->user, in_new->user))
655 if (string_changed(in->group, in_new->group))
658 if (in->uid != in_new->uid)
661 if (in->pw_gid != in_new->pw_gid)
664 if (string_changed(in->pidfile, in_new->pidfile))
667 if (in->respawn_retry != in_new->respawn_retry)
669 if (in->respawn_threshold != in_new->respawn_threshold)
671 if (in->respawn_timeout != in_new->respawn_timeout)
674 if ((!in->seccomp && in_new->seccomp) ||
675 (in->seccomp && !in_new->seccomp) ||
676 (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
679 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
682 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
685 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
692 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
694 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
695 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
697 return n1->ifindex == n2->ifindex;
701 instance_netdev_update(struct blobmsg_list_node *l)
703 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
705 n->ifindex = if_nametoindex(n->node.avl.key);
709 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
711 struct instance_file *f1 = container_of(l1, struct instance_file, node);
712 struct instance_file *f2 = container_of(l2, struct instance_file, node);
714 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
718 instance_file_update(struct blobmsg_list_node *l)
720 struct instance_file *f = container_of(l, struct instance_file, node);
725 memset(f->md5, 0, sizeof(f->md5));
727 fd = open(l->avl.key, O_RDONLY);
733 len = read(fd, buf, sizeof(buf));
743 md5_hash(buf, len, &md5);
746 md5_end(f->md5, &md5);
751 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
756 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
760 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
762 struct blobmsg_list_node *node;
767 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
770 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
772 blobmsg_list_for_each(l, node)
779 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
781 struct blob_attr *tb[__JAIL_ATTR_MAX];
782 struct jail *jail = &in->jail;
785 if (stat("/sbin/ujail", &s))
788 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
789 blobmsg_data(attr), blobmsg_data_len(attr));
793 if (tb[JAIL_ATTR_NAME]) {
794 jail->name = blobmsg_get_string(tb[JAIL_ATTR_NAME]);
797 if (tb[JAIL_ATTR_HOSTNAME]) {
798 jail->hostname = blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]);
801 if (tb[JAIL_ATTR_PROCFS]) {
802 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
805 if (tb[JAIL_ATTR_SYSFS]) {
806 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
809 if (tb[JAIL_ATTR_UBUS]) {
810 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
813 if (tb[JAIL_ATTR_LOG]) {
814 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
817 if (tb[JAIL_ATTR_RONLY]) {
818 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
821 if (tb[JAIL_ATTR_MOUNT]) {
822 struct blob_attr *cur;
825 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
827 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
836 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
838 struct blob_attr *cur, *cur2;
842 cur = tb[INSTANCE_ATTR_COMMAND];
848 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
851 blobmsg_for_each_attr(cur2, cur, rem) {
861 instance_config_parse(struct service_instance *in)
863 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
864 struct blob_attr *cur, *cur2;
867 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
868 blobmsg_data(in->config), blobmsg_data_len(in->config));
870 if (!instance_config_parse_command(in, tb))
873 if (tb[INSTANCE_ATTR_TERMTIMEOUT])
874 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
875 if (tb[INSTANCE_ATTR_RESPAWN]) {
877 uint32_t vals[3] = { 3600, 5, 5};
879 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
880 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
882 vals[i] = atoi(blobmsg_get_string(cur2));
886 in->respawn_count = 0;
887 in->respawn_threshold = vals[0];
888 in->respawn_timeout = vals[1];
889 in->respawn_retry = vals[2];
891 if (tb[INSTANCE_ATTR_TRIGGER]) {
892 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
893 trigger_add(in->trigger, in);
896 if (tb[INSTANCE_ATTR_WATCH]) {
897 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
898 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
900 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
901 watch_add(blobmsg_get_string(cur2), in);
905 if ((cur = tb[INSTANCE_ATTR_NICE])) {
906 in->nice = (int8_t) blobmsg_get_u32(cur);
907 if (in->nice < -20 || in->nice > 20)
911 if (tb[INSTANCE_ATTR_USER]) {
912 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
913 struct passwd *p = getpwnam(user);
915 in->user = strdup(user);
917 in->gr_gid = in->pw_gid = p->pw_gid;
921 if (tb[INSTANCE_ATTR_GROUP]) {
922 const char *group = blobmsg_get_string(tb[INSTANCE_ATTR_GROUP]);
923 struct group *p = getgrnam(group);
925 in->group = strdup(group);
926 in->gr_gid = p->gr_gid;
930 if (tb[INSTANCE_ATTR_TRACE])
931 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
933 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
934 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
936 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
937 in->seccomp = blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]);
939 if (tb[INSTANCE_ATTR_PIDFILE]) {
940 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
942 in->pidfile = pidfile;
945 if (tb[INSTANCE_ATTR_RELOADSIG])
946 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
948 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
949 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
951 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
952 in->_stdout.fd.fd = -1;
954 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
955 in->_stderr.fd.fd = -1;
957 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
959 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
962 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
965 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
968 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
971 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
974 if (tb[INSTANCE_ATTR_FACILITY]) {
975 int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
976 if (facility != -1) {
977 in->syslog_facility = facility;
978 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
980 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
987 instance_config_cleanup(struct service_instance *in)
989 blobmsg_list_free(&in->env);
990 blobmsg_list_free(&in->data);
991 blobmsg_list_free(&in->netdev);
992 blobmsg_list_free(&in->file);
993 blobmsg_list_free(&in->limits);
994 blobmsg_list_free(&in->errors);
995 blobmsg_list_free(&in->jail.mount);
999 instance_config_move(struct service_instance *in, struct service_instance *in_src)
1001 instance_config_cleanup(in);
1002 blobmsg_list_move(&in->env, &in_src->env);
1003 blobmsg_list_move(&in->data, &in_src->data);
1004 blobmsg_list_move(&in->netdev, &in_src->netdev);
1005 blobmsg_list_move(&in->file, &in_src->file);
1006 blobmsg_list_move(&in->limits, &in_src->limits);
1007 blobmsg_list_move(&in->errors, &in_src->errors);
1008 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
1009 in->trigger = in_src->trigger;
1010 in->command = in_src->command;
1011 in->pidfile = in_src->pidfile;
1012 in->respawn = in_src->respawn;
1013 in->respawn_retry = in_src->respawn_retry;
1014 in->respawn_threshold = in_src->respawn_threshold;
1015 in->respawn_timeout = in_src->respawn_timeout;
1016 in->name = in_src->name;
1017 in->trace = in_src->trace;
1018 in->seccomp = in_src->seccomp;
1019 in->node.avl.key = in_src->node.avl.key;
1020 in->syslog_facility = in_src->syslog_facility;
1023 in->config = in_src->config;
1024 in_src->config = NULL;
1028 instance_update(struct service_instance *in, struct service_instance *in_new)
1030 bool changed = instance_config_changed(in, in_new);
1031 bool running = in->proc.pending;
1032 bool stopping = in->halt;
1034 if (!running || stopping) {
1035 instance_config_move(in, in_new);
1039 instance_restart(in);
1040 instance_config_move(in, in_new);
1041 /* restart happens in the child callback handler */
1046 instance_free(struct service_instance *in)
1048 instance_free_stdio(in);
1049 uloop_process_delete(&in->proc);
1050 uloop_timeout_cancel(&in->timeout);
1053 instance_config_cleanup(in);
1061 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1063 config = blob_memdup(config);
1065 in->name = blobmsg_name(config);
1066 in->config = config;
1067 in->timeout.cb = instance_timeout;
1068 in->proc.cb = instance_exit;
1069 in->term_timeout = 5;
1070 in->syslog_facility = LOG_DAEMON;
1072 in->_stdout.fd.fd = -2;
1073 in->_stdout.stream.string_data = true;
1074 in->_stdout.stream.notify_read = instance_stdout;
1076 in->_stderr.fd.fd = -2;
1077 in->_stderr.stream.string_data = true;
1078 in->_stderr.stream.notify_read = instance_stderr;
1080 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1081 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1082 blobmsg_list_simple_init(&in->env);
1083 blobmsg_list_simple_init(&in->data);
1084 blobmsg_list_simple_init(&in->limits);
1085 blobmsg_list_simple_init(&in->errors);
1086 blobmsg_list_simple_init(&in->jail.mount);
1087 in->valid = instance_config_parse(in);
1090 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1097 i = blobmsg_open_table(b, in->name);
1098 blobmsg_add_u8(b, "running", in->proc.pending);
1099 if (in->proc.pending)
1100 blobmsg_add_u32(b, "pid", in->proc.pid);
1102 blobmsg_add_blob(b, in->command);
1103 blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1105 if (!avl_is_empty(&in->errors.avl)) {
1106 struct blobmsg_list_node *var;
1107 void *e = blobmsg_open_array(b, "errors");
1108 blobmsg_list_for_each(&in->errors, var)
1109 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1110 blobmsg_close_table(b, e);
1113 if (!avl_is_empty(&in->env.avl)) {
1114 struct blobmsg_list_node *var;
1115 void *e = blobmsg_open_table(b, "env");
1116 blobmsg_list_for_each(&in->env, var)
1117 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1118 blobmsg_close_table(b, e);
1121 if (!avl_is_empty(&in->data.avl)) {
1122 struct blobmsg_list_node *var;
1123 void *e = blobmsg_open_table(b, "data");
1124 blobmsg_list_for_each(&in->data, var)
1125 blobmsg_add_blob(b, var->data);
1126 blobmsg_close_table(b, e);
1129 if (!avl_is_empty(&in->limits.avl)) {
1130 struct blobmsg_list_node *var;
1131 void *e = blobmsg_open_table(b, "limits");
1132 blobmsg_list_for_each(&in->limits, var)
1133 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1134 blobmsg_close_table(b, e);
1137 if (in->reload_signal)
1138 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1141 void *r = blobmsg_open_table(b, "respawn");
1142 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1143 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1144 blobmsg_add_u32(b, "retry", in->respawn_retry);
1145 blobmsg_close_table(b, r);
1149 blobmsg_add_u8(b, "trace", true);
1151 if (in->no_new_privs)
1152 blobmsg_add_u8(b, "no_new_privs", true);
1155 blobmsg_add_string(b, "seccomp", in->seccomp);
1158 blobmsg_add_string(b, "pidfile", in->pidfile);
1161 void *r = blobmsg_open_table(b, "jail");
1163 blobmsg_add_string(b, "name", in->jail.name);
1164 if (in->jail.hostname)
1165 blobmsg_add_string(b, "hostname", in->jail.hostname);
1166 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1167 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1168 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1169 blobmsg_add_u8(b, "log", in->jail.log);
1170 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1171 blobmsg_close_table(b, r);
1172 if (!avl_is_empty(&in->jail.mount.avl)) {
1173 struct blobmsg_list_node *var;
1174 void *e = blobmsg_open_table(b, "mount");
1175 blobmsg_list_for_each(&in->jail.mount, var)
1176 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1177 blobmsg_close_table(b, e);
1181 if (verbose && in->trigger)
1182 blobmsg_add_blob(b, in->trigger);
1184 blobmsg_close_table(b, i);