The check for EROFS was wrong. For example, if you try to mount a filesystem
[oweals/busybox.git] / libbb / get_console.c
index 794888fa7b2520ce902d48a6cbe64a524977f6de..bfb7468a8fe606508902f2caef35819f315fb50c 100644 (file)
 
 
 
-
-
-/* From <linux/kd.h> */ 
+/* From <linux/kd.h> */
 static const int KDGKBTYPE = 0x4B33;  /* get keyboard type */
-static const int KB_84 = 0x01;
-static const int KB_101 = 0x02;    /* this is what we always answer */
 
-static int is_a_console(int fd)
-{
-       char arg;
 
-       arg = 0;
-       return (ioctl(fd, KDGKBTYPE, &arg) == 0
-                       && ((arg == KB_101) || (arg == KB_84)));
-}
-
-static int open_a_console(char *fnam)
+static int open_a_console(const char *fnam)
 {
        int fd;
 
-       /* try read-only */
+       /* try read-write */
        fd = open(fnam, O_RDWR);
 
        /* if failed, try read-only */
@@ -60,17 +48,6 @@ static int open_a_console(char *fnam)
        if (fd < 0 && errno == EACCES)
                fd = open(fnam, O_WRONLY);
 
-       /* if failed, fail */
-       if (fd < 0)
-               return -1;
-
-       /* if not a console, fail */
-       if (!is_a_console(fd)) {
-               close(fd);
-               return -1;
-       }
-
-       /* success */
        return fd;
 }
 
@@ -78,36 +55,37 @@ static int open_a_console(char *fnam)
  * Get an fd for use with kbd/console ioctls.
  * We try several things because opening /dev/console will fail
  * if someone else used X (which does a chown on /dev/console).
- *
- * if tty_name is non-NULL, try this one instead.
  */
 
 int get_console_fd(void)
 {
        int fd;
 
-       if (-1 == (fd = open_a_console("/dev/console")))
-                       return -1;
-               else
-                       return fd;
-       fd = open_a_console(CURRENT_TTY);
-       if (fd >= 0)
-               return fd;
-
-       fd = open_a_console(CURRENT_VC);
-       if (fd >= 0)
-               return fd;
-
-       fd = open_a_console(CONSOLE_DEV);
-       if (fd >= 0)
-               return fd;
-
-       for (fd = 0; fd < 3; fd++)
-               if (is_a_console(fd))
-                       return fd;
+       static const char * const choise_console_names[] = {
+               CONSOLE_DEV, CURRENT_VC, CURRENT_TTY
+       };
+
+       for (fd = 2; fd >= 0; fd--) {
+               int fd4name;
+               int choise_fd;
+               char arg;
+
+               fd4name = open_a_console(choise_console_names[fd]);
+       chk_std:
+               choise_fd = fd4name >= 0 ? fd4name : fd;
+
+               arg = 0;
+               if (ioctl(choise_fd, KDGKBTYPE, &arg) == 0)
+                       return choise_fd;
+               if(fd4name >= 0) {
+                       close(fd4name);
+                       fd4name = -1;
+                       goto chk_std;
+               }
+       }
 
-       error_msg("Couldn't get a file descriptor referring to the console");
-       return -1;                                      /* total failure */
+       bb_error_msg("Couldn't get a file descriptor referring to the console");
+       return fd;                      /* total failure */
 }