blob: b129518238608d9f1e28ce567fa640783a1d5387 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Glenn L McGrath005f83a2003-09-01 08:53:32 +00005 * Copyright (C) many different people. If you wrote this, please
6 * acknowledge your work.
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
10
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000011//#include <sys/ioctl.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000012#include "libbb.h"
13
14
Eric Andersenc7bda1c2004-03-15 08:29:22 +000015/* From <linux/kd.h> */
Rob Landleybc68cd12006-03-10 19:22:06 +000016enum { KDGKBTYPE = 0x4B33 }; /* get keyboard type */
Eric Andersenaad1a882001-03-16 22:47:14 +000017
Eric Andersenaad1a882001-03-16 22:47:14 +000018
Glenn L McGrath005f83a2003-09-01 08:53:32 +000019static int open_a_console(const char *fnam)
Eric Andersenaad1a882001-03-16 22:47:14 +000020{
21 int fd;
22
Glenn L McGrath005f83a2003-09-01 08:53:32 +000023 /* try read-write */
Eric Andersenaad1a882001-03-16 22:47:14 +000024 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 Andersenaad1a882001-03-16 22:47:14 +000034 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 Andersenaad1a882001-03-16 22:47:14 +000041 */
42
Eric Andersenc38678d2002-09-16 06:22:25 +000043int get_console_fd(void)
Eric Andersenaad1a882001-03-16 22:47:14 +000044{
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000045 static const char *const console_names[] = {
46 DEV_CONSOLE, CURRENT_VC, CURRENT_TTY
Glenn L McGrath005f83a2003-09-01 08:53:32 +000047 };
Eric Andersenaad1a882001-03-16 22:47:14 +000048
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000049 int fd;
50
Glenn L McGrath005f83a2003-09-01 08:53:32 +000051 for (fd = 2; fd >= 0; fd--) {
52 int fd4name;
Denis Vlasenko51742f42007-04-12 00:32:05 +000053 int choice_fd;
Glenn L McGrath005f83a2003-09-01 08:53:32 +000054 char arg;
Eric Andersenaad1a882001-03-16 22:47:14 +000055
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000056 fd4name = open_a_console(console_names[fd]);
Denis Vlasenko06af2162007-02-03 17:28:39 +000057 chk_std:
Denis Vlasenko51742f42007-04-12 00:32:05 +000058 choice_fd = (fd4name >= 0 ? fd4name : fd);
Eric Andersenaad1a882001-03-16 22:47:14 +000059
Glenn L McGrath005f83a2003-09-01 08:53:32 +000060 arg = 0;
Denis Vlasenko51742f42007-04-12 00:32:05 +000061 if (ioctl(choice_fd, KDGKBTYPE, &arg) == 0)
62 return choice_fd;
63 if (fd4name >= 0) {
Glenn L McGrath005f83a2003-09-01 08:53:32 +000064 close(fd4name);
65 fd4name = -1;
66 goto chk_std;
67 }
68 }
Eric Andersenaad1a882001-03-16 22:47:14 +000069
Denis Vlasenkoa9595882006-09-29 21:30:43 +000070 bb_error_msg("cannot get file descriptor referring to console");
Glenn L McGrath005f83a2003-09-01 08:53:32 +000071 return fd; /* total failure */
Eric Andersenaad1a882001-03-16 22:47:14 +000072}