script.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * script implementation for busybox
  4. *
  5. * pascal.bellard@ads-lu.com
  6. *
  7. * Based on code from util-linux v 2.12r
  8. * Copyright (c) 1980
  9. * The Regents of the University of California. All rights reserved.
  10. *
  11. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  12. */
  13. #include "libbb.h"
  14. int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  15. int script_main(int argc UNUSED_PARAM, char **argv)
  16. {
  17. int opt;
  18. int mode;
  19. int child_pid;
  20. int attr_ok; /* NB: 0: ok */
  21. int winsz_ok;
  22. int pty;
  23. char pty_line[GETPTY_BUFSIZE];
  24. struct termios tt, rtt;
  25. struct winsize win;
  26. const char *fname = "typescript";
  27. const char *shell;
  28. char shell_opt[] = "-i";
  29. char *shell_arg = NULL;
  30. enum {
  31. OPT_a = (1 << 0),
  32. OPT_c = (1 << 1),
  33. OPT_f = (1 << 2),
  34. OPT_q = (1 << 3),
  35. OPT_t = (1 << 4),
  36. };
  37. #if ENABLE_LONG_OPTS
  38. static const char getopt_longopts[] ALIGN1 =
  39. "append\0" No_argument "a"
  40. "command\0" Required_argument "c"
  41. "flush\0" No_argument "f"
  42. "quiet\0" No_argument "q"
  43. IF_SCRIPTREPLAY("timing\0" No_argument "t")
  44. ;
  45. applet_long_options = getopt_longopts;
  46. #endif
  47. opt_complementary = "?1"; /* max one arg */
  48. opt = getopt32(argv, "ac:fq" IF_SCRIPTREPLAY("t") , &shell_arg);
  49. //argc -= optind;
  50. argv += optind;
  51. if (argv[0]) {
  52. fname = argv[0];
  53. }
  54. mode = O_CREAT|O_TRUNC|O_WRONLY;
  55. if (opt & OPT_a) {
  56. mode = O_CREAT|O_APPEND|O_WRONLY;
  57. }
  58. if (opt & OPT_c) {
  59. shell_opt[1] = 'c';
  60. }
  61. if (!(opt & OPT_q)) {
  62. printf("Script started, file is %s\n", fname);
  63. }
  64. shell = getenv("SHELL");
  65. if (shell == NULL) {
  66. shell = DEFAULT_SHELL;
  67. }
  68. pty = xgetpty(pty_line);
  69. /* get current stdin's tty params */
  70. attr_ok = tcgetattr(0, &tt);
  71. winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
  72. rtt = tt;
  73. cfmakeraw(&rtt);
  74. rtt.c_lflag &= ~ECHO;
  75. tcsetattr(0, TCSAFLUSH, &rtt);
  76. /* "script" from util-linux exits when child exits,
  77. * we wouldn't wait for EOF from slave pty
  78. * (output may be produced by grandchildren of child) */
  79. signal(SIGCHLD, record_signo);
  80. /* TODO: SIGWINCH? pass window size changes down to slave? */
  81. child_pid = xvfork();
  82. if (child_pid) {
  83. /* parent */
  84. #define buf bb_common_bufsiz1
  85. struct pollfd pfd[2];
  86. int outfd, count, loop;
  87. double oldtime = ENABLE_SCRIPTREPLAY ? time(NULL) : 0;
  88. smallint fd_count = 2;
  89. outfd = xopen(fname, mode);
  90. pfd[0].fd = pty;
  91. pfd[0].events = POLLIN;
  92. pfd[1].fd = STDIN_FILENO;
  93. pfd[1].events = POLLIN;
  94. ndelay_on(pty); /* this descriptor is not shared, can do this */
  95. /* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */
  96. /* copy stdin to pty master input,
  97. * copy pty master output to stdout and file */
  98. /* TODO: don't use full_write's, use proper write buffering */
  99. while (fd_count && !bb_got_signal) {
  100. /* not safe_poll! we want SIGCHLD to EINTR poll */
  101. if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
  102. /* If child exits too quickly, we may get EIO:
  103. * for example, try "script -c true" */
  104. break;
  105. }
  106. if (pfd[0].revents) {
  107. errno = 0;
  108. count = safe_read(pty, buf, sizeof(buf));
  109. if (count <= 0 && errno != EAGAIN) {
  110. /* err/eof from pty: exit */
  111. goto restore;
  112. }
  113. if (count > 0) {
  114. if (ENABLE_SCRIPTREPLAY && (opt & OPT_t)) {
  115. struct timeval tv;
  116. double newtime;
  117. gettimeofday(&tv, NULL);
  118. newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
  119. fprintf(stderr, "%f %u\n", newtime - oldtime, count);
  120. oldtime = newtime;
  121. }
  122. full_write(STDOUT_FILENO, buf, count);
  123. full_write(outfd, buf, count);
  124. if (opt & OPT_f) {
  125. fsync(outfd);
  126. }
  127. }
  128. }
  129. if (pfd[1].revents) {
  130. count = safe_read(STDIN_FILENO, buf, sizeof(buf));
  131. if (count <= 0) {
  132. /* err/eof from stdin: don't read stdin anymore */
  133. pfd[1].revents = 0;
  134. fd_count--;
  135. } else {
  136. full_write(pty, buf, count);
  137. }
  138. }
  139. }
  140. /* If loop was exited because SIGCHLD handler set bb_got_signal,
  141. * there still can be some buffered output. But dont loop forever:
  142. * we won't pump orphaned grandchildren's output indefinitely.
  143. * Testcase: running this in script:
  144. * exec dd if=/dev/zero bs=1M count=1
  145. * must have "1+0 records in, 1+0 records out" captured too.
  146. * (util-linux's script doesn't do this. buggy :) */
  147. loop = 999;
  148. /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
  149. while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
  150. full_write(STDOUT_FILENO, buf, count);
  151. full_write(outfd, buf, count);
  152. }
  153. restore:
  154. if (attr_ok == 0)
  155. tcsetattr(0, TCSAFLUSH, &tt);
  156. if (!(opt & OPT_q))
  157. printf("Script done, file is %s\n", fname);
  158. return EXIT_SUCCESS;
  159. }
  160. /* child: make pty slave to be input, output, error; run shell */
  161. close(pty); /* close pty master */
  162. /* open pty slave to fd 0,1,2 */
  163. close(0);
  164. xopen(pty_line, O_RDWR); /* uses fd 0 */
  165. xdup2(0, 1);
  166. xdup2(0, 2);
  167. /* copy our original stdin tty's parameters to pty */
  168. if (attr_ok == 0)
  169. tcsetattr(0, TCSAFLUSH, &tt);
  170. if (winsz_ok == 0)
  171. ioctl(0, TIOCSWINSZ, (char *)&win);
  172. /* set pty as a controlling tty */
  173. setsid();
  174. ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
  175. /* Non-ignored signals revert to SIG_DFL on exec anyway */
  176. /*signal(SIGCHLD, SIG_DFL);*/
  177. execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
  178. bb_simple_perror_msg_and_die(shell);
  179. }