blob: abcd73bff5c0fc477c26a33c385491f8a6c8effc [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
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02009 * The Regents of the University of California. All rights reserved.
Denis Vlasenko5014dad2008-02-27 11:54:59 +000010 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020011 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenko5014dad2008-02-27 11:54:59 +000012 */
Pere Orga5bc8c002011-04-11 03:29:49 +020013
14//usage:#define script_trivial_usage
15//usage: "[-afq" IF_SCRIPTREPLAY("t") "] [-c PROG] [OUTFILE]"
16//usage:#define script_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020017//usage: " -a Append output"
Pere Orga5bc8c002011-04-11 03:29:49 +020018//usage: "\n -c PROG Run PROG, not shell"
19//usage: "\n -f Flush output after each write"
20//usage: "\n -q Quiet"
21//usage: IF_SCRIPTREPLAY(
22//usage: "\n -t Send timing to stderr"
23//usage: )
24
Denis Vlasenko5014dad2008-02-27 11:54:59 +000025#include "libbb.h"
26
Denis Vlasenko68404f12008-03-17 09:00:54 +000027int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000028int script_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko5014dad2008-02-27 11:54:59 +000029{
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000030 int opt;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000031 int mode;
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000032 int child_pid;
33 int attr_ok; /* NB: 0: ok */
34 int winsz_ok;
35 int pty;
Denis Vlasenko85c24712008-03-17 09:04:04 +000036 char pty_line[GETPTY_BUFSIZE];
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000037 struct termios tt, rtt;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000038 struct winsize win;
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000039 const char *fname = "typescript";
Denis Vlasenko5014dad2008-02-27 11:54:59 +000040 const char *shell;
41 char shell_opt[] = "-i";
42 char *shell_arg = NULL;
Denys Vlasenko5e611152009-05-19 17:36:16 +020043 enum {
44 OPT_a = (1 << 0),
45 OPT_c = (1 << 1),
46 OPT_f = (1 << 2),
47 OPT_q = (1 << 3),
Denys Vlasenko5e611152009-05-19 17:36:16 +020048 OPT_t = (1 << 4),
Denys Vlasenko5e611152009-05-19 17:36:16 +020049 };
Denis Vlasenko5014dad2008-02-27 11:54:59 +000050
Denys Vlasenkof3b92d32009-06-19 12:10:38 +020051#if ENABLE_LONG_OPTS
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000052 static const char getopt_longopts[] ALIGN1 =
53 "append\0" No_argument "a"
54 "command\0" Required_argument "c"
55 "flush\0" No_argument "f"
56 "quiet\0" No_argument "q"
Denys Vlasenko2634bf32009-06-09 18:40:07 +020057 IF_SCRIPTREPLAY("timing\0" No_argument "t")
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000058 ;
59
Denys Vlasenko2634bf32009-06-09 18:40:07 +020060 applet_long_options = getopt_longopts;
61#endif
Denys Vlasenko53f17912009-06-05 14:55:26 +020062
Denis Vlasenko5014dad2008-02-27 11:54:59 +000063 opt_complementary = "?1"; /* max one arg */
Denys Vlasenko5e611152009-05-19 17:36:16 +020064 opt = getopt32(argv, "ac:fq" IF_SCRIPTREPLAY("t") , &shell_arg);
Denis Vlasenko5014dad2008-02-27 11:54:59 +000065 //argc -= optind;
66 argv += optind;
67 if (argv[0]) {
68 fname = argv[0];
69 }
70 mode = O_CREAT|O_TRUNC|O_WRONLY;
Denys Vlasenko5e611152009-05-19 17:36:16 +020071 if (opt & OPT_a) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +000072 mode = O_CREAT|O_APPEND|O_WRONLY;
73 }
Denys Vlasenko5e611152009-05-19 17:36:16 +020074 if (opt & OPT_c) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +000075 shell_opt[1] = 'c';
76 }
Denys Vlasenko5e611152009-05-19 17:36:16 +020077 if (!(opt & OPT_q)) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +000078 printf("Script started, file is %s\n", fname);
79 }
Denys Vlasenko438b4ac2014-03-16 12:34:53 +010080
Denys Vlasenko681efe22011-03-08 21:00:36 +010081 shell = get_shell_name();
Denis Vlasenko5014dad2008-02-27 11:54:59 +000082
Denys Vlasenko438b4ac2014-03-16 12:34:53 +010083 /* Some people run "script ... 0>&-".
84 * Our code assumes that STDIN_FILENO != pty.
85 * Ensure STDIN_FILENO is not closed:
86 */
87 bb_sanitize_stdio();
88
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000089 pty = xgetpty(pty_line);
Denis Vlasenko5014dad2008-02-27 11:54:59 +000090
91 /* get current stdin's tty params */
92 attr_ok = tcgetattr(0, &tt);
93 winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
94
95 rtt = tt;
96 cfmakeraw(&rtt);
97 rtt.c_lflag &= ~ECHO;
98 tcsetattr(0, TCSAFLUSH, &rtt);
99
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000100 /* "script" from util-linux exits when child exits,
101 * we wouldn't wait for EOF from slave pty
102 * (output may be produced by grandchildren of child) */
Denys Vlasenko5e611152009-05-19 17:36:16 +0200103 signal(SIGCHLD, record_signo);
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000104
105 /* TODO: SIGWINCH? pass window size changes down to slave? */
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000106
Pascal Bellard926031b2010-07-04 15:32:38 +0200107 child_pid = xvfork();
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000108
109 if (child_pid) {
110 /* parent */
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000111#define buf bb_common_bufsiz1
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000112 struct pollfd pfd[2];
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000113 int outfd, count, loop;
Denys Vlasenko53f17912009-06-05 14:55:26 +0200114 double oldtime = ENABLE_SCRIPTREPLAY ? time(NULL) : 0;
Denys Vlasenko5e611152009-05-19 17:36:16 +0200115 smallint fd_count = 2;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000116
117 outfd = xopen(fname, mode);
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000118 pfd[0].fd = pty;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000119 pfd[0].events = POLLIN;
Denys Vlasenko5e611152009-05-19 17:36:16 +0200120 pfd[1].fd = STDIN_FILENO;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000121 pfd[1].events = POLLIN;
122 ndelay_on(pty); /* this descriptor is not shared, can do this */
Denys Vlasenko5e611152009-05-19 17:36:16 +0200123 /* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000124
125 /* copy stdin to pty master input,
126 * copy pty master output to stdout and file */
127 /* TODO: don't use full_write's, use proper write buffering */
Denys Vlasenko5e611152009-05-19 17:36:16 +0200128 while (fd_count && !bb_got_signal) {
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000129 /* not safe_poll! we want SIGCHLD to EINTR poll */
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000130 if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
Denis Vlasenko9dedf722008-04-01 16:12:17 +0000131 /* If child exits too quickly, we may get EIO:
132 * for example, try "script -c true" */
133 break;
134 }
Denys Vlasenko88aa5582010-03-02 15:02:45 +0100135 if (pfd[0].revents) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000136 errno = 0;
137 count = safe_read(pty, buf, sizeof(buf));
138 if (count <= 0 && errno != EAGAIN) {
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000139 /* err/eof from pty: exit */
140 goto restore;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000141 }
142 if (count > 0) {
Denys Vlasenko53f17912009-06-05 14:55:26 +0200143 if (ENABLE_SCRIPTREPLAY && (opt & OPT_t)) {
Denys Vlasenko5e611152009-05-19 17:36:16 +0200144 struct timeval tv;
145 double newtime;
146
147 gettimeofday(&tv, NULL);
148 newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
149 fprintf(stderr, "%f %u\n", newtime - oldtime, count);
150 oldtime = newtime;
151 }
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000152 full_write(STDOUT_FILENO, buf, count);
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000153 full_write(outfd, buf, count);
Denys Vlasenko5e611152009-05-19 17:36:16 +0200154 if (opt & OPT_f) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000155 fsync(outfd);
156 }
157 }
158 }
Denys Vlasenko88aa5582010-03-02 15:02:45 +0100159 if (pfd[1].revents) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000160 count = safe_read(STDIN_FILENO, buf, sizeof(buf));
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000161 if (count <= 0) {
162 /* err/eof from stdin: don't read stdin anymore */
163 pfd[1].revents = 0;
164 fd_count--;
165 } else {
166 full_write(pty, buf, count);
167 }
168 }
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000169 }
Denys Vlasenko5e611152009-05-19 17:36:16 +0200170 /* If loop was exited because SIGCHLD handler set bb_got_signal,
171 * there still can be some buffered output. But dont loop forever:
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000172 * we won't pump orphaned grandchildren's output indefinitely.
173 * Testcase: running this in script:
174 * exec dd if=/dev/zero bs=1M count=1
175 * must have "1+0 records in, 1+0 records out" captured too.
176 * (util-linux's script doesn't do this. buggy :) */
177 loop = 999;
178 /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
179 while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000180 full_write(STDOUT_FILENO, buf, count);
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000181 full_write(outfd, buf, count);
182 }
Denis Vlasenko06ebc162008-05-06 19:11:41 +0000183 restore:
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000184 if (attr_ok == 0)
185 tcsetattr(0, TCSAFLUSH, &tt);
Denys Vlasenko5e611152009-05-19 17:36:16 +0200186 if (!(opt & OPT_q))
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000187 printf("Script done, file is %s\n", fname);
188 return EXIT_SUCCESS;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000189 }
190
191 /* child: make pty slave to be input, output, error; run shell */
192 close(pty); /* close pty master */
193 /* open pty slave to fd 0,1,2 */
Denis Vlasenko42cc3042008-03-24 02:05:58 +0000194 close(0);
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000195 xopen(pty_line, O_RDWR); /* uses fd 0 */
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000196 xdup2(0, 1);
197 xdup2(0, 2);
198 /* copy our original stdin tty's parameters to pty */
199 if (attr_ok == 0)
200 tcsetattr(0, TCSAFLUSH, &tt);
201 if (winsz_ok == 0)
202 ioctl(0, TIOCSWINSZ, (char *)&win);
203 /* set pty as a controlling tty */
204 setsid();
205 ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
206
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000207 /* Non-ignored signals revert to SIG_DFL on exec anyway */
208 /*signal(SIGCHLD, SIG_DFL);*/
Denis Vlasenkof09f4e02009-02-26 12:29:59 +0000209 execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000210 bb_simple_perror_msg_and_die(shell);
211}