1 /* vi: set sw=4 ts=4: */
3 * script implementation for busybox
5 * pascal.bellard@ads-lu.com
7 * Based on code from util-linux v 2.12r
9 * The Regents of the University of California. All rights reserved.
11 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
13 //config:config SCRIPT
14 //config: bool "script (8 kb)"
17 //config: The script makes typescript of terminal session.
19 //applet:IF_SCRIPT(APPLET(script, BB_DIR_USR_BIN, BB_SUID_DROP))
21 //kbuild:lib-$(CONFIG_SCRIPT) += script.o
23 //usage:#define script_trivial_usage
24 //usage: "[-afq] [-t[FILE]] [-c PROG] [OUTFILE]"
25 //usage:#define script_full_usage "\n\n"
26 //usage: "Default OUTFILE is 'typescript'"
28 //usage: "\n -a Append output"
29 //usage: "\n -c PROG Run PROG, not shell"
30 /* Accepted but has no effect (we never buffer output) */
31 /*//usage: "\n -f Flush output after each write"*/
32 //usage: "\n -q Quiet"
33 //usage: "\n -t[FILE] Send timing to stderr or FILE"
36 //-e: return exit code of the child
38 //FYI (reported as bbox bug #2749):
39 // > script -q -c 'echo -e -n "1\n2\n3\n"' /dev/null </dev/null >123.txt
40 // > The output file on full-blown ubuntu system contains 6 bytes.
41 // > Output on Busybox system (arm-linux) contains extra '\r' byte in each line.
42 //however, in my test, "script" from util-linux-2.28 seems to also add '\r' bytes.
45 #include "common_bufsiz.h"
47 int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
48 int script_main(int argc UNUSED_PARAM, char **argv)
53 int attr_ok; /* NB: 0: ok */
56 char pty_line[GETPTY_BUFSIZE];
57 struct termios tt, rtt;
60 const char *str_t = NULL;
61 const char *fname = "typescript";
63 char shell_opt[] = "-i";
64 char *shell_arg = NULL;
74 static const char getopt_longopts[] ALIGN1 =
75 "append\0" No_argument "a"
76 "command\0" Required_argument "c"
77 "flush\0" No_argument "f"
78 "quiet\0" No_argument "q"
79 "timing\0" Optional_argument "t"
82 applet_long_options = getopt_longopts;
85 opt_complementary = "?1"; /* max one arg */
86 opt = getopt32(argv, "ac:fqt::", &shell_arg, &str_t);
92 mode = O_CREAT|O_TRUNC|O_WRONLY;
94 mode = O_CREAT|O_APPEND|O_WRONLY;
100 printf("Script started, file is %s\n", fname);
104 timing_fp = xfopen_for_write(str_t);
107 shell = get_shell_name();
109 /* Some people run "script ... 0>&-".
110 * Our code assumes that STDIN_FILENO != pty.
111 * Ensure STDIN_FILENO is not closed:
115 pty = xgetpty(pty_line);
117 /* get current stdin's tty params */
118 attr_ok = tcgetattr(0, &tt);
119 winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
123 rtt.c_lflag &= ~ECHO;
124 tcsetattr(0, TCSAFLUSH, &rtt);
126 /* "script" from util-linux exits when child exits,
127 * we wouldn't wait for EOF from slave pty
128 * (output may be produced by grandchildren of child) */
129 signal(SIGCHLD, record_signo);
131 /* TODO: SIGWINCH? pass window size changes down to slave? */
133 child_pid = xvfork();
137 struct pollfd pfd[2];
138 int outfd, count, loop;
139 double oldtime = time(NULL);
140 smallint fd_count = 2;
142 #define buf bb_common_bufsiz1
143 setup_common_bufsiz();
145 outfd = xopen(fname, mode);
147 pfd[0].events = POLLIN;
148 pfd[1].fd = STDIN_FILENO;
149 pfd[1].events = POLLIN;
150 ndelay_on(pty); /* this descriptor is not shared, can do this */
151 /* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */
153 /* copy stdin to pty master input,
154 * copy pty master output to stdout and file */
155 /* TODO: don't use full_write's, use proper write buffering */
156 while (fd_count && !bb_got_signal) {
157 /* not safe_poll! we want SIGCHLD to EINTR poll */
158 if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
159 /* If child exits too quickly, we may get EIO:
160 * for example, try "script -c true" */
163 if (pfd[0].revents) {
165 count = safe_read(pty, buf, COMMON_BUFSIZE);
166 if (count <= 0 && errno != EAGAIN) {
167 /* err/eof from pty: exit */
175 gettimeofday(&tv, NULL);
176 newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
177 fprintf(timing_fp, "%f %u\n", newtime - oldtime, count);
180 full_write(STDOUT_FILENO, buf, count);
181 full_write(outfd, buf, count);
182 // If we'd be using (buffered) FILE i/o, we'd need this:
188 if (pfd[1].revents) {
189 count = safe_read(STDIN_FILENO, buf, COMMON_BUFSIZE);
191 /* err/eof from stdin: don't read stdin anymore */
195 full_write(pty, buf, count);
199 /* If loop was exited because SIGCHLD handler set bb_got_signal,
200 * there still can be some buffered output. But dont loop forever:
201 * we won't pump orphaned grandchildren's output indefinitely.
202 * Testcase: running this in script:
203 * exec dd if=/dev/zero bs=1M count=1
204 * must have "1+0 records in, 1+0 records out" captured too.
205 * (util-linux's script doesn't do this. buggy :) */
207 /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
208 while (--loop && (count = safe_read(pty, buf, COMMON_BUFSIZE)) > 0) {
209 full_write(STDOUT_FILENO, buf, count);
210 full_write(outfd, buf, count);
214 tcsetattr(0, TCSAFLUSH, &tt);
216 printf("Script done, file is %s\n", fname);
220 /* child: make pty slave to be input, output, error; run shell */
221 close(pty); /* close pty master */
222 /* open pty slave to fd 0,1,2 */
224 xopen(pty_line, O_RDWR); /* uses fd 0 */
227 /* copy our original stdin tty's parameters to pty */
229 tcsetattr(0, TCSAFLUSH, &tt);
231 ioctl(0, TIOCSWINSZ, (char *)&win);
232 /* set pty as a controlling tty */
234 ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
236 /* Non-ignored signals revert to SIG_DFL on exec anyway */
237 /*signal(SIGCHLD, SIG_DFL);*/
238 execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
239 bb_simple_perror_msg_and_die(shell);