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