+static void mount_dump(Unit *u, FILE *f, const char *prefix) {
+ Mount *m = MOUNT(u);
+ MountParameters *p;
+
+ assert(m);
+ assert(f);
+
+ if (m->from_proc_self_mountinfo)
+ p = &m->parameters_proc_self_mountinfo;
+ else if (m->from_fragment)
+ p = &m->parameters_fragment;
+ else
+ p = &m->parameters_etc_fstab;
+
+ fprintf(f,
+ "%sMount State: %s\n"
+ "%sWhere: %s\n"
+ "%sWhat: %s\n"
+ "%sFile System Type: %s\n"
+ "%sOptions: %s\n"
+ "%sFrom /etc/fstab: %s\n"
+ "%sFrom /proc/self/mountinfo: %s\n"
+ "%sFrom fragment: %s\n"
+ "%sKillMode: %s\n",
+ prefix, mount_state_to_string(m->state),
+ prefix, m->where,
+ prefix, strna(p->what),
+ prefix, strna(p->fstype),
+ prefix, strna(p->options),
+ prefix, yes_no(m->from_etc_fstab),
+ prefix, yes_no(m->from_proc_self_mountinfo),
+ prefix, yes_no(m->from_fragment),
+ prefix, kill_mode_to_string(m->kill_mode));
+
+ if (m->control_pid > 0)
+ fprintf(f,
+ "%sControl PID: %llu\n",
+ prefix, (unsigned long long) m->control_pid);
+
+ exec_context_dump(&m->exec_context, f, prefix);
+}
+
+static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
+ pid_t pid;
+ int r;
+
+ assert(m);
+ assert(c);
+ assert(_pid);
+
+ if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
+ goto fail;
+
+ if ((r = exec_spawn(c,
+ NULL,
+ &m->exec_context,
+ NULL, 0,
+ true,
+ true,
+ UNIT(m)->meta.manager->confirm_spawn,
+ UNIT(m)->meta.cgroup_bondings,
+ &pid)) < 0)
+ goto fail;
+
+ if ((r = unit_watch_pid(UNIT(m), pid)) < 0)
+ /* FIXME: we need to do something here */
+ goto fail;
+
+ *_pid = pid;
+
+ return 0;
+
+fail:
+ unit_unwatch_timer(UNIT(m), &m->timer_watch);
+
+ return r;
+}
+
+static void mount_enter_dead(Mount *m, bool success) {
+ assert(m);
+
+ if (!success)
+ m->failure = true;
+
+ mount_set_state(m, m->failure ? MOUNT_MAINTAINANCE : MOUNT_DEAD);
+}
+
+static void mount_enter_mounted(Mount *m, bool success) {
+ assert(m);
+
+ if (!success)
+ m->failure = true;
+
+ mount_set_state(m, MOUNT_MOUNTED);
+}
+
+static void mount_enter_signal(Mount *m, MountState state, bool success) {
+ int r;
+ bool sent = false;
+
+ assert(m);
+
+ if (!success)
+ m->failure = true;
+
+ if (m->kill_mode != KILL_NONE) {
+ int sig = (state == MOUNT_MOUNTING_SIGTERM ||
+ state == MOUNT_UNMOUNTING_SIGTERM ||
+ state == MOUNT_REMOUNTING_SIGTERM) ? SIGTERM : SIGKILL;
+
+ if (m->kill_mode == KILL_CONTROL_GROUP) {
+
+ if ((r = cgroup_bonding_kill_list(UNIT(m)->meta.cgroup_bondings, sig)) < 0) {
+ if (r != -EAGAIN && r != -ESRCH)
+ goto fail;
+ } else
+ sent = true;
+ }
+
+ if (!sent && m->control_pid > 0)
+ if (kill(m->kill_mode == KILL_PROCESS ? m->control_pid : -m->control_pid, sig) < 0 && errno != ESRCH) {
+ r = -errno;
+ goto fail;
+ }
+ }
+
+ if (sent) {
+ if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
+ goto fail;
+
+ mount_set_state(m, state);
+ } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
+ mount_enter_mounted(m, true);
+ else
+ mount_enter_dead(m, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to kill processes: %s", UNIT(m)->meta.id, strerror(-r));
+
+ if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
+ mount_enter_mounted(m, false);
+ else
+ mount_enter_dead(m, false);
+}
+
+static void mount_enter_unmounting(Mount *m, bool success) {
+ int r;
+
+ assert(m);
+
+ if (!success)
+ m->failure = true;
+
+ m->control_command_id = MOUNT_EXEC_UNMOUNT;
+ m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
+
+ if ((r = exec_command_set(
+ m->control_command,
+ "/bin/umount",
+ m->where,
+ NULL)) < 0)
+ goto fail;
+
+ mount_unwatch_control_pid(m);
+
+ if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
+ goto fail;
+
+ mount_set_state(m, MOUNT_UNMOUNTING);
+
+ return;
+
+fail:
+ log_warning("%s failed to run umount exectuable: %s", UNIT(m)->meta.id, strerror(-r));
+ mount_enter_mounted(m, false);
+}
+
+static void mount_enter_mounting(Mount *m) {
+ int r;
+
+ assert(m);
+
+ m->control_command_id = MOUNT_EXEC_MOUNT;
+ m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
+
+ if (m->from_fragment)
+ r = exec_command_set(
+ m->control_command,
+ "/bin/mount",
+ m->parameters_fragment.what,
+ m->where,
+ "-t", m->parameters_fragment.fstype,
+ m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
+ NULL);
+ else if (m->from_etc_fstab)
+ r = exec_command_set(
+ m->control_command,
+ "/bin/mount",
+ m->where,
+ NULL);
+ else
+ r = -ENOENT;
+
+ if (r < 0)
+ goto fail;
+
+ mount_unwatch_control_pid(m);
+
+ if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
+ goto fail;
+
+ mount_set_state(m, MOUNT_MOUNTING);
+
+ return;
+
+fail:
+ log_warning("%s failed to run mount exectuable: %s", UNIT(m)->meta.id, strerror(-r));
+ mount_enter_dead(m, false);
+}
+
+static void mount_enter_mounting_done(Mount *m) {
+ assert(m);
+
+ mount_set_state(m, MOUNT_MOUNTING_DONE);
+}
+
+static void mount_enter_remounting(Mount *m, bool success) {
+ int r;
+
+ assert(m);
+
+ if (!success)
+ m->failure = true;
+
+ m->control_command_id = MOUNT_EXEC_REMOUNT;
+ m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
+
+ if (m->from_fragment) {
+ char *buf = NULL;
+ const char *o;
+
+ if (m->parameters_fragment.options) {
+ if (!(buf = strappend("remount,", m->parameters_fragment.options))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ o = buf;
+ } else
+ o = "remount";
+
+ r = exec_command_set(
+ m->control_command,
+ "/bin/mount",
+ m->parameters_fragment.what,
+ m->where,
+ "-t", m->parameters_fragment.fstype,
+ "-o", o,
+ NULL);
+
+ free(buf);
+ } else if (m->from_etc_fstab)
+ r = exec_command_set(
+ m->control_command,
+ "/bin/mount",
+ m->where,
+ "-o", "remount",
+ NULL);
+ else
+ r = -ENOENT;
+
+ if (r < 0) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ mount_unwatch_control_pid(m);
+
+ if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
+ goto fail;
+
+ mount_set_state(m, MOUNT_REMOUNTING);
+
+ return;
+
+fail:
+ mount_enter_mounted(m, false);
+}
+
+static int mount_start(Unit *u) {
+ Mount *m = MOUNT(u);
+
+ assert(m);
+
+ /* We cannot fulfill this request right now, try again later
+ * please! */
+ if (m->state == MOUNT_UNMOUNTING ||
+ m->state == MOUNT_UNMOUNTING_SIGTERM ||
+ m->state == MOUNT_UNMOUNTING_SIGKILL)
+ return -EAGAIN;
+
+ /* Already on it! */
+ if (m->state == MOUNT_MOUNTING ||
+ m->state == MOUNT_MOUNTING_SIGTERM ||
+ m->state == MOUNT_MOUNTING_SIGKILL)
+ return 0;
+
+ assert(m->state == MOUNT_DEAD || m->state == MOUNT_MAINTAINANCE);
+
+ m->failure = false;
+ mount_enter_mounting(m);
+ return 0;
+}
+
+static int mount_stop(Unit *u) {
+ Mount *m = MOUNT(u);
+
+ assert(m);
+
+ /* Cann't do this right now. */
+ if (m->state == MOUNT_MOUNTING ||
+ m->state == MOUNT_MOUNTING_DONE ||
+ m->state == MOUNT_MOUNTING_SIGTERM ||
+ m->state == MOUNT_MOUNTING_SIGKILL ||
+ m->state == MOUNT_REMOUNTING ||
+ m->state == MOUNT_REMOUNTING_SIGTERM ||
+ m->state == MOUNT_REMOUNTING_SIGKILL)
+ return -EAGAIN;
+
+ /* Already on it */
+ if (m->state == MOUNT_UNMOUNTING ||
+ m->state == MOUNT_UNMOUNTING_SIGKILL ||
+ m->state == MOUNT_UNMOUNTING_SIGTERM)
+ return 0;
+
+ assert(m->state == MOUNT_MOUNTED);
+
+ mount_enter_unmounting(m, true);
+ return 0;
+}
+
+static int mount_reload(Unit *u) {
+ Mount *m = MOUNT(u);
+
+ assert(m);
+
+ if (m->state == MOUNT_MOUNTING_DONE)
+ return -EAGAIN;
+
+ assert(m->state == MOUNT_MOUNTED);
+
+ mount_enter_remounting(m, true);
+ return 0;
+}
+
+static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
+ Mount *m = MOUNT(u);
+
+ assert(m);
+ assert(f);
+ assert(fds);
+
+ unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
+ unit_serialize_item(u, f, "failure", yes_no(m->failure));
+
+ if (m->control_pid > 0)
+ unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) m->control_pid);
+
+ if (m->control_command_id >= 0)
+ unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
+
+ return 0;
+}
+
+static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
+ Mount *m = MOUNT(u);
+ int r;
+
+ assert(u);
+ assert(key);
+ assert(value);
+ assert(fds);
+
+ if (streq(key, "state")) {
+ MountState state;
+
+ if ((state = mount_state_from_string(value)) < 0)
+ log_debug("Failed to parse state value %s", value);
+ else
+ m->deserialized_state = state;
+ } else if (streq(key, "failure")) {
+ int b;
+
+ if ((b = parse_boolean(value)) < 0)
+ log_debug("Failed to parse failure value %s", value);
+ else
+ m->failure = b || m->failure;
+
+ } else if (streq(key, "control-pid")) {
+ unsigned pid;
+
+ if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
+ log_debug("Failed to parse control-pid value %s", value);
+ else
+ m->control_pid = (pid_t) pid;
+ } else if (streq(key, "control-command")) {
+ MountExecCommand id;
+
+ if ((id = mount_exec_command_from_string(value)) < 0)
+ log_debug("Failed to parse exec-command value %s", value);
+ else {
+ m->control_command_id = id;
+ m->control_command = m->exec_command + id;
+ }
+
+ } else
+ log_debug("Unknown serialization key '%s'", key);
+
+ return 0;
+}
+
+static UnitActiveState mount_active_state(Unit *u) {
+ assert(u);
+
+ return state_translation_table[MOUNT(u)->state];
+}
+
+static const char *mount_sub_state_to_string(Unit *u) {
+ assert(u);
+
+ return mount_state_to_string(MOUNT(u)->state);
+}
+
+static bool mount_check_gc(Unit *u) {
+ Mount *m = MOUNT(u);
+
+ assert(m);
+
+ return m->from_etc_fstab || m->from_proc_self_mountinfo;
+}
+
+static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
+ Mount *m = MOUNT(u);
+ bool success;
+
+ assert(m);
+ assert(pid >= 0);
+
+ success = code == CLD_EXITED && status == 0;
+ m->failure = m->failure || !success;
+
+ assert(m->control_pid == pid);
+ m->control_pid = 0;
+
+ if (m->control_command) {
+ exec_status_fill(&m->control_command->exec_status, pid, code, status);
+ m->control_command = NULL;
+ m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
+ }
+
+ log_debug("%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
+
+ /* Note that mount(8) returning and the kernel sending us a
+ * mount table change event might happen out-of-order. If an
+ * operation succeed we assume the kernel will follow soon too
+ * and already change into the resulting state. If it fails
+ * we check if the kernel still knows about the mount. and
+ * change state accordingly. */
+
+ switch (m->state) {
+
+ case MOUNT_MOUNTING:
+ case MOUNT_MOUNTING_DONE:
+ case MOUNT_MOUNTING_SIGKILL:
+ case MOUNT_MOUNTING_SIGTERM:
+ case MOUNT_REMOUNTING:
+ case MOUNT_REMOUNTING_SIGKILL:
+ case MOUNT_REMOUNTING_SIGTERM:
+
+ if (success && m->from_proc_self_mountinfo)
+ mount_enter_mounted(m, true);
+ else if (m->from_proc_self_mountinfo)
+ mount_enter_mounted(m, false);
+ else
+ mount_enter_dead(m, false);
+ break;
+
+ case MOUNT_UNMOUNTING:
+ case MOUNT_UNMOUNTING_SIGKILL:
+ case MOUNT_UNMOUNTING_SIGTERM:
+
+ if (success)
+ mount_enter_dead(m, true);
+ else if (m->from_proc_self_mountinfo)
+ mount_enter_mounted(m, false);
+ else
+ mount_enter_dead(m, false);
+ break;
+
+ default:
+ assert_not_reached("Uh, control process died at wrong time.");
+ }
+}
+
+static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
+ Mount *m = MOUNT(u);
+
+ assert(m);
+ assert(elapsed == 1);
+ assert(w == &m->timer_watch);
+
+ switch (m->state) {
+
+ case MOUNT_MOUNTING:
+ case MOUNT_MOUNTING_DONE:
+ log_warning("%s mounting timed out. Stopping.", u->meta.id);
+ mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, false);
+ break;
+
+ case MOUNT_REMOUNTING:
+ log_warning("%s remounting timed out. Stopping.", u->meta.id);
+ mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, false);
+ break;
+
+ case MOUNT_UNMOUNTING:
+ log_warning("%s unmounting timed out. Stopping.", u->meta.id);
+ mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, false);
+ break;
+
+ case MOUNT_MOUNTING_SIGTERM:
+ log_warning("%s mounting timed out. Killing.", u->meta.id);
+ mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, false);
+ break;
+
+ case MOUNT_REMOUNTING_SIGTERM:
+ log_warning("%s remounting timed out. Killing.", u->meta.id);
+ mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, false);
+ break;
+
+ case MOUNT_UNMOUNTING_SIGTERM:
+ log_warning("%s unmounting timed out. Killing.", u->meta.id);
+ mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, false);
+ break;
+
+ case MOUNT_MOUNTING_SIGKILL:
+ case MOUNT_REMOUNTING_SIGKILL:
+ case MOUNT_UNMOUNTING_SIGKILL:
+ log_warning("%s mount process still around after SIGKILL. Ignoring.", u->meta.id);
+
+ if (m->from_proc_self_mountinfo)
+ mount_enter_mounted(m, false);
+ else
+ mount_enter_dead(m, false);
+ break;
+
+ default:
+ assert_not_reached("Timeout at wrong time.");
+ }
+}
+
+static int mount_add_one(
+ Manager *m,
+ const char *what,
+ const char *where,
+ const char *options,
+ const char *fstype,
+ bool from_proc_self_mountinfo,
+ bool set_flags) {