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