pseudotty.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* pseudotty - open pseudo-terminal and print name of slave device to
  2. standard output. Read and ignore any data sent to terminal. This
  3. is so we can run tests interactively without messing up the screen.
  4. Copyright 2014, 2015, 2016 Free Software Foundation, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. Originally written by Gavin Smith. */
  16. #define _XOPEN_SOURCE 600
  17. /* for posix_openpt */
  18. #include <config.h>
  19. #include <errno.h>
  20. #include <error.h>
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/types.h>
  26. #include <sys/select.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #if HAVE_STROPTS_H
  30. #include <stropts.h>
  31. #endif
  32. #include "termdep.h"
  33. /* Used by "error" function. */
  34. const char *program_name = "pseudotty";
  35. int
  36. main (int argc, char *argv[])
  37. {
  38. int master, slave, control;
  39. char *name;
  40. fd_set read_set;
  41. error (0, 0, "getting pty master fd");
  42. master = posix_openpt (O_RDWR);
  43. if (master == -1)
  44. exit (1);
  45. error (0, 0, "unlocking slave device");
  46. if (grantpt (master) < 0 || unlockpt (master) < 0)
  47. exit (1);
  48. error (0, 0, "getting file name of slave device...");
  49. name = ptsname (master);
  50. if (!name)
  51. exit (1);
  52. error (0, 0, "%s", name);
  53. #ifdef HAVE_STROPTS_H
  54. error (0, 0, "opening slave device");
  55. slave = open (name, O_RDWR);
  56. if (slave == -1)
  57. exit (1);
  58. if (!isatty (slave))
  59. {
  60. error (0, 0, "performing STREAMS ioctl's on slave");
  61. if (isastream (slave))
  62. {
  63. if (ioctl (slave, I_PUSH, "ptem") < 0
  64. || ioctl (slave, I_PUSH, "ldterm") < 0)
  65. error (1, 0, "STREAMS ioctl's failed");
  66. }
  67. }
  68. /* Don't close it because it just leads to an EOF read at the master end. */
  69. /*
  70. error (0, 0, "closing slave device");
  71. close (slave);
  72. error (0, 0, "...closed");
  73. */
  74. #endif
  75. #if defined (HAVE_TERMIOS_H)
  76. {
  77. struct termios t;
  78. long int disable;
  79. disable = fpathconf (slave, _PC_VDISABLE);
  80. if (tcgetattr (slave, &t) == -1)
  81. error (0, 0, "error calling tcgetattr");
  82. else
  83. {
  84. t.c_cc[VSTART] = disable; /* C-q */
  85. t.c_cc[VSTOP] = disable; /* C-s */
  86. t.c_cc[VKILL] = disable; /* C-u */
  87. t.c_cc[VINTR] = disable; /* C-c */
  88. if (tcsetattr (slave, TCSANOW, &t) == -1)
  89. error (0, 0, "error calling tcsetattr");
  90. }
  91. }
  92. #endif
  93. #if defined (TIOCSWINSZ)
  94. {
  95. struct winsize ws;
  96. ws.ws_col = ws.ws_row = 0;
  97. error (0, 0, "attempting to set window size");
  98. if (ioctl (master, TIOCSWINSZ, &ws) == 0)
  99. error (0, 0, "...succeeded");
  100. else
  101. error (0, 0, "...failed");
  102. }
  103. #endif
  104. printf ("%s\n", name);
  105. if (fclose (stdout) != 0)
  106. error (1, 0, "error closing stdout: aborting");
  107. error (0, 0, "opening control channel");
  108. control = open (argv[1], O_RDONLY);
  109. if (control == -1)
  110. error (1, 0, "error opening control channel: aborting");
  111. FD_ZERO (&read_set);
  112. error (0, 0, "entering main loop");
  113. while (1)
  114. {
  115. FD_SET (master, &read_set);
  116. FD_SET (control, &read_set);
  117. select (FD_SETSIZE, &read_set, 0, 0, 0);
  118. if (FD_ISSET (control, &read_set))
  119. {
  120. char c;
  121. int success;
  122. errno = 0;
  123. while (1)
  124. {
  125. error (0, 0, "trying to read");
  126. success = read (control, &c, 1);
  127. if (success < 0)
  128. {
  129. if (errno != EINTR)
  130. error (1, errno, "read error on control channel");
  131. }
  132. else if (success == 0)
  133. {
  134. error (1, 0, "end of file on control channel");
  135. }
  136. else if (success == 1)
  137. {
  138. error (0, 0, "read byte 0x%02X", c);
  139. break;
  140. }
  141. }
  142. /* Feed any read bytes to the program being controlled. */
  143. do
  144. {
  145. success = write (master, &c, 1);
  146. if (success == 0)
  147. {
  148. error (0, 0, "couldn't send byte!");
  149. sleep (1);
  150. continue;
  151. }
  152. }
  153. while (success == -1 && errno == EINTR);
  154. if (success != 1)
  155. {
  156. /* The controlled process has probably exited, or been killed. */
  157. error (0, 0, "couldn't send byte (giving up)");
  158. sleep (1);
  159. }
  160. }
  161. if (FD_ISSET (master, &read_set))
  162. {
  163. char c;
  164. int success;
  165. errno = 0;
  166. do
  167. {
  168. success = read (master, &c, 1);
  169. }
  170. while (success == -1 && errno == EINTR);
  171. if (success == -1)
  172. {
  173. /* The controlled process has probably exited, or been killed. */
  174. error (0, 0, "read error on master fd");
  175. sleep (1);
  176. }
  177. }
  178. }
  179. return 0; /* NOTREACHED */
  180. }