Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 5 | * Copyright (C) many different people. If you wrote this, please |
| 6 | * acknowledge your work. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
| 12 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 13 | /* From <linux/kd.h> */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 14 | enum { KDGKBTYPE = 0x4B33 }; /* get keyboard type */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 15 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 16 | static int open_a_console(const char *fnam) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | { |
| 18 | int fd; |
| 19 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 20 | /* try read-write */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 21 | fd = open(fnam, O_RDWR); |
| 22 | |
| 23 | /* if failed, try read-only */ |
| 24 | if (fd < 0 && errno == EACCES) |
| 25 | fd = open(fnam, O_RDONLY); |
| 26 | |
| 27 | /* if failed, try write-only */ |
| 28 | if (fd < 0 && errno == EACCES) |
| 29 | fd = open(fnam, O_WRONLY); |
| 30 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 31 | return fd; |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | * Get an fd for use with kbd/console ioctls. |
| 36 | * We try several things because opening /dev/console will fail |
| 37 | * if someone else used X (which does a chown on /dev/console). |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 38 | */ |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 39 | int FAST_FUNC get_console_fd_or_die(void) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 40 | { |
Denis Vlasenko | ec27feb | 2007-02-17 15:52:02 +0000 | [diff] [blame] | 41 | static const char *const console_names[] = { |
| 42 | DEV_CONSOLE, CURRENT_VC, CURRENT_TTY |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 43 | }; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 44 | |
Denis Vlasenko | ec27feb | 2007-02-17 15:52:02 +0000 | [diff] [blame] | 45 | int fd; |
| 46 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 47 | for (fd = 2; fd >= 0; fd--) { |
| 48 | int fd4name; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 49 | int choice_fd; |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 50 | char arg; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | ec27feb | 2007-02-17 15:52:02 +0000 | [diff] [blame] | 52 | fd4name = open_a_console(console_names[fd]); |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 53 | chk_std: |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 54 | choice_fd = (fd4name >= 0 ? fd4name : fd); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 55 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 56 | arg = 0; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 57 | if (ioctl(choice_fd, KDGKBTYPE, &arg) == 0) |
| 58 | return choice_fd; |
| 59 | if (fd4name >= 0) { |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 60 | close(fd4name); |
| 61 | fd4name = -1; |
| 62 | goto chk_std; |
| 63 | } |
| 64 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 66 | bb_error_msg_and_die("can't open console"); |
| 67 | /*return fd; - total failure */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 68 | } |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 69 | |
| 70 | /* From <linux/vt.h> */ |
| 71 | enum { |
| 72 | VT_ACTIVATE = 0x5606, /* make vt active */ |
| 73 | VT_WAITACTIVE = 0x5607 /* wait for vt active */ |
| 74 | }; |
| 75 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 76 | void FAST_FUNC console_make_active(int fd, const int vt_num) |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 77 | { |
| 78 | xioctl(fd, VT_ACTIVATE, (void *)(ptrdiff_t)vt_num); |
| 79 | xioctl(fd, VT_WAITACTIVE, (void *)(ptrdiff_t)vt_num); |
| 80 | } |