1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/epoll.h>
30 #include "load-fragment.h"
31 #include "load-dropin.h"
34 #include "mount-setup.h"
35 #include "unit-name.h"
37 #include "dbus-mount.h"
39 static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
40 [MOUNT_DEAD] = UNIT_INACTIVE,
41 [MOUNT_MOUNTING] = UNIT_ACTIVATING,
42 [MOUNT_MOUNTING_DONE] = UNIT_ACTIVE,
43 [MOUNT_MOUNTED] = UNIT_ACTIVE,
44 [MOUNT_REMOUNTING] = UNIT_ACTIVE_RELOADING,
45 [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
46 [MOUNT_MOUNTING_SIGTERM] = UNIT_DEACTIVATING,
47 [MOUNT_MOUNTING_SIGKILL] = UNIT_DEACTIVATING,
48 [MOUNT_REMOUNTING_SIGTERM] = UNIT_ACTIVE_RELOADING,
49 [MOUNT_REMOUNTING_SIGKILL] = UNIT_ACTIVE_RELOADING,
50 [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
51 [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
52 [MOUNT_MAINTAINANCE] = UNIT_INACTIVE,
55 static void mount_init(Unit *u) {
59 assert(u->meta.load_state == UNIT_STUB);
61 m->timeout_usec = DEFAULT_TIMEOUT_USEC;
62 exec_context_init(&m->exec_context);
64 /* We need to make sure that /bin/mount is always called in
65 * the same process group as us, so that the autofs kernel
66 * side doesn't send us another mount request while we are
67 * already trying to comply its last one. */
68 m->exec_context.no_setsid = true;
70 m->timer_watch.type = WATCH_INVALID;
72 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
75 static void mount_unwatch_control_pid(Mount *m) {
78 if (m->control_pid <= 0)
81 unit_unwatch_pid(UNIT(m), m->control_pid);
85 static void mount_parameters_done(MountParameters *p) {
92 p->what = p->options = p->fstype = NULL;
95 static void mount_done(Unit *u) {
103 mount_parameters_done(&m->parameters_etc_fstab);
104 mount_parameters_done(&m->parameters_proc_self_mountinfo);
105 mount_parameters_done(&m->parameters_fragment);
107 exec_context_done(&m->exec_context);
108 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
109 m->control_command = NULL;
111 mount_unwatch_control_pid(m);
113 unit_unwatch_timer(u, &m->timer_watch);
116 static int mount_add_mount_links(Mount *m) {
122 /* Adds in links to other mount points that might lie below or
123 * above us in the hierarchy */
125 LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_MOUNT]) {
126 Mount *n = (Mount*) other;
131 if (n->meta.load_state != UNIT_LOADED)
134 if (path_startswith(m->where, n->where)) {
136 if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(n), true)) < 0)
139 if (n->from_etc_fstab || n->from_fragment)
140 if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(n), true)) < 0)
143 } else if (path_startswith(n->where, m->where)) {
145 if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(n), true)) < 0)
148 if (m->from_etc_fstab || m->from_fragment)
149 if ((r = unit_add_dependency(UNIT(n), UNIT_REQUIRES, UNIT(m), true)) < 0)
157 static int mount_add_swap_links(Mount *m) {
163 LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_SWAP])
164 if ((r = swap_add_one_mount_link((Swap*) other, m)) < 0)
170 static int mount_add_automount_links(Mount *m) {
176 LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_AUTOMOUNT])
177 if ((r = automount_add_one_mount_link((Automount*) other, m)) < 0)
183 static int mount_add_socket_links(Mount *m) {
189 LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_SOCKET])
190 if ((r = socket_add_one_mount_link((Socket*) other, m)) < 0)
196 static char* mount_test_option(const char *haystack, const char *needle) {
201 /* Like glibc's hasmntopt(), but works on a string, not a
208 me.mnt_opts = (char*) haystack;
210 return hasmntopt(&me, needle);
213 static int mount_add_target_links(Mount *m) {
218 bool noauto, handle, automount;
222 if (m->from_fragment)
223 p = &m->parameters_fragment;
224 else if (m->from_etc_fstab)
225 p = &m->parameters_etc_fstab;
229 noauto = !!mount_test_option(p->options, MNTOPT_NOAUTO);
230 handle = !!mount_test_option(p->options, "comment=systemd.mount");
231 automount = !!mount_test_option(p->options, "comment=systemd.automount");
233 if (mount_test_option(p->options, "_netdev") ||
234 fstype_is_network(p->fstype))
235 target = SPECIAL_REMOTE_FS_TARGET;
237 target = SPECIAL_LOCAL_FS_TARGET;
239 if ((r = manager_load_unit(UNIT(m)->meta.manager, target, NULL, &tu)) < 0)
245 if ((r = unit_load_related_unit(UNIT(m), ".automount", &am)) < 0)
248 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(am), true)) < 0)
251 return unit_add_dependency(UNIT(am), UNIT_BEFORE, tu, true);
255 if (!noauto && handle)
256 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(m), true)) < 0)
259 return unit_add_dependency(UNIT(m), UNIT_BEFORE, tu, true);
263 static int mount_verify(Mount *m) {
268 if (UNIT(m)->meta.load_state != UNIT_LOADED)
271 if (!(e = unit_name_from_path(m->where, ".mount")))
274 b = unit_has_name(UNIT(m), e);
278 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(m)->meta.id);
282 if (m->meta.fragment_path && !m->parameters_fragment.what) {
283 log_error("%s's What setting is missing. Refusing.", UNIT(m)->meta.id);
290 static int mount_load(Unit *u) {
295 assert(u->meta.load_state == UNIT_STUB);
297 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
300 /* This is a new unit? Then let's add in some extras */
301 if (u->meta.load_state == UNIT_LOADED) {
302 const char *what = NULL;
304 if (m->meta.fragment_path)
305 m->from_fragment = true;
308 if (!(m->where = unit_name_to_path(u->meta.id)))
311 path_kill_slashes(m->where);
313 if (!m->meta.description)
314 if ((r = unit_set_description(u, m->where)) < 0)
317 if (m->from_fragment && m->parameters_fragment.what)
318 what = m->parameters_fragment.what;
319 else if (m->from_etc_fstab && m->parameters_etc_fstab.what)
320 what = m->parameters_etc_fstab.what;
321 else if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.what)
322 what = m->parameters_proc_self_mountinfo.what;
325 if ((r = unit_add_node_link(u, what,
326 (u->meta.manager->running_as == MANAGER_INIT ||
327 u->meta.manager->running_as == MANAGER_SYSTEM))) < 0)
330 if ((r = mount_add_mount_links(m)) < 0)
333 if ((r = mount_add_socket_links(m)) < 0)
336 if ((r = mount_add_swap_links(m)) < 0)
339 if ((r = mount_add_automount_links(m)) < 0)
342 if ((r = mount_add_target_links(m)) < 0)
345 if ((r = unit_add_default_cgroup(u)) < 0)
349 return mount_verify(m);
352 static int mount_notify_automount(Mount *m, int status) {
358 if ((r = unit_get_related_unit(UNIT(m), ".automount", &p)) < 0)
359 return r == -ENOENT ? 0 : r;
361 return automount_send_ready(AUTOMOUNT(p), status);
364 static void mount_set_state(Mount *m, MountState state) {
365 MountState old_state;
368 old_state = m->state;
371 if (state != MOUNT_MOUNTING &&
372 state != MOUNT_MOUNTING_DONE &&
373 state != MOUNT_REMOUNTING &&
374 state != MOUNT_UNMOUNTING &&
375 state != MOUNT_MOUNTING_SIGTERM &&
376 state != MOUNT_MOUNTING_SIGKILL &&
377 state != MOUNT_UNMOUNTING_SIGTERM &&
378 state != MOUNT_UNMOUNTING_SIGKILL &&
379 state != MOUNT_REMOUNTING_SIGTERM &&
380 state != MOUNT_REMOUNTING_SIGKILL) {
381 unit_unwatch_timer(UNIT(m), &m->timer_watch);
382 mount_unwatch_control_pid(m);
383 m->control_command = NULL;
384 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
387 if (state == MOUNT_MOUNTED ||
388 state == MOUNT_REMOUNTING)
389 mount_notify_automount(m, 0);
390 else if (state == MOUNT_DEAD ||
391 state == MOUNT_UNMOUNTING ||
392 state == MOUNT_MOUNTING_SIGTERM ||
393 state == MOUNT_MOUNTING_SIGKILL ||
394 state == MOUNT_REMOUNTING_SIGTERM ||
395 state == MOUNT_REMOUNTING_SIGKILL ||
396 state == MOUNT_UNMOUNTING_SIGTERM ||
397 state == MOUNT_UNMOUNTING_SIGKILL ||
398 state == MOUNT_MAINTAINANCE)
399 mount_notify_automount(m, -ENODEV);
401 if (state != old_state)
402 log_debug("%s changed %s -> %s",
404 mount_state_to_string(old_state),
405 mount_state_to_string(state));
407 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state]);
410 static int mount_coldplug(Unit *u) {
412 MountState new_state = MOUNT_DEAD;
416 assert(m->state == MOUNT_DEAD);
418 if (m->deserialized_state != m->state)
419 new_state = m->deserialized_state;
420 else if (m->from_proc_self_mountinfo)
421 new_state = MOUNT_MOUNTED;
423 if (new_state != m->state) {
425 if (new_state == MOUNT_MOUNTING ||
426 new_state == MOUNT_MOUNTING_DONE ||
427 new_state == MOUNT_REMOUNTING ||
428 new_state == MOUNT_UNMOUNTING ||
429 new_state == MOUNT_MOUNTING_SIGTERM ||
430 new_state == MOUNT_MOUNTING_SIGKILL ||
431 new_state == MOUNT_UNMOUNTING_SIGTERM ||
432 new_state == MOUNT_UNMOUNTING_SIGKILL ||
433 new_state == MOUNT_REMOUNTING_SIGTERM ||
434 new_state == MOUNT_REMOUNTING_SIGKILL) {
436 if (m->control_pid <= 0)
439 if ((r = unit_watch_pid(UNIT(m), m->control_pid)) < 0)
442 if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
446 mount_set_state(m, new_state);
452 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
459 if (m->from_proc_self_mountinfo)
460 p = &m->parameters_proc_self_mountinfo;
461 else if (m->from_fragment)
462 p = &m->parameters_fragment;
464 p = &m->parameters_etc_fstab;
467 "%sMount State: %s\n"
470 "%sFile System Type: %s\n"
472 "%sFrom /etc/fstab: %s\n"
473 "%sFrom /proc/self/mountinfo: %s\n"
474 "%sFrom fragment: %s\n"
476 prefix, mount_state_to_string(m->state),
478 prefix, strna(p->what),
479 prefix, strna(p->fstype),
480 prefix, strna(p->options),
481 prefix, yes_no(m->from_etc_fstab),
482 prefix, yes_no(m->from_proc_self_mountinfo),
483 prefix, yes_no(m->from_fragment),
484 prefix, kill_mode_to_string(m->kill_mode));
486 if (m->control_pid > 0)
488 "%sControl PID: %llu\n",
489 prefix, (unsigned long long) m->control_pid);
491 exec_context_dump(&m->exec_context, f, prefix);
494 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
502 if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
505 if ((r = exec_spawn(c,
509 m->meta.manager->environment,
512 UNIT(m)->meta.manager->confirm_spawn,
513 UNIT(m)->meta.cgroup_bondings,
517 if ((r = unit_watch_pid(UNIT(m), pid)) < 0)
518 /* FIXME: we need to do something here */
526 unit_unwatch_timer(UNIT(m), &m->timer_watch);
531 static void mount_enter_dead(Mount *m, bool success) {
537 mount_set_state(m, m->failure ? MOUNT_MAINTAINANCE : MOUNT_DEAD);
540 static void mount_enter_mounted(Mount *m, bool success) {
546 mount_set_state(m, MOUNT_MOUNTED);
549 static void mount_enter_signal(Mount *m, MountState state, bool success) {
558 if (m->kill_mode != KILL_NONE) {
559 int sig = (state == MOUNT_MOUNTING_SIGTERM ||
560 state == MOUNT_UNMOUNTING_SIGTERM ||
561 state == MOUNT_REMOUNTING_SIGTERM) ? SIGTERM : SIGKILL;
563 if (m->kill_mode == KILL_CONTROL_GROUP) {
565 if ((r = cgroup_bonding_kill_list(UNIT(m)->meta.cgroup_bondings, sig)) < 0) {
566 if (r != -EAGAIN && r != -ESRCH)
572 if (!sent && m->control_pid > 0)
573 if (kill(m->kill_mode == KILL_PROCESS ? m->control_pid : -m->control_pid, sig) < 0 && errno != ESRCH) {
580 if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
583 mount_set_state(m, state);
584 } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
585 mount_enter_mounted(m, true);
587 mount_enter_dead(m, true);
592 log_warning("%s failed to kill processes: %s", UNIT(m)->meta.id, strerror(-r));
594 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
595 mount_enter_mounted(m, false);
597 mount_enter_dead(m, false);
600 static void mount_enter_unmounting(Mount *m, bool success) {
608 m->control_command_id = MOUNT_EXEC_UNMOUNT;
609 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
611 if ((r = exec_command_set(
618 mount_unwatch_control_pid(m);
620 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
623 mount_set_state(m, MOUNT_UNMOUNTING);
628 log_warning("%s failed to run umount exectuable: %s", UNIT(m)->meta.id, strerror(-r));
629 mount_enter_mounted(m, false);
632 static void mount_enter_mounting(Mount *m) {
637 m->control_command_id = MOUNT_EXEC_MOUNT;
638 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
640 if (m->from_fragment)
641 r = exec_command_set(
644 m->parameters_fragment.what,
646 "-t", m->parameters_fragment.fstype,
647 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
649 else if (m->from_etc_fstab)
650 r = exec_command_set(
661 mount_unwatch_control_pid(m);
663 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
666 mount_set_state(m, MOUNT_MOUNTING);
671 log_warning("%s failed to run mount exectuable: %s", UNIT(m)->meta.id, strerror(-r));
672 mount_enter_dead(m, false);
675 static void mount_enter_mounting_done(Mount *m) {
678 mount_set_state(m, MOUNT_MOUNTING_DONE);
681 static void mount_enter_remounting(Mount *m, bool success) {
689 m->control_command_id = MOUNT_EXEC_REMOUNT;
690 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
692 if (m->from_fragment) {
696 if (m->parameters_fragment.options) {
697 if (!(buf = strappend("remount,", m->parameters_fragment.options))) {
706 r = exec_command_set(
709 m->parameters_fragment.what,
711 "-t", m->parameters_fragment.fstype,
716 } else if (m->from_etc_fstab)
717 r = exec_command_set(
731 mount_unwatch_control_pid(m);
733 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
736 mount_set_state(m, MOUNT_REMOUNTING);
741 mount_enter_mounted(m, false);
744 static int mount_start(Unit *u) {
749 /* We cannot fulfill this request right now, try again later
751 if (m->state == MOUNT_UNMOUNTING ||
752 m->state == MOUNT_UNMOUNTING_SIGTERM ||
753 m->state == MOUNT_UNMOUNTING_SIGKILL)
757 if (m->state == MOUNT_MOUNTING ||
758 m->state == MOUNT_MOUNTING_SIGTERM ||
759 m->state == MOUNT_MOUNTING_SIGKILL)
762 assert(m->state == MOUNT_DEAD || m->state == MOUNT_MAINTAINANCE);
765 mount_enter_mounting(m);
769 static int mount_stop(Unit *u) {
774 /* Cann't do this right now. */
775 if (m->state == MOUNT_MOUNTING ||
776 m->state == MOUNT_MOUNTING_DONE ||
777 m->state == MOUNT_MOUNTING_SIGTERM ||
778 m->state == MOUNT_MOUNTING_SIGKILL ||
779 m->state == MOUNT_REMOUNTING ||
780 m->state == MOUNT_REMOUNTING_SIGTERM ||
781 m->state == MOUNT_REMOUNTING_SIGKILL)
785 if (m->state == MOUNT_UNMOUNTING ||
786 m->state == MOUNT_UNMOUNTING_SIGKILL ||
787 m->state == MOUNT_UNMOUNTING_SIGTERM)
790 assert(m->state == MOUNT_MOUNTED);
792 mount_enter_unmounting(m, true);
796 static int mount_reload(Unit *u) {
801 if (m->state == MOUNT_MOUNTING_DONE)
804 assert(m->state == MOUNT_MOUNTED);
806 mount_enter_remounting(m, true);
810 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
817 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
818 unit_serialize_item(u, f, "failure", yes_no(m->failure));
820 if (m->control_pid > 0)
821 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) m->control_pid);
823 if (m->control_command_id >= 0)
824 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
829 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
838 if (streq(key, "state")) {
841 if ((state = mount_state_from_string(value)) < 0)
842 log_debug("Failed to parse state value %s", value);
844 m->deserialized_state = state;
845 } else if (streq(key, "failure")) {
848 if ((b = parse_boolean(value)) < 0)
849 log_debug("Failed to parse failure value %s", value);
851 m->failure = b || m->failure;
853 } else if (streq(key, "control-pid")) {
856 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
857 log_debug("Failed to parse control-pid value %s", value);
859 m->control_pid = (pid_t) pid;
860 } else if (streq(key, "control-command")) {
863 if ((id = mount_exec_command_from_string(value)) < 0)
864 log_debug("Failed to parse exec-command value %s", value);
866 m->control_command_id = id;
867 m->control_command = m->exec_command + id;
871 log_debug("Unknown serialization key '%s'", key);
876 static UnitActiveState mount_active_state(Unit *u) {
879 return state_translation_table[MOUNT(u)->state];
882 static const char *mount_sub_state_to_string(Unit *u) {
885 return mount_state_to_string(MOUNT(u)->state);
888 static bool mount_check_gc(Unit *u) {
893 return m->from_etc_fstab || m->from_proc_self_mountinfo;
896 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
903 success = code == CLD_EXITED && status == 0;
904 m->failure = m->failure || !success;
906 assert(m->control_pid == pid);
909 if (m->control_command) {
910 exec_status_fill(&m->control_command->exec_status, pid, code, status);
911 m->control_command = NULL;
912 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
915 log_debug("%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
917 /* Note that mount(8) returning and the kernel sending us a
918 * mount table change event might happen out-of-order. If an
919 * operation succeed we assume the kernel will follow soon too
920 * and already change into the resulting state. If it fails
921 * we check if the kernel still knows about the mount. and
922 * change state accordingly. */
927 case MOUNT_MOUNTING_DONE:
928 case MOUNT_MOUNTING_SIGKILL:
929 case MOUNT_MOUNTING_SIGTERM:
930 case MOUNT_REMOUNTING:
931 case MOUNT_REMOUNTING_SIGKILL:
932 case MOUNT_REMOUNTING_SIGTERM:
934 if (success && m->from_proc_self_mountinfo)
935 mount_enter_mounted(m, true);
936 else if (m->from_proc_self_mountinfo)
937 mount_enter_mounted(m, false);
939 mount_enter_dead(m, false);
942 case MOUNT_UNMOUNTING:
943 case MOUNT_UNMOUNTING_SIGKILL:
944 case MOUNT_UNMOUNTING_SIGTERM:
947 mount_enter_dead(m, true);
948 else if (m->from_proc_self_mountinfo)
949 mount_enter_mounted(m, false);
951 mount_enter_dead(m, false);
955 assert_not_reached("Uh, control process died at wrong time.");
959 static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
963 assert(elapsed == 1);
964 assert(w == &m->timer_watch);
969 case MOUNT_MOUNTING_DONE:
970 log_warning("%s mounting timed out. Stopping.", u->meta.id);
971 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, false);
974 case MOUNT_REMOUNTING:
975 log_warning("%s remounting timed out. Stopping.", u->meta.id);
976 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, false);
979 case MOUNT_UNMOUNTING:
980 log_warning("%s unmounting timed out. Stopping.", u->meta.id);
981 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, false);
984 case MOUNT_MOUNTING_SIGTERM:
985 log_warning("%s mounting timed out. Killing.", u->meta.id);
986 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, false);
989 case MOUNT_REMOUNTING_SIGTERM:
990 log_warning("%s remounting timed out. Killing.", u->meta.id);
991 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, false);
994 case MOUNT_UNMOUNTING_SIGTERM:
995 log_warning("%s unmounting timed out. Killing.", u->meta.id);
996 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, false);
999 case MOUNT_MOUNTING_SIGKILL:
1000 case MOUNT_REMOUNTING_SIGKILL:
1001 case MOUNT_UNMOUNTING_SIGKILL:
1002 log_warning("%s mount process still around after SIGKILL. Ignoring.", u->meta.id);
1004 if (m->from_proc_self_mountinfo)
1005 mount_enter_mounted(m, false);
1007 mount_enter_dead(m, false);
1011 assert_not_reached("Timeout at wrong time.");
1015 static int mount_add_one(
1019 const char *options,
1021 bool from_proc_self_mountinfo,
1026 char *e, *w = NULL, *o = NULL, *f = NULL;
1035 assert(!set_flags || from_proc_self_mountinfo);
1037 /* Ignore API mount points. They should never be referenced in
1038 * dependencies ever. */
1039 if (mount_point_is_api(where))
1042 if (streq(fstype, "autofs"))
1045 /* probably some kind of swap, ignore */
1046 if (!is_path(where))
1049 if (!(e = unit_name_from_path(where, ".mount")))
1052 if (!(u = manager_get_unit(m, e))) {
1055 if (!(u = unit_new(m))) {
1060 r = unit_add_name(u, e);
1066 if (!(MOUNT(u)->where = strdup(where))) {
1071 unit_add_to_load_queue(u);
1077 if (!(w = strdup(what)) ||
1078 !(o = strdup(options)) ||
1079 !(f = strdup(fstype))) {
1084 if (from_proc_self_mountinfo) {
1085 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1088 MOUNT(u)->is_mounted = true;
1089 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1090 MOUNT(u)->just_changed = !streq_ptr(p->options, o);
1093 MOUNT(u)->from_proc_self_mountinfo = true;
1095 p = &MOUNT(u)->parameters_etc_fstab;
1096 MOUNT(u)->from_etc_fstab = true;
1108 unit_add_to_dbus_queue(u);
1123 static char *fstab_node_to_udev_node(char *p) {
1127 /* FIXME: to follow udev's logic 100% we need to leave valid
1128 * UTF8 chars unescaped */
1130 if (startswith(p, "LABEL=")) {
1132 if (!(t = xescape(p+6, "/ ")))
1135 r = asprintf(&dn, "/dev/disk/by-label/%s", t);
1144 if (startswith(p, "UUID=")) {
1146 if (!(t = xescape(p+5, "/ ")))
1149 r = asprintf(&dn, "/dev/disk/by-uuid/%s", ascii_strlower(t));
1161 static int mount_find_pri(char *options) {
1165 if (!(pri = mount_test_option(options, "pri=")))
1171 r = strtoul(pri, &end, 10);
1176 if (end == pri || (*end != ',' && *end != 0))
1182 static int mount_load_etc_fstab(Manager *m) {
1190 if (!(f = setmntent("/etc/fstab", "r")))
1193 while ((me = getmntent(f))) {
1196 if (!(what = fstab_node_to_udev_node(me->mnt_fsname))) {
1201 if (!(where = strdup(me->mnt_dir))) {
1208 path_kill_slashes(what);
1210 if (where[0] == '/')
1211 path_kill_slashes(where);
1213 if (streq(me->mnt_type, "swap")) {
1216 if ((pri = mount_find_pri(me->mnt_opts)) < 0)
1222 !!mount_test_option(me->mnt_opts, MNTOPT_NOAUTO),
1223 !!mount_test_option(me->mnt_opts, "comment=systemd.swapon"),
1226 r = mount_add_one(m, what, where, me->mnt_opts, me->mnt_type, false, false);
1242 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1244 char *device, *path, *options, *fstype, *d, *p;
1248 rewind(m->proc_self_mountinfo);
1253 device = path = options = fstype = d = p = NULL;
1255 if ((k = fscanf(m->proc_self_mountinfo,
1256 "%*s " /* (1) mount id */
1257 "%*s " /* (2) parent id */
1258 "%*s " /* (3) major:minor */
1259 "%*s " /* (4) root */
1260 "%ms " /* (5) mount point */
1261 "%ms" /* (6) mount options */
1262 "%*[^-]" /* (7) optional fields */
1263 "- " /* (8) seperator */
1264 "%ms " /* (9) file system type */
1265 "%ms" /* (10) mount source */
1266 "%*[^\n]", /* some rubbish at the end */
1279 if (!(d = cunescape(device)) ||
1280 !(p = cunescape(path))) {
1285 if ((r = mount_add_one(m, d, p, options, fstype, true, set_flags)) < 0)
1309 static void mount_shutdown(Manager *m) {
1312 if (m->proc_self_mountinfo) {
1313 fclose(m->proc_self_mountinfo);
1314 m->proc_self_mountinfo = NULL;
1318 static int mount_enumerate(Manager *m) {
1320 struct epoll_event ev;
1323 if (!m->proc_self_mountinfo) {
1324 if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
1327 m->mount_watch.type = WATCH_MOUNT;
1328 m->mount_watch.fd = fileno(m->proc_self_mountinfo);
1331 ev.events = EPOLLERR;
1332 ev.data.ptr = &m->mount_watch;
1334 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
1338 if ((r = mount_load_etc_fstab(m)) < 0)
1341 if ((r = mount_load_proc_self_mountinfo(m, false)) < 0)
1351 void mount_fd_event(Manager *m, int events) {
1356 assert(events == EPOLLERR);
1358 /* The manager calls this for every fd event happening on the
1359 * /proc/self/mountinfo file, which informs us about mounting
1362 if ((r = mount_load_proc_self_mountinfo(m, true)) < 0) {
1363 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-errno));
1365 /* Reset flags, just in case, for later calls */
1366 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_MOUNT]) {
1367 Mount *mount = (Mount*) meta;
1369 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1375 manager_dispatch_load_queue(m);
1377 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_MOUNT]) {
1378 Mount *mount = (Mount*) meta;
1380 if (!mount->is_mounted) {
1381 /* This has just been unmounted. */
1383 mount->from_proc_self_mountinfo = false;
1385 switch (mount->state) {
1388 mount_enter_dead(mount, true);
1392 mount_set_state(mount, mount->state);
1397 } else if (mount->just_mounted || mount->just_changed) {
1399 /* New or changed entrymount */
1401 switch (mount->state) {
1404 case MOUNT_MAINTAINANCE:
1405 mount_enter_mounted(mount, true);
1408 case MOUNT_MOUNTING:
1409 mount_enter_mounting_done(mount);
1413 /* Nothing really changed, but let's
1414 * issue an notification call
1415 * nonetheless, in case somebody is
1416 * waiting for this. (e.g. file system
1417 * ro/rw remounts.) */
1418 mount_set_state(mount, mount->state);
1423 /* Reset the flags for later calls */
1424 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1428 int mount_path_is_mounted(Manager *m, const char* path) {
1438 if (!(t = strdup(path)))
1441 path_kill_slashes(t);
1447 if (!(e = unit_name_from_path(t, ".mount"))) {
1452 u = manager_get_unit(m, e);
1456 (MOUNT(u)->from_etc_fstab || MOUNT(u)->from_fragment) &&
1457 MOUNT(u)->state != MOUNT_MOUNTED) {
1462 assert_se(slash = strrchr(t, '/'));
1479 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1480 [MOUNT_DEAD] = "dead",
1481 [MOUNT_MOUNTING] = "mounting",
1482 [MOUNT_MOUNTING_DONE] = "mounting-done",
1483 [MOUNT_MOUNTED] = "mounted",
1484 [MOUNT_REMOUNTING] = "remounting",
1485 [MOUNT_UNMOUNTING] = "unmounting",
1486 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1487 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1488 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1489 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1490 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1491 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1492 [MOUNT_MAINTAINANCE] = "maintainance"
1495 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1497 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1498 [MOUNT_EXEC_MOUNT] = "ExecMount",
1499 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1500 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1503 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1505 const UnitVTable mount_vtable = {
1509 .no_instances = true,
1516 .coldplug = mount_coldplug,
1520 .start = mount_start,
1522 .reload = mount_reload,
1524 .serialize = mount_serialize,
1525 .deserialize_item = mount_deserialize_item,
1527 .active_state = mount_active_state,
1528 .sub_state_to_string = mount_sub_state_to_string,
1530 .check_gc = mount_check_gc,
1532 .sigchld_event = mount_sigchld_event,
1533 .timer_event = mount_timer_event,
1535 .bus_message_handler = bus_mount_message_handler,
1537 .enumerate = mount_enumerate,
1538 .shutdown = mount_shutdown