blob: b9ee59bd0c024f7fb7b42dc3c78b092cc51604ba [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 */
Denys Vlasenko51ca7762010-07-16 17:16:40 +02007//config:config CTTYHACK
Denys Vlasenkob097a842018-12-28 03:20:17 +01008//config: bool "cttyhack (2.4 kb)"
Denys Vlasenko51ca7762010-07-16 17:16:40 +02009//config: default y
10//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020011//config: One common problem reported on the mailing list is the "can't
12//config: access tty; job control turned off" error message, which typically
13//config: appears when one tries to use a shell with stdin/stdout on
14//config: /dev/console.
15//config: This device is special - it cannot be a controlling tty.
Denys Vlasenko51ca7762010-07-16 17:16:40 +020016//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: The proper solution is to use the correct device instead of
18//config: /dev/console.
Denys Vlasenko51ca7762010-07-16 17:16:40 +020019//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020020//config: cttyhack provides a "quick and dirty" solution to this problem.
21//config: It analyzes stdin with various ioctls, trying to determine whether
22//config: it is a /dev/ttyN or /dev/ttySN (virtual terminal or serial line).
23//config: On Linux it also checks sysfs for a pointer to the active console.
24//config: If cttyhack is able to find the real console device, it closes
25//config: stdin/out/err and reopens that device.
26//config: Then it executes the given program. Opening the device will make
27//config: that device a controlling tty. This may require cttyhack
28//config: to be a session leader.
Denys Vlasenko51ca7762010-07-16 17:16:40 +020029//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020030//config: Example for /etc/inittab (for busybox init):
Denys Vlasenko51ca7762010-07-16 17:16:40 +020031//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020032//config: ::respawn:/bin/cttyhack /bin/sh
Denys Vlasenko51ca7762010-07-16 17:16:40 +020033//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020034//config: Starting an interactive shell from boot shell script:
Denys Vlasenko51ca7762010-07-16 17:16:40 +020035//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020036//config: setsid cttyhack sh
Denys Vlasenko51ca7762010-07-16 17:16:40 +020037//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020038//config: Giving controlling tty to shell running with PID 1:
Denys Vlasenko51ca7762010-07-16 17:16:40 +020039//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020040//config: # exec cttyhack sh
Denys Vlasenko51ca7762010-07-16 17:16:40 +020041//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020042//config: Without cttyhack, you need to know exact tty name,
43//config: and do something like this:
Denys Vlasenko51ca7762010-07-16 17:16:40 +020044//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020045//config: # exec setsid sh -c 'exec sh </dev/tty1 >/dev/tty1 2>&1'
Denys Vlasenko51ca7762010-07-16 17:16:40 +020046//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020047//config: Starting getty on a controlling tty from a shell script:
Alexander Shishkin156840c2011-10-31 13:18:44 +010048//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020049//config: # getty 115200 $(cttyhack)
Denys Vlasenko51ca7762010-07-16 17:16:40 +020050
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020051//applet:IF_CTTYHACK(APPLET_NOEXEC(cttyhack, cttyhack, BB_DIR_BIN, BB_SUID_DROP, cttyhack))
52
53//kbuild:lib-$(CONFIG_CTTYHACK) += cttyhack.o
54
Denys Vlasenko51ca7762010-07-16 17:16:40 +020055//usage:#define cttyhack_trivial_usage
Alexander Shishkin156840c2011-10-31 13:18:44 +010056//usage: "[PROG ARGS]"
Denys Vlasenko51ca7762010-07-16 17:16:40 +020057//usage:#define cttyhack_full_usage "\n\n"
58//usage: "Give PROG a controlling tty if possible."
59//usage: "\nExample for /etc/inittab (for busybox init):"
60//usage: "\n ::respawn:/bin/cttyhack /bin/sh"
61//usage: "\nGiving controlling tty to shell running with PID 1:"
62//usage: "\n $ exec cttyhack sh"
63//usage: "\nStarting interactive shell from boot shell script:"
64//usage: "\n setsid cttyhack sh"
65
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020066#include "libbb.h"
67
Denys Vlasenko17662802010-07-30 17:41:35 +020068#if !defined(__linux__) && !defined(TIOCGSERIAL) && !ENABLE_WERROR
Jeremie Koenig430ba792010-07-30 06:21:21 +020069# warning cttyhack will not be able to detect a controlling tty on this system
70#endif
71
Denis Vlasenko6d709972007-05-18 09:45:36 +000072/* From <linux/vt.h> */
73struct vt_stat {
Denis Vlasenko2afabe82007-12-10 07:06:04 +000074 unsigned short v_active; /* active vt */
75 unsigned short v_signal; /* signal to send */
76 unsigned short v_state; /* vt bitmask */
Denis Vlasenko6d709972007-05-18 09:45:36 +000077};
Denis Vlasenko2afabe82007-12-10 07:06:04 +000078enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
Denis Vlasenko6d709972007-05-18 09:45:36 +000079
80/* From <linux/serial.h> */
81struct serial_struct {
82 int type;
83 int line;
84 unsigned int port;
85 int irq;
86 int flags;
87 int xmit_fifo_size;
88 int custom_divisor;
89 int baud_base;
90 unsigned short close_delay;
91 char io_type;
92 char reserved_char[1];
93 int hub6;
Denis Vlasenko2afabe82007-12-10 07:06:04 +000094 unsigned short closing_wait; /* time to wait before closing */
95 unsigned short closing_wait2; /* no longer used... */
Denis Vlasenko6d709972007-05-18 09:45:36 +000096 unsigned char *iomem_base;
97 unsigned short iomem_reg_shift;
98 unsigned int port_high;
99 unsigned long iomap_base; /* cookie passed into ioremap */
100 int reserved[1];
101};
102
Denis Vlasenko528a8b92007-12-22 19:57:28 +0000103int cttyhack_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000104int cttyhack_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko6d709972007-05-18 09:45:36 +0000105{
106 int fd;
107 char console[sizeof(int)*3 + 16];
108 union {
109 struct vt_stat vt;
110 struct serial_struct sr;
111 char paranoia[sizeof(struct serial_struct) * 3];
112 } u;
113
Denis Vlasenko6d709972007-05-18 09:45:36 +0000114 strcpy(console, "/dev/tty");
Denys Vlasenko5055a9f2010-05-14 04:08:20 +0200115 fd = open(console, O_RDWR);
Alexander Shishkin156840c2011-10-31 13:18:44 +0100116 if (fd < 0) {
Denys Vlasenko5055a9f2010-05-14 04:08:20 +0200117 /* We don't have ctty (or don't have "/dev/tty" node...) */
Kevin Cernekee064e9962011-07-13 09:26:58 +0200118 do {
Jeremie Koenig430ba792010-07-30 06:21:21 +0200119#ifdef __linux__
Denys Vlasenkof8a5b792011-10-31 16:57:04 +0100120 /* Note that this method does not use _stdin_.
121 * Thus, "cttyhack </dev/something" can't be used.
122 * However, this method is more reliable than
123 * TIOCGSERIAL check, which assumes that all
124 * serial lines follow /dev/ttySn convention -
125 * which is not always the case.
Aaro Koskinen6e9d0472012-02-04 21:55:01 +0100126 * Therefore, we use this method first:
Denys Vlasenkof8a5b792011-10-31 16:57:04 +0100127 */
128 int s = open_read_close("/sys/class/tty/console/active",
129 console + 5, sizeof(console) - 5);
130 if (s > 0) {
Aaro Koskinen6e9d0472012-02-04 21:55:01 +0100131 char *last;
132 /* Found active console via sysfs (Linux 2.6.38+).
133 * It looks like "[tty0 ]ttyS0\n" so zap the newline:
Denys Vlasenkof8a5b792011-10-31 16:57:04 +0100134 */
135 console[4 + s] = '\0';
Aaro Koskinen6e9d0472012-02-04 21:55:01 +0100136 /* If there are multiple consoles,
137 * take the last one:
138 */
139 last = strrchr(console + 5, ' ');
140 if (last)
141 overlapping_strcpy(console + 5, last + 1);
Denys Vlasenkof8a5b792011-10-31 16:57:04 +0100142 break;
143 }
144
Alexander Shishkin156840c2011-10-31 13:18:44 +0100145 if (ioctl(0, VT_GETSTATE, &u.vt) == 0) {
146 /* this is linux virtual tty */
147 sprintf(console + 8, "S%u" + 1, (int)u.vt.v_active);
148 break;
149 }
150#endif
151#ifdef TIOCGSERIAL
152 if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) {
153 /* this is a serial console; assuming it is named /dev/ttySn */
154 sprintf(console + 8, "S%u", (int)u.sr.line);
155 break;
156 }
157#endif
Kevin Cernekee064e9962011-07-13 09:26:58 +0200158 /* nope, could not find it */
Alexander Shishkin156840c2011-10-31 13:18:44 +0100159 console[0] = '\0';
Kevin Cernekee064e9962011-07-13 09:26:58 +0200160 } while (0);
Alexander Shishkin156840c2011-10-31 13:18:44 +0100161 }
Kevin Cernekee064e9962011-07-13 09:26:58 +0200162
Alexander Shishkin156840c2011-10-31 13:18:44 +0100163 argv++;
164 if (!argv[0]) {
165 if (!console[0])
166 return EXIT_FAILURE;
167 puts(console);
168 return EXIT_SUCCESS;
169 }
170
171 if (fd < 0) {
Kevin Cernekee43a668b2011-07-13 09:30:36 +0200172 fd = open_or_warn(console, O_RDWR);
173 if (fd < 0)
174 goto ret;
Denis Vlasenko6d709972007-05-18 09:45:36 +0000175 }
Alexander Shishkin156840c2011-10-31 13:18:44 +0100176 //bb_error_msg("switching to '%s'", console);
177 dup2(fd, 0);
178 dup2(fd, 1);
179 dup2(fd, 2);
180 while (fd > 2)
181 close(fd--);
182 /* Some other session may have it as ctty,
183 * try to steal it from them:
184 */
185 ioctl(0, TIOCSCTTY, 1);
186 ret:
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200187 BB_EXECVP_or_die(argv);
Denis Vlasenko6d709972007-05-18 09:45:36 +0000188}