blob: d9a62fbfefef573de72aebc969a7ac2f4cebd2f4 [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
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Licensed under GPLv2 or later, see file License in this tarball for details.
12 */
Denis Vlasenko5014dad2008-02-27 11:54:59 +000013#include "libbb.h"
14
Denis Vlasenko68404f12008-03-17 09:00:54 +000015int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000016int script_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko5014dad2008-02-27 11:54:59 +000017{
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000018 int opt;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000019 int mode;
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000020 int child_pid;
21 int attr_ok; /* NB: 0: ok */
22 int winsz_ok;
23 int pty;
Denis Vlasenko85c24712008-03-17 09:04:04 +000024 char pty_line[GETPTY_BUFSIZE];
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000025 struct termios tt, rtt;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000026 struct winsize win;
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000027 const char *fname = "typescript";
Denis Vlasenko5014dad2008-02-27 11:54:59 +000028 const char *shell;
29 char shell_opt[] = "-i";
30 char *shell_arg = NULL;
Denys Vlasenko5e611152009-05-19 17:36:16 +020031 enum {
32 OPT_a = (1 << 0),
33 OPT_c = (1 << 1),
34 OPT_f = (1 << 2),
35 OPT_q = (1 << 3),
Denys Vlasenko5e611152009-05-19 17:36:16 +020036 OPT_t = (1 << 4),
Denys Vlasenko5e611152009-05-19 17:36:16 +020037 };
Denis Vlasenko5014dad2008-02-27 11:54:59 +000038
Denys Vlasenkof3b92d32009-06-19 12:10:38 +020039#if ENABLE_LONG_OPTS
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000040 static const char getopt_longopts[] ALIGN1 =
41 "append\0" No_argument "a"
42 "command\0" Required_argument "c"
43 "flush\0" No_argument "f"
44 "quiet\0" No_argument "q"
Denys Vlasenko2634bf32009-06-09 18:40:07 +020045 IF_SCRIPTREPLAY("timing\0" No_argument "t")
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000046 ;
47
Denys Vlasenko2634bf32009-06-09 18:40:07 +020048 applet_long_options = getopt_longopts;
49#endif
Denys Vlasenko53f17912009-06-05 14:55:26 +020050
Denis Vlasenko5014dad2008-02-27 11:54:59 +000051 opt_complementary = "?1"; /* max one arg */
Denys Vlasenko5e611152009-05-19 17:36:16 +020052 opt = getopt32(argv, "ac:fq" IF_SCRIPTREPLAY("t") , &shell_arg);
Denis Vlasenko5014dad2008-02-27 11:54:59 +000053 //argc -= optind;
54 argv += optind;
55 if (argv[0]) {
56 fname = argv[0];
57 }
58 mode = O_CREAT|O_TRUNC|O_WRONLY;
Denys Vlasenko5e611152009-05-19 17:36:16 +020059 if (opt & OPT_a) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +000060 mode = O_CREAT|O_APPEND|O_WRONLY;
61 }
Denys Vlasenko5e611152009-05-19 17:36:16 +020062 if (opt & OPT_c) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +000063 shell_opt[1] = 'c';
64 }
Denys Vlasenko5e611152009-05-19 17:36:16 +020065 if (!(opt & OPT_q)) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +000066 printf("Script started, file is %s\n", fname);
67 }
68 shell = getenv("SHELL");
69 if (shell == NULL) {
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000070 shell = DEFAULT_SHELL;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000071 }
72
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000073 pty = xgetpty(pty_line);
Denis Vlasenko5014dad2008-02-27 11:54:59 +000074
75 /* get current stdin's tty params */
76 attr_ok = tcgetattr(0, &tt);
77 winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
78
79 rtt = tt;
80 cfmakeraw(&rtt);
81 rtt.c_lflag &= ~ECHO;
82 tcsetattr(0, TCSAFLUSH, &rtt);
83
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000084 /* "script" from util-linux exits when child exits,
85 * we wouldn't wait for EOF from slave pty
86 * (output may be produced by grandchildren of child) */
Denys Vlasenko5e611152009-05-19 17:36:16 +020087 signal(SIGCHLD, record_signo);
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000088
89 /* TODO: SIGWINCH? pass window size changes down to slave? */
Denis Vlasenko5014dad2008-02-27 11:54:59 +000090
Denis Vlasenko82604e92008-07-01 15:59:42 +000091 child_pid = vfork();
92 if (child_pid < 0) {
93 bb_perror_msg_and_die("vfork");
94 }
Denis Vlasenko5014dad2008-02-27 11:54:59 +000095
96 if (child_pid) {
97 /* parent */
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000098#define buf bb_common_bufsiz1
Denis Vlasenko5014dad2008-02-27 11:54:59 +000099 struct pollfd pfd[2];
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000100 int outfd, count, loop;
Denys Vlasenko53f17912009-06-05 14:55:26 +0200101 double oldtime = ENABLE_SCRIPTREPLAY ? time(NULL) : 0;
Denys Vlasenko5e611152009-05-19 17:36:16 +0200102 smallint fd_count = 2;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000103
104 outfd = xopen(fname, mode);
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000105 pfd[0].fd = pty;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000106 pfd[0].events = POLLIN;
Denys Vlasenko5e611152009-05-19 17:36:16 +0200107 pfd[1].fd = STDIN_FILENO;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000108 pfd[1].events = POLLIN;
109 ndelay_on(pty); /* this descriptor is not shared, can do this */
Denys Vlasenko5e611152009-05-19 17:36:16 +0200110 /* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000111
112 /* copy stdin to pty master input,
113 * copy pty master output to stdout and file */
114 /* TODO: don't use full_write's, use proper write buffering */
Denys Vlasenko5e611152009-05-19 17:36:16 +0200115 while (fd_count && !bb_got_signal) {
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000116 /* not safe_poll! we want SIGCHLD to EINTR poll */
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000117 if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
Denis Vlasenko9dedf722008-04-01 16:12:17 +0000118 /* If child exits too quickly, we may get EIO:
119 * for example, try "script -c true" */
120 break;
121 }
Denys Vlasenko88aa5582010-03-02 15:02:45 +0100122 if (pfd[0].revents) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000123 errno = 0;
124 count = safe_read(pty, buf, sizeof(buf));
125 if (count <= 0 && errno != EAGAIN) {
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000126 /* err/eof from pty: exit */
127 goto restore;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000128 }
129 if (count > 0) {
Denys Vlasenko53f17912009-06-05 14:55:26 +0200130 if (ENABLE_SCRIPTREPLAY && (opt & OPT_t)) {
Denys Vlasenko5e611152009-05-19 17:36:16 +0200131 struct timeval tv;
132 double newtime;
133
134 gettimeofday(&tv, NULL);
135 newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
136 fprintf(stderr, "%f %u\n", newtime - oldtime, count);
137 oldtime = newtime;
138 }
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000139 full_write(STDOUT_FILENO, buf, count);
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000140 full_write(outfd, buf, count);
Denys Vlasenko5e611152009-05-19 17:36:16 +0200141 if (opt & OPT_f) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000142 fsync(outfd);
143 }
144 }
145 }
Denys Vlasenko88aa5582010-03-02 15:02:45 +0100146 if (pfd[1].revents) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000147 count = safe_read(STDIN_FILENO, buf, sizeof(buf));
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000148 if (count <= 0) {
149 /* err/eof from stdin: don't read stdin anymore */
150 pfd[1].revents = 0;
151 fd_count--;
152 } else {
153 full_write(pty, buf, count);
154 }
155 }
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000156 }
Denys Vlasenko5e611152009-05-19 17:36:16 +0200157 /* If loop was exited because SIGCHLD handler set bb_got_signal,
158 * there still can be some buffered output. But dont loop forever:
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000159 * we won't pump orphaned grandchildren's output indefinitely.
160 * Testcase: running this in script:
161 * exec dd if=/dev/zero bs=1M count=1
162 * must have "1+0 records in, 1+0 records out" captured too.
163 * (util-linux's script doesn't do this. buggy :) */
164 loop = 999;
165 /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
166 while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000167 full_write(STDOUT_FILENO, buf, count);
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000168 full_write(outfd, buf, count);
169 }
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000170 restore:
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000171 if (attr_ok == 0)
172 tcsetattr(0, TCSAFLUSH, &tt);
Denys Vlasenko5e611152009-05-19 17:36:16 +0200173 if (!(opt & OPT_q))
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000174 printf("Script done, file is %s\n", fname);
175 return EXIT_SUCCESS;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000176 }
177
178 /* child: make pty slave to be input, output, error; run shell */
179 close(pty); /* close pty master */
180 /* open pty slave to fd 0,1,2 */
Denis Vlasenko42cc3042008-03-24 02:05:58 +0000181 close(0);
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000182 xopen(pty_line, O_RDWR); /* uses fd 0 */
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000183 xdup2(0, 1);
184 xdup2(0, 2);
185 /* copy our original stdin tty's parameters to pty */
186 if (attr_ok == 0)
187 tcsetattr(0, TCSAFLUSH, &tt);
188 if (winsz_ok == 0)
189 ioctl(0, TIOCSWINSZ, (char *)&win);
190 /* set pty as a controlling tty */
191 setsid();
192 ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
193
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000194 /* Non-ignored signals revert to SIG_DFL on exec anyway */
195 /*signal(SIGCHLD, SIG_DFL);*/
Denis Vlasenkof09f4e02009-02-26 12:29:59 +0000196 execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000197 bb_simple_perror_msg_and_die(shell);
198}