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 | * |
| 11 | * Licensed under GPLv2 or later, see file License in this tarball for details. |
| 12 | */ |
| 13 | |
| 14 | #include "libbb.h" |
| 15 | |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 16 | static smallint fd_count = 2; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 18 | static void handle_sigchld(int sig UNUSED_PARAM) |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 19 | { |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 20 | fd_count = 0; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 21 | } |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 22 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 23 | int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 24 | int script_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 25 | { |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 26 | int opt; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 27 | int mode; |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 28 | int child_pid; |
| 29 | int attr_ok; /* NB: 0: ok */ |
| 30 | int winsz_ok; |
| 31 | int pty; |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 32 | char pty_line[GETPTY_BUFSIZE]; |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 33 | struct termios tt, rtt; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 34 | struct winsize win; |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 35 | const char *fname = "typescript"; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 36 | const char *shell; |
| 37 | char shell_opt[] = "-i"; |
| 38 | char *shell_arg = NULL; |
| 39 | |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 40 | #if ENABLE_GETOPT_LONG |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 41 | static const char getopt_longopts[] ALIGN1 = |
| 42 | "append\0" No_argument "a" |
| 43 | "command\0" Required_argument "c" |
| 44 | "flush\0" No_argument "f" |
| 45 | "quiet\0" No_argument "q" |
| 46 | ; |
| 47 | |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 48 | applet_long_options = getopt_longopts; |
| 49 | #endif |
| 50 | opt_complementary = "?1"; /* max one arg */ |
| 51 | opt = getopt32(argv, "ac:fq", &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 & 1) { |
| 59 | mode = O_CREAT|O_APPEND|O_WRONLY; |
| 60 | } |
| 61 | if (opt & 2) { |
| 62 | shell_opt[1] = 'c'; |
| 63 | } |
| 64 | if (!(opt & 8)) { /* not -q */ |
| 65 | printf("Script started, file is %s\n", fname); |
| 66 | } |
| 67 | shell = getenv("SHELL"); |
| 68 | if (shell == NULL) { |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 69 | shell = DEFAULT_SHELL; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 72 | pty = xgetpty(pty_line); |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 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, handle_sigchld); |
| 87 | |
| 88 | /* TODO: SIGWINCH? pass window size changes down to slave? */ |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 90 | child_pid = vfork(); |
| 91 | if (child_pid < 0) { |
| 92 | bb_perror_msg_and_die("vfork"); |
| 93 | } |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 94 | |
| 95 | if (child_pid) { |
| 96 | /* parent */ |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 97 | #define buf bb_common_bufsiz1 |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 98 | struct pollfd pfd[2]; |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 99 | int outfd, count, loop; |
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; |
Denis Vlasenko | 06ebc16 | 2008-05-06 19:11:41 +0000 | [diff] [blame] | 104 | pfd[1].fd = 0; |
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 */ |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 107 | /* ndelay_on(0); - 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 */ |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 112 | while (fd_count) { |
| 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 | } |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [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) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 127 | full_write(STDOUT_FILENO, buf, count); |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 128 | full_write(outfd, buf, count); |
| 129 | if (opt & 4) { /* -f */ |
| 130 | fsync(outfd); |
| 131 | } |
| 132 | } |
| 133 | } |
Denis Vlasenko | 06ebc16 | 2008-05-06 19:11:41 +0000 | [diff] [blame] | 134 | if (pfd[1].revents) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 135 | count = safe_read(STDIN_FILENO, buf, sizeof(buf)); |
Denis Vlasenko | 06ebc16 | 2008-05-06 19:11:41 +0000 | [diff] [blame] | 136 | if (count <= 0) { |
| 137 | /* err/eof from stdin: don't read stdin anymore */ |
| 138 | pfd[1].revents = 0; |
| 139 | fd_count--; |
| 140 | } else { |
| 141 | full_write(pty, buf, count); |
| 142 | } |
| 143 | } |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 144 | } |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 145 | /* If loop was exited because SIGCHLD handler set fd_count to 0, |
| 146 | * there still can be some buffered output. But not loop forever: |
| 147 | * we won't pump orphaned grandchildren's output indefinitely. |
| 148 | * Testcase: running this in script: |
| 149 | * exec dd if=/dev/zero bs=1M count=1 |
| 150 | * must have "1+0 records in, 1+0 records out" captured too. |
| 151 | * (util-linux's script doesn't do this. buggy :) */ |
| 152 | loop = 999; |
| 153 | /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */ |
| 154 | while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 155 | full_write(STDOUT_FILENO, buf, count); |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 156 | full_write(outfd, buf, count); |
| 157 | } |
Denis Vlasenko | 06ebc16 | 2008-05-06 19:11:41 +0000 | [diff] [blame] | 158 | restore: |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 159 | if (attr_ok == 0) |
| 160 | tcsetattr(0, TCSAFLUSH, &tt); |
| 161 | if (!(opt & 8)) /* not -q */ |
| 162 | printf("Script done, file is %s\n", fname); |
| 163 | return EXIT_SUCCESS; |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* child: make pty slave to be input, output, error; run shell */ |
| 167 | close(pty); /* close pty master */ |
| 168 | /* open pty slave to fd 0,1,2 */ |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 169 | close(0); |
Denis Vlasenko | 32fd76c | 2008-02-28 10:10:10 +0000 | [diff] [blame] | 170 | xopen(pty_line, O_RDWR); /* uses fd 0 */ |
Denis Vlasenko | 5014dad | 2008-02-27 11:54:59 +0000 | [diff] [blame] | 171 | xdup2(0, 1); |
| 172 | xdup2(0, 2); |
| 173 | /* copy our original stdin tty's parameters to pty */ |
| 174 | if (attr_ok == 0) |
| 175 | tcsetattr(0, TCSAFLUSH, &tt); |
| 176 | if (winsz_ok == 0) |
| 177 | ioctl(0, TIOCSWINSZ, (char *)&win); |
| 178 | /* set pty as a controlling tty */ |
| 179 | setsid(); |
| 180 | ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */); |
| 181 | |
| 182 | /* signal(SIGCHLD, SIG_DFL); - exec does this for us */ |
| 183 | execl(shell, shell, shell_opt, shell_arg, NULL); |
| 184 | bb_simple_perror_msg_and_die(shell); |
| 185 | } |