summaryrefslogtreecommitdiff
path: root/src/remount-fs/remount-fs.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/remount-fs/remount-fs.c')
-rw-r--r--src/remount-fs/remount-fs.c173
1 files changed, 99 insertions, 74 deletions
diff --git a/src/remount-fs/remount-fs.c b/src/remount-fs/remount-fs.c
index 9220a00215..0bac355e0f 100644
--- a/src/remount-fs/remount-fs.c
+++ b/src/remount-fs/remount-fs.c
@@ -8,8 +8,10 @@
#include <sys/wait.h>
#include <unistd.h>
+#include "env-util.h"
#include "exit-status.h"
#include "log.h"
+#include "main-func.h"
#include "mount-setup.h"
#include "mount-util.h"
#include "path-util.h"
@@ -18,114 +20,137 @@
#include "strv.h"
#include "util.h"
-/* Goes through /etc/fstab and remounts all API file systems, applying
- * options that are in /etc/fstab that systemd might not have
- * respected */
+/* Goes through /etc/fstab and remounts all API file systems, applying options that are in /etc/fstab that systemd
+ * might not have respected */
-int main(int argc, char *argv[]) {
+static int track_pid(Hashmap **h, const char *path, pid_t pid) {
+ _cleanup_free_ char *c = NULL;
+ int r;
+
+ assert(h);
+ assert(path);
+ assert(pid_is_valid(pid));
+
+ r = hashmap_ensure_allocated(h, NULL);
+ if (r < 0)
+ return log_oom();
+
+ c = strdup(path);
+ if (!c)
+ return log_oom();
+
+ r = hashmap_put(*h, PID_TO_PTR(pid), c);
+ if (r < 0)
+ return log_oom();
+
+ TAKE_PTR(c);
+ return 0;
+}
+
+static int run(int argc, char *argv[]) {
_cleanup_hashmap_free_free_ Hashmap *pids = NULL;
_cleanup_endmntent_ FILE *f = NULL;
+ bool has_root = false;
struct mntent* me;
int r;
- if (argc > 1) {
- log_error("This program takes no argument.");
- return EXIT_FAILURE;
- }
+ log_setup_service();
- log_set_target(LOG_TARGET_AUTO);
- log_parse_environment();
- log_open();
+ if (argc > 1)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+ "This program takes no arguments.");
umask(0022);
f = setmntent("/etc/fstab", "re");
if (!f) {
- if (errno == ENOENT) {
- r = 0;
- goto finish;
- }
-
- r = log_error_errno(errno, "Failed to open /etc/fstab: %m");
- goto finish;
- }
-
- pids = hashmap_new(NULL);
- if (!pids) {
- r = log_oom();
- goto finish;
- }
-
- while ((me = getmntent(f))) {
- pid_t pid;
- int k;
- char *s;
-
- /* Remount the root fs, /usr and all API VFS */
- if (!mount_point_is_api(me->mnt_dir) &&
- !path_equal(me->mnt_dir, "/") &&
- !path_equal(me->mnt_dir, "/usr"))
- continue;
+ if (errno != ENOENT)
+ return log_error_errno(errno, "Failed to open /etc/fstab: %m");
+ } else {
+ while ((me = getmntent(f))) {
+ pid_t pid;
+
+ /* Remount the root fs, /usr and all API VFS */
+ if (!mount_point_is_api(me->mnt_dir) &&
+ !PATH_IN_SET(me->mnt_dir, "/", "/usr"))
+ continue;
- log_debug("Remounting %s", me->mnt_dir);
+ log_debug("Remounting %s...", me->mnt_dir);
- r = safe_fork("(remount)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
- if (r < 0)
- goto finish;
- if (r == 0) {
- /* Child */
+ if (path_equal(me->mnt_dir, "/"))
+ has_root = true;
- execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, me->mnt_dir, "-o", "remount"));
+ r = safe_fork("(remount)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
+ if (r < 0)
+ return r;
+ if (r == 0) {
+ /* Child */
+ execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, me->mnt_dir, "-o", "remount"));
+ log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
+ _exit(EXIT_FAILURE);
+ }
- log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
- _exit(EXIT_FAILURE);
+ /* Parent */
+ r = track_pid(&pids, me->mnt_dir, pid);
+ if (r < 0)
+ return r;
}
+ }
- /* Parent */
+ if (!has_root) {
+ /* The $SYSTEMD_REMOUNT_ROOT_RW environment variable is set by systemd-gpt-auto-generator to tell us
+ * whether to remount things. We honour it only if there's no explicit line in /etc/fstab configured
+ * which takes precedence. */
+
+ r = getenv_bool("SYSTEMD_REMOUNT_ROOT_RW");
+ if (r > 0) {
+ pid_t pid;
+
+ log_debug("Remounting / writable...");
+
+ r = safe_fork("(remount-rw)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
+ if (r < 0)
+ return r;
+ if (r == 0) {
+ /* Child */
+ execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, "/", "-o", "remount,rw"));
+ log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
+ _exit(EXIT_FAILURE);
+ }
- s = strdup(me->mnt_dir);
- if (!s) {
- r = log_oom();
- goto finish;
- }
+ r = track_pid(&pids, "/", pid);
+ if (r < 0)
+ return r;
- k = hashmap_put(pids, PID_TO_PTR(pid), s);
- if (k < 0) {
- free(s);
- r = log_oom();
- goto finish;
- }
+ } else if (r < 0 && r != -ENXIO)
+ log_warning_errno(r, "Failed to parse $SYSTEMD_REMOUNT_ROOT_RW, ignoring: %m");
}
r = 0;
while (!hashmap_isempty(pids)) {
+ _cleanup_free_ char *s = NULL;
siginfo_t si = {};
- char *s;
if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
-
if (errno == EINTR)
continue;
- r = log_error_errno(errno, "waitid() failed: %m");
- goto finish;
+ return log_error_errno(errno, "waitid() failed: %m");
}
s = hashmap_remove(pids, PID_TO_PTR(si.si_pid));
- if (s) {
- if (!is_clean_exit(si.si_code, si.si_status, EXIT_CLEAN_COMMAND, NULL)) {
- if (si.si_code == CLD_EXITED)
- log_error(MOUNT_PATH " for %s exited with exit status %i.", s, si.si_status);
- else
- log_error(MOUNT_PATH " for %s terminated by signal %s.", s, signal_to_string(si.si_status));
-
- r = -ENOEXEC;
- }
-
- free(s);
+ if (s &&
+ !is_clean_exit(si.si_code, si.si_status, EXIT_CLEAN_COMMAND, NULL)) {
+ if (si.si_code == CLD_EXITED)
+ log_error(MOUNT_PATH " for %s exited with exit status %i.", s, si.si_status);
+ else
+ log_error(MOUNT_PATH " for %s terminated by signal %s.", s, signal_to_string(si.si_status));
+
+ r = -ENOEXEC;
}
}
-finish:
- return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+ return r;
}
+
+DEFINE_MAIN_FUNCTION(run);