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_node_links(Mount
*m
) {
124 /* Adds in links to the device that this node is based on */
126 if (m
->parameters_fragment
.what
)
127 what
= m
->parameters_fragment
.what
;
128 else if (m
->parameters_etc_fstab
.what
)
129 what
= m
->parameters_etc_fstab
.what
;
131 /* We observe kernel mounts only while they are live,
132 * hence don't create any links for them */
135 if (!path_startswith(what
, "/dev/"))
138 if (!(e
= unit_name_build_escape(what
+1, NULL
, ".device")))
141 r
= manager_load_unit(UNIT(m
)->meta
.manager
, e
, NULL
, &device
);
147 if ((r
= unit_add_dependency(UNIT(m
), UNIT_AFTER
, device
)) < 0)
150 if ((r
= unit_add_dependency(UNIT(m
), UNIT_REQUIRES
, device
)) < 0)
153 if (UNIT(m
)->meta
.manager
->running_as
== MANAGER_INIT
||
154 UNIT(m
)->meta
.manager
->running_as
== MANAGER_SYSTEM
)
155 if ((r
= unit_add_dependency(device
, UNIT_WANTS
, UNIT(m
))) < 0)
161 static int mount_add_path_links(Mount
*m
) {
167 /* Adds in link to other mount points, that might lie below or
168 * above us in the hierarchy */
170 LIST_FOREACH(units_per_type
, other
, UNIT(m
)->meta
.manager
->units_per_type
[UNIT_MOUNT
]) {
178 if (m
->meta
.load_state
!= UNIT_LOADED
)
181 if (path_startswith(m
->where
, n
->where
)) {
183 if ((r
= unit_add_dependency(UNIT(m
), UNIT_AFTER
, UNIT(other
))) < 0)
186 if (n
->from_etc_fstab
|| n
->from_fragment
)
187 if ((r
= unit_add_dependency(UNIT(m
), UNIT_REQUIRES
, UNIT(other
))) < 0)
190 } else if (path_startswith(n
->where
, m
->where
)) {
192 if ((r
= unit_add_dependency(UNIT(m
), UNIT_BEFORE
, UNIT(other
))) < 0)
195 if (m
->from_etc_fstab
|| m
->from_fragment
)
196 if ((r
= unit_add_dependency(UNIT(other
), UNIT_REQUIRES
, UNIT(m
))) < 0)
204 static bool mount_test_option(const char *haystack
, const char *needle
) {
209 /* Like glibc's hasmntopt(), but works on a string, not a
216 me
.mnt_opts
= (char*) haystack
;
218 return !!hasmntopt(&me
, needle
);
221 static int mount_add_target_links(Mount
*m
) {
226 bool noauto
, handle
, automount
;
230 if (m
->from_fragment
)
231 p
= &m
->parameters_fragment
;
232 else if (m
->from_etc_fstab
)
233 p
= &m
->parameters_etc_fstab
;
237 noauto
= mount_test_option(p
->options
, MNTOPT_NOAUTO
);
238 handle
= mount_test_option(p
->options
, "comment=systemd.mount");
239 automount
= mount_test_option(p
->options
, "comment=systemd.automount");
241 if (noauto
&& !handle
&& !automount
)
244 if (mount_test_option(p
->options
, "_netdev") ||
245 fstype_is_network(p
->fstype
))
246 target
= SPECIAL_REMOTE_FS_TARGET
;
248 target
= SPECIAL_LOCAL_FS_TARGET
;
250 if ((r
= manager_load_unit(UNIT(m
)->meta
.manager
, target
, NULL
, &tu
)) < 0)
256 if ((r
= unit_load_related_unit(UNIT(m
), ".automount", &am
)) < 0)
259 if ((r
= unit_add_dependency(tu
, UNIT_WANTS
, UNIT(am
))) < 0)
262 return unit_add_dependency(UNIT(am
), UNIT_BEFORE
, tu
);
267 if ((r
= unit_add_dependency(tu
, UNIT_WANTS
, UNIT(m
))) < 0)
270 return unit_add_dependency(UNIT(m
), UNIT_BEFORE
, tu
);
274 static int mount_verify(Mount
*m
) {
279 if (UNIT(m
)->meta
.load_state
!= UNIT_LOADED
)
282 if (!(e
= unit_name_from_path(m
->where
, ".mount")))
285 b
= unit_has_name(UNIT(m
), e
);
289 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(m
)->meta
.id
);
296 static int mount_load(Unit
*u
) {
301 assert(u
->meta
.load_state
== UNIT_STUB
);
303 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
306 /* This is a new unit? Then let's add in some extras */
307 if (u
->meta
.load_state
== UNIT_LOADED
) {
310 if (!(m
->where
= unit_name_to_path(u
->meta
.id
)))
313 path_kill_slashes(m
->where
);
315 /* Minor validity checking */
316 if ((m
->parameters_fragment
.options
|| m
->parameters_fragment
.fstype
) && !m
->parameters_fragment
.what
)
319 if (m
->parameters_fragment
.what
)
320 m
->from_fragment
= true;
322 if ((r
= mount_add_node_links(MOUNT(u
))) < 0)
325 if ((r
= mount_add_path_links(MOUNT(u
))) < 0)
328 if ((r
= mount_add_target_links(MOUNT(u
))) < 0)
331 if ((r
= unit_add_default_cgroup(u
)) < 0)
335 return mount_verify(m
);
338 static int mount_notify_automount(Mount
*m
, int status
) {
344 if ((r
= unit_get_related_unit(UNIT(m
), ".automount", &p
)) < 0)
345 return r
== -ENOENT
? 0 : r
;
347 return automount_send_ready(AUTOMOUNT(p
), status
);
350 static void mount_set_state(Mount
*m
, MountState state
) {
351 MountState old_state
;
354 old_state
= m
->state
;
357 if (state
!= MOUNT_MOUNTING
&&
358 state
!= MOUNT_MOUNTING_DONE
&&
359 state
!= MOUNT_REMOUNTING
&&
360 state
!= MOUNT_UNMOUNTING
&&
361 state
!= MOUNT_MOUNTING_SIGTERM
&&
362 state
!= MOUNT_MOUNTING_SIGKILL
&&
363 state
!= MOUNT_UNMOUNTING_SIGTERM
&&
364 state
!= MOUNT_UNMOUNTING_SIGKILL
&&
365 state
!= MOUNT_REMOUNTING_SIGTERM
&&
366 state
!= MOUNT_REMOUNTING_SIGKILL
) {
367 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
368 mount_unwatch_control_pid(m
);
369 m
->control_command
= NULL
;
370 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
373 if (state
== MOUNT_MOUNTED
||
374 state
== MOUNT_REMOUNTING
)
375 mount_notify_automount(m
, 0);
376 else if (state
== MOUNT_DEAD
||
377 state
== MOUNT_UNMOUNTING
||
378 state
== MOUNT_MOUNTING_SIGTERM
||
379 state
== MOUNT_MOUNTING_SIGKILL
||
380 state
== MOUNT_REMOUNTING_SIGTERM
||
381 state
== MOUNT_REMOUNTING_SIGKILL
||
382 state
== MOUNT_UNMOUNTING_SIGTERM
||
383 state
== MOUNT_UNMOUNTING_SIGKILL
||
384 state
== MOUNT_MAINTAINANCE
)
385 mount_notify_automount(m
, -ENODEV
);
387 if (state
!= old_state
)
388 log_debug("%s changed %s → %s",
390 mount_state_to_string(old_state
),
391 mount_state_to_string(state
));
393 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
]);
396 static int mount_coldplug(Unit
*u
) {
398 MountState new_state
= MOUNT_DEAD
;
402 assert(m
->state
== MOUNT_DEAD
);
404 if (m
->deserialized_state
!= m
->state
)
405 new_state
= m
->deserialized_state
;
406 else if (m
->from_proc_self_mountinfo
)
407 new_state
= MOUNT_MOUNTED
;
409 if (new_state
!= m
->state
) {
411 if (new_state
== MOUNT_MOUNTING
||
412 new_state
== MOUNT_MOUNTING_DONE
||
413 new_state
== MOUNT_REMOUNTING
||
414 new_state
== MOUNT_UNMOUNTING
||
415 new_state
== MOUNT_MOUNTING_SIGTERM
||
416 new_state
== MOUNT_MOUNTING_SIGKILL
||
417 new_state
== MOUNT_UNMOUNTING_SIGTERM
||
418 new_state
== MOUNT_UNMOUNTING_SIGKILL
||
419 new_state
== MOUNT_REMOUNTING_SIGTERM
||
420 new_state
== MOUNT_REMOUNTING_SIGKILL
) {
422 if (m
->control_pid
<= 0)
425 if ((r
= unit_watch_pid(UNIT(m
), m
->control_pid
)) < 0)
428 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
432 mount_set_state(m
, new_state
);
438 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
445 if (m
->from_proc_self_mountinfo
)
446 p
= &m
->parameters_proc_self_mountinfo
;
447 else if (m
->from_fragment
)
448 p
= &m
->parameters_fragment
;
450 p
= &m
->parameters_etc_fstab
;
453 "%sMount State: %s\n"
456 "%sFile System Type: %s\n"
458 "%sFrom /etc/fstab: %s\n"
459 "%sFrom /proc/self/mountinfo: %s\n"
460 "%sFrom fragment: %s\n"
462 prefix
, mount_state_to_string(m
->state
),
464 prefix
, strna(p
->what
),
465 prefix
, strna(p
->fstype
),
466 prefix
, strna(p
->options
),
467 prefix
, yes_no(m
->from_etc_fstab
),
468 prefix
, yes_no(m
->from_proc_self_mountinfo
),
469 prefix
, yes_no(m
->from_fragment
),
470 prefix
, kill_mode_to_string(m
->kill_mode
));
472 if (m
->control_pid
> 0)
474 "%sControl PID: %llu\n",
475 prefix
, (unsigned long long) m
->control_pid
);
477 exec_context_dump(&m
->exec_context
, f
, prefix
);
480 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
488 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
491 if ((r
= exec_spawn(c
,
497 UNIT(m
)->meta
.manager
->confirm_spawn
,
498 UNIT(m
)->meta
.cgroup_bondings
,
502 if ((r
= unit_watch_pid(UNIT(m
), pid
)) < 0)
503 /* FIXME: we need to do something here */
511 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
516 static void mount_enter_dead(Mount
*m
, bool success
) {
522 mount_set_state(m
, m
->failure
? MOUNT_MAINTAINANCE
: MOUNT_DEAD
);
525 static void mount_enter_mounted(Mount
*m
, bool success
) {
531 mount_set_state(m
, MOUNT_MOUNTED
);
534 static void mount_enter_signal(Mount
*m
, MountState state
, bool success
) {
543 if (m
->kill_mode
!= KILL_NONE
) {
544 int sig
= (state
== MOUNT_MOUNTING_SIGTERM
||
545 state
== MOUNT_UNMOUNTING_SIGTERM
||
546 state
== MOUNT_REMOUNTING_SIGTERM
) ? SIGTERM
: SIGKILL
;
548 if (m
->kill_mode
== KILL_CONTROL_GROUP
) {
550 if ((r
= cgroup_bonding_kill_list(UNIT(m
)->meta
.cgroup_bondings
, sig
)) < 0) {
551 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
557 if (!sent
&& m
->control_pid
> 0)
558 if (kill(m
->kill_mode
== KILL_PROCESS
? m
->control_pid
: -m
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
565 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
568 mount_set_state(m
, state
);
569 } else if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
570 mount_enter_mounted(m
, true);
572 mount_enter_dead(m
, true);
577 log_warning("%s failed to kill processes: %s", UNIT(m
)->meta
.id
, strerror(-r
));
579 if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
580 mount_enter_mounted(m
, false);
582 mount_enter_dead(m
, false);
585 static void mount_enter_unmounting(Mount
*m
, bool success
) {
593 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
594 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
596 if ((r
= exec_command_set(
603 mount_unwatch_control_pid(m
);
605 if ((r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
)) < 0)
608 mount_set_state(m
, MOUNT_UNMOUNTING
);
613 log_warning("%s failed to run umount exectuable: %s", UNIT(m
)->meta
.id
, strerror(-r
));
614 mount_enter_mounted(m
, false);
617 static void mount_enter_mounting(Mount
*m
) {
622 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
623 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
625 if (m
->from_fragment
)
626 r
= exec_command_set(
629 m
->parameters_fragment
.what
,
631 "-t", m
->parameters_fragment
.fstype
,
632 m
->parameters_fragment
.options
? "-o" : NULL
, m
->parameters_fragment
.options
,
634 else if (m
->from_etc_fstab
)
635 r
= exec_command_set(
646 mount_unwatch_control_pid(m
);
648 if ((r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
)) < 0)
651 mount_set_state(m
, MOUNT_MOUNTING
);
656 log_warning("%s failed to run mount exectuable: %s", UNIT(m
)->meta
.id
, strerror(-r
));
657 mount_enter_dead(m
, false);
660 static void mount_enter_mounting_done(Mount
*m
) {
663 mount_set_state(m
, MOUNT_MOUNTING_DONE
);
666 static void mount_enter_remounting(Mount
*m
, bool success
) {
674 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
675 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
677 if (m
->from_fragment
) {
681 if (m
->parameters_fragment
.options
) {
682 if (!(buf
= strappend("remount,", m
->parameters_fragment
.options
))) {
691 r
= exec_command_set(
694 m
->parameters_fragment
.what
,
696 "-t", m
->parameters_fragment
.fstype
,
701 } else if (m
->from_etc_fstab
)
702 r
= exec_command_set(
716 mount_unwatch_control_pid(m
);
718 if ((r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
)) < 0)
721 mount_set_state(m
, MOUNT_REMOUNTING
);
726 mount_enter_mounted(m
, false);
729 static int mount_start(Unit
*u
) {
734 /* We cannot fulfill this request right now, try again later
736 if (m
->state
== MOUNT_UNMOUNTING
||
737 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
738 m
->state
== MOUNT_UNMOUNTING_SIGKILL
)
742 if (m
->state
== MOUNT_MOUNTING
||
743 m
->state
== MOUNT_MOUNTING_SIGTERM
||
744 m
->state
== MOUNT_MOUNTING_SIGKILL
)
747 assert(m
->state
== MOUNT_DEAD
|| m
->state
== MOUNT_MAINTAINANCE
);
750 mount_enter_mounting(m
);
754 static int mount_stop(Unit
*u
) {
759 /* Cann't do this right now. */
760 if (m
->state
== MOUNT_MOUNTING
||
761 m
->state
== MOUNT_MOUNTING_DONE
||
762 m
->state
== MOUNT_MOUNTING_SIGTERM
||
763 m
->state
== MOUNT_MOUNTING_SIGKILL
||
764 m
->state
== MOUNT_REMOUNTING
||
765 m
->state
== MOUNT_REMOUNTING_SIGTERM
||
766 m
->state
== MOUNT_REMOUNTING_SIGKILL
)
770 if (m
->state
== MOUNT_UNMOUNTING
||
771 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
772 m
->state
== MOUNT_UNMOUNTING_SIGTERM
)
775 assert(m
->state
== MOUNT_MOUNTED
);
777 mount_enter_unmounting(m
, true);
781 static int mount_reload(Unit
*u
) {
786 if (m
->state
== MOUNT_MOUNTING_DONE
)
789 assert(m
->state
== MOUNT_MOUNTED
);
791 mount_enter_remounting(m
, true);
795 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
802 unit_serialize_item(u
, f
, "state", mount_state_to_string(m
->state
));
803 unit_serialize_item(u
, f
, "failure", yes_no(m
->failure
));
805 if (m
->control_pid
> 0)
806 unit_serialize_item_format(u
, f
, "control-pid", "%u", (unsigned) m
->control_pid
);
808 if (m
->control_command_id
>= 0)
809 unit_serialize_item(u
, f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
814 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
823 if (streq(key
, "state")) {
826 if ((state
= mount_state_from_string(value
)) < 0)
827 log_debug("Failed to parse state value %s", value
);
829 m
->deserialized_state
= state
;
830 } else if (streq(key
, "failure")) {
833 if ((b
= parse_boolean(value
)) < 0)
834 log_debug("Failed to parse failure value %s", value
);
836 m
->failure
= b
|| m
->failure
;
838 } else if (streq(key
, "control-pid")) {
841 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
842 log_debug("Failed to parse control-pid value %s", value
);
844 m
->control_pid
= (pid_t
) pid
;
845 } else if (streq(key
, "control-command")) {
848 if ((id
= mount_exec_command_from_string(value
)) < 0)
849 log_debug("Failed to parse exec-command value %s", value
);
851 m
->control_command_id
= id
;
852 m
->control_command
= m
->exec_command
+ id
;
856 log_debug("Unknown serialization key '%s'", key
);
861 static UnitActiveState
mount_active_state(Unit
*u
) {
864 return state_translation_table
[MOUNT(u
)->state
];
867 static const char *mount_sub_state_to_string(Unit
*u
) {
870 return mount_state_to_string(MOUNT(u
)->state
);
873 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
880 success
= code
== CLD_EXITED
&& status
== 0;
881 m
->failure
= m
->failure
|| !success
;
883 assert(m
->control_pid
== pid
);
886 if (m
->control_command
) {
887 exec_status_fill(&m
->control_command
->exec_status
, pid
, code
, status
);
888 m
->control_command
= NULL
;
889 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
892 log_debug("%s control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
894 /* Note that mount(8) returning and the kernel sending us a
895 * mount table change event might happen out-of-order. If an
896 * operation succeed we assume the kernel will follow soon too
897 * and already change into the resulting state. If it fails
898 * we check if the kernel still knows about the mount. and
899 * change state accordingly. */
904 case MOUNT_MOUNTING_DONE
:
905 case MOUNT_MOUNTING_SIGKILL
:
906 case MOUNT_MOUNTING_SIGTERM
:
907 case MOUNT_REMOUNTING
:
908 case MOUNT_REMOUNTING_SIGKILL
:
909 case MOUNT_REMOUNTING_SIGTERM
:
911 if (success
&& m
->from_proc_self_mountinfo
)
912 mount_enter_mounted(m
, true);
913 else if (m
->from_proc_self_mountinfo
)
914 mount_enter_mounted(m
, false);
916 mount_enter_dead(m
, false);
919 case MOUNT_UNMOUNTING
:
920 case MOUNT_UNMOUNTING_SIGKILL
:
921 case MOUNT_UNMOUNTING_SIGTERM
:
924 mount_enter_dead(m
, true);
925 else if (m
->from_proc_self_mountinfo
)
926 mount_enter_mounted(m
, false);
928 mount_enter_dead(m
, false);
932 assert_not_reached("Uh, control process died at wrong time.");
936 static void mount_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
940 assert(elapsed
== 1);
941 assert(w
== &m
->timer_watch
);
946 case MOUNT_MOUNTING_DONE
:
947 log_warning("%s mounting timed out. Stopping.", u
->meta
.id
);
948 mount_enter_signal(m
, MOUNT_MOUNTING_SIGTERM
, false);
951 case MOUNT_REMOUNTING
:
952 log_warning("%s remounting timed out. Stopping.", u
->meta
.id
);
953 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, false);
956 case MOUNT_UNMOUNTING
:
957 log_warning("%s unmounting timed out. Stopping.", u
->meta
.id
);
958 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, false);
961 case MOUNT_MOUNTING_SIGTERM
:
962 log_warning("%s mounting timed out. Killing.", u
->meta
.id
);
963 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, false);
966 case MOUNT_REMOUNTING_SIGTERM
:
967 log_warning("%s remounting timed out. Killing.", u
->meta
.id
);
968 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, false);
971 case MOUNT_UNMOUNTING_SIGTERM
:
972 log_warning("%s unmounting timed out. Killing.", u
->meta
.id
);
973 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, false);
976 case MOUNT_MOUNTING_SIGKILL
:
977 case MOUNT_REMOUNTING_SIGKILL
:
978 case MOUNT_UNMOUNTING_SIGKILL
:
979 log_warning("%s mount process still around after SIGKILL. Ignoring.", u
->meta
.id
);
981 if (m
->from_proc_self_mountinfo
)
982 mount_enter_mounted(m
, false);
984 mount_enter_dead(m
, false);
988 assert_not_reached("Timeout at wrong time.");
992 static int mount_add_one(
998 bool from_proc_self_mountinfo
,
1003 char *e
, *w
= NULL
, *o
= NULL
, *f
= NULL
;
1004 MountParameters
*mp
;
1012 assert(!set_flags
|| from_proc_self_mountinfo
);
1014 /* Ignore API mount points. They should never be referenced in
1015 * dependencies ever. */
1016 if (mount_point_is_api(where
))
1019 if (streq(fstype
, "autofs"))
1022 /* probably some kind of swap, which we don't cover for now */
1023 if (where
[0] != '/')
1026 if (!(e
= unit_name_from_path(where
, ".mount")))
1029 if (!(u
= manager_get_unit(m
, e
))) {
1032 if (!(u
= unit_new(m
))) {
1037 r
= unit_add_name(u
, e
);
1043 if (!(MOUNT(u
)->where
= strdup(where
))) {
1048 if ((r
= unit_set_description(u
, where
)) < 0)
1051 unit_add_to_load_queue(u
);
1057 if (!(w
= strdup(what
)) ||
1058 !(o
= strdup(options
)) ||
1059 !(f
= strdup(fstype
))) {
1064 if (from_proc_self_mountinfo
) {
1065 mp
= &MOUNT(u
)->parameters_proc_self_mountinfo
;
1068 MOUNT(u
)->is_mounted
= true;
1069 MOUNT(u
)->just_mounted
= !MOUNT(u
)->from_proc_self_mountinfo
;
1070 MOUNT(u
)->just_changed
= !streq_ptr(MOUNT(u
)->parameters_proc_self_mountinfo
.options
, o
);
1073 MOUNT(u
)->from_proc_self_mountinfo
= true;
1076 mp
= &MOUNT(u
)->parameters_etc_fstab
;
1078 MOUNT(u
)->from_etc_fstab
= true;
1090 unit_add_to_dbus_queue(u
);
1105 static char *fstab_node_to_udev_node(char *p
) {
1109 /* FIXME: to follow udev's logic 100% we need to leave valid
1110 * UTF8 chars unescaped */
1112 if (startswith(p
, "LABEL=")) {
1114 if (!(t
= xescape(p
+6, "/ ")))
1117 r
= asprintf(&dn
, "/dev/disk/by-label/%s", t
);
1126 if (startswith(p
, "UUID=")) {
1128 if (!(t
= xescape(p
+5, "/ ")))
1131 r
= asprintf(&dn
, "/dev/disk/by-uuid/%s", ascii_strlower(t
));
1143 static int mount_load_etc_fstab(Manager
*m
) {
1151 if (!(f
= setmntent("/etc/fstab", "r")))
1154 while ((me
= getmntent(f
))) {
1157 if (!(what
= fstab_node_to_udev_node(me
->mnt_fsname
))) {
1162 if (!(where
= strdup(me
->mnt_dir
))) {
1169 path_kill_slashes(what
);
1171 if (where
[0] == '/')
1172 path_kill_slashes(where
);
1174 r
= mount_add_one(m
, what
, where
, me
->mnt_opts
, me
->mnt_type
, false, false);
1189 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1191 char *device
, *path
, *options
, *fstype
, *d
, *p
;
1195 rewind(m
->proc_self_mountinfo
);
1200 device
= path
= options
= fstype
= d
= p
= NULL
;
1202 if ((k
= fscanf(m
->proc_self_mountinfo
,
1203 "%*s " /* (1) mount id */
1204 "%*s " /* (2) parent id */
1205 "%*s " /* (3) major:minor */
1206 "%*s " /* (4) root */
1207 "%ms " /* (5) mount point */
1208 "%ms" /* (6) mount options */
1209 "%*[^-]" /* (7) optional fields */
1210 "- " /* (8) seperator */
1211 "%ms " /* (9) file system type */
1212 "%ms" /* (10) mount source */
1213 "%*[^\n]", /* some rubbish at the end */
1226 if (!(d
= cunescape(device
)) ||
1227 !(p
= cunescape(path
))) {
1232 if ((r
= mount_add_one(m
, d
, p
, options
, fstype
, true, set_flags
)) < 0)
1256 static void mount_shutdown(Manager
*m
) {
1259 if (m
->proc_self_mountinfo
) {
1260 fclose(m
->proc_self_mountinfo
);
1261 m
->proc_self_mountinfo
= NULL
;
1265 static int mount_enumerate(Manager
*m
) {
1267 struct epoll_event ev
;
1270 if (!m
->proc_self_mountinfo
) {
1271 if (!(m
->proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re")))
1274 m
->mount_watch
.type
= WATCH_MOUNT
;
1275 m
->mount_watch
.fd
= fileno(m
->proc_self_mountinfo
);
1278 ev
.events
= EPOLLERR
;
1279 ev
.data
.ptr
= &m
->mount_watch
;
1281 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->mount_watch
.fd
, &ev
) < 0)
1285 if ((r
= mount_load_etc_fstab(m
)) < 0)
1288 if ((r
= mount_load_proc_self_mountinfo(m
, false)) < 0)
1298 void mount_fd_event(Manager
*m
, int events
) {
1303 assert(events
== EPOLLERR
);
1305 /* The manager calls this for every fd event happening on the
1306 * /proc/self/mountinfo file, which informs us about mounting
1309 if ((r
= mount_load_proc_self_mountinfo(m
, true)) < 0) {
1310 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-errno
));
1312 /* Reset flags, just in case, for later calls */
1313 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1314 Mount
*mount
= (Mount
*) meta
;
1316 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1322 manager_dispatch_load_queue(m
);
1324 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1325 Mount
*mount
= (Mount
*) meta
;
1327 if (!mount
->is_mounted
) {
1328 /* This has just been unmounted. */
1330 mount
->from_proc_self_mountinfo
= false;
1332 switch (mount
->state
) {
1335 mount_enter_dead(mount
, true);
1339 mount_set_state(mount
, mount
->state
);
1344 } else if (mount
->just_mounted
|| mount
->just_changed
) {
1346 /* New or changed entrymount */
1348 switch (mount
->state
) {
1351 case MOUNT_MAINTAINANCE
:
1352 mount_enter_mounted(mount
, true);
1355 case MOUNT_MOUNTING
:
1356 mount_enter_mounting_done(mount
);
1360 /* Nothing really changed, but let's
1361 * issue an notification call
1362 * nonetheless, in case somebody is
1363 * waiting for this. (e.g. file system
1364 * ro/rw remounts.) */
1365 mount_set_state(mount
, mount
->state
);
1370 /* Reset the flags for later calls */
1371 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1375 int mount_path_is_mounted(Manager
*m
, const char* path
) {
1385 if (!(t
= strdup(path
)))
1388 path_kill_slashes(t
);
1394 if (!(e
= unit_name_from_path(t
, ".mount"))) {
1399 u
= manager_get_unit(m
, e
);
1403 (MOUNT(u
)->from_etc_fstab
|| MOUNT(u
)->from_fragment
) &&
1404 MOUNT(u
)->state
!= MOUNT_MOUNTED
) {
1409 assert_se(slash
= strrchr(t
, '/'));
1426 static const char* const mount_state_table
[_MOUNT_STATE_MAX
] = {
1427 [MOUNT_DEAD
] = "dead",
1428 [MOUNT_MOUNTING
] = "mounting",
1429 [MOUNT_MOUNTING_DONE
] = "mounting-done",
1430 [MOUNT_MOUNTED
] = "mounted",
1431 [MOUNT_REMOUNTING
] = "remounting",
1432 [MOUNT_UNMOUNTING
] = "unmounting",
1433 [MOUNT_MOUNTING_SIGTERM
] = "mounting-sigterm",
1434 [MOUNT_MOUNTING_SIGKILL
] = "mounting-sigkill",
1435 [MOUNT_REMOUNTING_SIGTERM
] = "remounting-sigterm",
1436 [MOUNT_REMOUNTING_SIGKILL
] = "remounting-sigkill",
1437 [MOUNT_UNMOUNTING_SIGTERM
] = "unmounting-sigterm",
1438 [MOUNT_UNMOUNTING_SIGKILL
] = "unmounting-sigkill",
1439 [MOUNT_MAINTAINANCE
] = "maintainance"
1442 DEFINE_STRING_TABLE_LOOKUP(mount_state
, MountState
);
1444 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
1445 [MOUNT_EXEC_MOUNT
] = "ExecMount",
1446 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
1447 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
1450 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
1452 const UnitVTable mount_vtable
= {
1456 .no_instances
= true,
1462 .coldplug
= mount_coldplug
,
1466 .start
= mount_start
,
1468 .reload
= mount_reload
,
1470 .serialize
= mount_serialize
,
1471 .deserialize_item
= mount_deserialize_item
,
1473 .active_state
= mount_active_state
,
1474 .sub_state_to_string
= mount_sub_state_to_string
,
1476 .sigchld_event
= mount_sigchld_event
,
1477 .timer_event
= mount_timer_event
,
1479 .bus_message_handler
= bus_mount_message_handler
,
1481 .enumerate
= mount_enumerate
,
1482 .shutdown
= mount_shutdown