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"
36 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
37 [MOUNT_DEAD
] = UNIT_INACTIVE
,
38 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
39 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVE
,
40 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
41 [MOUNT_REMOUNTING
] = UNIT_ACTIVE_RELOADING
,
42 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
43 [MOUNT_MOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
44 [MOUNT_MOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
45 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_ACTIVE_RELOADING
,
46 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_ACTIVE_RELOADING
,
47 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
48 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
49 [MOUNT_MAINTAINANCE
] = UNIT_INACTIVE
,
52 static const char* const state_string_table
[_MOUNT_STATE_MAX
] = {
53 [MOUNT_DEAD
] = "dead",
54 [MOUNT_MOUNTING
] = "mounting",
55 [MOUNT_MOUNTING_DONE
] = "mounting-done",
56 [MOUNT_MOUNTED
] = "mounted",
57 [MOUNT_REMOUNTING
] = "remounting",
58 [MOUNT_UNMOUNTING
] = "unmounting",
59 [MOUNT_MOUNTING_SIGTERM
] = "mounting-sigterm",
60 [MOUNT_MOUNTING_SIGKILL
] = "mounting-sigkill",
61 [MOUNT_REMOUNTING_SIGTERM
] = "remounting-sigterm",
62 [MOUNT_REMOUNTING_SIGKILL
] = "remounting-sigkill",
63 [MOUNT_UNMOUNTING_SIGTERM
] = "unmounting-sigterm",
64 [MOUNT_UNMOUNTING_SIGKILL
] = "unmounting-sigkill",
65 [MOUNT_MAINTAINANCE
] = "maintainance"
68 static void service_unwatch_control_pid(Mount
*m
) {
71 if (m
->control_pid
<= 0)
74 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
78 static void mount_parameters_done(MountParameters
*p
) {
85 p
->what
= p
->options
= p
->fstype
= NULL
;
88 static void mount_done(Unit
*u
) {
96 mount_parameters_done(&m
->parameters_etc_fstab
);
97 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
98 mount_parameters_done(&m
->parameters_fragment
);
100 exec_context_done(&m
->exec_context
);
101 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
102 m
->control_command
= NULL
;
104 service_unwatch_control_pid(m
);
106 unit_unwatch_timer(u
, &m
->timer_watch
);
109 static void mount_init(Unit
*u
) {
113 assert(u
->meta
.load_state
== UNIT_STUB
);
116 m
->from_etc_fstab
= false;
117 m
->from_proc_self_mountinfo
= false;
118 m
->from_fragment
= false;
120 m
->is_mounted
= false;
121 m
->just_mounted
= false;
122 m
->just_changed
= false;
124 m
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
126 zero(m
->exec_command
);
127 exec_context_init(&m
->exec_context
);
134 m
->timer_watch
.type
= WATCH_INVALID
;
137 static int mount_add_node_links(Mount
*m
) {
145 /* Adds in links to the device that this node is based on */
147 if (m
->parameters_fragment
.what
)
148 what
= m
->parameters_fragment
.what
;
149 else if (m
->parameters_etc_fstab
.what
)
150 what
= m
->parameters_etc_fstab
.what
;
152 /* We observe kernel mounts only while they are live,
153 * hence don't create any links for them */
156 if (!path_startswith(what
, "/dev/"))
159 if (!(e
= unit_name_escape_path(what
+1, ".device")))
162 r
= manager_load_unit(UNIT(m
)->meta
.manager
, e
, &device
);
168 if ((r
= unit_add_dependency(UNIT(m
), UNIT_AFTER
, device
)) < 0)
171 if ((r
= unit_add_dependency(UNIT(m
), UNIT_REQUIRES
, device
)) < 0)
174 if (UNIT(m
)->meta
.manager
->running_as
== MANAGER_INIT
||
175 UNIT(m
)->meta
.manager
->running_as
== MANAGER_SYSTEM
)
176 if ((r
= unit_add_dependency(device
, UNIT_WANTS
, UNIT(m
))) < 0)
182 static int mount_add_path_links(Mount
*m
) {
188 /* Adds in link to other mount points, that might lie below or
189 * above us in the hierarchy */
191 LIST_FOREACH(units_per_type
, other
, UNIT(m
)->meta
.manager
->units_per_type
[UNIT_MOUNT
]) {
199 if (m
->meta
.load_state
!= UNIT_LOADED
)
202 if (path_startswith(m
->where
, n
->where
)) {
204 if ((r
= unit_add_dependency(UNIT(m
), UNIT_AFTER
, UNIT(other
))) < 0)
207 if (n
->from_etc_fstab
|| n
->from_fragment
)
208 if ((r
= unit_add_dependency(UNIT(m
), UNIT_REQUIRES
, UNIT(other
))) < 0)
211 } else if (path_startswith(n
->where
, m
->where
)) {
213 if ((r
= unit_add_dependency(UNIT(m
), UNIT_BEFORE
, UNIT(other
))) < 0)
216 if (m
->from_etc_fstab
|| m
->from_fragment
)
217 if ((r
= unit_add_dependency(UNIT(other
), UNIT_REQUIRES
, UNIT(m
))) < 0)
225 static bool mount_test_option(const char *haystack
, const char *needle
) {
230 /* Like glibc's hasmntopt(), but works on a string, not a
237 me
.mnt_opts
= (char*) haystack
;
239 return !!hasmntopt(&me
, needle
);
242 static int mount_add_target_links(Mount
*m
) {
250 if (m
->from_fragment
)
251 p
= &m
->parameters_fragment
;
252 else if (m
->from_etc_fstab
)
253 p
= &m
->parameters_etc_fstab
;
260 if (mount_test_option(p
->options
, MNTOPT_NOAUTO
))
263 if (mount_test_option(p
->options
, "_netdev") ||
264 fstype_is_network(p
->fstype
))
265 target
= SPECIAL_REMOTE_FS_TARGET
;
267 target
= SPECIAL_LOCAL_FS_TARGET
;
269 if ((r
= manager_load_unit(UNIT(m
)->meta
.manager
, target
, &u
)) < 0)
272 if ((r
= unit_add_dependency(u
, UNIT_WANTS
, UNIT(m
))) < 0)
275 return unit_add_dependency(UNIT(m
), UNIT_BEFORE
, u
);
278 static int mount_load(Unit
*u
) {
283 assert(u
->meta
.load_state
== UNIT_STUB
);
285 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
288 /* This is a new unit? Then let's add in some extras */
289 if (u
->meta
.load_state
== UNIT_LOADED
) {
291 /* Minor validity checking */
292 if ((m
->parameters_fragment
.options
|| m
->parameters_fragment
.fstype
) && !m
->parameters_fragment
.what
)
295 if (m
->parameters_fragment
.what
)
296 m
->from_fragment
= true;
298 if ((r
= mount_add_node_links(MOUNT(u
))) < 0)
301 if ((r
= mount_add_path_links(MOUNT(u
))) < 0)
304 if ((r
= mount_add_target_links(MOUNT(u
))) < 0)
307 if ((r
= unit_add_default_cgroup(u
)) < 0)
314 static void mount_set_state(Mount
*m
, MountState state
) {
315 MountState old_state
;
318 old_state
= m
->state
;
321 if (state
!= MOUNT_MOUNTING
&&
322 state
!= MOUNT_MOUNTING_DONE
&&
323 state
!= MOUNT_REMOUNTING
&&
324 state
!= MOUNT_UNMOUNTING
&&
325 state
!= MOUNT_MOUNTING_SIGTERM
&&
326 state
!= MOUNT_MOUNTING_SIGKILL
&&
327 state
!= MOUNT_UNMOUNTING_SIGTERM
&&
328 state
!= MOUNT_UNMOUNTING_SIGKILL
&&
329 state
!= MOUNT_REMOUNTING_SIGTERM
&&
330 state
!= MOUNT_REMOUNTING_SIGKILL
) {
331 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
332 service_unwatch_control_pid(m
);
333 m
->control_command
= NULL
;
336 if (state
!= old_state
)
337 log_debug("%s changed %s → %s", unit_id(UNIT(m
)), state_string_table
[old_state
], state_string_table
[state
]);
339 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
]);
342 static int mount_coldplug(Unit
*u
) {
346 assert(m
->state
== MOUNT_DEAD
);
348 if (m
->from_proc_self_mountinfo
)
349 mount_set_state(m
, MOUNT_MOUNTED
);
354 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
362 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
365 if ((r
= exec_spawn(c
,
370 UNIT(m
)->meta
.manager
->confirm_spawn
,
371 UNIT(m
)->meta
.cgroup_bondings
,
375 if ((r
= unit_watch_pid(UNIT(m
), pid
)) < 0)
376 /* FIXME: we need to do something here */
384 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
389 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
396 if (m
->from_proc_self_mountinfo
)
397 p
= &m
->parameters_proc_self_mountinfo
;
398 else if (m
->from_fragment
)
399 p
= &m
->parameters_fragment
;
401 p
= &m
->parameters_etc_fstab
;
404 "%sMount State: %s\n"
407 "%sFile System Type: %s\n"
409 "%sFrom /etc/fstab: %s\n"
410 "%sFrom /proc/self/mountinfo: %s\n"
411 "%sFrom fragment: %s\n"
413 prefix
, state_string_table
[m
->state
],
415 prefix
, strna(p
->what
),
416 prefix
, strna(p
->fstype
),
417 prefix
, strna(p
->options
),
418 prefix
, yes_no(m
->from_etc_fstab
),
419 prefix
, yes_no(m
->from_proc_self_mountinfo
),
420 prefix
, yes_no(m
->from_fragment
),
421 prefix
, kill_mode_to_string(m
->kill_mode
));
423 if (m
->control_pid
> 0)
425 "%sControl PID: %llu\n",
426 prefix
, (unsigned long long) m
->control_pid
);
428 exec_context_dump(&m
->exec_context
, f
, prefix
);
431 static void mount_enter_dead(Mount
*m
, bool success
) {
437 mount_set_state(m
, m
->failure
? MOUNT_MAINTAINANCE
: MOUNT_DEAD
);
440 static void mount_enter_mounted(Mount
*m
, bool success
) {
446 mount_set_state(m
, MOUNT_MOUNTED
);
449 static void mount_enter_signal(Mount
*m
, MountState state
, bool success
) {
458 if (m
->kill_mode
!= KILL_NONE
) {
459 int sig
= (state
== MOUNT_MOUNTING_SIGTERM
||
460 state
== MOUNT_UNMOUNTING_SIGTERM
||
461 state
== MOUNT_REMOUNTING_SIGTERM
) ? SIGTERM
: SIGKILL
;
463 if (m
->kill_mode
== KILL_CONTROL_GROUP
) {
465 if ((r
= cgroup_bonding_kill_list(UNIT(m
)->meta
.cgroup_bondings
, sig
)) < 0) {
466 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
472 if (!sent
&& m
->control_pid
> 0)
473 if (kill(m
->kill_mode
== KILL_PROCESS
? m
->control_pid
: -m
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
480 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
483 mount_set_state(m
, state
);
484 } else if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
485 mount_enter_mounted(m
, true);
487 mount_enter_dead(m
, true);
492 log_warning("%s failed to kill processes: %s", unit_id(UNIT(m
)), strerror(-r
));
494 if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
495 mount_enter_mounted(m
, false);
497 mount_enter_dead(m
, false);
500 static void mount_enter_unmounting(Mount
*m
, bool success
) {
509 m
->control_command
= c
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
511 if ((r
= exec_command_set(
518 service_unwatch_control_pid(m
);
520 if ((r
= mount_spawn(m
, c
, &m
->control_pid
)) < 0)
523 mount_set_state(m
, MOUNT_UNMOUNTING
);
528 log_warning("%s failed to run umount exectuable: %s", unit_id(UNIT(m
)), strerror(-r
));
529 mount_enter_mounted(m
, false);
532 static void mount_enter_mounting(Mount
*m
, bool success
) {
541 m
->control_command
= c
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
543 if (m
->from_fragment
)
544 r
= exec_command_set(
547 m
->parameters_fragment
.what
,
549 "-t", m
->parameters_fragment
.fstype
,
550 "-o", m
->parameters_fragment
.options
,
552 else if (m
->from_etc_fstab
)
553 r
= exec_command_set(
564 service_unwatch_control_pid(m
);
566 if ((r
= mount_spawn(m
, c
, &m
->control_pid
)) < 0)
569 mount_set_state(m
, MOUNT_MOUNTING
);
574 log_warning("%s failed to run mount exectuable: %s", unit_id(UNIT(m
)), strerror(-r
));
575 mount_enter_dead(m
, false);
578 static void mount_enter_mounting_done(Mount
*m
, bool success
) {
584 mount_set_state(m
, MOUNT_MOUNTING_DONE
);
587 static void mount_enter_remounting(Mount
*m
, bool success
) {
596 m
->control_command
= c
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
598 if (m
->from_fragment
) {
602 if (m
->parameters_fragment
.options
) {
603 if (!(buf
= strappend("remount,", m
->parameters_fragment
.options
))) {
612 r
= exec_command_set(
615 m
->parameters_fragment
.what
,
617 "-t", m
->parameters_fragment
.fstype
,
622 } else if (m
->from_etc_fstab
)
623 r
= exec_command_set(
637 service_unwatch_control_pid(m
);
639 if ((r
= mount_spawn(m
, c
, &m
->control_pid
)) < 0)
642 mount_set_state(m
, MOUNT_REMOUNTING
);
647 mount_enter_mounted(m
, false);
650 static int mount_start(Unit
*u
) {
655 /* We cannot fulfill this request right now, try again later
657 if (m
->state
== MOUNT_UNMOUNTING
||
658 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
659 m
->state
== MOUNT_UNMOUNTING_SIGKILL
)
663 if (m
->state
== MOUNT_MOUNTING
||
664 m
->state
== MOUNT_MOUNTING_SIGTERM
||
665 m
->state
== MOUNT_MOUNTING_SIGKILL
)
668 assert(m
->state
== MOUNT_DEAD
|| m
->state
== MOUNT_MAINTAINANCE
);
672 mount_enter_mounting(m
, true);
676 static int mount_stop(Unit
*u
) {
681 /* Cann't do this right now. */
682 if (m
->state
== MOUNT_MOUNTING
||
683 m
->state
== MOUNT_MOUNTING_DONE
||
684 m
->state
== MOUNT_MOUNTING_SIGTERM
||
685 m
->state
== MOUNT_MOUNTING_SIGKILL
||
686 m
->state
== MOUNT_REMOUNTING
||
687 m
->state
== MOUNT_REMOUNTING_SIGTERM
||
688 m
->state
== MOUNT_REMOUNTING_SIGKILL
)
692 if (m
->state
== MOUNT_UNMOUNTING
||
693 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
694 m
->state
== MOUNT_UNMOUNTING_SIGTERM
)
697 assert(m
->state
== MOUNT_MOUNTED
);
699 mount_enter_unmounting(m
, true);
703 static int mount_reload(Unit
*u
) {
708 if (m
->state
== MOUNT_MOUNTING_DONE
)
711 assert(m
->state
== MOUNT_MOUNTED
);
713 mount_enter_remounting(m
, true);
717 static UnitActiveState
mount_active_state(Unit
*u
) {
720 return state_translation_table
[MOUNT(u
)->state
];
723 static const char *mount_sub_state_to_string(Unit
*u
) {
726 return state_string_table
[MOUNT(u
)->state
];
729 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
736 success
= code
== CLD_EXITED
&& status
== 0;
737 m
->failure
= m
->failure
|| !success
;
739 assert(m
->control_pid
== pid
);
740 assert(m
->control_command
);
742 exec_status_fill(&m
->control_command
->exec_status
, pid
, code
, status
);
745 log_debug("%s control process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
747 /* Note that mount(8) returning and the kernel sending us a
748 * mount table change event might happen out-of-order. If an
749 * operation succeed we assume the kernel will follow soon too
750 * and already change into the resulting state. If it fails
751 * we check if the kernel still knows about the mount. and
752 * change state accordingly. */
757 case MOUNT_MOUNTING_DONE
:
758 case MOUNT_MOUNTING_SIGKILL
:
759 case MOUNT_MOUNTING_SIGTERM
:
760 case MOUNT_REMOUNTING
:
761 case MOUNT_REMOUNTING_SIGKILL
:
762 case MOUNT_REMOUNTING_SIGTERM
:
764 if (success
&& m
->from_proc_self_mountinfo
)
765 mount_enter_mounted(m
, true);
766 else if (m
->from_proc_self_mountinfo
)
767 mount_enter_mounted(m
, false);
769 mount_enter_dead(m
, false);
772 case MOUNT_UNMOUNTING
:
773 case MOUNT_UNMOUNTING_SIGKILL
:
774 case MOUNT_UNMOUNTING_SIGTERM
:
777 mount_enter_dead(m
, true);
778 else if (m
->from_proc_self_mountinfo
)
779 mount_enter_mounted(m
, false);
781 mount_enter_dead(m
, false);
785 assert_not_reached("Uh, control process died at wrong time.");
789 static void mount_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
793 assert(elapsed
== 1);
794 assert(w
== &m
->timer_watch
);
799 case MOUNT_MOUNTING_DONE
:
800 log_warning("%s mounting timed out. Stopping.", unit_id(u
));
801 mount_enter_signal(m
, MOUNT_MOUNTING_SIGTERM
, false);
804 case MOUNT_REMOUNTING
:
805 log_warning("%s remounting timed out. Stopping.", unit_id(u
));
806 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, false);
809 case MOUNT_UNMOUNTING
:
810 log_warning("%s unmounting timed out. Stopping.", unit_id(u
));
811 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, false);
814 case MOUNT_MOUNTING_SIGTERM
:
815 log_warning("%s mounting timed out. Killing.", unit_id(u
));
816 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, false);
819 case MOUNT_REMOUNTING_SIGTERM
:
820 log_warning("%s remounting timed out. Killing.", unit_id(u
));
821 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, false);
824 case MOUNT_UNMOUNTING_SIGTERM
:
825 log_warning("%s unmounting timed out. Killing.", unit_id(u
));
826 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, false);
829 case MOUNT_MOUNTING_SIGKILL
:
830 case MOUNT_REMOUNTING_SIGKILL
:
831 case MOUNT_UNMOUNTING_SIGKILL
:
832 log_warning("%s mount process still around after SIGKILL. Ignoring.", unit_id(u
));
834 if (m
->from_proc_self_mountinfo
)
835 mount_enter_mounted(m
, false);
837 mount_enter_dead(m
, false);
841 assert_not_reached("Timeout at wrong time.");
845 static int mount_add_one(
851 bool from_proc_self_mountinfo
,
856 char *e
, *w
= NULL
, *o
= NULL
, *f
= NULL
;
865 assert(!set_flags
|| from_proc_self_mountinfo
);
867 /* Ignore API mount points. They should never be referenced in
868 * dependencies ever. */
869 if (mount_point_is_api(where
))
872 /* probably some kind of swap, which we don't cover for now */
876 if (streq(where
, "/"))
877 e
= strdup("-.mount");
879 e
= unit_name_escape_path(where
+1, ".mount");
884 if (!(u
= manager_get_unit(m
, e
))) {
887 if (!(u
= unit_new(m
))) {
892 r
= unit_add_name(u
, e
);
898 if (!(MOUNT(u
)->where
= strdup(where
))) {
903 if ((r
= unit_set_description(u
, where
)) < 0)
906 unit_add_to_load_queue(u
);
912 if (!(w
= strdup(what
)) ||
913 !(o
= strdup(options
)) ||
914 !(f
= strdup(fstype
))) {
919 if (from_proc_self_mountinfo
) {
920 mp
= &MOUNT(u
)->parameters_proc_self_mountinfo
;
923 MOUNT(u
)->is_mounted
= true;
924 MOUNT(u
)->just_mounted
= !MOUNT(u
)->from_proc_self_mountinfo
;
925 MOUNT(u
)->just_changed
= !streq_ptr(MOUNT(u
)->parameters_proc_self_mountinfo
.options
, o
);
928 MOUNT(u
)->from_proc_self_mountinfo
= true;
931 mp
= &MOUNT(u
)->parameters_etc_fstab
;
933 MOUNT(u
)->from_etc_fstab
= true;
945 unit_add_to_dbus_queue(u
);
960 static char *fstab_node_to_udev_node(char *p
) {
964 /* FIXME: to follow udev's logic 100% we need to leave valid
965 * UTF8 chars unescaped */
967 if (startswith(p
, "LABEL=")) {
969 if (!(t
= xescape(p
+6, "/ ")))
972 r
= asprintf(&dn
, "/dev/disk/by-label/%s", t
);
981 if (startswith(p
, "UUID=")) {
983 if (!(t
= xescape(p
+5, "/ ")))
986 r
= asprintf(&dn
, "/dev/disk/by-uuid/%s", ascii_strlower(t
));
998 static int mount_load_etc_fstab(Manager
*m
) {
1006 if (!(f
= setmntent("/etc/fstab", "r")))
1009 while ((me
= getmntent(f
))) {
1012 if (!(what
= fstab_node_to_udev_node(me
->mnt_fsname
))) {
1017 if (!(where
= strdup(me
->mnt_dir
))) {
1024 path_kill_slashes(what
);
1026 if (where
[0] == '/')
1027 path_kill_slashes(where
);
1029 r
= mount_add_one(m
, what
, where
, me
->mnt_opts
, me
->mnt_type
, false, false);
1044 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1046 char *device
, *path
, *options
, *fstype
, *d
, *p
;
1050 rewind(m
->proc_self_mountinfo
);
1055 device
= path
= options
= fstype
= d
= p
= NULL
;
1057 if ((k
= fscanf(m
->proc_self_mountinfo
,
1058 "%*s " /* (1) mount id */
1059 "%*s " /* (2) parent id */
1060 "%*s " /* (3) major:minor */
1061 "%*s " /* (4) root */
1062 "%ms " /* (5) mount point */
1063 "%ms" /* (6) mount options */
1064 "%*[^-]" /* (7) optional fields */
1065 "- " /* (8) seperator */
1066 "%ms " /* (9) file system type */
1067 "%ms" /* (10) mount source */
1068 "%*[^\n]", /* some rubbish at the end */
1081 if (!(d
= cunescape(device
)) ||
1082 !(p
= cunescape(path
))) {
1087 if ((r
= mount_add_one(m
, d
, p
, options
, fstype
, true, set_flags
)) < 0)
1111 static void mount_shutdown(Manager
*m
) {
1114 if (m
->proc_self_mountinfo
)
1115 fclose(m
->proc_self_mountinfo
);
1118 static int mount_enumerate(Manager
*m
) {
1120 struct epoll_event ev
;
1123 if (!(m
->proc_self_mountinfo
= fopen("/proc/self/mountinfo", "r")))
1126 m
->mount_watch
.type
= WATCH_MOUNT
;
1127 m
->mount_watch
.fd
= fileno(m
->proc_self_mountinfo
);
1130 ev
.events
= EPOLLERR
;
1131 ev
.data
.ptr
= &m
->mount_watch
;
1133 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->mount_watch
.fd
, &ev
) < 0)
1136 if ((r
= mount_load_etc_fstab(m
)) < 0)
1139 if ((r
= mount_load_proc_self_mountinfo(m
, false)) < 0)
1149 void mount_fd_event(Manager
*m
, int events
) {
1154 assert(events
== EPOLLERR
);
1156 /* The manager calls this for every fd event happening on the
1157 * /proc/self/mountinfo file, which informs us about mounting
1160 if ((r
= mount_load_proc_self_mountinfo(m
, true)) < 0) {
1161 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-errno
));
1163 /* Reset flags, just in case, for later calls */
1164 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1165 Mount
*mount
= (Mount
*) meta
;
1167 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1173 manager_dispatch_load_queue(m
);
1175 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1176 Mount
*mount
= (Mount
*) meta
;
1178 if (!mount
->is_mounted
) {
1179 /* This has just been unmounted. */
1181 mount
->from_proc_self_mountinfo
= false;
1183 switch (mount
->state
) {
1186 mount_enter_dead(mount
, true);
1190 mount_set_state(mount
, mount
->state
);
1195 } else if (mount
->just_mounted
|| mount
->just_changed
) {
1197 /* New or changed entrymount */
1199 switch (mount
->state
) {
1202 case MOUNT_MAINTAINANCE
:
1203 mount_enter_mounted(mount
, true);
1206 case MOUNT_MOUNTING
:
1207 mount_enter_mounting_done(mount
, true);
1211 /* Nothing really changed, but let's
1212 * issue an notification call
1213 * nonetheless, in case somebody is
1214 * waiting for this. (e.g. file system
1215 * ro/rw remounts.) */
1216 mount_set_state(mount
, mount
->state
);
1221 /* Reset the flags for later calls */
1222 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1226 int mount_path_is_mounted(Manager
*m
, const char* path
) {
1236 if (!(t
= strdup(path
)))
1239 path_kill_slashes(t
);
1245 if (!(e
= unit_name_escape_path(t
+1, ".mount"))) {
1250 u
= manager_get_unit(m
, e
);
1254 (MOUNT(u
)->from_etc_fstab
|| MOUNT(u
)->from_fragment
) &&
1255 MOUNT(u
)->state
!= MOUNT_MOUNTED
) {
1260 assert_se(slash
= strrchr(t
, '/'));
1277 const UnitVTable mount_vtable
= {
1286 .coldplug
= mount_coldplug
,
1290 .start
= mount_start
,
1292 .reload
= mount_reload
,
1294 .active_state
= mount_active_state
,
1295 .sub_state_to_string
= mount_sub_state_to_string
,
1297 .sigchld_event
= mount_sigchld_event
,
1298 .timer_event
= mount_timer_event
,
1300 .enumerate
= mount_enumerate
,
1301 .shutdown
= mount_shutdown