]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/remount-fs/remount-fs.c
basic/log: add concept of "synthethic errnos"
[thirdparty/systemd.git] / src / remount-fs / remount-fs.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
449ddb2d 2
449ddb2d 3#include <errno.h>
cf0fbc49 4#include <mntent.h>
449ddb2d 5#include <string.h>
b16fee15 6#include <sys/prctl.h>
449ddb2d
LP
7#include <sys/stat.h>
8#include <sys/wait.h>
cf0fbc49 9#include <unistd.h>
449ddb2d 10
4349cd7c 11#include "exit-status.h"
449ddb2d 12#include "log.h"
5e332028 13#include "main-func.h"
4349cd7c
LP
14#include "mount-setup.h"
15#include "mount-util.h"
9eb977db 16#include "path-util.h"
b16fee15 17#include "process-util.h"
24882e06 18#include "signal-util.h"
b16fee15 19#include "strv.h"
4349cd7c 20#include "util.h"
449ddb2d
LP
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
6e61c701 26static int run(int argc, char *argv[]) {
b16fee15 27 _cleanup_hashmap_free_free_ Hashmap *pids = NULL;
5862d652 28 _cleanup_endmntent_ FILE *f = NULL;
449ddb2d 29 struct mntent* me;
b16fee15 30 int r;
449ddb2d 31
6bf3c61c 32 log_setup_service();
449ddb2d 33
6e61c701
ZJS
34 if (argc > 1) {
35 log_error("This program takes no arguments.");
36 return -EINVAL;
37 }
38
4c12626c
LP
39 umask(0022);
40
9ffcff0e 41 f = setmntent("/etc/fstab", "re");
adb2ce5f 42 if (!f) {
6e61c701
ZJS
43 if (errno == ENOENT)
44 return 0;
e0295d26 45
6e61c701 46 return log_error_errno(errno, "Failed to open /etc/fstab: %m");
449ddb2d
LP
47 }
48
d5099efc 49 pids = hashmap_new(NULL);
6e61c701
ZJS
50 if (!pids)
51 return log_oom();
449ddb2d 52
449ddb2d 53 while ((me = getmntent(f))) {
6e61c701 54 _cleanup_free_ char *s = NULL;
449ddb2d
LP
55 pid_t pid;
56 int k;
449ddb2d 57
b4efdf97 58 /* Remount the root fs, /usr and all API VFS */
2b93b027 59 if (!mount_point_is_api(me->mnt_dir) &&
b4efdf97
LP
60 !path_equal(me->mnt_dir, "/") &&
61 !path_equal(me->mnt_dir, "/usr"))
449ddb2d
LP
62 continue;
63
64 log_debug("Remounting %s", me->mnt_dir);
65
b6e1fff1
LP
66 r = safe_fork("(remount)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
67 if (r < 0)
6e61c701 68 return r;
4c253ed1 69 if (r == 0) {
449ddb2d
LP
70 /* Child */
71
b16fee15 72 execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, me->mnt_dir, "-o", "remount"));
449ddb2d 73
f00929ad 74 log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
22f4096c 75 _exit(EXIT_FAILURE);
449ddb2d
LP
76 }
77
78 /* Parent */
79
80 s = strdup(me->mnt_dir);
6e61c701
ZJS
81 if (!s)
82 return log_oom();
adb2ce5f 83
4a0b58c4 84 k = hashmap_put(pids, PID_TO_PTR(pid), s);
6e61c701
ZJS
85 if (k < 0)
86 return log_oom();
87 TAKE_PTR(s);
449ddb2d
LP
88 }
89
b16fee15 90 r = 0;
449ddb2d 91 while (!hashmap_isempty(pids)) {
b92bea5d 92 siginfo_t si = {};
6e61c701 93 _cleanup_free_ char *s = NULL;
449ddb2d 94
449ddb2d 95 if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
449ddb2d
LP
96 if (errno == EINTR)
97 continue;
98
6e61c701 99 return log_error_errno(errno, "waitid() failed: %m");
449ddb2d
LP
100 }
101
4a0b58c4 102 s = hashmap_remove(pids, PID_TO_PTR(si.si_pid));
6e61c701
ZJS
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;
449ddb2d
LP
111 }
112 }
113
6e61c701 114 return r;
449ddb2d 115}
6e61c701
ZJS
116
117DEFINE_MAIN_FUNCTION(run);