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