[INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
[INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
[INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
[INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
[INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
[INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
[INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
[INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
[INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
[INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
[INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
[INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
[INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
[INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
[INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
[INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
[INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
[INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
+ [INSTANCE_ATTR_FACILITY] = { "facility", BLOBMSG_TYPE_STRING },
+ [INSTANCE_ATTR_EXTROOT] = { "extroot", BLOBMSG_TYPE_STRING },
+ [INSTANCE_ATTR_OVERLAYDIR] = { "overlaydir", BLOBMSG_TYPE_STRING },
+ [INSTANCE_ATTR_TMPOVERLAYSIZE] = { "tmpoverlaysize", BLOBMSG_TYPE_STRING },
[JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
[JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
[JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
[JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
[JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
[JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
+ [JAIL_ATTR_NETNS] = { "netns", BLOBMSG_TYPE_BOOL },
+ [JAIL_ATTR_USERNS] = { "userns", BLOBMSG_TYPE_BOOL },
+ [JAIL_ATTR_CGROUPSNS] = { "cgroupsns", BLOBMSG_TYPE_BOOL },
+ [JAIL_ATTR_REQUIREJAIL] = { "requirejail", BLOBMSG_TYPE_BOOL },
+/* convert a string into numeric syslog facility or return -1 if no match found */
+static int
+syslog_facility_str_to_int(const char *facility)
+{
+ CODE *p = facilitynames;
+
+ while (p->c_name && strcasecmp(p->c_name, facility))
+ p++;
+
+ return p->c_val;
+}
+
blobmsg_list_for_each(&jail->mount, var) {
const char *type = blobmsg_data(var->data);
blobmsg_list_for_each(&jail->mount, var) {
const char *type = blobmsg_data(var->data);
- ERROR("Failed to removed pidfile: %s: %d - %s\n",
- in->pidfile, errno, strerror(errno));
+ ERROR("Failed to remove pidfile: %s: %m\n", in->pidfile);
- ERROR("failed to open pidfile for writing: %s: %d (%s)",
- in->pidfile, errno, strerror(errno));
+ ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
- ERROR("failed to write pidfile: %s: %d (%s)",
- in->pidfile, errno, strerror(errno));
+ ERROR("failed to write pidfile: %s: %m", in->pidfile);
- ERROR("failed to close pidfile: %s: %d (%s)",
- in->pidfile, errno, strerror(errno));
+ ERROR("failed to close pidfile: %s: %m", in->pidfile);
- if ((seccomp || setlbf) && asprintf(&ld_preload, "LD_PRELOAD=%s%s%s",
- seccomp ? "/lib/libpreload-seccomp.so" : "",
- seccomp && setlbf ? ":" : "",
- setlbf ? "/lib/libsetlbf.so" : "") > 0)
- putenv(ld_preload);
+ if (setlbf)
+ setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
blobmsg_list_for_each(&in->limits, var)
instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
blobmsg_list_for_each(&in->limits, var)
instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
+ argv[argc++] = "/sbin/utrace";
+ else if (seccomp)
+ argv[argc++] = "/sbin/seccomp-trace";
+#else
+ if (in->trace || seccomp)
+ ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
+#endif
+ if (argc != in->jail.argc)
+ ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
+ in->jail.argc, argc, in->srv->name, in->name);
+ }
blobmsg_for_each_attr(cur, in->command, rem)
argv[argc++] = blobmsg_data(cur);
blobmsg_for_each_attr(cur, in->command, rem)
argv[argc++] = blobmsg_data(cur);
- if (in->gid && setgid(in->gid)) {
- ERROR("failed to set group id %d: %d (%s)\n", in->gid, errno, strerror(errno));
+ if (!in->has_jail && in->user && in->pw_gid && initgroups(in->user, in->pw_gid)) {
+ ERROR("failed to initgroups() for user %s: %m\n", in->user);
+ exit(127);
+ }
+ if (!in->has_jail && in->gr_gid && setgid(in->gr_gid)) {
+ ERROR("failed to set group id %d: %m\n", in->gr_gid);
- if (in->uid && setuid(in->uid)) {
- ERROR("failed to set user id %d: %d (%s)\n", in->uid, errno, strerror(errno));
+ if (!in->has_jail && in->uid && setuid(in->uid)) {
+ ERROR("failed to set user id %d: %m\n", in->uid);
arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
- newline = strchr(str, '\n');
- if (!newline)
+ newline = memchr(str, '\n', len);
+ if (!newline && (s->r.buffer_len != len))
+static void
+instance_delete(struct service_instance *in)
+{
+ struct service *s = in->srv;
+
+ avl_delete(&s->instances.avl, &in->node.avl);
+ instance_free(in);
+ service_stopped(s);
+}
+
+static int
+instance_exit_code(int ret)
+{
+ if (WIFEXITED(ret)) {
+ return WEXITSTATUS(ret);
+ }
+
+ if (WIFSIGNALED(ret)) {
+ return SIGNALLED_OFFSET + WTERMSIG(ret);
+ }
+
+ if (WIFSTOPPED(ret)) {
+ return WSTOPSIG(ret);
+ }
+
+ return 1;
+}
+
DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
uloop_timeout_cancel(&in->timeout);
service_event("instance.stop", in->srv->name, in->name);
uloop_timeout_cancel(&in->timeout);
service_event("instance.stop", in->srv->name, in->name);
in->halt = halt;
in->restart = in->respawn = false;
kill(in->proc.pid, SIGTERM);
in->halt = halt;
in->restart = in->respawn = false;
kill(in->proc.pid, SIGTERM);
- if (!in->pidfile && in_new->pidfile)
+ if ((!in->seccomp && in_new->seccomp) ||
+ (in->seccomp && !in_new->seccomp) ||
+ (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
blobmsg_data(attr), blobmsg_data_len(attr));
jail->argc = 2;
blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
blobmsg_data(attr), blobmsg_data_len(attr));
jail->argc = 2;
+ if (tb[JAIL_ATTR_NETNS]) {
+ jail->netns = blobmsg_get_bool(tb[JAIL_ATTR_NETNS]);
+ jail->argc++;
+ }
+ if (tb[JAIL_ATTR_USERNS]) {
+ jail->userns = blobmsg_get_bool(tb[JAIL_ATTR_USERNS]);
+ jail->argc++;
+ }
+ if (tb[JAIL_ATTR_CGROUPSNS]) {
+ jail->cgroupsns = blobmsg_get_bool(tb[JAIL_ATTR_CGROUPSNS]);
+ jail->argc++;
+ }
+
{
struct blob_attr *tb[__INSTANCE_ATTR_MAX];
struct blob_attr *cur, *cur2;
{
struct blob_attr *tb[__INSTANCE_ATTR_MAX];
struct blob_attr *cur, *cur2;
blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
blobmsg_data(in->config), blobmsg_data_len(in->config));
blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
blobmsg_data(in->config), blobmsg_data_len(in->config));
+ in->gr_gid = in->pw_gid = p->pw_gid;
+ }
+ }
+
+ if (tb[INSTANCE_ATTR_GROUP]) {
+ const char *group = blobmsg_get_string(tb[INSTANCE_ATTR_GROUP]);
+ struct group *p = getgrnam(group);
+ if (p) {
+ in->group = strdup(group);
+ in->gr_gid = p->gr_gid;
if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
- if (!in->trace && tb[INSTANCE_ATTR_SECCOMP]) {
- char *seccomp = blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]);
- struct stat s;
+ if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
+ in->seccomp = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]));
- if (stat(seccomp, &s))
- ERROR("%s: not starting seccomp as %s is missing\n", in->name, seccomp);
- else
- in->seccomp = seccomp;
- }
+ if (tb[INSTANCE_ATTR_EXTROOT])
+ in->extroot = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_EXTROOT]));
+
+ if (tb[INSTANCE_ATTR_OVERLAYDIR])
+ in->overlaydir = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_OVERLAYDIR]));
+
+ if (tb[INSTANCE_ATTR_TMPOVERLAYSIZE])
+ in->tmpoverlaysize = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_TMPOVERLAYSIZE]));
if (!in->trace && tb[INSTANCE_ATTR_JAIL])
in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
if (!in->trace && tb[INSTANCE_ATTR_JAIL])
in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
+ if (in->has_jail) {
+ r = stat(UJAIL_BIN_PATH, &s);
+ if (r < 0) {
+ if (in->require_jail) {
+ ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
+ in->srv->name, in->name, UJAIL_BIN_PATH, r);
+ return false;
+ }
+ DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH, r);
+ in->has_jail = false;
+ }
+ }
+
if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
return false;
if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
return false;
+ if (tb[INSTANCE_ATTR_FACILITY]) {
+ int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
+ if (facility != -1) {
+ in->syslog_facility = facility;
+ DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
+ } else
+ DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
+ }
+
blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
in->trigger = in_src->trigger;
in->command = in_src->command;
blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
in->trigger = in_src->trigger;
in->command = in_src->command;
- in->pidfile = in_src->pidfile;
+ in->respawn = in_src->respawn;
+ in->respawn_retry = in_src->respawn_retry;
+ in->respawn_threshold = in_src->respawn_threshold;
+ in->respawn_timeout = in_src->respawn_timeout;
+ in->syslog_facility = in_src->syslog_facility;
+
+ instance_config_move_strdup(&in->pidfile, in_src->pidfile);
+ instance_config_move_strdup(&in->seccomp, in_src->seccomp);
+ instance_config_move_strdup(&in->jail.name, in_src->jail.name);
+ instance_config_move_strdup(&in->jail.hostname, in_src->jail.hostname);
if (in->command)
blobmsg_add_blob(b, in->command);
blobmsg_add_u32(b, "term_timeout", in->term_timeout);
if (in->command)
blobmsg_add_blob(b, in->command);
blobmsg_add_u32(b, "term_timeout", in->term_timeout);
if (in->has_jail) {
void *r = blobmsg_open_table(b, "jail");
if (in->jail.name)
blobmsg_add_string(b, "name", in->jail.name);
if (in->jail.hostname)
blobmsg_add_string(b, "hostname", in->jail.hostname);
if (in->has_jail) {
void *r = blobmsg_open_table(b, "jail");
if (in->jail.name)
blobmsg_add_string(b, "name", in->jail.name);
if (in->jail.hostname)
blobmsg_add_string(b, "hostname", in->jail.hostname);
blobmsg_add_u8(b, "procfs", in->jail.procfs);
blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
blobmsg_add_u8(b, "ubus", in->jail.ubus);
blobmsg_add_u8(b, "log", in->jail.log);
blobmsg_add_u8(b, "ronly", in->jail.ronly);
blobmsg_add_u8(b, "procfs", in->jail.procfs);
blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
blobmsg_add_u8(b, "ubus", in->jail.ubus);
blobmsg_add_u8(b, "log", in->jail.log);
blobmsg_add_u8(b, "ronly", in->jail.ronly);
+ blobmsg_add_u8(b, "netns", in->jail.netns);
+ blobmsg_add_u8(b, "userns", in->jail.userns);
+ blobmsg_add_u8(b, "cgroupsns", in->jail.cgroupsns);
blobmsg_close_table(b, r);
if (!avl_is_empty(&in->jail.mount.avl)) {
struct blobmsg_list_node *var;
blobmsg_close_table(b, r);
if (!avl_is_empty(&in->jail.mount.avl)) {
struct blobmsg_list_node *var;
+ if (in->extroot)
+ blobmsg_add_string(b, "extroot", in->extroot);
+ if (in->overlaydir)
+ blobmsg_add_string(b, "overlaydir", in->overlaydir);
+ if (in->tmpoverlaysize)
+ blobmsg_add_string(b, "tmpoverlaysize", in->tmpoverlaysize);
+
if (verbose && in->trigger)
blobmsg_add_blob(b, in->trigger);
if (verbose && in->trigger)
blobmsg_add_blob(b, in->trigger);