d042afa2b52f3ba15c1102567553e07a028f3dbf
[platform/upstream/busybox.git] / libbb / get_console.c
1 /* 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
17 static int open_a_console(const char *fnam)
18 {
19         int fd;
20
21         /* try read-write */
22         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
32         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).
39  */
40
41 int FAST_FUNC get_console_fd(void)
42 {
43         static const char *const console_names[] = {
44                 DEV_CONSOLE, CURRENT_VC, CURRENT_TTY
45         };
46
47         int fd;
48
49         for (fd = 2; fd >= 0; fd--) {
50                 int fd4name;
51                 int choice_fd;
52                 char arg;
53
54                 fd4name = open_a_console(console_names[fd]);
55  chk_std:
56                 choice_fd = (fd4name >= 0 ? fd4name : fd);
57
58                 arg = 0;
59                 if (ioctl(choice_fd, KDGKBTYPE, &arg) == 0)
60                         return choice_fd;
61                 if (fd4name >= 0) {
62                         close(fd4name);
63                         fd4name = -1;
64                         goto chk_std;
65                 }
66         }
67
68         bb_error_msg("can't open console");
69         return fd;                      /* total failure */
70 }
71
72 /* From <linux/vt.h> */
73 enum {
74         VT_ACTIVATE = 0x5606,   /* make vt active */
75         VT_WAITACTIVE = 0x5607  /* wait for vt active */
76 };
77
78 void FAST_FUNC console_make_active(int fd, const int vt_num)
79 {
80         xioctl(fd, VT_ACTIVATE, (void *)(ptrdiff_t)vt_num);
81         xioctl(fd, VT_WAITACTIVE, (void *)(ptrdiff_t)vt_num);
82 }