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"
40 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
41 [MOUNT_DEAD
] = UNIT_INACTIVE
,
42 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
43 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVE
,
44 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
45 [MOUNT_REMOUNTING
] = UNIT_ACTIVE_RELOADING
,
46 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
47 [MOUNT_MOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
48 [MOUNT_MOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
49 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_ACTIVE_RELOADING
,
50 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_ACTIVE_RELOADING
,
51 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
52 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
53 [MOUNT_MAINTENANCE
] = UNIT_INACTIVE
,
56 static void mount_init(Unit
*u
) {
60 assert(u
->meta
.load_state
== UNIT_STUB
);
62 m
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
63 exec_context_init(&m
->exec_context
);
65 /* We need to make sure that /bin/mount is always called in
66 * the same process group as us, so that the autofs kernel
67 * side doesn't send us another mount request while we are
68 * already trying to comply its last one. */
69 m
->exec_context
.no_setsid
= true;
71 m
->timer_watch
.type
= WATCH_INVALID
;
73 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
76 static void mount_unwatch_control_pid(Mount
*m
) {
79 if (m
->control_pid
<= 0)
82 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
86 static void mount_parameters_done(MountParameters
*p
) {
93 p
->what
= p
->options
= p
->fstype
= NULL
;
96 static void mount_done(Unit
*u
) {
104 mount_parameters_done(&m
->parameters_etc_fstab
);
105 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
106 mount_parameters_done(&m
->parameters_fragment
);
108 exec_context_done(&m
->exec_context
);
109 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
110 m
->control_command
= NULL
;
112 mount_unwatch_control_pid(m
);
114 unit_unwatch_timer(u
, &m
->timer_watch
);
117 static int mount_add_mount_links(Mount
*m
) {
123 /* Adds in links to other mount points that might lie below or
124 * above us in the hierarchy */
126 LIST_FOREACH(units_per_type
, other
, m
->meta
.manager
->units_per_type
[UNIT_MOUNT
]) {
127 Mount
*n
= (Mount
*) other
;
132 if (n
->meta
.load_state
!= UNIT_LOADED
)
135 if (path_startswith(m
->where
, n
->where
)) {
137 if ((r
= unit_add_dependency(UNIT(m
), UNIT_AFTER
, UNIT(n
), true)) < 0)
140 if (n
->from_etc_fstab
|| n
->from_fragment
)
141 if ((r
= unit_add_dependency(UNIT(m
), UNIT_REQUIRES
, UNIT(n
), true)) < 0)
144 } else if (path_startswith(n
->where
, m
->where
)) {
146 if ((r
= unit_add_dependency(UNIT(m
), UNIT_BEFORE
, UNIT(n
), true)) < 0)
149 if (m
->from_etc_fstab
|| m
->from_fragment
)
150 if ((r
= unit_add_dependency(UNIT(n
), UNIT_REQUIRES
, UNIT(m
), true)) < 0)
158 static int mount_add_swap_links(Mount
*m
) {
164 LIST_FOREACH(units_per_type
, other
, m
->meta
.manager
->units_per_type
[UNIT_SWAP
])
165 if ((r
= swap_add_one_mount_link((Swap
*) other
, m
)) < 0)
171 static int mount_add_path_links(Mount
*m
) {
177 LIST_FOREACH(units_per_type
, other
, m
->meta
.manager
->units_per_type
[UNIT_PATH
])
178 if ((r
= path_add_one_mount_link((Path
*) other
, m
)) < 0)
184 static int mount_add_automount_links(Mount
*m
) {
190 LIST_FOREACH(units_per_type
, other
, m
->meta
.manager
->units_per_type
[UNIT_AUTOMOUNT
])
191 if ((r
= automount_add_one_mount_link((Automount
*) other
, m
)) < 0)
197 static int mount_add_socket_links(Mount
*m
) {
203 LIST_FOREACH(units_per_type
, other
, m
->meta
.manager
->units_per_type
[UNIT_SOCKET
])
204 if ((r
= socket_add_one_mount_link((Socket
*) other
, m
)) < 0)
210 static char* mount_test_option(const char *haystack
, const char *needle
) {
215 /* Like glibc's hasmntopt(), but works on a string, not a
222 me
.mnt_opts
= (char*) haystack
;
224 return hasmntopt(&me
, needle
);
227 static int mount_add_target_links(Mount
*m
) {
232 bool noauto
, handle
, automount
, user
;
236 if (m
->from_fragment
)
237 p
= &m
->parameters_fragment
;
238 else if (m
->from_etc_fstab
)
239 p
= &m
->parameters_etc_fstab
;
243 noauto
= !!mount_test_option(p
->options
, MNTOPT_NOAUTO
);
244 user
= mount_test_option(p
->options
, "user") || mount_test_option(p
->options
, "users");
245 handle
= !!mount_test_option(p
->options
, "comment=systemd.mount");
246 automount
= !!mount_test_option(p
->options
, "comment=systemd.automount");
248 if (mount_test_option(p
->options
, "_netdev") ||
249 fstype_is_network(p
->fstype
))
250 target
= SPECIAL_REMOTE_FS_TARGET
;
252 target
= SPECIAL_LOCAL_FS_TARGET
;
254 if ((r
= manager_load_unit(UNIT(m
)->meta
.manager
, target
, NULL
, &tu
)) < 0)
257 if (automount
&& m
->meta
.manager
->running_as
== MANAGER_SYSTEM
) {
260 if ((r
= unit_load_related_unit(UNIT(m
), ".automount", &am
)) < 0)
263 if ((r
= unit_add_dependency(tu
, UNIT_WANTS
, UNIT(am
), true)) < 0)
266 return unit_add_dependency(UNIT(am
), UNIT_BEFORE
, tu
, true);
270 if (!noauto
&& handle
)
271 if (user
|| m
->meta
.manager
->running_as
== MANAGER_SYSTEM
)
272 if ((r
= unit_add_dependency(tu
, UNIT_WANTS
, UNIT(m
), true)) < 0)
275 return unit_add_dependency(UNIT(m
), UNIT_BEFORE
, tu
, true);
279 static int mount_verify(Mount
*m
) {
284 if (m
->meta
.load_state
!= UNIT_LOADED
)
287 if (!m
->from_etc_fstab
&& !m
->from_fragment
&& !m
->from_proc_self_mountinfo
)
290 if (!(e
= unit_name_from_path(m
->where
, ".mount")))
293 b
= unit_has_name(UNIT(m
), e
);
297 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(m
)->meta
.id
);
301 if (m
->meta
.fragment_path
&& !m
->parameters_fragment
.what
) {
302 log_error("%s's What setting is missing. Refusing.", UNIT(m
)->meta
.id
);
309 static int mount_load(Unit
*u
) {
314 assert(u
->meta
.load_state
== UNIT_STUB
);
316 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
319 /* This is a new unit? Then let's add in some extras */
320 if (u
->meta
.load_state
== UNIT_LOADED
) {
321 const char *what
= NULL
;
323 if (m
->meta
.fragment_path
)
324 m
->from_fragment
= true;
327 if (!(m
->where
= unit_name_to_path(u
->meta
.id
)))
330 path_kill_slashes(m
->where
);
332 if (!m
->meta
.description
)
333 if ((r
= unit_set_description(u
, m
->where
)) < 0)
336 if (m
->from_fragment
&& m
->parameters_fragment
.what
)
337 what
= m
->parameters_fragment
.what
;
338 else if (m
->from_etc_fstab
&& m
->parameters_etc_fstab
.what
)
339 what
= m
->parameters_etc_fstab
.what
;
340 else if (m
->from_proc_self_mountinfo
&& m
->parameters_proc_self_mountinfo
.what
)
341 what
= m
->parameters_proc_self_mountinfo
.what
;
344 if ((r
= unit_add_node_link(u
, what
, u
->meta
.manager
->running_as
== MANAGER_SYSTEM
)) < 0)
347 if ((r
= mount_add_mount_links(m
)) < 0)
350 if ((r
= mount_add_socket_links(m
)) < 0)
353 if ((r
= mount_add_swap_links(m
)) < 0)
356 if ((r
= mount_add_path_links(m
)) < 0)
359 if ((r
= mount_add_automount_links(m
)) < 0)
362 if ((r
= mount_add_target_links(m
)) < 0)
365 if ((r
= unit_add_default_cgroup(u
)) < 0)
369 return mount_verify(m
);
372 static int mount_notify_automount(Mount
*m
, int status
) {
378 if ((r
= unit_get_related_unit(UNIT(m
), ".automount", &p
)) < 0)
379 return r
== -ENOENT
? 0 : r
;
381 return automount_send_ready(AUTOMOUNT(p
), status
);
384 static void mount_set_state(Mount
*m
, MountState state
) {
385 MountState old_state
;
388 old_state
= m
->state
;
391 if (state
!= MOUNT_MOUNTING
&&
392 state
!= MOUNT_MOUNTING_DONE
&&
393 state
!= MOUNT_REMOUNTING
&&
394 state
!= MOUNT_UNMOUNTING
&&
395 state
!= MOUNT_MOUNTING_SIGTERM
&&
396 state
!= MOUNT_MOUNTING_SIGKILL
&&
397 state
!= MOUNT_UNMOUNTING_SIGTERM
&&
398 state
!= MOUNT_UNMOUNTING_SIGKILL
&&
399 state
!= MOUNT_REMOUNTING_SIGTERM
&&
400 state
!= MOUNT_REMOUNTING_SIGKILL
) {
401 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
402 mount_unwatch_control_pid(m
);
403 m
->control_command
= NULL
;
404 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
407 if (state
== MOUNT_MOUNTED
||
408 state
== MOUNT_REMOUNTING
)
409 mount_notify_automount(m
, 0);
410 else if (state
== MOUNT_DEAD
||
411 state
== MOUNT_UNMOUNTING
||
412 state
== MOUNT_MOUNTING_SIGTERM
||
413 state
== MOUNT_MOUNTING_SIGKILL
||
414 state
== MOUNT_REMOUNTING_SIGTERM
||
415 state
== MOUNT_REMOUNTING_SIGKILL
||
416 state
== MOUNT_UNMOUNTING_SIGTERM
||
417 state
== MOUNT_UNMOUNTING_SIGKILL
||
418 state
== MOUNT_MAINTENANCE
)
419 mount_notify_automount(m
, -ENODEV
);
421 if (state
!= old_state
)
422 log_debug("%s changed %s -> %s",
424 mount_state_to_string(old_state
),
425 mount_state_to_string(state
));
427 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
]);
430 static int mount_coldplug(Unit
*u
) {
432 MountState new_state
= MOUNT_DEAD
;
436 assert(m
->state
== MOUNT_DEAD
);
438 if (m
->deserialized_state
!= m
->state
)
439 new_state
= m
->deserialized_state
;
440 else if (m
->from_proc_self_mountinfo
)
441 new_state
= MOUNT_MOUNTED
;
443 if (new_state
!= m
->state
) {
445 if (new_state
== MOUNT_MOUNTING
||
446 new_state
== MOUNT_MOUNTING_DONE
||
447 new_state
== MOUNT_REMOUNTING
||
448 new_state
== MOUNT_UNMOUNTING
||
449 new_state
== MOUNT_MOUNTING_SIGTERM
||
450 new_state
== MOUNT_MOUNTING_SIGKILL
||
451 new_state
== MOUNT_UNMOUNTING_SIGTERM
||
452 new_state
== MOUNT_UNMOUNTING_SIGKILL
||
453 new_state
== MOUNT_REMOUNTING_SIGTERM
||
454 new_state
== MOUNT_REMOUNTING_SIGKILL
) {
456 if (m
->control_pid
<= 0)
459 if ((r
= unit_watch_pid(UNIT(m
), m
->control_pid
)) < 0)
462 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
466 mount_set_state(m
, new_state
);
472 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
479 if (m
->from_proc_self_mountinfo
)
480 p
= &m
->parameters_proc_self_mountinfo
;
481 else if (m
->from_fragment
)
482 p
= &m
->parameters_fragment
;
484 p
= &m
->parameters_etc_fstab
;
487 "%sMount State: %s\n"
490 "%sFile System Type: %s\n"
492 "%sFrom /etc/fstab: %s\n"
493 "%sFrom /proc/self/mountinfo: %s\n"
494 "%sFrom fragment: %s\n"
496 prefix
, mount_state_to_string(m
->state
),
498 prefix
, strna(p
->what
),
499 prefix
, strna(p
->fstype
),
500 prefix
, strna(p
->options
),
501 prefix
, yes_no(m
->from_etc_fstab
),
502 prefix
, yes_no(m
->from_proc_self_mountinfo
),
503 prefix
, yes_no(m
->from_fragment
),
504 prefix
, kill_mode_to_string(m
->kill_mode
));
506 if (m
->control_pid
> 0)
508 "%sControl PID: %lu\n",
509 prefix
, (unsigned long) m
->control_pid
);
511 exec_context_dump(&m
->exec_context
, f
, prefix
);
514 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
522 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
525 if ((r
= exec_spawn(c
,
529 m
->meta
.manager
->environment
,
532 UNIT(m
)->meta
.manager
->confirm_spawn
,
533 UNIT(m
)->meta
.cgroup_bondings
,
537 if ((r
= unit_watch_pid(UNIT(m
), pid
)) < 0)
538 /* FIXME: we need to do something here */
546 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
551 static void mount_enter_dead(Mount
*m
, bool success
) {
557 mount_set_state(m
, m
->failure
? MOUNT_MAINTENANCE
: MOUNT_DEAD
);
560 static void mount_enter_mounted(Mount
*m
, bool success
) {
566 mount_set_state(m
, MOUNT_MOUNTED
);
569 static void mount_enter_signal(Mount
*m
, MountState state
, bool success
) {
578 if (m
->kill_mode
!= KILL_NONE
) {
579 int sig
= (state
== MOUNT_MOUNTING_SIGTERM
||
580 state
== MOUNT_UNMOUNTING_SIGTERM
||
581 state
== MOUNT_REMOUNTING_SIGTERM
) ? SIGTERM
: SIGKILL
;
583 if (m
->kill_mode
== KILL_CONTROL_GROUP
) {
585 if ((r
= cgroup_bonding_kill_list(UNIT(m
)->meta
.cgroup_bondings
, sig
)) < 0) {
586 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
592 if (!sent
&& m
->control_pid
> 0)
593 if (kill(m
->kill_mode
== KILL_PROCESS
? m
->control_pid
: -m
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
600 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
603 mount_set_state(m
, state
);
604 } else if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
605 mount_enter_mounted(m
, true);
607 mount_enter_dead(m
, true);
612 log_warning("%s failed to kill processes: %s", UNIT(m
)->meta
.id
, strerror(-r
));
614 if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
615 mount_enter_mounted(m
, false);
617 mount_enter_dead(m
, false);
620 static void mount_enter_unmounting(Mount
*m
, bool success
) {
628 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
629 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
631 if ((r
= exec_command_set(
638 mount_unwatch_control_pid(m
);
640 if ((r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
)) < 0)
643 mount_set_state(m
, MOUNT_UNMOUNTING
);
648 log_warning("%s failed to run 'umount' task: %s", UNIT(m
)->meta
.id
, strerror(-r
));
649 mount_enter_mounted(m
, false);
652 static void mount_enter_mounting(Mount
*m
) {
657 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
658 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
660 if (m
->from_fragment
)
661 r
= exec_command_set(
664 m
->parameters_fragment
.what
,
666 "-t", m
->parameters_fragment
.fstype
,
667 m
->parameters_fragment
.options
? "-o" : NULL
, m
->parameters_fragment
.options
,
669 else if (m
->from_etc_fstab
)
670 r
= exec_command_set(
681 mount_unwatch_control_pid(m
);
683 if ((r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
)) < 0)
686 mount_set_state(m
, MOUNT_MOUNTING
);
691 log_warning("%s failed to run 'mount' task: %s", UNIT(m
)->meta
.id
, strerror(-r
));
692 mount_enter_dead(m
, false);
695 static void mount_enter_mounting_done(Mount
*m
) {
698 mount_set_state(m
, MOUNT_MOUNTING_DONE
);
701 static void mount_enter_remounting(Mount
*m
, bool success
) {
709 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
710 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
712 if (m
->from_fragment
) {
716 if (m
->parameters_fragment
.options
) {
717 if (!(buf
= strappend("remount,", m
->parameters_fragment
.options
))) {
726 r
= exec_command_set(
729 m
->parameters_fragment
.what
,
731 "-t", m
->parameters_fragment
.fstype
,
736 } else if (m
->from_etc_fstab
)
737 r
= exec_command_set(
751 mount_unwatch_control_pid(m
);
753 if ((r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
)) < 0)
756 mount_set_state(m
, MOUNT_REMOUNTING
);
761 mount_enter_mounted(m
, false);
764 static int mount_start(Unit
*u
) {
769 /* We cannot fulfill this request right now, try again later
771 if (m
->state
== MOUNT_UNMOUNTING
||
772 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
773 m
->state
== MOUNT_UNMOUNTING_SIGKILL
)
777 if (m
->state
== MOUNT_MOUNTING
||
778 m
->state
== MOUNT_MOUNTING_SIGTERM
||
779 m
->state
== MOUNT_MOUNTING_SIGKILL
)
782 assert(m
->state
== MOUNT_DEAD
|| m
->state
== MOUNT_MAINTENANCE
);
785 mount_enter_mounting(m
);
789 static int mount_stop(Unit
*u
) {
794 /* Cann't do this right now. */
795 if (m
->state
== MOUNT_MOUNTING
||
796 m
->state
== MOUNT_MOUNTING_DONE
||
797 m
->state
== MOUNT_MOUNTING_SIGTERM
||
798 m
->state
== MOUNT_MOUNTING_SIGKILL
||
799 m
->state
== MOUNT_REMOUNTING
||
800 m
->state
== MOUNT_REMOUNTING_SIGTERM
||
801 m
->state
== MOUNT_REMOUNTING_SIGKILL
)
805 if (m
->state
== MOUNT_UNMOUNTING
||
806 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
807 m
->state
== MOUNT_UNMOUNTING_SIGTERM
)
810 assert(m
->state
== MOUNT_MOUNTED
);
812 mount_enter_unmounting(m
, true);
816 static int mount_reload(Unit
*u
) {
821 if (m
->state
== MOUNT_MOUNTING_DONE
)
824 assert(m
->state
== MOUNT_MOUNTED
);
826 mount_enter_remounting(m
, true);
830 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
837 unit_serialize_item(u
, f
, "state", mount_state_to_string(m
->state
));
838 unit_serialize_item(u
, f
, "failure", yes_no(m
->failure
));
840 if (m
->control_pid
> 0)
841 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) m
->control_pid
);
843 if (m
->control_command_id
>= 0)
844 unit_serialize_item(u
, f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
849 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
858 if (streq(key
, "state")) {
861 if ((state
= mount_state_from_string(value
)) < 0)
862 log_debug("Failed to parse state value %s", value
);
864 m
->deserialized_state
= state
;
865 } else if (streq(key
, "failure")) {
868 if ((b
= parse_boolean(value
)) < 0)
869 log_debug("Failed to parse failure value %s", value
);
871 m
->failure
= b
|| m
->failure
;
873 } else if (streq(key
, "control-pid")) {
876 if ((r
= parse_pid(value
, &pid
)) < 0)
877 log_debug("Failed to parse control-pid value %s", value
);
879 m
->control_pid
= pid
;
880 } else if (streq(key
, "control-command")) {
883 if ((id
= mount_exec_command_from_string(value
)) < 0)
884 log_debug("Failed to parse exec-command value %s", value
);
886 m
->control_command_id
= id
;
887 m
->control_command
= m
->exec_command
+ id
;
891 log_debug("Unknown serialization key '%s'", key
);
896 static UnitActiveState
mount_active_state(Unit
*u
) {
899 return state_translation_table
[MOUNT(u
)->state
];
902 static const char *mount_sub_state_to_string(Unit
*u
) {
905 return mount_state_to_string(MOUNT(u
)->state
);
908 static bool mount_check_gc(Unit
*u
) {
913 return m
->from_etc_fstab
|| m
->from_proc_self_mountinfo
;
916 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
923 if (pid
!= m
->control_pid
)
928 success
= is_clean_exit(code
, status
);
929 m
->failure
= m
->failure
|| !success
;
931 if (m
->control_command
) {
932 exec_status_fill(&m
->control_command
->exec_status
, pid
, code
, status
);
933 m
->control_command
= NULL
;
934 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
937 log_debug("%s control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
939 /* Note that mount(8) returning and the kernel sending us a
940 * mount table change event might happen out-of-order. If an
941 * operation succeed we assume the kernel will follow soon too
942 * and already change into the resulting state. If it fails
943 * we check if the kernel still knows about the mount. and
944 * change state accordingly. */
949 case MOUNT_MOUNTING_DONE
:
950 case MOUNT_MOUNTING_SIGKILL
:
951 case MOUNT_MOUNTING_SIGTERM
:
952 case MOUNT_REMOUNTING
:
953 case MOUNT_REMOUNTING_SIGKILL
:
954 case MOUNT_REMOUNTING_SIGTERM
:
957 mount_enter_mounted(m
, true);
958 else if (m
->from_proc_self_mountinfo
)
959 mount_enter_mounted(m
, false);
961 mount_enter_dead(m
, false);
964 case MOUNT_UNMOUNTING
:
965 case MOUNT_UNMOUNTING_SIGKILL
:
966 case MOUNT_UNMOUNTING_SIGTERM
:
969 mount_enter_dead(m
, true);
970 else if (m
->from_proc_self_mountinfo
)
971 mount_enter_mounted(m
, false);
973 mount_enter_dead(m
, false);
977 assert_not_reached("Uh, control process died at wrong time.");
981 static void mount_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
985 assert(elapsed
== 1);
986 assert(w
== &m
->timer_watch
);
991 case MOUNT_MOUNTING_DONE
:
992 log_warning("%s mounting timed out. Stopping.", u
->meta
.id
);
993 mount_enter_signal(m
, MOUNT_MOUNTING_SIGTERM
, false);
996 case MOUNT_REMOUNTING
:
997 log_warning("%s remounting timed out. Stopping.", u
->meta
.id
);
998 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, false);
1001 case MOUNT_UNMOUNTING
:
1002 log_warning("%s unmounting timed out. Stopping.", u
->meta
.id
);
1003 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, false);
1006 case MOUNT_MOUNTING_SIGTERM
:
1007 log_warning("%s mounting timed out. Killing.", u
->meta
.id
);
1008 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, false);
1011 case MOUNT_REMOUNTING_SIGTERM
:
1012 log_warning("%s remounting timed out. Killing.", u
->meta
.id
);
1013 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, false);
1016 case MOUNT_UNMOUNTING_SIGTERM
:
1017 log_warning("%s unmounting timed out. Killing.", u
->meta
.id
);
1018 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, false);
1021 case MOUNT_MOUNTING_SIGKILL
:
1022 case MOUNT_REMOUNTING_SIGKILL
:
1023 case MOUNT_UNMOUNTING_SIGKILL
:
1024 log_warning("%s mount process still around after SIGKILL. Ignoring.", u
->meta
.id
);
1026 if (m
->from_proc_self_mountinfo
)
1027 mount_enter_mounted(m
, false);
1029 mount_enter_dead(m
, false);
1033 assert_not_reached("Timeout at wrong time.");
1037 static int mount_add_one(
1041 const char *options
,
1043 bool from_proc_self_mountinfo
,
1048 char *e
, *w
= NULL
, *o
= NULL
, *f
= NULL
;
1057 assert(!set_flags
|| from_proc_self_mountinfo
);
1059 /* Ignore API mount points. They should never be referenced in
1060 * dependencies ever. */
1061 if (mount_point_is_api(where
))
1064 if (streq(fstype
, "autofs"))
1067 /* probably some kind of swap, ignore */
1068 if (!is_path(where
))
1071 if (!(e
= unit_name_from_path(where
, ".mount")))
1074 if (!(u
= manager_get_unit(m
, e
))) {
1077 if (!(u
= unit_new(m
))) {
1082 r
= unit_add_name(u
, e
);
1088 if (!(MOUNT(u
)->where
= strdup(where
))) {
1093 unit_add_to_load_queue(u
);
1099 if (!(w
= strdup(what
)) ||
1100 !(o
= strdup(options
)) ||
1101 !(f
= strdup(fstype
))) {
1106 if (from_proc_self_mountinfo
) {
1107 p
= &MOUNT(u
)->parameters_proc_self_mountinfo
;
1110 MOUNT(u
)->is_mounted
= true;
1111 MOUNT(u
)->just_mounted
= !MOUNT(u
)->from_proc_self_mountinfo
;
1112 MOUNT(u
)->just_changed
= !streq_ptr(p
->options
, o
);
1115 MOUNT(u
)->from_proc_self_mountinfo
= true;
1117 p
= &MOUNT(u
)->parameters_etc_fstab
;
1118 MOUNT(u
)->from_etc_fstab
= true;
1130 unit_add_to_dbus_queue(u
);
1145 static char *fstab_node_to_udev_node(char *p
) {
1149 /* FIXME: to follow udev's logic 100% we need to leave valid
1150 * UTF8 chars unescaped */
1152 if (startswith(p
, "LABEL=")) {
1154 if (!(t
= xescape(p
+6, "/ ")))
1157 r
= asprintf(&dn
, "/dev/disk/by-label/%s", t
);
1166 if (startswith(p
, "UUID=")) {
1168 if (!(t
= xescape(p
+5, "/ ")))
1171 r
= asprintf(&dn
, "/dev/disk/by-uuid/%s", ascii_strlower(t
));
1183 static int mount_find_pri(char *options
) {
1187 if (!(pri
= mount_test_option(options
, "pri=")))
1193 r
= strtoul(pri
, &end
, 10);
1198 if (end
== pri
|| (*end
!= ',' && *end
!= 0))
1204 static int mount_load_etc_fstab(Manager
*m
) {
1212 if (!(f
= setmntent("/etc/fstab", "r")))
1215 while ((me
= getmntent(f
))) {
1218 if (!(what
= fstab_node_to_udev_node(me
->mnt_fsname
))) {
1223 if (!(where
= strdup(me
->mnt_dir
))) {
1230 path_kill_slashes(what
);
1232 if (where
[0] == '/')
1233 path_kill_slashes(where
);
1235 if (streq(me
->mnt_type
, "swap")) {
1238 if ((pri
= mount_find_pri(me
->mnt_opts
)) < 0)
1244 !!mount_test_option(me
->mnt_opts
, MNTOPT_NOAUTO
),
1245 !!mount_test_option(me
->mnt_opts
, "comment=systemd.swapon"),
1248 r
= mount_add_one(m
, what
, where
, me
->mnt_opts
, me
->mnt_type
, false, false);
1264 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1266 char *device
, *path
, *options
, *options2
, *fstype
, *d
, *p
, *o
;
1270 rewind(m
->proc_self_mountinfo
);
1275 device
= path
= options
= options2
= fstype
= d
= p
= o
= NULL
;
1277 if ((k
= fscanf(m
->proc_self_mountinfo
,
1278 "%*s " /* (1) mount id */
1279 "%*s " /* (2) parent id */
1280 "%*s " /* (3) major:minor */
1281 "%*s " /* (4) root */
1282 "%ms " /* (5) mount point */
1283 "%ms" /* (6) mount options */
1284 "%*[^-]" /* (7) optional fields */
1285 "- " /* (8) seperator */
1286 "%ms " /* (9) file system type */
1287 "%ms" /* (10) mount source */
1288 "%ms" /* (11) mount options 2 */
1289 "%*[^\n]", /* some rubbish at the end */
1303 if (asprintf(&o
, "%s,%s", options
, options2
) < 0) {
1308 if (!(d
= cunescape(device
)) ||
1309 !(p
= cunescape(path
))) {
1314 if ((r
= mount_add_one(m
, d
, p
, o
, fstype
, true, set_flags
)) < 0)
1342 static void mount_shutdown(Manager
*m
) {
1345 if (m
->proc_self_mountinfo
) {
1346 fclose(m
->proc_self_mountinfo
);
1347 m
->proc_self_mountinfo
= NULL
;
1351 static int mount_enumerate(Manager
*m
) {
1353 struct epoll_event ev
;
1356 if (!m
->proc_self_mountinfo
) {
1357 if (!(m
->proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re")))
1360 m
->mount_watch
.type
= WATCH_MOUNT
;
1361 m
->mount_watch
.fd
= fileno(m
->proc_self_mountinfo
);
1364 ev
.events
= EPOLLERR
;
1365 ev
.data
.ptr
= &m
->mount_watch
;
1367 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->mount_watch
.fd
, &ev
) < 0)
1371 if ((r
= mount_load_etc_fstab(m
)) < 0)
1374 if ((r
= mount_load_proc_self_mountinfo(m
, false)) < 0)
1384 void mount_fd_event(Manager
*m
, int events
) {
1389 assert(events
== EPOLLERR
);
1391 /* The manager calls this for every fd event happening on the
1392 * /proc/self/mountinfo file, which informs us about mounting
1395 if ((r
= mount_load_proc_self_mountinfo(m
, true)) < 0) {
1396 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(errno
));
1398 /* Reset flags, just in case, for later calls */
1399 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1400 Mount
*mount
= (Mount
*) meta
;
1402 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1408 manager_dispatch_load_queue(m
);
1410 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1411 Mount
*mount
= (Mount
*) meta
;
1413 if (!mount
->is_mounted
) {
1414 /* This has just been unmounted. */
1416 mount
->from_proc_self_mountinfo
= false;
1418 switch (mount
->state
) {
1421 mount_enter_dead(mount
, true);
1425 mount_set_state(mount
, mount
->state
);
1430 } else if (mount
->just_mounted
|| mount
->just_changed
) {
1432 /* New or changed entrymount */
1434 switch (mount
->state
) {
1437 case MOUNT_MAINTENANCE
:
1438 mount_enter_mounted(mount
, true);
1441 case MOUNT_MOUNTING
:
1442 mount_enter_mounting_done(mount
);
1446 /* Nothing really changed, but let's
1447 * issue an notification call
1448 * nonetheless, in case somebody is
1449 * waiting for this. (e.g. file system
1450 * ro/rw remounts.) */
1451 mount_set_state(mount
, mount
->state
);
1456 /* Reset the flags for later calls */
1457 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1461 int mount_path_is_mounted(Manager
*m
, const char* path
) {
1471 if (!(t
= strdup(path
)))
1474 path_kill_slashes(t
);
1480 if (!(e
= unit_name_from_path(t
, ".mount"))) {
1485 u
= manager_get_unit(m
, e
);
1489 (MOUNT(u
)->from_etc_fstab
|| MOUNT(u
)->from_fragment
) &&
1490 MOUNT(u
)->state
!= MOUNT_MOUNTED
) {
1495 assert_se(slash
= strrchr(t
, '/'));
1512 static const char* const mount_state_table
[_MOUNT_STATE_MAX
] = {
1513 [MOUNT_DEAD
] = "dead",
1514 [MOUNT_MOUNTING
] = "mounting",
1515 [MOUNT_MOUNTING_DONE
] = "mounting-done",
1516 [MOUNT_MOUNTED
] = "mounted",
1517 [MOUNT_REMOUNTING
] = "remounting",
1518 [MOUNT_UNMOUNTING
] = "unmounting",
1519 [MOUNT_MOUNTING_SIGTERM
] = "mounting-sigterm",
1520 [MOUNT_MOUNTING_SIGKILL
] = "mounting-sigkill",
1521 [MOUNT_REMOUNTING_SIGTERM
] = "remounting-sigterm",
1522 [MOUNT_REMOUNTING_SIGKILL
] = "remounting-sigkill",
1523 [MOUNT_UNMOUNTING_SIGTERM
] = "unmounting-sigterm",
1524 [MOUNT_UNMOUNTING_SIGKILL
] = "unmounting-sigkill",
1525 [MOUNT_MAINTENANCE
] = "maintenance"
1528 DEFINE_STRING_TABLE_LOOKUP(mount_state
, MountState
);
1530 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
1531 [MOUNT_EXEC_MOUNT
] = "ExecMount",
1532 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
1533 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
1536 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
1538 const UnitVTable mount_vtable
= {
1542 .no_instances
= true,
1549 .coldplug
= mount_coldplug
,
1553 .start
= mount_start
,
1555 .reload
= mount_reload
,
1557 .serialize
= mount_serialize
,
1558 .deserialize_item
= mount_deserialize_item
,
1560 .active_state
= mount_active_state
,
1561 .sub_state_to_string
= mount_sub_state_to_string
,
1563 .check_gc
= mount_check_gc
,
1565 .sigchld_event
= mount_sigchld_event
,
1566 .timer_event
= mount_timer_event
,
1568 .bus_message_handler
= bus_mount_message_handler
,
1570 .enumerate
= mount_enumerate
,
1571 .shutdown
= mount_shutdown