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