1 /* vi: set sw=4 ts=4: */
5 * Copyright (c) 2007 Denys Vlasenko <vda.linux@googlemail.com>
9 //applet:IF_CTTYHACK(APPLET(cttyhack, _BB_DIR_BIN, _BB_SUID_DROP))
11 //kbuild:lib-$(CONFIG_CTTYHACK) += cttyhack.o
13 //config:config CTTYHACK
14 //config: bool "cttyhack"
17 //config: One common problem reported on the mailing list is "can't access tty;
18 //config: job control turned off" error message which typically appears when
19 //config: one tries to use shell with stdin/stdout opened to /dev/console.
20 //config: This device is special - it cannot be a controlling tty.
22 //config: Proper solution is to use correct device instead of /dev/console.
24 //config: cttyhack provides "quick and dirty" solution to this problem.
25 //config: It analyzes stdin with various ioctls, trying to determine whether
26 //config: it is a /dev/ttyN or /dev/ttySN (virtual terminal or serial line).
27 //config: If it detects one, it closes stdin/out/err and reopens that device.
28 //config: Then it executes given program. Opening the device will make
29 //config: that device a controlling tty. This may require cttyhack
30 //config: to be a session leader.
32 //config: Example for /etc/inittab (for busybox init):
34 //config: ::respawn:/bin/cttyhack /bin/sh
36 //config: Starting an interactive shell from boot shell script:
38 //config: setsid cttyhack sh
40 //config: Giving controlling tty to shell running with PID 1:
42 //config: # exec cttyhack sh
44 //config: Without cttyhack, you need to know exact tty name,
45 //config: and do something like this:
47 //config: # exec setsid sh -c 'exec sh </dev/tty1 >/dev/tty1 2>&1'
50 //usage:#define cttyhack_trivial_usage
52 //usage:#define cttyhack_full_usage "\n\n"
53 //usage: "Give PROG a controlling tty if possible."
54 //usage: "\nExample for /etc/inittab (for busybox init):"
55 //usage: "\n ::respawn:/bin/cttyhack /bin/sh"
56 //usage: "\nGiving controlling tty to shell running with PID 1:"
57 //usage: "\n $ exec cttyhack sh"
58 //usage: "\nStarting interactive shell from boot shell script:"
59 //usage: "\n setsid cttyhack sh"
61 /* From <linux/vt.h> */
63 unsigned short v_active; /* active vt */
64 unsigned short v_signal; /* signal to send */
65 unsigned short v_state; /* vt bitmask */
67 enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
69 /* From <linux/serial.h> */
70 struct serial_struct {
79 unsigned short close_delay;
81 char reserved_char[1];
83 unsigned short closing_wait; /* time to wait before closing */
84 unsigned short closing_wait2; /* no longer used... */
85 unsigned char *iomem_base;
86 unsigned short iomem_reg_shift;
87 unsigned int port_high;
88 unsigned long iomap_base; /* cookie passed into ioremap */
92 int cttyhack_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
93 int cttyhack_main(int argc UNUSED_PARAM, char **argv)
96 char console[sizeof(int)*3 + 16];
99 struct serial_struct sr;
100 char paranoia[sizeof(struct serial_struct) * 3];
107 strcpy(console, "/dev/tty");
108 fd = open(console, O_RDWR);
110 /* We already have ctty, nothing to do */
113 /* We don't have ctty (or don't have "/dev/tty" node...) */
114 if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) {
115 /* this is a serial console */
116 sprintf(console + 8, "S%d", u.sr.line);
117 } else if (ioctl(0, VT_GETSTATE, &u.vt) == 0) {
118 /* this is linux virtual tty */
119 sprintf(console + 8, "S%d" + 1, u.vt.v_active);
122 fd = xopen(console, O_RDWR);
123 //bb_error_msg("switching to '%s'", console);
129 /* Some other session may have it as ctty,
130 * steal it from them:
132 ioctl(0, TIOCSCTTY, 1);
136 BB_EXECVP_or_die(argv);