blob: 5d6f4d924ae8b5678e58fb66e22dad81009ebb5f [file] [log] [blame]
Denis Vlasenko5014dad2008-02-27 11:54:59 +00001/* 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 Vlasenko32fd76c2008-02-28 10:10:10 +000016static smallint fd_count = 2;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000017
Denis Vlasenko68404f12008-03-17 09:00:54 +000018static void handle_sigchld(int sig ATTRIBUTE_UNUSED)
Denis Vlasenko5014dad2008-02-27 11:54:59 +000019{
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000020 fd_count = 0;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000021}
Denis Vlasenko5014dad2008-02-27 11:54:59 +000022
Denis Vlasenko68404f12008-03-17 09:00:54 +000023int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
24int script_main(int argc ATTRIBUTE_UNUSED, char **argv)
Denis Vlasenko5014dad2008-02-27 11:54:59 +000025{
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000026 int opt;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000027 int mode;
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000028 int child_pid;
29 int attr_ok; /* NB: 0: ok */
30 int winsz_ok;
31 int pty;
Denis Vlasenko85c24712008-03-17 09:04:04 +000032 char pty_line[GETPTY_BUFSIZE];
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000033 struct termios tt, rtt;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000034 struct winsize win;
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000035 const char *fname = "typescript";
Denis Vlasenko5014dad2008-02-27 11:54:59 +000036 const char *shell;
37 char shell_opt[] = "-i";
38 char *shell_arg = NULL;
39
Denis Vlasenko5014dad2008-02-27 11:54:59 +000040#if ENABLE_GETOPT_LONG
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000041 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 Vlasenko5014dad2008-02-27 11:54:59 +000048 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 Vlasenko32fd76c2008-02-28 10:10:10 +000069 shell = DEFAULT_SHELL;
Denis Vlasenko5014dad2008-02-27 11:54:59 +000070 }
71
Denis Vlasenko85c24712008-03-17 09:04:04 +000072 pty = getpty(pty_line);
Denis Vlasenko5014dad2008-02-27 11:54:59 +000073 if (pty < 0) {
74 bb_perror_msg_and_die("can't get pty");
75 }
76
77 /* get current stdin's tty params */
78 attr_ok = tcgetattr(0, &tt);
79 winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
80
81 rtt = tt;
82 cfmakeraw(&rtt);
83 rtt.c_lflag &= ~ECHO;
84 tcsetattr(0, TCSAFLUSH, &rtt);
85
Denis Vlasenko32fd76c2008-02-28 10:10:10 +000086 /* "script" from util-linux exits when child exits,
87 * we wouldn't wait for EOF from slave pty
88 * (output may be produced by grandchildren of child) */
89 signal(SIGCHLD, handle_sigchld);
90
91 /* TODO: SIGWINCH? pass window size changes down to slave? */
Denis Vlasenko5014dad2008-02-27 11:54:59 +000092
93 child_pid = vfork();
94 if (child_pid < 0) {
95 bb_perror_msg_and_die("vfork");
96 }
97
98 if (child_pid) {
99 /* parent */
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000100#define buf bb_common_bufsiz1
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000101 struct pollfd pfd[2];
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000102 struct pollfd *ppfd = pfd;
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000103 int outfd, count, loop;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000104
105 outfd = xopen(fname, mode);
106 pfd[0].fd = 0;
107 pfd[0].events = POLLIN;
108 pfd[1].fd = pty;
109 pfd[1].events = POLLIN;
110 ndelay_on(pty); /* this descriptor is not shared, can do this */
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000111 /* ndelay_on(0); - NO, stdin can be shared! Pity :( */
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000112
113 /* copy stdin to pty master input,
114 * copy pty master output to stdout and file */
115 /* TODO: don't use full_write's, use proper write buffering */
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000116 while (fd_count) {
117 /* not safe_poll! we want SIGCHLD to EINTR poll */
Denis Vlasenko9dedf722008-04-01 16:12:17 +0000118 if (poll(ppfd, fd_count, -1) < 0 && errno != EINTR) {
119 /* If child exits too quickly, we may get EIO:
120 * for example, try "script -c true" */
121 break;
122 }
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000123 if (pfd[0].revents) {
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000124 count = safe_read(0, buf, sizeof(buf));
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000125 if (count <= 0) {
126 /* err/eof: don't read anymore */
127 pfd[0].revents = 0;
128 ppfd++;
129 fd_count--;
130 } else {
131 full_write(pty, buf, count);
132 }
133 }
134 if (pfd[1].revents) {
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000135 errno = 0;
136 count = safe_read(pty, buf, sizeof(buf));
137 if (count <= 0 && errno != EAGAIN) {
138 /* err/eof: don't read anymore */
139 pfd[1].revents = 0;
140 fd_count--;
141 }
142 if (count > 0) {
143 full_write(1, buf, count);
144 full_write(outfd, buf, count);
145 if (opt & 4) { /* -f */
146 fsync(outfd);
147 }
148 }
149 }
150 }
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000151 /* If loop was exited because SIGCHLD handler set fd_count to 0,
152 * there still can be some buffered output. But not loop forever:
153 * we won't pump orphaned grandchildren's output indefinitely.
154 * Testcase: running this in script:
155 * exec dd if=/dev/zero bs=1M count=1
156 * must have "1+0 records in, 1+0 records out" captured too.
157 * (util-linux's script doesn't do this. buggy :) */
158 loop = 999;
159 /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
160 while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
161 full_write(1, buf, count);
162 full_write(outfd, buf, count);
163 }
164
165 if (attr_ok == 0)
166 tcsetattr(0, TCSAFLUSH, &tt);
167 if (!(opt & 8)) /* not -q */
168 printf("Script done, file is %s\n", fname);
169 return EXIT_SUCCESS;
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000170 }
171
172 /* child: make pty slave to be input, output, error; run shell */
173 close(pty); /* close pty master */
174 /* open pty slave to fd 0,1,2 */
Denis Vlasenko42cc3042008-03-24 02:05:58 +0000175 close(0);
Denis Vlasenko32fd76c2008-02-28 10:10:10 +0000176 xopen(pty_line, O_RDWR); /* uses fd 0 */
Denis Vlasenko5014dad2008-02-27 11:54:59 +0000177 xdup2(0, 1);
178 xdup2(0, 2);
179 /* copy our original stdin tty's parameters to pty */
180 if (attr_ok == 0)
181 tcsetattr(0, TCSAFLUSH, &tt);
182 if (winsz_ok == 0)
183 ioctl(0, TIOCSWINSZ, (char *)&win);
184 /* set pty as a controlling tty */
185 setsid();
186 ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
187
188 /* signal(SIGCHLD, SIG_DFL); - exec does this for us */
189 execl(shell, shell, shell_opt, shell_arg, NULL);
190 bb_simple_perror_msg_and_die(shell);
191}