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 static const UnitActiveState state_translation_table
[_MOUNT_STATE_MAX
] = {
38 [MOUNT_DEAD
] = UNIT_INACTIVE
,
39 [MOUNT_MOUNTING
] = UNIT_ACTIVATING
,
40 [MOUNT_MOUNTING_DONE
] = UNIT_ACTIVE
,
41 [MOUNT_MOUNTED
] = UNIT_ACTIVE
,
42 [MOUNT_REMOUNTING
] = UNIT_ACTIVE_RELOADING
,
43 [MOUNT_UNMOUNTING
] = UNIT_DEACTIVATING
,
44 [MOUNT_MOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
45 [MOUNT_MOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
46 [MOUNT_REMOUNTING_SIGTERM
] = UNIT_ACTIVE_RELOADING
,
47 [MOUNT_REMOUNTING_SIGKILL
] = UNIT_ACTIVE_RELOADING
,
48 [MOUNT_UNMOUNTING_SIGTERM
] = UNIT_DEACTIVATING
,
49 [MOUNT_UNMOUNTING_SIGKILL
] = UNIT_DEACTIVATING
,
50 [MOUNT_MAINTAINANCE
] = UNIT_INACTIVE
,
53 static const char* const state_string_table
[_MOUNT_STATE_MAX
] = {
54 [MOUNT_DEAD
] = "dead",
55 [MOUNT_MOUNTING
] = "mounting",
56 [MOUNT_MOUNTING_DONE
] = "mounting-done",
57 [MOUNT_MOUNTED
] = "mounted",
58 [MOUNT_REMOUNTING
] = "remounting",
59 [MOUNT_UNMOUNTING
] = "unmounting",
60 [MOUNT_MOUNTING_SIGTERM
] = "mounting-sigterm",
61 [MOUNT_MOUNTING_SIGKILL
] = "mounting-sigkill",
62 [MOUNT_REMOUNTING_SIGTERM
] = "remounting-sigterm",
63 [MOUNT_REMOUNTING_SIGKILL
] = "remounting-sigkill",
64 [MOUNT_UNMOUNTING_SIGTERM
] = "unmounting-sigterm",
65 [MOUNT_UNMOUNTING_SIGKILL
] = "unmounting-sigkill",
66 [MOUNT_MAINTAINANCE
] = "maintainance"
69 static void service_unwatch_control_pid(Mount
*m
) {
72 if (m
->control_pid
<= 0)
75 unit_unwatch_pid(UNIT(m
), m
->control_pid
);
79 static void mount_parameters_done(MountParameters
*p
) {
86 p
->what
= p
->options
= p
->fstype
= NULL
;
89 static void mount_done(Unit
*u
) {
97 mount_parameters_done(&m
->parameters_etc_fstab
);
98 mount_parameters_done(&m
->parameters_proc_self_mountinfo
);
99 mount_parameters_done(&m
->parameters_fragment
);
101 exec_context_done(&m
->exec_context
);
102 exec_command_done_array(m
->exec_command
, _MOUNT_EXEC_COMMAND_MAX
);
103 m
->control_command
= NULL
;
105 service_unwatch_control_pid(m
);
107 unit_unwatch_timer(u
, &m
->timer_watch
);
110 static void mount_init(Unit
*u
) {
114 assert(u
->meta
.load_state
== UNIT_STUB
);
117 m
->from_etc_fstab
= false;
118 m
->from_proc_self_mountinfo
= false;
119 m
->from_fragment
= false;
121 m
->is_mounted
= false;
122 m
->just_mounted
= false;
123 m
->just_changed
= false;
125 m
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
127 zero(m
->exec_command
);
128 exec_context_init(&m
->exec_context
);
135 m
->timer_watch
.type
= WATCH_INVALID
;
138 static int mount_add_node_links(Mount
*m
) {
146 /* Adds in links to the device that this node is based on */
148 if (m
->parameters_fragment
.what
)
149 what
= m
->parameters_fragment
.what
;
150 else if (m
->parameters_etc_fstab
.what
)
151 what
= m
->parameters_etc_fstab
.what
;
153 /* We observe kernel mounts only while they are live,
154 * hence don't create any links for them */
157 if (!path_startswith(what
, "/dev/"))
160 if (!(e
= unit_name_build_escape(what
+1, NULL
, ".device")))
163 r
= manager_load_unit(UNIT(m
)->meta
.manager
, e
, NULL
, &device
);
169 if ((r
= unit_add_dependency(UNIT(m
), UNIT_AFTER
, device
)) < 0)
172 if ((r
= unit_add_dependency(UNIT(m
), UNIT_REQUIRES
, device
)) < 0)
175 if (UNIT(m
)->meta
.manager
->running_as
== MANAGER_INIT
||
176 UNIT(m
)->meta
.manager
->running_as
== MANAGER_SYSTEM
)
177 if ((r
= unit_add_dependency(device
, UNIT_WANTS
, UNIT(m
))) < 0)
183 static int mount_add_path_links(Mount
*m
) {
189 /* Adds in link to other mount points, that might lie below or
190 * above us in the hierarchy */
192 LIST_FOREACH(units_per_type
, other
, UNIT(m
)->meta
.manager
->units_per_type
[UNIT_MOUNT
]) {
200 if (m
->meta
.load_state
!= UNIT_LOADED
)
203 if (path_startswith(m
->where
, n
->where
)) {
205 if ((r
= unit_add_dependency(UNIT(m
), UNIT_AFTER
, UNIT(other
))) < 0)
208 if (n
->from_etc_fstab
|| n
->from_fragment
)
209 if ((r
= unit_add_dependency(UNIT(m
), UNIT_REQUIRES
, UNIT(other
))) < 0)
212 } else if (path_startswith(n
->where
, m
->where
)) {
214 if ((r
= unit_add_dependency(UNIT(m
), UNIT_BEFORE
, UNIT(other
))) < 0)
217 if (m
->from_etc_fstab
|| m
->from_fragment
)
218 if ((r
= unit_add_dependency(UNIT(other
), UNIT_REQUIRES
, UNIT(m
))) < 0)
226 static bool mount_test_option(const char *haystack
, const char *needle
) {
231 /* Like glibc's hasmntopt(), but works on a string, not a
238 me
.mnt_opts
= (char*) haystack
;
240 return !!hasmntopt(&me
, needle
);
243 static int mount_add_target_links(Mount
*m
) {
253 if (m
->from_fragment
)
254 p
= &m
->parameters_fragment
;
255 else if (m
->from_etc_fstab
)
256 p
= &m
->parameters_etc_fstab
;
260 noauto
= mount_test_option(p
->options
, MNTOPT_NOAUTO
);
261 handle
= mount_test_option(p
->options
, "comment=systemd.mount");
263 if (noauto
&& !handle
)
266 if (mount_test_option(p
->options
, "_netdev") ||
267 fstype_is_network(p
->fstype
))
268 target
= SPECIAL_REMOTE_FS_TARGET
;
270 target
= SPECIAL_LOCAL_FS_TARGET
;
272 if ((r
= manager_load_unit(UNIT(m
)->meta
.manager
, target
, NULL
, &u
)) < 0)
276 if ((r
= unit_add_dependency(u
, UNIT_WANTS
, UNIT(m
))) < 0)
279 return unit_add_dependency(UNIT(m
), UNIT_BEFORE
, u
);
282 static int mount_load(Unit
*u
) {
287 assert(u
->meta
.load_state
== UNIT_STUB
);
289 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
292 /* This is a new unit? Then let's add in some extras */
293 if (u
->meta
.load_state
== UNIT_LOADED
) {
295 /* Minor validity checking */
296 if ((m
->parameters_fragment
.options
|| m
->parameters_fragment
.fstype
) && !m
->parameters_fragment
.what
)
299 if (m
->parameters_fragment
.what
)
300 m
->from_fragment
= true;
302 if ((r
= mount_add_node_links(MOUNT(u
))) < 0)
305 if ((r
= mount_add_path_links(MOUNT(u
))) < 0)
308 if ((r
= mount_add_target_links(MOUNT(u
))) < 0)
311 if ((r
= unit_add_default_cgroup(u
)) < 0)
318 static void mount_set_state(Mount
*m
, MountState state
) {
319 MountState old_state
;
322 old_state
= m
->state
;
325 if (state
!= MOUNT_MOUNTING
&&
326 state
!= MOUNT_MOUNTING_DONE
&&
327 state
!= MOUNT_REMOUNTING
&&
328 state
!= MOUNT_UNMOUNTING
&&
329 state
!= MOUNT_MOUNTING_SIGTERM
&&
330 state
!= MOUNT_MOUNTING_SIGKILL
&&
331 state
!= MOUNT_UNMOUNTING_SIGTERM
&&
332 state
!= MOUNT_UNMOUNTING_SIGKILL
&&
333 state
!= MOUNT_REMOUNTING_SIGTERM
&&
334 state
!= MOUNT_REMOUNTING_SIGKILL
) {
335 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
336 service_unwatch_control_pid(m
);
337 m
->control_command
= NULL
;
340 if (state
!= old_state
)
341 log_debug("%s changed %s → %s", UNIT(m
)->meta
.id
, state_string_table
[old_state
], state_string_table
[state
]);
343 unit_notify(UNIT(m
), state_translation_table
[old_state
], state_translation_table
[state
]);
346 static int mount_coldplug(Unit
*u
) {
350 assert(m
->state
== MOUNT_DEAD
);
352 if (m
->from_proc_self_mountinfo
)
353 mount_set_state(m
, MOUNT_MOUNTED
);
358 static int mount_spawn(Mount
*m
, ExecCommand
*c
, pid_t
*_pid
) {
366 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
369 if ((r
= exec_spawn(c
,
375 UNIT(m
)->meta
.manager
->confirm_spawn
,
376 UNIT(m
)->meta
.cgroup_bondings
,
380 if ((r
= unit_watch_pid(UNIT(m
), pid
)) < 0)
381 /* FIXME: we need to do something here */
389 unit_unwatch_timer(UNIT(m
), &m
->timer_watch
);
394 static void mount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
401 if (m
->from_proc_self_mountinfo
)
402 p
= &m
->parameters_proc_self_mountinfo
;
403 else if (m
->from_fragment
)
404 p
= &m
->parameters_fragment
;
406 p
= &m
->parameters_etc_fstab
;
409 "%sMount State: %s\n"
412 "%sFile System Type: %s\n"
414 "%sFrom /etc/fstab: %s\n"
415 "%sFrom /proc/self/mountinfo: %s\n"
416 "%sFrom fragment: %s\n"
418 prefix
, state_string_table
[m
->state
],
420 prefix
, strna(p
->what
),
421 prefix
, strna(p
->fstype
),
422 prefix
, strna(p
->options
),
423 prefix
, yes_no(m
->from_etc_fstab
),
424 prefix
, yes_no(m
->from_proc_self_mountinfo
),
425 prefix
, yes_no(m
->from_fragment
),
426 prefix
, kill_mode_to_string(m
->kill_mode
));
428 if (m
->control_pid
> 0)
430 "%sControl PID: %llu\n",
431 prefix
, (unsigned long long) m
->control_pid
);
433 exec_context_dump(&m
->exec_context
, f
, prefix
);
436 static void mount_enter_dead(Mount
*m
, bool success
) {
442 mount_set_state(m
, m
->failure
? MOUNT_MAINTAINANCE
: MOUNT_DEAD
);
445 static void mount_enter_mounted(Mount
*m
, bool success
) {
451 mount_set_state(m
, MOUNT_MOUNTED
);
454 static void mount_enter_signal(Mount
*m
, MountState state
, bool success
) {
463 if (m
->kill_mode
!= KILL_NONE
) {
464 int sig
= (state
== MOUNT_MOUNTING_SIGTERM
||
465 state
== MOUNT_UNMOUNTING_SIGTERM
||
466 state
== MOUNT_REMOUNTING_SIGTERM
) ? SIGTERM
: SIGKILL
;
468 if (m
->kill_mode
== KILL_CONTROL_GROUP
) {
470 if ((r
= cgroup_bonding_kill_list(UNIT(m
)->meta
.cgroup_bondings
, sig
)) < 0) {
471 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
477 if (!sent
&& m
->control_pid
> 0)
478 if (kill(m
->kill_mode
== KILL_PROCESS
? m
->control_pid
: -m
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
485 if ((r
= unit_watch_timer(UNIT(m
), m
->timeout_usec
, &m
->timer_watch
)) < 0)
488 mount_set_state(m
, state
);
489 } else if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
490 mount_enter_mounted(m
, true);
492 mount_enter_dead(m
, true);
497 log_warning("%s failed to kill processes: %s", UNIT(m
)->meta
.id
, strerror(-r
));
499 if (state
== MOUNT_REMOUNTING_SIGTERM
|| state
== MOUNT_REMOUNTING_SIGKILL
)
500 mount_enter_mounted(m
, false);
502 mount_enter_dead(m
, false);
505 static void mount_enter_unmounting(Mount
*m
, bool success
) {
514 m
->control_command
= c
= m
->exec_command
+ MOUNT_EXEC_UNMOUNT
;
516 if ((r
= exec_command_set(
523 service_unwatch_control_pid(m
);
525 if ((r
= mount_spawn(m
, c
, &m
->control_pid
)) < 0)
528 mount_set_state(m
, MOUNT_UNMOUNTING
);
533 log_warning("%s failed to run umount exectuable: %s", UNIT(m
)->meta
.id
, strerror(-r
));
534 mount_enter_mounted(m
, false);
537 static void mount_enter_mounting(Mount
*m
, bool success
) {
546 m
->control_command
= c
= m
->exec_command
+ MOUNT_EXEC_MOUNT
;
548 if (m
->from_fragment
)
549 r
= exec_command_set(
552 m
->parameters_fragment
.what
,
554 "-t", m
->parameters_fragment
.fstype
,
555 "-o", m
->parameters_fragment
.options
,
557 else if (m
->from_etc_fstab
)
558 r
= exec_command_set(
569 service_unwatch_control_pid(m
);
571 if ((r
= mount_spawn(m
, c
, &m
->control_pid
)) < 0)
574 mount_set_state(m
, MOUNT_MOUNTING
);
579 log_warning("%s failed to run mount exectuable: %s", UNIT(m
)->meta
.id
, strerror(-r
));
580 mount_enter_dead(m
, false);
583 static void mount_enter_mounting_done(Mount
*m
, bool success
) {
589 mount_set_state(m
, MOUNT_MOUNTING_DONE
);
592 static void mount_enter_remounting(Mount
*m
, bool success
) {
601 m
->control_command
= c
= m
->exec_command
+ MOUNT_EXEC_REMOUNT
;
603 if (m
->from_fragment
) {
607 if (m
->parameters_fragment
.options
) {
608 if (!(buf
= strappend("remount,", m
->parameters_fragment
.options
))) {
617 r
= exec_command_set(
620 m
->parameters_fragment
.what
,
622 "-t", m
->parameters_fragment
.fstype
,
627 } else if (m
->from_etc_fstab
)
628 r
= exec_command_set(
642 service_unwatch_control_pid(m
);
644 if ((r
= mount_spawn(m
, c
, &m
->control_pid
)) < 0)
647 mount_set_state(m
, MOUNT_REMOUNTING
);
652 mount_enter_mounted(m
, false);
655 static int mount_start(Unit
*u
) {
660 /* We cannot fulfill this request right now, try again later
662 if (m
->state
== MOUNT_UNMOUNTING
||
663 m
->state
== MOUNT_UNMOUNTING_SIGTERM
||
664 m
->state
== MOUNT_UNMOUNTING_SIGKILL
)
668 if (m
->state
== MOUNT_MOUNTING
||
669 m
->state
== MOUNT_MOUNTING_SIGTERM
||
670 m
->state
== MOUNT_MOUNTING_SIGKILL
)
673 assert(m
->state
== MOUNT_DEAD
|| m
->state
== MOUNT_MAINTAINANCE
);
677 mount_enter_mounting(m
, true);
681 static int mount_stop(Unit
*u
) {
686 /* Cann't do this right now. */
687 if (m
->state
== MOUNT_MOUNTING
||
688 m
->state
== MOUNT_MOUNTING_DONE
||
689 m
->state
== MOUNT_MOUNTING_SIGTERM
||
690 m
->state
== MOUNT_MOUNTING_SIGKILL
||
691 m
->state
== MOUNT_REMOUNTING
||
692 m
->state
== MOUNT_REMOUNTING_SIGTERM
||
693 m
->state
== MOUNT_REMOUNTING_SIGKILL
)
697 if (m
->state
== MOUNT_UNMOUNTING
||
698 m
->state
== MOUNT_UNMOUNTING_SIGKILL
||
699 m
->state
== MOUNT_UNMOUNTING_SIGTERM
)
702 assert(m
->state
== MOUNT_MOUNTED
);
704 mount_enter_unmounting(m
, true);
708 static int mount_reload(Unit
*u
) {
713 if (m
->state
== MOUNT_MOUNTING_DONE
)
716 assert(m
->state
== MOUNT_MOUNTED
);
718 mount_enter_remounting(m
, true);
722 static UnitActiveState
mount_active_state(Unit
*u
) {
725 return state_translation_table
[MOUNT(u
)->state
];
728 static const char *mount_sub_state_to_string(Unit
*u
) {
731 return state_string_table
[MOUNT(u
)->state
];
734 static void mount_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
741 success
= code
== CLD_EXITED
&& status
== 0;
742 m
->failure
= m
->failure
|| !success
;
744 assert(m
->control_pid
== pid
);
745 assert(m
->control_command
);
747 exec_status_fill(&m
->control_command
->exec_status
, pid
, code
, status
);
750 log_debug("%s control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
752 /* Note that mount(8) returning and the kernel sending us a
753 * mount table change event might happen out-of-order. If an
754 * operation succeed we assume the kernel will follow soon too
755 * and already change into the resulting state. If it fails
756 * we check if the kernel still knows about the mount. and
757 * change state accordingly. */
762 case MOUNT_MOUNTING_DONE
:
763 case MOUNT_MOUNTING_SIGKILL
:
764 case MOUNT_MOUNTING_SIGTERM
:
765 case MOUNT_REMOUNTING
:
766 case MOUNT_REMOUNTING_SIGKILL
:
767 case MOUNT_REMOUNTING_SIGTERM
:
769 if (success
&& m
->from_proc_self_mountinfo
)
770 mount_enter_mounted(m
, true);
771 else if (m
->from_proc_self_mountinfo
)
772 mount_enter_mounted(m
, false);
774 mount_enter_dead(m
, false);
777 case MOUNT_UNMOUNTING
:
778 case MOUNT_UNMOUNTING_SIGKILL
:
779 case MOUNT_UNMOUNTING_SIGTERM
:
782 mount_enter_dead(m
, true);
783 else if (m
->from_proc_self_mountinfo
)
784 mount_enter_mounted(m
, false);
786 mount_enter_dead(m
, false);
790 assert_not_reached("Uh, control process died at wrong time.");
794 static void mount_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
798 assert(elapsed
== 1);
799 assert(w
== &m
->timer_watch
);
804 case MOUNT_MOUNTING_DONE
:
805 log_warning("%s mounting timed out. Stopping.", u
->meta
.id
);
806 mount_enter_signal(m
, MOUNT_MOUNTING_SIGTERM
, false);
809 case MOUNT_REMOUNTING
:
810 log_warning("%s remounting timed out. Stopping.", u
->meta
.id
);
811 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGTERM
, false);
814 case MOUNT_UNMOUNTING
:
815 log_warning("%s unmounting timed out. Stopping.", u
->meta
.id
);
816 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGTERM
, false);
819 case MOUNT_MOUNTING_SIGTERM
:
820 log_warning("%s mounting timed out. Killing.", u
->meta
.id
);
821 mount_enter_signal(m
, MOUNT_MOUNTING_SIGKILL
, false);
824 case MOUNT_REMOUNTING_SIGTERM
:
825 log_warning("%s remounting timed out. Killing.", u
->meta
.id
);
826 mount_enter_signal(m
, MOUNT_REMOUNTING_SIGKILL
, false);
829 case MOUNT_UNMOUNTING_SIGTERM
:
830 log_warning("%s unmounting timed out. Killing.", u
->meta
.id
);
831 mount_enter_signal(m
, MOUNT_UNMOUNTING_SIGKILL
, false);
834 case MOUNT_MOUNTING_SIGKILL
:
835 case MOUNT_REMOUNTING_SIGKILL
:
836 case MOUNT_UNMOUNTING_SIGKILL
:
837 log_warning("%s mount process still around after SIGKILL. Ignoring.", u
->meta
.id
);
839 if (m
->from_proc_self_mountinfo
)
840 mount_enter_mounted(m
, false);
842 mount_enter_dead(m
, false);
846 assert_not_reached("Timeout at wrong time.");
850 static int mount_add_one(
856 bool from_proc_self_mountinfo
,
861 char *e
, *w
= NULL
, *o
= NULL
, *f
= NULL
;
870 assert(!set_flags
|| from_proc_self_mountinfo
);
872 /* Ignore API mount points. They should never be referenced in
873 * dependencies ever. */
874 if (mount_point_is_api(where
))
877 /* probably some kind of swap, which we don't cover for now */
881 if (streq(where
, "/"))
882 e
= strdup("-.mount");
884 e
= unit_name_build_escape(where
+1, NULL
, ".mount");
889 if (!(u
= manager_get_unit(m
, e
))) {
892 if (!(u
= unit_new(m
))) {
897 r
= unit_add_name(u
, e
);
903 if (!(MOUNT(u
)->where
= strdup(where
))) {
908 if ((r
= unit_set_description(u
, where
)) < 0)
911 unit_add_to_load_queue(u
);
917 if (!(w
= strdup(what
)) ||
918 !(o
= strdup(options
)) ||
919 !(f
= strdup(fstype
))) {
924 if (from_proc_self_mountinfo
) {
925 mp
= &MOUNT(u
)->parameters_proc_self_mountinfo
;
928 MOUNT(u
)->is_mounted
= true;
929 MOUNT(u
)->just_mounted
= !MOUNT(u
)->from_proc_self_mountinfo
;
930 MOUNT(u
)->just_changed
= !streq_ptr(MOUNT(u
)->parameters_proc_self_mountinfo
.options
, o
);
933 MOUNT(u
)->from_proc_self_mountinfo
= true;
936 mp
= &MOUNT(u
)->parameters_etc_fstab
;
938 MOUNT(u
)->from_etc_fstab
= true;
950 unit_add_to_dbus_queue(u
);
965 static char *fstab_node_to_udev_node(char *p
) {
969 /* FIXME: to follow udev's logic 100% we need to leave valid
970 * UTF8 chars unescaped */
972 if (startswith(p
, "LABEL=")) {
974 if (!(t
= xescape(p
+6, "/ ")))
977 r
= asprintf(&dn
, "/dev/disk/by-label/%s", t
);
986 if (startswith(p
, "UUID=")) {
988 if (!(t
= xescape(p
+5, "/ ")))
991 r
= asprintf(&dn
, "/dev/disk/by-uuid/%s", ascii_strlower(t
));
1003 static int mount_load_etc_fstab(Manager
*m
) {
1011 if (!(f
= setmntent("/etc/fstab", "r")))
1014 while ((me
= getmntent(f
))) {
1017 if (!(what
= fstab_node_to_udev_node(me
->mnt_fsname
))) {
1022 if (!(where
= strdup(me
->mnt_dir
))) {
1029 path_kill_slashes(what
);
1031 if (where
[0] == '/')
1032 path_kill_slashes(where
);
1034 r
= mount_add_one(m
, what
, where
, me
->mnt_opts
, me
->mnt_type
, false, false);
1049 static int mount_load_proc_self_mountinfo(Manager
*m
, bool set_flags
) {
1051 char *device
, *path
, *options
, *fstype
, *d
, *p
;
1055 rewind(m
->proc_self_mountinfo
);
1060 device
= path
= options
= fstype
= d
= p
= NULL
;
1062 if ((k
= fscanf(m
->proc_self_mountinfo
,
1063 "%*s " /* (1) mount id */
1064 "%*s " /* (2) parent id */
1065 "%*s " /* (3) major:minor */
1066 "%*s " /* (4) root */
1067 "%ms " /* (5) mount point */
1068 "%ms" /* (6) mount options */
1069 "%*[^-]" /* (7) optional fields */
1070 "- " /* (8) seperator */
1071 "%ms " /* (9) file system type */
1072 "%ms" /* (10) mount source */
1073 "%*[^\n]", /* some rubbish at the end */
1086 if (!(d
= cunescape(device
)) ||
1087 !(p
= cunescape(path
))) {
1092 if ((r
= mount_add_one(m
, d
, p
, options
, fstype
, true, set_flags
)) < 0)
1116 static void mount_shutdown(Manager
*m
) {
1119 if (m
->proc_self_mountinfo
)
1120 fclose(m
->proc_self_mountinfo
);
1123 static int mount_enumerate(Manager
*m
) {
1125 struct epoll_event ev
;
1128 if (!(m
->proc_self_mountinfo
= fopen("/proc/self/mountinfo", "r")))
1131 m
->mount_watch
.type
= WATCH_MOUNT
;
1132 m
->mount_watch
.fd
= fileno(m
->proc_self_mountinfo
);
1135 ev
.events
= EPOLLERR
;
1136 ev
.data
.ptr
= &m
->mount_watch
;
1138 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->mount_watch
.fd
, &ev
) < 0)
1141 if ((r
= mount_load_etc_fstab(m
)) < 0)
1144 if ((r
= mount_load_proc_self_mountinfo(m
, false)) < 0)
1154 void mount_fd_event(Manager
*m
, int events
) {
1159 assert(events
== EPOLLERR
);
1161 /* The manager calls this for every fd event happening on the
1162 * /proc/self/mountinfo file, which informs us about mounting
1165 if ((r
= mount_load_proc_self_mountinfo(m
, true)) < 0) {
1166 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-errno
));
1168 /* Reset flags, just in case, for later calls */
1169 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1170 Mount
*mount
= (Mount
*) meta
;
1172 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1178 manager_dispatch_load_queue(m
);
1180 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_MOUNT
]) {
1181 Mount
*mount
= (Mount
*) meta
;
1183 if (!mount
->is_mounted
) {
1184 /* This has just been unmounted. */
1186 mount
->from_proc_self_mountinfo
= false;
1188 switch (mount
->state
) {
1191 mount_enter_dead(mount
, true);
1195 mount_set_state(mount
, mount
->state
);
1200 } else if (mount
->just_mounted
|| mount
->just_changed
) {
1202 /* New or changed entrymount */
1204 switch (mount
->state
) {
1207 case MOUNT_MAINTAINANCE
:
1208 mount_enter_mounted(mount
, true);
1211 case MOUNT_MOUNTING
:
1212 mount_enter_mounting_done(mount
, true);
1216 /* Nothing really changed, but let's
1217 * issue an notification call
1218 * nonetheless, in case somebody is
1219 * waiting for this. (e.g. file system
1220 * ro/rw remounts.) */
1221 mount_set_state(mount
, mount
->state
);
1226 /* Reset the flags for later calls */
1227 mount
->is_mounted
= mount
->just_mounted
= mount
->just_changed
= false;
1231 int mount_path_is_mounted(Manager
*m
, const char* path
) {
1241 if (!(t
= strdup(path
)))
1244 path_kill_slashes(t
);
1250 if (!(e
= unit_name_build_escape(t
+1, NULL
, ".mount"))) {
1255 u
= manager_get_unit(m
, e
);
1259 (MOUNT(u
)->from_etc_fstab
|| MOUNT(u
)->from_fragment
) &&
1260 MOUNT(u
)->state
!= MOUNT_MOUNTED
) {
1265 assert_se(slash
= strrchr(t
, '/'));
1282 const UnitVTable mount_vtable
= {
1286 .no_instances
= true,
1292 .coldplug
= mount_coldplug
,
1296 .start
= mount_start
,
1298 .reload
= mount_reload
,
1300 .active_state
= mount_active_state
,
1301 .sub_state_to_string
= mount_sub_state_to_string
,
1303 .sigchld_event
= mount_sigchld_event
,
1304 .timer_event
= mount_timer_event
,
1306 .enumerate
= mount_enumerate
,
1307 .shutdown
= mount_shutdown