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 const char* const state_string_table
[_MOUNT_STATE_MAX
] = {
56 [MOUNT_DEAD
] = "dead",
57 [MOUNT_MOUNTING
] = "mounting",
58 [MOUNT_MOUNTING_DONE
] = "mounting-done",
59 [MOUNT_MOUNTED
] = "mounted",
60 [MOUNT_REMOUNTING
] = "remounting",
61 [MOUNT_UNMOUNTING
] = "unmounting",
62 [MOUNT_MOUNTING_SIGTERM
] = "mounting-sigterm",
63 [MOUNT_MOUNTING_SIGKILL
] = "mounting-sigkill",
64 [MOUNT_REMOUNTING_SIGTERM
] = "remounting-sigterm",
65 [MOUNT_REMOUNTING_SIGKILL
] = "remounting-sigkill",
66 [MOUNT_UNMOUNTING_SIGTERM
] = "unmounting-sigterm",
67 [MOUNT_UNMOUNTING_SIGKILL
] = "unmounting-sigkill",
68 [MOUNT_MAINTAINANCE
] = "maintainance"
71 static char *mount_name_from_where(const char *where
) {
74 if (streq(where
, "/"))
75 return strdup("-.mount");
77 return unit_name_build_escape(where
+1, NULL
, ".mount");
80 static void service_unwatch_control_pid(Mount
*m
) {
83 if (m
->control_pid
<= 0)
86 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
90 static void mount_parameters_done(MountParameters
*p
) {
97 p
->what
= p
->options
= p
->fstype
= NULL
;
100 static void mount_done(Unit
*u
) {
108 mount_parameters_done(&m
->parameters_etc_fstab
);
109 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
110 mount_parameters_done(&m
->parameters_fragment
);
112 exec_context_done(&m
->exec_context
);
113 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
114 m
->control_command
= NULL
;
116 service_unwatch_control_pid(m
);
118 unit_unwatch_timer(u
, &m
->timer_watch
);
121 static void mount_init(Unit
*u
) {
125 assert(u
->meta
.load_state
== UNIT_STUB
);
127 m
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
128 exec_context_init(&m
->exec_context
);
130 /* We need to make sure that /bin/mount is always called in
131 * the same process group as us, so that the autofs kernel
132 * side doesn't send us another mount request while we are
133 * already trying to comply its last one. */
134 m
->exec_context
.no_setsid
= true;
136 m
->timer_watch
.type
= WATCH_INVALID
;
139 static int mount_notify_automount(Mount
*m
, int status
) {
145 if (!(k
= unit_name_change_suffix(UNIT(m
)->meta
.id
, ".automount")))
148 p
= manager_get_unit(UNIT(m
)->meta
.manager
, k
);
154 return automount_send_ready(AUTOMOUNT(p
), status
);
157 static int mount_add_node_links(Mount
*m
) {
165 /* Adds in links to the device that this node is based on */
167 if (m
->parameters_fragment
.what
)
168 what
= m
->parameters_fragment
.what
;
169 else if (m
->parameters_etc_fstab
.what
)
170 what
= m
->parameters_etc_fstab
.what
;
172 /* We observe kernel mounts only while they are live,
173 * hence don't create any links for them */
176 if (!path_startswith(what
, "/dev/"))
179 if (!(e
= unit_name_build_escape(what
+1, NULL
, ".device")))
182 r
= manager_load_unit(UNIT(m
)->meta
.manager
, e
, NULL
, &device
);
188 if ((r
= unit_add_dependency(UNIT(m
), UNIT_AFTER
, device
)) < 0)
191 if ((r
= unit_add_dependency(UNIT(m
), UNIT_REQUIRES
, device
)) < 0)
194 if (UNIT(m
)->meta
.manager
->running_as
== MANAGER_INIT
||
195 UNIT(m
)->meta
.manager
->running_as
== MANAGER_SYSTEM
)
196 if ((r
= unit_add_dependency(device
, UNIT_WANTS
, UNIT(m
))) < 0)
202 static int mount_add_path_links(Mount
*m
) {
208 /* Adds in link to other mount points, that might lie below or
209 * above us in the hierarchy */
211 LIST_FOREACH(units_per_type
, other
, UNIT(m
)->meta
.manager
->units_per_type
[UNIT_MOUNT
]) {
219 if (m
->meta
.load_state
!= UNIT_LOADED
)
222 if (path_startswith(m
->where
, n
->where
)) {
224 if ((r
= unit_add_dependency(UNIT(m
), UNIT_AFTER
, UNIT(other
))) < 0)
227 if (n
->from_etc_fstab
|| n
->from_fragment
)
228 if ((r
= unit_add_dependency(UNIT(m
), UNIT_REQUIRES
, UNIT(other
))) < 0)
231 } else if (path_startswith(n
->where
, m
->where
)) {
233 if ((r
= unit_add_dependency(UNIT(m
), UNIT_BEFORE
, UNIT(other
))) < 0)
236 if (m
->from_etc_fstab
|| m
->from_fragment
)
237 if ((r
= unit_add_dependency(UNIT(other
), UNIT_REQUIRES
, UNIT(m
))) < 0)
245 static bool mount_test_option(const char *haystack
, const char *needle
) {
250 /* Like glibc's hasmntopt(), but works on a string, not a
257 me
.mnt_opts
= (char*) haystack
;
259 return !!hasmntopt(&me
, needle
);
262 static int mount_add_target_links(Mount
*m
) {
272 if (m
->from_fragment
)
273 p
= &m
->parameters_fragment
;
274 else if (m
->from_etc_fstab
)
275 p
= &m
->parameters_etc_fstab
;
279 noauto
= mount_test_option(p
->options
, MNTOPT_NOAUTO
);
280 handle
= mount_test_option(p
->options
, "comment=systemd.mount");
282 if (noauto
&& !handle
)
285 if (mount_test_option(p
->options
, "_netdev") ||
286 fstype_is_network(p
->fstype
))
287 target
= SPECIAL_REMOTE_FS_TARGET
;
289 target
= SPECIAL_LOCAL_FS_TARGET
;
291 if ((r
= manager_load_unit(UNIT(m
)->meta
.manager
, target
, NULL
, &u
)) < 0)
295 if ((r
= unit_add_dependency(u
, UNIT_WANTS
, UNIT(m
))) < 0)
298 return unit_add_dependency(UNIT(m
), UNIT_BEFORE
, u
);
301 static int mount_verify(Mount
*m
) {
306 if (UNIT(m
)->meta
.load_state
!= UNIT_LOADED
)
310 log_error("%s lacks Where setting. Refusing.", UNIT(m
)->meta
.id
);
314 path_kill_slashes(m
->where
);
316 if (!(e
= mount_name_from_where(m
->where
)))
319 b
= unit_has_name(UNIT(m
), e
);
323 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(m
)->meta
.id
);
330 static int mount_load(Unit
*u
) {
335 assert(u
->meta
.load_state
== UNIT_STUB
);
337 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
340 /* This is a new unit? Then let's add in some extras */
341 if (u
->meta
.load_state
== UNIT_LOADED
) {
343 /* Minor validity checking */
344 if ((m
->parameters_fragment
.options
|| m
->parameters_fragment
.fstype
) && !m
->parameters_fragment
.what
)
347 if (m
->parameters_fragment
.what
)
348 m
->from_fragment
= true;
350 if ((r
= mount_add_node_links(MOUNT(u
))) < 0)
353 if ((r
= mount_add_path_links(MOUNT(u
))) < 0)
356 if ((r
= mount_add_target_links(MOUNT(u
))) < 0)
359 if ((r
= unit_add_default_cgroup(u
)) < 0)
363 return mount_verify(m
);
366 static void mount_set_state(Mount
*m
, MountState state
) {
367 MountState old_state
;
370 old_state
= m
->state
;
373 if (state
!= MOUNT_MOUNTING
&&
374 state
!= MOUNT_MOUNTING_DONE
&&
375 state
!= MOUNT_REMOUNTING
&&
376 state
!= MOUNT_UNMOUNTING
&&
377 state
!= MOUNT_MOUNTING_SIGTERM
&&
378 state
!= MOUNT_MOUNTING_SIGKILL
&&
379 state
!= MOUNT_UNMOUNTING_SIGTERM
&&
380 state
!= MOUNT_UNMOUNTING_SIGKILL
&&
381 state
!= MOUNT_REMOUNTING_SIGTERM
&&
382 state
!= MOUNT_REMOUNTING_SIGKILL
) {
383 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
384 service_unwatch_control_pid(m
);
385 m
->control_command
= NULL
;
388 if (state
== MOUNT_MOUNTED
||
389 state
== MOUNT_REMOUNTING
)
390 mount_notify_automount(m
, 0);
391 else if (state
== MOUNT_DEAD
||
392 state
== MOUNT_UNMOUNTING
||
393 state
== MOUNT_MOUNTING_SIGTERM
||
394 state
== MOUNT_MOUNTING_SIGKILL
||
395 state
== MOUNT_REMOUNTING_SIGTERM
||
396 state
== MOUNT_REMOUNTING_SIGKILL
||
397 state
== MOUNT_UNMOUNTING_SIGTERM
||
398 state
== MOUNT_UNMOUNTING_SIGKILL
||
399 state
== MOUNT_MAINTAINANCE
)
400 mount_notify_automount(m
, -ENODEV
);
402 if (state
!= old_state
)
403 log_debug("%s changed %s → %s", UNIT(m
)->meta
.id
, state_string_table
[old_state
], state_string_table
[state
]);
405 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
]);
408 static int mount_coldplug(Unit
*u
) {
412 assert(m
->state
== MOUNT_DEAD
);
414 if (m
->from_proc_self_mountinfo
)
415 mount_set_state(m
, MOUNT_MOUNTED
);
420 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
428 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
431 if ((r
= exec_spawn(c
,
437 UNIT(m
)->meta
.manager
->confirm_spawn
,
438 UNIT(m
)->meta
.cgroup_bondings
,
442 if ((r
= unit_watch_pid(UNIT(m
), pid
)) < 0)
443 /* FIXME: we need to do something here */
451 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
456 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
463 if (m
->from_proc_self_mountinfo
)
464 p
= &m
->parameters_proc_self_mountinfo
;
465 else if (m
->from_fragment
)
466 p
= &m
->parameters_fragment
;
468 p
= &m
->parameters_etc_fstab
;
471 "%sMount State: %s\n"
474 "%sFile System Type: %s\n"
476 "%sFrom /etc/fstab: %s\n"
477 "%sFrom /proc/self/mountinfo: %s\n"
478 "%sFrom fragment: %s\n"
480 prefix
, state_string_table
[m
->state
],
482 prefix
, strna(p
->what
),
483 prefix
, strna(p
->fstype
),
484 prefix
, strna(p
->options
),
485 prefix
, yes_no(m
->from_etc_fstab
),
486 prefix
, yes_no(m
->from_proc_self_mountinfo
),
487 prefix
, yes_no(m
->from_fragment
),
488 prefix
, kill_mode_to_string(m
->kill_mode
));
490 if (m
->control_pid
> 0)
492 "%sControl PID: %llu\n",
493 prefix
, (unsigned long long) m
->control_pid
);
495 exec_context_dump(&m
->exec_context
, f
, prefix
);
498 static void mount_enter_dead(Mount
*m
, bool success
) {
504 mount_set_state(m
, m
->failure
? MOUNT_MAINTAINANCE
: MOUNT_DEAD
);
507 static void mount_enter_mounted(Mount
*m
, bool success
) {
513 mount_set_state(m
, MOUNT_MOUNTED
);
516 static void mount_enter_signal(Mount
*m
, MountState state
, bool success
) {
525 if (m
->kill_mode
!= KILL_NONE
) {
526 int sig
= (state
== MOUNT_MOUNTING_SIGTERM
||
527 state
== MOUNT_UNMOUNTING_SIGTERM
||
528 state
== MOUNT_REMOUNTING_SIGTERM
) ? SIGTERM
: SIGKILL
;
530 if (m
->kill_mode
== KILL_CONTROL_GROUP
) {
532 if ((r
= cgroup_bonding_kill_list(UNIT(m
)->meta
.cgroup_bondings
, sig
)) < 0) {
533 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
539 if (!sent
&& m
->control_pid
> 0)
540 if (kill(m
->kill_mode
== KILL_PROCESS
? m
->control_pid
: -m
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
547 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
550 mount_set_state(m
, state
);
551 } else if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
552 mount_enter_mounted(m
, true);
554 mount_enter_dead(m
, true);
559 log_warning("%s failed to kill processes: %s", UNIT(m
)->meta
.id
, strerror(-r
));
561 if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
562 mount_enter_mounted(m
, false);
564 mount_enter_dead(m
, false);
567 static void mount_enter_unmounting(Mount
*m
, bool success
) {
576 m
->control_command
= c
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
578 if ((r
= exec_command_set(
585 service_unwatch_control_pid(m
);
587 if ((r
= mount_spawn(m
, c
, &m
->control_pid
)) < 0)
590 mount_set_state(m
, MOUNT_UNMOUNTING
);
595 log_warning("%s failed to run umount exectuable: %s", UNIT(m
)->meta
.id
, strerror(-r
));
596 mount_enter_mounted(m
, false);
599 static void mount_enter_mounting(Mount
*m
) {
605 m
->control_command
= c
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
607 if (m
->from_fragment
)
608 r
= exec_command_set(
611 m
->parameters_fragment
.what
,
613 "-t", m
->parameters_fragment
.fstype
,
614 m
->parameters_fragment
.options
? "-o" : NULL
, m
->parameters_fragment
.options
,
616 else if (m
->from_etc_fstab
)
617 r
= exec_command_set(
628 service_unwatch_control_pid(m
);
630 if ((r
= mount_spawn(m
, c
, &m
->control_pid
)) < 0)
633 mount_set_state(m
, MOUNT_MOUNTING
);
638 log_warning("%s failed to run mount exectuable: %s", UNIT(m
)->meta
.id
, strerror(-r
));
639 mount_enter_dead(m
, false);
642 static void mount_enter_mounting_done(Mount
*m
) {
645 mount_set_state(m
, MOUNT_MOUNTING_DONE
);
648 static void mount_enter_remounting(Mount
*m
, bool success
) {
657 m
->control_command
= c
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
659 if (m
->from_fragment
) {
663 if (m
->parameters_fragment
.options
) {
664 if (!(buf
= strappend("remount,", m
->parameters_fragment
.options
))) {
673 r
= exec_command_set(
676 m
->parameters_fragment
.what
,
678 "-t", m
->parameters_fragment
.fstype
,
683 } else if (m
->from_etc_fstab
)
684 r
= exec_command_set(
698 service_unwatch_control_pid(m
);
700 if ((r
= mount_spawn(m
, c
, &m
->control_pid
)) < 0)
703 mount_set_state(m
, MOUNT_REMOUNTING
);
708 mount_enter_mounted(m
, false);
711 static int mount_start(Unit
*u
) {
716 /* We cannot fulfill this request right now, try again later
718 if (m
->state
== MOUNT_UNMOUNTING
||
719 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
720 m
->state
== MOUNT_UNMOUNTING_SIGKILL
)
724 if (m
->state
== MOUNT_MOUNTING
||
725 m
->state
== MOUNT_MOUNTING_SIGTERM
||
726 m
->state
== MOUNT_MOUNTING_SIGKILL
)
729 assert(m
->state
== MOUNT_DEAD
|| m
->state
== MOUNT_MAINTAINANCE
);
733 mount_enter_mounting(m
);
737 static int mount_stop(Unit
*u
) {
742 /* Cann't do this right now. */
743 if (m
->state
== MOUNT_MOUNTING
||
744 m
->state
== MOUNT_MOUNTING_DONE
||
745 m
->state
== MOUNT_MOUNTING_SIGTERM
||
746 m
->state
== MOUNT_MOUNTING_SIGKILL
||
747 m
->state
== MOUNT_REMOUNTING
||
748 m
->state
== MOUNT_REMOUNTING_SIGTERM
||
749 m
->state
== MOUNT_REMOUNTING_SIGKILL
)
753 if (m
->state
== MOUNT_UNMOUNTING
||
754 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
755 m
->state
== MOUNT_UNMOUNTING_SIGTERM
)
758 assert(m
->state
== MOUNT_MOUNTED
);
760 mount_enter_unmounting(m
, true);
764 static int mount_reload(Unit
*u
) {
769 if (m
->state
== MOUNT_MOUNTING_DONE
)
772 assert(m
->state
== MOUNT_MOUNTED
);
774 mount_enter_remounting(m
, true);
778 static UnitActiveState
mount_active_state(Unit
*u
) {
781 return state_translation_table
[MOUNT(u
)->state
];
784 static const char *mount_sub_state_to_string(Unit
*u
) {
787 return state_string_table
[MOUNT(u
)->state
];
790 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
797 success
= code
== CLD_EXITED
&& status
== 0;
798 m
->failure
= m
->failure
|| !success
;
800 assert(m
->control_pid
== pid
);
801 assert(m
->control_command
);
803 exec_status_fill(&m
->control_command
->exec_status
, pid
, code
, status
);
806 log_debug("%s control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
808 /* Note that mount(8) returning and the kernel sending us a
809 * mount table change event might happen out-of-order. If an
810 * operation succeed we assume the kernel will follow soon too
811 * and already change into the resulting state. If it fails
812 * we check if the kernel still knows about the mount. and
813 * change state accordingly. */
818 case MOUNT_MOUNTING_DONE
:
819 case MOUNT_MOUNTING_SIGKILL
:
820 case MOUNT_MOUNTING_SIGTERM
:
821 case MOUNT_REMOUNTING
:
822 case MOUNT_REMOUNTING_SIGKILL
:
823 case MOUNT_REMOUNTING_SIGTERM
:
825 if (success
&& m
->from_proc_self_mountinfo
)
826 mount_enter_mounted(m
, true);
827 else if (m
->from_proc_self_mountinfo
)
828 mount_enter_mounted(m
, false);
830 mount_enter_dead(m
, false);
833 case MOUNT_UNMOUNTING
:
834 case MOUNT_UNMOUNTING_SIGKILL
:
835 case MOUNT_UNMOUNTING_SIGTERM
:
838 mount_enter_dead(m
, true);
839 else if (m
->from_proc_self_mountinfo
)
840 mount_enter_mounted(m
, false);
842 mount_enter_dead(m
, false);
846 assert_not_reached("Uh, control process died at wrong time.");
850 static void mount_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
854 assert(elapsed
== 1);
855 assert(w
== &m
->timer_watch
);
860 case MOUNT_MOUNTING_DONE
:
861 log_warning("%s mounting timed out. Stopping.", u
->meta
.id
);
862 mount_enter_signal(m
, MOUNT_MOUNTING_SIGTERM
, false);
865 case MOUNT_REMOUNTING
:
866 log_warning("%s remounting timed out. Stopping.", u
->meta
.id
);
867 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, false);
870 case MOUNT_UNMOUNTING
:
871 log_warning("%s unmounting timed out. Stopping.", u
->meta
.id
);
872 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, false);
875 case MOUNT_MOUNTING_SIGTERM
:
876 log_warning("%s mounting timed out. Killing.", u
->meta
.id
);
877 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, false);
880 case MOUNT_REMOUNTING_SIGTERM
:
881 log_warning("%s remounting timed out. Killing.", u
->meta
.id
);
882 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, false);
885 case MOUNT_UNMOUNTING_SIGTERM
:
886 log_warning("%s unmounting timed out. Killing.", u
->meta
.id
);
887 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, false);
890 case MOUNT_MOUNTING_SIGKILL
:
891 case MOUNT_REMOUNTING_SIGKILL
:
892 case MOUNT_UNMOUNTING_SIGKILL
:
893 log_warning("%s mount process still around after SIGKILL. Ignoring.", u
->meta
.id
);
895 if (m
->from_proc_self_mountinfo
)
896 mount_enter_mounted(m
, false);
898 mount_enter_dead(m
, false);
902 assert_not_reached("Timeout at wrong time.");
906 static int mount_add_one(
912 bool from_proc_self_mountinfo
,
917 char *e
, *w
= NULL
, *o
= NULL
, *f
= NULL
;
926 assert(!set_flags
|| from_proc_self_mountinfo
);
928 /* Ignore API mount points. They should never be referenced in
929 * dependencies ever. */
930 if (mount_point_is_api(where
))
933 if (streq(fstype
, "autofs"))
936 /* probably some kind of swap, which we don't cover for now */
940 e
= mount_name_from_where(where
);
945 if (!(u
= manager_get_unit(m
, e
))) {
948 if (!(u
= unit_new(m
))) {
953 r
= unit_add_name(u
, e
);
959 if (!(MOUNT(u
)->where
= strdup(where
))) {
964 if ((r
= unit_set_description(u
, where
)) < 0)
967 unit_add_to_load_queue(u
);
973 if (!(w
= strdup(what
)) ||
974 !(o
= strdup(options
)) ||
975 !(f
= strdup(fstype
))) {
980 if (from_proc_self_mountinfo
) {
981 mp
= &MOUNT(u
)->parameters_proc_self_mountinfo
;
984 MOUNT(u
)->is_mounted
= true;
985 MOUNT(u
)->just_mounted
= !MOUNT(u
)->from_proc_self_mountinfo
;
986 MOUNT(u
)->just_changed
= !streq_ptr(MOUNT(u
)->parameters_proc_self_mountinfo
.options
, o
);
989 MOUNT(u
)->from_proc_self_mountinfo
= true;
992 mp
= &MOUNT(u
)->parameters_etc_fstab
;
994 MOUNT(u
)->from_etc_fstab
= true;
1006 unit_add_to_dbus_queue(u
);
1021 static char *fstab_node_to_udev_node(char *p
) {
1025 /* FIXME: to follow udev's logic 100% we need to leave valid
1026 * UTF8 chars unescaped */
1028 if (startswith(p
, "LABEL=")) {
1030 if (!(t
= xescape(p
+6, "/ ")))
1033 r
= asprintf(&dn
, "/dev/disk/by-label/%s", t
);
1042 if (startswith(p
, "UUID=")) {
1044 if (!(t
= xescape(p
+5, "/ ")))
1047 r
= asprintf(&dn
, "/dev/disk/by-uuid/%s", ascii_strlower(t
));
1059 static int mount_load_etc_fstab(Manager
*m
) {
1067 if (!(f
= setmntent("/etc/fstab", "r")))
1070 while ((me
= getmntent(f
))) {
1073 if (!(what
= fstab_node_to_udev_node(me
->mnt_fsname
))) {
1078 if (!(where
= strdup(me
->mnt_dir
))) {
1085 path_kill_slashes(what
);
1087 if (where
[0] == '/')
1088 path_kill_slashes(where
);
1090 r
= mount_add_one(m
, what
, where
, me
->mnt_opts
, me
->mnt_type
, false, false);
1105 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1107 char *device
, *path
, *options
, *fstype
, *d
, *p
;
1111 rewind(m
->proc_self_mountinfo
);
1116 device
= path
= options
= fstype
= d
= p
= NULL
;
1118 if ((k
= fscanf(m
->proc_self_mountinfo
,
1119 "%*s " /* (1) mount id */
1120 "%*s " /* (2) parent id */
1121 "%*s " /* (3) major:minor */
1122 "%*s " /* (4) root */
1123 "%ms " /* (5) mount point */
1124 "%ms" /* (6) mount options */
1125 "%*[^-]" /* (7) optional fields */
1126 "- " /* (8) seperator */
1127 "%ms " /* (9) file system type */
1128 "%ms" /* (10) mount source */
1129 "%*[^\n]", /* some rubbish at the end */
1142 if (!(d
= cunescape(device
)) ||
1143 !(p
= cunescape(path
))) {
1148 if ((r
= mount_add_one(m
, d
, p
, options
, fstype
, true, set_flags
)) < 0)
1172 static void mount_shutdown(Manager
*m
) {
1175 if (m
->proc_self_mountinfo
)
1176 fclose(m
->proc_self_mountinfo
);
1179 static int mount_enumerate(Manager
*m
) {
1181 struct epoll_event ev
;
1184 if (!(m
->proc_self_mountinfo
= fopen("/proc/self/mountinfo", "r")))
1187 m
->mount_watch
.type
= WATCH_MOUNT
;
1188 m
->mount_watch
.fd
= fileno(m
->proc_self_mountinfo
);
1191 ev
.events
= EPOLLERR
;
1192 ev
.data
.ptr
= &m
->mount_watch
;
1194 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->mount_watch
.fd
, &ev
) < 0)
1197 if ((r
= mount_load_etc_fstab(m
)) < 0)
1200 if ((r
= mount_load_proc_self_mountinfo(m
, false)) < 0)
1210 void mount_fd_event(Manager
*m
, int events
) {
1215 assert(events
== EPOLLERR
);
1217 /* The manager calls this for every fd event happening on the
1218 * /proc/self/mountinfo file, which informs us about mounting
1221 if ((r
= mount_load_proc_self_mountinfo(m
, true)) < 0) {
1222 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-errno
));
1224 /* Reset flags, just in case, for later calls */
1225 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1226 Mount
*mount
= (Mount
*) meta
;
1228 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1234 manager_dispatch_load_queue(m
);
1236 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1237 Mount
*mount
= (Mount
*) meta
;
1239 if (!mount
->is_mounted
) {
1240 /* This has just been unmounted. */
1242 mount
->from_proc_self_mountinfo
= false;
1244 switch (mount
->state
) {
1247 mount_enter_dead(mount
, true);
1251 mount_set_state(mount
, mount
->state
);
1256 } else if (mount
->just_mounted
|| mount
->just_changed
) {
1258 /* New or changed entrymount */
1260 switch (mount
->state
) {
1263 case MOUNT_MAINTAINANCE
:
1264 mount_enter_mounted(mount
, true);
1267 case MOUNT_MOUNTING
:
1268 mount_enter_mounting_done(mount
);
1272 /* Nothing really changed, but let's
1273 * issue an notification call
1274 * nonetheless, in case somebody is
1275 * waiting for this. (e.g. file system
1276 * ro/rw remounts.) */
1277 mount_set_state(mount
, mount
->state
);
1282 /* Reset the flags for later calls */
1283 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1287 int mount_path_is_mounted(Manager
*m
, const char* path
) {
1297 if (!(t
= strdup(path
)))
1300 path_kill_slashes(t
);
1306 if (!(e
= mount_name_from_where(t
))) {
1311 u
= manager_get_unit(m
, e
);
1315 (MOUNT(u
)->from_etc_fstab
|| MOUNT(u
)->from_fragment
) &&
1316 MOUNT(u
)->state
!= MOUNT_MOUNTED
) {
1321 assert_se(slash
= strrchr(t
, '/'));
1338 const UnitVTable mount_vtable
= {
1342 .no_instances
= true,
1348 .coldplug
= mount_coldplug
,
1352 .start
= mount_start
,
1354 .reload
= mount_reload
,
1356 .active_state
= mount_active_state
,
1357 .sub_state_to_string
= mount_sub_state_to_string
,
1359 .sigchld_event
= mount_sigchld_event
,
1360 .timer_event
= mount_timer_event
,
1362 .bus_message_handler
= bus_mount_message_handler
,
1364 .enumerate
= mount_enumerate
,
1365 .shutdown
= mount_shutdown