source: branches/3.2/mindi-busybox/util-linux/script.c @ 3232

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