2006-12-06 17:25:32 -06:00
|
|
|
/*
|
2019-10-03 07:33:25 -05:00
|
|
|
* Copyright (C) 1980 Regents of the University of California.
|
|
|
|
* Copyright (C) 2013-2019 Karel Zak <kzak@redhat.com>
|
|
|
|
*
|
2006-12-06 17:25:32 -06:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
2014-12-19 19:05:49 -06:00
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
2006-12-06 17:25:32 -06:00
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2006-12-06 17:25:43 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <time.h>
|
2006-12-06 17:25:32 -06:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/time.h>
|
2008-07-24 07:18:00 -05:00
|
|
|
#include <signal.h>
|
2007-08-06 17:36:31 -05:00
|
|
|
#include <errno.h>
|
2007-11-28 03:59:24 -06:00
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
2011-06-28 05:43:25 -05:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stddef.h>
|
2013-12-03 06:32:55 -06:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <poll.h>
|
2014-12-26 05:51:00 -06:00
|
|
|
#include <sys/signalfd.h>
|
|
|
|
#include <assert.h>
|
2015-10-16 04:48:47 -05:00
|
|
|
#include <inttypes.h>
|
2007-11-28 03:59:24 -06:00
|
|
|
|
2012-04-04 12:44:04 -05:00
|
|
|
#include "closestream.h"
|
2006-12-06 17:25:39 -06:00
|
|
|
#include "nls.h"
|
2011-04-12 05:57:56 -05:00
|
|
|
#include "c.h"
|
2013-12-03 06:32:55 -06:00
|
|
|
#include "ttyutils.h"
|
2014-01-16 07:44:37 -06:00
|
|
|
#include "all-io.h"
|
2014-12-30 17:03:32 -06:00
|
|
|
#include "monotonic.h"
|
2017-04-14 11:43:41 -05:00
|
|
|
#include "timeutils.h"
|
2018-05-14 04:20:06 -05:00
|
|
|
#include "strutils.h"
|
2019-04-03 09:32:41 -05:00
|
|
|
#include "xalloc.h"
|
2019-04-12 05:47:03 -05:00
|
|
|
#include "optutils.h"
|
2019-06-24 05:45:44 -05:00
|
|
|
#include "signames.h"
|
2019-10-03 07:33:25 -05:00
|
|
|
#include "pty-session.h"
|
2015-06-16 07:03:09 -05:00
|
|
|
#include "debug.h"
|
|
|
|
|
2017-02-11 18:19:33 -06:00
|
|
|
static UL_DEBUG_DEFINE_MASK(script);
|
2015-06-16 07:03:09 -05:00
|
|
|
UL_DEBUG_DEFINE_MASKNAMES(script) = UL_DEBUG_EMPTY_MASKNAMES;
|
|
|
|
|
|
|
|
#define SCRIPT_DEBUG_INIT (1 << 1)
|
2019-10-03 07:33:25 -05:00
|
|
|
#define SCRIPT_DEBUG_PTY (1 << 2)
|
|
|
|
#define SCRIPT_DEBUG_IO (1 << 3)
|
|
|
|
#define SCRIPT_DEBUG_SIGNAL (1 << 4)
|
2015-06-16 07:03:09 -05:00
|
|
|
#define SCRIPT_DEBUG_MISC (1 << 5)
|
|
|
|
#define SCRIPT_DEBUG_ALL 0xFFFF
|
|
|
|
|
|
|
|
#define DBG(m, x) __UL_DBG(script, SCRIPT_DEBUG_, m, x)
|
|
|
|
#define ON_DBG(m, x) __UL_DBG_CALL(script, SCRIPT_DEBUG_, m, x)
|
|
|
|
|
2010-04-29 05:25:06 -05:00
|
|
|
#ifdef HAVE_LIBUTEMPTER
|
2011-12-01 16:10:21 -06:00
|
|
|
# include <utempter.h>
|
2010-04-29 05:25:06 -05:00
|
|
|
#endif
|
|
|
|
|
2015-06-16 05:26:00 -05:00
|
|
|
#define DEFAULT_TYPESCRIPT_FILENAME "typescript"
|
2011-06-28 05:43:26 -05:00
|
|
|
|
2019-04-23 07:41:20 -05:00
|
|
|
/*
|
|
|
|
* Script is driven by stream (stdout/stdin) activity. It's possible to
|
|
|
|
* associate arbitrary number of log files with the stream. We have two basic
|
|
|
|
* types of log files: "timing file" (simple or multistream) and "data file"
|
|
|
|
* (raw).
|
|
|
|
*
|
2019-12-11 13:50:06 -06:00
|
|
|
* The same log file maybe be shared between both streams. For example
|
2019-04-23 07:41:20 -05:00
|
|
|
* multi-stream timing file is possible to use for stdin as well as for stdout.
|
|
|
|
*/
|
2019-04-03 09:32:41 -05:00
|
|
|
enum {
|
|
|
|
SCRIPT_FMT_RAW = 1, /* raw slave/master data */
|
2019-06-19 08:47:44 -05:00
|
|
|
SCRIPT_FMT_TIMING_SIMPLE, /* (classic) in format "<delta> <offset>" */
|
|
|
|
SCRIPT_FMT_TIMING_MULTI, /* (advanced) multiple streams in format "<type> <delta> <offset|etc> */
|
2019-04-03 09:32:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct script_log {
|
|
|
|
FILE *fp; /* file pointer (handler) */
|
|
|
|
int format; /* SCRIPT_FMT_* */
|
|
|
|
char *filename; /* on command line specified name */
|
2019-04-18 06:21:05 -05:00
|
|
|
struct timeval oldtime; /* previous entry log time (SCRIPT_FMT_TIMING_* only) */
|
2019-06-28 03:01:29 -05:00
|
|
|
struct timeval starttime;
|
2019-04-12 09:27:54 -05:00
|
|
|
|
|
|
|
unsigned int initialized : 1;
|
2019-04-03 09:32:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct script_stream {
|
2019-04-12 04:40:52 -05:00
|
|
|
struct script_log **logs; /* logs where to write data from stream */
|
2019-04-03 09:32:41 -05:00
|
|
|
size_t nlogs; /* number of logs */
|
2019-04-12 09:27:54 -05:00
|
|
|
char ident; /* stream identifier */
|
2019-04-03 09:32:41 -05:00
|
|
|
};
|
|
|
|
|
2014-12-19 19:05:49 -06:00
|
|
|
struct script_control {
|
2019-04-03 09:32:41 -05:00
|
|
|
uint64_t outsz; /* current output files size */
|
|
|
|
uint64_t maxsz; /* maximum output files size */
|
|
|
|
|
|
|
|
struct script_stream out; /* output */
|
|
|
|
struct script_stream in; /* input */
|
|
|
|
|
2019-06-26 10:32:18 -05:00
|
|
|
struct script_log *siglog; /* log for signal entries */
|
|
|
|
struct script_log *infolog; /* log for info entries */
|
|
|
|
|
|
|
|
const char *ttyname;
|
|
|
|
const char *ttytype;
|
|
|
|
int ttycols;
|
|
|
|
int ttylines;
|
2019-06-24 05:45:44 -05:00
|
|
|
|
2019-10-07 05:24:43 -05:00
|
|
|
struct ul_pty *pty; /* pseudo-terminal */
|
2014-12-19 19:05:49 -06:00
|
|
|
pid_t child; /* child pid */
|
|
|
|
int childstatus; /* child process exit value */
|
2019-10-03 07:33:25 -05:00
|
|
|
|
2014-12-19 19:05:49 -06:00
|
|
|
unsigned int
|
2015-06-16 05:34:04 -05:00
|
|
|
append:1, /* append output */
|
2015-06-16 05:26:00 -05:00
|
|
|
rc_wanted:1, /* return child exit value */
|
2015-06-16 05:34:04 -05:00
|
|
|
flush:1, /* flush after each write */
|
|
|
|
quiet:1, /* suppress most output */
|
|
|
|
force:1, /* write output to links */
|
2019-10-03 07:33:25 -05:00
|
|
|
isterm:1; /* is child process running as terminal */
|
2014-12-19 19:05:49 -06:00
|
|
|
};
|
2007-08-06 17:36:31 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
static ssize_t log_info(struct script_control *ctl, const char *name, const char *msgfmt, ...);
|
2019-04-03 09:32:41 -05:00
|
|
|
|
2015-06-16 07:03:09 -05:00
|
|
|
static void script_init_debug(void)
|
|
|
|
{
|
2018-01-17 06:58:29 -06:00
|
|
|
__UL_INIT_DEBUG_FROM_ENV(script, SCRIPT_DEBUG_, 0, SCRIPT_DEBUG);
|
2015-06-16 07:03:09 -05:00
|
|
|
}
|
|
|
|
|
2019-06-26 10:32:18 -05:00
|
|
|
static void init_terminal_info(struct script_control *ctl)
|
|
|
|
{
|
|
|
|
if (ctl->ttyname || !ctl->isterm)
|
|
|
|
return; /* already initialized */
|
|
|
|
|
|
|
|
get_terminal_dimension(&ctl->ttycols, &ctl->ttylines);
|
|
|
|
get_terminal_name(&ctl->ttyname, NULL, NULL);
|
|
|
|
get_terminal_type(&ctl->ttytype);
|
|
|
|
}
|
|
|
|
|
2015-05-24 12:14:58 -05:00
|
|
|
/*
|
|
|
|
* For tests we want to be able to control time output
|
|
|
|
*/
|
|
|
|
#ifdef TEST_SCRIPT
|
|
|
|
static inline time_t script_time(time_t *t)
|
|
|
|
{
|
|
|
|
const char *str = getenv("SCRIPT_TEST_SECOND_SINCE_EPOCH");
|
2015-10-15 20:12:59 -05:00
|
|
|
int64_t sec;
|
2015-05-24 12:14:58 -05:00
|
|
|
|
2015-10-16 04:48:47 -05:00
|
|
|
if (!str || sscanf(str, "%"SCNi64, &sec) != 1)
|
2015-10-15 20:12:59 -05:00
|
|
|
return time(t);
|
|
|
|
if (t)
|
|
|
|
*t = (time_t)sec;
|
|
|
|
return (time_t)sec;
|
2015-05-24 12:14:58 -05:00
|
|
|
}
|
|
|
|
#else /* !TEST_SCRIPT */
|
|
|
|
# define script_time(x) time(x)
|
|
|
|
#endif
|
|
|
|
|
2017-06-20 13:20:29 -05:00
|
|
|
static void __attribute__((__noreturn__)) usage(void)
|
2011-02-20 06:21:15 -06:00
|
|
|
{
|
2017-06-20 13:20:29 -05:00
|
|
|
FILE *out = stdout;
|
2014-09-28 14:51:41 -05:00
|
|
|
fputs(USAGE_HEADER, out);
|
2014-12-19 19:05:49 -06:00
|
|
|
fprintf(out, _(" %s [options] [file]\n"), program_invocation_short_name);
|
2011-08-16 06:41:06 -05:00
|
|
|
|
2014-12-22 15:57:17 -06:00
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(_("Make a typescript of a terminal session.\n"), out);
|
|
|
|
|
2014-09-28 14:51:41 -05:00
|
|
|
fputs(USAGE_OPTIONS, out);
|
2019-04-12 07:14:55 -05:00
|
|
|
fputs(_(" -I, --log-in <file> log stdin to file\n"), out);
|
2019-04-12 05:13:08 -05:00
|
|
|
fputs(_(" -O, --log-out <file> log stdout to file (default)\n"), out);
|
2019-04-12 09:27:54 -05:00
|
|
|
fputs(_(" -B, --log-io <file> log stdin and stdout to file\n"), out);
|
2019-06-19 05:55:02 -05:00
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
|
2019-04-12 05:47:03 -05:00
|
|
|
fputs(_(" -T, --log-timing <file> log timing information to file\n"), out);
|
2019-06-19 05:55:02 -05:00
|
|
|
fputs(_(" -t[<file>], --timing[=<file>] deprecated alias to -T (default file is stderr)\n"), out);
|
2019-06-19 08:47:44 -05:00
|
|
|
fputs(_(" -m, --logging-format <name> force to 'classic' or 'advanced' format\n"), out);
|
2019-06-19 05:55:02 -05:00
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
|
|
|
|
fputs(_(" -a, --append append to the log file\n"), out);
|
2019-04-12 04:53:29 -05:00
|
|
|
fputs(_(" -c, --command <command> run command rather than interactive shell\n"), out);
|
|
|
|
fputs(_(" -e, --return return exit code of the child process\n"), out);
|
|
|
|
fputs(_(" -f, --flush run flush after each write\n"), out);
|
|
|
|
fputs(_(" --force use output file even when it is a link\n"), out);
|
2019-11-25 05:39:52 -06:00
|
|
|
fputs(_(" -E, --echo <when> echo input (auto, always or never)\n"), out);
|
2019-04-12 04:53:29 -05:00
|
|
|
fputs(_(" -o, --output-limit <size> terminate if output files exceed size\n"), out);
|
|
|
|
fputs(_(" -q, --quiet be quiet\n"), out);
|
2011-02-20 06:21:15 -06:00
|
|
|
|
2019-04-12 04:53:29 -05:00
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
printf(USAGE_HELP_OPTIONS(31));
|
2017-06-29 08:52:16 -05:00
|
|
|
printf(USAGE_MAN_TAIL("script(1)"));
|
2019-04-12 04:53:29 -05:00
|
|
|
|
2017-06-20 13:20:29 -05:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-02-20 06:21:15 -06:00
|
|
|
}
|
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
static struct script_log *get_log_by_name(struct script_stream *stream,
|
|
|
|
const char *name)
|
2018-05-14 05:51:50 -05:00
|
|
|
{
|
2019-04-03 09:32:41 -05:00
|
|
|
size_t i;
|
2018-05-14 05:51:50 -05:00
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
for (i = 0; i < stream->nlogs; i++) {
|
2019-04-12 04:40:52 -05:00
|
|
|
struct script_log *log = stream->logs[i];
|
|
|
|
if (strcmp(log->filename, name) == 0)
|
2019-04-03 09:32:41 -05:00
|
|
|
return log;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-14 05:51:50 -05:00
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
static struct script_log *log_associate(struct script_control *ctl,
|
|
|
|
struct script_stream *stream,
|
|
|
|
const char *filename, int format)
|
|
|
|
{
|
|
|
|
struct script_log *log;
|
|
|
|
|
2019-04-12 09:27:54 -05:00
|
|
|
DBG(MISC, ul_debug("associate %s with stream", filename));
|
|
|
|
|
2019-04-12 04:40:52 -05:00
|
|
|
assert(ctl);
|
|
|
|
assert(filename);
|
|
|
|
assert(stream);
|
|
|
|
|
|
|
|
log = get_log_by_name(stream, filename);
|
|
|
|
if (log)
|
|
|
|
return log; /* already defined */
|
|
|
|
|
|
|
|
log = get_log_by_name(stream == &ctl->out ? &ctl->in : &ctl->out, filename);
|
2019-04-03 09:32:41 -05:00
|
|
|
if (!log) {
|
|
|
|
/* create a new log */
|
2019-04-12 04:40:52 -05:00
|
|
|
log = xcalloc(1, sizeof(*log));
|
|
|
|
log->filename = xstrdup(filename);
|
2019-04-03 09:32:41 -05:00
|
|
|
log->format = format;
|
|
|
|
}
|
2018-05-14 05:51:50 -05:00
|
|
|
|
2019-04-12 04:40:52 -05:00
|
|
|
/* add log to the stream */
|
|
|
|
stream->logs = xrealloc(stream->logs,
|
|
|
|
(stream->nlogs + 1) * sizeof(log));
|
|
|
|
stream->logs[stream->nlogs] = log;
|
|
|
|
stream->nlogs++;
|
|
|
|
|
2019-06-24 05:45:44 -05:00
|
|
|
/* remember where to write info about signals */
|
2019-06-26 10:32:18 -05:00
|
|
|
if (format == SCRIPT_FMT_TIMING_MULTI) {
|
|
|
|
if (!ctl->siglog)
|
|
|
|
ctl->siglog = log;
|
|
|
|
if (!ctl->infolog)
|
|
|
|
ctl->infolog = log;
|
|
|
|
}
|
2019-06-24 05:45:44 -05:00
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
return log;
|
|
|
|
}
|
2018-05-14 05:51:50 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
static int log_close(struct script_control *ctl,
|
2019-04-03 09:32:41 -05:00
|
|
|
struct script_log *log,
|
|
|
|
const char *msg,
|
|
|
|
int status)
|
|
|
|
{
|
2019-10-03 07:33:25 -05:00
|
|
|
int rc = 0;
|
|
|
|
|
2019-10-08 07:20:39 -05:00
|
|
|
if (!log || !log->initialized)
|
2019-10-03 07:33:25 -05:00
|
|
|
return 0;
|
2019-04-12 09:27:54 -05:00
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
DBG(MISC, ul_debug("closing %s", log->filename));
|
2018-05-14 05:51:50 -05:00
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
switch (log->format) {
|
|
|
|
case SCRIPT_FMT_RAW:
|
|
|
|
{
|
|
|
|
char buf[FORMAT_TIMESTAMP_MAX];
|
|
|
|
time_t tvec = script_time((time_t *)NULL);
|
2018-05-14 05:51:50 -05:00
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf));
|
|
|
|
if (msg)
|
|
|
|
fprintf(log->fp, _("\nScript done on %s [<%s>]\n"), buf, msg);
|
|
|
|
else
|
|
|
|
fprintf(log->fp, _("\nScript done on %s [COMMAND_EXIT_CODE=\"%d\"]\n"), buf, status);
|
|
|
|
break;
|
|
|
|
}
|
2019-06-28 03:01:29 -05:00
|
|
|
case SCRIPT_FMT_TIMING_MULTI:
|
|
|
|
{
|
|
|
|
struct timeval now, delta;
|
|
|
|
|
|
|
|
gettime_monotonic(&now);
|
|
|
|
timersub(&now, &log->starttime, &delta);
|
|
|
|
|
|
|
|
log_info(ctl, "DURATION", "%ld.%06ld",
|
|
|
|
(long)delta.tv_sec, (long)delta.tv_usec);
|
|
|
|
log_info(ctl, "EXIT_CODE", "%d", status);
|
|
|
|
break;
|
|
|
|
}
|
2019-04-03 09:32:41 -05:00
|
|
|
case SCRIPT_FMT_TIMING_SIMPLE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
if (close_stream(log->fp) != 0) {
|
|
|
|
warn(_("write failed: %s"), log->filename);
|
|
|
|
rc = -errno;
|
|
|
|
}
|
2018-05-14 05:51:50 -05:00
|
|
|
|
2019-10-08 07:20:39 -05:00
|
|
|
free(log->filename);
|
|
|
|
memset(log, 0, sizeof(*log));
|
2019-10-03 07:33:25 -05:00
|
|
|
|
|
|
|
return rc;
|
2018-05-14 05:51:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-09 09:04:18 -06:00
|
|
|
static int log_flush(struct script_control *ctl __attribute__((__unused__)), struct script_log *log)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!log || !log->initialized)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
DBG(MISC, ul_debug("flushing %s", log->filename));
|
|
|
|
|
|
|
|
fflush(log->fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-08 07:20:39 -05:00
|
|
|
static void log_free(struct script_control *ctl, struct script_log *log)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!log)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* the same log is possible to reference from more places, remove all
|
|
|
|
* (TODO: maybe use include/list.h to make it more elegant)
|
|
|
|
*/
|
|
|
|
if (ctl->siglog == log)
|
|
|
|
ctl->siglog = NULL;
|
|
|
|
else if (ctl->infolog == log)
|
|
|
|
ctl->infolog = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < ctl->out.nlogs; i++) {
|
|
|
|
if (ctl->out.logs[i] == log)
|
|
|
|
ctl->out.logs[i] = NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < ctl->in.nlogs; i++) {
|
|
|
|
if (ctl->in.logs[i] == log)
|
|
|
|
ctl->in.logs[i] = NULL;
|
|
|
|
}
|
|
|
|
free(log);
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
static int log_start(struct script_control *ctl,
|
2019-04-03 09:32:41 -05:00
|
|
|
struct script_log *log)
|
2018-05-14 06:51:01 -05:00
|
|
|
{
|
2019-04-12 09:27:54 -05:00
|
|
|
if (log->initialized)
|
2019-10-03 07:33:25 -05:00
|
|
|
return 0;
|
2018-05-14 06:51:01 -05:00
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
DBG(MISC, ul_debug("opening %s", log->filename));
|
2018-05-14 06:51:01 -05:00
|
|
|
|
2019-04-12 09:27:54 -05:00
|
|
|
assert(log->fp == NULL);
|
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
/* open the log */
|
|
|
|
log->fp = fopen(log->filename,
|
|
|
|
ctl->append && log->format == SCRIPT_FMT_RAW ?
|
2019-04-12 09:27:54 -05:00
|
|
|
"a" UL_CLOEXECSTR :
|
|
|
|
"w" UL_CLOEXECSTR);
|
2019-04-03 09:32:41 -05:00
|
|
|
if (!log->fp) {
|
|
|
|
warn(_("cannot open %s"), log->filename);
|
2019-10-03 07:33:25 -05:00
|
|
|
return -errno;
|
2019-04-03 09:32:41 -05:00
|
|
|
}
|
2018-05-14 06:51:01 -05:00
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
/* write header, etc. */
|
|
|
|
switch (log->format) {
|
|
|
|
case SCRIPT_FMT_RAW:
|
|
|
|
{
|
|
|
|
char buf[FORMAT_TIMESTAMP_MAX];
|
|
|
|
time_t tvec = script_time((time_t *)NULL);
|
|
|
|
|
|
|
|
strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf));
|
|
|
|
fprintf(log->fp, _("Script started on %s ["), buf);
|
|
|
|
|
|
|
|
if (ctl->isterm) {
|
2019-06-26 10:32:18 -05:00
|
|
|
init_terminal_info(ctl);
|
2019-04-03 09:32:41 -05:00
|
|
|
|
2019-06-26 10:32:18 -05:00
|
|
|
if (ctl->ttytype)
|
|
|
|
fprintf(log->fp, "TERM=\"%s\" ", ctl->ttytype);
|
|
|
|
if (ctl->ttyname)
|
|
|
|
fprintf(log->fp, "TTY=\"%s\" ", ctl->ttyname);
|
2019-04-03 09:32:41 -05:00
|
|
|
|
2019-06-26 10:32:18 -05:00
|
|
|
fprintf(log->fp, "COLUMNS=\"%d\" LINES=\"%d\"", ctl->ttycols, ctl->ttylines);
|
2019-04-03 09:32:41 -05:00
|
|
|
} else
|
|
|
|
fprintf(log->fp, _("<not executed on terminal>"));
|
|
|
|
|
|
|
|
fputs("]\n", log->fp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SCRIPT_FMT_TIMING_SIMPLE:
|
2019-04-23 10:06:29 -05:00
|
|
|
case SCRIPT_FMT_TIMING_MULTI:
|
2019-04-03 09:32:41 -05:00
|
|
|
gettime_monotonic(&log->oldtime);
|
2019-06-28 03:01:29 -05:00
|
|
|
gettime_monotonic(&log->starttime);
|
2019-04-03 09:32:41 -05:00
|
|
|
break;
|
|
|
|
}
|
2019-04-12 09:27:54 -05:00
|
|
|
|
|
|
|
log->initialized = 1;
|
2019-10-03 07:33:25 -05:00
|
|
|
return 0;
|
2019-04-03 09:32:41 -05:00
|
|
|
}
|
|
|
|
|
2019-10-08 07:20:39 -05:00
|
|
|
static int logging_start(struct script_control *ctl)
|
2019-06-27 07:02:47 -05:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* start all output logs */
|
2019-10-03 07:33:25 -05:00
|
|
|
for (i = 0; i < ctl->out.nlogs; i++) {
|
|
|
|
int rc = log_start(ctl, ctl->out.logs[i]);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
}
|
2019-06-27 07:02:47 -05:00
|
|
|
|
|
|
|
/* start all input logs */
|
2019-10-03 07:33:25 -05:00
|
|
|
for (i = 0; i < ctl->in.nlogs; i++) {
|
|
|
|
int rc = log_start(ctl, ctl->in.logs[i]);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
return 0;
|
2019-06-27 07:02:47 -05:00
|
|
|
}
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
static ssize_t log_write(struct script_control *ctl,
|
2019-04-12 09:27:54 -05:00
|
|
|
struct script_stream *stream,
|
2019-04-03 09:32:41 -05:00
|
|
|
struct script_log *log,
|
|
|
|
char *obuf, size_t bytes)
|
|
|
|
{
|
2019-10-03 07:33:25 -05:00
|
|
|
int rc;
|
|
|
|
ssize_t ssz = 0;
|
|
|
|
struct timeval now, delta;
|
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
if (!log->fp)
|
|
|
|
return 0;
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
DBG(IO, ul_debug(" writing [file=%s]", log->filename));
|
2019-04-03 09:32:41 -05:00
|
|
|
|
|
|
|
switch (log->format) {
|
|
|
|
case SCRIPT_FMT_RAW:
|
2019-10-03 07:33:25 -05:00
|
|
|
DBG(IO, ul_debug(" log raw data"));
|
|
|
|
rc = fwrite_all(obuf, 1, bytes, log->fp);
|
|
|
|
if (rc) {
|
2019-04-03 09:32:41 -05:00
|
|
|
warn(_("cannot write %s"), log->filename);
|
2019-10-03 07:33:25 -05:00
|
|
|
return rc;
|
2019-04-03 09:32:41 -05:00
|
|
|
}
|
2019-10-03 07:33:25 -05:00
|
|
|
ssz = bytes;
|
2019-04-03 09:32:41 -05:00
|
|
|
break;
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
case SCRIPT_FMT_TIMING_SIMPLE:
|
|
|
|
DBG(IO, ul_debug(" log timing info"));
|
2019-04-03 09:32:41 -05:00
|
|
|
|
|
|
|
gettime_monotonic(&now);
|
|
|
|
timersub(&now, &log->oldtime, &delta);
|
2019-10-03 07:33:25 -05:00
|
|
|
ssz = fprintf(log->fp, "%ld.%06ld %zd\n",
|
2019-04-03 09:32:41 -05:00
|
|
|
(long)delta.tv_sec, (long)delta.tv_usec, bytes);
|
2019-10-03 07:33:25 -05:00
|
|
|
if (ssz < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
log->oldtime = now;
|
|
|
|
break;
|
2019-04-12 09:27:54 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
case SCRIPT_FMT_TIMING_MULTI:
|
|
|
|
DBG(IO, ul_debug(" log multi-stream timing info"));
|
2019-04-12 09:27:54 -05:00
|
|
|
|
|
|
|
gettime_monotonic(&now);
|
|
|
|
timersub(&now, &log->oldtime, &delta);
|
2019-10-03 07:33:25 -05:00
|
|
|
ssz = fprintf(log->fp, "%c %ld.%06ld %zd\n",
|
2019-04-12 09:27:54 -05:00
|
|
|
stream->ident,
|
|
|
|
(long)delta.tv_sec, (long)delta.tv_usec, bytes);
|
2019-10-03 07:33:25 -05:00
|
|
|
if (ssz < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2019-04-12 09:27:54 -05:00
|
|
|
log->oldtime = now;
|
2019-10-03 07:33:25 -05:00
|
|
|
break;
|
2019-04-03 09:32:41 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctl->flush)
|
|
|
|
fflush(log->fp);
|
2019-10-03 07:33:25 -05:00
|
|
|
return ssz;
|
2018-05-14 06:51:01 -05:00
|
|
|
}
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
static ssize_t log_stream_activity(
|
2019-04-03 09:32:41 -05:00
|
|
|
struct script_control *ctl,
|
|
|
|
struct script_stream *stream,
|
|
|
|
char *buf, size_t bytes)
|
|
|
|
{
|
|
|
|
size_t i;
|
2019-10-03 07:33:25 -05:00
|
|
|
ssize_t outsz = 0;
|
2019-04-03 09:32:41 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
for (i = 0; i < stream->nlogs; i++) {
|
|
|
|
ssize_t ssz = log_write(ctl, stream, stream->logs[i], buf, bytes);
|
|
|
|
|
|
|
|
if (ssz < 0)
|
|
|
|
return ssz;
|
|
|
|
outsz += ssz;
|
|
|
|
}
|
2019-04-03 09:32:41 -05:00
|
|
|
|
|
|
|
return outsz;
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
static ssize_t log_signal(struct script_control *ctl, int signum, char *msgfmt, ...)
|
2019-06-24 05:45:44 -05:00
|
|
|
{
|
|
|
|
struct script_log *log;
|
|
|
|
struct timeval now, delta;
|
|
|
|
char msg[BUFSIZ] = {0};
|
|
|
|
va_list ap;
|
2019-10-03 07:33:25 -05:00
|
|
|
ssize_t sz;
|
2019-06-24 05:45:44 -05:00
|
|
|
|
|
|
|
assert(ctl);
|
|
|
|
|
|
|
|
log = ctl->siglog;
|
|
|
|
if (!log)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
assert(log->format == SCRIPT_FMT_TIMING_MULTI);
|
|
|
|
DBG(IO, ul_debug(" writing signal to multi-stream timing"));
|
|
|
|
|
|
|
|
gettime_monotonic(&now);
|
|
|
|
timersub(&now, &log->oldtime, &delta);
|
|
|
|
|
|
|
|
if (msgfmt) {
|
|
|
|
int rc;
|
|
|
|
va_start(ap, msgfmt);
|
|
|
|
rc = vsnprintf(msg, sizeof(msg), msgfmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (rc < 0)
|
|
|
|
*msg = '\0';;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*msg)
|
2019-06-27 07:02:47 -05:00
|
|
|
sz = fprintf(log->fp, "S %ld.%06ld SIG%s %s\n",
|
2019-06-24 05:45:44 -05:00
|
|
|
(long)delta.tv_sec, (long)delta.tv_usec,
|
|
|
|
signum_to_signame(signum), msg);
|
|
|
|
else
|
|
|
|
sz = fprintf(log->fp, "S %ld.%06ld SIG%s\n",
|
|
|
|
(long)delta.tv_sec, (long)delta.tv_usec,
|
|
|
|
signum_to_signame(signum));
|
|
|
|
|
|
|
|
log->oldtime = now;
|
2019-10-03 07:33:25 -05:00
|
|
|
return sz;
|
2019-06-24 05:45:44 -05:00
|
|
|
}
|
2019-04-03 09:32:41 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
static ssize_t log_info(struct script_control *ctl, const char *name, const char *msgfmt, ...)
|
2019-06-26 10:32:18 -05:00
|
|
|
{
|
|
|
|
struct script_log *log;
|
|
|
|
char msg[BUFSIZ] = {0};
|
|
|
|
va_list ap;
|
2019-10-03 07:33:25 -05:00
|
|
|
ssize_t sz;
|
2019-06-26 10:32:18 -05:00
|
|
|
|
|
|
|
assert(ctl);
|
|
|
|
|
|
|
|
log = ctl->infolog;
|
|
|
|
if (!log)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
assert(log->format == SCRIPT_FMT_TIMING_MULTI);
|
|
|
|
DBG(IO, ul_debug(" writing info to multi-stream log"));
|
|
|
|
|
|
|
|
if (msgfmt) {
|
|
|
|
int rc;
|
|
|
|
va_start(ap, msgfmt);
|
|
|
|
rc = vsnprintf(msg, sizeof(msg), msgfmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (rc < 0)
|
|
|
|
*msg = '\0';;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*msg)
|
2019-07-29 05:43:06 -05:00
|
|
|
sz = fprintf(log->fp, "H %f %s %s\n", 0.0, name, msg);
|
2019-06-26 10:32:18 -05:00
|
|
|
else
|
2019-07-29 05:43:06 -05:00
|
|
|
sz = fprintf(log->fp, "H %f %s\n", 0.0, name);
|
2019-06-26 10:32:18 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
return sz;
|
2017-06-24 05:48:08 -05:00
|
|
|
}
|
|
|
|
|
2017-06-26 08:30:12 -05:00
|
|
|
|
2019-10-08 07:20:39 -05:00
|
|
|
static void logging_done(struct script_control *ctl, const char *msg)
|
2014-12-19 18:02:42 -06:00
|
|
|
{
|
2019-04-03 09:32:41 -05:00
|
|
|
int status;
|
|
|
|
size_t i;
|
2018-05-14 06:51:01 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
DBG(MISC, ul_debug("stop logging"));
|
2017-06-24 05:48:08 -05:00
|
|
|
|
2018-05-14 06:51:01 -05:00
|
|
|
if (WIFSIGNALED(ctl->childstatus))
|
2019-04-03 09:32:41 -05:00
|
|
|
status = WTERMSIG(ctl->childstatus) + 0x80;
|
2018-05-14 06:51:01 -05:00
|
|
|
else
|
2019-04-03 09:32:41 -05:00
|
|
|
status = WEXITSTATUS(ctl->childstatus);
|
|
|
|
|
|
|
|
DBG(MISC, ul_debug(" status=%d", status));
|
|
|
|
|
|
|
|
/* close all output logs */
|
2019-10-08 07:20:39 -05:00
|
|
|
for (i = 0; i < ctl->out.nlogs; i++) {
|
|
|
|
struct script_log *log = ctl->out.logs[i];
|
|
|
|
log_close(ctl, log, msg, status);
|
|
|
|
log_free(ctl, log);
|
|
|
|
}
|
|
|
|
free(ctl->out.logs);
|
|
|
|
ctl->out.logs = NULL;
|
|
|
|
ctl->out.nlogs = 0;
|
2019-04-03 09:32:41 -05:00
|
|
|
|
|
|
|
/* close all input logs */
|
2019-10-08 07:20:39 -05:00
|
|
|
for (i = 0; i < ctl->in.nlogs; i++) {
|
|
|
|
struct script_log *log = ctl->in.logs[i];
|
|
|
|
log_close(ctl, log, msg, status);
|
|
|
|
log_free(ctl, log);
|
|
|
|
}
|
|
|
|
free(ctl->in.logs);
|
|
|
|
ctl->in.logs = NULL;
|
|
|
|
ctl->in.nlogs = 0;
|
2014-12-19 18:02:42 -06:00
|
|
|
}
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2019-10-07 05:24:43 -05:00
|
|
|
static void callback_child_die(
|
|
|
|
void *data,
|
|
|
|
pid_t child __attribute__((__unused__)),
|
|
|
|
int status)
|
2014-12-19 18:02:42 -06:00
|
|
|
{
|
2019-10-03 07:33:25 -05:00
|
|
|
struct script_control *ctl = (struct script_control *) data;
|
2014-12-19 18:02:42 -06:00
|
|
|
|
2019-10-07 05:24:43 -05:00
|
|
|
ctl->child = (pid_t) -1;
|
|
|
|
ctl->childstatus = status;
|
2015-06-17 08:48:50 -05:00
|
|
|
}
|
|
|
|
|
2019-10-07 05:24:43 -05:00
|
|
|
static void callback_child_sigstop(
|
|
|
|
void *data __attribute__((__unused__)),
|
|
|
|
pid_t child)
|
2015-06-17 08:48:50 -05:00
|
|
|
{
|
2019-10-03 07:33:25 -05:00
|
|
|
DBG(SIGNAL, ul_debug(" child stop by SIGSTOP -- stop parent too"));
|
|
|
|
kill(getpid(), SIGSTOP);
|
|
|
|
DBG(SIGNAL, ul_debug(" resume"));
|
2019-10-07 05:24:43 -05:00
|
|
|
kill(child, SIGCONT);
|
2015-06-17 08:48:50 -05:00
|
|
|
}
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
static int callback_log_stream_activity(void *data, int fd, char *buf, size_t bufsz)
|
2015-06-17 08:48:50 -05:00
|
|
|
{
|
2019-10-03 07:33:25 -05:00
|
|
|
struct script_control *ctl = (struct script_control *) data;
|
|
|
|
ssize_t ssz = 0;
|
2015-06-17 08:48:50 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
DBG(IO, ul_debug("stream activity callback"));
|
2016-04-13 04:52:43 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
/* from stdin (user) to command */
|
|
|
|
if (fd == STDIN_FILENO)
|
|
|
|
ssz = log_stream_activity(ctl, &ctl->in, buf, (size_t) bufsz);
|
2006-12-06 17:25:48 -06:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
/* from command (master) to stdout and log */
|
|
|
|
else if (fd == ul_pty_get_childfd(ctl->pty))
|
|
|
|
ssz = log_stream_activity(ctl, &ctl->out, buf, (size_t) bufsz);
|
2015-06-16 06:11:05 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
if (ssz < 0)
|
|
|
|
return (int) ssz;
|
2015-06-17 08:48:50 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
DBG(IO, ul_debug(" append %ld bytes [summary=%zu, max=%zu]", ssz,
|
|
|
|
ctl->outsz, ctl->maxsz));
|
2015-06-16 07:03:09 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
ctl->outsz += ssz;
|
2015-06-16 06:11:05 -05:00
|
|
|
|
2018-05-14 04:20:06 -05:00
|
|
|
|
2019-04-03 09:32:41 -05:00
|
|
|
/* check output limit */
|
|
|
|
if (ctl->maxsz != 0 && ctl->outsz >= ctl->maxsz) {
|
|
|
|
if (!ctl->quiet)
|
|
|
|
printf(_("Script terminated, max output files size %"PRIu64" exceeded.\n"), ctl->maxsz);
|
|
|
|
DBG(IO, ul_debug("output size %"PRIu64", exceeded limit %"PRIu64, ctl->outsz, ctl->maxsz));
|
2019-10-08 07:20:39 -05:00
|
|
|
logging_done(ctl, _("max output size exceeded"));
|
2019-10-03 07:33:25 -05:00
|
|
|
return 1;
|
2015-06-16 07:03:09 -05:00
|
|
|
}
|
2019-10-03 07:33:25 -05:00
|
|
|
return 0;
|
2014-12-26 16:32:25 -06:00
|
|
|
}
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
static int callback_log_signal(void *data, struct signalfd_siginfo *info, void *sigdata)
|
2014-12-26 16:32:25 -06:00
|
|
|
{
|
2019-10-03 07:33:25 -05:00
|
|
|
struct script_control *ctl = (struct script_control *) data;
|
|
|
|
ssize_t ssz = 0;
|
2014-12-26 16:32:25 -06:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
switch (info->ssi_signo) {
|
2014-12-26 16:32:25 -06:00
|
|
|
case SIGWINCH:
|
2019-10-03 07:33:25 -05:00
|
|
|
{
|
|
|
|
struct winsize *win = (struct winsize *) sigdata;
|
|
|
|
ssz = log_signal(ctl, info->ssi_signo, "ROWS=%d COLS=%d",
|
|
|
|
win->ws_row, win->ws_col);
|
2014-12-26 16:32:25 -06:00
|
|
|
break;
|
2019-10-03 07:33:25 -05:00
|
|
|
}
|
2015-07-28 03:36:29 -05:00
|
|
|
case SIGTERM:
|
|
|
|
/* fallthrough */
|
|
|
|
case SIGINT:
|
|
|
|
/* fallthrough */
|
|
|
|
case SIGQUIT:
|
2019-10-03 07:33:25 -05:00
|
|
|
ssz = log_signal(ctl, info->ssi_signo, NULL);
|
|
|
|
break;
|
2014-12-26 16:32:25 -06:00
|
|
|
default:
|
2019-10-03 07:33:25 -05:00
|
|
|
/* no log */
|
|
|
|
break;
|
2014-12-19 18:02:42 -06:00
|
|
|
}
|
2012-03-08 09:02:28 -06:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
return ssz < 0 ? ssz : 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2019-12-09 09:04:18 -06:00
|
|
|
static int callback_flush_logs(void *data)
|
|
|
|
{
|
|
|
|
struct script_control *ctl = (struct script_control *) data;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ctl->out.nlogs; i++) {
|
|
|
|
int rc = log_flush(ctl, ctl->out.logs[i]);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ctl->in.nlogs; i++) {
|
|
|
|
int rc = log_flush(ctl, ctl->in.logs[i]);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
static void die_if_link(struct script_control *ctl, const char *filename)
|
2014-12-19 18:02:42 -06:00
|
|
|
{
|
2019-10-03 07:33:25 -05:00
|
|
|
struct stat s;
|
2015-06-17 08:48:50 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
if (ctl->force)
|
|
|
|
return;
|
|
|
|
if (lstat(filename, &s) == 0 && (S_ISLNK(s.st_mode) || s.st_nlink > 1))
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
_("output file `%s' is a link\n"
|
|
|
|
"Use --force if you really want to use it.\n"
|
|
|
|
"Program not started."), filename);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-12-19 18:02:42 -06:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2014-12-19 19:05:49 -06:00
|
|
|
struct script_control ctl = {
|
2019-04-12 09:27:54 -05:00
|
|
|
.out = { .ident = 'O' },
|
|
|
|
.in = { .ident = 'I' },
|
2014-12-19 19:05:49 -06:00
|
|
|
};
|
2019-10-03 07:33:25 -05:00
|
|
|
struct ul_pty_callbacks *cb;
|
2019-11-25 05:39:52 -06:00
|
|
|
int ch, format = 0, caught_signal = 0, rc = 0, echo = 0;
|
2019-04-12 07:14:55 -05:00
|
|
|
const char *outfile = NULL, *infile = NULL;
|
2019-10-03 07:33:25 -05:00
|
|
|
const char *timingfile = NULL, *shell = NULL, *command = NULL;
|
2014-12-19 18:02:42 -06:00
|
|
|
|
|
|
|
enum { FORCE_OPTION = CHAR_MAX + 1 };
|
|
|
|
|
|
|
|
static const struct option longopts[] = {
|
2014-12-19 19:05:49 -06:00
|
|
|
{"append", no_argument, NULL, 'a'},
|
|
|
|
{"command", required_argument, NULL, 'c'},
|
2019-11-25 05:39:52 -06:00
|
|
|
{"echo", required_argument, NULL, 'E'},
|
2014-12-19 19:05:49 -06:00
|
|
|
{"return", no_argument, NULL, 'e'},
|
|
|
|
{"flush", no_argument, NULL, 'f'},
|
|
|
|
{"force", no_argument, NULL, FORCE_OPTION,},
|
2019-04-12 07:14:55 -05:00
|
|
|
{"log-in", required_argument, NULL, 'I'},
|
2019-04-12 05:13:08 -05:00
|
|
|
{"log-out", required_argument, NULL, 'O'},
|
2019-04-12 09:27:54 -05:00
|
|
|
{"log-io", required_argument, NULL, 'B'},
|
2019-04-12 05:47:03 -05:00
|
|
|
{"log-timing", required_argument, NULL, 'T'},
|
2019-06-19 08:47:44 -05:00
|
|
|
{"logging-format", required_argument, NULL, 'm'},
|
2018-05-14 04:20:06 -05:00
|
|
|
{"output-limit", required_argument, NULL, 'o'},
|
2014-12-19 19:05:49 -06:00
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
|
|
|
{"timing", optional_argument, NULL, 't'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}
|
2014-12-19 18:02:42 -06:00
|
|
|
};
|
2019-04-12 05:47:03 -05:00
|
|
|
static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
|
|
|
|
{ 'T', 't' },
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
|
2014-12-19 18:02:42 -06:00
|
|
|
setlocale(LC_ALL, "");
|
2014-12-27 04:14:28 -06:00
|
|
|
/*
|
|
|
|
* script -t prints time delays as floating point numbers. The example
|
|
|
|
* program (scriptreplay) that we provide to handle this timing output
|
|
|
|
* is a perl script, and does not handle numbers in locale format (not
|
|
|
|
* even when "use locale;" is added). So, since these numbers are not
|
|
|
|
* for human consumption, it seems easiest to set LC_NUMERIC here.
|
|
|
|
*/
|
|
|
|
setlocale(LC_NUMERIC, "C");
|
2014-12-19 18:02:42 -06:00
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2019-04-16 08:14:13 -05:00
|
|
|
close_stdout_atexit();
|
2014-12-19 18:02:42 -06:00
|
|
|
|
2015-06-16 07:03:09 -05:00
|
|
|
script_init_debug();
|
2019-10-03 07:33:25 -05:00
|
|
|
ON_DBG(PTY, ul_pty_init_debug(0xFFFF));
|
2015-06-16 07:03:09 -05:00
|
|
|
|
2019-11-25 05:39:52 -06:00
|
|
|
/* The default is to keep ECHO flag when stdin is not terminal. We need
|
|
|
|
* it to make stdin (in case of "echo foo | script") log-able and
|
2019-12-11 13:50:06 -06:00
|
|
|
* visible on terminal, and for backward compatibility.
|
2019-11-25 05:39:52 -06:00
|
|
|
*/
|
|
|
|
ctl.isterm = isatty(STDIN_FILENO);
|
|
|
|
echo = ctl.isterm ? 0 : 1;
|
|
|
|
|
|
|
|
while ((ch = getopt_long(argc, argv, "aB:c:eE:fI:O:o:qm:T:t::Vh", longopts, NULL)) != -1) {
|
2019-04-12 05:47:03 -05:00
|
|
|
|
|
|
|
err_exclusive_options(ch, longopts, excl, excl_st);
|
|
|
|
|
2014-12-19 19:05:49 -06:00
|
|
|
switch (ch) {
|
2014-12-19 18:02:42 -06:00
|
|
|
case 'a':
|
2015-06-16 05:34:04 -05:00
|
|
|
ctl.append = 1;
|
2014-12-19 18:02:42 -06:00
|
|
|
break;
|
|
|
|
case 'c':
|
2019-10-03 07:33:25 -05:00
|
|
|
command = optarg;
|
2014-12-19 18:02:42 -06:00
|
|
|
break;
|
2019-11-25 05:39:52 -06:00
|
|
|
case 'E':
|
|
|
|
if (strcmp(optarg, "auto") == 0)
|
|
|
|
; /* keep default */
|
|
|
|
else if (strcmp(optarg, "never") == 0)
|
|
|
|
echo = 0;
|
|
|
|
else if (strcmp(optarg, "always") == 0)
|
|
|
|
echo = 1;
|
|
|
|
else
|
|
|
|
errx(EXIT_FAILURE, _("unssuported echo mode: '%s'"), optarg);
|
|
|
|
break;
|
2014-12-19 18:02:42 -06:00
|
|
|
case 'e':
|
2015-06-16 05:26:00 -05:00
|
|
|
ctl.rc_wanted = 1;
|
2014-12-19 18:02:42 -06:00
|
|
|
break;
|
|
|
|
case 'f':
|
2015-06-16 05:34:04 -05:00
|
|
|
ctl.flush = 1;
|
2014-12-19 18:02:42 -06:00
|
|
|
break;
|
|
|
|
case FORCE_OPTION:
|
2015-06-16 05:34:04 -05:00
|
|
|
ctl.force = 1;
|
2014-12-19 18:02:42 -06:00
|
|
|
break;
|
2019-04-12 09:27:54 -05:00
|
|
|
case 'B':
|
|
|
|
log_associate(&ctl, &ctl.in, optarg, SCRIPT_FMT_RAW);
|
|
|
|
log_associate(&ctl, &ctl.out, optarg, SCRIPT_FMT_RAW);
|
|
|
|
infile = outfile = optarg;
|
|
|
|
break;
|
2019-04-12 07:14:55 -05:00
|
|
|
case 'I':
|
|
|
|
log_associate(&ctl, &ctl.in, optarg, SCRIPT_FMT_RAW);
|
|
|
|
infile = optarg;
|
|
|
|
break;
|
2019-04-12 05:13:08 -05:00
|
|
|
case 'O':
|
2019-04-12 07:14:55 -05:00
|
|
|
log_associate(&ctl, &ctl.out, optarg, SCRIPT_FMT_RAW);
|
|
|
|
outfile = optarg;
|
2019-04-12 05:13:08 -05:00
|
|
|
break;
|
2018-05-14 04:20:06 -05:00
|
|
|
case 'o':
|
|
|
|
ctl.maxsz = strtosize_or_err(optarg, _("failed to parse output limit size"));
|
|
|
|
break;
|
2014-12-19 18:02:42 -06:00
|
|
|
case 'q':
|
2015-06-16 05:34:04 -05:00
|
|
|
ctl.quiet = 1;
|
2014-12-19 18:02:42 -06:00
|
|
|
break;
|
2019-06-19 08:47:44 -05:00
|
|
|
case 'm':
|
|
|
|
if (strcasecmp(optarg, "classic") == 0)
|
|
|
|
format = SCRIPT_FMT_TIMING_SIMPLE;
|
|
|
|
else if (strcasecmp(optarg, "advanced") == 0)
|
|
|
|
format = SCRIPT_FMT_TIMING_MULTI;
|
|
|
|
else
|
2020-01-08 10:26:14 -06:00
|
|
|
errx(EXIT_FAILURE, _("unsupported logging format: '%s'"), optarg);
|
2019-06-19 08:47:44 -05:00
|
|
|
break;
|
2014-12-19 18:02:42 -06:00
|
|
|
case 't':
|
2019-04-03 09:36:43 -05:00
|
|
|
if (optarg && *optarg == '=')
|
|
|
|
optarg++;
|
2020-01-15 06:01:06 -06:00
|
|
|
timingfile = optarg ? optarg : "/dev/stderr";
|
2014-12-19 18:02:42 -06:00
|
|
|
break;
|
2019-04-12 05:47:03 -05:00
|
|
|
case 'T' :
|
|
|
|
timingfile = optarg;
|
|
|
|
break;
|
2014-12-19 18:02:42 -06:00
|
|
|
case 'V':
|
2019-04-16 08:14:13 -05:00
|
|
|
print_version(EXIT_SUCCESS);
|
2014-12-19 18:02:42 -06:00
|
|
|
case 'h':
|
2017-06-20 13:20:29 -05:00
|
|
|
usage();
|
2014-12-19 18:02:42 -06:00
|
|
|
default:
|
2016-12-19 06:13:34 -06:00
|
|
|
errtryhelp(EXIT_FAILURE);
|
2014-12-19 18:02:42 -06:00
|
|
|
}
|
2019-04-12 05:47:03 -05:00
|
|
|
}
|
2014-12-19 18:02:42 -06:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2019-04-12 07:14:55 -05:00
|
|
|
/* default if no --log-* specified */
|
|
|
|
if (!outfile && !infile) {
|
2019-04-12 05:13:08 -05:00
|
|
|
if (argc > 0)
|
2019-04-12 07:14:55 -05:00
|
|
|
outfile = argv[0];
|
|
|
|
else {
|
2019-04-12 05:13:08 -05:00
|
|
|
die_if_link(&ctl, DEFAULT_TYPESCRIPT_FILENAME);
|
2019-04-12 07:14:55 -05:00
|
|
|
outfile = DEFAULT_TYPESCRIPT_FILENAME;
|
|
|
|
}
|
2019-04-03 09:32:41 -05:00
|
|
|
|
2019-04-12 07:14:55 -05:00
|
|
|
/* associate stdout with typescript file */
|
|
|
|
log_associate(&ctl, &ctl.out, outfile, SCRIPT_FMT_RAW);
|
|
|
|
}
|
2014-12-19 18:02:42 -06:00
|
|
|
|
2019-04-12 09:27:54 -05:00
|
|
|
if (timingfile) {
|
2019-06-20 04:50:15 -05:00
|
|
|
/* the old SCRIPT_FMT_TIMING_SIMPLE should be used when
|
|
|
|
* recoding output only (just for backward compatibility),
|
|
|
|
* otherwise switch to new format. */
|
2019-04-12 09:27:54 -05:00
|
|
|
if (!format)
|
2019-06-20 04:50:15 -05:00
|
|
|
format = infile || (outfile && infile) ?
|
2019-04-12 09:27:54 -05:00
|
|
|
SCRIPT_FMT_TIMING_MULTI :
|
|
|
|
SCRIPT_FMT_TIMING_SIMPLE;
|
2019-06-19 08:47:44 -05:00
|
|
|
|
|
|
|
else if (format == SCRIPT_FMT_TIMING_SIMPLE && outfile && infile)
|
|
|
|
errx(EXIT_FAILURE, _("log multiple streams is mutually "
|
|
|
|
"exclusive with 'classic' format"));
|
2019-04-12 09:27:54 -05:00
|
|
|
if (outfile)
|
|
|
|
log_associate(&ctl, &ctl.out, timingfile, format);
|
|
|
|
if (infile)
|
|
|
|
log_associate(&ctl, &ctl.in, timingfile, format);
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
shell = getenv("SHELL");
|
|
|
|
if (!shell)
|
|
|
|
shell = _PATH_BSHELL;
|
2014-12-19 18:02:42 -06:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
ctl.pty = ul_new_pty(ctl.isterm);
|
|
|
|
if (!ctl.pty)
|
|
|
|
err(EXIT_FAILURE, "failed to allocate PTY handler");
|
|
|
|
|
2019-11-25 05:39:52 -06:00
|
|
|
ul_pty_slave_echo(ctl.pty, echo);
|
2019-11-21 05:28:51 -06:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
ul_pty_set_callback_data(ctl.pty, (void *) &ctl);
|
|
|
|
cb = ul_pty_get_callbacks(ctl.pty);
|
2019-10-07 05:24:43 -05:00
|
|
|
cb->child_die = callback_child_die;
|
2019-10-03 07:33:25 -05:00
|
|
|
cb->child_sigstop = callback_child_sigstop;
|
|
|
|
cb->log_stream_activity = callback_log_stream_activity;
|
|
|
|
cb->log_signal = callback_log_signal;
|
2019-12-09 09:04:18 -06:00
|
|
|
cb->flush_logs = callback_flush_logs;
|
2019-06-26 10:32:18 -05:00
|
|
|
|
2019-04-03 09:51:18 -05:00
|
|
|
if (!ctl.quiet) {
|
2019-04-12 07:14:55 -05:00
|
|
|
printf(_("Script started"));
|
|
|
|
if (outfile)
|
|
|
|
printf(_(", output log file is '%s'"), outfile);
|
|
|
|
if (infile)
|
|
|
|
printf(_(", input log file is '%s'"), infile);
|
|
|
|
if (timingfile)
|
|
|
|
printf(_(", timing file is '%s'"), timingfile);
|
|
|
|
printf(_(".\n"));
|
2019-04-03 09:51:18 -05:00
|
|
|
}
|
2019-10-03 07:33:25 -05:00
|
|
|
|
2019-10-09 09:43:50 -05:00
|
|
|
#ifdef HAVE_LIBUTEMPTER
|
|
|
|
utempter_add_record(ul_pty_get_childfd(ctl.pty), NULL);
|
|
|
|
#endif
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
if (ul_pty_setup(ctl.pty))
|
|
|
|
err(EXIT_FAILURE, _("failed to create pseudo-terminal"));
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have terminal, do not use err() from now, use "goto done"
|
|
|
|
*/
|
2014-12-19 18:02:42 -06:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
switch ((int) (ctl.child = fork())) {
|
|
|
|
case -1: /* error */
|
|
|
|
warn(_("cannot create child process"));
|
|
|
|
rc = -errno;
|
|
|
|
goto done;
|
2015-06-16 07:03:09 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
case 0: /* child */
|
|
|
|
{
|
|
|
|
const char *shname;
|
2014-12-19 18:02:42 -06:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
ul_pty_init_slave(ctl.pty);
|
2015-06-16 07:03:09 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
signal(SIGTERM, SIG_DFL); /* because /etc/csh.login */
|
2014-12-19 18:02:42 -06:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
shname = strrchr(shell, '/');
|
|
|
|
shname = shname ? shname + 1 : shell;
|
|
|
|
|
2019-11-21 05:28:51 -06:00
|
|
|
if (access(shell, X_OK) == 0) {
|
|
|
|
if (command)
|
|
|
|
execl(shell, shname, "-c", command, NULL);
|
|
|
|
else
|
|
|
|
execl(shell, shname, "-i", NULL);
|
|
|
|
} else {
|
|
|
|
if (command)
|
|
|
|
execlp(shname, "-c", command, NULL);
|
|
|
|
else
|
|
|
|
execlp(shname, "-i", NULL);
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
err(EXIT_FAILURE, "failed to execute %s", shell);
|
2015-06-17 06:25:46 -05:00
|
|
|
break;
|
2019-10-03 07:33:25 -05:00
|
|
|
}
|
|
|
|
default:
|
2015-06-17 06:25:46 -05:00
|
|
|
break;
|
2019-10-03 07:33:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parent */
|
|
|
|
ul_pty_set_child(ctl.pty, ctl.child);
|
|
|
|
|
2019-10-08 07:20:39 -05:00
|
|
|
rc = logging_start(&ctl);
|
2019-10-03 07:33:25 -05:00
|
|
|
if (rc)
|
|
|
|
goto done;
|
|
|
|
|
2019-10-07 05:24:43 -05:00
|
|
|
/* add extra info to advanced timing file */
|
2019-10-03 07:33:25 -05:00
|
|
|
if (timingfile && format == SCRIPT_FMT_TIMING_MULTI) {
|
|
|
|
char buf[FORMAT_TIMESTAMP_MAX];
|
|
|
|
time_t tvec = script_time((time_t *)NULL);
|
|
|
|
|
|
|
|
strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf));
|
|
|
|
log_info(&ctl, "START_TIME", buf);
|
|
|
|
|
|
|
|
if (ctl.isterm) {
|
|
|
|
init_terminal_info(&ctl);
|
|
|
|
log_info(&ctl, "TERM", ctl.ttytype);
|
|
|
|
log_info(&ctl, "TTY", ctl.ttyname);
|
|
|
|
log_info(&ctl, "COLUMNS", "%d", ctl.ttycols);
|
|
|
|
log_info(&ctl, "LINES", "%d", ctl.ttylines);
|
2019-06-27 07:02:47 -05:00
|
|
|
}
|
2019-10-03 07:33:25 -05:00
|
|
|
log_info(&ctl, "SHELL", shell);
|
|
|
|
if (command)
|
|
|
|
log_info(&ctl, "COMMAND", command);
|
|
|
|
log_info(&ctl, "TIMING_LOG", timingfile);
|
|
|
|
if (outfile)
|
|
|
|
log_info(&ctl, "OUTPUT_LOG", outfile);
|
|
|
|
if (infile)
|
|
|
|
log_info(&ctl, "INPUT_LOG", infile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is the main loop */
|
|
|
|
rc = ul_pty_proxy_master(ctl.pty);
|
|
|
|
|
|
|
|
/* all done; cleanup and kill */
|
|
|
|
caught_signal = ul_pty_get_delivered_signal(ctl.pty);
|
|
|
|
|
|
|
|
if (!caught_signal && ctl.child != (pid_t)-1)
|
2019-10-07 05:24:43 -05:00
|
|
|
ul_pty_wait_for_child(ctl.pty); /* final wait */
|
2019-10-03 07:33:25 -05:00
|
|
|
|
|
|
|
if (caught_signal && ctl.child != (pid_t)-1) {
|
|
|
|
fprintf(stderr, "\nSession terminated, killing shell...");
|
|
|
|
kill(ctl.child, SIGTERM);
|
|
|
|
sleep(2);
|
|
|
|
kill(ctl.child, SIGKILL);
|
|
|
|
fprintf(stderr, " ...killed.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
ul_pty_cleanup(ctl.pty);
|
2019-10-08 07:20:39 -05:00
|
|
|
logging_done(&ctl, NULL);
|
2019-10-03 07:33:25 -05:00
|
|
|
|
|
|
|
if (!ctl.quiet)
|
|
|
|
printf(_("Script done.\n"));
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBUTEMPTER
|
|
|
|
if (ul_pty_get_childfd(ctl.pty) >= 0)
|
|
|
|
utempter_remove_record(ul_pty_get_childfd(ctl.pty));
|
|
|
|
#endif
|
|
|
|
ul_free_pty(ctl.pty);
|
|
|
|
|
|
|
|
/* default exit code */
|
|
|
|
rc = rc ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
|
|
|
|
/* exit code based on child status */
|
|
|
|
if (ctl.rc_wanted && rc == EXIT_SUCCESS) {
|
|
|
|
if (WIFSIGNALED(ctl.childstatus))
|
|
|
|
rc = WTERMSIG(ctl.childstatus) + 0x80;
|
|
|
|
else
|
|
|
|
rc = WEXITSTATUS(ctl.childstatus);
|
2014-12-19 18:02:42 -06:00
|
|
|
}
|
2015-06-17 06:25:46 -05:00
|
|
|
|
2019-10-03 07:33:25 -05:00
|
|
|
DBG(MISC, ul_debug("done [rc=%d]", rc));
|
|
|
|
return rc;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|