]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
sulogin: mount temporary /dev and /proc if not found
authorWerner Fink <werner@suse.de>
Mon, 10 Dec 2012 12:27:10 +0000 (13:27 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 18 Dec 2012 14:45:33 +0000 (15:45 +0100)
This is very usefull if initrd can not loaded that is no /dev and no
/proc is found.  Also if the /etc/shadow and /etc/passwd is copied into
the initrd the sulogin can be used in initrd even before /dev and/or /proc
are mounted.

Signed-off-by: Werner Fink <werner@suse.de>
configure.ac
lib/consoles.c

index 3be3715e50bd2702a2f2042d65063cc306a03432..c0783056c605e968f5a5d1671256084d5c80791e 100644 (file)
@@ -1296,6 +1296,19 @@ if test "x$enable_use_tty_group" = xyes; then
   AC_DEFINE(USE_TTY_GROUP, 1, [Should wall and write be installed setgid tty?])
 fi
 
+
+AC_ARG_ENABLE([sulogin-emergency-mount],
+  AS_HELP_STRING([--disable-sulogin-emergency-mount],
+                [do not use emergency mount of /dev and /proc for sulogin]),
+  [], enable_sulogin_emergency_mount=yes
+)
+
+if test "x$enable_sulogin_emergency_mount" = xyes; then
+  AC_DEFINE(USE_SULOGIN_EMERGENCY_MOUNT, 1,
+           [Should sulogin use a emergency mount of /dev and /proc?])
+fi
+
+
 AC_ARG_ENABLE([makeinstall-chown],
   AS_HELP_STRING([--disable-makeinstall-chown], [do not do chown-like operations during "make install"]),
   [], enable_makeinstall_chown=yes
index 21c5a249a3ee0aed737821af32a148e8000b3c4b..7175a0882979a8a0d333a43182787c46fd54e6f7 100644 (file)
 #include <dirent.h>
 #include <unistd.h>
 
+#ifdef USE_SULOGIN_EMERGENCY_MOUNT
+# include <sys/mount.h>
+# include <linux/fs.h>
+# include <linux/magic.h>
+# include <linux/major.h>
+# ifndef MNT_DETACH
+#  define MNT_DETACH   2
+# endif
+#endif
+
 #include "c.h"
 #include "canonicalize.h"
 #include "consoles.h"
@@ -77,6 +87,64 @@ dbgprint(const char *mesg, ...)
        fputc('\n', stderr);
 }
 
+#ifdef USE_SULOGIN_EMERGENCY_MOUNT
+/*
+ * Make C library standard calls such like ttyname(3) work
+ * even if the system does not show any of the standard
+ * directories.
+ */
+
+static uint32_t emergency_flags;
+# define MNT_PROCFS    0x0001
+# define MNT_DEVTMPFS  0x0002
+
+static __attribute__((__destructor__))
+void emergency_do_umounts(void)
+{
+       if (emergency_flags & MNT_DEVTMPFS)
+               umount2("/dev", MNT_DETACH);
+       if (emergency_flags & MNT_PROCFS)
+               umount2("/proc", MNT_DETACH);
+}
+
+static __attribute__((__constructor__))
+void emergency_do_mounts(void)
+{
+       struct stat rt, xt;
+
+       if (emergency_flags) {
+               emergency_flags = 0;
+               return;
+       }
+
+       if (stat("/", &rt) != 0) {
+               warn("can not get file status of root file system\n");
+               return;
+       }
+
+       if (stat("/proc", &xt) == 0
+           && rt.st_dev == xt.st_dev
+           && mount("proc", "/proc", "proc", MS_RELATIME, NULL) == 0)
+               emergency_flags |= MNT_PROCFS;
+
+       if (stat("/dev", &xt) == 0
+           && rt.st_dev == xt.st_dev
+           && mount("devtmpfs", "/dev", "devtmpfs",
+                    MS_RELATIME, "mode=0755,nr_inodes=0") == 0) {
+
+               emergency_flags |= MNT_DEVTMPFS;
+               mknod("/dev/console", S_IFCHR|S_IRUSR|S_IWUSR,
+                                       makedev(TTYAUX_MAJOR, 1));
+
+               if (symlink("/proc/self/fd", "/dev/fd") == 0) {
+                       ignore_result( symlink("fd/0", "/dev/stdin") );
+                       ignore_result( symlink("fd/1", "/dev/stdout") );
+                       ignore_result( symlink("fd/2", "/dev/stderr") );
+               }
+       }
+}
+#endif /* USE_SULOGIN_EMERGENCY_MOUNT */
+
 /*
  * Read and allocate one line from file,
  * the caller has to free the result
@@ -182,6 +250,11 @@ char* scandev(DIR *dir, dev_t comparedev)
                        continue;
                if ((size_t)snprintf(path, sizeof(path), "/dev/%s", dent->d_name) >= sizeof(path))
                        continue;
+#ifdef USE_SULOGIN_EMERGENCY_MOUNT
+               if (emergency_flags & MNT_DEVTMPFS)
+                       mknod(path, S_IFCHR|S_IRUSR|S_IWUSR, comparedev);
+#endif
+
                name = canonicalize_path(path);
                break;
        }