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 source tree.
12 /* From <linux/kd.h> */
13 enum { KDGKBTYPE = 0x4B33 }; /* get keyboard type */
15 static int open_a_console(const char *fnam)
20 fd = open(fnam, O_RDWR);
22 /* if failed, try read-only */
23 if (fd < 0 && errno == EACCES)
24 fd = open(fnam, O_RDONLY);
26 /* if failed, try write-only */
27 if (fd < 0 && errno == EACCES)
28 fd = open(fnam, O_WRONLY);
34 * Get an fd for use with kbd/console ioctls.
35 * We try several things because opening /dev/console will fail
36 * if someone else used X (which does a chown on /dev/console).
38 int FAST_FUNC get_console_fd_or_die(void)
40 static const char *const console_names[] = {
41 DEV_CONSOLE, CURRENT_VC, CURRENT_TTY
46 for (fd = 2; fd >= 0; fd--) {
51 fd4name = open_a_console(console_names[fd]);
53 choice_fd = (fd4name >= 0 ? fd4name : fd);
56 if (ioctl(choice_fd, KDGKBTYPE, &arg) == 0)
65 bb_error_msg_and_die("can't open console");
68 /* From <linux/vt.h> */
70 VT_ACTIVATE = 0x5606, /* make vt active */
71 VT_WAITACTIVE = 0x5607 /* wait for vt active */
74 void FAST_FUNC console_make_active(int fd, const int vt_num)
76 xioctl(fd, VT_ACTIVATE, (void *)(ptrdiff_t)vt_num);
77 xioctl(fd, VT_WAITACTIVE, (void *)(ptrdiff_t)vt_num);