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