3
0

script.c 4.8 KB

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