kbd_mode: try harder to find console device if -C TTY is not given
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 6 Aug 2017 10:17:46 +0000 (12:17 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 6 Aug 2017 10:17:46 +0000 (12:17 +0200)
Was (under X):
$ ./busybox_old kbd_mode
kbd_mode: ioctl 0x4b44 failed: Inappropriate ioctl for device
Now:
$ ./busybox kbd_mode
The keyboard is in off mode

function                                             old     new   delta
kbd_mode_main                                        166     174      +8
packed_usage                                       31782   31764     -18
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 8/-18)             Total: -10 bytes
   text    data     bss     dec     hex filename
 915757     485    6880  923122   e15f2 busybox_old
 915747     485    6880  923112   e15e8 busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
console-tools/kbd_mode.c
libbb/get_console.c

index da31af28d33cde71bf6b49eadc104ad1224ed3ce..b0b5946143bb0fe5663b78ab46497d5ea0678374 100644 (file)
@@ -22,7 +22,7 @@
 //usage:#define kbd_mode_trivial_usage
 //usage:       "[-a|k|s|u] [-C TTY]"
 //usage:#define kbd_mode_full_usage "\n\n"
-//usage:       "Report or set the keyboard mode\n"
+//usage:       "Report or set VT console keyboard mode\n"
 //usage:     "\n       -a      Default (ASCII)"
 //usage:     "\n       -k      Medium-raw (keycode)"
 //usage:     "\n       -s      Raw (scancode)"
@@ -43,15 +43,20 @@ int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
        };
        int fd;
        unsigned opt;
-//TODO? kbd-2.0.3 without -C tries in sequence:
-//fd#0, /dev/tty, /dev/tty0.
-//Also, it checks KDGKBTYPE before doing KDGKBMODE
-//maybe we can use get_console_fd_or_die()?
-       const char *tty_name = CURRENT_TTY;
+       const char *tty_name;
 
        opt = getopt32(argv, "sakuC:", &tty_name);
-       fd = xopen_nonblocking(tty_name);
-       opt &= 0xf; /* clear -C bit, see (*) */
+       if (opt & 0x10) {
+               opt &= 0xf; /* clear -C bit, see (*) */
+               fd = xopen_nonblocking(tty_name);
+       } else {
+               /* kbd-2.0.3 tries in sequence:
+                * fd#0, /dev/tty, /dev/tty0.
+                * get_console_fd_or_die: /dev/console, /dev/tty0, /dev/tty.
+                * kbd-2.0.3 checks KDGKBTYPE, get_console_fd_or_die checks too.
+                */
+               fd = get_console_fd_or_die();
+       }
 
        if (!opt) { /* print current setting */
                const char *mode = "unknown";
@@ -79,6 +84,7 @@ int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
                 * #define K_OFF           0x04
                 * (looks like "-ak" together would cause the same effect as -u)
                 */
+               opt &= 0xf; /* clear -C bit */
                opt = opt & UNICODE ? 3 : opt >> 1;
                /* double cast prevents warnings about widening conversion */
                xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
index 9b6407bd022b0699d48368a2417c430650edf6b1..96b339ca70a7a22121c67096838928cb31924097 100644 (file)
@@ -64,7 +64,6 @@ int FAST_FUNC get_console_fd_or_die(void)
        }
 
        bb_error_msg_and_die("can't open console");
-       /*return fd; - total failure */
 }
 
 /* From <linux/vt.h> */