3
0

cttyhack.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (c) 2007 Denys Vlasenko <vda.linux@googlemail.com>
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this source tree.
  6. */
  7. //config:config CTTYHACK
  8. //config: bool "cttyhack (2.4 kb)"
  9. //config: default y
  10. //config: help
  11. //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.
  16. //config:
  17. //config: The proper solution is to use the correct device instead of
  18. //config: /dev/console.
  19. //config:
  20. //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.
  29. //config:
  30. //config: Example for /etc/inittab (for busybox init):
  31. //config:
  32. //config: ::respawn:/bin/cttyhack /bin/sh
  33. //config:
  34. //config: Starting an interactive shell from boot shell script:
  35. //config:
  36. //config: setsid cttyhack sh
  37. //config:
  38. //config: Giving controlling tty to shell running with PID 1:
  39. //config:
  40. //config: # exec cttyhack sh
  41. //config:
  42. //config: Without cttyhack, you need to know exact tty name,
  43. //config: and do something like this:
  44. //config:
  45. //config: # exec setsid sh -c 'exec sh </dev/tty1 >/dev/tty1 2>&1'
  46. //config:
  47. //config: Starting getty on a controlling tty from a shell script:
  48. //config:
  49. //config: # getty 115200 $(cttyhack)
  50. //applet:IF_CTTYHACK(APPLET_NOEXEC(cttyhack, cttyhack, BB_DIR_BIN, BB_SUID_DROP, cttyhack))
  51. //kbuild:lib-$(CONFIG_CTTYHACK) += cttyhack.o
  52. //usage:#define cttyhack_trivial_usage
  53. //usage: "[PROG ARGS]"
  54. //usage:#define cttyhack_full_usage "\n\n"
  55. //usage: "Give PROG a controlling tty if possible."
  56. //usage: "\nExample for /etc/inittab (for busybox init):"
  57. //usage: "\n ::respawn:/bin/cttyhack /bin/sh"
  58. //usage: "\nGiving controlling tty to shell running with PID 1:"
  59. //usage: "\n $ exec cttyhack sh"
  60. //usage: "\nStarting interactive shell from boot shell script:"
  61. //usage: "\n setsid cttyhack sh"
  62. #include "libbb.h"
  63. #if !defined(__linux__) && !defined(TIOCGSERIAL) && !ENABLE_WERROR
  64. # warning cttyhack will not be able to detect a controlling tty on this system
  65. #endif
  66. /* From <linux/vt.h> */
  67. struct vt_stat {
  68. unsigned short v_active; /* active vt */
  69. unsigned short v_signal; /* signal to send */
  70. unsigned short v_state; /* vt bitmask */
  71. };
  72. enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
  73. /* From <linux/serial.h> */
  74. struct serial_struct {
  75. int type;
  76. int line;
  77. unsigned int port;
  78. int irq;
  79. int flags;
  80. int xmit_fifo_size;
  81. int custom_divisor;
  82. int baud_base;
  83. unsigned short close_delay;
  84. char io_type;
  85. char reserved_char[1];
  86. int hub6;
  87. unsigned short closing_wait; /* time to wait before closing */
  88. unsigned short closing_wait2; /* no longer used... */
  89. unsigned char *iomem_base;
  90. unsigned short iomem_reg_shift;
  91. unsigned int port_high;
  92. unsigned long iomap_base; /* cookie passed into ioremap */
  93. int reserved[1];
  94. };
  95. int cttyhack_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  96. int cttyhack_main(int argc UNUSED_PARAM, char **argv)
  97. {
  98. int fd;
  99. char console[sizeof(int)*3 + 16];
  100. union {
  101. struct vt_stat vt;
  102. struct serial_struct sr;
  103. char paranoia[sizeof(struct serial_struct) * 3];
  104. } u;
  105. strcpy(console, "/dev/tty");
  106. fd = open(console, O_RDWR);
  107. if (fd < 0) {
  108. /* We don't have ctty (or don't have "/dev/tty" node...) */
  109. do {
  110. #ifdef __linux__
  111. /* Note that this method does not use _stdin_.
  112. * Thus, "cttyhack </dev/something" can't be used.
  113. * However, this method is more reliable than
  114. * TIOCGSERIAL check, which assumes that all
  115. * serial lines follow /dev/ttySn convention -
  116. * which is not always the case.
  117. * Therefore, we use this method first:
  118. */
  119. int s = open_read_close("/sys/class/tty/console/active",
  120. console + 5, sizeof(console) - 5);
  121. if (s > 0) {
  122. char *last;
  123. /* Found active console via sysfs (Linux 2.6.38+).
  124. * It looks like "[tty0 ]ttyS0\n" so zap the newline:
  125. */
  126. console[4 + s] = '\0';
  127. /* If there are multiple consoles,
  128. * take the last one:
  129. */
  130. last = strrchr(console + 5, ' ');
  131. if (last)
  132. overlapping_strcpy(console + 5, last + 1);
  133. break;
  134. }
  135. if (ioctl(0, VT_GETSTATE, &u.vt) == 0) {
  136. /* this is linux virtual tty */
  137. sprintf(console + 8, "S%u" + 1, (int)u.vt.v_active);
  138. break;
  139. }
  140. #endif
  141. #ifdef TIOCGSERIAL
  142. if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) {
  143. /* this is a serial console; assuming it is named /dev/ttySn */
  144. sprintf(console + 8, "S%u", (int)u.sr.line);
  145. break;
  146. }
  147. #endif
  148. /* nope, could not find it */
  149. console[0] = '\0';
  150. } while (0);
  151. }
  152. argv++;
  153. if (!argv[0]) {
  154. if (!console[0])
  155. return EXIT_FAILURE;
  156. puts(console);
  157. return EXIT_SUCCESS;
  158. }
  159. if (fd < 0) {
  160. fd = open_or_warn(console, O_RDWR);
  161. if (fd < 0)
  162. goto ret;
  163. }
  164. //bb_error_msg("switching to '%s'", console);
  165. dup2(fd, 0);
  166. dup2(fd, 1);
  167. dup2(fd, 2);
  168. while (fd > 2)
  169. close(fd--);
  170. /* Some other session may have it as ctty,
  171. * try to steal it from them:
  172. */
  173. ioctl(0, TIOCSCTTY, 1);
  174. ret:
  175. BB_EXECVP_or_die(argv);
  176. }