blob: 963435335e0624b52d9a646bd7357ad86c041812 [file] [log] [blame]
Denis Vlasenko5014dad2008-02-27 11:54:59 +00001/* 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
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02009 * The Regents of the University of California. All rights reserved.
Denis Vlasenko5014dad2008-02-27 11:54:59 +000010 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020011 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenko5014dad2008-02-27 11:54:59 +000012 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010013//config:config SCRIPT
Denys Vlasenkob097a842018-12-28 03:20:17 +010014//config: bool "script (8.6 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010015//config: default y
16//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: The script makes typescript of terminal session.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010018
19//applet:IF_SCRIPT(APPLET(script, BB_DIR_USR_BIN, BB_SUID_DROP))
20
21//kbuild:lib-$(CONFIG_SCRIPT) += script.o
Pere Orga5bc8c002011-04-11 03:29:49 +020022
23//usage:#define script_trivial_usage
Denys Vlasenko269b36a2017-08-07 02:12:36 +020024//usage: "[-afq] [-t[FILE]] [-c PROG] [OUTFILE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage:#define script_full_usage "\n\n"
Denys Vlasenko269b36a2017-08-07 02:12:36 +020026//usage: "Default OUTFILE is 'typescript'"
27//usage: "\n"
28//usage: "\n -a Append output"
Pere Orga5bc8c002011-04-11 03:29:49 +020029//usage: "\n -c PROG Run PROG, not shell"
Denys Vlasenko4dea1ed2017-08-07 02:21:34 +020030/* Accepted but has no effect (we never buffer output) */
31/*//usage: "\n -f Flush output after each write"*/
Pere Orga5bc8c002011-04-11 03:29:49 +020032//usage: "\n -q Quiet"
Denys Vlasenko269b36a2017-08-07 02:12:36 +020033//usage: "\n -t[FILE] Send timing to stderr or FILE"
Denys Vlasenkodd55d5d2017-08-07 01:53:17 +020034
35//util-linux-2.28:
Denys Vlasenkodd55d5d2017-08-07 01:53:17 +020036//-e: return exit code of the child
37
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.
Pere Orga5bc8c002011-04-11 03:29:49 +020043
Denis Vlasenko5014dad2008-02-27 11:54:59 +000044#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020045#include "common_bufsiz.h"
Denis Vlasenko5014dad2008-02-27 11:54:59 +000046
Denis Vlasenko68404f12008-03-17 09:00:54 +000047int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000048int script_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko5014dad2008-02-27 11:54:59 +000049{
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000050 int opt;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000051 int mode;
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000052 int child_pid;
53 int attr_ok; /* NB: 0: ok */
54 int winsz_ok;
55 int pty;
Denis Vlasenko85c24712008-03-17 09:04:04 +000056 char pty_line[GETPTY_BUFSIZE];
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000057 struct termios tt, rtt;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000058 struct winsize win;
Denys Vlasenko269b36a2017-08-07 02:12:36 +020059 FILE *timing_fp;
60 const char *str_t = NULL;
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000061 const char *fname = "typescript";
Denis Vlasenko5014dad2008-02-27 11:54:59 +000062 const char *shell;
63 char shell_opt[] = "-i";
64 char *shell_arg = NULL;
Denys Vlasenko5e611152009-05-19 17:36:16 +020065 enum {
66 OPT_a = (1 << 0),
67 OPT_c = (1 << 1),
68 OPT_f = (1 << 2),
69 OPT_q = (1 << 3),
Denys Vlasenko5e611152009-05-19 17:36:16 +020070 OPT_t = (1 << 4),
Denys Vlasenko5e611152009-05-19 17:36:16 +020071 };
Denis Vlasenko5014dad2008-02-27 11:54:59 +000072
Denys Vlasenkof3b92d32009-06-19 12:10:38 +020073#if ENABLE_LONG_OPTS
Denys Vlasenko00677b52017-08-08 14:59:35 +020074 static const char script_longopts[] ALIGN1 =
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000075 "append\0" No_argument "a"
76 "command\0" Required_argument "c"
77 "flush\0" No_argument "f"
78 "quiet\0" No_argument "q"
Denys Vlasenko269b36a2017-08-07 02:12:36 +020079 "timing\0" Optional_argument "t"
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000080 ;
Denys Vlasenko2634bf32009-06-09 18:40:07 +020081#endif
Denys Vlasenko53f17912009-06-05 14:55:26 +020082
Denys Vlasenko22542ec2017-08-08 21:55:02 +020083 opt = getopt32long(argv, "^" "ac:fqt::" "\0" "?1"/* max one arg */,
84 script_longopts,
85 &shell_arg, &str_t
86 );
Denis Vlasenko5014dad2008-02-27 11:54:59 +000087 //argc -= optind;
88 argv += optind;
89 if (argv[0]) {
90 fname = argv[0];
91 }
92 mode = O_CREAT|O_TRUNC|O_WRONLY;
Denys Vlasenko5e611152009-05-19 17:36:16 +020093 if (opt & OPT_a) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +000094 mode = O_CREAT|O_APPEND|O_WRONLY;
95 }
Denys Vlasenko5e611152009-05-19 17:36:16 +020096 if (opt & OPT_c) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +000097 shell_opt[1] = 'c';
98 }
Denys Vlasenko5e611152009-05-19 17:36:16 +020099 if (!(opt & OPT_q)) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000100 printf("Script started, file is %s\n", fname);
101 }
Denys Vlasenko269b36a2017-08-07 02:12:36 +0200102 timing_fp = stderr;
103 if (str_t) {
104 timing_fp = xfopen_for_write(str_t);
105 }
Denys Vlasenko438b4ac2014-03-16 12:34:53 +0100106
Denys Vlasenko681efe22011-03-08 21:00:36 +0100107 shell = get_shell_name();
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000108
Denys Vlasenko438b4ac2014-03-16 12:34:53 +0100109 /* Some people run "script ... 0>&-".
110 * Our code assumes that STDIN_FILENO != pty.
111 * Ensure STDIN_FILENO is not closed:
112 */
113 bb_sanitize_stdio();
114
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000115 pty = xgetpty(pty_line);
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000116
117 /* get current stdin's tty params */
118 attr_ok = tcgetattr(0, &tt);
119 winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
120
121 rtt = tt;
122 cfmakeraw(&rtt);
123 rtt.c_lflag &= ~ECHO;
124 tcsetattr(0, TCSAFLUSH, &rtt);
125
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000126 /* "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) */
Denys Vlasenko5e611152009-05-19 17:36:16 +0200129 signal(SIGCHLD, record_signo);
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000130
131 /* TODO: SIGWINCH? pass window size changes down to slave? */
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000132
Pascal Bellard926031b2010-07-04 15:32:38 +0200133 child_pid = xvfork();
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000134
135 if (child_pid) {
136 /* parent */
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000137 struct pollfd pfd[2];
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000138 int outfd, count, loop;
Denys Vlasenkodd55d5d2017-08-07 01:53:17 +0200139 double oldtime = time(NULL);
Denys Vlasenko5e611152009-05-19 17:36:16 +0200140 smallint fd_count = 2;
Denys Vlasenko269b36a2017-08-07 02:12:36 +0200141
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200142#define buf bb_common_bufsiz1
143 setup_common_bufsiz();
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000144
145 outfd = xopen(fname, mode);
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000146 pfd[0].fd = pty;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000147 pfd[0].events = POLLIN;
Denys Vlasenko5e611152009-05-19 17:36:16 +0200148 pfd[1].fd = STDIN_FILENO;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000149 pfd[1].events = POLLIN;
150 ndelay_on(pty); /* this descriptor is not shared, can do this */
Denys Vlasenko5e611152009-05-19 17:36:16 +0200151 /* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000152
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 */
Denys Vlasenko5e611152009-05-19 17:36:16 +0200156 while (fd_count && !bb_got_signal) {
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000157 /* not safe_poll! we want SIGCHLD to EINTR poll */
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000158 if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
Denis Vlasenko9dedf722008-04-01 16:12:17 +0000159 /* If child exits too quickly, we may get EIO:
160 * for example, try "script -c true" */
161 break;
162 }
Denys Vlasenko88aa5582010-03-02 15:02:45 +0100163 if (pfd[0].revents) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000164 errno = 0;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200165 count = safe_read(pty, buf, COMMON_BUFSIZE);
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000166 if (count <= 0 && errno != EAGAIN) {
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000167 /* err/eof from pty: exit */
168 goto restore;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000169 }
170 if (count > 0) {
Denys Vlasenkodd55d5d2017-08-07 01:53:17 +0200171 if (opt & OPT_t) {
Denys Vlasenko5e611152009-05-19 17:36:16 +0200172 struct timeval tv;
173 double newtime;
174
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100175 xgettimeofday(&tv);
Denys Vlasenko5e611152009-05-19 17:36:16 +0200176 newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
Denys Vlasenko269b36a2017-08-07 02:12:36 +0200177 fprintf(timing_fp, "%f %u\n", newtime - oldtime, count);
Denys Vlasenko5e611152009-05-19 17:36:16 +0200178 oldtime = newtime;
179 }
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000180 full_write(STDOUT_FILENO, buf, count);
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000181 full_write(outfd, buf, count);
Denys Vlasenko4dea1ed2017-08-07 02:21:34 +0200182 // If we'd be using (buffered) FILE i/o, we'd need this:
183 //if (opt & OPT_f) {
184 // fflush(outfd);
185 //}
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000186 }
187 }
Denys Vlasenko88aa5582010-03-02 15:02:45 +0100188 if (pfd[1].revents) {
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200189 count = safe_read(STDIN_FILENO, buf, COMMON_BUFSIZE);
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000190 if (count <= 0) {
191 /* err/eof from stdin: don't read stdin anymore */
192 pfd[1].revents = 0;
193 fd_count--;
194 } else {
195 full_write(pty, buf, count);
196 }
197 }
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000198 }
Denys Vlasenko5e611152009-05-19 17:36:16 +0200199 /* If loop was exited because SIGCHLD handler set bb_got_signal,
200 * there still can be some buffered output. But dont loop forever:
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000201 * 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 :) */
206 loop = 999;
207 /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200208 while (--loop && (count = safe_read(pty, buf, COMMON_BUFSIZE)) > 0) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000209 full_write(STDOUT_FILENO, buf, count);
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000210 full_write(outfd, buf, count);
211 }
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000212 restore:
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000213 if (attr_ok == 0)
214 tcsetattr(0, TCSAFLUSH, &tt);
Denys Vlasenko5e611152009-05-19 17:36:16 +0200215 if (!(opt & OPT_q))
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000216 printf("Script done, file is %s\n", fname);
217 return EXIT_SUCCESS;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000218 }
219
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 */
Denis Vlasenko42cc3042008-03-24 02:05:58 +0000223 close(0);
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000224 xopen(pty_line, O_RDWR); /* uses fd 0 */
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000225 xdup2(0, 1);
226 xdup2(0, 2);
227 /* copy our original stdin tty's parameters to pty */
228 if (attr_ok == 0)
229 tcsetattr(0, TCSAFLUSH, &tt);
230 if (winsz_ok == 0)
231 ioctl(0, TIOCSWINSZ, (char *)&win);
232 /* set pty as a controlling tty */
233 setsid();
234 ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
235
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000236 /* Non-ignored signals revert to SIG_DFL on exec anyway */
237 /*signal(SIGCHLD, SIG_DFL);*/
Denis Vlasenkof09f4e02009-02-26 12:29:59 +0000238 execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000239 bb_simple_perror_msg_and_die(shell);
240}