httpd: remove redundant NULL assignment and save one strrchr. -8 bytes
[oweals/busybox.git] / util-linux / switch_root.c
index 0f00b605afe2bfc539b67f39ed8e3f06481fca0b..db6ae3542e95f4b9a744643ae036c1dbddbd918f 100644 (file)
@@ -3,20 +3,27 @@
  *
  * Switch from rootfs to another filesystem as the root of the mount tree.
  *
- * Licensed under GPL version 2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
-#include "libbb.h"
-#include <sys/vfs.h>
 
+//usage:#define switch_root_trivial_usage
+//usage:       "[-c /dev/console] NEW_ROOT NEW_INIT [ARGS]"
+//usage:#define switch_root_full_usage "\n\n"
+//usage:       "Free initramfs and switch to another root fs:\n"
+//usage:       "chroot to NEW_ROOT, delete all in /, move NEW_ROOT to /,\n"
+//usage:       "execute NEW_INIT. PID must be 1. NEW_ROOT must be a mountpoint.\n"
+//usage:     "\n       -c DEV  Reopen stdio to DEV after switch"
+
+#include <sys/vfs.h>
+#include <sys/mount.h>
+#include "libbb.h"
 // Make up for header deficiencies
 #ifndef RAMFS_MAGIC
 # define RAMFS_MAGIC ((unsigned)0x858458f6)
 #endif
-
 #ifndef TMPFS_MAGIC
 # define TMPFS_MAGIC ((unsigned)0x01021994)
 #endif
-
 #ifndef MS_MOVE
 # define MS_MOVE     8192
 #endif
@@ -87,12 +94,14 @@ int switch_root_main(int argc UNUSED_PARAM, char **argv)
        // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
        // we mean it. I could make this a CONFIG option, but I would get email
        // from all the people who WILL destroy their filesystems.
+       if (stat("/init", &st) != 0 || !S_ISREG(st.st_mode)) {
+               bb_error_msg_and_die("/init is not a regular file");
+       }
        statfs("/", &stfs); // this never fails
-       if (stat("/init", &st) != 0 || !S_ISREG(st.st_mode)
-        || ((unsigned)stfs.f_type != RAMFS_MAGIC
-            && (unsigned)stfs.f_type != TMPFS_MAGIC)
+       if ((unsigned)stfs.f_type != RAMFS_MAGIC
+        && (unsigned)stfs.f_type != TMPFS_MAGIC
        ) {
-               bb_error_msg_and_die("not rootfs");
+               bb_error_msg_and_die("root filesystem is not ramfs/tmpfs");
        }
 
        // Zap everything out of rootdev