aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util-linux/script.c112
1 files changed, 52 insertions, 60 deletions
diff --git a/util-linux/script.c b/util-linux/script.c
index e6dbb1aa2..a4aaa422f 100644
--- a/util-linux/script.c
+++ b/util-linux/script.c
@@ -13,67 +13,38 @@
13 13
14#include "libbb.h" 14#include "libbb.h"
15 15
16struct globals { 16static smallint fd_count = 2;
17 int child_pid;
18 int attr_ok; /* NB: 0: ok */
19 struct termios tt;
20 const char *fname;
21};
22#define G (*ptr_to_globals)
23#define child_pid (G.child_pid)
24#define attr_ok (G.attr_ok )
25#define tt (G.tt )
26#define fname (G.fname )
27#define INIT_G() do { \
28 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
29 fname = "typescript"; \
30} while (0)
31
32static void done(void)
33{
34 if (child_pid) { /* we are parent */
35 if (attr_ok == 0)
36 tcsetattr(0, TCSAFLUSH, &tt);
37 if (!(option_mask32 & 8)) /* not -q */
38 printf("Script done, file is %s\n", fname);
39 }
40 exit(0);
41}
42 17
43#ifdef UNUSED
44static void handle_sigchld(int sig) 18static void handle_sigchld(int sig)
45{ 19{
46 /* wait for the exited child and exit */ 20 fd_count = 0;
47 while (wait_any_nohang(&sig) > 0)
48 continue;
49 done();
50} 21}
51#endif
52
53#if ENABLE_GETOPT_LONG
54static const char getopt_longopts[] ALIGN1 =
55 "append\0" No_argument "a"
56 "command\0" Required_argument "c"
57 "flush\0" No_argument "f"
58 "quiet\0" No_argument "q"
59 ;
60#endif
61 22
62int script_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE; 23int script_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
63int script_main(int argc, char *argv[]) 24int script_main(int argc, char *argv[])
64{ 25{
65 int opt, pty; 26 int opt;
66 int winsz_ok;
67 int mode; 27 int mode;
68 struct termios rtt; 28 int child_pid;
29 int attr_ok; /* NB: 0: ok */
30 int winsz_ok;
31 int pty;
32 char pty_line[32];
33 struct termios tt, rtt;
69 struct winsize win; 34 struct winsize win;
70 char line[32]; 35 const char *fname = "typescript";
71 const char *shell; 36 const char *shell;
72 char shell_opt[] = "-i"; 37 char shell_opt[] = "-i";
73 char *shell_arg = NULL; 38 char *shell_arg = NULL;
74 39
75 INIT_G();
76#if ENABLE_GETOPT_LONG 40#if ENABLE_GETOPT_LONG
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
77 applet_long_options = getopt_longopts; 48 applet_long_options = getopt_longopts;
78#endif 49#endif
79 opt_complementary = "?1"; /* max one arg */ 50 opt_complementary = "?1"; /* max one arg */
@@ -95,10 +66,10 @@ int script_main(int argc, char *argv[])
95 } 66 }
96 shell = getenv("SHELL"); 67 shell = getenv("SHELL");
97 if (shell == NULL) { 68 if (shell == NULL) {
98 shell = _PATH_BSHELL; 69 shell = DEFAULT_SHELL;
99 } 70 }
100 71
101 pty = getpty(line, sizeof(line)); 72 pty = getpty(pty_line, sizeof(pty_line));
102 if (pty < 0) { 73 if (pty < 0) {
103 bb_perror_msg_and_die("can't get pty"); 74 bb_perror_msg_and_die("can't get pty");
104 } 75 }
@@ -112,9 +83,12 @@ int script_main(int argc, char *argv[])
112 rtt.c_lflag &= ~ECHO; 83 rtt.c_lflag &= ~ECHO;
113 tcsetattr(0, TCSAFLUSH, &rtt); 84 tcsetattr(0, TCSAFLUSH, &rtt);
114 85
115 /* We exit as soon as child exits */ 86 /* "script" from util-linux exits when child exits,
116 //signal(SIGCHLD, handle_sigchld); 87 * we wouldn't wait for EOF from slave pty
117 signal(SIGCHLD, (void (*)(int)) done); 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? */
118 92
119 child_pid = vfork(); 93 child_pid = vfork();
120 if (child_pid < 0) { 94 if (child_pid < 0) {
@@ -123,11 +97,10 @@ int script_main(int argc, char *argv[])
123 97
124 if (child_pid) { 98 if (child_pid) {
125 /* parent */ 99 /* parent */
126 char buf[256]; 100#define buf bb_common_bufsiz1
127 struct pollfd pfd[2]; 101 struct pollfd pfd[2];
128 int outfd;
129 int fd_count = 2;
130 struct pollfd *ppfd = pfd; 102 struct pollfd *ppfd = pfd;
103 int outfd, count, loop;
131 104
132 outfd = xopen(fname, mode); 105 outfd = xopen(fname, mode);
133 pfd[0].fd = 0; 106 pfd[0].fd = 0;
@@ -135,14 +108,16 @@ int script_main(int argc, char *argv[])
135 pfd[1].fd = pty; 108 pfd[1].fd = pty;
136 pfd[1].events = POLLIN; 109 pfd[1].events = POLLIN;
137 ndelay_on(pty); /* this descriptor is not shared, can do this */ 110 ndelay_on(pty); /* this descriptor is not shared, can do this */
138 /* ndelay_on(0); - NO, stdin can be shared! */ 111 /* ndelay_on(0); - NO, stdin can be shared! Pity :( */
139 112
140 /* copy stdin to pty master input, 113 /* copy stdin to pty master input,
141 * copy pty master output to stdout and file */ 114 * copy pty master output to stdout and file */
142 /* TODO: don't use full_write's, use proper write buffering */ 115 /* TODO: don't use full_write's, use proper write buffering */
143 while (fd_count && safe_poll(ppfd, fd_count, -1) > 0) { 116 while (fd_count) {
117 /* not safe_poll! we want SIGCHLD to EINTR poll */
118 poll(ppfd, fd_count, -1);
144 if (pfd[0].revents) { 119 if (pfd[0].revents) {
145 int count = safe_read(0, buf, sizeof(buf)); 120 count = safe_read(0, buf, sizeof(buf));
146 if (count <= 0) { 121 if (count <= 0) {
147 /* err/eof: don't read anymore */ 122 /* err/eof: don't read anymore */
148 pfd[0].revents = 0; 123 pfd[0].revents = 0;
@@ -153,7 +128,6 @@ int script_main(int argc, char *argv[])
153 } 128 }
154 } 129 }
155 if (pfd[1].revents) { 130 if (pfd[1].revents) {
156 int count;
157 errno = 0; 131 errno = 0;
158 count = safe_read(pty, buf, sizeof(buf)); 132 count = safe_read(pty, buf, sizeof(buf));
159 if (count <= 0 && errno != EAGAIN) { 133 if (count <= 0 && errno != EAGAIN) {
@@ -170,14 +144,32 @@ int script_main(int argc, char *argv[])
170 } 144 }
171 } 145 }
172 } 146 }
173 done(); /* does not return */ 147 /* If loop was exited because SIGCHLD handler set fd_count to 0,
148 * there still can be some buffered output. But not loop forever:
149 * we won't pump orphaned grandchildren's output indefinitely.
150 * Testcase: running this in script:
151 * exec dd if=/dev/zero bs=1M count=1
152 * must have "1+0 records in, 1+0 records out" captured too.
153 * (util-linux's script doesn't do this. buggy :) */
154 loop = 999;
155 /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
156 while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
157 full_write(1, buf, count);
158 full_write(outfd, buf, count);
159 }
160
161 if (attr_ok == 0)
162 tcsetattr(0, TCSAFLUSH, &tt);
163 if (!(opt & 8)) /* not -q */
164 printf("Script done, file is %s\n", fname);
165 return EXIT_SUCCESS;
174 } 166 }
175 167
176 /* child: make pty slave to be input, output, error; run shell */ 168 /* child: make pty slave to be input, output, error; run shell */
177 close(pty); /* close pty master */ 169 close(pty); /* close pty master */
178 /* open pty slave to fd 0,1,2 */ 170 /* open pty slave to fd 0,1,2 */
179 close(0); 171 close(0);
180 xopen(line, O_RDWR); /* uses fd 0 */ 172 xopen(pty_line, O_RDWR); /* uses fd 0 */
181 xdup2(0, 1); 173 xdup2(0, 1);
182 xdup2(0, 2); 174 xdup2(0, 2);
183 /* copy our original stdin tty's parameters to pty */ 175 /* copy our original stdin tty's parameters to pty */