Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame^] | 11 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 12 | */ |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
| 14 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 15 | int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 16 | int script_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 17 | { |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 18 | int opt; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 19 | int mode; |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 20 | int child_pid; |
| 21 | int attr_ok; /* NB: 0: ok */ |
| 22 | int winsz_ok; |
| 23 | int pty; |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 24 | char pty_line[GETPTY_BUFSIZE]; |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 25 | struct termios tt, rtt; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 26 | struct winsize win; |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 27 | const char *fname = "typescript"; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 28 | const char *shell; |
| 29 | char shell_opt[] = "-i"; |
| 30 | char *shell_arg = NULL; |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 31 | enum { |
| 32 | OPT_a = (1 << 0), |
| 33 | OPT_c = (1 << 1), |
| 34 | OPT_f = (1 << 2), |
| 35 | OPT_q = (1 << 3), |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 36 | OPT_t = (1 << 4), |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 37 | }; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 38 | |
Denys Vlasenko | f3b92d3 | 2009-06-19 12:10:38 +0200 | [diff] [blame] | 39 | #if ENABLE_LONG_OPTS |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 40 | 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 Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 45 | IF_SCRIPTREPLAY("timing\0" No_argument "t") |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 46 | ; |
| 47 | |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 48 | applet_long_options = getopt_longopts; |
| 49 | #endif |
Denys Vlasenko | 53f1791 | 2009-06-05 14:55:26 +0200 | [diff] [blame] | 50 | |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 51 | opt_complementary = "?1"; /* max one arg */ |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 52 | opt = getopt32(argv, "ac:fq" IF_SCRIPTREPLAY("t") , &shell_arg); |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 53 | //argc -= optind; |
| 54 | argv += optind; |
| 55 | if (argv[0]) { |
| 56 | fname = argv[0]; |
| 57 | } |
| 58 | mode = O_CREAT|O_TRUNC|O_WRONLY; |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 59 | if (opt & OPT_a) { |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 60 | mode = O_CREAT|O_APPEND|O_WRONLY; |
| 61 | } |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 62 | if (opt & OPT_c) { |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 63 | shell_opt[1] = 'c'; |
| 64 | } |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 65 | if (!(opt & OPT_q)) { |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 66 | printf("Script started, file is %s\n", fname); |
| 67 | } |
| 68 | shell = getenv("SHELL"); |
| 69 | if (shell == NULL) { |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 70 | shell = DEFAULT_SHELL; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 73 | pty = xgetpty(pty_line); |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 74 | |
| 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 Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 84 | /* "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 Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 87 | signal(SIGCHLD, record_signo); |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 88 | |
| 89 | /* TODO: SIGWINCH? pass window size changes down to slave? */ |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 90 | |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 91 | child_pid = xvfork(); |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 92 | |
| 93 | if (child_pid) { |
| 94 | /* parent */ |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 95 | #define buf bb_common_bufsiz1 |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 96 | struct pollfd pfd[2]; |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 97 | int outfd, count, loop; |
Denys Vlasenko | 53f1791 | 2009-06-05 14:55:26 +0200 | [diff] [blame] | 98 | double oldtime = ENABLE_SCRIPTREPLAY ? time(NULL) : 0; |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 99 | smallint fd_count = 2; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 100 | |
| 101 | outfd = xopen(fname, mode); |
Denis Vlasenko | 06ebc16 | 2008-05-06 19:11:41 +0000 | [diff] [blame] | 102 | pfd[0].fd = pty; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 103 | pfd[0].events = POLLIN; |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 104 | pfd[1].fd = STDIN_FILENO; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 105 | pfd[1].events = POLLIN; |
| 106 | ndelay_on(pty); /* this descriptor is not shared, can do this */ |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 107 | /* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */ |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 108 | |
| 109 | /* copy stdin to pty master input, |
| 110 | * copy pty master output to stdout and file */ |
| 111 | /* TODO: don't use full_write's, use proper write buffering */ |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 112 | while (fd_count && !bb_got_signal) { |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 113 | /* not safe_poll! we want SIGCHLD to EINTR poll */ |
Denis Vlasenko | 06ebc16 | 2008-05-06 19:11:41 +0000 | [diff] [blame] | 114 | if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) { |
Denis Vlasenko | 9dedf72 | 2008-04-01 16:12:17 +0000 | [diff] [blame] | 115 | /* If child exits too quickly, we may get EIO: |
| 116 | * for example, try "script -c true" */ |
| 117 | break; |
| 118 | } |
Denys Vlasenko | 88aa558 | 2010-03-02 15:02:45 +0100 | [diff] [blame] | 119 | if (pfd[0].revents) { |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 120 | errno = 0; |
| 121 | count = safe_read(pty, buf, sizeof(buf)); |
| 122 | if (count <= 0 && errno != EAGAIN) { |
Denis Vlasenko | 06ebc16 | 2008-05-06 19:11:41 +0000 | [diff] [blame] | 123 | /* err/eof from pty: exit */ |
| 124 | goto restore; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 125 | } |
| 126 | if (count > 0) { |
Denys Vlasenko | 53f1791 | 2009-06-05 14:55:26 +0200 | [diff] [blame] | 127 | if (ENABLE_SCRIPTREPLAY && (opt & OPT_t)) { |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 128 | struct timeval tv; |
| 129 | double newtime; |
| 130 | |
| 131 | gettimeofday(&tv, NULL); |
| 132 | newtime = tv.tv_sec + (double) tv.tv_usec / 1000000; |
| 133 | fprintf(stderr, "%f %u\n", newtime - oldtime, count); |
| 134 | oldtime = newtime; |
| 135 | } |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 136 | full_write(STDOUT_FILENO, buf, count); |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 137 | full_write(outfd, buf, count); |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 138 | if (opt & OPT_f) { |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 139 | fsync(outfd); |
| 140 | } |
| 141 | } |
| 142 | } |
Denys Vlasenko | 88aa558 | 2010-03-02 15:02:45 +0100 | [diff] [blame] | 143 | if (pfd[1].revents) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 144 | count = safe_read(STDIN_FILENO, buf, sizeof(buf)); |
Denis Vlasenko | 06ebc16 | 2008-05-06 19:11:41 +0000 | [diff] [blame] | 145 | if (count <= 0) { |
| 146 | /* err/eof from stdin: don't read stdin anymore */ |
| 147 | pfd[1].revents = 0; |
| 148 | fd_count--; |
| 149 | } else { |
| 150 | full_write(pty, buf, count); |
| 151 | } |
| 152 | } |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 153 | } |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 154 | /* If loop was exited because SIGCHLD handler set bb_got_signal, |
| 155 | * there still can be some buffered output. But dont loop forever: |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 156 | * we won't pump orphaned grandchildren's output indefinitely. |
| 157 | * Testcase: running this in script: |
| 158 | * exec dd if=/dev/zero bs=1M count=1 |
| 159 | * must have "1+0 records in, 1+0 records out" captured too. |
| 160 | * (util-linux's script doesn't do this. buggy :) */ |
| 161 | loop = 999; |
| 162 | /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */ |
| 163 | while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 164 | full_write(STDOUT_FILENO, buf, count); |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 165 | full_write(outfd, buf, count); |
| 166 | } |
Denis Vlasenko | 06ebc16 | 2008-05-06 19:11:41 +0000 | [diff] [blame] | 167 | restore: |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 168 | if (attr_ok == 0) |
| 169 | tcsetattr(0, TCSAFLUSH, &tt); |
Denys Vlasenko | 5e61115 | 2009-05-19 17:36:16 +0200 | [diff] [blame] | 170 | if (!(opt & OPT_q)) |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 171 | printf("Script done, file is %s\n", fname); |
| 172 | return EXIT_SUCCESS; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* child: make pty slave to be input, output, error; run shell */ |
| 176 | close(pty); /* close pty master */ |
| 177 | /* open pty slave to fd 0,1,2 */ |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 178 | close(0); |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 179 | xopen(pty_line, O_RDWR); /* uses fd 0 */ |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 180 | xdup2(0, 1); |
| 181 | xdup2(0, 2); |
| 182 | /* copy our original stdin tty's parameters to pty */ |
| 183 | if (attr_ok == 0) |
| 184 | tcsetattr(0, TCSAFLUSH, &tt); |
| 185 | if (winsz_ok == 0) |
| 186 | ioctl(0, TIOCSWINSZ, (char *)&win); |
| 187 | /* set pty as a controlling tty */ |
| 188 | setsid(); |
| 189 | ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */); |
| 190 | |
Denis Vlasenko | 3fa36e2 | 2008-11-09 00:15:11 +0000 | [diff] [blame] | 191 | /* Non-ignored signals revert to SIG_DFL on exec anyway */ |
| 192 | /*signal(SIGCHLD, SIG_DFL);*/ |
Denis Vlasenko | f09f4e0 | 2009-02-26 12:29:59 +0000 | [diff] [blame] | 193 | execl(shell, shell, shell_opt, shell_arg, (char *) NULL); |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 194 | bb_simple_perror_msg_and_die(shell); |
| 195 | } |