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
|
|
|
|
* 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 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),
|
|
|
|
#if ENABLE_SCRIPTREPLAY
|
|
|
|
OPT_t = (1 << 4),
|
|
|
|
#endif
|
|
|
|
};
|
2008-02-27 11:54:59 +00:00
|
|
|
|
|
|
|
#if ENABLE_GETOPT_LONG
|
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-05-19 15:36:16 +00:00
|
|
|
# if ENABLE_SCRIPTREPLAY
|
|
|
|
"timing\0" No_argument "t"
|
|
|
|
# endif
|
2008-02-28 10:10:10 +00:00
|
|
|
;
|
|
|
|
|
2008-02-27 11:54:59 +00:00
|
|
|
applet_long_options = getopt_longopts;
|
|
|
|
#endif
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
shell = getenv("SHELL");
|
|
|
|
if (shell == NULL) {
|
2008-02-28 10:10:10 +00:00
|
|
|
shell = DEFAULT_SHELL;
|
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
|
|
|
|
2008-07-01 15:59:42 +00:00
|
|
|
child_pid = vfork();
|
|
|
|
if (child_pid < 0) {
|
|
|
|
bb_perror_msg_and_die("vfork");
|
|
|
|
}
|
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-05-19 15:36:16 +00:00
|
|
|
#if ENABLE_SCRIPTREPLAY
|
|
|
|
double oldtime = time(NULL);
|
|
|
|
#endif
|
|
|
|
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;
|
|
|
|
}
|
2008-02-27 11:54:59 +00:00
|
|
|
if (pfd[0].revents) {
|
|
|
|
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-05-19 15:36:16 +00:00
|
|
|
#if ENABLE_SCRIPTREPLAY
|
|
|
|
if (opt & OPT_t) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-06 19:11:41 +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);
|
|
|
|
}
|