Annotation of /tags/mkinitrd-6_1_11/busybox/libbb/get_console.c
Parent Directory | Revision Log
Revision 928 -
(hide annotations)
(download)
Wed Oct 28 13:31:19 2009 UTC (14 years, 11 months ago) by niro
File MIME type: text/plain
File size: 1691 byte(s)
Wed Oct 28 13:31:19 2009 UTC (14 years, 11 months ago) by niro
File MIME type: text/plain
File size: 1691 byte(s)
tagged 'mkinitrd-6_1_11'
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) many different people. If you wrote this, please | ||
6 | * acknowledge your work. | ||
7 | * | ||
8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
9 | */ | ||
10 | |||
11 | #include "libbb.h" | ||
12 | |||
13 | /* From <linux/kd.h> */ | ||
14 | enum { KDGKBTYPE = 0x4B33 }; /* get keyboard type */ | ||
15 | |||
16 | static int open_a_console(const char *fnam) | ||
17 | { | ||
18 | int fd; | ||
19 | |||
20 | /* try read-write */ | ||
21 | fd = open(fnam, O_RDWR); | ||
22 | |||
23 | /* if failed, try read-only */ | ||
24 | if (fd < 0 && errno == EACCES) | ||
25 | fd = open(fnam, O_RDONLY); | ||
26 | |||
27 | /* if failed, try write-only */ | ||
28 | if (fd < 0 && errno == EACCES) | ||
29 | fd = open(fnam, O_WRONLY); | ||
30 | |||
31 | return fd; | ||
32 | } | ||
33 | |||
34 | /* | ||
35 | * Get an fd for use with kbd/console ioctls. | ||
36 | * We try several things because opening /dev/console will fail | ||
37 | * if someone else used X (which does a chown on /dev/console). | ||
38 | */ | ||
39 | niro | 816 | int FAST_FUNC get_console_fd_or_die(void) |
40 | { | ||
41 | static const char *const console_names[] = { | ||
42 | DEV_CONSOLE, CURRENT_VC, CURRENT_TTY | ||
43 | }; | ||
44 | niro | 532 | |
45 | int fd; | ||
46 | |||
47 | for (fd = 2; fd >= 0; fd--) { | ||
48 | int fd4name; | ||
49 | niro | 816 | int choice_fd; |
50 | niro | 532 | char arg; |
51 | |||
52 | niro | 816 | fd4name = open_a_console(console_names[fd]); |
53 | chk_std: | ||
54 | choice_fd = (fd4name >= 0 ? fd4name : fd); | ||
55 | niro | 532 | |
56 | arg = 0; | ||
57 | niro | 816 | if (ioctl(choice_fd, KDGKBTYPE, &arg) == 0) |
58 | return choice_fd; | ||
59 | if (fd4name >= 0) { | ||
60 | niro | 532 | close(fd4name); |
61 | fd4name = -1; | ||
62 | goto chk_std; | ||
63 | } | ||
64 | } | ||
65 | |||
66 | niro | 816 | bb_error_msg_and_die("can't open console"); |
67 | /*return fd; - total failure */ | ||
68 | niro | 532 | } |
69 | niro | 816 | |
70 | /* From <linux/vt.h> */ | ||
71 | enum { | ||
72 | VT_ACTIVATE = 0x5606, /* make vt active */ | ||
73 | VT_WAITACTIVE = 0x5607 /* wait for vt active */ | ||
74 | }; | ||
75 | |||
76 | void FAST_FUNC console_make_active(int fd, const int vt_num) | ||
77 | { | ||
78 | xioctl(fd, VT_ACTIVATE, (void *)(ptrdiff_t)vt_num); | ||
79 | xioctl(fd, VT_WAITACTIVE, (void *)(ptrdiff_t)vt_num); | ||
80 | } |