]>
Commit | Line | Data |
---|---|---|
6dbe3af9 | 1 | /* |
ec10634e KZ |
2 | * Copyright (C) 1980 Regents of the University of California. |
3 | * Copyright (C) 2013-2019 Karel Zak <kzak@redhat.com> | |
4 | * | |
6dbe3af9 KZ |
5 | * All rights reserved. |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. All advertising materials mentioning features or use of this software | |
16 | * must display the following acknowledgement: | |
edc7e420 SK |
17 | * This product includes software developed by the University of |
18 | * California, Berkeley and its contributors. | |
6dbe3af9 KZ |
19 | * 4. Neither the name of the University nor the names of its contributors |
20 | * may be used to endorse or promote products derived from this software | |
21 | * without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
33 | * SUCH DAMAGE. | |
34 | */ | |
22853e4a KZ |
35 | #include <stdio.h> |
36 | #include <stdlib.h> | |
37 | #include <paths.h> | |
38 | #include <time.h> | |
6dbe3af9 KZ |
39 | #include <sys/stat.h> |
40 | #include <termios.h> | |
41 | #include <sys/ioctl.h> | |
42 | #include <sys/time.h> | |
172b3c27 | 43 | #include <signal.h> |
1f58c445 | 44 | #include <errno.h> |
2b7ff0d9 ST |
45 | #include <string.h> |
46 | #include <getopt.h> | |
47 | #include <unistd.h> | |
8fb810ff | 48 | #include <fcntl.h> |
8fb810ff SK |
49 | #include <limits.h> |
50 | #include <locale.h> | |
51 | #include <stddef.h> | |
3822032d KZ |
52 | #include <sys/wait.h> |
53 | #include <poll.h> | |
cc1a88fb SK |
54 | #include <sys/signalfd.h> |
55 | #include <assert.h> | |
c2f03da9 | 56 | #include <inttypes.h> |
2b7ff0d9 | 57 | |
cdd2a8c3 | 58 | #include "closestream.h" |
7eda085c | 59 | #include "nls.h" |
91239874 | 60 | #include "c.h" |
107293a6 | 61 | #include "cctype.h" |
3822032d | 62 | #include "ttyutils.h" |
c6fca22e | 63 | #include "all-io.h" |
04639805 | 64 | #include "monotonic.h" |
bdef362d | 65 | #include "timeutils.h" |
aefe9893 | 66 | #include "strutils.h" |
7268e79b | 67 | #include "strv.h" |
596f4202 | 68 | #include "xalloc.h" |
fc58044f | 69 | #include "optutils.h" |
fbed5507 | 70 | #include "signames.h" |
ec10634e | 71 | #include "pty-session.h" |
a2b4dec5 KZ |
72 | #include "debug.h" |
73 | ||
2ba641e5 | 74 | static UL_DEBUG_DEFINE_MASK(script); |
a2b4dec5 KZ |
75 | UL_DEBUG_DEFINE_MASKNAMES(script) = UL_DEBUG_EMPTY_MASKNAMES; |
76 | ||
77 | #define SCRIPT_DEBUG_INIT (1 << 1) | |
ec10634e KZ |
78 | #define SCRIPT_DEBUG_PTY (1 << 2) |
79 | #define SCRIPT_DEBUG_IO (1 << 3) | |
80 | #define SCRIPT_DEBUG_SIGNAL (1 << 4) | |
a2b4dec5 KZ |
81 | #define SCRIPT_DEBUG_MISC (1 << 5) |
82 | #define SCRIPT_DEBUG_ALL 0xFFFF | |
83 | ||
84 | #define DBG(m, x) __UL_DBG(script, SCRIPT_DEBUG_, m, x) | |
85 | #define ON_DBG(m, x) __UL_DBG_CALL(script, SCRIPT_DEBUG_, m, x) | |
86 | ||
f0bc3fa0 | 87 | #ifdef HAVE_LIBUTEMPTER |
e11a5e63 | 88 | # include <utempter.h> |
f0bc3fa0 KZ |
89 | #endif |
90 | ||
7e5796c9 | 91 | #define DEFAULT_TYPESCRIPT_FILENAME "typescript" |
05644dab | 92 | |
fec06a06 KZ |
93 | /* |
94 | * Script is driven by stream (stdout/stdin) activity. It's possible to | |
95 | * associate arbitrary number of log files with the stream. We have two basic | |
96 | * types of log files: "timing file" (simple or multistream) and "data file" | |
97 | * (raw). | |
98 | * | |
ac407b16 | 99 | * The same log file maybe be shared between both streams. For example |
fec06a06 KZ |
100 | * multi-stream timing file is possible to use for stdin as well as for stdout. |
101 | */ | |
596f4202 KZ |
102 | enum { |
103 | SCRIPT_FMT_RAW = 1, /* raw slave/master data */ | |
daee4d95 KZ |
104 | SCRIPT_FMT_TIMING_SIMPLE, /* (classic) in format "<delta> <offset>" */ |
105 | SCRIPT_FMT_TIMING_MULTI, /* (advanced) multiple streams in format "<type> <delta> <offset|etc> */ | |
596f4202 KZ |
106 | }; |
107 | ||
108 | struct script_log { | |
109 | FILE *fp; /* file pointer (handler) */ | |
110 | int format; /* SCRIPT_FMT_* */ | |
111 | char *filename; /* on command line specified name */ | |
88025c74 | 112 | struct timeval oldtime; /* previous entry log time (SCRIPT_FMT_TIMING_* only) */ |
70c2cd9a | 113 | struct timeval starttime; |
c1c2ee0b KZ |
114 | |
115 | unsigned int initialized : 1; | |
596f4202 KZ |
116 | }; |
117 | ||
118 | struct script_stream { | |
9f822648 | 119 | struct script_log **logs; /* logs where to write data from stream */ |
596f4202 | 120 | size_t nlogs; /* number of logs */ |
c1c2ee0b | 121 | char ident; /* stream identifier */ |
596f4202 KZ |
122 | }; |
123 | ||
edc7e420 | 124 | struct script_control { |
596f4202 KZ |
125 | uint64_t outsz; /* current output files size */ |
126 | uint64_t maxsz; /* maximum output files size */ | |
127 | ||
596f4202 KZ |
128 | struct script_stream out; /* output */ |
129 | struct script_stream in; /* input */ | |
130 | ||
3cecd176 KZ |
131 | struct script_log *siglog; /* log for signal entries */ |
132 | struct script_log *infolog; /* log for info entries */ | |
3cecd176 KZ |
133 | |
134 | const char *ttyname; | |
135 | const char *ttytype; | |
4d8edee5 | 136 | const char *command; |
bfcdfccf | 137 | char *command_norm; /* normalized (without \n) */ |
3cecd176 KZ |
138 | int ttycols; |
139 | int ttylines; | |
fbed5507 | 140 | |
bdd43357 | 141 | struct ul_pty *pty; /* pseudo-terminal */ |
edc7e420 | 142 | pid_t child; /* child pid */ |
edc7e420 | 143 | int childstatus; /* child process exit value */ |
ec10634e | 144 | |
63d79371 ZJS |
145 | bool append, /* append output */ |
146 | rc_wanted, /* return child exit value */ | |
147 | flush, /* flush after each write */ | |
148 | quiet, /* suppress most output */ | |
149 | force, /* write output to links */ | |
150 | isterm; /* is child process running as terminal */ | |
edc7e420 | 151 | }; |
1f58c445 | 152 | |
94e4a414 KZ |
153 | static ssize_t log_info(struct script_control *ctl, const char *name, const char *msgfmt, ...) |
154 | __attribute__((__format__ (__printf__, 3, 4))); | |
596f4202 | 155 | |
a2b4dec5 KZ |
156 | static void script_init_debug(void) |
157 | { | |
a15dca2f | 158 | __UL_INIT_DEBUG_FROM_ENV(script, SCRIPT_DEBUG_, 0, SCRIPT_DEBUG); |
a2b4dec5 KZ |
159 | } |
160 | ||
3cecd176 KZ |
161 | static void init_terminal_info(struct script_control *ctl) |
162 | { | |
163 | if (ctl->ttyname || !ctl->isterm) | |
164 | return; /* already initialized */ | |
165 | ||
166 | get_terminal_dimension(&ctl->ttycols, &ctl->ttylines); | |
167 | get_terminal_name(&ctl->ttyname, NULL, NULL); | |
168 | get_terminal_type(&ctl->ttytype); | |
169 | } | |
170 | ||
6a40c65f SK |
171 | /* |
172 | * For tests we want to be able to control time output | |
173 | */ | |
174 | #ifdef TEST_SCRIPT | |
175 | static inline time_t script_time(time_t *t) | |
176 | { | |
177 | const char *str = getenv("SCRIPT_TEST_SECOND_SINCE_EPOCH"); | |
345208a5 | 178 | int64_t sec; |
6a40c65f | 179 | |
c2f03da9 | 180 | if (!str || sscanf(str, "%"SCNi64, &sec) != 1) |
345208a5 ID |
181 | return time(t); |
182 | if (t) | |
183 | *t = (time_t)sec; | |
184 | return (time_t)sec; | |
6a40c65f SK |
185 | } |
186 | #else /* !TEST_SCRIPT */ | |
187 | # define script_time(x) time(x) | |
188 | #endif | |
189 | ||
86be6a32 | 190 | static void __attribute__((__noreturn__)) usage(void) |
3ff52639 | 191 | { |
86be6a32 | 192 | FILE *out = stdout; |
db433bf7 | 193 | fputs(USAGE_HEADER, out); |
3d997c94 | 194 | fprintf(out, _(" %s [options] [<file>] [-- <command> [<argument>...]]\n"), program_invocation_short_name); |
87d6050b | 195 | |
451dbcfa BS |
196 | fputs(USAGE_SEPARATOR, out); |
197 | fputs(_("Make a typescript of a terminal session.\n"), out); | |
198 | ||
db433bf7 | 199 | fputs(USAGE_OPTIONS, out); |
70062aad | 200 | fputs(_(" -I, --log-in <file> log stdin to file\n"), out); |
ddbdb792 | 201 | fputs(_(" -O, --log-out <file> log stdout to file (default)\n"), out); |
c1c2ee0b | 202 | fputs(_(" -B, --log-io <file> log stdin and stdout to file\n"), out); |
65dc24ab KZ |
203 | fputs(USAGE_SEPARATOR, out); |
204 | ||
fc58044f | 205 | fputs(_(" -T, --log-timing <file> log timing information to file\n"), out); |
65dc24ab | 206 | fputs(_(" -t[<file>], --timing[=<file>] deprecated alias to -T (default file is stderr)\n"), out); |
daee4d95 | 207 | fputs(_(" -m, --logging-format <name> force to 'classic' or 'advanced' format\n"), out); |
65dc24ab KZ |
208 | fputs(USAGE_SEPARATOR, out); |
209 | ||
210 | fputs(_(" -a, --append append to the log file\n"), out); | |
3d997c94 BS |
211 | fputs(_(" -c, --command <command> run <command> rather than an interactive shell\n" |
212 | " (alternative to '-- <command> [<argument...>]')\n"), out); | |
c64963f8 KZ |
213 | fputs(_(" -e, --return return exit code of the child process\n"), out); |
214 | fputs(_(" -f, --flush run flush after each write\n"), out); | |
215 | fputs(_(" --force use output file even when it is a link\n"), out); | |
75ccd75a | 216 | fputs(_(" -E, --echo <when> echo input in session (auto, always or never)\n"), out); |
c64963f8 KZ |
217 | fputs(_(" -o, --output-limit <size> terminate if output files exceed size\n"), out); |
218 | fputs(_(" -q, --quiet be quiet\n"), out); | |
3ff52639 | 219 | |
c64963f8 | 220 | fputs(USAGE_SEPARATOR, out); |
bad4c729 | 221 | fprintf(out, USAGE_HELP_OPTIONS(31)); |
89232ab3 BS |
222 | |
223 | fputs(USAGE_ARGUMENTS, out); | |
224 | fprintf(out, USAGE_ARG_SIZE(_("<size>"))); | |
225 | ||
bad4c729 | 226 | fprintf(out, USAGE_MAN_TAIL("script(1)")); |
c64963f8 | 227 | |
86be6a32 | 228 | exit(EXIT_SUCCESS); |
3ff52639 SK |
229 | } |
230 | ||
596f4202 KZ |
231 | static struct script_log *get_log_by_name(struct script_stream *stream, |
232 | const char *name) | |
4d9b788d | 233 | { |
596f4202 | 234 | size_t i; |
4d9b788d | 235 | |
596f4202 | 236 | for (i = 0; i < stream->nlogs; i++) { |
9f822648 KZ |
237 | struct script_log *log = stream->logs[i]; |
238 | if (strcmp(log->filename, name) == 0) | |
596f4202 KZ |
239 | return log; |
240 | } | |
241 | return NULL; | |
242 | } | |
4d9b788d | 243 | |
596f4202 KZ |
244 | static struct script_log *log_associate(struct script_control *ctl, |
245 | struct script_stream *stream, | |
246 | const char *filename, int format) | |
247 | { | |
248 | struct script_log *log; | |
249 | ||
c1c2ee0b KZ |
250 | DBG(MISC, ul_debug("associate %s with stream", filename)); |
251 | ||
9f822648 KZ |
252 | assert(ctl); |
253 | assert(filename); | |
254 | assert(stream); | |
255 | ||
256 | log = get_log_by_name(stream, filename); | |
257 | if (log) | |
258 | return log; /* already defined */ | |
259 | ||
260 | log = get_log_by_name(stream == &ctl->out ? &ctl->in : &ctl->out, filename); | |
596f4202 KZ |
261 | if (!log) { |
262 | /* create a new log */ | |
9f822648 KZ |
263 | log = xcalloc(1, sizeof(*log)); |
264 | log->filename = xstrdup(filename); | |
596f4202 KZ |
265 | log->format = format; |
266 | } | |
4d9b788d | 267 | |
9f822648 | 268 | /* add log to the stream */ |
64d6d400 TW |
269 | stream->logs = xreallocarray(stream->logs, |
270 | stream->nlogs + 1, sizeof(log)); | |
9f822648 KZ |
271 | stream->logs[stream->nlogs] = log; |
272 | stream->nlogs++; | |
273 | ||
fbed5507 | 274 | /* remember where to write info about signals */ |
3cecd176 KZ |
275 | if (format == SCRIPT_FMT_TIMING_MULTI) { |
276 | if (!ctl->siglog) | |
277 | ctl->siglog = log; | |
278 | if (!ctl->infolog) | |
279 | ctl->infolog = log; | |
3cecd176 | 280 | } |
fbed5507 | 281 | |
596f4202 KZ |
282 | return log; |
283 | } | |
4d9b788d | 284 | |
ec10634e | 285 | static int log_close(struct script_control *ctl, |
596f4202 KZ |
286 | struct script_log *log, |
287 | const char *msg, | |
288 | int status) | |
289 | { | |
ec10634e KZ |
290 | int rc = 0; |
291 | ||
9e9b3d65 | 292 | if (!log || !log->initialized) |
ec10634e | 293 | return 0; |
c1c2ee0b | 294 | |
596f4202 | 295 | DBG(MISC, ul_debug("closing %s", log->filename)); |
4d9b788d | 296 | |
596f4202 KZ |
297 | switch (log->format) { |
298 | case SCRIPT_FMT_RAW: | |
299 | { | |
300 | char buf[FORMAT_TIMESTAMP_MAX]; | |
301 | time_t tvec = script_time((time_t *)NULL); | |
4d9b788d | 302 | |
596f4202 KZ |
303 | strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf)); |
304 | if (msg) | |
305 | fprintf(log->fp, _("\nScript done on %s [<%s>]\n"), buf, msg); | |
306 | else | |
307 | fprintf(log->fp, _("\nScript done on %s [COMMAND_EXIT_CODE=\"%d\"]\n"), buf, status); | |
596f4202 KZ |
308 | break; |
309 | } | |
70c2cd9a KZ |
310 | case SCRIPT_FMT_TIMING_MULTI: |
311 | { | |
117cbf91 | 312 | struct timeval now = { 0 }, delta = { 0 }; |
70c2cd9a KZ |
313 | |
314 | gettime_monotonic(&now); | |
315 | timersub(&now, &log->starttime, &delta); | |
316 | ||
9406284a KZ |
317 | log_info(ctl, "DURATION", "%"PRId64".%06"PRId64, |
318 | (int64_t)delta.tv_sec, | |
319 | (int64_t)delta.tv_usec); | |
70c2cd9a KZ |
320 | log_info(ctl, "EXIT_CODE", "%d", status); |
321 | break; | |
322 | } | |
596f4202 KZ |
323 | case SCRIPT_FMT_TIMING_SIMPLE: |
324 | break; | |
325 | } | |
326 | ||
ec10634e KZ |
327 | if (close_stream(log->fp) != 0) { |
328 | warn(_("write failed: %s"), log->filename); | |
329 | rc = -errno; | |
330 | } | |
4d9b788d | 331 | |
9e9b3d65 KZ |
332 | free(log->filename); |
333 | memset(log, 0, sizeof(*log)); | |
ec10634e KZ |
334 | |
335 | return rc; | |
4d9b788d KZ |
336 | } |
337 | ||
7727be1a KZ |
338 | static int log_flush(struct script_control *ctl __attribute__((__unused__)), struct script_log *log) |
339 | { | |
340 | ||
341 | if (!log || !log->initialized) | |
342 | return 0; | |
343 | ||
344 | DBG(MISC, ul_debug("flushing %s", log->filename)); | |
345 | ||
346 | fflush(log->fp); | |
347 | return 0; | |
348 | } | |
349 | ||
9e9b3d65 KZ |
350 | static void log_free(struct script_control *ctl, struct script_log *log) |
351 | { | |
352 | size_t i; | |
353 | ||
354 | if (!log) | |
355 | return; | |
356 | ||
357 | /* the same log is possible to reference from more places, remove all | |
358 | * (TODO: maybe use include/list.h to make it more elegant) | |
359 | */ | |
360 | if (ctl->siglog == log) | |
361 | ctl->siglog = NULL; | |
362 | else if (ctl->infolog == log) | |
363 | ctl->infolog = NULL; | |
364 | ||
365 | for (i = 0; i < ctl->out.nlogs; i++) { | |
366 | if (ctl->out.logs[i] == log) | |
367 | ctl->out.logs[i] = NULL; | |
368 | } | |
369 | for (i = 0; i < ctl->in.nlogs; i++) { | |
370 | if (ctl->in.logs[i] == log) | |
371 | ctl->in.logs[i] = NULL; | |
372 | } | |
373 | free(log); | |
374 | } | |
375 | ||
ec10634e | 376 | static int log_start(struct script_control *ctl, |
596f4202 | 377 | struct script_log *log) |
6343ee8c | 378 | { |
c1c2ee0b | 379 | if (log->initialized) |
ec10634e | 380 | return 0; |
6343ee8c | 381 | |
596f4202 | 382 | DBG(MISC, ul_debug("opening %s", log->filename)); |
6343ee8c | 383 | |
c1c2ee0b KZ |
384 | assert(log->fp == NULL); |
385 | ||
596f4202 KZ |
386 | /* open the log */ |
387 | log->fp = fopen(log->filename, | |
388 | ctl->append && log->format == SCRIPT_FMT_RAW ? | |
c1c2ee0b KZ |
389 | "a" UL_CLOEXECSTR : |
390 | "w" UL_CLOEXECSTR); | |
596f4202 | 391 | if (!log->fp) { |
596f4202 | 392 | warn(_("cannot open %s"), log->filename); |
ec10634e | 393 | return -errno; |
596f4202 | 394 | } |
6343ee8c | 395 | |
596f4202 KZ |
396 | /* write header, etc. */ |
397 | switch (log->format) { | |
398 | case SCRIPT_FMT_RAW: | |
399 | { | |
4d8edee5 | 400 | int x = 0; |
596f4202 KZ |
401 | char buf[FORMAT_TIMESTAMP_MAX]; |
402 | time_t tvec = script_time((time_t *)NULL); | |
403 | ||
404 | strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf)); | |
405 | fprintf(log->fp, _("Script started on %s ["), buf); | |
406 | ||
4d8edee5 | 407 | if (ctl->command) |
bfcdfccf | 408 | x += fprintf(log->fp, "COMMAND=\"%s\"", ctl->command_norm); |
4d8edee5 | 409 | |
596f4202 | 410 | if (ctl->isterm) { |
3cecd176 | 411 | init_terminal_info(ctl); |
596f4202 | 412 | |
3cecd176 | 413 | if (ctl->ttytype) |
4d8edee5 | 414 | x += fprintf(log->fp, "%*sTERM=\"%s\"", !!x, "", ctl->ttytype); |
3cecd176 | 415 | if (ctl->ttyname) |
4d8edee5 | 416 | x += fprintf(log->fp, "%*sTTY=\"%s\"", !!x, "", ctl->ttyname); |
596f4202 | 417 | |
4eda4ec8 | 418 | x += fprintf(log->fp, "%*sCOLUMNS=\"%d\" LINES=\"%d\"", !!x, "", |
4d8edee5 | 419 | ctl->ttycols, ctl->ttylines); |
596f4202 | 420 | } else |
4eda4ec8 | 421 | fprintf(log->fp, _("%*s<not executed on terminal>"), !!x, ""); |
596f4202 KZ |
422 | |
423 | fputs("]\n", log->fp); | |
424 | break; | |
425 | } | |
426 | case SCRIPT_FMT_TIMING_SIMPLE: | |
36b1369b | 427 | case SCRIPT_FMT_TIMING_MULTI: |
596f4202 | 428 | gettime_monotonic(&log->oldtime); |
70c2cd9a | 429 | gettime_monotonic(&log->starttime); |
596f4202 KZ |
430 | break; |
431 | } | |
c1c2ee0b KZ |
432 | |
433 | log->initialized = 1; | |
ec10634e | 434 | return 0; |
596f4202 KZ |
435 | } |
436 | ||
9e9b3d65 | 437 | static int logging_start(struct script_control *ctl) |
fd42a45b KZ |
438 | { |
439 | size_t i; | |
440 | ||
441 | /* start all output logs */ | |
ec10634e KZ |
442 | for (i = 0; i < ctl->out.nlogs; i++) { |
443 | int rc = log_start(ctl, ctl->out.logs[i]); | |
444 | if (rc) | |
445 | return rc; | |
446 | } | |
fd42a45b KZ |
447 | |
448 | /* start all input logs */ | |
ec10634e KZ |
449 | for (i = 0; i < ctl->in.nlogs; i++) { |
450 | int rc = log_start(ctl, ctl->in.logs[i]); | |
451 | if (rc) | |
452 | return rc; | |
453 | } | |
454 | return 0; | |
fd42a45b KZ |
455 | } |
456 | ||
ec10634e | 457 | static ssize_t log_write(struct script_control *ctl, |
c1c2ee0b | 458 | struct script_stream *stream, |
596f4202 KZ |
459 | struct script_log *log, |
460 | char *obuf, size_t bytes) | |
461 | { | |
ec10634e KZ |
462 | int rc; |
463 | ssize_t ssz = 0; | |
464 | struct timeval now, delta; | |
465 | ||
596f4202 KZ |
466 | if (!log->fp) |
467 | return 0; | |
468 | ||
ec10634e | 469 | DBG(IO, ul_debug(" writing [file=%s]", log->filename)); |
596f4202 KZ |
470 | |
471 | switch (log->format) { | |
472 | case SCRIPT_FMT_RAW: | |
ec10634e KZ |
473 | DBG(IO, ul_debug(" log raw data")); |
474 | rc = fwrite_all(obuf, 1, bytes, log->fp); | |
475 | if (rc) { | |
596f4202 | 476 | warn(_("cannot write %s"), log->filename); |
ec10634e | 477 | return rc; |
596f4202 | 478 | } |
ec10634e | 479 | ssz = bytes; |
596f4202 | 480 | break; |
596f4202 | 481 | |
ec10634e KZ |
482 | case SCRIPT_FMT_TIMING_SIMPLE: |
483 | DBG(IO, ul_debug(" log timing info")); | |
596f4202 KZ |
484 | |
485 | gettime_monotonic(&now); | |
486 | timersub(&now, &log->oldtime, &delta); | |
9406284a KZ |
487 | ssz = fprintf(log->fp, "%"PRId64".%06"PRId64" %zd\n", |
488 | (int64_t)delta.tv_sec, (int64_t)delta.tv_usec, bytes); | |
ec10634e KZ |
489 | if (ssz < 0) |
490 | return -errno; | |
491 | ||
596f4202 | 492 | log->oldtime = now; |
596f4202 | 493 | break; |
c1c2ee0b | 494 | |
ec10634e KZ |
495 | case SCRIPT_FMT_TIMING_MULTI: |
496 | DBG(IO, ul_debug(" log multi-stream timing info")); | |
c1c2ee0b KZ |
497 | |
498 | gettime_monotonic(&now); | |
499 | timersub(&now, &log->oldtime, &delta); | |
9406284a | 500 | ssz = fprintf(log->fp, "%c %"PRId64".%06"PRId64" %zd\n", |
c1c2ee0b | 501 | stream->ident, |
9406284a | 502 | (int64_t)delta.tv_sec, (int64_t)delta.tv_usec, bytes); |
ec10634e KZ |
503 | if (ssz < 0) |
504 | return -errno; | |
505 | ||
c1c2ee0b | 506 | log->oldtime = now; |
ec10634e | 507 | break; |
596f4202 KZ |
508 | default: |
509 | break; | |
510 | } | |
511 | ||
512 | if (ctl->flush) | |
513 | fflush(log->fp); | |
ec10634e | 514 | return ssz; |
6343ee8c KZ |
515 | } |
516 | ||
ec10634e | 517 | static ssize_t log_stream_activity( |
596f4202 KZ |
518 | struct script_control *ctl, |
519 | struct script_stream *stream, | |
520 | char *buf, size_t bytes) | |
521 | { | |
522 | size_t i; | |
ec10634e | 523 | ssize_t outsz = 0; |
596f4202 | 524 | |
ec10634e KZ |
525 | for (i = 0; i < stream->nlogs; i++) { |
526 | ssize_t ssz = log_write(ctl, stream, stream->logs[i], buf, bytes); | |
527 | ||
528 | if (ssz < 0) | |
529 | return ssz; | |
530 | outsz += ssz; | |
531 | } | |
596f4202 KZ |
532 | |
533 | return outsz; | |
534 | } | |
535 | ||
94e4a414 KZ |
536 | static ssize_t __attribute__ ((__format__ (__printf__, 3, 4))) |
537 | log_signal(struct script_control *ctl, int signum, const char *msgfmt, ...) | |
fbed5507 KZ |
538 | { |
539 | struct script_log *log; | |
540 | struct timeval now, delta; | |
541 | char msg[BUFSIZ] = {0}; | |
542 | va_list ap; | |
ec10634e | 543 | ssize_t sz; |
fbed5507 KZ |
544 | |
545 | assert(ctl); | |
546 | ||
547 | log = ctl->siglog; | |
548 | if (!log) | |
549 | return 0; | |
550 | ||
551 | assert(log->format == SCRIPT_FMT_TIMING_MULTI); | |
fbed5507 KZ |
552 | DBG(IO, ul_debug(" writing signal to multi-stream timing")); |
553 | ||
554 | gettime_monotonic(&now); | |
555 | timersub(&now, &log->oldtime, &delta); | |
556 | ||
557 | if (msgfmt) { | |
558 | int rc; | |
559 | va_start(ap, msgfmt); | |
560 | rc = vsnprintf(msg, sizeof(msg), msgfmt, ap); | |
561 | va_end(ap); | |
562 | if (rc < 0) | |
563 | *msg = '\0';; | |
564 | } | |
565 | ||
566 | if (*msg) | |
9406284a KZ |
567 | sz = fprintf(log->fp, "S %"PRId64".%06"PRId64" SIG%s %s\n", |
568 | (int64_t)delta.tv_sec, (int64_t)delta.tv_usec, | |
fbed5507 KZ |
569 | signum_to_signame(signum), msg); |
570 | else | |
9406284a KZ |
571 | sz = fprintf(log->fp, "S %"PRId64".%06"PRId64" SIG%s\n", |
572 | (int64_t)delta.tv_sec, (int64_t)delta.tv_usec, | |
fbed5507 KZ |
573 | signum_to_signame(signum)); |
574 | ||
575 | log->oldtime = now; | |
ec10634e | 576 | return sz; |
fbed5507 | 577 | } |
596f4202 | 578 | |
ec10634e | 579 | static ssize_t log_info(struct script_control *ctl, const char *name, const char *msgfmt, ...) |
3cecd176 KZ |
580 | { |
581 | struct script_log *log; | |
582 | char msg[BUFSIZ] = {0}; | |
583 | va_list ap; | |
ec10634e | 584 | ssize_t sz; |
3cecd176 KZ |
585 | |
586 | assert(ctl); | |
587 | ||
588 | log = ctl->infolog; | |
589 | if (!log) | |
590 | return 0; | |
591 | ||
592 | assert(log->format == SCRIPT_FMT_TIMING_MULTI); | |
593 | DBG(IO, ul_debug(" writing info to multi-stream log")); | |
594 | ||
595 | if (msgfmt) { | |
596 | int rc; | |
597 | va_start(ap, msgfmt); | |
598 | rc = vsnprintf(msg, sizeof(msg), msgfmt, ap); | |
599 | va_end(ap); | |
600 | if (rc < 0) | |
601 | *msg = '\0';; | |
602 | } | |
603 | ||
604 | if (*msg) | |
a1cc831b | 605 | sz = fprintf(log->fp, "H %f %s %s\n", 0.0, name, msg); |
3cecd176 | 606 | else |
a1cc831b | 607 | sz = fprintf(log->fp, "H %f %s\n", 0.0, name); |
3cecd176 | 608 | |
ec10634e | 609 | return sz; |
8198052c SK |
610 | } |
611 | ||
7fb65db1 | 612 | |
9e9b3d65 | 613 | static void logging_done(struct script_control *ctl, const char *msg) |
93af8d8b | 614 | { |
596f4202 KZ |
615 | int status; |
616 | size_t i; | |
6343ee8c | 617 | |
ec10634e | 618 | DBG(MISC, ul_debug("stop logging")); |
8198052c | 619 | |
6343ee8c | 620 | if (WIFSIGNALED(ctl->childstatus)) |
596f4202 | 621 | status = WTERMSIG(ctl->childstatus) + 0x80; |
6343ee8c | 622 | else |
596f4202 KZ |
623 | status = WEXITSTATUS(ctl->childstatus); |
624 | ||
596f4202 KZ |
625 | DBG(MISC, ul_debug(" status=%d", status)); |
626 | ||
627 | /* close all output logs */ | |
9e9b3d65 KZ |
628 | for (i = 0; i < ctl->out.nlogs; i++) { |
629 | struct script_log *log = ctl->out.logs[i]; | |
630 | log_close(ctl, log, msg, status); | |
631 | log_free(ctl, log); | |
632 | } | |
633 | free(ctl->out.logs); | |
634 | ctl->out.logs = NULL; | |
635 | ctl->out.nlogs = 0; | |
596f4202 KZ |
636 | |
637 | /* close all input logs */ | |
9e9b3d65 KZ |
638 | for (i = 0; i < ctl->in.nlogs; i++) { |
639 | struct script_log *log = ctl->in.logs[i]; | |
640 | log_close(ctl, log, msg, status); | |
641 | log_free(ctl, log); | |
642 | } | |
643 | free(ctl->in.logs); | |
644 | ctl->in.logs = NULL; | |
645 | ctl->in.nlogs = 0; | |
93af8d8b | 646 | } |
5c36a0eb | 647 | |
bdd43357 KZ |
648 | static void callback_child_die( |
649 | void *data, | |
650 | pid_t child __attribute__((__unused__)), | |
651 | int status) | |
93af8d8b | 652 | { |
ec10634e | 653 | struct script_control *ctl = (struct script_control *) data; |
93af8d8b | 654 | |
bdd43357 KZ |
655 | ctl->child = (pid_t) -1; |
656 | ctl->childstatus = status; | |
54c6611d KZ |
657 | } |
658 | ||
bdd43357 KZ |
659 | static void callback_child_sigstop( |
660 | void *data __attribute__((__unused__)), | |
661 | pid_t child) | |
54c6611d | 662 | { |
ec10634e KZ |
663 | DBG(SIGNAL, ul_debug(" child stop by SIGSTOP -- stop parent too")); |
664 | kill(getpid(), SIGSTOP); | |
665 | DBG(SIGNAL, ul_debug(" resume")); | |
bdd43357 | 666 | kill(child, SIGCONT); |
54c6611d KZ |
667 | } |
668 | ||
ec10634e | 669 | static int callback_log_stream_activity(void *data, int fd, char *buf, size_t bufsz) |
54c6611d | 670 | { |
ec10634e KZ |
671 | struct script_control *ctl = (struct script_control *) data; |
672 | ssize_t ssz = 0; | |
54c6611d | 673 | |
ec10634e | 674 | DBG(IO, ul_debug("stream activity callback")); |
b2bff066 | 675 | |
ec10634e KZ |
676 | /* from stdin (user) to command */ |
677 | if (fd == STDIN_FILENO) | |
678 | ssz = log_stream_activity(ctl, &ctl->in, buf, (size_t) bufsz); | |
364cda48 | 679 | |
ec10634e KZ |
680 | /* from command (master) to stdout and log */ |
681 | else if (fd == ul_pty_get_childfd(ctl->pty)) | |
682 | ssz = log_stream_activity(ctl, &ctl->out, buf, (size_t) bufsz); | |
da26af4e | 683 | |
ec10634e KZ |
684 | if (ssz < 0) |
685 | return (int) ssz; | |
54c6611d | 686 | |
710e8ecb | 687 | DBG(IO, ul_debug(" append %zd bytes [summary=%" PRIu64 ", max=%" PRIu64 "]", ssz, |
ec10634e | 688 | ctl->outsz, ctl->maxsz)); |
a2b4dec5 | 689 | |
ec10634e | 690 | ctl->outsz += ssz; |
da26af4e | 691 | |
596f4202 KZ |
692 | /* check output limit */ |
693 | if (ctl->maxsz != 0 && ctl->outsz >= ctl->maxsz) { | |
694 | if (!ctl->quiet) | |
695 | printf(_("Script terminated, max output files size %"PRIu64" exceeded.\n"), ctl->maxsz); | |
696 | DBG(IO, ul_debug("output size %"PRIu64", exceeded limit %"PRIu64, ctl->outsz, ctl->maxsz)); | |
9e9b3d65 | 697 | logging_done(ctl, _("max output size exceeded")); |
ec10634e | 698 | return 1; |
a2b4dec5 | 699 | } |
ec10634e | 700 | return 0; |
a8896ad5 SK |
701 | } |
702 | ||
ec10634e | 703 | static int callback_log_signal(void *data, struct signalfd_siginfo *info, void *sigdata) |
a8896ad5 | 704 | { |
ec10634e KZ |
705 | struct script_control *ctl = (struct script_control *) data; |
706 | ssize_t ssz = 0; | |
a8896ad5 | 707 | |
ec10634e | 708 | switch (info->ssi_signo) { |
a8896ad5 | 709 | case SIGWINCH: |
ec10634e KZ |
710 | { |
711 | struct winsize *win = (struct winsize *) sigdata; | |
712 | ssz = log_signal(ctl, info->ssi_signo, "ROWS=%d COLS=%d", | |
713 | win->ws_row, win->ws_col); | |
a8896ad5 | 714 | break; |
ec10634e | 715 | } |
5860c45e | 716 | case SIGTERM: |
b77025b6 | 717 | FALLTHROUGH; |
5860c45e | 718 | case SIGINT: |
b77025b6 | 719 | FALLTHROUGH; |
5860c45e | 720 | case SIGQUIT: |
ec10634e KZ |
721 | ssz = log_signal(ctl, info->ssi_signo, NULL); |
722 | break; | |
a8896ad5 | 723 | default: |
ec10634e KZ |
724 | /* no log */ |
725 | break; | |
93af8d8b | 726 | } |
a17f3264 | 727 | |
ec10634e | 728 | return ssz < 0 ? ssz : 0; |
6dbe3af9 KZ |
729 | } |
730 | ||
7727be1a KZ |
731 | static int callback_flush_logs(void *data) |
732 | { | |
733 | struct script_control *ctl = (struct script_control *) data; | |
734 | size_t i; | |
735 | ||
736 | for (i = 0; i < ctl->out.nlogs; i++) { | |
737 | int rc = log_flush(ctl, ctl->out.logs[i]); | |
738 | if (rc) | |
739 | return rc; | |
740 | } | |
741 | ||
742 | for (i = 0; i < ctl->in.nlogs; i++) { | |
743 | int rc = log_flush(ctl, ctl->in.logs[i]); | |
744 | if (rc) | |
745 | return rc; | |
746 | } | |
747 | return 0; | |
748 | } | |
749 | ||
ec10634e | 750 | static void die_if_link(struct script_control *ctl, const char *filename) |
93af8d8b | 751 | { |
ec10634e | 752 | struct stat s; |
54c6611d | 753 | |
ec10634e KZ |
754 | if (ctl->force) |
755 | return; | |
756 | if (lstat(filename, &s) == 0 && (S_ISLNK(s.st_mode) || s.st_nlink > 1)) | |
757 | errx(EXIT_FAILURE, | |
758 | _("output file `%s' is a link\n" | |
759 | "Use --force if you really want to use it.\n" | |
760 | "Program not started."), filename); | |
6dbe3af9 KZ |
761 | } |
762 | ||
93af8d8b SK |
763 | int main(int argc, char **argv) |
764 | { | |
edc7e420 | 765 | struct script_control ctl = { |
c1c2ee0b KZ |
766 | .out = { .ident = 'O' }, |
767 | .in = { .ident = 'I' }, | |
edc7e420 | 768 | }; |
ec10634e | 769 | struct ul_pty_callbacks *cb; |
75ccd75a | 770 | int ch, format = 0, caught_signal = 0, rc = 0, echo = 1; |
70062aad | 771 | const char *outfile = NULL, *infile = NULL; |
4d8edee5 | 772 | const char *timingfile = NULL, *shell = NULL; |
93af8d8b SK |
773 | |
774 | enum { FORCE_OPTION = CHAR_MAX + 1 }; | |
775 | ||
776 | static const struct option longopts[] = { | |
edc7e420 SK |
777 | {"append", no_argument, NULL, 'a'}, |
778 | {"command", required_argument, NULL, 'c'}, | |
1eee1acb | 779 | {"echo", required_argument, NULL, 'E'}, |
edc7e420 SK |
780 | {"return", no_argument, NULL, 'e'}, |
781 | {"flush", no_argument, NULL, 'f'}, | |
782 | {"force", no_argument, NULL, FORCE_OPTION,}, | |
70062aad | 783 | {"log-in", required_argument, NULL, 'I'}, |
ddbdb792 | 784 | {"log-out", required_argument, NULL, 'O'}, |
c1c2ee0b | 785 | {"log-io", required_argument, NULL, 'B'}, |
fc58044f | 786 | {"log-timing", required_argument, NULL, 'T'}, |
daee4d95 | 787 | {"logging-format", required_argument, NULL, 'm'}, |
aefe9893 | 788 | {"output-limit", required_argument, NULL, 'o'}, |
edc7e420 SK |
789 | {"quiet", no_argument, NULL, 'q'}, |
790 | {"timing", optional_argument, NULL, 't'}, | |
791 | {"version", no_argument, NULL, 'V'}, | |
792 | {"help", no_argument, NULL, 'h'}, | |
793 | {NULL, 0, NULL, 0} | |
93af8d8b | 794 | }; |
fc58044f KZ |
795 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
796 | { 'T', 't' }, | |
797 | { 0 } | |
798 | }; | |
799 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
93af8d8b | 800 | setlocale(LC_ALL, ""); |
b09feab9 SK |
801 | /* |
802 | * script -t prints time delays as floating point numbers. The example | |
803 | * program (scriptreplay) that we provide to handle this timing output | |
804 | * is a perl script, and does not handle numbers in locale format (not | |
805 | * even when "use locale;" is added). So, since these numbers are not | |
806 | * for human consumption, it seems easiest to set LC_NUMERIC here. | |
807 | */ | |
808 | setlocale(LC_NUMERIC, "C"); | |
93af8d8b SK |
809 | bindtextdomain(PACKAGE, LOCALEDIR); |
810 | textdomain(PACKAGE); | |
2c308875 | 811 | close_stdout_atexit(); |
93af8d8b | 812 | |
a2b4dec5 | 813 | script_init_debug(); |
ec10634e | 814 | ON_DBG(PTY, ul_pty_init_debug(0xFFFF)); |
a2b4dec5 | 815 | |
1eee1acb | 816 | ctl.isterm = isatty(STDIN_FILENO); |
1eee1acb | 817 | |
7268e79b | 818 | while ((ch = getopt_long(argc, argv, "+aB:c:eE:fI:O:o:qm:T:t::Vh", longopts, NULL)) != -1) { |
fc58044f KZ |
819 | |
820 | err_exclusive_options(ch, longopts, excl, excl_st); | |
821 | ||
edc7e420 | 822 | switch (ch) { |
93af8d8b | 823 | case 'a': |
3f19b85f | 824 | ctl.append = 1; |
93af8d8b SK |
825 | break; |
826 | case 'c': | |
4d8edee5 | 827 | ctl.command = optarg; |
bfcdfccf | 828 | ctl.command_norm = xstrdup(ctl.command); |
7bb1151e | 829 | ul_strrep(ctl.command_norm, '\n', ' '); |
93af8d8b | 830 | break; |
1eee1acb KZ |
831 | case 'E': |
832 | if (strcmp(optarg, "auto") == 0) | |
833 | ; /* keep default */ | |
834 | else if (strcmp(optarg, "never") == 0) | |
835 | echo = 0; | |
836 | else if (strcmp(optarg, "always") == 0) | |
837 | echo = 1; | |
838 | else | |
c722cb59 | 839 | errx(EXIT_FAILURE, _("unsupported echo mode: '%s'"), optarg); |
1eee1acb | 840 | break; |
93af8d8b | 841 | case 'e': |
7e5796c9 | 842 | ctl.rc_wanted = 1; |
93af8d8b SK |
843 | break; |
844 | case 'f': | |
3f19b85f | 845 | ctl.flush = 1; |
93af8d8b SK |
846 | break; |
847 | case FORCE_OPTION: | |
3f19b85f | 848 | ctl.force = 1; |
93af8d8b | 849 | break; |
c1c2ee0b KZ |
850 | case 'B': |
851 | log_associate(&ctl, &ctl.in, optarg, SCRIPT_FMT_RAW); | |
852 | log_associate(&ctl, &ctl.out, optarg, SCRIPT_FMT_RAW); | |
853 | infile = outfile = optarg; | |
854 | break; | |
70062aad KZ |
855 | case 'I': |
856 | log_associate(&ctl, &ctl.in, optarg, SCRIPT_FMT_RAW); | |
857 | infile = optarg; | |
858 | break; | |
ddbdb792 | 859 | case 'O': |
70062aad KZ |
860 | log_associate(&ctl, &ctl.out, optarg, SCRIPT_FMT_RAW); |
861 | outfile = optarg; | |
ddbdb792 | 862 | break; |
aefe9893 FM |
863 | case 'o': |
864 | ctl.maxsz = strtosize_or_err(optarg, _("failed to parse output limit size")); | |
865 | break; | |
93af8d8b | 866 | case 'q': |
3f19b85f | 867 | ctl.quiet = 1; |
93af8d8b | 868 | break; |
daee4d95 | 869 | case 'm': |
107293a6 | 870 | if (c_strcasecmp(optarg, "classic") == 0) |
daee4d95 | 871 | format = SCRIPT_FMT_TIMING_SIMPLE; |
107293a6 | 872 | else if (c_strcasecmp(optarg, "advanced") == 0) |
daee4d95 KZ |
873 | format = SCRIPT_FMT_TIMING_MULTI; |
874 | else | |
760ecc36 | 875 | errx(EXIT_FAILURE, _("unsupported logging format: '%s'"), optarg); |
daee4d95 | 876 | break; |
93af8d8b | 877 | case 't': |
4f7521d6 KZ |
878 | if (optarg && *optarg == '=') |
879 | optarg++; | |
00130885 | 880 | timingfile = optarg ? optarg : "/dev/stderr"; |
93af8d8b | 881 | break; |
fc58044f | 882 | case 'T' : |
fc58044f KZ |
883 | timingfile = optarg; |
884 | break; | |
93af8d8b | 885 | case 'V': |
2c308875 | 886 | print_version(EXIT_SUCCESS); |
93af8d8b | 887 | case 'h': |
86be6a32 | 888 | usage(); |
93af8d8b | 889 | default: |
677ec86c | 890 | errtryhelp(EXIT_FAILURE); |
93af8d8b | 891 | } |
fc58044f | 892 | } |
7268e79b | 893 | |
93af8d8b SK |
894 | argc -= optind; |
895 | argv += optind; | |
896 | ||
7268e79b W |
897 | /* |
898 | * Note that "--" separates non-option arguments. The script can be | |
899 | * used with an <outfile> name as well as with a <command>. The | |
900 | * <outfile> is always before "--" and <command> is always after "--". | |
901 | * Possible combinations are: | |
902 | * | |
903 | * script [options] <outfile> | |
904 | * script [options] -- <command ...> | |
905 | * script [options] <outfile> -- <command ...> | |
906 | */ | |
907 | ||
70062aad KZ |
908 | /* default if no --log-* specified */ |
909 | if (!outfile && !infile) { | |
7268e79b W |
910 | /* if argv[0] is not after --, use argv[0] as outfile */ |
911 | if (argc > 0 && strcmp(argv[-1], "--") != 0) { | |
70062aad | 912 | outfile = argv[0]; |
ec96a89e CH |
913 | argc--; |
914 | argv++; | |
915 | } else { | |
ddbdb792 | 916 | die_if_link(&ctl, DEFAULT_TYPESCRIPT_FILENAME); |
70062aad KZ |
917 | outfile = DEFAULT_TYPESCRIPT_FILENAME; |
918 | } | |
596f4202 | 919 | |
70062aad KZ |
920 | /* associate stdout with typescript file */ |
921 | log_associate(&ctl, &ctl.out, outfile, SCRIPT_FMT_RAW); | |
922 | } | |
93af8d8b | 923 | |
7268e79b W |
924 | /* skip -- to non-option arguments */ |
925 | if (argc > 0 && strcmp(argv[0], "--") == 0) { | |
926 | argc--; | |
927 | argv++; | |
928 | } | |
929 | ||
930 | /* concat non-option arguments as command */ | |
931 | if (argc > 0 && strcmp(argv[-1], "--") == 0) { | |
932 | if (ctl.command != NULL) { | |
3d997c94 | 933 | warnx(_("option --command and a command after '--' cannot be combined")); |
7268e79b W |
934 | errtryhelp(EXIT_FAILURE); |
935 | } | |
936 | ||
f39ffccf | 937 | ctl.command = ul_strv_join(argv, " "); |
7268e79b W |
938 | if (!ctl.command) |
939 | errx(EXIT_FAILURE, _("failed to concatenate arguments")); | |
940 | ||
941 | ctl.command_norm = xstrdup(ctl.command); | |
7bb1151e | 942 | ul_strrep(ctl.command_norm, '\n', ' '); |
7268e79b W |
943 | argc = 0; |
944 | } | |
945 | ||
ec96a89e | 946 | if (argc > 0) { |
ec96a89e CH |
947 | warnx(_("unexpected number of arguments")); |
948 | errtryhelp(EXIT_FAILURE); | |
949 | } | |
950 | ||
c1c2ee0b | 951 | if (timingfile) { |
d628e9cf KZ |
952 | /* the old SCRIPT_FMT_TIMING_SIMPLE should be used when |
953 | * recoding output only (just for backward compatibility), | |
954 | * otherwise switch to new format. */ | |
c1c2ee0b | 955 | if (!format) |
d628e9cf | 956 | format = infile || (outfile && infile) ? |
c1c2ee0b KZ |
957 | SCRIPT_FMT_TIMING_MULTI : |
958 | SCRIPT_FMT_TIMING_SIMPLE; | |
daee4d95 KZ |
959 | |
960 | else if (format == SCRIPT_FMT_TIMING_SIMPLE && outfile && infile) | |
961 | errx(EXIT_FAILURE, _("log multiple streams is mutually " | |
962 | "exclusive with 'classic' format")); | |
c1c2ee0b KZ |
963 | if (outfile) |
964 | log_associate(&ctl, &ctl.out, timingfile, format); | |
965 | if (infile) | |
966 | log_associate(&ctl, &ctl.in, timingfile, format); | |
967 | } | |
968 | ||
ec10634e KZ |
969 | shell = getenv("SHELL"); |
970 | if (!shell) | |
971 | shell = _PATH_BSHELL; | |
93af8d8b | 972 | |
ec10634e KZ |
973 | ctl.pty = ul_new_pty(ctl.isterm); |
974 | if (!ctl.pty) | |
975 | err(EXIT_FAILURE, "failed to allocate PTY handler"); | |
976 | ||
1eee1acb | 977 | ul_pty_slave_echo(ctl.pty, echo); |
4169bcb7 | 978 | |
ec10634e KZ |
979 | ul_pty_set_callback_data(ctl.pty, (void *) &ctl); |
980 | cb = ul_pty_get_callbacks(ctl.pty); | |
bdd43357 | 981 | cb->child_die = callback_child_die; |
ec10634e KZ |
982 | cb->child_sigstop = callback_child_sigstop; |
983 | cb->log_stream_activity = callback_log_stream_activity; | |
984 | cb->log_signal = callback_log_signal; | |
7727be1a | 985 | cb->flush_logs = callback_flush_logs; |
3cecd176 | 986 | |
d805688a | 987 | if (!ctl.quiet) { |
70062aad KZ |
988 | printf(_("Script started")); |
989 | if (outfile) | |
990 | printf(_(", output log file is '%s'"), outfile); | |
991 | if (infile) | |
992 | printf(_(", input log file is '%s'"), infile); | |
993 | if (timingfile) | |
994 | printf(_(", timing file is '%s'"), timingfile); | |
995 | printf(_(".\n")); | |
d805688a | 996 | } |
ec10634e | 997 | |
b1154c4e | 998 | |
ec10634e KZ |
999 | if (ul_pty_setup(ctl.pty)) |
1000 | err(EXIT_FAILURE, _("failed to create pseudo-terminal")); | |
1001 | ||
6ed644fb KZ |
1002 | #ifdef HAVE_LIBUTEMPTER |
1003 | utempter_add_record(ul_pty_get_childfd(ctl.pty), NULL); | |
1004 | #endif | |
1005 | ||
1006 | if (ul_pty_signals_setup(ctl.pty)) | |
1007 | err(EXIT_FAILURE, _("failed to initialize signals handler")); | |
ec10634e KZ |
1008 | fflush(stdout); |
1009 | ||
1010 | /* | |
1011 | * We have terminal, do not use err() from now, use "goto done" | |
1012 | */ | |
93af8d8b | 1013 | |
ec10634e KZ |
1014 | switch ((int) (ctl.child = fork())) { |
1015 | case -1: /* error */ | |
1016 | warn(_("cannot create child process")); | |
1017 | rc = -errno; | |
1018 | goto done; | |
a2b4dec5 | 1019 | |
ec10634e KZ |
1020 | case 0: /* child */ |
1021 | { | |
1022 | const char *shname; | |
93af8d8b | 1023 | |
ec10634e | 1024 | ul_pty_init_slave(ctl.pty); |
a2b4dec5 | 1025 | |
ec10634e | 1026 | signal(SIGTERM, SIG_DFL); /* because /etc/csh.login */ |
93af8d8b | 1027 | |
ec10634e KZ |
1028 | shname = strrchr(shell, '/'); |
1029 | shname = shname ? shname + 1 : shell; | |
1030 | ||
4169bcb7 | 1031 | if (access(shell, X_OK) == 0) { |
4d8edee5 KZ |
1032 | if (ctl.command) |
1033 | execl(shell, shname, "-c", ctl.command, (char *)NULL); | |
4169bcb7 | 1034 | else |
1b10fa0e | 1035 | execl(shell, shname, "-i", (char *)NULL); |
4169bcb7 | 1036 | } else { |
4d8edee5 | 1037 | if (ctl.command) |
521eb057 | 1038 | execlp(shname, shname, "-c", ctl.command, (char *)NULL); |
4169bcb7 | 1039 | else |
521eb057 | 1040 | execlp(shname, shname, "-i", (char *)NULL); |
4169bcb7 KZ |
1041 | } |
1042 | ||
ec10634e | 1043 | err(EXIT_FAILURE, "failed to execute %s", shell); |
d35ffe80 | 1044 | break; |
ec10634e KZ |
1045 | } |
1046 | default: | |
d35ffe80 | 1047 | break; |
ec10634e KZ |
1048 | } |
1049 | ||
1050 | /* parent */ | |
1051 | ul_pty_set_child(ctl.pty, ctl.child); | |
1052 | ||
9e9b3d65 | 1053 | rc = logging_start(&ctl); |
ec10634e KZ |
1054 | if (rc) |
1055 | goto done; | |
1056 | ||
bdd43357 | 1057 | /* add extra info to advanced timing file */ |
ec10634e KZ |
1058 | if (timingfile && format == SCRIPT_FMT_TIMING_MULTI) { |
1059 | char buf[FORMAT_TIMESTAMP_MAX]; | |
1060 | time_t tvec = script_time((time_t *)NULL); | |
1061 | ||
1062 | strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf)); | |
94e4a414 | 1063 | log_info(&ctl, "START_TIME", "%s", buf); |
ec10634e KZ |
1064 | |
1065 | if (ctl.isterm) { | |
1066 | init_terminal_info(&ctl); | |
94e4a414 KZ |
1067 | log_info(&ctl, "TERM", "%s", ctl.ttytype); |
1068 | log_info(&ctl, "TTY", "%s", ctl.ttyname); | |
ec10634e KZ |
1069 | log_info(&ctl, "COLUMNS", "%d", ctl.ttycols); |
1070 | log_info(&ctl, "LINES", "%d", ctl.ttylines); | |
fd42a45b | 1071 | } |
bd4739da | 1072 | log_info(&ctl, "SHELL", "%s", shell); |
4d8edee5 | 1073 | if (ctl.command) |
bfcdfccf | 1074 | log_info(&ctl, "COMMAND", "%s", ctl.command_norm); |
bd4739da | 1075 | log_info(&ctl, "TIMING_LOG", "%s", timingfile); |
ec10634e | 1076 | if (outfile) |
bd4739da | 1077 | log_info(&ctl, "OUTPUT_LOG", "%s", outfile); |
ec10634e | 1078 | if (infile) |
bd4739da | 1079 | log_info(&ctl, "INPUT_LOG", "%s", infile); |
ec10634e KZ |
1080 | } |
1081 | ||
1082 | /* this is the main loop */ | |
1083 | rc = ul_pty_proxy_master(ctl.pty); | |
1084 | ||
1085 | /* all done; cleanup and kill */ | |
1086 | caught_signal = ul_pty_get_delivered_signal(ctl.pty); | |
1087 | ||
1088 | if (!caught_signal && ctl.child != (pid_t)-1) | |
bdd43357 | 1089 | ul_pty_wait_for_child(ctl.pty); /* final wait */ |
ec10634e KZ |
1090 | |
1091 | if (caught_signal && ctl.child != (pid_t)-1) { | |
1092 | fprintf(stderr, "\nSession terminated, killing shell..."); | |
1093 | kill(ctl.child, SIGTERM); | |
1094 | sleep(2); | |
1095 | kill(ctl.child, SIGKILL); | |
1096 | fprintf(stderr, " ...killed.\n"); | |
1097 | } | |
1098 | ||
1099 | done: | |
1100 | ul_pty_cleanup(ctl.pty); | |
9e9b3d65 | 1101 | logging_done(&ctl, NULL); |
ec10634e KZ |
1102 | |
1103 | if (!ctl.quiet) | |
1104 | printf(_("Script done.\n")); | |
1105 | ||
1106 | #ifdef HAVE_LIBUTEMPTER | |
1107 | if (ul_pty_get_childfd(ctl.pty) >= 0) | |
1108 | utempter_remove_record(ul_pty_get_childfd(ctl.pty)); | |
1109 | #endif | |
1110 | ul_free_pty(ctl.pty); | |
1111 | ||
1112 | /* default exit code */ | |
1113 | rc = rc ? EXIT_FAILURE : EXIT_SUCCESS; | |
1114 | ||
1115 | /* exit code based on child status */ | |
1116 | if (ctl.rc_wanted && rc == EXIT_SUCCESS) { | |
1117 | if (WIFSIGNALED(ctl.childstatus)) | |
1118 | rc = WTERMSIG(ctl.childstatus) + 0x80; | |
1119 | else | |
1120 | rc = WEXITSTATUS(ctl.childstatus); | |
93af8d8b | 1121 | } |
d35ffe80 | 1122 | |
ec10634e KZ |
1123 | DBG(MISC, ul_debug("done [rc=%d]", rc)); |
1124 | return rc; | |
6dbe3af9 | 1125 | } |