]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/remount-fs/remount-fs.c
tree-wide: remove Lennart's copyright lines
[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 "exit-status.h"
12 #include "log.h"
13 #include "mount-setup.h"
14 #include "mount-util.h"
15 #include "path-util.h"
16 #include "process-util.h"
17 #include "signal-util.h"
18 #include "strv.h"
19 #include "util.h"
20
21 /* Goes through /etc/fstab and remounts all API file systems, applying
22 * options that are in /etc/fstab that systemd might not have
23 * respected */
24
25 int main(int argc, char *argv[]) {
26 _cleanup_hashmap_free_free_ Hashmap *pids = NULL;
27 _cleanup_endmntent_ FILE *f = NULL;
28 struct mntent* me;
29 int r;
30
31 if (argc > 1) {
32 log_error("This program takes no argument.");
33 return EXIT_FAILURE;
34 }
35
36 log_set_target(LOG_TARGET_AUTO);
37 log_parse_environment();
38 log_open();
39
40 umask(0022);
41
42 f = setmntent("/etc/fstab", "re");
43 if (!f) {
44 if (errno == ENOENT) {
45 r = 0;
46 goto finish;
47 }
48
49 r = log_error_errno(errno, "Failed to open /etc/fstab: %m");
50 goto finish;
51 }
52
53 pids = hashmap_new(NULL);
54 if (!pids) {
55 r = log_oom();
56 goto finish;
57 }
58
59 while ((me = getmntent(f))) {
60 pid_t pid;
61 int k;
62 char *s;
63
64 /* Remount the root fs, /usr and all API VFS */
65 if (!mount_point_is_api(me->mnt_dir) &&
66 !path_equal(me->mnt_dir, "/") &&
67 !path_equal(me->mnt_dir, "/usr"))
68 continue;
69
70 log_debug("Remounting %s", me->mnt_dir);
71
72 r = safe_fork("(remount)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
73 if (r < 0)
74 goto finish;
75 if (r == 0) {
76 /* Child */
77
78 execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, me->mnt_dir, "-o", "remount"));
79
80 log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
81 _exit(EXIT_FAILURE);
82 }
83
84 /* Parent */
85
86 s = strdup(me->mnt_dir);
87 if (!s) {
88 r = log_oom();
89 goto finish;
90 }
91
92 k = hashmap_put(pids, PID_TO_PTR(pid), s);
93 if (k < 0) {
94 free(s);
95 r = log_oom();
96 goto finish;
97 }
98 }
99
100 r = 0;
101 while (!hashmap_isempty(pids)) {
102 siginfo_t si = {};
103 char *s;
104
105 if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
106
107 if (errno == EINTR)
108 continue;
109
110 r = log_error_errno(errno, "waitid() failed: %m");
111 goto finish;
112 }
113
114 s = hashmap_remove(pids, PID_TO_PTR(si.si_pid));
115 if (s) {
116 if (!is_clean_exit(si.si_code, si.si_status, EXIT_CLEAN_COMMAND, NULL)) {
117 if (si.si_code == CLD_EXITED)
118 log_error(MOUNT_PATH " for %s exited with exit status %i.", s, si.si_status);
119 else
120 log_error(MOUNT_PATH " for %s terminated by signal %s.", s, signal_to_string(si.si_status));
121
122 r = -ENOEXEC;
123 }
124
125 free(s);
126 }
127 }
128
129 finish:
130 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
131 }