]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/remount-fs/remount-fs.c
basic/log: add concept of "synthethic errnos"
[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 log_error("This program takes no arguments.");
36 return -EINVAL;
37 }
38
39 umask(0022);
40
41 f = setmntent("/etc/fstab", "re");
42 if (!f) {
43 if (errno == ENOENT)
44 return 0;
45
46 return log_error_errno(errno, "Failed to open /etc/fstab: %m");
47 }
48
49 pids = hashmap_new(NULL);
50 if (!pids)
51 return log_oom();
52
53 while ((me = getmntent(f))) {
54 _cleanup_free_ char *s = NULL;
55 pid_t pid;
56 int k;
57
58 /* Remount the root fs, /usr and all API VFS */
59 if (!mount_point_is_api(me->mnt_dir) &&
60 !path_equal(me->mnt_dir, "/") &&
61 !path_equal(me->mnt_dir, "/usr"))
62 continue;
63
64 log_debug("Remounting %s", me->mnt_dir);
65
66 r = safe_fork("(remount)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
67 if (r < 0)
68 return r;
69 if (r == 0) {
70 /* Child */
71
72 execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, me->mnt_dir, "-o", "remount"));
73
74 log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
75 _exit(EXIT_FAILURE);
76 }
77
78 /* Parent */
79
80 s = strdup(me->mnt_dir);
81 if (!s)
82 return log_oom();
83
84 k = hashmap_put(pids, PID_TO_PTR(pid), s);
85 if (k < 0)
86 return log_oom();
87 TAKE_PTR(s);
88 }
89
90 r = 0;
91 while (!hashmap_isempty(pids)) {
92 siginfo_t si = {};
93 _cleanup_free_ char *s = NULL;
94
95 if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
96 if (errno == EINTR)
97 continue;
98
99 return log_error_errno(errno, "waitid() failed: %m");
100 }
101
102 s = hashmap_remove(pids, PID_TO_PTR(si.si_pid));
103 if (s &&
104 !is_clean_exit(si.si_code, si.si_status, EXIT_CLEAN_COMMAND, NULL)) {
105 if (si.si_code == CLD_EXITED)
106 log_error(MOUNT_PATH " for %s exited with exit status %i.", s, si.si_status);
107 else
108 log_error(MOUNT_PATH " for %s terminated by signal %s.", s, signal_to_string(si.si_status));
109
110 r = -ENOEXEC;
111 }
112 }
113
114 return r;
115 }
116
117 DEFINE_MAIN_FUNCTION(run);