]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
edc7e420 SK |
15 | * This product includes software developed by the University of |
16 | * California, Berkeley and its contributors. | |
6dbe3af9 KZ |
17 | * 4. Neither the name of the University nor the names of its contributors |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
66ee8158 | 34 | /* |
b50945d4 | 35 | * 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL> |
7eda085c | 36 | * - added Native Language Support |
66ee8158 KZ |
37 | * |
38 | * 2000-07-30 Per Andreas Buer <per@linpro.no> - added "q"-option | |
26ed9fb8 CK |
39 | * |
40 | * 2014-05-30 Csaba Kos <csaba.kos@gmail.com> | |
41 | * - fixed a rare deadlock after child termination | |
7eda085c KZ |
42 | */ |
43 | ||
22853e4a KZ |
44 | #include <stdio.h> |
45 | #include <stdlib.h> | |
46 | #include <paths.h> | |
47 | #include <time.h> | |
6dbe3af9 KZ |
48 | #include <sys/stat.h> |
49 | #include <termios.h> | |
50 | #include <sys/ioctl.h> | |
51 | #include <sys/time.h> | |
172b3c27 | 52 | #include <signal.h> |
1f58c445 | 53 | #include <errno.h> |
2b7ff0d9 ST |
54 | #include <string.h> |
55 | #include <getopt.h> | |
56 | #include <unistd.h> | |
8fb810ff | 57 | #include <fcntl.h> |
8fb810ff SK |
58 | #include <limits.h> |
59 | #include <locale.h> | |
60 | #include <stddef.h> | |
3822032d KZ |
61 | #include <sys/wait.h> |
62 | #include <poll.h> | |
cc1a88fb SK |
63 | #include <sys/signalfd.h> |
64 | #include <assert.h> | |
c2f03da9 | 65 | #include <inttypes.h> |
2b7ff0d9 | 66 | |
cdd2a8c3 | 67 | #include "closestream.h" |
7eda085c | 68 | #include "nls.h" |
91239874 | 69 | #include "c.h" |
3822032d | 70 | #include "ttyutils.h" |
c6fca22e | 71 | #include "all-io.h" |
04639805 | 72 | #include "monotonic.h" |
bdef362d | 73 | #include "timeutils.h" |
aefe9893 | 74 | #include "strutils.h" |
6dbe3af9 | 75 | |
a2b4dec5 KZ |
76 | #include "debug.h" |
77 | ||
2ba641e5 | 78 | static UL_DEBUG_DEFINE_MASK(script); |
a2b4dec5 KZ |
79 | UL_DEBUG_DEFINE_MASKNAMES(script) = UL_DEBUG_EMPTY_MASKNAMES; |
80 | ||
81 | #define SCRIPT_DEBUG_INIT (1 << 1) | |
82 | #define SCRIPT_DEBUG_POLL (1 << 2) | |
83 | #define SCRIPT_DEBUG_SIGNAL (1 << 3) | |
84 | #define SCRIPT_DEBUG_IO (1 << 4) | |
85 | #define SCRIPT_DEBUG_MISC (1 << 5) | |
86 | #define SCRIPT_DEBUG_ALL 0xFFFF | |
87 | ||
88 | #define DBG(m, x) __UL_DBG(script, SCRIPT_DEBUG_, m, x) | |
89 | #define ON_DBG(m, x) __UL_DBG_CALL(script, SCRIPT_DEBUG_, m, x) | |
90 | ||
e11a5e63 SK |
91 | #if defined(HAVE_LIBUTIL) && defined(HAVE_PTY_H) |
92 | # include <pty.h> | |
5c36a0eb KZ |
93 | #endif |
94 | ||
f0bc3fa0 | 95 | #ifdef HAVE_LIBUTEMPTER |
e11a5e63 | 96 | # include <utempter.h> |
f0bc3fa0 KZ |
97 | #endif |
98 | ||
7e5796c9 | 99 | #define DEFAULT_TYPESCRIPT_FILENAME "typescript" |
05644dab | 100 | |
edc7e420 SK |
101 | struct script_control { |
102 | char *shell; /* shell to be executed */ | |
3f19b85f | 103 | char *command; /* command to be executed */ |
edc7e420 SK |
104 | char *fname; /* output file path */ |
105 | FILE *typescriptfp; /* output file pointer */ | |
9580536a | 106 | char *tname; /* timing file path */ |
edc7e420 | 107 | FILE *timingfp; /* timing file pointer */ |
0da73643 KZ |
108 | uint64_t outsz; /* current output file size */ |
109 | uint64_t maxsz; /* maximum output file size */ | |
04639805 | 110 | struct timeval oldtime; /* previous write or command start time */ |
edc7e420 SK |
111 | int master; /* pseudoterminal master file descriptor */ |
112 | int slave; /* pseudoterminal slave file descriptor */ | |
89a859d4 | 113 | int poll_timeout; /* poll() timeout, used in end of execution */ |
edc7e420 | 114 | pid_t child; /* child pid */ |
edc7e420 | 115 | int childstatus; /* child process exit value */ |
3f19b85f | 116 | struct termios attrs; /* slave terminal runtime attributes */ |
edc7e420 | 117 | struct winsize win; /* terminal window size */ |
9779651e | 118 | #if !HAVE_LIBUTIL || !HAVE_PTY_H |
8d6fdd2f | 119 | char *line; /* terminal line */ |
5c36a0eb | 120 | #endif |
edc7e420 | 121 | unsigned int |
3f19b85f | 122 | append:1, /* append output */ |
7e5796c9 | 123 | rc_wanted:1, /* return child exit value */ |
3f19b85f KZ |
124 | flush:1, /* flush after each write */ |
125 | quiet:1, /* suppress most output */ | |
126 | timing:1, /* include timing file */ | |
127 | force:1, /* write output to links */ | |
edc7e420 | 128 | isterm:1, /* is child process running as terminal */ |
edc7e420 | 129 | die:1; /* terminate program */ |
7e5796c9 | 130 | |
cc1a88fb | 131 | sigset_t sigset; /* catch SIGCHLD and SIGWINCH with signalfd() */ |
d35ffe80 | 132 | sigset_t sigorg; /* original signal mask */ |
cc1a88fb | 133 | int sigfd; /* file descriptor for signalfd() */ |
edc7e420 | 134 | }; |
1f58c445 | 135 | |
a2b4dec5 KZ |
136 | static void script_init_debug(void) |
137 | { | |
a15dca2f | 138 | __UL_INIT_DEBUG_FROM_ENV(script, SCRIPT_DEBUG_, 0, SCRIPT_DEBUG); |
a2b4dec5 KZ |
139 | } |
140 | ||
6a40c65f SK |
141 | /* |
142 | * For tests we want to be able to control time output | |
143 | */ | |
144 | #ifdef TEST_SCRIPT | |
145 | static inline time_t script_time(time_t *t) | |
146 | { | |
147 | const char *str = getenv("SCRIPT_TEST_SECOND_SINCE_EPOCH"); | |
345208a5 | 148 | int64_t sec; |
6a40c65f | 149 | |
c2f03da9 | 150 | if (!str || sscanf(str, "%"SCNi64, &sec) != 1) |
345208a5 ID |
151 | return time(t); |
152 | if (t) | |
153 | *t = (time_t)sec; | |
154 | return (time_t)sec; | |
6a40c65f SK |
155 | } |
156 | #else /* !TEST_SCRIPT */ | |
157 | # define script_time(x) time(x) | |
158 | #endif | |
159 | ||
86be6a32 | 160 | static void __attribute__((__noreturn__)) usage(void) |
3ff52639 | 161 | { |
86be6a32 | 162 | FILE *out = stdout; |
db433bf7 | 163 | fputs(USAGE_HEADER, out); |
edc7e420 | 164 | fprintf(out, _(" %s [options] [file]\n"), program_invocation_short_name); |
87d6050b | 165 | |
451dbcfa BS |
166 | fputs(USAGE_SEPARATOR, out); |
167 | fputs(_("Make a typescript of a terminal session.\n"), out); | |
168 | ||
db433bf7 | 169 | fputs(USAGE_OPTIONS, out); |
3cf274c9 AO |
170 | fputs(_(" -a, --append append the output\n" |
171 | " -c, --command <command> run command rather than interactive shell\n" | |
172 | " -e, --return return exit code of the child process\n" | |
173 | " -f, --flush run flush after each write\n" | |
174 | " --force use output file even when it is a link\n" | |
aefe9893 | 175 | " -o, --output-limit <size> terminate if output files exceed size\n" |
3cf274c9 AO |
176 | " -q, --quiet be quiet\n" |
177 | " -t[<file>], --timing[=<file>] output timing data to stderr or to FILE\n" | |
b3054454 | 178 | ), out); |
3cf274c9 | 179 | printf(USAGE_HELP_OPTIONS(31)); |
3ff52639 | 180 | |
f45f3ec3 | 181 | printf(USAGE_MAN_TAIL("script(1)")); |
86be6a32 | 182 | exit(EXIT_SUCCESS); |
3ff52639 SK |
183 | } |
184 | ||
4d9b788d KZ |
185 | static void typescript_message_start(const struct script_control *ctl, time_t *tvec) |
186 | { | |
187 | char buf[FORMAT_TIMESTAMP_MAX]; | |
188 | int cols = 0, lines = 0; | |
189 | const char *tty = NULL, *term = NULL; | |
190 | ||
191 | if (!ctl->typescriptfp) | |
192 | return; | |
193 | ||
194 | strtime_iso(tvec, ISO_TIMESTAMP, buf, sizeof(buf)); | |
195 | ||
196 | fprintf(ctl->typescriptfp, _("Script started on %s ["), buf); | |
197 | ||
198 | if (ctl->isterm) { | |
199 | get_terminal_dimension(&cols, &lines); | |
200 | get_terminal_name(&tty, NULL, NULL); | |
201 | get_terminal_type(&term); | |
202 | ||
203 | if (term) | |
204 | fprintf(ctl->typescriptfp, "TERM=\"%s\" ", term); | |
205 | if (tty) | |
206 | fprintf(ctl->typescriptfp, "TTY=\"%s\" ", tty); | |
207 | ||
8a69ad47 | 208 | fprintf(ctl->typescriptfp, "COLUMNS=\"%d\" LINES=\"%d\"", cols, lines); |
4d9b788d KZ |
209 | } else |
210 | fprintf(ctl->typescriptfp, _("<not executed on terminal>")); | |
211 | ||
212 | fputs("]\n", ctl->typescriptfp); | |
213 | } | |
214 | ||
6343ee8c KZ |
215 | static void typescript_message_done(const struct script_control *ctl, int status, const char *msg) |
216 | { | |
217 | char buf[FORMAT_TIMESTAMP_MAX]; | |
218 | time_t tvec; | |
219 | ||
220 | if (!ctl->typescriptfp) | |
221 | return; | |
222 | ||
223 | tvec = script_time((time_t *)NULL); | |
224 | ||
225 | strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf)); | |
226 | ||
227 | if (msg) | |
228 | fprintf(ctl->typescriptfp, _("\nScript done on %s [<%s>]\n"), buf, msg); | |
229 | else | |
230 | fprintf(ctl->typescriptfp, _("\nScript done on %s [COMMAND_EXIT_CODE=\"%d\"]\n"), buf, status); | |
231 | } | |
232 | ||
edc7e420 | 233 | static void die_if_link(const struct script_control *ctl) |
93af8d8b SK |
234 | { |
235 | struct stat s; | |
6dbe3af9 | 236 | |
3f19b85f | 237 | if (ctl->force) |
93af8d8b | 238 | return; |
edc7e420 | 239 | if (lstat(ctl->fname, &s) == 0 && (S_ISLNK(s.st_mode) || s.st_nlink > 1)) |
93af8d8b SK |
240 | errx(EXIT_FAILURE, |
241 | _("output file `%s' is a link\n" | |
242 | "Use --force if you really want to use it.\n" | |
edc7e420 | 243 | "Program not started."), ctl->fname); |
93af8d8b | 244 | } |
fd4c1f63 | 245 | |
8198052c SK |
246 | static void restore_tty(struct script_control *ctl, int mode) |
247 | { | |
248 | struct termios rtt; | |
249 | ||
250 | if (!ctl->isterm) | |
251 | return; | |
252 | ||
253 | rtt = ctl->attrs; | |
254 | tcsetattr(STDIN_FILENO, mode, &rtt); | |
255 | } | |
256 | ||
7fb65db1 KZ |
257 | static void enable_rawmode_tty(struct script_control *ctl) |
258 | { | |
259 | struct termios rtt; | |
260 | ||
261 | if (!ctl->isterm) | |
262 | return; | |
263 | ||
264 | rtt = ctl->attrs; | |
265 | cfmakeraw(&rtt); | |
266 | rtt.c_lflag &= ~ECHO; | |
267 | tcsetattr(STDIN_FILENO, TCSANOW, &rtt); | |
268 | } | |
269 | ||
6343ee8c | 270 | static void __attribute__((__noreturn__)) done_log(struct script_control *ctl, const char *log_msg) |
93af8d8b | 271 | { |
6343ee8c KZ |
272 | int childstatus; |
273 | ||
a2b4dec5 KZ |
274 | DBG(MISC, ul_debug("done!")); |
275 | ||
8198052c SK |
276 | restore_tty(ctl, TCSADRAIN); |
277 | ||
6343ee8c KZ |
278 | if (WIFSIGNALED(ctl->childstatus)) |
279 | childstatus = WTERMSIG(ctl->childstatus) + 0x80; | |
280 | else | |
281 | childstatus = WEXITSTATUS(ctl->childstatus); | |
93df6a58 | 282 | |
6343ee8c KZ |
283 | if (ctl->typescriptfp) { |
284 | typescript_message_done(ctl, childstatus, log_msg); | |
285 | if (!ctl->quiet) | |
286 | printf(_("Script done, file is %s\n"), ctl->fname); | |
93df6a58 | 287 | } |
f0bc3fa0 | 288 | #ifdef HAVE_LIBUTEMPTER |
6ddf53a5 SK |
289 | if (ctl->master >= 0) |
290 | utempter_remove_record(ctl->master); | |
f0bc3fa0 | 291 | #endif |
6ddf53a5 | 292 | kill(ctl->child, SIGTERM); /* make sure we don't create orphans */ |
f1014a4f | 293 | |
53ca0532 KZ |
294 | if (ctl->timingfp && close_stream(ctl->timingfp) != 0) |
295 | err(EXIT_FAILURE, "write failed: %s", ctl->tname); | |
296 | if (ctl->typescriptfp && close_stream(ctl->typescriptfp) != 0) | |
297 | err(EXIT_FAILURE, "write failed: %s", ctl->fname); | |
7e5796c9 | 298 | |
6343ee8c KZ |
299 | exit(ctl->rc_wanted ? childstatus : EXIT_SUCCESS); |
300 | } | |
301 | ||
302 | static void __attribute__((__noreturn__)) done(struct script_control *ctl) | |
303 | { | |
304 | done_log(ctl, NULL); | |
93af8d8b | 305 | } |
5c36a0eb | 306 | |
2ddadb5e | 307 | static void __attribute__((__noreturn__)) fail(struct script_control *ctl) |
edc7e420 | 308 | { |
a2b4dec5 | 309 | DBG(MISC, ul_debug("fail!")); |
93af8d8b | 310 | kill(0, SIGTERM); |
edc7e420 | 311 | done(ctl); |
6dbe3af9 KZ |
312 | } |
313 | ||
5860c45e | 314 | static void wait_for_child(struct script_control *ctl, int wait) |
93af8d8b SK |
315 | { |
316 | int status; | |
317 | pid_t pid; | |
93af8d8b SK |
318 | int options = wait ? 0 : WNOHANG; |
319 | ||
a2b4dec5 KZ |
320 | DBG(MISC, ul_debug("waiting for child")); |
321 | ||
b09feab9 | 322 | while ((pid = wait3(&status, options, NULL)) > 0) |
89a859d4 | 323 | if (pid == ctl->child) |
edc7e420 | 324 | ctl->childstatus = status; |
93af8d8b SK |
325 | } |
326 | ||
cf470183 | 327 | static void write_output(struct script_control *ctl, char *obuf, |
04639805 | 328 | ssize_t bytes) |
93af8d8b | 329 | { |
aefe9893 FM |
330 | int timing_bytes = 0; |
331 | ||
a2b4dec5 KZ |
332 | DBG(IO, ul_debug(" writing output")); |
333 | ||
3f19b85f | 334 | if (ctl->timing && ctl->timingfp) { |
04639805 | 335 | struct timeval now, delta; |
cf470183 | 336 | |
a2b4dec5 KZ |
337 | DBG(IO, ul_debug(" writing timing info")); |
338 | ||
04639805 SK |
339 | gettime_monotonic(&now); |
340 | timersub(&now, &ctl->oldtime, &delta); | |
aefe9893 | 341 | timing_bytes = fprintf(ctl->timingfp, "%ld.%06ld %zd\n", |
7231fb2a | 342 | (long)delta.tv_sec, (long)delta.tv_usec, bytes); |
3f19b85f | 343 | if (ctl->flush) |
cf470183 | 344 | fflush(ctl->timingfp); |
04639805 | 345 | ctl->oldtime = now; |
aefe9893 FM |
346 | if (timing_bytes < 0) |
347 | timing_bytes = 0; | |
cf470183 | 348 | } |
a2b4dec5 KZ |
349 | |
350 | DBG(IO, ul_debug(" writing to script file")); | |
351 | ||
cf470183 SK |
352 | if (fwrite_all(obuf, 1, bytes, ctl->typescriptfp)) { |
353 | warn(_("cannot write script file")); | |
354 | fail(ctl); | |
355 | } | |
3f19b85f | 356 | if (ctl->flush) |
cf470183 | 357 | fflush(ctl->typescriptfp); |
a2b4dec5 KZ |
358 | |
359 | DBG(IO, ul_debug(" writing to output")); | |
360 | ||
cf470183 | 361 | if (write_all(STDOUT_FILENO, obuf, bytes)) { |
54c6611d | 362 | DBG(IO, ul_debug(" writing output *failed*")); |
cf470183 SK |
363 | warn(_("write failed")); |
364 | fail(ctl); | |
365 | } | |
54c6611d | 366 | |
aefe9893 FM |
367 | if (ctl->maxsz != 0) |
368 | ctl->outsz += bytes + timing_bytes; | |
369 | ||
54c6611d KZ |
370 | DBG(IO, ul_debug(" writing output *done*")); |
371 | } | |
372 | ||
b2bff066 KZ |
373 | static int write_to_shell(struct script_control *ctl, |
374 | char *buf, size_t bufsz) | |
54c6611d | 375 | { |
b2bff066 | 376 | return write_all(ctl->master, buf, bufsz); |
54c6611d KZ |
377 | } |
378 | ||
379 | /* | |
23e35eca RM |
380 | * The script(1) is usually faster than shell, so it's a good idea to wait until |
381 | * the previous message has been already read by shell from slave before we | |
382 | * write to master. This is necessary especially for EOF situation when we can | |
54c6611d KZ |
383 | * send EOF to master before shell is fully initialized, to workaround this |
384 | * problem we wait until slave is empty. For example: | |
385 | * | |
386 | * echo "date" | script | |
b2bff066 KZ |
387 | * |
388 | * Unfortunately, the child (usually shell) can ignore stdin at all, so we | |
389 | * don't wait forever to avoid dead locks... | |
390 | * | |
391 | * Note that script is primarily designed for interactive sessions as it | |
392 | * maintains master+slave tty stuff within the session. Use pipe to write to | |
393 | * script(1) and assume non-interactive (tee-like) behavior is NOT well | |
394 | * supported. | |
54c6611d | 395 | */ |
54c6611d KZ |
396 | static void write_eof_to_shell(struct script_control *ctl) |
397 | { | |
b2bff066 KZ |
398 | unsigned int tries = 0; |
399 | struct pollfd fds[] = { | |
400 | { .fd = ctl->slave, .events = POLLIN } | |
401 | }; | |
54c6611d KZ |
402 | char c = DEF_EOF; |
403 | ||
b2bff066 KZ |
404 | DBG(IO, ul_debug(" waiting for empty slave")); |
405 | while (poll(fds, 1, 10) == 1 && tries < 8) { | |
406 | DBG(IO, ul_debug(" slave is not empty")); | |
407 | xusleep(250000); | |
408 | tries++; | |
409 | } | |
410 | if (tries < 8) | |
411 | DBG(IO, ul_debug(" slave is empty now")); | |
412 | ||
54c6611d KZ |
413 | DBG(IO, ul_debug(" sending EOF to master")); |
414 | write_to_shell(ctl, &c, sizeof(char)); | |
364cda48 KZ |
415 | } |
416 | ||
54c6611d | 417 | static void handle_io(struct script_control *ctl, int fd, int *eof) |
93af8d8b | 418 | { |
6ddf53a5 | 419 | char buf[BUFSIZ]; |
a8896ad5 | 420 | ssize_t bytes; |
a2b4dec5 | 421 | DBG(IO, ul_debug("%d FD active", fd)); |
54c6611d | 422 | *eof = 0; |
a2b4dec5 | 423 | |
da26af4e | 424 | /* read from active FD */ |
a8896ad5 SK |
425 | bytes = read(fd, buf, sizeof(buf)); |
426 | if (bytes < 0) { | |
54c6611d | 427 | if (errno == EAGAIN || errno == EINTR) |
a8896ad5 SK |
428 | return; |
429 | fail(ctl); | |
430 | } | |
da26af4e | 431 | |
54c6611d | 432 | if (bytes == 0) { |
7dbcd80e | 433 | *eof = 1; |
54c6611d KZ |
434 | return; |
435 | } | |
436 | ||
da26af4e KZ |
437 | /* from stdin (user) to command */ |
438 | if (fd == STDIN_FILENO) { | |
2e9418b7 | 439 | DBG(IO, ul_debug(" stdin --> master %zd bytes", bytes)); |
a2b4dec5 | 440 | |
54c6611d | 441 | if (write_to_shell(ctl, buf, bytes)) { |
a8896ad5 SK |
442 | warn(_("write failed")); |
443 | fail(ctl); | |
444 | } | |
445 | /* without sync write_output() will write both input & | |
446 | * shell output that looks like double echoing */ | |
447 | fdatasync(ctl->master); | |
da26af4e | 448 | |
aefe9893 | 449 | /* from command (master) to stdout and log */ |
a2b4dec5 | 450 | } else if (fd == ctl->master) { |
2e9418b7 | 451 | DBG(IO, ul_debug(" master --> stdout %zd bytes", bytes)); |
04639805 | 452 | write_output(ctl, buf, bytes); |
aefe9893 FM |
453 | |
454 | /* check output limit */ | |
455 | if (ctl->maxsz != 0 && ctl->outsz >= ctl->maxsz) { | |
456 | if (!ctl->quiet) | |
aafa27c1 RM |
457 | printf(_("Script terminated, max output file size %"PRIu64" exceeded.\n"), ctl->maxsz); |
458 | DBG(IO, ul_debug("output size %"PRIu64", exceeded limit %"PRIu64, ctl->outsz, ctl->maxsz)); | |
6343ee8c | 459 | done_log(ctl, _("max output size exceeded")); |
aefe9893 | 460 | } |
a2b4dec5 | 461 | } |
a8896ad5 SK |
462 | } |
463 | ||
464 | static void handle_signal(struct script_control *ctl, int fd) | |
465 | { | |
466 | struct signalfd_siginfo info; | |
467 | ssize_t bytes; | |
468 | ||
a2b4dec5 KZ |
469 | DBG(SIGNAL, ul_debug("signal FD %d active", fd)); |
470 | ||
a8896ad5 | 471 | bytes = read(fd, &info, sizeof(info)); |
d35ffe80 | 472 | if (bytes != sizeof(info)) { |
7dbcd80e | 473 | if (bytes < 0 && (errno == EAGAIN || errno == EINTR)) |
d35ffe80 KZ |
474 | return; |
475 | fail(ctl); | |
476 | } | |
da26af4e | 477 | |
a8896ad5 SK |
478 | switch (info.ssi_signo) { |
479 | case SIGCHLD: | |
27afe501 KZ |
480 | DBG(SIGNAL, ul_debug(" get signal SIGCHLD [ssi_code=%d, ssi_status=%d]", |
481 | info.ssi_code, info.ssi_status)); | |
482 | if (info.ssi_code == CLD_EXITED | |
483 | || info.ssi_code == CLD_KILLED | |
484 | || info.ssi_code == CLD_DUMPED) { | |
2e7a9227 KZ |
485 | wait_for_child(ctl, 0); |
486 | ctl->poll_timeout = 10; | |
27afe501 KZ |
487 | |
488 | /* In case of ssi_code is CLD_TRAPPED, CLD_STOPPED, or CLD_CONTINUED */ | |
2e7a9227 KZ |
489 | } else if (info.ssi_status == SIGSTOP && ctl->child) { |
490 | DBG(SIGNAL, ul_debug(" child stop by SIGSTOP -- stop parent too")); | |
491 | kill(getpid(), SIGSTOP); | |
492 | DBG(SIGNAL, ul_debug(" resume")); | |
493 | kill(ctl->child, SIGCONT); | |
494 | } | |
a8896ad5 SK |
495 | return; |
496 | case SIGWINCH: | |
b2bff066 | 497 | DBG(SIGNAL, ul_debug(" get signal SIGWINCH")); |
a8896ad5 SK |
498 | if (ctl->isterm) { |
499 | ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ctl->win); | |
500 | ioctl(ctl->slave, TIOCSWINSZ, (char *)&ctl->win); | |
501 | } | |
502 | break; | |
5860c45e KZ |
503 | case SIGTERM: |
504 | /* fallthrough */ | |
505 | case SIGINT: | |
506 | /* fallthrough */ | |
507 | case SIGQUIT: | |
b2bff066 | 508 | DBG(SIGNAL, ul_debug(" get signal SIG{TERM,INT,QUIT}")); |
5860c45e KZ |
509 | fprintf(stderr, _("\nSession terminated.\n")); |
510 | /* Child termination is going to generate SIGCHILD (see above) */ | |
511 | kill(ctl->child, SIGTERM); | |
512 | return; | |
a8896ad5 SK |
513 | default: |
514 | abort(); | |
515 | } | |
27afe501 | 516 | DBG(SIGNAL, ul_debug("signal handle on FD %d done", fd)); |
a8896ad5 SK |
517 | } |
518 | ||
519 | static void do_io(struct script_control *ctl) | |
520 | { | |
d88b739f | 521 | int ret, eof = 0; |
076ffa48 | 522 | time_t tvec = script_time((time_t *)NULL); |
da26af4e | 523 | enum { |
54c6611d | 524 | POLLFD_SIGNAL = 0, |
da26af4e | 525 | POLLFD_MASTER, |
d88b739f | 526 | POLLFD_STDIN |
54c6611d | 527 | |
da26af4e KZ |
528 | }; |
529 | struct pollfd pfd[] = { | |
0664d41d SK |
530 | [POLLFD_SIGNAL] = { .fd = ctl->sigfd, .events = POLLIN | POLLERR | POLLHUP }, |
531 | [POLLFD_MASTER] = { .fd = ctl->master, .events = POLLIN | POLLERR | POLLHUP }, | |
532 | [POLLFD_STDIN] = { .fd = STDIN_FILENO, .events = POLLIN | POLLERR | POLLHUP } | |
da26af4e KZ |
533 | }; |
534 | ||
6dbe3af9 | 535 | |
760e5e68 SK |
536 | if ((ctl->typescriptfp = |
537 | fopen(ctl->fname, ctl->append ? "a" UL_CLOEXECSTR : "w" UL_CLOEXECSTR)) == NULL) { | |
8198052c SK |
538 | |
539 | restore_tty(ctl, TCSANOW); | |
9580536a SK |
540 | warn(_("cannot open %s"), ctl->fname); |
541 | fail(ctl); | |
542 | } | |
3f19b85f | 543 | if (ctl->timing) { |
8198052c SK |
544 | const char *tname = ctl->tname ? ctl->tname : "/dev/stderr"; |
545 | ||
546 | if (!(ctl->timingfp = fopen(tname, "w" UL_CLOEXECSTR))) { | |
547 | restore_tty(ctl, TCSANOW); | |
548 | warn(_("cannot open %s"), tname); | |
549 | fail(ctl); | |
550 | } | |
9580536a | 551 | } |
4368e3e6 | 552 | |
54c6611d | 553 | |
4d9b788d KZ |
554 | if (ctl->typescriptfp) |
555 | typescript_message_start(ctl, &tvec); | |
556 | ||
04639805 | 557 | gettime_monotonic(&ctl->oldtime); |
a5a16c68 | 558 | |
6ddf53a5 | 559 | while (!ctl->die) { |
da26af4e | 560 | size_t i; |
7dbcd80e | 561 | int errsv; |
da26af4e | 562 | |
a2b4dec5 KZ |
563 | DBG(POLL, ul_debug("calling poll()")); |
564 | ||
cf470183 | 565 | /* wait for input or signal */ |
d88b739f | 566 | ret = poll(pfd, ARRAY_SIZE(pfd), ctl->poll_timeout); |
7dbcd80e | 567 | errsv = errno; |
a2b4dec5 KZ |
568 | DBG(POLL, ul_debug("poll() rc=%d", ret)); |
569 | ||
cf470183 | 570 | if (ret < 0) { |
7dbcd80e | 571 | if (errsv == EAGAIN) |
cf470183 SK |
572 | continue; |
573 | warn(_("poll failed")); | |
edc7e420 | 574 | fail(ctl); |
36c1d79b | 575 | } |
a2b4dec5 KZ |
576 | if (ret == 0) { |
577 | DBG(POLL, ul_debug("setting die=1")); | |
89a859d4 | 578 | ctl->die = 1; |
5860c45e | 579 | break; |
a2b4dec5 | 580 | } |
da26af4e | 581 | |
d88b739f | 582 | for (i = 0; i < ARRAY_SIZE(pfd); i++) { |
cf470183 SK |
583 | if (pfd[i].revents == 0) |
584 | continue; | |
a2b4dec5 | 585 | |
2e9418b7 | 586 | DBG(POLL, ul_debug(" active pfd[%s].fd=%d %s %s %s", |
54c6611d | 587 | i == POLLFD_STDIN ? "stdin" : |
2e9418b7 KZ |
588 | i == POLLFD_MASTER ? "master" : |
589 | i == POLLFD_SIGNAL ? "signal" : "???", | |
590 | pfd[i].fd, | |
54c6611d | 591 | pfd[i].revents & POLLIN ? "POLLIN" : "", |
1200cfbf KZ |
592 | pfd[i].revents & POLLHUP ? "POLLHUP" : "", |
593 | pfd[i].revents & POLLERR ? "POLLERR" : "")); | |
da26af4e KZ |
594 | switch (i) { |
595 | case POLLFD_STDIN: | |
596 | case POLLFD_MASTER: | |
54c6611d KZ |
597 | /* data */ |
598 | if (pfd[i].revents & POLLIN) | |
599 | handle_io(ctl, pfd[i].fd, &eof); | |
600 | /* EOF maybe detected by two ways: | |
601 | * A) poll() return POLLHUP event after close() | |
7dbcd80e | 602 | * B) read() returns 0 (no data) */ |
54c6611d KZ |
603 | if ((pfd[i].revents & POLLHUP) || eof) { |
604 | DBG(POLL, ul_debug(" ignore FD")); | |
605 | pfd[i].fd = -1; | |
54c6611d | 606 | if (i == POLLFD_STDIN) { |
54c6611d | 607 | write_eof_to_shell(ctl); |
b2bff066 | 608 | DBG(POLL, ul_debug(" ignore STDIN")); |
54c6611d KZ |
609 | } |
610 | } | |
cf470183 | 611 | continue; |
da26af4e | 612 | case POLLFD_SIGNAL: |
a8896ad5 | 613 | handle_signal(ctl, pfd[i].fd); |
da26af4e | 614 | break; |
cf470183 | 615 | } |
968e632c | 616 | } |
cf470183 | 617 | } |
a2b4dec5 KZ |
618 | |
619 | DBG(POLL, ul_debug("poll() done")); | |
620 | ||
6ddf53a5 | 621 | if (!ctl->die) |
5860c45e | 622 | wait_for_child(ctl, 1); |
6f3c9c34 | 623 | |
6ddf53a5 | 624 | done(ctl); |
6dbe3af9 KZ |
625 | } |
626 | ||
edc7e420 | 627 | static void getslave(struct script_control *ctl) |
93af8d8b SK |
628 | { |
629 | #ifndef HAVE_LIBUTIL | |
edc7e420 | 630 | ctl->line[strlen("/dev/")] = 't'; |
760e5e68 | 631 | ctl->slave = open(ctl->line, O_RDWR | O_CLOEXEC); |
edc7e420 SK |
632 | if (ctl->slave < 0) { |
633 | warn(_("cannot open %s"), ctl->line); | |
634 | fail(ctl); | |
93af8d8b | 635 | } |
edc7e420 | 636 | if (ctl->isterm) { |
3f19b85f | 637 | tcsetattr(ctl->slave, TCSANOW, &ctl->attrs); |
edc7e420 | 638 | ioctl(ctl->slave, TIOCSWINSZ, (char *)&ctl->win); |
93af8d8b SK |
639 | } |
640 | #endif | |
641 | setsid(); | |
edc7e420 | 642 | ioctl(ctl->slave, TIOCSCTTY, 0); |
93af8d8b SK |
643 | } |
644 | ||
54c6611d | 645 | /* don't use DBG() stuff here otherwise it will be in the typescript file */ |
d35ffe80 | 646 | static void __attribute__((__noreturn__)) do_shell(struct script_control *ctl) |
93af8d8b | 647 | { |
df1dddf9 KZ |
648 | char *shname; |
649 | ||
edc7e420 | 650 | getslave(ctl); |
4368e3e6 KZ |
651 | |
652 | /* close things irrelevant for this process */ | |
edc7e420 | 653 | close(ctl->master); |
d35ffe80 | 654 | close(ctl->sigfd); |
4368e3e6 | 655 | |
edc7e420 SK |
656 | dup2(ctl->slave, STDIN_FILENO); |
657 | dup2(ctl->slave, STDOUT_FILENO); | |
658 | dup2(ctl->slave, STDERR_FILENO); | |
659 | close(ctl->slave); | |
df1dddf9 | 660 | |
edc7e420 | 661 | ctl->master = -1; |
f0bc3fa0 | 662 | |
edc7e420 | 663 | shname = strrchr(ctl->shell, '/'); |
df1dddf9 KZ |
664 | if (shname) |
665 | shname++; | |
666 | else | |
edc7e420 | 667 | shname = ctl->shell; |
df1dddf9 | 668 | |
d35ffe80 KZ |
669 | sigprocmask(SIG_SETMASK, &ctl->sigorg, NULL); |
670 | ||
a17f3264 KZ |
671 | /* |
672 | * When invoked from within /etc/csh.login, script spawns a csh shell | |
673 | * that spawns programs that cannot be killed with a SIGTERM. This is | |
ee312c65 | 674 | * because csh has a documented behavior wherein it disables all |
a17f3264 KZ |
675 | * signals when processing the /etc/csh.* files. |
676 | * | |
677 | * Let's restore the default behavior. | |
678 | */ | |
679 | signal(SIGTERM, SIG_DFL); | |
680 | ||
edc7e420 | 681 | if (access(ctl->shell, X_OK) == 0) { |
3f19b85f KZ |
682 | if (ctl->command) |
683 | execl(ctl->shell, shname, "-c", ctl->command, NULL); | |
b4ff2f54 | 684 | else |
edc7e420 | 685 | execl(ctl->shell, shname, "-i", NULL); |
b4ff2f54 | 686 | } else { |
3f19b85f KZ |
687 | if (ctl->command) |
688 | execlp(shname, "-c", ctl->command, NULL); | |
b4ff2f54 SK |
689 | else |
690 | execlp(shname, "-i", NULL); | |
691 | } | |
edc7e420 SK |
692 | warn(_("failed to execute %s"), ctl->shell); |
693 | fail(ctl); | |
6dbe3af9 KZ |
694 | } |
695 | ||
6dbe3af9 | 696 | |
edc7e420 | 697 | static void getmaster(struct script_control *ctl) |
93af8d8b | 698 | { |
e11a5e63 | 699 | #if defined(HAVE_LIBUTIL) && defined(HAVE_PTY_H) |
3822032d KZ |
700 | int rc; |
701 | ||
edc7e420 | 702 | ctl->isterm = isatty(STDIN_FILENO); |
3822032d | 703 | |
edc7e420 | 704 | if (ctl->isterm) { |
3f19b85f | 705 | if (tcgetattr(STDIN_FILENO, &ctl->attrs) != 0) |
3822032d | 706 | err(EXIT_FAILURE, _("failed to get terminal attributes")); |
edc7e420 | 707 | ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ctl->win); |
3f19b85f | 708 | rc = openpty(&ctl->master, &ctl->slave, NULL, &ctl->attrs, &ctl->win); |
3822032d | 709 | } else |
edc7e420 | 710 | rc = openpty(&ctl->master, &ctl->slave, NULL, NULL, NULL); |
3822032d KZ |
711 | |
712 | if (rc < 0) { | |
360d5005 | 713 | warn(_("openpty failed")); |
edc7e420 | 714 | fail(ctl); |
5c36a0eb KZ |
715 | } |
716 | #else | |
6dbe3af9 | 717 | char *pty, *bank, *cp; |
6dbe3af9 | 718 | |
edc7e420 | 719 | ctl->isterm = isatty(STDIN_FILENO); |
3822032d | 720 | |
edc7e420 | 721 | pty = &ctl->line[strlen("/dev/ptyp")]; |
6dbe3af9 | 722 | for (bank = "pqrs"; *bank; bank++) { |
edc7e420 | 723 | ctl->line[strlen("/dev/pty")] = *bank; |
6dbe3af9 | 724 | *pty = '0'; |
a4aeb5bd | 725 | if (access(ctl->line, F_OK) != 0) |
6dbe3af9 KZ |
726 | break; |
727 | for (cp = "0123456789abcdef"; *cp; cp++) { | |
728 | *pty = *cp; | |
760e5e68 | 729 | ctl->master = open(ctl->line, O_RDWR | O_CLOEXEC); |
edc7e420 SK |
730 | if (ctl->master >= 0) { |
731 | char *tp = &ctl->line[strlen("/dev/")]; | |
6dbe3af9 KZ |
732 | int ok; |
733 | ||
734 | /* verify slave side is usable */ | |
735 | *tp = 't'; | |
edc7e420 | 736 | ok = access(ctl->line, R_OK | W_OK) == 0; |
6dbe3af9 KZ |
737 | *tp = 'p'; |
738 | if (ok) { | |
edc7e420 | 739 | if (ctl->isterm) { |
3f19b85f | 740 | tcgetattr(STDIN_FILENO, &ctl->attrs); |
edc7e420 | 741 | ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ctl->win); |
3822032d | 742 | } |
6dbe3af9 KZ |
743 | return; |
744 | } | |
edc7e420 SK |
745 | close(ctl->master); |
746 | ctl->master = -1; | |
6dbe3af9 KZ |
747 | } |
748 | } | |
749 | } | |
edc7e420 | 750 | ctl->master = -1; |
360d5005 | 751 | warn(_("out of pty's")); |
edc7e420 SK |
752 | fail(ctl); |
753 | #endif /* not HAVE_LIBUTIL */ | |
54c6611d KZ |
754 | |
755 | DBG(IO, ul_debug("master fd: %d", ctl->master)); | |
6dbe3af9 KZ |
756 | } |
757 | ||
93af8d8b SK |
758 | int main(int argc, char **argv) |
759 | { | |
edc7e420 SK |
760 | struct script_control ctl = { |
761 | #if !HAVE_LIBUTIL || !HAVE_PTY_H | |
762 | .line = "/dev/ptyXX", | |
763 | #endif | |
764 | .master = -1, | |
aefe9893 | 765 | .poll_timeout = -1 |
edc7e420 | 766 | }; |
93af8d8b SK |
767 | int ch; |
768 | ||
769 | enum { FORCE_OPTION = CHAR_MAX + 1 }; | |
770 | ||
771 | static const struct option longopts[] = { | |
edc7e420 SK |
772 | {"append", no_argument, NULL, 'a'}, |
773 | {"command", required_argument, NULL, 'c'}, | |
774 | {"return", no_argument, NULL, 'e'}, | |
775 | {"flush", no_argument, NULL, 'f'}, | |
776 | {"force", no_argument, NULL, FORCE_OPTION,}, | |
aefe9893 | 777 | {"output-limit", required_argument, NULL, 'o'}, |
edc7e420 SK |
778 | {"quiet", no_argument, NULL, 'q'}, |
779 | {"timing", optional_argument, NULL, 't'}, | |
780 | {"version", no_argument, NULL, 'V'}, | |
781 | {"help", no_argument, NULL, 'h'}, | |
782 | {NULL, 0, NULL, 0} | |
93af8d8b SK |
783 | }; |
784 | ||
785 | setlocale(LC_ALL, ""); | |
b09feab9 SK |
786 | /* |
787 | * script -t prints time delays as floating point numbers. The example | |
788 | * program (scriptreplay) that we provide to handle this timing output | |
789 | * is a perl script, and does not handle numbers in locale format (not | |
790 | * even when "use locale;" is added). So, since these numbers are not | |
791 | * for human consumption, it seems easiest to set LC_NUMERIC here. | |
792 | */ | |
793 | setlocale(LC_NUMERIC, "C"); | |
93af8d8b SK |
794 | bindtextdomain(PACKAGE, LOCALEDIR); |
795 | textdomain(PACKAGE); | |
796 | atexit(close_stdout); | |
797 | ||
a2b4dec5 KZ |
798 | script_init_debug(); |
799 | ||
aefe9893 | 800 | while ((ch = getopt_long(argc, argv, "ac:efo:qt::Vh", longopts, NULL)) != -1) |
edc7e420 | 801 | switch (ch) { |
93af8d8b | 802 | case 'a': |
3f19b85f | 803 | ctl.append = 1; |
93af8d8b SK |
804 | break; |
805 | case 'c': | |
3f19b85f | 806 | ctl.command = optarg; |
93af8d8b SK |
807 | break; |
808 | case 'e': | |
7e5796c9 | 809 | ctl.rc_wanted = 1; |
93af8d8b SK |
810 | break; |
811 | case 'f': | |
3f19b85f | 812 | ctl.flush = 1; |
93af8d8b SK |
813 | break; |
814 | case FORCE_OPTION: | |
3f19b85f | 815 | ctl.force = 1; |
93af8d8b | 816 | break; |
aefe9893 FM |
817 | case 'o': |
818 | ctl.maxsz = strtosize_or_err(optarg, _("failed to parse output limit size")); | |
819 | break; | |
93af8d8b | 820 | case 'q': |
3f19b85f | 821 | ctl.quiet = 1; |
93af8d8b SK |
822 | break; |
823 | case 't': | |
9580536a SK |
824 | if (optarg) |
825 | ctl.tname = optarg; | |
3f19b85f | 826 | ctl.timing = 1; |
93af8d8b SK |
827 | break; |
828 | case 'V': | |
829 | printf(UTIL_LINUX_VERSION); | |
830 | exit(EXIT_SUCCESS); | |
831 | break; | |
832 | case 'h': | |
86be6a32 | 833 | usage(); |
93af8d8b | 834 | break; |
93af8d8b | 835 | default: |
677ec86c | 836 | errtryhelp(EXIT_FAILURE); |
93af8d8b SK |
837 | } |
838 | argc -= optind; | |
839 | argv += optind; | |
840 | ||
841 | if (argc > 0) | |
edc7e420 | 842 | ctl.fname = argv[0]; |
93af8d8b | 843 | else { |
7e5796c9 | 844 | ctl.fname = DEFAULT_TYPESCRIPT_FILENAME; |
edc7e420 | 845 | die_if_link(&ctl); |
6dbe3af9 | 846 | } |
93af8d8b | 847 | |
edc7e420 SK |
848 | ctl.shell = getenv("SHELL"); |
849 | if (ctl.shell == NULL) | |
850 | ctl.shell = _PATH_BSHELL; | |
93af8d8b | 851 | |
edc7e420 | 852 | getmaster(&ctl); |
3f19b85f | 853 | if (!ctl.quiet) |
edc7e420 | 854 | printf(_("Script started, file is %s\n"), ctl.fname); |
7fb65db1 | 855 | enable_rawmode_tty(&ctl); |
93af8d8b SK |
856 | |
857 | #ifdef HAVE_LIBUTEMPTER | |
edc7e420 | 858 | utempter_add_record(ctl.master, NULL); |
5c36a0eb | 859 | #endif |
cc1a88fb | 860 | /* setup signal handler */ |
d35ffe80 KZ |
861 | sigemptyset(&ctl.sigset); |
862 | sigaddset(&ctl.sigset, SIGCHLD); | |
863 | sigaddset(&ctl.sigset, SIGWINCH); | |
5860c45e KZ |
864 | sigaddset(&ctl.sigset, SIGTERM); |
865 | sigaddset(&ctl.sigset, SIGINT); | |
866 | sigaddset(&ctl.sigset, SIGQUIT); | |
d35ffe80 KZ |
867 | |
868 | /* block signals used for signalfd() to prevent the signals being | |
869 | * handled according to their default dispositions */ | |
870 | sigprocmask(SIG_BLOCK, &ctl.sigset, &ctl.sigorg); | |
a2b4dec5 | 871 | |
760e5e68 | 872 | if ((ctl.sigfd = signalfd(-1, &ctl.sigset, SFD_CLOEXEC)) < 0) |
cc1a88fb | 873 | err(EXIT_FAILURE, _("cannot set signal handler")); |
93af8d8b | 874 | |
a2b4dec5 KZ |
875 | DBG(SIGNAL, ul_debug("signal fd=%d", ctl.sigfd)); |
876 | ||
93af8d8b | 877 | fflush(stdout); |
edc7e420 | 878 | ctl.child = fork(); |
93af8d8b | 879 | |
d35ffe80 KZ |
880 | switch (ctl.child) { |
881 | case -1: /* error */ | |
93af8d8b | 882 | warn(_("fork failed")); |
edc7e420 | 883 | fail(&ctl); |
d35ffe80 KZ |
884 | break; |
885 | case 0: /* child */ | |
886 | do_shell(&ctl); | |
887 | break; | |
888 | default: /* parent */ | |
889 | do_io(&ctl); | |
890 | break; | |
93af8d8b | 891 | } |
d35ffe80 KZ |
892 | |
893 | /* should not happen, all used functions are non-return */ | |
b09feab9 | 894 | return EXIT_FAILURE; |
6dbe3af9 | 895 | } |