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_path_links(Mount
*m
) {
176 LIST_FOREACH(units_per_type
, other
, m
->meta
.manager
->units_per_type
[UNIT_PATH
])
177 if ((r
= path_add_one_mount_link((Path
*) other
, m
)) < 0)
183 static int mount_add_automount_links(Mount
*m
) {
189 LIST_FOREACH(units_per_type
, other
, m
->meta
.manager
->units_per_type
[UNIT_AUTOMOUNT
])
190 if ((r
= automount_add_one_mount_link((Automount
*) other
, m
)) < 0)
196 static int mount_add_socket_links(Mount
*m
) {
202 LIST_FOREACH(units_per_type
, other
, m
->meta
.manager
->units_per_type
[UNIT_SOCKET
])
203 if ((r
= socket_add_one_mount_link((Socket
*) other
, m
)) < 0)
209 static char* mount_test_option(const char *haystack
, const char *needle
) {
214 /* Like glibc's hasmntopt(), but works on a string, not a
221 me
.mnt_opts
= (char*) haystack
;
223 return hasmntopt(&me
, needle
);
226 static int mount_add_target_links(Mount
*m
) {
231 bool noauto
, handle
, automount
, user
;
235 if (m
->from_fragment
)
236 p
= &m
->parameters_fragment
;
237 else if (m
->from_etc_fstab
)
238 p
= &m
->parameters_etc_fstab
;
242 noauto
= !!mount_test_option(p
->options
, MNTOPT_NOAUTO
);
243 user
= mount_test_option(p
->options
, "user") || mount_test_option(p
->options
, "users");
244 handle
= !!mount_test_option(p
->options
, "comment=systemd.mount");
245 automount
= !!mount_test_option(p
->options
, "comment=systemd.automount");
247 if (mount_test_option(p
->options
, "_netdev") ||
248 fstype_is_network(p
->fstype
))
249 target
= SPECIAL_REMOTE_FS_TARGET
;
251 target
= SPECIAL_LOCAL_FS_TARGET
;
253 if ((r
= manager_load_unit(UNIT(m
)->meta
.manager
, target
, NULL
, &tu
)) < 0)
256 if (automount
&& m
->meta
.manager
->running_as
!= MANAGER_SESSION
) {
259 if ((r
= unit_load_related_unit(UNIT(m
), ".automount", &am
)) < 0)
262 if ((r
= unit_add_dependency(tu
, UNIT_WANTS
, UNIT(am
), true)) < 0)
265 return unit_add_dependency(UNIT(am
), UNIT_BEFORE
, tu
, true);
269 if (!noauto
&& handle
)
270 if (user
|| m
->meta
.manager
->running_as
!= MANAGER_SESSION
)
271 if ((r
= unit_add_dependency(tu
, UNIT_WANTS
, UNIT(m
), true)) < 0)
274 return unit_add_dependency(UNIT(m
), UNIT_BEFORE
, tu
, true);
278 static int mount_verify(Mount
*m
) {
283 if (m
->meta
.load_state
!= UNIT_LOADED
)
286 if (!m
->from_etc_fstab
&& !m
->from_fragment
&& !m
->from_proc_self_mountinfo
)
289 if (!(e
= unit_name_from_path(m
->where
, ".mount")))
292 b
= unit_has_name(UNIT(m
), e
);
296 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(m
)->meta
.id
);
300 if (m
->meta
.fragment_path
&& !m
->parameters_fragment
.what
) {
301 log_error("%s's What setting is missing. Refusing.", UNIT(m
)->meta
.id
);
308 static int mount_load(Unit
*u
) {
313 assert(u
->meta
.load_state
== UNIT_STUB
);
315 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
318 /* This is a new unit? Then let's add in some extras */
319 if (u
->meta
.load_state
== UNIT_LOADED
) {
320 const char *what
= NULL
;
322 if (m
->meta
.fragment_path
)
323 m
->from_fragment
= true;
326 if (!(m
->where
= unit_name_to_path(u
->meta
.id
)))
329 path_kill_slashes(m
->where
);
331 if (!m
->meta
.description
)
332 if ((r
= unit_set_description(u
, m
->where
)) < 0)
335 if (m
->from_fragment
&& m
->parameters_fragment
.what
)
336 what
= m
->parameters_fragment
.what
;
337 else if (m
->from_etc_fstab
&& m
->parameters_etc_fstab
.what
)
338 what
= m
->parameters_etc_fstab
.what
;
339 else if (m
->from_proc_self_mountinfo
&& m
->parameters_proc_self_mountinfo
.what
)
340 what
= m
->parameters_proc_self_mountinfo
.what
;
343 if ((r
= unit_add_node_link(u
, what
,
344 (u
->meta
.manager
->running_as
== MANAGER_INIT
||
345 u
->meta
.manager
->running_as
== MANAGER_SYSTEM
))) < 0)
348 if ((r
= mount_add_mount_links(m
)) < 0)
351 if ((r
= mount_add_socket_links(m
)) < 0)
354 if ((r
= mount_add_swap_links(m
)) < 0)
357 if ((r
= mount_add_path_links(m
)) < 0)
360 if ((r
= mount_add_automount_links(m
)) < 0)
363 if ((r
= mount_add_target_links(m
)) < 0)
366 if ((r
= unit_add_default_cgroup(u
)) < 0)
370 return mount_verify(m
);
373 static int mount_notify_automount(Mount
*m
, int status
) {
379 if ((r
= unit_get_related_unit(UNIT(m
), ".automount", &p
)) < 0)
380 return r
== -ENOENT
? 0 : r
;
382 return automount_send_ready(AUTOMOUNT(p
), status
);
385 static void mount_set_state(Mount
*m
, MountState state
) {
386 MountState old_state
;
389 old_state
= m
->state
;
392 if (state
!= MOUNT_MOUNTING
&&
393 state
!= MOUNT_MOUNTING_DONE
&&
394 state
!= MOUNT_REMOUNTING
&&
395 state
!= MOUNT_UNMOUNTING
&&
396 state
!= MOUNT_MOUNTING_SIGTERM
&&
397 state
!= MOUNT_MOUNTING_SIGKILL
&&
398 state
!= MOUNT_UNMOUNTING_SIGTERM
&&
399 state
!= MOUNT_UNMOUNTING_SIGKILL
&&
400 state
!= MOUNT_REMOUNTING_SIGTERM
&&
401 state
!= MOUNT_REMOUNTING_SIGKILL
) {
402 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
403 mount_unwatch_control_pid(m
);
404 m
->control_command
= NULL
;
405 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
408 if (state
== MOUNT_MOUNTED
||
409 state
== MOUNT_REMOUNTING
)
410 mount_notify_automount(m
, 0);
411 else if (state
== MOUNT_DEAD
||
412 state
== MOUNT_UNMOUNTING
||
413 state
== MOUNT_MOUNTING_SIGTERM
||
414 state
== MOUNT_MOUNTING_SIGKILL
||
415 state
== MOUNT_REMOUNTING_SIGTERM
||
416 state
== MOUNT_REMOUNTING_SIGKILL
||
417 state
== MOUNT_UNMOUNTING_SIGTERM
||
418 state
== MOUNT_UNMOUNTING_SIGKILL
||
419 state
== MOUNT_MAINTAINANCE
)
420 mount_notify_automount(m
, -ENODEV
);
422 if (state
!= old_state
)
423 log_debug("%s changed %s -> %s",
425 mount_state_to_string(old_state
),
426 mount_state_to_string(state
));
428 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
]);
431 static int mount_coldplug(Unit
*u
) {
433 MountState new_state
= MOUNT_DEAD
;
437 assert(m
->state
== MOUNT_DEAD
);
439 if (m
->deserialized_state
!= m
->state
)
440 new_state
= m
->deserialized_state
;
441 else if (m
->from_proc_self_mountinfo
)
442 new_state
= MOUNT_MOUNTED
;
444 if (new_state
!= m
->state
) {
446 if (new_state
== MOUNT_MOUNTING
||
447 new_state
== MOUNT_MOUNTING_DONE
||
448 new_state
== MOUNT_REMOUNTING
||
449 new_state
== MOUNT_UNMOUNTING
||
450 new_state
== MOUNT_MOUNTING_SIGTERM
||
451 new_state
== MOUNT_MOUNTING_SIGKILL
||
452 new_state
== MOUNT_UNMOUNTING_SIGTERM
||
453 new_state
== MOUNT_UNMOUNTING_SIGKILL
||
454 new_state
== MOUNT_REMOUNTING_SIGTERM
||
455 new_state
== MOUNT_REMOUNTING_SIGKILL
) {
457 if (m
->control_pid
<= 0)
460 if ((r
= unit_watch_pid(UNIT(m
), m
->control_pid
)) < 0)
463 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
467 mount_set_state(m
, new_state
);
473 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
480 if (m
->from_proc_self_mountinfo
)
481 p
= &m
->parameters_proc_self_mountinfo
;
482 else if (m
->from_fragment
)
483 p
= &m
->parameters_fragment
;
485 p
= &m
->parameters_etc_fstab
;
488 "%sMount State: %s\n"
491 "%sFile System Type: %s\n"
493 "%sFrom /etc/fstab: %s\n"
494 "%sFrom /proc/self/mountinfo: %s\n"
495 "%sFrom fragment: %s\n"
497 prefix
, mount_state_to_string(m
->state
),
499 prefix
, strna(p
->what
),
500 prefix
, strna(p
->fstype
),
501 prefix
, strna(p
->options
),
502 prefix
, yes_no(m
->from_etc_fstab
),
503 prefix
, yes_no(m
->from_proc_self_mountinfo
),
504 prefix
, yes_no(m
->from_fragment
),
505 prefix
, kill_mode_to_string(m
->kill_mode
));
507 if (m
->control_pid
> 0)
509 "%sControl PID: %llu\n",
510 prefix
, (unsigned long long) m
->control_pid
);
512 exec_context_dump(&m
->exec_context
, f
, prefix
);
515 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
523 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
526 if ((r
= exec_spawn(c
,
530 m
->meta
.manager
->environment
,
533 UNIT(m
)->meta
.manager
->confirm_spawn
,
534 UNIT(m
)->meta
.cgroup_bondings
,
538 if ((r
= unit_watch_pid(UNIT(m
), pid
)) < 0)
539 /* FIXME: we need to do something here */
547 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
552 static void mount_enter_dead(Mount
*m
, bool success
) {
558 mount_set_state(m
, m
->failure
? MOUNT_MAINTAINANCE
: MOUNT_DEAD
);
561 static void mount_enter_mounted(Mount
*m
, bool success
) {
567 mount_set_state(m
, MOUNT_MOUNTED
);
570 static void mount_enter_signal(Mount
*m
, MountState state
, bool success
) {
579 if (m
->kill_mode
!= KILL_NONE
) {
580 int sig
= (state
== MOUNT_MOUNTING_SIGTERM
||
581 state
== MOUNT_UNMOUNTING_SIGTERM
||
582 state
== MOUNT_REMOUNTING_SIGTERM
) ? SIGTERM
: SIGKILL
;
584 if (m
->kill_mode
== KILL_CONTROL_GROUP
) {
586 if ((r
= cgroup_bonding_kill_list(UNIT(m
)->meta
.cgroup_bondings
, sig
)) < 0) {
587 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
593 if (!sent
&& m
->control_pid
> 0)
594 if (kill(m
->kill_mode
== KILL_PROCESS
? m
->control_pid
: -m
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
601 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
604 mount_set_state(m
, state
);
605 } else if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
606 mount_enter_mounted(m
, true);
608 mount_enter_dead(m
, true);
613 log_warning("%s failed to kill processes: %s", UNIT(m
)->meta
.id
, strerror(-r
));
615 if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
616 mount_enter_mounted(m
, false);
618 mount_enter_dead(m
, false);
621 static void mount_enter_unmounting(Mount
*m
, bool success
) {
629 m
->control_command_id
= MOUNT_EXEC_UNMOUNT
;
630 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
632 if ((r
= exec_command_set(
639 mount_unwatch_control_pid(m
);
641 if ((r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
)) < 0)
644 mount_set_state(m
, MOUNT_UNMOUNTING
);
649 log_warning("%s failed to run umount exectuable: %s", UNIT(m
)->meta
.id
, strerror(-r
));
650 mount_enter_mounted(m
, false);
653 static void mount_enter_mounting(Mount
*m
) {
658 m
->control_command_id
= MOUNT_EXEC_MOUNT
;
659 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
661 if (m
->from_fragment
)
662 r
= exec_command_set(
665 m
->parameters_fragment
.what
,
667 "-t", m
->parameters_fragment
.fstype
,
668 m
->parameters_fragment
.options
? "-o" : NULL
, m
->parameters_fragment
.options
,
670 else if (m
->from_etc_fstab
)
671 r
= exec_command_set(
682 mount_unwatch_control_pid(m
);
684 if ((r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
)) < 0)
687 mount_set_state(m
, MOUNT_MOUNTING
);
692 log_warning("%s failed to run mount exectuable: %s", UNIT(m
)->meta
.id
, strerror(-r
));
693 mount_enter_dead(m
, false);
696 static void mount_enter_mounting_done(Mount
*m
) {
699 mount_set_state(m
, MOUNT_MOUNTING_DONE
);
702 static void mount_enter_remounting(Mount
*m
, bool success
) {
710 m
->control_command_id
= MOUNT_EXEC_REMOUNT
;
711 m
->control_command
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
713 if (m
->from_fragment
) {
717 if (m
->parameters_fragment
.options
) {
718 if (!(buf
= strappend("remount,", m
->parameters_fragment
.options
))) {
727 r
= exec_command_set(
730 m
->parameters_fragment
.what
,
732 "-t", m
->parameters_fragment
.fstype
,
737 } else if (m
->from_etc_fstab
)
738 r
= exec_command_set(
752 mount_unwatch_control_pid(m
);
754 if ((r
= mount_spawn(m
, m
->control_command
, &m
->control_pid
)) < 0)
757 mount_set_state(m
, MOUNT_REMOUNTING
);
762 mount_enter_mounted(m
, false);
765 static int mount_start(Unit
*u
) {
770 /* We cannot fulfill this request right now, try again later
772 if (m
->state
== MOUNT_UNMOUNTING
||
773 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
774 m
->state
== MOUNT_UNMOUNTING_SIGKILL
)
778 if (m
->state
== MOUNT_MOUNTING
||
779 m
->state
== MOUNT_MOUNTING_SIGTERM
||
780 m
->state
== MOUNT_MOUNTING_SIGKILL
)
783 assert(m
->state
== MOUNT_DEAD
|| m
->state
== MOUNT_MAINTAINANCE
);
786 mount_enter_mounting(m
);
790 static int mount_stop(Unit
*u
) {
795 /* Cann't do this right now. */
796 if (m
->state
== MOUNT_MOUNTING
||
797 m
->state
== MOUNT_MOUNTING_DONE
||
798 m
->state
== MOUNT_MOUNTING_SIGTERM
||
799 m
->state
== MOUNT_MOUNTING_SIGKILL
||
800 m
->state
== MOUNT_REMOUNTING
||
801 m
->state
== MOUNT_REMOUNTING_SIGTERM
||
802 m
->state
== MOUNT_REMOUNTING_SIGKILL
)
806 if (m
->state
== MOUNT_UNMOUNTING
||
807 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
808 m
->state
== MOUNT_UNMOUNTING_SIGTERM
)
811 assert(m
->state
== MOUNT_MOUNTED
);
813 mount_enter_unmounting(m
, true);
817 static int mount_reload(Unit
*u
) {
822 if (m
->state
== MOUNT_MOUNTING_DONE
)
825 assert(m
->state
== MOUNT_MOUNTED
);
827 mount_enter_remounting(m
, true);
831 static int mount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
838 unit_serialize_item(u
, f
, "state", mount_state_to_string(m
->state
));
839 unit_serialize_item(u
, f
, "failure", yes_no(m
->failure
));
841 if (m
->control_pid
> 0)
842 unit_serialize_item_format(u
, f
, "control-pid", "%u", (unsigned) m
->control_pid
);
844 if (m
->control_command_id
>= 0)
845 unit_serialize_item(u
, f
, "control-command", mount_exec_command_to_string(m
->control_command_id
));
850 static int mount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
859 if (streq(key
, "state")) {
862 if ((state
= mount_state_from_string(value
)) < 0)
863 log_debug("Failed to parse state value %s", value
);
865 m
->deserialized_state
= state
;
866 } else if (streq(key
, "failure")) {
869 if ((b
= parse_boolean(value
)) < 0)
870 log_debug("Failed to parse failure value %s", value
);
872 m
->failure
= b
|| m
->failure
;
874 } else if (streq(key
, "control-pid")) {
877 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
878 log_debug("Failed to parse control-pid value %s", value
);
880 m
->control_pid
= (pid_t
) pid
;
881 } else if (streq(key
, "control-command")) {
884 if ((id
= mount_exec_command_from_string(value
)) < 0)
885 log_debug("Failed to parse exec-command value %s", value
);
887 m
->control_command_id
= id
;
888 m
->control_command
= m
->exec_command
+ id
;
892 log_debug("Unknown serialization key '%s'", key
);
897 static UnitActiveState
mount_active_state(Unit
*u
) {
900 return state_translation_table
[MOUNT(u
)->state
];
903 static const char *mount_sub_state_to_string(Unit
*u
) {
906 return mount_state_to_string(MOUNT(u
)->state
);
909 static bool mount_check_gc(Unit
*u
) {
914 return m
->from_etc_fstab
|| m
->from_proc_self_mountinfo
;
917 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
924 if (pid
!= m
->control_pid
)
929 success
= is_clean_exit(code
, status
);
930 m
->failure
= m
->failure
|| !success
;
932 if (m
->control_command
) {
933 exec_status_fill(&m
->control_command
->exec_status
, pid
, code
, status
);
934 m
->control_command
= NULL
;
935 m
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
938 log_debug("%s control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
940 /* Note that mount(8) returning and the kernel sending us a
941 * mount table change event might happen out-of-order. If an
942 * operation succeed we assume the kernel will follow soon too
943 * and already change into the resulting state. If it fails
944 * we check if the kernel still knows about the mount. and
945 * change state accordingly. */
950 case MOUNT_MOUNTING_DONE
:
951 case MOUNT_MOUNTING_SIGKILL
:
952 case MOUNT_MOUNTING_SIGTERM
:
953 case MOUNT_REMOUNTING
:
954 case MOUNT_REMOUNTING_SIGKILL
:
955 case MOUNT_REMOUNTING_SIGTERM
:
958 mount_enter_mounted(m
, true);
959 else if (m
->from_proc_self_mountinfo
)
960 mount_enter_mounted(m
, false);
962 mount_enter_dead(m
, false);
965 case MOUNT_UNMOUNTING
:
966 case MOUNT_UNMOUNTING_SIGKILL
:
967 case MOUNT_UNMOUNTING_SIGTERM
:
970 mount_enter_dead(m
, true);
971 else if (m
->from_proc_self_mountinfo
)
972 mount_enter_mounted(m
, false);
974 mount_enter_dead(m
, false);
978 assert_not_reached("Uh, control process died at wrong time.");
982 static void mount_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
986 assert(elapsed
== 1);
987 assert(w
== &m
->timer_watch
);
992 case MOUNT_MOUNTING_DONE
:
993 log_warning("%s mounting timed out. Stopping.", u
->meta
.id
);
994 mount_enter_signal(m
, MOUNT_MOUNTING_SIGTERM
, false);
997 case MOUNT_REMOUNTING
:
998 log_warning("%s remounting timed out. Stopping.", u
->meta
.id
);
999 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, false);
1002 case MOUNT_UNMOUNTING
:
1003 log_warning("%s unmounting timed out. Stopping.", u
->meta
.id
);
1004 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, false);
1007 case MOUNT_MOUNTING_SIGTERM
:
1008 log_warning("%s mounting timed out. Killing.", u
->meta
.id
);
1009 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, false);
1012 case MOUNT_REMOUNTING_SIGTERM
:
1013 log_warning("%s remounting timed out. Killing.", u
->meta
.id
);
1014 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, false);
1017 case MOUNT_UNMOUNTING_SIGTERM
:
1018 log_warning("%s unmounting timed out. Killing.", u
->meta
.id
);
1019 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, false);
1022 case MOUNT_MOUNTING_SIGKILL
:
1023 case MOUNT_REMOUNTING_SIGKILL
:
1024 case MOUNT_UNMOUNTING_SIGKILL
:
1025 log_warning("%s mount process still around after SIGKILL. Ignoring.", u
->meta
.id
);
1027 if (m
->from_proc_self_mountinfo
)
1028 mount_enter_mounted(m
, false);
1030 mount_enter_dead(m
, false);
1034 assert_not_reached("Timeout at wrong time.");
1038 static int mount_add_one(
1042 const char *options
,
1044 bool from_proc_self_mountinfo
,
1049 char *e
, *w
= NULL
, *o
= NULL
, *f
= NULL
;
1058 assert(!set_flags
|| from_proc_self_mountinfo
);
1060 /* Ignore API mount points. They should never be referenced in
1061 * dependencies ever. */
1062 if (mount_point_is_api(where
))
1065 if (streq(fstype
, "autofs"))
1068 /* probably some kind of swap, ignore */
1069 if (!is_path(where
))
1072 if (!(e
= unit_name_from_path(where
, ".mount")))
1075 if (!(u
= manager_get_unit(m
, e
))) {
1078 if (!(u
= unit_new(m
))) {
1083 r
= unit_add_name(u
, e
);
1089 if (!(MOUNT(u
)->where
= strdup(where
))) {
1094 unit_add_to_load_queue(u
);
1100 if (!(w
= strdup(what
)) ||
1101 !(o
= strdup(options
)) ||
1102 !(f
= strdup(fstype
))) {
1107 if (from_proc_self_mountinfo
) {
1108 p
= &MOUNT(u
)->parameters_proc_self_mountinfo
;
1111 MOUNT(u
)->is_mounted
= true;
1112 MOUNT(u
)->just_mounted
= !MOUNT(u
)->from_proc_self_mountinfo
;
1113 MOUNT(u
)->just_changed
= !streq_ptr(p
->options
, o
);
1116 MOUNT(u
)->from_proc_self_mountinfo
= true;
1118 p
= &MOUNT(u
)->parameters_etc_fstab
;
1119 MOUNT(u
)->from_etc_fstab
= true;
1131 unit_add_to_dbus_queue(u
);
1146 static char *fstab_node_to_udev_node(char *p
) {
1150 /* FIXME: to follow udev's logic 100% we need to leave valid
1151 * UTF8 chars unescaped */
1153 if (startswith(p
, "LABEL=")) {
1155 if (!(t
= xescape(p
+6, "/ ")))
1158 r
= asprintf(&dn
, "/dev/disk/by-label/%s", t
);
1167 if (startswith(p
, "UUID=")) {
1169 if (!(t
= xescape(p
+5, "/ ")))
1172 r
= asprintf(&dn
, "/dev/disk/by-uuid/%s", ascii_strlower(t
));
1184 static int mount_find_pri(char *options
) {
1188 if (!(pri
= mount_test_option(options
, "pri=")))
1194 r
= strtoul(pri
, &end
, 10);
1199 if (end
== pri
|| (*end
!= ',' && *end
!= 0))
1205 static int mount_load_etc_fstab(Manager
*m
) {
1213 if (!(f
= setmntent("/etc/fstab", "r")))
1216 while ((me
= getmntent(f
))) {
1219 if (!(what
= fstab_node_to_udev_node(me
->mnt_fsname
))) {
1224 if (!(where
= strdup(me
->mnt_dir
))) {
1231 path_kill_slashes(what
);
1233 if (where
[0] == '/')
1234 path_kill_slashes(where
);
1236 if (streq(me
->mnt_type
, "swap")) {
1239 if ((pri
= mount_find_pri(me
->mnt_opts
)) < 0)
1245 !!mount_test_option(me
->mnt_opts
, MNTOPT_NOAUTO
),
1246 !!mount_test_option(me
->mnt_opts
, "comment=systemd.swapon"),
1249 r
= mount_add_one(m
, what
, where
, me
->mnt_opts
, me
->mnt_type
, false, false);
1265 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1267 char *device
, *path
, *options
, *options2
, *fstype
, *d
, *p
, *o
;
1271 rewind(m
->proc_self_mountinfo
);
1276 device
= path
= options
= options2
= fstype
= d
= p
= o
= NULL
;
1278 if ((k
= fscanf(m
->proc_self_mountinfo
,
1279 "%*s " /* (1) mount id */
1280 "%*s " /* (2) parent id */
1281 "%*s " /* (3) major:minor */
1282 "%*s " /* (4) root */
1283 "%ms " /* (5) mount point */
1284 "%ms" /* (6) mount options */
1285 "%*[^-]" /* (7) optional fields */
1286 "- " /* (8) seperator */
1287 "%ms " /* (9) file system type */
1288 "%ms" /* (10) mount source */
1289 "%ms" /* (11) mount options 2 */
1290 "%*[^\n]", /* some rubbish at the end */
1304 if (asprintf(&o
, "%s,%s", options
, options2
) < 0) {
1309 if (!(d
= cunescape(device
)) ||
1310 !(p
= cunescape(path
))) {
1315 if ((r
= mount_add_one(m
, d
, p
, o
, fstype
, true, set_flags
)) < 0)
1343 static void mount_shutdown(Manager
*m
) {
1346 if (m
->proc_self_mountinfo
) {
1347 fclose(m
->proc_self_mountinfo
);
1348 m
->proc_self_mountinfo
= NULL
;
1352 static int mount_enumerate(Manager
*m
) {
1354 struct epoll_event ev
;
1357 if (!m
->proc_self_mountinfo
) {
1358 if (!(m
->proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re")))
1361 m
->mount_watch
.type
= WATCH_MOUNT
;
1362 m
->mount_watch
.fd
= fileno(m
->proc_self_mountinfo
);
1365 ev
.events
= EPOLLERR
;
1366 ev
.data
.ptr
= &m
->mount_watch
;
1368 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->mount_watch
.fd
, &ev
) < 0)
1372 if ((r
= mount_load_etc_fstab(m
)) < 0)
1375 if ((r
= mount_load_proc_self_mountinfo(m
, false)) < 0)
1385 void mount_fd_event(Manager
*m
, int events
) {
1390 assert(events
== EPOLLERR
);
1392 /* The manager calls this for every fd event happening on the
1393 * /proc/self/mountinfo file, which informs us about mounting
1396 if ((r
= mount_load_proc_self_mountinfo(m
, true)) < 0) {
1397 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(errno
));
1399 /* Reset flags, just in case, for later calls */
1400 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1401 Mount
*mount
= (Mount
*) meta
;
1403 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1409 manager_dispatch_load_queue(m
);
1411 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1412 Mount
*mount
= (Mount
*) meta
;
1414 if (!mount
->is_mounted
) {
1415 /* This has just been unmounted. */
1417 mount
->from_proc_self_mountinfo
= false;
1419 switch (mount
->state
) {
1422 mount_enter_dead(mount
, true);
1426 mount_set_state(mount
, mount
->state
);
1431 } else if (mount
->just_mounted
|| mount
->just_changed
) {
1433 /* New or changed entrymount */
1435 switch (mount
->state
) {
1438 case MOUNT_MAINTAINANCE
:
1439 mount_enter_mounted(mount
, true);
1442 case MOUNT_MOUNTING
:
1443 mount_enter_mounting_done(mount
);
1447 /* Nothing really changed, but let's
1448 * issue an notification call
1449 * nonetheless, in case somebody is
1450 * waiting for this. (e.g. file system
1451 * ro/rw remounts.) */
1452 mount_set_state(mount
, mount
->state
);
1457 /* Reset the flags for later calls */
1458 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1462 int mount_path_is_mounted(Manager
*m
, const char* path
) {
1472 if (!(t
= strdup(path
)))
1475 path_kill_slashes(t
);
1481 if (!(e
= unit_name_from_path(t
, ".mount"))) {
1486 u
= manager_get_unit(m
, e
);
1490 (MOUNT(u
)->from_etc_fstab
|| MOUNT(u
)->from_fragment
) &&
1491 MOUNT(u
)->state
!= MOUNT_MOUNTED
) {
1496 assert_se(slash
= strrchr(t
, '/'));
1513 static const char* const mount_state_table
[_MOUNT_STATE_MAX
] = {
1514 [MOUNT_DEAD
] = "dead",
1515 [MOUNT_MOUNTING
] = "mounting",
1516 [MOUNT_MOUNTING_DONE
] = "mounting-done",
1517 [MOUNT_MOUNTED
] = "mounted",
1518 [MOUNT_REMOUNTING
] = "remounting",
1519 [MOUNT_UNMOUNTING
] = "unmounting",
1520 [MOUNT_MOUNTING_SIGTERM
] = "mounting-sigterm",
1521 [MOUNT_MOUNTING_SIGKILL
] = "mounting-sigkill",
1522 [MOUNT_REMOUNTING_SIGTERM
] = "remounting-sigterm",
1523 [MOUNT_REMOUNTING_SIGKILL
] = "remounting-sigkill",
1524 [MOUNT_UNMOUNTING_SIGTERM
] = "unmounting-sigterm",
1525 [MOUNT_UNMOUNTING_SIGKILL
] = "unmounting-sigkill",
1526 [MOUNT_MAINTAINANCE
] = "maintainance"
1529 DEFINE_STRING_TABLE_LOOKUP(mount_state
, MountState
);
1531 static const char* const mount_exec_command_table
[_MOUNT_EXEC_COMMAND_MAX
] = {
1532 [MOUNT_EXEC_MOUNT
] = "ExecMount",
1533 [MOUNT_EXEC_UNMOUNT
] = "ExecUnmount",
1534 [MOUNT_EXEC_REMOUNT
] = "ExecRemount",
1537 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command
, MountExecCommand
);
1539 const UnitVTable mount_vtable
= {
1543 .no_instances
= true,
1550 .coldplug
= mount_coldplug
,
1554 .start
= mount_start
,
1556 .reload
= mount_reload
,
1558 .serialize
= mount_serialize
,
1559 .deserialize_item
= mount_deserialize_item
,
1561 .active_state
= mount_active_state
,
1562 .sub_state_to_string
= mount_sub_state_to_string
,
1564 .check_gc
= mount_check_gc
,
1566 .sigchld_event
= mount_sigchld_event
,
1567 .timer_event
= mount_timer_event
,
1569 .bus_message_handler
= bus_mount_message_handler
,
1571 .enumerate
= mount_enumerate
,
1572 .shutdown
= mount_shutdown