script.c 5.7 KB

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