2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2010, 2012 David E. O'Brien
5 * Copyright (c) 1980, 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 __FBSDID("$FreeBSD$");
36 static const char copyright[] =
37 "@(#) Copyright (c) 1980, 1992, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/ioctl.h>
49 #include <sys/endian.h>
50 #include <dev/filemon/filemon.h>
67 uint64_t scr_len; /* amount of data */
68 uint64_t scr_sec; /* time it arrived in seconds... */
69 uint32_t scr_usec; /* ...and microseconds */
70 uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */
74 static int master, slave;
76 static const char *fname;
78 static int fflg, qflg, ttyflg;
79 static int usesleep, rawout, showexit;
81 static struct termios tt;
83 static void done(int) __dead2;
84 static void doshell(char **);
85 static void finish(void);
86 static void record(FILE *, char *, size_t, int);
87 static void consume(FILE *, off_t, char *, int);
88 static void playback(FILE *) __dead2;
89 static void usage(void);
92 main(int argc, char *argv[])
95 struct termios rtt, stt;
97 struct timeval tv, *tvp;
102 int aflg, Fflg, kflg, pflg, ch, k, n;
103 int flushtime, readstdin;
106 aflg = Fflg = kflg = pflg = 0;
110 fm_fd = -1; /* Shut up stupid "may be used uninitialized" GCC
111 warning. (not needed w/clang) */
114 while ((ch = getopt(argc, argv, "adFfkpqrt:")) != -1)
141 flushtime = atoi(optarg);
143 err(1, "invalid flush time %d", flushtime);
157 fname = "typescript";
159 if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
163 asprintf(&fmfname, "%s.filemon", fname);
165 err(1, "%s.filemon", fname);
166 if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1)
167 err(1, "open(\"/dev/filemon\", O_RDWR)");
168 if ((fm_log = open(fmfname,
169 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
170 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
171 err(1, "open(%s)", fmfname);
172 if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0)
173 err(1, "Cannot set filemon log file descriptor");
179 if ((ttyflg = isatty(STDIN_FILENO)) != 0) {
180 if (tcgetattr(STDIN_FILENO, &tt) == -1)
182 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
184 if (openpty(&master, &slave, NULL, &tt, &win) == -1)
187 if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
192 record(fscript, NULL, 0, 's');
196 (void)printf("Script started, output file is %s\n", fname);
198 (void)fprintf(fscript, "Script started on %s",
202 fprintf(fscript, "Command: ");
203 for (k = 0 ; argv[k] ; ++k)
204 fprintf(fscript, "%s%s", k ? " " : "",
206 fprintf(fscript, "\n");
211 (void)printf("Filemon started, output file is %s\n",
218 rtt.c_lflag &= ~ECHO;
219 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
232 if (ioctl(fm_fd, FILEMON_SET_PID, &pid) < 0)
233 err(1, "Cannot set filemon PID");
240 start = tvec = time(0);
244 FD_SET(master, &rfd);
246 FD_SET(STDIN_FILENO, &rfd);
247 if (!readstdin && ttyflg) {
252 } else if (flushtime > 0) {
253 tv.tv_sec = flushtime - (tvec - start);
259 n = select(master + 1, &rfd, 0, 0, tvp);
260 if (n < 0 && errno != EINTR)
262 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
263 cc = read(STDIN_FILENO, ibuf, BUFSIZ);
267 if (tcgetattr(master, &stt) == 0 &&
268 (stt.c_lflag & ICANON) != 0) {
269 (void)write(master, &stt.c_cc[VEOF], 1);
275 record(fscript, ibuf, cc, 'i');
276 (void)write(master, ibuf, cc);
277 if (kflg && tcgetattr(master, &stt) >= 0 &&
278 ((stt.c_lflag & ECHO) == 0)) {
279 (void)fwrite(ibuf, 1, cc, fscript);
283 if (n > 0 && FD_ISSET(master, &rfd)) {
284 cc = read(master, obuf, sizeof (obuf));
287 (void)write(STDOUT_FILENO, obuf, cc);
289 record(fscript, obuf, cc, 'o');
291 (void)fwrite(obuf, 1, cc, fscript);
294 if (tvec - start >= flushtime) {
308 (void)fprintf(stderr,
309 "usage: script [-adfkpqr] [-t time] [file [command ...]]\n");
318 if (waitpid(child, &status, 0) == child) {
319 if (WIFEXITED(status))
320 e = WEXITSTATUS(status);
321 else if (WIFSIGNALED(status))
322 e = WTERMSIG(status);
323 else /* can't happen */
334 shell = getenv("SHELL");
336 shell = _PATH_BSHELL;
339 (void)fclose(fscript);
342 setenv("SCRIPT", fname, 1);
347 execl(shell, shell, "-i", (char *)NULL);
359 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
362 record(fscript, NULL, 0, 'e');
366 (void)fprintf(fscript, "\nCommand exit status:"
368 (void)fprintf(fscript,"\nScript done on %s",
371 (void)printf("\nScript done, output file is %s\n", fname);
373 (void)printf("Filemon done, output file is %s\n",
377 (void)fclose(fscript);
383 record(FILE *fp, char *buf, size_t cc, int direction)
389 (void)gettimeofday(&tv, NULL);
391 stamp.scr_sec = tv.tv_sec;
392 stamp.scr_usec = tv.tv_usec;
393 stamp.scr_direction = direction;
394 iov[0].iov_len = sizeof(stamp);
395 iov[0].iov_base = &stamp;
397 iov[1].iov_base = buf;
398 if (writev(fileno(fp), &iov[0], 2) == -1)
403 consume(FILE *fp, off_t len, char *buf, int reg)
408 if (fseeko(fp, len, SEEK_CUR) == -1)
413 l = MIN(DEF_BUF, len);
414 if (fread(buf, sizeof(char), l, fp) != l)
415 err(1, "cannot read buffer");
421 #define swapstamp(stamp) do { \
422 if (stamp.scr_direction > 0xff) { \
423 stamp.scr_len = bswap64(stamp.scr_len); \
424 stamp.scr_sec = bswap64(stamp.scr_sec); \
425 stamp.scr_usec = bswap32(stamp.scr_usec); \
426 stamp.scr_direction = bswap32(stamp.scr_direction); \
428 } while (0/*CONSTCOND*/)
433 struct timespec tsi, tso;
437 off_t nread, save_len;
442 if (fstat(fileno(fp), &pst) == -1)
443 err(1, "fstat failed");
445 reg = S_ISREG(pst.st_mode);
447 for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
448 if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
450 err(1, "reading playback header");
455 save_len = sizeof(stamp);
457 if (reg && stamp.scr_len >
458 (uint64_t)(pst.st_size - save_len) - nread)
459 errx(1, "invalid stamp");
461 save_len += stamp.scr_len;
462 tclock = stamp.scr_sec;
463 tso.tv_sec = stamp.scr_sec;
464 tso.tv_nsec = stamp.scr_usec * 1000;
466 switch (stamp.scr_direction) {
469 (void)printf("Script started on %s",
472 (void)consume(fp, stamp.scr_len, buf, reg);
476 (void)printf("\nScript done on %s",
478 (void)consume(fp, stamp.scr_len, buf, reg);
481 /* throw input away */
482 (void)consume(fp, stamp.scr_len, buf, reg);
485 tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
486 tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
487 if (tsi.tv_nsec < 0) {
489 tsi.tv_nsec += 1000000000;
492 (void)nanosleep(&tsi, NULL);
494 while (stamp.scr_len > 0) {
495 l = MIN(DEF_BUF, stamp.scr_len);
496 if (fread(buf, sizeof(char), l, fp) != l)
497 err(1, "cannot read buffer");
499 (void)write(STDOUT_FILENO, buf, l);
504 errx(1, "invalid direction");