script.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. pty = xgetpty(pty_line);
  76. /* get current stdin's tty params */
  77. attr_ok = tcgetattr(0, &tt);
  78. winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
  79. rtt = tt;
  80. cfmakeraw(&rtt);
  81. rtt.c_lflag &= ~ECHO;
  82. tcsetattr(0, TCSAFLUSH, &rtt);
  83. /* "script" from util-linux exits when child exits,
  84. * we wouldn't wait for EOF from slave pty
  85. * (output may be produced by grandchildren of child) */
  86. signal(SIGCHLD, record_signo);
  87. /* TODO: SIGWINCH? pass window size changes down to slave? */
  88. child_pid = xvfork();
  89. if (child_pid) {
  90. /* parent */
  91. #define buf bb_common_bufsiz1
  92. struct pollfd pfd[2];
  93. int outfd, count, loop;
  94. double oldtime = ENABLE_SCRIPTREPLAY ? time(NULL) : 0;
  95. smallint fd_count = 2;
  96. outfd = xopen(fname, mode);
  97. pfd[0].fd = pty;
  98. pfd[0].events = POLLIN;
  99. pfd[1].fd = STDIN_FILENO;
  100. pfd[1].events = POLLIN;
  101. ndelay_on(pty); /* this descriptor is not shared, can do this */
  102. /* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */
  103. /* copy stdin to pty master input,
  104. * copy pty master output to stdout and file */
  105. /* TODO: don't use full_write's, use proper write buffering */
  106. while (fd_count && !bb_got_signal) {
  107. /* not safe_poll! we want SIGCHLD to EINTR poll */
  108. if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
  109. /* If child exits too quickly, we may get EIO:
  110. * for example, try "script -c true" */
  111. break;
  112. }
  113. if (pfd[0].revents) {
  114. errno = 0;
  115. count = safe_read(pty, buf, sizeof(buf));
  116. if (count <= 0 && errno != EAGAIN) {
  117. /* err/eof from pty: exit */
  118. goto restore;
  119. }
  120. if (count > 0) {
  121. if (ENABLE_SCRIPTREPLAY && (opt & OPT_t)) {
  122. struct timeval tv;
  123. double newtime;
  124. gettimeofday(&tv, NULL);
  125. newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
  126. fprintf(stderr, "%f %u\n", newtime - oldtime, count);
  127. oldtime = newtime;
  128. }
  129. full_write(STDOUT_FILENO, buf, count);
  130. full_write(outfd, buf, count);
  131. if (opt & OPT_f) {
  132. fsync(outfd);
  133. }
  134. }
  135. }
  136. if (pfd[1].revents) {
  137. count = safe_read(STDIN_FILENO, buf, sizeof(buf));
  138. if (count <= 0) {
  139. /* err/eof from stdin: don't read stdin anymore */
  140. pfd[1].revents = 0;
  141. fd_count--;
  142. } else {
  143. full_write(pty, buf, count);
  144. }
  145. }
  146. }
  147. /* If loop was exited because SIGCHLD handler set bb_got_signal,
  148. * there still can be some buffered output. But dont loop forever:
  149. * we won't pump orphaned grandchildren's output indefinitely.
  150. * Testcase: running this in script:
  151. * exec dd if=/dev/zero bs=1M count=1
  152. * must have "1+0 records in, 1+0 records out" captured too.
  153. * (util-linux's script doesn't do this. buggy :) */
  154. loop = 999;
  155. /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
  156. while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
  157. full_write(STDOUT_FILENO, buf, count);
  158. full_write(outfd, buf, count);
  159. }
  160. restore:
  161. if (attr_ok == 0)
  162. tcsetattr(0, TCSAFLUSH, &tt);
  163. if (!(opt & OPT_q))
  164. printf("Script done, file is %s\n", fname);
  165. return EXIT_SUCCESS;
  166. }
  167. /* child: make pty slave to be input, output, error; run shell */
  168. close(pty); /* close pty master */
  169. /* open pty slave to fd 0,1,2 */
  170. close(0);
  171. xopen(pty_line, O_RDWR); /* uses fd 0 */
  172. xdup2(0, 1);
  173. xdup2(0, 2);
  174. /* copy our original stdin tty's parameters to pty */
  175. if (attr_ok == 0)
  176. tcsetattr(0, TCSAFLUSH, &tt);
  177. if (winsz_ok == 0)
  178. ioctl(0, TIOCSWINSZ, (char *)&win);
  179. /* set pty as a controlling tty */
  180. setsid();
  181. ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
  182. /* Non-ignored signals revert to SIG_DFL on exec anyway */
  183. /*signal(SIGCHLD, SIG_DFL);*/
  184. execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
  185. bb_simple_perror_msg_and_die(shell);
  186. }