mark Linux-specific configuration options
[platform/upstream/busybox.git] / shell / cttyhack.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under GPLv2
4  *
5  * Copyright (c) 2007 Denys Vlasenko <vda.linux@googlemail.com>
6  */
7 #include "libbb.h"
8
9 //applet:IF_CTTYHACK(APPLET(cttyhack, _BB_DIR_BIN, _BB_SUID_DROP))
10
11 //kbuild:lib-$(CONFIG_CTTYHACK) += cttyhack.o
12
13 //config:config CTTYHACK
14 //config:       bool "cttyhack"
15 //config:       default y
16 //config:       depends on PLATFORM_LINUX
17 //config:       help
18 //config:         One common problem reported on the mailing list is "can't access tty;
19 //config:         job control turned off" error message which typically appears when
20 //config:         one tries to use shell with stdin/stdout opened to /dev/console.
21 //config:         This device is special - it cannot be a controlling tty.
22 //config:
23 //config:         Proper solution is to use correct device instead of /dev/console.
24 //config:
25 //config:         cttyhack provides "quick and dirty" solution to this problem.
26 //config:         It analyzes stdin with various ioctls, trying to determine whether
27 //config:         it is a /dev/ttyN or /dev/ttySN (virtual terminal or serial line).
28 //config:         If it detects one, it closes stdin/out/err and reopens that device.
29 //config:         Then it executes given program. Opening the device will make
30 //config:         that device a controlling tty. This may require cttyhack
31 //config:         to be a session leader.
32 //config:
33 //config:         Example for /etc/inittab (for busybox init):
34 //config:
35 //config:         ::respawn:/bin/cttyhack /bin/sh
36 //config:
37 //config:         Starting an interactive shell from boot shell script:
38 //config:
39 //config:         setsid cttyhack sh
40 //config:
41 //config:         Giving controlling tty to shell running with PID 1:
42 //config:
43 //config:         # exec cttyhack sh
44 //config:
45 //config:         Without cttyhack, you need to know exact tty name,
46 //config:         and do something like this:
47 //config:
48 //config:         # exec setsid sh -c 'exec sh </dev/tty1 >/dev/tty1 2>&1'
49 //config:
50
51 //usage:#define cttyhack_trivial_usage
52 //usage:       "PROG ARGS"
53 //usage:#define cttyhack_full_usage "\n\n"
54 //usage:       "Give PROG a controlling tty if possible."
55 //usage:     "\nExample for /etc/inittab (for busybox init):"
56 //usage:     "\n        ::respawn:/bin/cttyhack /bin/sh"
57 //usage:     "\nGiving controlling tty to shell running with PID 1:"
58 //usage:     "\n        $ exec cttyhack sh"
59 //usage:     "\nStarting interactive shell from boot shell script:"
60 //usage:     "\n        setsid cttyhack sh"
61
62 /* From <linux/vt.h> */
63 struct vt_stat {
64         unsigned short v_active;        /* active vt */
65         unsigned short v_signal;        /* signal to send */
66         unsigned short v_state;         /* vt bitmask */
67 };
68 enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
69
70 /* From <linux/serial.h> */
71 struct serial_struct {
72         int     type;
73         int     line;
74         unsigned int    port;
75         int     irq;
76         int     flags;
77         int     xmit_fifo_size;
78         int     custom_divisor;
79         int     baud_base;
80         unsigned short  close_delay;
81         char    io_type;
82         char    reserved_char[1];
83         int     hub6;
84         unsigned short  closing_wait;   /* time to wait before closing */
85         unsigned short  closing_wait2;  /* no longer used... */
86         unsigned char   *iomem_base;
87         unsigned short  iomem_reg_shift;
88         unsigned int    port_high;
89         unsigned long   iomap_base;     /* cookie passed into ioremap */
90         int     reserved[1];
91 };
92
93 int cttyhack_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
94 int cttyhack_main(int argc UNUSED_PARAM, char **argv)
95 {
96         int fd;
97         char console[sizeof(int)*3 + 16];
98         union {
99                 struct vt_stat vt;
100                 struct serial_struct sr;
101                 char paranoia[sizeof(struct serial_struct) * 3];
102         } u;
103
104         if (!*++argv) {
105                 bb_show_usage();
106         }
107
108         strcpy(console, "/dev/tty");
109         fd = open(console, O_RDWR);
110         if (fd >= 0) {
111                 /* We already have ctty, nothing to do */
112                 close(fd);
113         } else {
114                 /* We don't have ctty (or don't have "/dev/tty" node...) */
115                 if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) {
116                         /* this is a serial console */
117                         sprintf(console + 8, "S%d", u.sr.line);
118                 } else if (ioctl(0, VT_GETSTATE, &u.vt) == 0) {
119                         /* this is linux virtual tty */
120                         sprintf(console + 8, "S%d" + 1, u.vt.v_active);
121                 }
122                 if (console[8]) {
123                         fd = xopen(console, O_RDWR);
124                         //bb_error_msg("switching to '%s'", console);
125                         dup2(fd, 0);
126                         dup2(fd, 1);
127                         dup2(fd, 2);
128                         while (fd > 2)
129                                 close(fd--);
130                         /* Some other session may have it as ctty,
131                          * steal it from them:
132                          */
133                         ioctl(0, TIOCSCTTY, 1);
134                 }
135         }
136
137         BB_EXECVP_or_die(argv);
138 }