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