script.c 6.6 KB

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