blob: 6241c76a9166108449bc2f0c6091bff64c621520 [file] [log] [blame]
Denis Vlasenko2afabe82007-12-10 07:06:04 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko2afabe82007-12-10 07:06:04 +00003 * Copyright (c) 2007 Denys Vlasenko <vda.linux@googlemail.com>
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 *
5 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko6d709972007-05-18 09:45:36 +00006 */
7#include "libbb.h"
8
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +01009//applet:IF_CTTYHACK(APPLET(cttyhack, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenko51ca7762010-07-16 17:16:40 +020010
11//kbuild:lib-$(CONFIG_CTTYHACK) += cttyhack.o
12
13//config:config CTTYHACK
14//config: bool "cttyhack"
15//config: default y
16//config: help
Kevin Cernekee064e9962011-07-13 09:26:58 +020017//config: One common problem reported on the mailing list is the "can't
18//config: access tty; job control turned off" error message, which typically
19//config: appears when one tries to use a shell with stdin/stdout on
20//config: /dev/console.
Denys Vlasenko51ca7762010-07-16 17:16:40 +020021//config: This device is special - it cannot be a controlling tty.
22//config:
Kevin Cernekee064e9962011-07-13 09:26:58 +020023//config: The proper solution is to use the correct device instead of
24//config: /dev/console.
Denys Vlasenko51ca7762010-07-16 17:16:40 +020025//config:
Kevin Cernekee064e9962011-07-13 09:26:58 +020026//config: cttyhack provides a "quick and dirty" solution to this problem.
Denys Vlasenko51ca7762010-07-16 17:16:40 +020027//config: It analyzes stdin with various ioctls, trying to determine whether
28//config: it is a /dev/ttyN or /dev/ttySN (virtual terminal or serial line).
Kevin Cernekee064e9962011-07-13 09:26:58 +020029//config: On Linux it also checks sysfs for a pointer to the active console.
30//config: If cttyhack is able to find the real console device, it closes
31//config: stdin/out/err and reopens that device.
32//config: Then it executes the given program. Opening the device will make
Denys Vlasenko51ca7762010-07-16 17:16:40 +020033//config: that device a controlling tty. This may require cttyhack
34//config: to be a session leader.
35//config:
36//config: Example for /etc/inittab (for busybox init):
37//config:
38//config: ::respawn:/bin/cttyhack /bin/sh
39//config:
40//config: Starting an interactive shell from boot shell script:
41//config:
42//config: setsid cttyhack sh
43//config:
44//config: Giving controlling tty to shell running with PID 1:
45//config:
46//config: # exec cttyhack sh
47//config:
48//config: Without cttyhack, you need to know exact tty name,
49//config: and do something like this:
50//config:
51//config: # exec setsid sh -c 'exec sh </dev/tty1 >/dev/tty1 2>&1'
52//config:
53
54//usage:#define cttyhack_trivial_usage
55//usage: "PROG ARGS"
56//usage:#define cttyhack_full_usage "\n\n"
57//usage: "Give PROG a controlling tty if possible."
58//usage: "\nExample for /etc/inittab (for busybox init):"
59//usage: "\n ::respawn:/bin/cttyhack /bin/sh"
60//usage: "\nGiving controlling tty to shell running with PID 1:"
61//usage: "\n $ exec cttyhack sh"
62//usage: "\nStarting interactive shell from boot shell script:"
63//usage: "\n setsid cttyhack sh"
64
Denys Vlasenko17662802010-07-30 17:41:35 +020065#if !defined(__linux__) && !defined(TIOCGSERIAL) && !ENABLE_WERROR
Jeremie Koenig430ba792010-07-30 06:21:21 +020066# warning cttyhack will not be able to detect a controlling tty on this system
67#endif
68
Denis Vlasenko6d709972007-05-18 09:45:36 +000069/* From <linux/vt.h> */
70struct vt_stat {
Denis Vlasenko2afabe82007-12-10 07:06:04 +000071 unsigned short v_active; /* active vt */
72 unsigned short v_signal; /* signal to send */
73 unsigned short v_state; /* vt bitmask */
Denis Vlasenko6d709972007-05-18 09:45:36 +000074};
Denis Vlasenko2afabe82007-12-10 07:06:04 +000075enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
Denis Vlasenko6d709972007-05-18 09:45:36 +000076
77/* From <linux/serial.h> */
78struct serial_struct {
79 int type;
80 int line;
81 unsigned int port;
82 int irq;
83 int flags;
84 int xmit_fifo_size;
85 int custom_divisor;
86 int baud_base;
87 unsigned short close_delay;
88 char io_type;
89 char reserved_char[1];
90 int hub6;
Denis Vlasenko2afabe82007-12-10 07:06:04 +000091 unsigned short closing_wait; /* time to wait before closing */
92 unsigned short closing_wait2; /* no longer used... */
Denis Vlasenko6d709972007-05-18 09:45:36 +000093 unsigned char *iomem_base;
94 unsigned short iomem_reg_shift;
95 unsigned int port_high;
96 unsigned long iomap_base; /* cookie passed into ioremap */
97 int reserved[1];
98};
99
Denis Vlasenko528a8b92007-12-22 19:57:28 +0000100int cttyhack_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000101int cttyhack_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko6d709972007-05-18 09:45:36 +0000102{
103 int fd;
104 char console[sizeof(int)*3 + 16];
105 union {
106 struct vt_stat vt;
107 struct serial_struct sr;
108 char paranoia[sizeof(struct serial_struct) * 3];
109 } u;
110
111 if (!*++argv) {
112 bb_show_usage();
113 }
114
115 strcpy(console, "/dev/tty");
Denys Vlasenko5055a9f2010-05-14 04:08:20 +0200116 fd = open(console, O_RDWR);
117 if (fd >= 0) {
118 /* We already have ctty, nothing to do */
119 close(fd);
120 } else {
121 /* We don't have ctty (or don't have "/dev/tty" node...) */
Kevin Cernekee064e9962011-07-13 09:26:58 +0200122 do {
Jeremie Koenig430ba792010-07-30 06:21:21 +0200123#ifdef __linux__
Kevin Cernekee064e9962011-07-13 09:26:58 +0200124 int s = open_read_close("/sys/class/tty/console/active",
Kevin Cernekee816cd162011-07-13 09:29:55 -0700125 console + 5, sizeof(console) - 5);
Kevin Cernekee064e9962011-07-13 09:26:58 +0200126 if (s > 0) {
Kevin Cernekee816cd162011-07-13 09:29:55 -0700127 /* found active console via sysfs (Linux 2.6.38+)
128 * sysfs string looks like "ttyS0\n" so zap the newline:
129 */
130 console[4 + s] = '\0';
Kevin Cernekee064e9962011-07-13 09:26:58 +0200131 break;
132 }
133
134 if (ioctl(0, VT_GETSTATE, &u.vt) == 0) {
135 /* this is linux virtual tty */
136 sprintf(console + 8, "S%d" + 1, u.vt.v_active);
137 break;
138 }
Jeremie Koenig430ba792010-07-30 06:21:21 +0200139#endif
Kevin Cernekee064e9962011-07-13 09:26:58 +0200140#ifdef TIOCGSERIAL
141 if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) {
Kevin Cernekee46198022011-08-14 13:35:25 -0700142 /* this is a serial console; assuming it is named /dev/ttySn */
Kevin Cernekee064e9962011-07-13 09:26:58 +0200143 sprintf(console + 8, "S%d", u.sr.line);
144 break;
145 }
146#endif
147 /* nope, could not find it */
148 goto ret;
149 } while (0);
150
Kevin Cernekee43a668b2011-07-13 09:30:36 +0200151 fd = open_or_warn(console, O_RDWR);
152 if (fd < 0)
153 goto ret;
Kevin Cernekee064e9962011-07-13 09:26:58 +0200154 //bb_error_msg("switching to '%s'", console);
155 dup2(fd, 0);
156 dup2(fd, 1);
157 dup2(fd, 2);
158 while (fd > 2)
159 close(fd--);
160 /* Some other session may have it as ctty,
161 * steal it from them:
162 */
163 ioctl(0, TIOCSCTTY, 1);
Denis Vlasenko6d709972007-05-18 09:45:36 +0000164 }
165
Kevin Cernekee064e9962011-07-13 09:26:58 +0200166ret:
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200167 BB_EXECVP_or_die(argv);
Denis Vlasenko6d709972007-05-18 09:45:36 +0000168}