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 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <errno.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <unistd.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | #include "libbb.h" |
| 29 | |
| 30 | |
| 31 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 32 | /* From <linux/kd.h> */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 33 | enum { KDGKBTYPE = 0x4B33 }; /* get keyboard type */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 34 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 35 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 36 | static int open_a_console(const char *fnam) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 37 | { |
| 38 | int fd; |
| 39 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 40 | /* try read-write */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 41 | fd = open(fnam, O_RDWR); |
| 42 | |
| 43 | /* if failed, try read-only */ |
| 44 | if (fd < 0 && errno == EACCES) |
| 45 | fd = open(fnam, O_RDONLY); |
| 46 | |
| 47 | /* if failed, try write-only */ |
| 48 | if (fd < 0 && errno == EACCES) |
| 49 | fd = open(fnam, O_WRONLY); |
| 50 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 51 | return fd; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Get an fd for use with kbd/console ioctls. |
| 56 | * We try several things because opening /dev/console will fail |
| 57 | * if someone else used X (which does a chown on /dev/console). |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 58 | */ |
| 59 | |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 60 | int get_console_fd(void) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 61 | { |
| 62 | int fd; |
| 63 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 64 | static const char * const choise_console_names[] = { |
| 65 | CONSOLE_DEV, CURRENT_VC, CURRENT_TTY |
| 66 | }; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 67 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 68 | for (fd = 2; fd >= 0; fd--) { |
| 69 | int fd4name; |
| 70 | int choise_fd; |
| 71 | char arg; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 72 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 73 | fd4name = open_a_console(choise_console_names[fd]); |
| 74 | chk_std: |
| 75 | choise_fd = fd4name >= 0 ? fd4name : fd; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 76 | |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 77 | arg = 0; |
| 78 | if (ioctl(choise_fd, KDGKBTYPE, &arg) == 0) |
| 79 | return choise_fd; |
| 80 | if(fd4name >= 0) { |
| 81 | close(fd4name); |
| 82 | fd4name = -1; |
| 83 | goto chk_std; |
| 84 | } |
| 85 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 86 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | bb_error_msg("Couldn't get a file descriptor referring to the console"); |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 88 | return fd; /* total failure */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
| 92 | /* END CODE */ |
| 93 | /* |
| 94 | Local Variables: |
| 95 | c-file-style: "linux" |
| 96 | c-basic-offset: 4 |
| 97 | tab-width: 4 |
| 98 | End: |
| 99 | */ |