Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 3 | * Copyright (c) 2007 Denys Vlasenko <vda.linux@googlemail.com> |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 4 | * |
| 5 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 6d70997 | 2007-05-18 09:45:36 +0000 | [diff] [blame] | 6 | */ |
| 7 | #include "libbb.h" |
| 8 | |
Denys Vlasenko | b9f2d9f | 2011-01-18 13:58:01 +0100 | [diff] [blame] | 9 | //applet:IF_CTTYHACK(APPLET(cttyhack, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 51ca776 | 2010-07-16 17:16:40 +0200 | [diff] [blame] | 10 | |
| 11 | //kbuild:lib-$(CONFIG_CTTYHACK) += cttyhack.o |
| 12 | |
| 13 | //config:config CTTYHACK |
| 14 | //config: bool "cttyhack" |
| 15 | //config: default y |
| 16 | //config: help |
Kevin Cernekee | 064e996 | 2011-07-13 09:26:58 +0200 | [diff] [blame] | 17 | //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 Vlasenko | 51ca776 | 2010-07-16 17:16:40 +0200 | [diff] [blame] | 21 | //config: This device is special - it cannot be a controlling tty. |
| 22 | //config: |
Kevin Cernekee | 064e996 | 2011-07-13 09:26:58 +0200 | [diff] [blame] | 23 | //config: The proper solution is to use the correct device instead of |
| 24 | //config: /dev/console. |
Denys Vlasenko | 51ca776 | 2010-07-16 17:16:40 +0200 | [diff] [blame] | 25 | //config: |
Kevin Cernekee | 064e996 | 2011-07-13 09:26:58 +0200 | [diff] [blame] | 26 | //config: cttyhack provides a "quick and dirty" solution to this problem. |
Denys Vlasenko | 51ca776 | 2010-07-16 17:16:40 +0200 | [diff] [blame] | 27 | //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 Cernekee | 064e996 | 2011-07-13 09:26:58 +0200 | [diff] [blame] | 29 | //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 Vlasenko | 51ca776 | 2010-07-16 17:16:40 +0200 | [diff] [blame] | 33 | //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: |
Alexander Shishkin | 156840c | 2011-10-31 13:18:44 +0100 | [diff] [blame] | 53 | //config: Starting getty on a controlling tty from a shell script: |
| 54 | //config: |
| 55 | //config: # getty 115200 $(cttyhack) |
Denys Vlasenko | 51ca776 | 2010-07-16 17:16:40 +0200 | [diff] [blame] | 56 | |
| 57 | //usage:#define cttyhack_trivial_usage |
Alexander Shishkin | 156840c | 2011-10-31 13:18:44 +0100 | [diff] [blame] | 58 | //usage: "[PROG ARGS]" |
Denys Vlasenko | 51ca776 | 2010-07-16 17:16:40 +0200 | [diff] [blame] | 59 | //usage:#define cttyhack_full_usage "\n\n" |
| 60 | //usage: "Give PROG a controlling tty if possible." |
| 61 | //usage: "\nExample for /etc/inittab (for busybox init):" |
| 62 | //usage: "\n ::respawn:/bin/cttyhack /bin/sh" |
| 63 | //usage: "\nGiving controlling tty to shell running with PID 1:" |
| 64 | //usage: "\n $ exec cttyhack sh" |
| 65 | //usage: "\nStarting interactive shell from boot shell script:" |
| 66 | //usage: "\n setsid cttyhack sh" |
| 67 | |
Denys Vlasenko | 1766280 | 2010-07-30 17:41:35 +0200 | [diff] [blame] | 68 | #if !defined(__linux__) && !defined(TIOCGSERIAL) && !ENABLE_WERROR |
Jeremie Koenig | 430ba79 | 2010-07-30 06:21:21 +0200 | [diff] [blame] | 69 | # warning cttyhack will not be able to detect a controlling tty on this system |
| 70 | #endif |
| 71 | |
Denis Vlasenko | 6d70997 | 2007-05-18 09:45:36 +0000 | [diff] [blame] | 72 | /* From <linux/vt.h> */ |
| 73 | struct vt_stat { |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 74 | unsigned short v_active; /* active vt */ |
| 75 | unsigned short v_signal; /* signal to send */ |
| 76 | unsigned short v_state; /* vt bitmask */ |
Denis Vlasenko | 6d70997 | 2007-05-18 09:45:36 +0000 | [diff] [blame] | 77 | }; |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 78 | enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */ |
Denis Vlasenko | 6d70997 | 2007-05-18 09:45:36 +0000 | [diff] [blame] | 79 | |
| 80 | /* From <linux/serial.h> */ |
| 81 | struct 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 Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 94 | unsigned short closing_wait; /* time to wait before closing */ |
| 95 | unsigned short closing_wait2; /* no longer used... */ |
Denis Vlasenko | 6d70997 | 2007-05-18 09:45:36 +0000 | [diff] [blame] | 96 | 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 Vlasenko | 528a8b9 | 2007-12-22 19:57:28 +0000 | [diff] [blame] | 103 | int cttyhack_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 104 | int cttyhack_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 6d70997 | 2007-05-18 09:45:36 +0000 | [diff] [blame] | 105 | { |
| 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 Vlasenko | 6d70997 | 2007-05-18 09:45:36 +0000 | [diff] [blame] | 114 | strcpy(console, "/dev/tty"); |
Denys Vlasenko | 5055a9f | 2010-05-14 04:08:20 +0200 | [diff] [blame] | 115 | fd = open(console, O_RDWR); |
Alexander Shishkin | 156840c | 2011-10-31 13:18:44 +0100 | [diff] [blame] | 116 | if (fd < 0) { |
Denys Vlasenko | 5055a9f | 2010-05-14 04:08:20 +0200 | [diff] [blame] | 117 | /* We don't have ctty (or don't have "/dev/tty" node...) */ |
Kevin Cernekee | 064e996 | 2011-07-13 09:26:58 +0200 | [diff] [blame] | 118 | do { |
Jeremie Koenig | 430ba79 | 2010-07-30 06:21:21 +0200 | [diff] [blame] | 119 | #ifdef __linux__ |
Denys Vlasenko | f8a5b79 | 2011-10-31 16:57:04 +0100 | [diff] [blame] | 120 | /* 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. |
| 126 | * Therefore, we use this methos first: |
| 127 | */ |
| 128 | int s = open_read_close("/sys/class/tty/console/active", |
| 129 | console + 5, sizeof(console) - 5); |
| 130 | if (s > 0) { |
| 131 | /* found active console via sysfs (Linux 2.6.38+) |
| 132 | * sysfs string looks like "ttyS0\n" so zap the newline: |
| 133 | */ |
| 134 | console[4 + s] = '\0'; |
| 135 | break; |
| 136 | } |
| 137 | |
Alexander Shishkin | 156840c | 2011-10-31 13:18:44 +0100 | [diff] [blame] | 138 | if (ioctl(0, VT_GETSTATE, &u.vt) == 0) { |
| 139 | /* this is linux virtual tty */ |
| 140 | sprintf(console + 8, "S%u" + 1, (int)u.vt.v_active); |
| 141 | break; |
| 142 | } |
| 143 | #endif |
| 144 | #ifdef TIOCGSERIAL |
| 145 | if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) { |
| 146 | /* this is a serial console; assuming it is named /dev/ttySn */ |
| 147 | sprintf(console + 8, "S%u", (int)u.sr.line); |
| 148 | break; |
| 149 | } |
| 150 | #endif |
Kevin Cernekee | 064e996 | 2011-07-13 09:26:58 +0200 | [diff] [blame] | 151 | /* nope, could not find it */ |
Alexander Shishkin | 156840c | 2011-10-31 13:18:44 +0100 | [diff] [blame] | 152 | console[0] = '\0'; |
Kevin Cernekee | 064e996 | 2011-07-13 09:26:58 +0200 | [diff] [blame] | 153 | } while (0); |
Alexander Shishkin | 156840c | 2011-10-31 13:18:44 +0100 | [diff] [blame] | 154 | } |
Kevin Cernekee | 064e996 | 2011-07-13 09:26:58 +0200 | [diff] [blame] | 155 | |
Alexander Shishkin | 156840c | 2011-10-31 13:18:44 +0100 | [diff] [blame] | 156 | argv++; |
| 157 | if (!argv[0]) { |
| 158 | if (!console[0]) |
| 159 | return EXIT_FAILURE; |
| 160 | puts(console); |
| 161 | return EXIT_SUCCESS; |
| 162 | } |
| 163 | |
| 164 | if (fd < 0) { |
Kevin Cernekee | 43a668b | 2011-07-13 09:30:36 +0200 | [diff] [blame] | 165 | fd = open_or_warn(console, O_RDWR); |
| 166 | if (fd < 0) |
| 167 | goto ret; |
Denis Vlasenko | 6d70997 | 2007-05-18 09:45:36 +0000 | [diff] [blame] | 168 | } |
Alexander Shishkin | 156840c | 2011-10-31 13:18:44 +0100 | [diff] [blame] | 169 | //bb_error_msg("switching to '%s'", console); |
| 170 | dup2(fd, 0); |
| 171 | dup2(fd, 1); |
| 172 | dup2(fd, 2); |
| 173 | while (fd > 2) |
| 174 | close(fd--); |
| 175 | /* Some other session may have it as ctty, |
| 176 | * try to steal it from them: |
| 177 | */ |
| 178 | ioctl(0, TIOCSCTTY, 1); |
| 179 | ret: |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 180 | BB_EXECVP_or_die(argv); |
Denis Vlasenko | 6d70997 | 2007-05-18 09:45:36 +0000 | [diff] [blame] | 181 | } |