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