2008-02-27 17:24:59 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* script implementation for busybox
|
|
|
|
*
|
|
|
|
* pascal.bellard@ads-lu.com
|
|
|
|
*
|
|
|
|
* Based on code from util-linux v 2.12r
|
|
|
|
* Copyright (c) 1980
|
2010-10-28 22:27:19 +05:30
|
|
|
* The Regents of the University of California. All rights reserved.
|
2008-02-27 17:24:59 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2008-02-27 17:24:59 +05:30
|
|
|
*/
|
2016-11-23 16:16:32 +05:30
|
|
|
//config:config SCRIPT
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "script (8 kb)"
|
2016-11-23 16:16:32 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: The script makes typescript of terminal session.
|
2016-11-23 16:16:32 +05:30
|
|
|
|
|
|
|
//applet:IF_SCRIPT(APPLET(script, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_SCRIPT) += script.o
|
2011-04-11 06:59:49 +05:30
|
|
|
|
|
|
|
//usage:#define script_trivial_usage
|
2017-08-07 05:42:36 +05:30
|
|
|
//usage: "[-afq] [-t[FILE]] [-c PROG] [OUTFILE]"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define script_full_usage "\n\n"
|
2017-08-07 05:42:36 +05:30
|
|
|
//usage: "Default OUTFILE is 'typescript'"
|
|
|
|
//usage: "\n"
|
|
|
|
//usage: "\n -a Append output"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\n -c PROG Run PROG, not shell"
|
2017-08-07 05:51:34 +05:30
|
|
|
/* Accepted but has no effect (we never buffer output) */
|
|
|
|
/*//usage: "\n -f Flush output after each write"*/
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\n -q Quiet"
|
2017-08-07 05:42:36 +05:30
|
|
|
//usage: "\n -t[FILE] Send timing to stderr or FILE"
|
2017-08-07 05:23:17 +05:30
|
|
|
|
|
|
|
//util-linux-2.28:
|
|
|
|
//-e: return exit code of the child
|
|
|
|
|
|
|
|
//FYI (reported as bbox bug #2749):
|
|
|
|
// > script -q -c 'echo -e -n "1\n2\n3\n"' /dev/null </dev/null >123.txt
|
|
|
|
// > The output file on full-blown ubuntu system contains 6 bytes.
|
|
|
|
// > Output on Busybox system (arm-linux) contains extra '\r' byte in each line.
|
|
|
|
//however, in my test, "script" from util-linux-2.28 seems to also add '\r' bytes.
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2008-02-27 17:24:59 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2008-02-27 17:24:59 +05:30
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int script_main(int argc UNUSED_PARAM, char **argv)
|
2008-02-27 17:24:59 +05:30
|
|
|
{
|
2008-02-28 15:40:10 +05:30
|
|
|
int opt;
|
2008-02-27 17:24:59 +05:30
|
|
|
int mode;
|
2008-02-28 15:40:10 +05:30
|
|
|
int child_pid;
|
|
|
|
int attr_ok; /* NB: 0: ok */
|
|
|
|
int winsz_ok;
|
|
|
|
int pty;
|
2008-03-17 14:34:04 +05:30
|
|
|
char pty_line[GETPTY_BUFSIZE];
|
2008-02-28 15:40:10 +05:30
|
|
|
struct termios tt, rtt;
|
2008-02-27 17:24:59 +05:30
|
|
|
struct winsize win;
|
2017-08-07 05:42:36 +05:30
|
|
|
FILE *timing_fp;
|
|
|
|
const char *str_t = NULL;
|
2008-02-28 15:40:10 +05:30
|
|
|
const char *fname = "typescript";
|
2008-02-27 17:24:59 +05:30
|
|
|
const char *shell;
|
|
|
|
char shell_opt[] = "-i";
|
|
|
|
char *shell_arg = NULL;
|
2009-05-19 21:06:16 +05:30
|
|
|
enum {
|
|
|
|
OPT_a = (1 << 0),
|
|
|
|
OPT_c = (1 << 1),
|
|
|
|
OPT_f = (1 << 2),
|
|
|
|
OPT_q = (1 << 3),
|
|
|
|
OPT_t = (1 << 4),
|
|
|
|
};
|
2008-02-27 17:24:59 +05:30
|
|
|
|
2009-06-19 15:40:38 +05:30
|
|
|
#if ENABLE_LONG_OPTS
|
2017-08-08 18:29:35 +05:30
|
|
|
static const char script_longopts[] ALIGN1 =
|
2008-02-28 15:40:10 +05:30
|
|
|
"append\0" No_argument "a"
|
|
|
|
"command\0" Required_argument "c"
|
|
|
|
"flush\0" No_argument "f"
|
|
|
|
"quiet\0" No_argument "q"
|
2017-08-07 05:42:36 +05:30
|
|
|
"timing\0" Optional_argument "t"
|
2008-02-28 15:40:10 +05:30
|
|
|
;
|
2009-06-09 22:10:07 +05:30
|
|
|
#endif
|
2009-06-05 18:25:26 +05:30
|
|
|
|
2017-08-09 01:25:02 +05:30
|
|
|
opt = getopt32long(argv, "^" "ac:fqt::" "\0" "?1"/* max one arg */,
|
|
|
|
script_longopts,
|
|
|
|
&shell_arg, &str_t
|
|
|
|
);
|
2008-02-27 17:24:59 +05:30
|
|
|
//argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argv[0]) {
|
|
|
|
fname = argv[0];
|
|
|
|
}
|
|
|
|
mode = O_CREAT|O_TRUNC|O_WRONLY;
|
2009-05-19 21:06:16 +05:30
|
|
|
if (opt & OPT_a) {
|
2008-02-27 17:24:59 +05:30
|
|
|
mode = O_CREAT|O_APPEND|O_WRONLY;
|
|
|
|
}
|
2009-05-19 21:06:16 +05:30
|
|
|
if (opt & OPT_c) {
|
2008-02-27 17:24:59 +05:30
|
|
|
shell_opt[1] = 'c';
|
|
|
|
}
|
2009-05-19 21:06:16 +05:30
|
|
|
if (!(opt & OPT_q)) {
|
2008-02-27 17:24:59 +05:30
|
|
|
printf("Script started, file is %s\n", fname);
|
|
|
|
}
|
2017-08-07 05:42:36 +05:30
|
|
|
timing_fp = stderr;
|
|
|
|
if (str_t) {
|
|
|
|
timing_fp = xfopen_for_write(str_t);
|
|
|
|
}
|
2014-03-16 17:04:53 +05:30
|
|
|
|
2011-03-09 01:30:36 +05:30
|
|
|
shell = get_shell_name();
|
2008-02-27 17:24:59 +05:30
|
|
|
|
2014-03-16 17:04:53 +05:30
|
|
|
/* Some people run "script ... 0>&-".
|
|
|
|
* Our code assumes that STDIN_FILENO != pty.
|
|
|
|
* Ensure STDIN_FILENO is not closed:
|
|
|
|
*/
|
|
|
|
bb_sanitize_stdio();
|
|
|
|
|
2008-05-19 13:48:50 +05:30
|
|
|
pty = xgetpty(pty_line);
|
2008-02-27 17:24:59 +05:30
|
|
|
|
|
|
|
/* get current stdin's tty params */
|
|
|
|
attr_ok = tcgetattr(0, &tt);
|
|
|
|
winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
|
|
|
|
|
|
|
|
rtt = tt;
|
|
|
|
cfmakeraw(&rtt);
|
|
|
|
rtt.c_lflag &= ~ECHO;
|
|
|
|
tcsetattr(0, TCSAFLUSH, &rtt);
|
|
|
|
|
2008-02-28 15:40:10 +05:30
|
|
|
/* "script" from util-linux exits when child exits,
|
|
|
|
* we wouldn't wait for EOF from slave pty
|
|
|
|
* (output may be produced by grandchildren of child) */
|
2009-05-19 21:06:16 +05:30
|
|
|
signal(SIGCHLD, record_signo);
|
2008-02-28 15:40:10 +05:30
|
|
|
|
|
|
|
/* TODO: SIGWINCH? pass window size changes down to slave? */
|
2008-02-27 17:24:59 +05:30
|
|
|
|
2010-07-04 19:02:38 +05:30
|
|
|
child_pid = xvfork();
|
2008-02-27 17:24:59 +05:30
|
|
|
|
|
|
|
if (child_pid) {
|
|
|
|
/* parent */
|
|
|
|
struct pollfd pfd[2];
|
2008-02-28 15:40:10 +05:30
|
|
|
int outfd, count, loop;
|
2017-08-07 05:23:17 +05:30
|
|
|
double oldtime = time(NULL);
|
2009-05-19 21:06:16 +05:30
|
|
|
smallint fd_count = 2;
|
2017-08-07 05:42:36 +05:30
|
|
|
|
2016-04-21 22:08:51 +05:30
|
|
|
#define buf bb_common_bufsiz1
|
|
|
|
setup_common_bufsiz();
|
2008-02-27 17:24:59 +05:30
|
|
|
|
|
|
|
outfd = xopen(fname, mode);
|
2008-05-07 00:41:41 +05:30
|
|
|
pfd[0].fd = pty;
|
2008-02-27 17:24:59 +05:30
|
|
|
pfd[0].events = POLLIN;
|
2009-05-19 21:06:16 +05:30
|
|
|
pfd[1].fd = STDIN_FILENO;
|
2008-02-27 17:24:59 +05:30
|
|
|
pfd[1].events = POLLIN;
|
|
|
|
ndelay_on(pty); /* this descriptor is not shared, can do this */
|
2009-05-19 21:06:16 +05:30
|
|
|
/* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */
|
2008-02-27 17:24:59 +05:30
|
|
|
|
|
|
|
/* copy stdin to pty master input,
|
|
|
|
* copy pty master output to stdout and file */
|
|
|
|
/* TODO: don't use full_write's, use proper write buffering */
|
2009-05-19 21:06:16 +05:30
|
|
|
while (fd_count && !bb_got_signal) {
|
2008-02-28 15:40:10 +05:30
|
|
|
/* not safe_poll! we want SIGCHLD to EINTR poll */
|
2008-05-07 00:41:41 +05:30
|
|
|
if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
|
2008-04-01 21:42:17 +05:30
|
|
|
/* If child exits too quickly, we may get EIO:
|
|
|
|
* for example, try "script -c true" */
|
|
|
|
break;
|
|
|
|
}
|
2010-03-02 19:32:45 +05:30
|
|
|
if (pfd[0].revents) {
|
2008-02-27 17:24:59 +05:30
|
|
|
errno = 0;
|
2016-04-21 22:08:51 +05:30
|
|
|
count = safe_read(pty, buf, COMMON_BUFSIZE);
|
2008-02-27 17:24:59 +05:30
|
|
|
if (count <= 0 && errno != EAGAIN) {
|
2008-05-07 00:41:41 +05:30
|
|
|
/* err/eof from pty: exit */
|
|
|
|
goto restore;
|
2008-02-27 17:24:59 +05:30
|
|
|
}
|
|
|
|
if (count > 0) {
|
2017-08-07 05:23:17 +05:30
|
|
|
if (opt & OPT_t) {
|
2009-05-19 21:06:16 +05:30
|
|
|
struct timeval tv;
|
|
|
|
double newtime;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
|
2017-08-07 05:42:36 +05:30
|
|
|
fprintf(timing_fp, "%f %u\n", newtime - oldtime, count);
|
2009-05-19 21:06:16 +05:30
|
|
|
oldtime = newtime;
|
|
|
|
}
|
2008-05-19 15:18:17 +05:30
|
|
|
full_write(STDOUT_FILENO, buf, count);
|
2008-02-27 17:24:59 +05:30
|
|
|
full_write(outfd, buf, count);
|
2017-08-07 05:51:34 +05:30
|
|
|
// If we'd be using (buffered) FILE i/o, we'd need this:
|
|
|
|
//if (opt & OPT_f) {
|
|
|
|
// fflush(outfd);
|
|
|
|
//}
|
2008-02-27 17:24:59 +05:30
|
|
|
}
|
|
|
|
}
|
2010-03-02 19:32:45 +05:30
|
|
|
if (pfd[1].revents) {
|
2016-04-21 22:08:51 +05:30
|
|
|
count = safe_read(STDIN_FILENO, buf, COMMON_BUFSIZE);
|
2008-05-07 00:41:41 +05:30
|
|
|
if (count <= 0) {
|
|
|
|
/* err/eof from stdin: don't read stdin anymore */
|
|
|
|
pfd[1].revents = 0;
|
|
|
|
fd_count--;
|
|
|
|
} else {
|
|
|
|
full_write(pty, buf, count);
|
|
|
|
}
|
|
|
|
}
|
2008-02-27 17:24:59 +05:30
|
|
|
}
|
2009-05-19 21:06:16 +05:30
|
|
|
/* If loop was exited because SIGCHLD handler set bb_got_signal,
|
|
|
|
* there still can be some buffered output. But dont loop forever:
|
2008-02-28 15:40:10 +05:30
|
|
|
* we won't pump orphaned grandchildren's output indefinitely.
|
|
|
|
* Testcase: running this in script:
|
|
|
|
* exec dd if=/dev/zero bs=1M count=1
|
|
|
|
* must have "1+0 records in, 1+0 records out" captured too.
|
|
|
|
* (util-linux's script doesn't do this. buggy :) */
|
|
|
|
loop = 999;
|
|
|
|
/* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
|
2016-04-21 22:08:51 +05:30
|
|
|
while (--loop && (count = safe_read(pty, buf, COMMON_BUFSIZE)) > 0) {
|
2008-05-19 15:18:17 +05:30
|
|
|
full_write(STDOUT_FILENO, buf, count);
|
2008-02-28 15:40:10 +05:30
|
|
|
full_write(outfd, buf, count);
|
|
|
|
}
|
2008-05-07 00:41:41 +05:30
|
|
|
restore:
|
2008-02-28 15:40:10 +05:30
|
|
|
if (attr_ok == 0)
|
|
|
|
tcsetattr(0, TCSAFLUSH, &tt);
|
2009-05-19 21:06:16 +05:30
|
|
|
if (!(opt & OPT_q))
|
2008-02-28 15:40:10 +05:30
|
|
|
printf("Script done, file is %s\n", fname);
|
|
|
|
return EXIT_SUCCESS;
|
2008-02-27 17:24:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* child: make pty slave to be input, output, error; run shell */
|
|
|
|
close(pty); /* close pty master */
|
|
|
|
/* open pty slave to fd 0,1,2 */
|
2008-03-24 07:35:58 +05:30
|
|
|
close(0);
|
2008-02-28 15:40:10 +05:30
|
|
|
xopen(pty_line, O_RDWR); /* uses fd 0 */
|
2008-02-27 17:24:59 +05:30
|
|
|
xdup2(0, 1);
|
|
|
|
xdup2(0, 2);
|
|
|
|
/* copy our original stdin tty's parameters to pty */
|
|
|
|
if (attr_ok == 0)
|
|
|
|
tcsetattr(0, TCSAFLUSH, &tt);
|
|
|
|
if (winsz_ok == 0)
|
|
|
|
ioctl(0, TIOCSWINSZ, (char *)&win);
|
|
|
|
/* set pty as a controlling tty */
|
|
|
|
setsid();
|
|
|
|
ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
|
|
|
|
|
2008-11-09 05:45:11 +05:30
|
|
|
/* Non-ignored signals revert to SIG_DFL on exec anyway */
|
|
|
|
/*signal(SIGCHLD, SIG_DFL);*/
|
2009-02-26 17:59:59 +05:30
|
|
|
execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
|
2008-02-27 17:24:59 +05:30
|
|
|
bb_simple_perror_msg_and_die(shell);
|
|
|
|
}
|