blob: 4be9adc84166ef1688099e64891e7c515f30d157 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <sys/ioctl.h>
33#include "libbb.h"
34
35
36
37
38
39/* From <linux/kd.h> */
40static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
41static const int KB_84 = 0x01;
42static const int KB_101 = 0x02; /* this is what we always answer */
43
44int is_a_console(int fd)
45{
46 char arg;
47
48 arg = 0;
49 return (ioctl(fd, KDGKBTYPE, &arg) == 0
50 && ((arg == KB_101) || (arg == KB_84)));
51}
52
53static int open_a_console(char *fnam)
54{
55 int fd;
56
57 /* try read-only */
58 fd = open(fnam, O_RDWR);
59
60 /* if failed, try read-only */
61 if (fd < 0 && errno == EACCES)
62 fd = open(fnam, O_RDONLY);
63
64 /* if failed, try write-only */
65 if (fd < 0 && errno == EACCES)
66 fd = open(fnam, O_WRONLY);
67
68 /* if failed, fail */
69 if (fd < 0)
70 return -1;
71
72 /* if not a console, fail */
73 if (!is_a_console(fd)) {
74 close(fd);
75 return -1;
76 }
77
78 /* success */
79 return fd;
80}
81
82/*
83 * Get an fd for use with kbd/console ioctls.
84 * We try several things because opening /dev/console will fail
85 * if someone else used X (which does a chown on /dev/console).
86 *
87 * if tty_name is non-NULL, try this one instead.
88 */
89
90int get_console_fd(char *tty_name)
91{
92 int fd;
93
94 if (tty_name) {
95 if (-1 == (fd = open_a_console(tty_name)))
96 return -1;
97 else
98 return fd;
99 }
100
101 fd = open_a_console("/dev/tty");
102 if (fd >= 0)
103 return fd;
104
105 fd = open_a_console("/dev/tty0");
106 if (fd >= 0)
107 return fd;
108
109 fd = open_a_console("/dev/console");
110 if (fd >= 0)
111 return fd;
112
113 for (fd = 0; fd < 3; fd++)
114 if (is_a_console(fd))
115 return fd;
116
117 error_msg("Couldnt get a file descriptor referring to the console");
118 return -1; /* total failure */
119}
120
121
122/* END CODE */
123/*
124Local Variables:
125c-file-style: "linux"
126c-basic-offset: 4
127tab-width: 4
128End:
129*/