remount-fs.c revision b4efdf97203ddf781c17f77be84cc61516a077d2
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2010 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <unistd.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <fcntl.h>
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering#include <errno.h>
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering#include <string.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/stat.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/wait.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <mntent.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "log.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "path-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "set.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "mount-setup.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "exit-status.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/* Goes through /etc/fstab and remounts all API file systems, applying
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * options that are in /etc/fstab that systemd might not have
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering * respected */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint main(int argc, char *argv[]) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int ret = EXIT_FAILURE;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering FILE *f = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct mntent* me;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Hashmap *pids = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (argc > 1) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("This program takes no argument.");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return EXIT_FAILURE;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_set_target(LOG_TARGET_AUTO);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_parse_environment();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_open();
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering umask(0022);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering f = setmntent("/etc/fstab", "r");
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (!f) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (errno == ENOENT) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ret = EXIT_SUCCESS;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering goto finish;
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to open /etc/fstab: %m");
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering goto finish;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering pids = hashmap_new(trivial_hash_func, trivial_compare_func);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!pids) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to allocate set");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto finish;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering }
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ret = EXIT_SUCCESS;
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering while ((me = getmntent(f))) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering pid_t pid;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int k;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering char *s;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Remount the root fs, /usr and all API VFS */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!mount_point_is_api(me->mnt_dir) &&
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering !path_equal(me->mnt_dir, "/") &&
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering !path_equal(me->mnt_dir, "/usr"))
continue;
log_debug("Remounting %s", me->mnt_dir);
pid = fork();
if (pid < 0) {
log_error("Failed to fork: %m");
ret = EXIT_FAILURE;
continue;
}
if (pid == 0) {
const char *arguments[5];
/* Child */
arguments[0] = "/bin/mount";
arguments[1] = me->mnt_dir;
arguments[2] = "-o";
arguments[3] = "remount";
arguments[4] = NULL;
execv("/bin/mount", (char **) arguments);
log_error("Failed to execute /bin/mount: %m");
_exit(EXIT_FAILURE);
}
/* Parent */
s = strdup(me->mnt_dir);
if (!s) {
log_error("Out of memory.");
ret = EXIT_FAILURE;
continue;
}
k = hashmap_put(pids, UINT_TO_PTR(pid), s);
if (k < 0) {
log_error("Failed to add PID to set: %s", strerror(-k));
ret = EXIT_FAILURE;
continue;
}
}
while (!hashmap_isempty(pids)) {
siginfo_t si;
char *s;
zero(si);
if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
if (errno == EINTR)
continue;
log_error("waitid() failed: %m");
ret = EXIT_FAILURE;
break;
}
s = hashmap_remove(pids, UINT_TO_PTR(si.si_pid));
if (s) {
if (!is_clean_exit(si.si_code, si.si_status)) {
if (si.si_code == CLD_EXITED)
log_error("/bin/mount for %s exited with exit status %i.", s, si.si_status);
else
log_error("/bin/mount for %s terminated by signal %s.", s, signal_to_string(si.si_status));
ret = EXIT_FAILURE;
}
free(s);
}
}
finish:
if (pids)
hashmap_free_free(pids);
if (f)
endmntent(f);
return ret;
}