1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include "exit-status.h"
14 #include "main-func.h"
15 #include "mount-setup.h"
16 #include "mount-util.h"
17 #include "path-util.h"
18 #include "process-util.h"
19 #include "signal-util.h"
23 /* Goes through /etc/fstab and remounts all API file systems, applying options that are in /etc/fstab that systemd
24 * might not have respected */
26 static int track_pid(Hashmap
**h
, const char *path
, pid_t pid
) {
27 _cleanup_free_
char *c
= NULL
;
32 assert(pid_is_valid(pid
));
34 r
= hashmap_ensure_allocated(h
, NULL
);
42 r
= hashmap_put(*h
, PID_TO_PTR(pid
), c
);
50 static int run(int argc
, char *argv
[]) {
51 _cleanup_hashmap_free_free_ Hashmap
*pids
= NULL
;
52 _cleanup_endmntent_
FILE *f
= NULL
;
53 bool has_root
= false;
60 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
61 "This program takes no arguments.");
65 f
= setmntent("/etc/fstab", "re");
68 return log_error_errno(errno
, "Failed to open /etc/fstab: %m");
70 while ((me
= getmntent(f
))) {
73 /* Remount the root fs, /usr and all API VFS */
74 if (!mount_point_is_api(me
->mnt_dir
) &&
75 !PATH_IN_SET(me
->mnt_dir
, "/", "/usr"))
78 log_debug("Remounting %s...", me
->mnt_dir
);
80 if (path_equal(me
->mnt_dir
, "/"))
83 r
= safe_fork("(remount)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pid
);
88 execv(MOUNT_PATH
, STRV_MAKE(MOUNT_PATH
, me
->mnt_dir
, "-o", "remount"));
89 log_error_errno(errno
, "Failed to execute " MOUNT_PATH
": %m");
94 r
= track_pid(&pids
, me
->mnt_dir
, pid
);
101 /* The $SYSTEMD_REMOUNT_ROOT_RW environment variable is set by systemd-gpt-auto-generator to tell us
102 * whether to remount things. We honour it only if there's no explicit line in /etc/fstab configured
103 * which takes precedence. */
105 r
= getenv_bool("SYSTEMD_REMOUNT_ROOT_RW");
109 log_debug("Remounting / writable...");
111 r
= safe_fork("(remount-rw)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
116 execv(MOUNT_PATH
, STRV_MAKE(MOUNT_PATH
, "/", "-o", "remount,rw"));
117 log_error_errno(errno
, "Failed to execute " MOUNT_PATH
": %m");
121 r
= track_pid(&pids
, "/", pid
);
125 } else if (r
< 0 && r
!= -ENXIO
)
126 log_warning_errno(r
, "Failed to parse $SYSTEMD_REMOUNT_ROOT_RW, ignoring: %m");
130 while (!hashmap_isempty(pids
)) {
131 _cleanup_free_
char *s
= NULL
;
134 if (waitid(P_ALL
, 0, &si
, WEXITED
) < 0) {
138 return log_error_errno(errno
, "waitid() failed: %m");
141 s
= hashmap_remove(pids
, PID_TO_PTR(si
.si_pid
));
143 !is_clean_exit(si
.si_code
, si
.si_status
, EXIT_CLEAN_COMMAND
, NULL
)) {
144 if (si
.si_code
== CLD_EXITED
)
145 log_error(MOUNT_PATH
" for %s exited with exit status %i.", s
, si
.si_status
);
147 log_error(MOUNT_PATH
" for %s terminated by signal %s.", s
, signal_to_string(si
.si_status
));
156 DEFINE_MAIN_FUNCTION(run
);