]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | } | |
449ddb2d | 49 | |
becccb52 ZJS |
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 | ||
6e61c701 | 75 | static int run(int argc, char *argv[]) { |
b16fee15 | 76 | _cleanup_hashmap_free_free_ Hashmap *pids = NULL; |
5862d652 | 77 | _cleanup_endmntent_ FILE *f = NULL; |
59f13dd6 | 78 | bool has_root = false; |
449ddb2d | 79 | struct mntent* me; |
b16fee15 | 80 | int r; |
449ddb2d | 81 | |
6bf3c61c | 82 | log_setup_service(); |
449ddb2d | 83 | |
baaa35ad ZJS |
84 | if (argc > 1) |
85 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
86 | "This program takes no arguments."); | |
6e61c701 | 87 | |
4c12626c LP |
88 | umask(0022); |
89 | ||
ed4ad488 | 90 | f = setmntent(fstab_path(), "re"); |
adb2ce5f | 91 | if (!f) { |
59f13dd6 | 92 | if (errno != ENOENT) |
ed4ad488 | 93 | return log_error_errno(errno, "Failed to open %s: %m", fstab_path()); |
becccb52 | 94 | } else |
59f13dd6 | 95 | while ((me = getmntent(f))) { |
becccb52 | 96 | /* Remount the root fs, /usr, and all API VFSs */ |
59f13dd6 LP |
97 | if (!mount_point_is_api(me->mnt_dir) && |
98 | !PATH_IN_SET(me->mnt_dir, "/", "/usr")) | |
99 | continue; | |
449ddb2d | 100 | |
59f13dd6 LP |
101 | if (path_equal(me->mnt_dir, "/")) |
102 | has_root = true; | |
103 | ||
becccb52 | 104 | r = do_remount(me->mnt_dir, false, &pids); |
59f13dd6 LP |
105 | if (r < 0) |
106 | return r; | |
449ddb2d LP |
107 | } |
108 | ||
59f13dd6 LP |
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"); | |
becccb52 ZJS |
115 | if (r < 0 && r != -ENXIO) |
116 | log_warning_errno(r, "Failed to parse $SYSTEMD_REMOUNT_ROOT_RW, ignoring: %m"); | |
59f13dd6 | 117 | |
becccb52 ZJS |
118 | if (r > 0) { |
119 | r = do_remount("/", true, &pids); | |
59f13dd6 LP |
120 | if (r < 0) |
121 | return r; | |
becccb52 | 122 | } |
449ddb2d LP |
123 | } |
124 | ||
b16fee15 | 125 | r = 0; |
449ddb2d | 126 | while (!hashmap_isempty(pids)) { |
6e61c701 | 127 | _cleanup_free_ char *s = NULL; |
59f13dd6 | 128 | siginfo_t si = {}; |
449ddb2d | 129 | |
449ddb2d | 130 | if (waitid(P_ALL, 0, &si, WEXITED) < 0) { |
449ddb2d LP |
131 | if (errno == EINTR) |
132 | continue; | |
133 | ||
6e61c701 | 134 | return log_error_errno(errno, "waitid() failed: %m"); |
449ddb2d LP |
135 | } |
136 | ||
4a0b58c4 | 137 | s = hashmap_remove(pids, PID_TO_PTR(si.si_pid)); |
6e61c701 ZJS |
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; | |
449ddb2d LP |
146 | } |
147 | } | |
148 | ||
6e61c701 | 149 | return r; |
449ddb2d | 150 | } |
6e61c701 ZJS |
151 | |
152 | DEFINE_MAIN_FUNCTION(run); |