diff --git a/Makefile.am b/Makefile.am index 43d0ebb1..30403afa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,8 +1,8 @@ ACLOCAL_AMFLAGS = -I m4 sbin_PROGRAMS = finit initctl reboot -finit_SOURCES = finit.c api.c client.c conf.c exec.c getty.c helpers.c pid.c \ - sig.c svc.c service.c plugin.c tty.c cond.c cond-w.c sm.c \ - utmp-api.c utmp-api.h +finit_SOURCES = finit.c api.c client.c conf.c exec.c getty.c helpers.c \ + mount.c pid.c sig.c svc.c service.c plugin.c tty.c \ + cond.c cond-w.c sm.c utmp-api.c utmp-api.h pkginclude_HEADERS = cond.h finit.h helpers.h inetd.h plugin.h svc.h if INETD finit_SOURCES += inetd.c diff --git a/mount.c b/mount.c new file mode 100644 index 00000000..425c359b --- /dev/null +++ b/mount.c @@ -0,0 +1,110 @@ +/* Mount and unmount helpers + * + * Copyright (c) 2016 Joachim Nilsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include + +/* + * SysV init on Debian/Ubuntu skips these protected mount points + * + * It also skips anything below /proc, /sys and /run for good meausre so + * we do the same here. What we want is a list of non-protected tmpfs + * and regular filesystems that can be safely unmounted before we do + * swapoff, and remount / as read-only, respectively. + */ +static int is_protected(char *dir) +{ + size_t i; + char *protected[] = { + "/sys", "/proc", + "/.dev", "/dev", "/dev/pts", "/dev/shm", "dev/.static/dev", "/dev/vcs", + "/run", "/var/run", + "/", NULL + }; + char *any[] = { + "/proc/", "/sys/", "/run/", NULL + }; + + for (i = 0; protected[i]; i++) { + if (!strcmp(dir, protected[i])) + return 1; + } + + for (i = 0; any[i]; i++) { + if (!strncmp(dir, any[i], strlen(any[i]))) + return 1; + } + + return 0; +} + +static struct mntent *iterator(char *fstab) +{ + static FILE *fp = NULL; + static struct mntent *mnt; + + if (!fp && fstab) { + fp = setmntent(fstab, "r"); + if (!fp) + return NULL; + } + + while ((mnt = getmntent(fp))) { + if (is_protected(mnt->mnt_dir)) + continue; + + return mnt; + } + + endmntent(fp); + fp = NULL; + + return NULL; +} + +void unmount_tmpfs(void) +{ + struct mntent *mnt; + + while ((mnt = iterator("/proc/mounts"))) { + if (!strcmp("tmpfs", mnt->mnt_fsname)) + umount(mnt->mnt_dir); + } +} + +void unmount_regular(void) +{ + struct mntent *mnt; + + while ((mnt = iterator("/proc/mounts"))) { + umount(mnt->mnt_dir); + } +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */ diff --git a/sig.c b/sig.c index 3fa7efef..448a1bdf 100644 --- a/sig.c +++ b/sig.c @@ -86,6 +86,9 @@ static uev_t sighup_watcher, sigint_watcher, sigpwr_watcher; static uev_t sigchld_watcher, sigsegv_watcher; static uev_t sigstop_watcher, sigtstp_watcher, sigcont_watcher; +void unmount_tmpfs(void); +void unmount_regular(void); + void do_shutdown(shutop_t op) { @@ -141,17 +144,19 @@ void do_shutdown(shutop_t op) } /* Unmount any tmpfs before unmounting swap ... */ - run("/bin/umount -r -a"); + unmount_tmpfs(); run("/sbin/swapoff -e -a"); - /* Now, unmount everything else ... err, just force it. */ - run("/bin/umount -n -f -r -a"); + /* ... unmount remaining regular file systems. */ + unmount_regular(); /* We sit on / so we must remount it ro, try all the things! */ run("/bin/mount -n -o remount,ro -t dummytype dummydev /"); run("/bin/mount -n -o remount,ro dummydev /"); run("/bin/mount -n -o remount,ro /"); + /* XXX: Call mdadm to mark RAID array(s) as clean before halting. */ + _d("%s.", op == SHUT_REBOOT ? "Rebooting" : "Halting"); if (op == SHUT_REBOOT) reboot(RB_AUTOBOOT);