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