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
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file License in this tarball for details.
|
|
|
|
*/
|
|
|
|
#include "libbb.h"
|
|
|
|
|
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;
|
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
|
2008-02-28 15:40:10 +05:30
|
|
|
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 22:10:07 +05:30
|
|
|
IF_SCRIPTREPLAY("timing\0" No_argument "t")
|
2008-02-28 15:40:10 +05:30
|
|
|
;
|
|
|
|
|
2009-06-09 22:10:07 +05:30
|
|
|
applet_long_options = getopt_longopts;
|
|
|
|
#endif
|
2009-06-05 18:25:26 +05:30
|
|
|
|
2008-02-27 17:24:59 +05:30
|
|
|
opt_complementary = "?1"; /* max one arg */
|
2009-05-19 21:06:16 +05:30
|
|
|
opt = getopt32(argv, "ac:fq" IF_SCRIPTREPLAY("t") , &shell_arg);
|
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);
|
|
|
|
}
|
|
|
|
shell = getenv("SHELL");
|
|
|
|
if (shell == NULL) {
|
2008-02-28 15:40:10 +05:30
|
|
|
shell = DEFAULT_SHELL;
|
2008-02-27 17:24:59 +05:30
|
|
|
}
|
|
|
|
|
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 */
|
2008-02-28 15:40:10 +05:30
|
|
|
#define buf bb_common_bufsiz1
|
2008-02-27 17:24:59 +05:30
|
|
|
struct pollfd pfd[2];
|
2008-02-28 15:40:10 +05:30
|
|
|
int outfd, count, loop;
|
2009-06-05 18:25:26 +05:30
|
|
|
double oldtime = ENABLE_SCRIPTREPLAY ? time(NULL) : 0;
|
2009-05-19 21:06:16 +05:30
|
|
|
smallint fd_count = 2;
|
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;
|
|
|
|
count = safe_read(pty, buf, sizeof(buf));
|
|
|
|
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) {
|
2009-06-05 18:25:26 +05:30
|
|
|
if (ENABLE_SCRIPTREPLAY && (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;
|
|
|
|
fprintf(stderr, "%f %u\n", newtime - oldtime, count);
|
|
|
|
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);
|
2009-05-19 21:06:16 +05:30
|
|
|
if (opt & OPT_f) {
|
2008-02-27 17:24:59 +05:30
|
|
|
fsync(outfd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-02 19:32:45 +05:30
|
|
|
if (pfd[1].revents) {
|
2008-05-19 15:18:17 +05:30
|
|
|
count = safe_read(STDIN_FILENO, buf, sizeof(buf));
|
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 */
|
|
|
|
while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 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);
|
|
|
|
}
|