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