X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=mount.c;h=cc94ca6f2365cb078275ee9ad323efefbe345c83;hp=d52c11d3cabd9d7f15bef62589c85ff18474eb22;hb=8d567588cad053f79abe603ab113e1b85a92f1da;hpb=e537352b9bfffe6f6286483bff2c7601c78407e3 diff --git a/mount.c b/mount.c index d52c11d3c..cc94ca6f2 100644 --- a/mount.c +++ b/mount.c @@ -32,6 +32,7 @@ #include "log.h" #include "strv.h" #include "mount-setup.h" +#include "unit-name.h" static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = { [MOUNT_DEAD] = UNIT_INACTIVE, @@ -65,6 +66,25 @@ static const char* const state_string_table[_MOUNT_STATE_MAX] = { [MOUNT_MAINTAINANCE] = "maintainance" }; +static char *mount_name_from_where(const char *where) { + assert(where); + + if (streq(where, "/")) + return strdup("-.mount"); + + return unit_name_build_escape(where+1, NULL, ".mount"); +} + +static void service_unwatch_control_pid(Mount *m) { + assert(m); + + if (m->control_pid <= 0) + return; + + unit_unwatch_pid(UNIT(m), m->control_pid); + m->control_pid = 0; +} + static void mount_parameters_done(MountParameters *p) { assert(p); @@ -91,10 +111,7 @@ static void mount_done(Unit *u) { exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX); m->control_command = NULL; - if (m->control_pid > 0) { - unit_unwatch_pid(u, m->control_pid); - m->control_pid = 0; - } + service_unwatch_control_pid(m); unit_unwatch_timer(u, &m->timer_watch); } @@ -105,26 +122,34 @@ static void mount_init(Unit *u) { assert(u); assert(u->meta.load_state == UNIT_STUB); - m->state = 0; - m->from_etc_fstab = false; - m->from_proc_self_mountinfo = false; - m->from_fragment = false; + m->timeout_usec = DEFAULT_TIMEOUT_USEC; + exec_context_init(&m->exec_context); - m->is_mounted = false; - m->just_mounted = false; - m->just_changed = false; + /* We need to make sure that /bin/mount is always called in + * the same process group as us, so that the autofs kernel + * side doesn't send us another mount request while we are + * already trying to comply its last one. */ + m->exec_context.no_setsid = true; - m->timeout_usec = DEFAULT_TIMEOUT_USEC; + m->timer_watch.type = WATCH_INVALID; +} - zero(m->exec_command); - exec_context_init(&m->exec_context); +static int mount_notify_automount(Mount *m, int status) { + Unit *p; + char *k; - m->kill_mode = 0; + assert(m); - m->control_pid = 0; - m->failure = false; + if (!(k = unit_name_change_suffix(UNIT(m)->meta.id, ".automount"))) + return -ENOMEM; - m->timer_watch.type = WATCH_INVALID; + p = manager_get_unit(UNIT(m)->meta.manager, k); + free(k); + + if (!p) + return 0; + + return automount_send_ready(AUTOMOUNT(p), status); } static int mount_add_node_links(Mount *m) { @@ -149,10 +174,10 @@ static int mount_add_node_links(Mount *m) { if (!path_startswith(what, "/dev/")) return 0; - if (!(e = unit_name_escape_path(what+1, ".device"))) + if (!(e = unit_name_build_escape(what+1, NULL, ".device"))) return -ENOMEM; - r = manager_load_unit(UNIT(m)->meta.manager, e, &device); + r = manager_load_unit(UNIT(m)->meta.manager, e, NULL, &device); free(e); if (r < 0) @@ -237,6 +262,8 @@ static int mount_add_target_links(Mount *m) { MountParameters *p; Unit *u; int r; + bool noauto; + bool handle; assert(m); @@ -247,10 +274,10 @@ static int mount_add_target_links(Mount *m) { else return 0; - if (!p->fstype) - return 0; + noauto = mount_test_option(p->options, MNTOPT_NOAUTO); + handle = mount_test_option(p->options, "comment=systemd.mount"); - if (mount_test_option(p->options, MNTOPT_NOAUTO)) + if (noauto && !handle) return 0; if (mount_test_option(p->options, "_netdev") || @@ -259,15 +286,45 @@ static int mount_add_target_links(Mount *m) { else target = SPECIAL_LOCAL_FS_TARGET; - if ((r = manager_load_unit(UNIT(m)->meta.manager, target, &u)) < 0) + if ((r = manager_load_unit(UNIT(m)->meta.manager, target, NULL, &u)) < 0) return r; - if ((r = unit_add_dependency(u, UNIT_WANTS, UNIT(m))) < 0) - return r; + if (handle) + if ((r = unit_add_dependency(u, UNIT_WANTS, UNIT(m))) < 0) + return r; return unit_add_dependency(UNIT(m), UNIT_BEFORE, u); } +static int mount_verify(Mount *m) { + bool b; + char *e; + assert(m); + + if (UNIT(m)->meta.load_state != UNIT_LOADED) + return 0; + + if (!m->where) { + log_error("%s lacks Where setting. Refusing.", UNIT(m)->meta.id); + return -EINVAL; + } + + path_kill_slashes(m->where); + + if (!(e = mount_name_from_where(m->where))) + return -ENOMEM; + + b = unit_has_name(UNIT(m), e); + free(e); + + if (!b) { + log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(m)->meta.id); + return -EINVAL; + } + + return 0; +} + static int mount_load(Unit *u) { Mount *m = MOUNT(u); int r; @@ -301,7 +358,7 @@ static int mount_load(Unit *u) { return r; } - return 0; + return mount_verify(m); } static void mount_set_state(Mount *m, MountState state) { @@ -322,17 +379,26 @@ static void mount_set_state(Mount *m, MountState state) { state != MOUNT_REMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGKILL) { unit_unwatch_timer(UNIT(m), &m->timer_watch); - - if (m->control_pid > 0) { - unit_unwatch_pid(UNIT(m), m->control_pid); - m->control_pid = 0; - } - + service_unwatch_control_pid(m); m->control_command = NULL; } + if (state == MOUNT_MOUNTED || + state == MOUNT_REMOUNTING) + mount_notify_automount(m, 0); + else if (state == MOUNT_DEAD || + state == MOUNT_UNMOUNTING || + state == MOUNT_MOUNTING_SIGTERM || + state == MOUNT_MOUNTING_SIGKILL || + state == MOUNT_REMOUNTING_SIGTERM || + state == MOUNT_REMOUNTING_SIGKILL || + state == MOUNT_UNMOUNTING_SIGTERM || + state == MOUNT_UNMOUNTING_SIGKILL || + state == MOUNT_MAINTAINANCE) + mount_notify_automount(m, -ENODEV); + if (state != old_state) - log_debug("%s changed %s → %s", unit_id(UNIT(m)), state_string_table[old_state], state_string_table[state]); + log_debug("%s changed %s → %s", UNIT(m)->meta.id, state_string_table[old_state], state_string_table[state]); unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state]); } @@ -361,10 +427,12 @@ static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) { 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; @@ -434,21 +502,28 @@ static void mount_enter_dead(Mount *m, bool success) { 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->control_pid > 0) { - int sig; - bool sent = false; - - sig = (state == MOUNT_MOUNTING_SIGTERM || - state == MOUNT_UNMOUNTING_SIGTERM || - state == MOUNT_REMOUNTING_SIGTERM) ? SIGTERM : SIGKILL; + 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) { @@ -459,32 +534,32 @@ static void mount_enter_signal(Mount *m, MountState state, bool success) { sent = true; } - if (!sent) + 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; } } - mount_set_state(m, state); + if (sent) { + if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0) + goto fail; - if (m->control_pid <= 0) + 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_id(UNIT(m)), strerror(-r)); - mount_enter_dead(m, false); -} + log_warning("%s failed to kill processes: %s", UNIT(m)->meta.id, strerror(-r)); -static void mount_enter_mounted(Mount *m, bool success) { - assert(m); - - if (!success) - m->failure = true; - - mount_set_state(m, MOUNT_MOUNTED); + 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) { @@ -505,6 +580,8 @@ static void mount_enter_unmounting(Mount *m, bool success) { NULL)) < 0) goto fail; + service_unwatch_control_pid(m); + if ((r = mount_spawn(m, c, &m->control_pid)) < 0) goto fail; @@ -513,19 +590,16 @@ static void mount_enter_unmounting(Mount *m, bool success) { return; fail: - log_warning("%s failed to run umount exectuable: %s", unit_id(UNIT(m)), strerror(-r)); + 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, bool success) { +static void mount_enter_mounting(Mount *m) { ExecCommand *c; int r; assert(m); - if (!success) - m->failure = true; - m->control_command = c = m->exec_command + MOUNT_EXEC_MOUNT; if (m->from_fragment) @@ -535,7 +609,7 @@ static void mount_enter_mounting(Mount *m, bool success) { m->parameters_fragment.what, m->where, "-t", m->parameters_fragment.fstype, - "-o", m->parameters_fragment.options, + m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options, NULL); else if (m->from_etc_fstab) r = exec_command_set( @@ -549,6 +623,8 @@ static void mount_enter_mounting(Mount *m, bool success) { if (r < 0) goto fail; + service_unwatch_control_pid(m); + if ((r = mount_spawn(m, c, &m->control_pid)) < 0) goto fail; @@ -557,16 +633,13 @@ static void mount_enter_mounting(Mount *m, bool success) { return; fail: - log_warning("%s failed to run mount exectuable: %s", unit_id(UNIT(m)), strerror(-r)); + 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, bool success) { +static void mount_enter_mounting_done(Mount *m) { assert(m); - if (!success) - m->failure = true; - mount_set_state(m, MOUNT_MOUNTING_DONE); } @@ -620,6 +693,8 @@ static void mount_enter_remounting(Mount *m, bool success) { goto fail; } + service_unwatch_control_pid(m); + if ((r = mount_spawn(m, c, &m->control_pid)) < 0) goto fail; @@ -653,7 +728,7 @@ static int mount_start(Unit *u) { m->failure = false; - mount_enter_mounting(m, true); + mount_enter_mounting(m); return 0; } @@ -704,6 +779,12 @@ static UnitActiveState mount_active_state(Unit *u) { return state_translation_table[MOUNT(u)->state]; } +static const char *mount_sub_state_to_string(Unit *u) { + assert(u); + + return state_string_table[MOUNT(u)->state]; +} + static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) { Mount *m = MOUNT(u); bool success; @@ -720,7 +801,7 @@ static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) { exec_status_fill(&m->control_command->exec_status, pid, code, status); m->control_pid = 0; - log_debug("%s control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status); + 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 @@ -775,39 +856,39 @@ static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) { case MOUNT_MOUNTING: case MOUNT_MOUNTING_DONE: - log_warning("%s mounting timed out. Stopping.", unit_id(u)); + 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.", unit_id(u)); + 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.", unit_id(u)); + 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.", unit_id(u)); + 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.", unit_id(u)); + 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.", unit_id(u)); + 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.", unit_id(u)); + log_warning("%s mount process still around after SIGKILL. Ignoring.", u->meta.id); if (m->from_proc_self_mountinfo) mount_enter_mounted(m, false); @@ -847,14 +928,14 @@ static int mount_add_one( if (mount_point_is_api(where)) return 0; + if (streq(fstype, "autofs")) + return 0; + /* probably some kind of swap, which we don't cover for now */ if (where[0] != '/') return 0; - if (streq(where, "/")) - e = strdup("-.mount"); - else - e = unit_name_escape_path(where+1, ".mount"); + e = mount_name_from_where(where); if (!e) return -ENOMEM; @@ -1182,7 +1263,7 @@ void mount_fd_event(Manager *m, int events) { break; case MOUNT_MOUNTING: - mount_enter_mounting_done(mount, true); + mount_enter_mounting_done(mount); break; default: @@ -1220,7 +1301,7 @@ int mount_path_is_mounted(Manager *m, const char* path) { char *e, *slash; Unit *u; - if (!(e = unit_name_escape_path(t+1, ".mount"))) { + if (!(e = mount_name_from_where(t))) { r = -ENOMEM; goto finish; } @@ -1256,6 +1337,7 @@ const UnitVTable mount_vtable = { .suffix = ".mount", .no_alias = true, + .no_instances = true, .init = mount_init, .load = mount_load, @@ -1270,6 +1352,7 @@ const UnitVTable mount_vtable = { .reload = mount_reload, .active_state = mount_active_state, + .sub_state_to_string = mount_sub_state_to_string, .sigchld_event = mount_sigchld_event, .timer_event = mount_timer_event,