ttyemu.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* Run program with its first three file descriptors attached to a tty.
  2. Copyright 2014, 2016 Free Software Foundation, Inc.
  3. This file is part of GNU tar.
  4. GNU tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #define _XOPEN_SOURCE 600
  16. #include <config.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <sys/time.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <sys/wait.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <signal.h>
  27. #include <string.h>
  28. #include <sys/socket.h>
  29. #include <sys/select.h>
  30. #include <termios.h>
  31. #include <sys/ioctl.h>
  32. #ifndef TCSASOFT
  33. # define TCSASOFT 0
  34. #endif
  35. #define C_EOT 4
  36. #define EX_OK 0
  37. #define EX_USAGE 125
  38. #define EX_ERR 126
  39. #define EX_EXEC 127
  40. #define BUF_SIZE 1024
  41. #if 0
  42. # define DEBUG(c) fprintf (stderr, "%s\n", c)
  43. #else
  44. # define DEBUG(c)
  45. #endif
  46. struct buffer
  47. {
  48. char buf[BUF_SIZE];
  49. int avail;
  50. int written;
  51. int cr;
  52. time_t ts;
  53. };
  54. #define shut(fildes) \
  55. do \
  56. { \
  57. DEBUG (("closing " #fildes)); \
  58. close(fildes); \
  59. fildes = -1; \
  60. } \
  61. while(0)
  62. #define bufinit(buffer,all) \
  63. do \
  64. { \
  65. (buffer).avail = (buffer).written = 0; \
  66. (buffer).ts = time (NULL); \
  67. if (all) \
  68. (buffer).cr = 0; \
  69. } \
  70. while(0)
  71. #define bufisempty(buffer) ((buffer).avail == (buffer).written)
  72. #define bufavail(buffer) (BUF_SIZE - (buffer).avail)
  73. #define bufread(buffer,fildes,tty) \
  74. do \
  75. { \
  76. int r = read (fildes, (buffer).buf + (buffer).avail, \
  77. BUF_SIZE - (buffer).avail); \
  78. (buffer).ts = time (NULL); \
  79. if (r < 0) \
  80. { \
  81. if (errno == EINTR) \
  82. continue; \
  83. if (tty && errno == EIO) \
  84. shut (fildes); \
  85. else \
  86. { \
  87. fprintf (stderr, "%s:%d: reading from %s: %s", \
  88. __FILE__,__LINE__,#fildes, strerror (errno)); \
  89. exit (EX_ERR); \
  90. } \
  91. } \
  92. else if (r == 0) \
  93. shut (fildes); \
  94. else \
  95. (buffer).avail += r; \
  96. } \
  97. while(0)
  98. #define bufwrite(buffer,fildes) \
  99. do \
  100. { \
  101. int r = write (fildes, (buffer).buf + (buffer).written, \
  102. (buffer).avail - (buffer).written); \
  103. (buffer).ts = time (NULL); \
  104. if (r < 0) \
  105. { \
  106. if (errno == EINTR) \
  107. continue; \
  108. if (stop) \
  109. shut (fildes); \
  110. else \
  111. { \
  112. perror ("writing"); \
  113. exit (EX_ERR); \
  114. } \
  115. } \
  116. else if (r == 0) \
  117. /*shut (fildes)*/; \
  118. else \
  119. (buffer).written += r; \
  120. } \
  121. while(0)
  122. void
  123. tr (struct buffer *bp)
  124. {
  125. int i, j;
  126. for (i = j = bp->written; i < bp->avail;)
  127. {
  128. if (bp->buf[i] == '\r')
  129. {
  130. bp->cr = 1;
  131. i++;
  132. }
  133. else
  134. {
  135. if (bp->cr)
  136. {
  137. bp->cr = 0;
  138. if (bp->buf[i] != '\n')
  139. bp->buf[j++] = '\r';
  140. }
  141. bp->buf[j++] = bp->buf[i++];
  142. }
  143. }
  144. bp->avail = j;
  145. }
  146. int stop;
  147. int status;
  148. void
  149. sigchld (int sig)
  150. {
  151. DEBUG (("child exited"));
  152. wait (&status);
  153. stop = 1;
  154. }
  155. void
  156. noecho (int fd)
  157. {
  158. struct termios to;
  159. if (tcgetattr (fd, &to))
  160. {
  161. perror ("tcgetattr");
  162. exit (EX_ERR);
  163. }
  164. to.c_lflag |= ICANON;
  165. to.c_lflag &= ~(ECHO | ISIG);
  166. to.c_cc[VEOF] = C_EOT;
  167. if (tcsetattr (fd, TCSAFLUSH | TCSASOFT, &to))
  168. {
  169. perror ("tcsetattr");
  170. exit (EX_ERR);
  171. }
  172. }
  173. char *usage_text[] = {
  174. "usage: ttyemu [-ah] [-i INFILE] [-o OUTFILE] [-t TIMEOUT] PROGRAM [ARGS...]",
  175. "ttyemu runs PROGRAM with its first three file descriptors connected to a"
  176. " terminal",
  177. "",
  178. "Options are:",
  179. "",
  180. " -a append output to OUTFILE, instead of overwriting it",
  181. " -i INFILE read input from INFILE",
  182. " -o OUTFILE write output to OUTFILE",
  183. " -t TIMEOUT set I/O timeout",
  184. " -h print this help summary",
  185. "",
  186. "Report bugs and suggestions to <[email protected]>.",
  187. NULL
  188. };
  189. static void
  190. usage (void)
  191. {
  192. int i;
  193. for (i = 0; usage_text[i]; i++)
  194. {
  195. fputs (usage_text[i], stderr);
  196. fputc ('\n', stderr);
  197. }
  198. }
  199. int
  200. main (int argc, char **argv)
  201. {
  202. int i;
  203. int master, slave;
  204. pid_t pid;
  205. fd_set rdset, wrset;
  206. struct buffer ibuf, obuf;
  207. int in = 0, out = 1;
  208. char *infile = NULL, *outfile = NULL;
  209. int outflags = O_TRUNC;
  210. int maxfd;
  211. int eot = C_EOT;
  212. int timeout = 0;
  213. while ((i = getopt (argc, argv, "ai:o:t:h")) != EOF)
  214. {
  215. switch (i)
  216. {
  217. case 'a':
  218. outflags &= ~O_TRUNC;
  219. break;
  220. case 'i':
  221. infile = optarg;
  222. break;
  223. case 'o':
  224. outfile = optarg;
  225. break;
  226. case 't':
  227. timeout = atoi (optarg);
  228. break;
  229. case 'h':
  230. usage ();
  231. return EX_OK;
  232. default:
  233. return EX_USAGE;
  234. }
  235. }
  236. argc -= optind;
  237. argv += optind;
  238. if (argc == 0)
  239. {
  240. usage ();
  241. return EX_USAGE;
  242. }
  243. if (infile)
  244. {
  245. in = open (infile, O_RDONLY);
  246. if (in == -1)
  247. {
  248. perror (infile);
  249. return EX_ERR;
  250. }
  251. }
  252. if (outfile)
  253. {
  254. out = open (outfile, O_RDWR|O_CREAT|outflags, 0666);
  255. if (out == -1)
  256. {
  257. perror (outfile);
  258. return EX_ERR;
  259. }
  260. }
  261. master = posix_openpt (O_RDWR);
  262. if (master == -1)
  263. {
  264. perror ("posix_openpty");
  265. return EX_ERR;
  266. }
  267. if (grantpt (master))
  268. {
  269. perror ("grantpt");
  270. return EX_ERR;
  271. }
  272. if (unlockpt (master))
  273. {
  274. perror ("unlockpt");
  275. return EX_ERR;
  276. }
  277. signal (SIGCHLD, sigchld);
  278. pid = fork ();
  279. if (pid == -1)
  280. {
  281. perror ("fork");
  282. return EX_ERR;
  283. }
  284. if (pid == 0)
  285. {
  286. slave = open (ptsname (master), O_RDWR);
  287. if (slave < 0)
  288. {
  289. perror ("open");
  290. return EX_ERR;
  291. }
  292. noecho (slave);
  293. for (i = 0; i < 3; i++)
  294. {
  295. if (slave != i)
  296. {
  297. close (i);
  298. if (dup (slave) != i)
  299. {
  300. perror ("dup");
  301. _exit (EX_EXEC);
  302. }
  303. }
  304. }
  305. for (i = sysconf (_SC_OPEN_MAX) - 1; i > 2; --i)
  306. close (i);
  307. setsid ();
  308. #ifdef TIOCSCTTY
  309. ioctl (0, TIOCSCTTY, 1);
  310. #endif
  311. execvp (argv[0], argv);
  312. perror (argv[0]);
  313. _exit (EX_EXEC);
  314. }
  315. sleep (1);
  316. bufinit (ibuf, 1);
  317. bufinit (obuf, 1);
  318. while (1)
  319. {
  320. FD_ZERO (&rdset);
  321. FD_ZERO (&wrset);
  322. maxfd = 0;
  323. if (in != -1)
  324. {
  325. FD_SET (in, &rdset);
  326. if (in > maxfd)
  327. maxfd = in;
  328. }
  329. if (master != -1)
  330. {
  331. FD_SET (master, &rdset);
  332. if (!stop)
  333. FD_SET (master, &wrset);
  334. if (master > maxfd)
  335. maxfd = master;
  336. }
  337. if (maxfd == 0)
  338. {
  339. if (stop)
  340. break;
  341. pause ();
  342. continue;
  343. }
  344. if (select (maxfd + 1, &rdset, &wrset, NULL, NULL) < 0)
  345. {
  346. if (errno == EINTR)
  347. continue;
  348. perror ("select");
  349. return EX_ERR;
  350. }
  351. if (timeout)
  352. {
  353. time_t now = time (NULL);
  354. if (now - ibuf.ts > timeout || now - obuf.ts > timeout)
  355. {
  356. fprintf (stderr, "ttyemu: I/O timeout\n");
  357. return EX_ERR;
  358. }
  359. }
  360. if (in >= 0)
  361. {
  362. if (bufavail (ibuf) && FD_ISSET (in, &rdset))
  363. bufread (ibuf, in, 0);
  364. }
  365. else if (master == -1)
  366. break;
  367. if (master >= 0 && FD_ISSET (master, &wrset))
  368. {
  369. if (!bufisempty (ibuf))
  370. bufwrite (ibuf, master);
  371. else if (in == -1 && eot)
  372. {
  373. DEBUG (("sent EOT"));
  374. if (write (master, &eot, 1) <= 0)
  375. {
  376. perror ("write");
  377. return EX_ERR;
  378. }
  379. eot = 0;
  380. }
  381. }
  382. if (master >= 0 && bufavail (obuf) && FD_ISSET (master, &rdset))
  383. bufread (obuf, master, 1);
  384. if (bufisempty (obuf))
  385. bufinit (obuf, 0);
  386. else
  387. {
  388. tr (&obuf);
  389. bufwrite (obuf, out);
  390. }
  391. if (bufisempty (ibuf))
  392. bufinit (ibuf, 0);
  393. }
  394. if (WIFEXITED (status))
  395. return WEXITSTATUS (status);
  396. if (WIFSIGNALED (status))
  397. fprintf (stderr, "ttyemu: child process %s failed on signal %d\n",
  398. argv[0], WTERMSIG (status));
  399. else
  400. fprintf (stderr, "ttyemu: child process %s failed\n", argv[0]);
  401. return EX_EXEC;
  402. }