2008-02-27 11:54:59 +00:00
|
|
|
/* 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 16:57:19 +00:00
|
|
|
* The Regents of the University of California. All rights reserved.
|
2008-02-27 11:54:59 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2008-02-27 11:54:59 +00:00
|
|
|
*/
|
2011-04-11 01:29:49 +00:00
|
|
|
|
|
|
|
//usage:#define script_trivial_usage
|
|
|
|
//usage: "[-afq" IF_SCRIPTREPLAY("t") "] [-c PROG] [OUTFILE]"
|
|
|
|
//usage:#define script_full_usage "\n\n"
|
2011-06-05 01:58:28 +00:00
|
|
|
//usage: " -a Append output"
|
2011-04-11 01:29:49 +00:00
|
|
|
//usage: "\n -c PROG Run PROG, not shell"
|
|
|
|
//usage: "\n -f Flush output after each write"
|
|
|
|
//usage: "\n -q Quiet"
|
|
|
|
//usage: IF_SCRIPTREPLAY(
|
|
|
|
//usage: "\n -t Send timing to stderr"
|
|
|
|
//usage: )
|
|
|
|
|
2008-02-27 11:54:59 +00:00
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-03-17 09:00:54 +00:00
|
|
|
int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int script_main(int argc UNUSED_PARAM, char **argv)
|
2008-02-27 11:54:59 +00:00
|
|
|
{
|
2008-02-28 10:10:10 +00:00
|
|
|
int opt;
|
2008-02-27 11:54:59 +00:00
|
|
|
int mode;
|
2008-02-28 10:10:10 +00:00
|
|
|
int child_pid;
|
|
|
|
int attr_ok; /* NB: 0: ok */
|
|
|
|
int winsz_ok;
|
|
|
|
int pty;
|
2008-03-17 09:04:04 +00:00
|
|
|
char pty_line[GETPTY_BUFSIZE];
|
2008-02-28 10:10:10 +00:00
|
|
|
struct termios tt, rtt;
|
2008-02-27 11:54:59 +00:00
|
|
|
struct winsize win;
|
2008-02-28 10:10:10 +00:00
|
|
|
const char *fname = "typescript";
|
2008-02-27 11:54:59 +00:00
|
|
|
const char *shell;
|
|
|
|
char shell_opt[] = "-i";
|
|
|
|
char *shell_arg = NULL;
|
2009-05-19 15:36:16 +00:00
|
|
|
enum {
|
|
|
|
OPT_a = (1 << 0),
|
|
|
|
OPT_c = (1 << 1),
|
|
|
|
OPT_f = (1 << 2),
|
|
|
|
OPT_q = (1 << 3),
|
|
|
|
OPT_t = (1 << 4),
|
|
|
|
};
|
2008-02-27 11:54:59 +00:00
|
|
|
|
2009-06-19 10:10:38 +00:00
|
|
|
#if ENABLE_LONG_OPTS
|
2008-02-28 10:10:10 +00:00
|
|
|
static const char getopt_longopts[] ALIGN1 =
|
|
|
|
"append\0" No_argument "a"
|
|
|
|
"command\0" Required_argument "c"
|
|
|
|
"flush\0" No_argument "f"
|
|
|
|
"quiet\0" No_argument "q"
|
2009-06-09 16:40:07 +00:00
|
|
|
IF_SCRIPTREPLAY("timing\0" No_argument "t")
|
2008-02-28 10:10:10 +00:00
|
|
|
;
|
|
|
|
|
2009-06-09 16:40:07 +00:00
|
|
|
applet_long_options = getopt_longopts;
|
|
|
|
#endif
|
2009-06-05 12:55:26 +00:00
|
|
|
|
2008-02-27 11:54:59 +00:00
|
|
|
opt_complementary = "?1"; /* max one arg */
|
2009-05-19 15:36:16 +00:00
|
|
|
opt = getopt32(argv, "ac:fq" IF_SCRIPTREPLAY("t") , &shell_arg);
|
2008-02-27 11:54:59 +00:00
|
|
|
//argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argv[0]) {
|
|
|
|
fname = argv[0];
|
|
|
|
}
|
|
|
|
mode = O_CREAT|O_TRUNC|O_WRONLY;
|
2009-05-19 15:36:16 +00:00
|
|
|
if (opt & OPT_a) {
|
2008-02-27 11:54:59 +00:00
|
|
|
mode = O_CREAT|O_APPEND|O_WRONLY;
|
|
|
|
}
|
2009-05-19 15:36:16 +00:00
|
|
|
if (opt & OPT_c) {
|
2008-02-27 11:54:59 +00:00
|
|
|
shell_opt[1] = 'c';
|
|
|
|
}
|
2009-05-19 15:36:16 +00:00
|
|
|
if (!(opt & OPT_q)) {
|
2008-02-27 11:54:59 +00:00
|
|
|
printf("Script started, file is %s\n", fname);
|
|
|
|
}
|
2011-03-08 20:00:36 +00:00
|
|
|
shell = get_shell_name();
|
2008-02-27 11:54:59 +00:00
|
|
|
|
2008-05-19 08:18:50 +00:00
|
|
|
pty = xgetpty(pty_line);
|
2008-02-27 11:54:59 +00:00
|
|
|
|
|
|
|
/* 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 10:10:10 +00:00
|
|
|
/* "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 15:36:16 +00:00
|
|
|
signal(SIGCHLD, record_signo);
|
2008-02-28 10:10:10 +00:00
|
|
|
|
|
|
|
/* TODO: SIGWINCH? pass window size changes down to slave? */
|
2008-02-27 11:54:59 +00:00
|
|
|
|
2010-07-04 13:32:38 +00:00
|
|
|
child_pid = xvfork();
|
2008-02-27 11:54:59 +00:00
|
|
|
|
|
|
|
if (child_pid) {
|
|
|
|
/* parent */
|
2008-02-28 10:10:10 +00:00
|
|
|
#define buf bb_common_bufsiz1
|
2008-02-27 11:54:59 +00:00
|
|
|
struct pollfd pfd[2];
|
2008-02-28 10:10:10 +00:00
|
|
|
int outfd, count, loop;
|
2009-06-05 12:55:26 +00:00
|
|
|
double oldtime = ENABLE_SCRIPTREPLAY ? time(NULL) : 0;
|
2009-05-19 15:36:16 +00:00
|
|
|
smallint fd_count = 2;
|
2008-02-27 11:54:59 +00:00
|
|
|
|
|
|
|
outfd = xopen(fname, mode);
|
2008-05-06 19:11:41 +00:00
|
|
|
pfd[0].fd = pty;
|
2008-02-27 11:54:59 +00:00
|
|
|
pfd[0].events = POLLIN;
|
2009-05-19 15:36:16 +00:00
|
|
|
pfd[1].fd = STDIN_FILENO;
|
2008-02-27 11:54:59 +00:00
|
|
|
pfd[1].events = POLLIN;
|
|
|
|
ndelay_on(pty); /* this descriptor is not shared, can do this */
|
2009-05-19 15:36:16 +00:00
|
|
|
/* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */
|
2008-02-27 11:54:59 +00:00
|
|
|
|
|
|
|
/* 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 15:36:16 +00:00
|
|
|
while (fd_count && !bb_got_signal) {
|
2008-02-28 10:10:10 +00:00
|
|
|
/* not safe_poll! we want SIGCHLD to EINTR poll */
|
2008-05-06 19:11:41 +00:00
|
|
|
if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
|
2008-04-01 16:12:17 +00:00
|
|
|
/* If child exits too quickly, we may get EIO:
|
|
|
|
* for example, try "script -c true" */
|
|
|
|
break;
|
|
|
|
}
|
2010-03-02 14:02:45 +00:00
|
|
|
if (pfd[0].revents) {
|
2008-02-27 11:54:59 +00:00
|
|
|
errno = 0;
|
|
|
|
count = safe_read(pty, buf, sizeof(buf));
|
|
|
|
if (count <= 0 && errno != EAGAIN) {
|
2008-05-06 19:11:41 +00:00
|
|
|
/* err/eof from pty: exit */
|
|
|
|
goto restore;
|
2008-02-27 11:54:59 +00:00
|
|
|
}
|
|
|
|
if (count > 0) {
|
2009-06-05 12:55:26 +00:00
|
|
|
if (ENABLE_SCRIPTREPLAY && (opt & OPT_t)) {
|
2009-05-19 15:36:16 +00:00
|
|
|
struct timeval tv;
|
|
|
|
double newtime;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
|
|
|
|
fprintf(stderr, "%f %u\n", newtime - oldtime, count);
|
|
|
|
oldtime = newtime;
|
|
|
|
}
|
2008-05-19 09:48:17 +00:00
|
|
|
full_write(STDOUT_FILENO, buf, count);
|
2008-02-27 11:54:59 +00:00
|
|
|
full_write(outfd, buf, count);
|
2009-05-19 15:36:16 +00:00
|
|
|
if (opt & OPT_f) {
|
2008-02-27 11:54:59 +00:00
|
|
|
fsync(outfd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-02 14:02:45 +00:00
|
|
|
if (pfd[1].revents) {
|
2008-05-19 09:48:17 +00:00
|
|
|
count = safe_read(STDIN_FILENO, buf, sizeof(buf));
|
2008-05-06 19:11:41 +00:00
|
|
|
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 11:54:59 +00:00
|
|
|
}
|
2009-05-19 15:36:16 +00:00
|
|
|
/* 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 10:10:10 +00:00
|
|
|
* 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 */
|
|
|
|
while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
|
2008-05-19 09:48:17 +00:00
|
|
|
full_write(STDOUT_FILENO, buf, count);
|
2008-02-28 10:10:10 +00:00
|
|
|
full_write(outfd, buf, count);
|
|
|
|
}
|
2008-05-06 19:11:41 +00:00
|
|
|
restore:
|
2008-02-28 10:10:10 +00:00
|
|
|
if (attr_ok == 0)
|
|
|
|
tcsetattr(0, TCSAFLUSH, &tt);
|
2009-05-19 15:36:16 +00:00
|
|
|
if (!(opt & OPT_q))
|
2008-02-28 10:10:10 +00:00
|
|
|
printf("Script done, file is %s\n", fname);
|
|
|
|
return EXIT_SUCCESS;
|
2008-02-27 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 02:05:58 +00:00
|
|
|
close(0);
|
2008-02-28 10:10:10 +00:00
|
|
|
xopen(pty_line, O_RDWR); /* uses fd 0 */
|
2008-02-27 11:54:59 +00:00
|
|
|
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 00:15:11 +00:00
|
|
|
/* Non-ignored signals revert to SIG_DFL on exec anyway */
|
|
|
|
/*signal(SIGCHLD, SIG_DFL);*/
|
2009-02-26 12:29:59 +00:00
|
|
|
execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
|
2008-02-27 11:54:59 +00:00
|
|
|
bb_simple_perror_msg_and_die(shell);
|
|
|
|
}
|