]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/remount-fs/remount-fs.c
4515592387baa3323c302b58e4114a9cb3181ae5
[thirdparty/systemd.git] / src / remount-fs / remount-fs.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <mntent.h>
5 #include <sys/prctl.h>
6 #include <sys/stat.h>
7 #include <sys/wait.h>
8 #include <unistd.h>
9
10 #include "env-util.h"
11 #include "exit-status.h"
12 #include "fstab-util.h"
13 #include "log.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"
20 #include "strv.h"
21
22 /* Goes through /etc/fstab and remounts all API file systems, applying options that are in /etc/fstab that systemd
23 * might not have respected */
24
25 static int track_pid(Hashmap **h, const char *path, pid_t pid) {
26 _cleanup_free_ char *c = NULL;
27 int r;
28
29 assert(h);
30 assert(path);
31 assert(pid_is_valid(pid));
32
33 c = strdup(path);
34 if (!c)
35 return log_oom();
36
37 r = hashmap_ensure_put(h, NULL, PID_TO_PTR(pid), c);
38 if (r == -ENOMEM)
39 return log_oom();
40 if (r < 0)
41 return log_error_errno(r, "Failed to store pid " PID_FMT, pid);
42
43 TAKE_PTR(c);
44 return 0;
45 }
46
47 static int do_remount(const char *path, bool force_rw, Hashmap **pids) {
48 pid_t pid;
49 int r;
50
51 log_debug("Remounting %s...", path);
52
53 r = safe_fork(force_rw ? "(remount-rw)" : "(remount)",
54 FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
55 if (r < 0)
56 return r;
57 if (r == 0) {
58 /* Child */
59 execv(MOUNT_PATH,
60 STRV_MAKE(MOUNT_PATH,
61 path,
62 "-o",
63 force_rw ? "remount,rw" : "remount"));
64 log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
65 _exit(EXIT_FAILURE);
66 }
67
68 /* Parent */
69 return track_pid(pids, path, pid);
70 }
71
72 static int run(int argc, char *argv[]) {
73 _cleanup_hashmap_free_free_ Hashmap *pids = NULL;
74 _cleanup_endmntent_ FILE *f = NULL;
75 bool has_root = false;
76 struct mntent* me;
77 int r;
78
79 log_setup();
80
81 if (argc > 1)
82 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
83 "This program takes no arguments.");
84
85 umask(0022);
86
87 f = setmntent(fstab_path(), "re");
88 if (!f) {
89 if (errno != ENOENT)
90 return log_error_errno(errno, "Failed to open %s: %m", fstab_path());
91 } else
92 while ((me = getmntent(f))) {
93 /* Remount the root fs, /usr, and all API VFSs */
94 if (!mount_point_is_api(me->mnt_dir) &&
95 !PATH_IN_SET(me->mnt_dir, "/", "/usr"))
96 continue;
97
98 if (path_equal(me->mnt_dir, "/"))
99 has_root = true;
100
101 r = do_remount(me->mnt_dir, false, &pids);
102 if (r < 0)
103 return r;
104 }
105
106 if (!has_root) {
107 /* The $SYSTEMD_REMOUNT_ROOT_RW environment variable is set by systemd-gpt-auto-generator to tell us
108 * whether to remount things. We honour it only if there's no explicit line in /etc/fstab configured
109 * which takes precedence. */
110
111 r = getenv_bool("SYSTEMD_REMOUNT_ROOT_RW");
112 if (r < 0 && r != -ENXIO)
113 log_warning_errno(r, "Failed to parse $SYSTEMD_REMOUNT_ROOT_RW, ignoring: %m");
114
115 if (r > 0) {
116 r = do_remount("/", true, &pids);
117 if (r < 0)
118 return r;
119 }
120 }
121
122 r = 0;
123 while (!hashmap_isempty(pids)) {
124 _cleanup_free_ char *s = NULL;
125 siginfo_t si = {};
126
127 if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
128 if (errno == EINTR)
129 continue;
130
131 return log_error_errno(errno, "waitid() failed: %m");
132 }
133
134 s = hashmap_remove(pids, PID_TO_PTR(si.si_pid));
135 if (s &&
136 !is_clean_exit(si.si_code, si.si_status, EXIT_CLEAN_COMMAND, NULL)) {
137 if (si.si_code == CLD_EXITED)
138 log_error(MOUNT_PATH " for %s exited with exit status %i.", s, si.si_status);
139 else
140 log_error(MOUNT_PATH " for %s terminated by signal %s.", s, signal_to_string(si.si_status));
141
142 r = -ENOEXEC;
143 }
144 }
145
146 return r;
147 }
148
149 DEFINE_MAIN_FUNCTION(run);