script.c 5.8 KB

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