1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) many different people. If you wrote this, please
6 * acknowledge your work.
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
15 #include <sys/ioctl.h>
20 /* From <linux/kd.h> */
21 enum { KDGKBTYPE = 0x4B33 }; /* get keyboard type */
24 static int open_a_console(const char *fnam)
29 fd = open(fnam, O_RDWR);
31 /* if failed, try read-only */
32 if (fd < 0 && errno == EACCES)
33 fd = open(fnam, O_RDONLY);
35 /* if failed, try write-only */
36 if (fd < 0 && errno == EACCES)
37 fd = open(fnam, O_WRONLY);
43 * Get an fd for use with kbd/console ioctls.
44 * We try several things because opening /dev/console will fail
45 * if someone else used X (which does a chown on /dev/console).
48 int get_console_fd(void)
52 static const char * const choise_console_names[] = {
53 CONSOLE_DEV, CURRENT_VC, CURRENT_TTY
56 for (fd = 2; fd >= 0; fd--) {
61 fd4name = open_a_console(choise_console_names[fd]);
63 choise_fd = fd4name >= 0 ? fd4name : fd;
66 if (ioctl(choise_fd, KDGKBTYPE, &arg) == 0)
75 bb_error_msg("cannot get file descriptor referring to console");
76 return fd; /* total failure */