scriptreplay: new applet. +423 bytes
[oweals/busybox.git] / util-linux / script.c
1 /* 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  */
13 #include "libbb.h"
14
15 int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
16 int script_main(int argc UNUSED_PARAM, char **argv)
17 {
18         int opt;
19         int mode;
20         int child_pid;
21         int attr_ok; /* NB: 0: ok */
22         int winsz_ok;
23         int pty;
24         char pty_line[GETPTY_BUFSIZE];
25         struct termios tt, rtt;
26         struct winsize win;
27         const char *fname = "typescript";
28         const char *shell;
29         char shell_opt[] = "-i";
30         char *shell_arg = NULL;
31         enum {
32                 OPT_a = (1 << 0),
33                 OPT_c = (1 << 1),
34                 OPT_f = (1 << 2),
35                 OPT_q = (1 << 3),
36 #if ENABLE_SCRIPTREPLAY
37                 OPT_t = (1 << 4),
38 #endif
39         };
40
41 #if ENABLE_GETOPT_LONG
42         static const char getopt_longopts[] ALIGN1 =
43                 "append\0"  No_argument       "a"
44                 "command\0" Required_argument "c"
45                 "flush\0"   No_argument       "f"
46                 "quiet\0"   No_argument       "q"
47 # if ENABLE_SCRIPTREPLAY
48                 "timing\0"  No_argument       "t"
49 # endif
50                 ;
51
52         applet_long_options = getopt_longopts;
53 #endif
54         opt_complementary = "?1"; /* max one arg */
55         opt = getopt32(argv, "ac:fq" IF_SCRIPTREPLAY("t") , &shell_arg);
56         //argc -= optind;
57         argv += optind;
58         if (argv[0]) {
59                 fname = argv[0];
60         }
61         mode = O_CREAT|O_TRUNC|O_WRONLY;
62         if (opt & OPT_a) {
63                 mode = O_CREAT|O_APPEND|O_WRONLY;
64         }
65         if (opt & OPT_c) {
66                 shell_opt[1] = 'c';
67         }
68         if (!(opt & OPT_q)) {
69                 printf("Script started, file is %s\n", fname);
70         }
71         shell = getenv("SHELL");
72         if (shell == NULL) {
73                 shell = DEFAULT_SHELL;
74         }
75
76         pty = xgetpty(pty_line);
77
78         /* get current stdin's tty params */
79         attr_ok = tcgetattr(0, &tt);
80         winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
81
82         rtt = tt;
83         cfmakeraw(&rtt);
84         rtt.c_lflag &= ~ECHO;
85         tcsetattr(0, TCSAFLUSH, &rtt);
86
87         /* "script" from util-linux exits when child exits,
88          * we wouldn't wait for EOF from slave pty
89          * (output may be produced by grandchildren of child) */
90         signal(SIGCHLD, record_signo);
91
92         /* TODO: SIGWINCH? pass window size changes down to slave? */
93
94         child_pid = vfork();
95         if (child_pid < 0) {
96                 bb_perror_msg_and_die("vfork");
97         }
98
99         if (child_pid) {
100                 /* parent */
101 #define buf bb_common_bufsiz1
102                 struct pollfd pfd[2];
103                 int outfd, count, loop;
104 #if ENABLE_SCRIPTREPLAY
105                 double oldtime = time(NULL);
106 #endif
107                 smallint fd_count = 2;
108
109                 outfd = xopen(fname, mode);
110                 pfd[0].fd = pty;
111                 pfd[0].events = POLLIN;
112                 pfd[1].fd = STDIN_FILENO;
113                 pfd[1].events = POLLIN;
114                 ndelay_on(pty); /* this descriptor is not shared, can do this */
115                 /* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */
116
117                 /* copy stdin to pty master input,
118                  * copy pty master output to stdout and file */
119                 /* TODO: don't use full_write's, use proper write buffering */
120                 while (fd_count && !bb_got_signal) {
121                         /* not safe_poll! we want SIGCHLD to EINTR poll */
122                         if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
123                                 /* If child exits too quickly, we may get EIO:
124                                  * for example, try "script -c true" */
125                                 break;
126                         }
127                         if (pfd[0].revents) {
128                                 errno = 0;
129                                 count = safe_read(pty, buf, sizeof(buf));
130                                 if (count <= 0 && errno != EAGAIN) {
131                                         /* err/eof from pty: exit */
132                                         goto restore;
133                                 }
134                                 if (count > 0) {
135 #if ENABLE_SCRIPTREPLAY
136                                         if (opt & OPT_t) {
137                                                 struct timeval tv;
138                                                 double newtime;
139
140                                                 gettimeofday(&tv, NULL);
141                                                 newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
142                                                 fprintf(stderr, "%f %u\n", newtime - oldtime, count);
143                                                 oldtime = newtime;
144                                         }
145 #endif
146                                         full_write(STDOUT_FILENO, buf, count);
147                                         full_write(outfd, buf, count);
148                                         if (opt & OPT_f) {
149                                                 fsync(outfd);
150                                         }
151                                 }
152                         }
153                         if (pfd[1].revents) {
154                                 count = safe_read(STDIN_FILENO, buf, sizeof(buf));
155                                 if (count <= 0) {
156                                         /* err/eof from stdin: don't read stdin anymore */
157                                         pfd[1].revents = 0;
158                                         fd_count--;
159                                 } else {
160                                         full_write(pty, buf, count);
161                                 }
162                         }
163                 }
164                 /* If loop was exited because SIGCHLD handler set bb_got_signal,
165                  * there still can be some buffered output. But dont loop forever:
166                  * we won't pump orphaned grandchildren's output indefinitely.
167                  * Testcase: running this in script:
168                  *      exec dd if=/dev/zero bs=1M count=1
169                  * must have "1+0 records in, 1+0 records out" captured too.
170                  * (util-linux's script doesn't do this. buggy :) */
171                 loop = 999;
172                 /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
173                 while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
174                         full_write(STDOUT_FILENO, buf, count);
175                         full_write(outfd, buf, count);
176                 }
177  restore:
178                 if (attr_ok == 0)
179                         tcsetattr(0, TCSAFLUSH, &tt);
180                 if (!(opt & OPT_q))
181                         printf("Script done, file is %s\n", fname);
182                 return EXIT_SUCCESS;
183         }
184
185         /* child: make pty slave to be input, output, error; run shell */
186         close(pty); /* close pty master */
187         /* open pty slave to fd 0,1,2 */
188         close(0);
189         xopen(pty_line, O_RDWR); /* uses fd 0 */
190         xdup2(0, 1);
191         xdup2(0, 2);
192         /* copy our original stdin tty's parameters to pty */
193         if (attr_ok == 0)
194                 tcsetattr(0, TCSAFLUSH, &tt);
195         if (winsz_ok == 0)
196                 ioctl(0, TIOCSWINSZ, (char *)&win);
197         /* set pty as a controlling tty */
198         setsid();
199         ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
200
201         /* Non-ignored signals revert to SIG_DFL on exec anyway */
202         /*signal(SIGCHLD, SIG_DFL);*/
203         execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
204         bb_simple_perror_msg_and_die(shell);
205 }