Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +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 | |
| 32 | |
| 33 | |
| 34 | /* From <linux/kd.h> */ |
| 35 | static const int KDGKBTYPE = 0x4B33; /* get keyboard type */ |
| 36 | static const int KB_84 = 0x01; |
| 37 | static const int KB_101 = 0x02; /* this is what we always answer */ |
| 38 | |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 39 | static int is_a_console(int fd) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 40 | { |
| 41 | char arg; |
| 42 | |
| 43 | arg = 0; |
| 44 | return (ioctl(fd, KDGKBTYPE, &arg) == 0 |
| 45 | && ((arg == KB_101) || (arg == KB_84))); |
| 46 | } |
| 47 | |
| 48 | static int open_a_console(char *fnam) |
| 49 | { |
| 50 | int fd; |
| 51 | |
| 52 | /* try read-only */ |
| 53 | fd = open(fnam, O_RDWR); |
| 54 | |
| 55 | /* if failed, try read-only */ |
| 56 | if (fd < 0 && errno == EACCES) |
| 57 | fd = open(fnam, O_RDONLY); |
| 58 | |
| 59 | /* if failed, try write-only */ |
| 60 | if (fd < 0 && errno == EACCES) |
| 61 | fd = open(fnam, O_WRONLY); |
| 62 | |
| 63 | /* if failed, fail */ |
| 64 | if (fd < 0) |
| 65 | return -1; |
| 66 | |
| 67 | /* if not a console, fail */ |
| 68 | if (!is_a_console(fd)) { |
| 69 | close(fd); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | /* success */ |
| 74 | return fd; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Get an fd for use with kbd/console ioctls. |
| 79 | * We try several things because opening /dev/console will fail |
| 80 | * if someone else used X (which does a chown on /dev/console). |
| 81 | * |
| 82 | * if tty_name is non-NULL, try this one instead. |
| 83 | */ |
| 84 | |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 85 | int get_console_fd(void) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 86 | { |
| 87 | int fd; |
| 88 | |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 89 | if (-1 == (fd = open_a_console("/dev/console"))) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 90 | return -1; |
| 91 | else |
| 92 | return fd; |
Matt Kraai | 439e3df | 2001-07-23 14:52:08 +0000 | [diff] [blame] | 93 | fd = open_a_console(CURRENT_TTY); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 94 | if (fd >= 0) |
| 95 | return fd; |
| 96 | |
Matt Kraai | 439e3df | 2001-07-23 14:52:08 +0000 | [diff] [blame] | 97 | fd = open_a_console(CURRENT_VC); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 98 | if (fd >= 0) |
| 99 | return fd; |
| 100 | |
Matt Kraai | 439e3df | 2001-07-23 14:52:08 +0000 | [diff] [blame] | 101 | fd = open_a_console(CONSOLE_DEV); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 102 | if (fd >= 0) |
| 103 | return fd; |
| 104 | |
| 105 | for (fd = 0; fd < 3; fd++) |
| 106 | if (is_a_console(fd)) |
| 107 | return fd; |
| 108 | |
Matt Kraai | 439e3df | 2001-07-23 14:52:08 +0000 | [diff] [blame] | 109 | error_msg("Couldn't get a file descriptor referring to the console"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 110 | return -1; /* total failure */ |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /* END CODE */ |
| 115 | /* |
| 116 | Local Variables: |
| 117 | c-file-style: "linux" |
| 118 | c-basic-offset: 4 |
| 119 | tab-width: 4 |
| 120 | End: |
| 121 | */ |