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 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame^] | 11 | //#include <sys/ioctl.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
| 13 | |
| 14 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 15 | /* From <linux/kd.h> */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 16 | enum { KDGKBTYPE = 0x4B33 }; /* get keyboard type */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 18 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 19 | static int open_a_console(const char *fnam) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | { |
| 21 | int fd; |
| 22 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 23 | /* try read-write */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 24 | fd = open(fnam, O_RDWR); |
| 25 | |
| 26 | /* if failed, try read-only */ |
| 27 | if (fd < 0 && errno == EACCES) |
| 28 | fd = open(fnam, O_RDONLY); |
| 29 | |
| 30 | /* if failed, try write-only */ |
| 31 | if (fd < 0 && errno == EACCES) |
| 32 | fd = open(fnam, O_WRONLY); |
| 33 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 34 | return fd; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Get an fd for use with kbd/console ioctls. |
| 39 | * We try several things because opening /dev/console will fail |
| 40 | * if someone else used X (which does a chown on /dev/console). |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 41 | */ |
| 42 | |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 43 | int get_console_fd(void) |
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 | static const char *const console_names[] = { |
| 46 | DEV_CONSOLE, CURRENT_VC, CURRENT_TTY |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 47 | }; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | ec27feb | 2007-02-17 15:52:02 +0000 | [diff] [blame] | 49 | int fd; |
| 50 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 51 | for (fd = 2; fd >= 0; fd--) { |
| 52 | int fd4name; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 53 | int choice_fd; |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 54 | char arg; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | ec27feb | 2007-02-17 15:52:02 +0000 | [diff] [blame] | 56 | fd4name = open_a_console(console_names[fd]); |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 57 | chk_std: |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 58 | choice_fd = (fd4name >= 0 ? fd4name : fd); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 59 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 60 | arg = 0; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 61 | if (ioctl(choice_fd, KDGKBTYPE, &arg) == 0) |
| 62 | return choice_fd; |
| 63 | if (fd4name >= 0) { |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 64 | close(fd4name); |
| 65 | fd4name = -1; |
| 66 | goto chk_std; |
| 67 | } |
| 68 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | a959588 | 2006-09-29 21:30:43 +0000 | [diff] [blame] | 70 | bb_error_msg("cannot get file descriptor referring to console"); |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 71 | return fd; /* total failure */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 72 | } |