cttyhack.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "libbb.h"
  8. //applet:IF_CTTYHACK(APPLET(cttyhack, _BB_DIR_BIN, _BB_SUID_DROP))
  9. //kbuild:lib-$(CONFIG_CTTYHACK) += cttyhack.o
  10. //config:config CTTYHACK
  11. //config: bool "cttyhack"
  12. //config: default y
  13. //config: help
  14. //config: One common problem reported on the mailing list is "can't access tty;
  15. //config: job control turned off" error message which typically appears when
  16. //config: one tries to use shell with stdin/stdout opened to /dev/console.
  17. //config: This device is special - it cannot be a controlling tty.
  18. //config:
  19. //config: Proper solution is to use correct device instead of /dev/console.
  20. //config:
  21. //config: cttyhack provides "quick and dirty" solution to this problem.
  22. //config: It analyzes stdin with various ioctls, trying to determine whether
  23. //config: it is a /dev/ttyN or /dev/ttySN (virtual terminal or serial line).
  24. //config: If it detects one, it closes stdin/out/err and reopens that device.
  25. //config: Then it executes given program. Opening the device will make
  26. //config: that device a controlling tty. This may require cttyhack
  27. //config: to be a session leader.
  28. //config:
  29. //config: Example for /etc/inittab (for busybox init):
  30. //config:
  31. //config: ::respawn:/bin/cttyhack /bin/sh
  32. //config:
  33. //config: Starting an interactive shell from boot shell script:
  34. //config:
  35. //config: setsid cttyhack sh
  36. //config:
  37. //config: Giving controlling tty to shell running with PID 1:
  38. //config:
  39. //config: # exec cttyhack sh
  40. //config:
  41. //config: Without cttyhack, you need to know exact tty name,
  42. //config: and do something like this:
  43. //config:
  44. //config: # exec setsid sh -c 'exec sh </dev/tty1 >/dev/tty1 2>&1'
  45. //config:
  46. //usage:#define cttyhack_trivial_usage
  47. //usage: "PROG ARGS"
  48. //usage:#define cttyhack_full_usage "\n\n"
  49. //usage: "Give PROG a controlling tty if possible."
  50. //usage: "\nExample for /etc/inittab (for busybox init):"
  51. //usage: "\n ::respawn:/bin/cttyhack /bin/sh"
  52. //usage: "\nGiving controlling tty to shell running with PID 1:"
  53. //usage: "\n $ exec cttyhack sh"
  54. //usage: "\nStarting interactive shell from boot shell script:"
  55. //usage: "\n setsid cttyhack sh"
  56. #if !defined(__linux__) && !defined(TIOCGSERIAL) && !ENABLE_WERROR
  57. # warning cttyhack will not be able to detect a controlling tty on this system
  58. #endif
  59. /* From <linux/vt.h> */
  60. struct vt_stat {
  61. unsigned short v_active; /* active vt */
  62. unsigned short v_signal; /* signal to send */
  63. unsigned short v_state; /* vt bitmask */
  64. };
  65. enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
  66. /* From <linux/serial.h> */
  67. struct serial_struct {
  68. int type;
  69. int line;
  70. unsigned int port;
  71. int irq;
  72. int flags;
  73. int xmit_fifo_size;
  74. int custom_divisor;
  75. int baud_base;
  76. unsigned short close_delay;
  77. char io_type;
  78. char reserved_char[1];
  79. int hub6;
  80. unsigned short closing_wait; /* time to wait before closing */
  81. unsigned short closing_wait2; /* no longer used... */
  82. unsigned char *iomem_base;
  83. unsigned short iomem_reg_shift;
  84. unsigned int port_high;
  85. unsigned long iomap_base; /* cookie passed into ioremap */
  86. int reserved[1];
  87. };
  88. int cttyhack_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  89. int cttyhack_main(int argc UNUSED_PARAM, char **argv)
  90. {
  91. int fd;
  92. char console[sizeof(int)*3 + 16];
  93. union {
  94. struct vt_stat vt;
  95. struct serial_struct sr;
  96. char paranoia[sizeof(struct serial_struct) * 3];
  97. } u;
  98. if (!*++argv) {
  99. bb_show_usage();
  100. }
  101. strcpy(console, "/dev/tty");
  102. fd = open(console, O_RDWR);
  103. if (fd >= 0) {
  104. /* We already have ctty, nothing to do */
  105. close(fd);
  106. } else {
  107. /* We don't have ctty (or don't have "/dev/tty" node...) */
  108. if (0) {}
  109. #ifdef TIOCGSERIAL
  110. else if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) {
  111. /* this is a serial console */
  112. sprintf(console + 8, "S%d", u.sr.line);
  113. }
  114. #endif
  115. #ifdef __linux__
  116. else if (ioctl(0, VT_GETSTATE, &u.vt) == 0) {
  117. /* this is linux virtual tty */
  118. sprintf(console + 8, "S%d" + 1, u.vt.v_active);
  119. }
  120. #endif
  121. if (console[8]) {
  122. fd = xopen(console, O_RDWR);
  123. //bb_error_msg("switching to '%s'", console);
  124. dup2(fd, 0);
  125. dup2(fd, 1);
  126. dup2(fd, 2);
  127. while (fd > 2)
  128. close(fd--);
  129. /* Some other session may have it as ctty,
  130. * steal it from them:
  131. */
  132. ioctl(0, TIOCSCTTY, 1);
  133. }
  134. }
  135. BB_EXECVP_or_die(argv);
  136. }