2001-03-16 22:47:14 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2003-09-01 08:53:32 +00:00
|
|
|
* Copyright (C) many different people. If you wrote this, please
|
|
|
|
* acknowledge your work.
|
2001-03-16 22:47:14 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2001-03-16 22:47:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2004-03-15 08:29:22 +00:00
|
|
|
/* From <linux/kd.h> */
|
2006-03-10 19:22:06 +00:00
|
|
|
enum { KDGKBTYPE = 0x4B33 }; /* get keyboard type */
|
2001-03-16 22:47:14 +00:00
|
|
|
|
2003-09-01 08:53:32 +00:00
|
|
|
static int open_a_console(const char *fnam)
|
2001-03-16 22:47:14 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2003-09-01 08:53:32 +00:00
|
|
|
/* try read-write */
|
2001-03-16 22:47:14 +00:00
|
|
|
fd = open(fnam, O_RDWR);
|
|
|
|
|
|
|
|
/* if failed, try read-only */
|
|
|
|
if (fd < 0 && errno == EACCES)
|
|
|
|
fd = open(fnam, O_RDONLY);
|
|
|
|
|
|
|
|
/* if failed, try write-only */
|
|
|
|
if (fd < 0 && errno == EACCES)
|
|
|
|
fd = open(fnam, O_WRONLY);
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*/
|
2008-08-05 23:32:27 +00:00
|
|
|
int FAST_FUNC get_console_fd_or_die(void)
|
2001-03-16 22:47:14 +00:00
|
|
|
{
|
2007-02-17 15:52:02 +00:00
|
|
|
static const char *const console_names[] = {
|
|
|
|
DEV_CONSOLE, CURRENT_VC, CURRENT_TTY
|
2003-09-01 08:53:32 +00:00
|
|
|
};
|
|
|
|
|
2007-02-17 15:52:02 +00:00
|
|
|
int fd;
|
|
|
|
|
2003-09-01 08:53:32 +00:00
|
|
|
for (fd = 2; fd >= 0; fd--) {
|
|
|
|
int fd4name;
|
2007-04-12 00:32:05 +00:00
|
|
|
int choice_fd;
|
2003-09-01 08:53:32 +00:00
|
|
|
char arg;
|
|
|
|
|
2007-02-17 15:52:02 +00:00
|
|
|
fd4name = open_a_console(console_names[fd]);
|
2007-02-03 17:28:39 +00:00
|
|
|
chk_std:
|
2007-04-12 00:32:05 +00:00
|
|
|
choice_fd = (fd4name >= 0 ? fd4name : fd);
|
2003-09-01 08:53:32 +00:00
|
|
|
|
|
|
|
arg = 0;
|
2007-04-12 00:32:05 +00:00
|
|
|
if (ioctl(choice_fd, KDGKBTYPE, &arg) == 0)
|
|
|
|
return choice_fd;
|
|
|
|
if (fd4name >= 0) {
|
2003-09-01 08:53:32 +00:00
|
|
|
close(fd4name);
|
|
|
|
fd4name = -1;
|
|
|
|
goto chk_std;
|
|
|
|
}
|
|
|
|
}
|
2001-03-16 22:47:14 +00:00
|
|
|
|
2008-08-05 23:32:27 +00:00
|
|
|
bb_error_msg_and_die("can't open console");
|
|
|
|
/*return fd; - total failure */
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|
2008-05-19 08:18:50 +00:00
|
|
|
|
|
|
|
/* From <linux/vt.h> */
|
|
|
|
enum {
|
|
|
|
VT_ACTIVATE = 0x5606, /* make vt active */
|
|
|
|
VT_WAITACTIVE = 0x5607 /* wait for vt active */
|
|
|
|
};
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC console_make_active(int fd, const int vt_num)
|
2008-05-19 08:18:50 +00:00
|
|
|
{
|
|
|
|
xioctl(fd, VT_ACTIVATE, (void *)(ptrdiff_t)vt_num);
|
|
|
|
xioctl(fd, VT_WAITACTIVE, (void *)(ptrdiff_t)vt_num);
|
|
|
|
}
|