blob: ad56e740f7ee68ba5584c4e09096e6dcc3c509a5 [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
Eric Andersenaad1a882001-03-16 22:47:14 +000011#include "libbb.h"
12
Eric Andersenc7bda1c2004-03-15 08:29:22 +000013/* From <linux/kd.h> */
Rob Landleybc68cd12006-03-10 19:22:06 +000014enum { KDGKBTYPE = 0x4B33 }; /* get keyboard type */
Eric Andersenaad1a882001-03-16 22:47:14 +000015
Eric Andersenaad1a882001-03-16 22:47:14 +000016
Glenn L McGrath005f83a2003-09-01 08:53:32 +000017static int open_a_console(const char *fnam)
Eric Andersenaad1a882001-03-16 22:47:14 +000018{
19 int fd;
20
Glenn L McGrath005f83a2003-09-01 08:53:32 +000021 /* try read-write */
Eric Andersenaad1a882001-03-16 22:47:14 +000022 fd = open(fnam, O_RDWR);
23
24 /* if failed, try read-only */
25 if (fd < 0 && errno == EACCES)
26 fd = open(fnam, O_RDONLY);
27
28 /* if failed, try write-only */
29 if (fd < 0 && errno == EACCES)
30 fd = open(fnam, O_WRONLY);
31
Eric Andersenaad1a882001-03-16 22:47:14 +000032 return fd;
33}
34
35/*
36 * Get an fd for use with kbd/console ioctls.
37 * We try several things because opening /dev/console will fail
38 * if someone else used X (which does a chown on /dev/console).
Eric Andersenaad1a882001-03-16 22:47:14 +000039 */
40
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000041int FAST_FUNC get_console_fd_or_die(void)
Eric Andersenaad1a882001-03-16 22:47:14 +000042{
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000043 static const char *const console_names[] = {
44 DEV_CONSOLE, CURRENT_VC, CURRENT_TTY
Glenn L McGrath005f83a2003-09-01 08:53:32 +000045 };
Eric Andersenaad1a882001-03-16 22:47:14 +000046
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000047 int fd;
48
Glenn L McGrath005f83a2003-09-01 08:53:32 +000049 for (fd = 2; fd >= 0; fd--) {
50 int fd4name;
Denis Vlasenko51742f42007-04-12 00:32:05 +000051 int choice_fd;
Glenn L McGrath005f83a2003-09-01 08:53:32 +000052 char arg;
Eric Andersenaad1a882001-03-16 22:47:14 +000053
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000054 fd4name = open_a_console(console_names[fd]);
Denis Vlasenko06af2162007-02-03 17:28:39 +000055 chk_std:
Denis Vlasenko51742f42007-04-12 00:32:05 +000056 choice_fd = (fd4name >= 0 ? fd4name : fd);
Eric Andersenaad1a882001-03-16 22:47:14 +000057
Glenn L McGrath005f83a2003-09-01 08:53:32 +000058 arg = 0;
Denis Vlasenko51742f42007-04-12 00:32:05 +000059 if (ioctl(choice_fd, KDGKBTYPE, &arg) == 0)
60 return choice_fd;
61 if (fd4name >= 0) {
Glenn L McGrath005f83a2003-09-01 08:53:32 +000062 close(fd4name);
63 fd4name = -1;
64 goto chk_std;
65 }
66 }
Eric Andersenaad1a882001-03-16 22:47:14 +000067
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000068 bb_error_msg_and_die("can't open console");
69 /*return fd; - total failure */
Eric Andersenaad1a882001-03-16 22:47:14 +000070}
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000071
72/* From <linux/vt.h> */
73enum {
74 VT_ACTIVATE = 0x5606, /* make vt active */
75 VT_WAITACTIVE = 0x5607 /* wait for vt active */
76};
77
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000078void FAST_FUNC console_make_active(int fd, const int vt_num)
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000079{
80 xioctl(fd, VT_ACTIVATE, (void *)(ptrdiff_t)vt_num);
81 xioctl(fd, VT_WAITACTIVE, (void *)(ptrdiff_t)vt_num);
82}