3
0

script.c 5.3 KB

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