19b50d902SRodney W. Grimes /* 26cff4e07SDavid E. O'Brien * Copyright (c) 2010, 2012 David E. O'Brien 39b50d902SRodney W. Grimes * Copyright (c) 1980, 1992, 1993 49b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 59b50d902SRodney W. Grimes * 69b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 79b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 89b50d902SRodney W. Grimes * are met: 99b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 109b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 119b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 139b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 149b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 159b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 169b50d902SRodney W. Grimes * without specific prior written permission. 179b50d902SRodney W. Grimes * 189b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 199b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 209b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 219b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 229b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 239b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 249b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 259b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 269b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 279b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 289b50d902SRodney W. Grimes * SUCH DAMAGE. 299b50d902SRodney W. Grimes */ 309b50d902SRodney W. Grimes 3113f02f28SDavid E. O'Brien #include <sys/param.h> 32d20f95e1SMark Murray __FBSDID("$FreeBSD$"); 339b50d902SRodney W. Grimes #ifndef lint 34236d2f55SPhilippe Charnier static const char copyright[] = 359b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1992, 1993\n\ 369b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 37d20f95e1SMark Murray #endif 389b50d902SRodney W. Grimes #ifndef lint 39d20f95e1SMark Murray static const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 40236d2f55SPhilippe Charnier #endif 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #include <sys/wait.h> 439b50d902SRodney W. Grimes #include <sys/stat.h> 449b50d902SRodney W. Grimes #include <sys/ioctl.h> 459b50d902SRodney W. Grimes #include <sys/time.h> 46df53360cSBrian Somers #include <sys/uio.h> 47df53360cSBrian Somers #include <sys/endian.h> 486cff4e07SDavid E. O'Brien #include <dev/filemon/filemon.h> 499b50d902SRodney W. Grimes 50236d2f55SPhilippe Charnier #include <err.h> 51e8eb82a8SPeter Wemm #include <errno.h> 529b50d902SRodney W. Grimes #include <fcntl.h> 53236d2f55SPhilippe Charnier #include <libutil.h> 549b50d902SRodney W. Grimes #include <paths.h> 559b50d902SRodney W. Grimes #include <signal.h> 569b50d902SRodney W. Grimes #include <stdio.h> 579b50d902SRodney W. Grimes #include <stdlib.h> 589b50d902SRodney W. Grimes #include <string.h> 599b50d902SRodney W. Grimes #include <termios.h> 609b50d902SRodney W. Grimes #include <unistd.h> 619b50d902SRodney W. Grimes 62df53360cSBrian Somers #define DEF_BUF 65536 63df53360cSBrian Somers 64df53360cSBrian Somers struct stamp { 65df53360cSBrian Somers uint64_t scr_len; /* amount of data */ 66df53360cSBrian Somers uint64_t scr_sec; /* time it arrived in seconds... */ 67df53360cSBrian Somers uint32_t scr_usec; /* ...and microseconds */ 68df53360cSBrian Somers uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */ 69df53360cSBrian Somers }; 70df53360cSBrian Somers 71e8efeec6SEd Schouten static FILE *fscript; 72e8efeec6SEd Schouten static int master, slave; 73e8efeec6SEd Schouten static int child; 74e8efeec6SEd Schouten static const char *fname; 756cff4e07SDavid E. O'Brien static char *fmfname; 766cff4e07SDavid E. O'Brien static int fflg, qflg, ttyflg; 77df53360cSBrian Somers static int usesleep, rawout; 789b50d902SRodney W. Grimes 79e8efeec6SEd Schouten static struct termios tt; 809b50d902SRodney W. Grimes 81e8efeec6SEd Schouten static void done(int) __dead2; 82e8efeec6SEd Schouten static void doshell(char **); 83e8efeec6SEd Schouten static void fail(void); 84e8efeec6SEd Schouten static void finish(void); 85df53360cSBrian Somers static void record(FILE *, char *, size_t, int); 86df53360cSBrian Somers static void consume(FILE *, off_t, char *, int); 87df53360cSBrian Somers static void playback(FILE *) __dead2; 883f330d7dSWarner Losh static void usage(void); 899b50d902SRodney W. Grimes 909b50d902SRodney W. Grimes int 91f4ac32deSDavid Malone main(int argc, char *argv[]) 929b50d902SRodney W. Grimes { 93d20f95e1SMark Murray int cc; 94e8eb82a8SPeter Wemm struct termios rtt, stt; 959b50d902SRodney W. Grimes struct winsize win; 96e8eb82a8SPeter Wemm struct timeval tv, *tvp; 97e8eb82a8SPeter Wemm time_t tvec, start; 98e8eb82a8SPeter Wemm char obuf[BUFSIZ]; 999b50d902SRodney W. Grimes char ibuf[BUFSIZ]; 100e8eb82a8SPeter Wemm fd_set rfd; 1018d105abcSTom Rhodes int aflg, Fflg, kflg, pflg, ch, k, n; 1026cff4e07SDavid E. O'Brien int flushtime, readstdin; 1036cff4e07SDavid E. O'Brien int fm_fd, fm_log; 1049b50d902SRodney W. Grimes 1058d105abcSTom Rhodes aflg = Fflg = kflg = pflg = 0; 106df53360cSBrian Somers usesleep = 1; 107df53360cSBrian Somers rawout = 0; 1086cff4e07SDavid E. O'Brien flushtime = 30; 1096cff4e07SDavid E. O'Brien fm_fd = -1; /* Shut up stupid "may be used uninitialized" GCC 1106cff4e07SDavid E. O'Brien warning. (not needed w/clang) */ 111df53360cSBrian Somers 1128d105abcSTom Rhodes while ((ch = getopt(argc, argv, "adFfkpqrt:")) != -1) 1139b50d902SRodney W. Grimes switch(ch) { 1149b50d902SRodney W. Grimes case 'a': 1159b50d902SRodney W. Grimes aflg = 1; 1169b50d902SRodney W. Grimes break; 117df53360cSBrian Somers case 'd': 118df53360cSBrian Somers usesleep = 0; 11951afb8dfSPeter Wemm break; 1208d105abcSTom Rhodes case 'F': 1218d105abcSTom Rhodes Fflg = 1; 1228d105abcSTom Rhodes break; 1236cff4e07SDavid E. O'Brien case 'f': 1246cff4e07SDavid E. O'Brien fflg = 1; 1256cff4e07SDavid E. O'Brien break; 126e8eb82a8SPeter Wemm case 'k': 127e8eb82a8SPeter Wemm kflg = 1; 128e8eb82a8SPeter Wemm break; 129df53360cSBrian Somers case 'p': 130df53360cSBrian Somers pflg = 1; 131df53360cSBrian Somers break; 132df53360cSBrian Somers case 'q': 133df53360cSBrian Somers qflg = 1; 134df53360cSBrian Somers break; 135df53360cSBrian Somers case 'r': 136df53360cSBrian Somers rawout = 1; 137df53360cSBrian Somers break; 138e8eb82a8SPeter Wemm case 't': 139e8eb82a8SPeter Wemm flushtime = atoi(optarg); 140e8eb82a8SPeter Wemm if (flushtime < 0) 141e8eb82a8SPeter Wemm err(1, "invalid flush time %d", flushtime); 142e8eb82a8SPeter Wemm break; 1439b50d902SRodney W. Grimes case '?': 1449b50d902SRodney W. Grimes default: 145236d2f55SPhilippe Charnier usage(); 1469b50d902SRodney W. Grimes } 1479b50d902SRodney W. Grimes argc -= optind; 1489b50d902SRodney W. Grimes argv += optind; 1499b50d902SRodney W. Grimes 15051afb8dfSPeter Wemm if (argc > 0) { 1519b50d902SRodney W. Grimes fname = argv[0]; 15251afb8dfSPeter Wemm argv++; 15351afb8dfSPeter Wemm argc--; 15451afb8dfSPeter Wemm } else 1559b50d902SRodney W. Grimes fname = "typescript"; 1569b50d902SRodney W. Grimes 157df53360cSBrian Somers if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL) 158236d2f55SPhilippe Charnier err(1, "%s", fname); 1599b50d902SRodney W. Grimes 1606cff4e07SDavid E. O'Brien if (fflg) { 1616cff4e07SDavid E. O'Brien asprintf(&fmfname, "%s.filemon", fname); 1626cff4e07SDavid E. O'Brien if (!fmfname) 1636cff4e07SDavid E. O'Brien err(1, "%s.filemon", fname); 1646cff4e07SDavid E. O'Brien if ((fm_fd = open("/dev/filemon", O_RDWR)) == -1) 1656cff4e07SDavid E. O'Brien err(1, "open(\"/dev/filemon\", O_RDWR)"); 1666cff4e07SDavid E. O'Brien if ((fm_log = open(fmfname, O_WRONLY | O_CREAT | O_TRUNC, 1676cff4e07SDavid E. O'Brien S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) 1686cff4e07SDavid E. O'Brien err(1, "open(%s)", fmfname); 1696cff4e07SDavid E. O'Brien if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0) 1706cff4e07SDavid E. O'Brien err(1, "Cannot set filemon log file descriptor"); 1716cff4e07SDavid E. O'Brien 1726cff4e07SDavid E. O'Brien /* Set up these two fd's to close on exec. */ 1736cff4e07SDavid E. O'Brien (void)fcntl(fm_fd, F_SETFD, FD_CLOEXEC); 1746cff4e07SDavid E. O'Brien (void)fcntl(fm_log, F_SETFD, FD_CLOEXEC); 1756cff4e07SDavid E. O'Brien } 1766cff4e07SDavid E. O'Brien 177df53360cSBrian Somers if (pflg) 178df53360cSBrian Somers playback(fscript); 179df53360cSBrian Somers 180d93708c3SEd Schouten if ((ttyflg = isatty(STDIN_FILENO)) != 0) { 181e292a0e3SColin Percival if (tcgetattr(STDIN_FILENO, &tt) == -1) 182e292a0e3SColin Percival err(1, "tcgetattr"); 183e292a0e3SColin Percival if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) 184e292a0e3SColin Percival err(1, "ioctl"); 1859b50d902SRodney W. Grimes if (openpty(&master, &slave, NULL, &tt, &win) == -1) 186236d2f55SPhilippe Charnier err(1, "openpty"); 187e292a0e3SColin Percival } else { 188e292a0e3SColin Percival if (openpty(&master, &slave, NULL, NULL, NULL) == -1) 189e292a0e3SColin Percival err(1, "openpty"); 190e292a0e3SColin Percival } 1919b50d902SRodney W. Grimes 192df53360cSBrian Somers if (rawout) 193df53360cSBrian Somers record(fscript, NULL, 0, 's'); 194df53360cSBrian Somers 195e8eb82a8SPeter Wemm if (!qflg) { 196e8eb82a8SPeter Wemm tvec = time(NULL); 1979b50d902SRodney W. Grimes (void)printf("Script started, output file is %s\n", fname); 198df53360cSBrian Somers if (!rawout) { 199df53360cSBrian Somers (void)fprintf(fscript, "Script started on %s", 200df53360cSBrian Somers ctime(&tvec)); 201df53360cSBrian Somers if (argv[0]) { 202df53360cSBrian Somers fprintf(fscript, "command: "); 203df53360cSBrian Somers for (k = 0 ; argv[k] ; ++k) 204df53360cSBrian Somers fprintf(fscript, "%s%s", k ? " " : "", 205df53360cSBrian Somers argv[k]); 206df53360cSBrian Somers fprintf(fscript, "\n"); 207df53360cSBrian Somers } 208df53360cSBrian Somers } 209e8eb82a8SPeter Wemm fflush(fscript); 2106cff4e07SDavid E. O'Brien if (fflg) { 2116cff4e07SDavid E. O'Brien (void)printf("Filemon started, output file is %s\n", 2126cff4e07SDavid E. O'Brien fmfname); 2136cff4e07SDavid E. O'Brien } 214e8eb82a8SPeter Wemm } 215e292a0e3SColin Percival if (ttyflg) { 2169b50d902SRodney W. Grimes rtt = tt; 2179b50d902SRodney W. Grimes cfmakeraw(&rtt); 2189b50d902SRodney W. Grimes rtt.c_lflag &= ~ECHO; 2199b50d902SRodney W. Grimes (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 220e292a0e3SColin Percival } 2219b50d902SRodney W. Grimes 2229b50d902SRodney W. Grimes child = fork(); 2239b50d902SRodney W. Grimes if (child < 0) { 224236d2f55SPhilippe Charnier warn("fork"); 225b1c66970SDag-Erling Smørgrav done(1); 2269b50d902SRodney W. Grimes } 227*7b245cb8SBryan Drewery if (child == 0) { 228*7b245cb8SBryan Drewery if (fflg) { 229*7b245cb8SBryan Drewery int pid; 2309b50d902SRodney W. Grimes 231*7b245cb8SBryan Drewery pid = getpid(); 232*7b245cb8SBryan Drewery if (ioctl(fm_fd, FILEMON_SET_PID, &pid) < 0) 2336cff4e07SDavid E. O'Brien err(1, "Cannot set filemon PID"); 234*7b245cb8SBryan Drewery } 235*7b245cb8SBryan Drewery 236*7b245cb8SBryan Drewery doshell(argv); 237*7b245cb8SBryan Drewery } 238*7b245cb8SBryan Drewery close(slave); 2396cff4e07SDavid E. O'Brien 24029da7547SMikolaj Golub start = tvec = time(0); 24129da7547SMikolaj Golub readstdin = 1; 242e8eb82a8SPeter Wemm for (;;) { 24329da7547SMikolaj Golub FD_ZERO(&rfd); 244e8eb82a8SPeter Wemm FD_SET(master, &rfd); 24529da7547SMikolaj Golub if (readstdin) 246e8eb82a8SPeter Wemm FD_SET(STDIN_FILENO, &rfd); 24729f4384aSMikolaj Golub if (!readstdin && ttyflg) { 24829f4384aSMikolaj Golub tv.tv_sec = 1; 249e8eb82a8SPeter Wemm tv.tv_usec = 0; 25029da7547SMikolaj Golub tvp = &tv; 25129da7547SMikolaj Golub readstdin = 1; 25229f4384aSMikolaj Golub } else if (flushtime > 0) { 25329f4384aSMikolaj Golub tv.tv_sec = flushtime - (tvec - start); 25429f4384aSMikolaj Golub tv.tv_usec = 0; 25529f4384aSMikolaj Golub tvp = &tv; 25629da7547SMikolaj Golub } else { 25729da7547SMikolaj Golub tvp = NULL; 258e8eb82a8SPeter Wemm } 259e8eb82a8SPeter Wemm n = select(master + 1, &rfd, 0, 0, tvp); 260e8eb82a8SPeter Wemm if (n < 0 && errno != EINTR) 261e8eb82a8SPeter Wemm break; 262e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { 263e8eb82a8SPeter Wemm cc = read(STDIN_FILENO, ibuf, BUFSIZ); 264d6a68195SColin Percival if (cc < 0) 265e8eb82a8SPeter Wemm break; 26629da7547SMikolaj Golub if (cc == 0) { 26729da7547SMikolaj Golub if (tcgetattr(master, &stt) == 0 && 26829da7547SMikolaj Golub (stt.c_lflag & ICANON) != 0) { 26929da7547SMikolaj Golub (void)write(master, &stt.c_cc[VEOF], 1); 27029da7547SMikolaj Golub } 27129da7547SMikolaj Golub readstdin = 0; 27229da7547SMikolaj Golub } 273e8eb82a8SPeter Wemm if (cc > 0) { 274df53360cSBrian Somers if (rawout) 275df53360cSBrian Somers record(fscript, ibuf, cc, 'i'); 2769b50d902SRodney W. Grimes (void)write(master, ibuf, cc); 277e8eb82a8SPeter Wemm if (kflg && tcgetattr(master, &stt) >= 0 && 278e8eb82a8SPeter Wemm ((stt.c_lflag & ECHO) == 0)) { 279e8eb82a8SPeter Wemm (void)fwrite(ibuf, 1, cc, fscript); 280e8eb82a8SPeter Wemm } 281e8eb82a8SPeter Wemm } 282e8eb82a8SPeter Wemm } 283e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(master, &rfd)) { 284e8eb82a8SPeter Wemm cc = read(master, obuf, sizeof (obuf)); 285e8eb82a8SPeter Wemm if (cc <= 0) 286e8eb82a8SPeter Wemm break; 287e1b4d8d0SSheldon Hearn (void)write(STDOUT_FILENO, obuf, cc); 288df53360cSBrian Somers if (rawout) 289df53360cSBrian Somers record(fscript, obuf, cc, 'o'); 290df53360cSBrian Somers else 291e8eb82a8SPeter Wemm (void)fwrite(obuf, 1, cc, fscript); 292e8eb82a8SPeter Wemm } 293e8eb82a8SPeter Wemm tvec = time(0); 294e8eb82a8SPeter Wemm if (tvec - start >= flushtime) { 295e8eb82a8SPeter Wemm fflush(fscript); 296e8eb82a8SPeter Wemm start = tvec; 297e8eb82a8SPeter Wemm } 2988d105abcSTom Rhodes if (Fflg) 2998d105abcSTom Rhodes fflush(fscript); 300e8eb82a8SPeter Wemm } 301b1c66970SDag-Erling Smørgrav finish(); 302b1c66970SDag-Erling Smørgrav done(0); 3039b50d902SRodney W. Grimes } 3049b50d902SRodney W. Grimes 305236d2f55SPhilippe Charnier static void 306f4ac32deSDavid Malone usage(void) 307236d2f55SPhilippe Charnier { 308e8eb82a8SPeter Wemm (void)fprintf(stderr, 3096cff4e07SDavid E. O'Brien "usage: script [-adfkpqr] [-t time] [file [command ...]]\n"); 310236d2f55SPhilippe Charnier exit(1); 311236d2f55SPhilippe Charnier } 312236d2f55SPhilippe Charnier 313e8efeec6SEd Schouten static void 314f4ac32deSDavid Malone finish(void) 3159b50d902SRodney W. Grimes { 316e1e9ba33SEd Schouten int e, status; 3179b50d902SRodney W. Grimes 318e1e9ba33SEd Schouten if (waitpid(child, &status, 0) == child) { 319b1c66970SDag-Erling Smørgrav if (WIFEXITED(status)) 320b1c66970SDag-Erling Smørgrav e = WEXITSTATUS(status); 321b1c66970SDag-Erling Smørgrav else if (WIFSIGNALED(status)) 322b1c66970SDag-Erling Smørgrav e = WTERMSIG(status); 323b1c66970SDag-Erling Smørgrav else /* can't happen */ 324b1c66970SDag-Erling Smørgrav e = 1; 325b1c66970SDag-Erling Smørgrav done(e); 3269b50d902SRodney W. Grimes } 327e1e9ba33SEd Schouten } 3289b50d902SRodney W. Grimes 329e8efeec6SEd Schouten static void 330f4ac32deSDavid Malone doshell(char **av) 3319b50d902SRodney W. Grimes { 332b139689cSDavid Malone const char *shell; 3339b50d902SRodney W. Grimes 3349b50d902SRodney W. Grimes shell = getenv("SHELL"); 3359b50d902SRodney W. Grimes if (shell == NULL) 3369b50d902SRodney W. Grimes shell = _PATH_BSHELL; 3379b50d902SRodney W. Grimes 3389b50d902SRodney W. Grimes (void)close(master); 3399b50d902SRodney W. Grimes (void)fclose(fscript); 3406cff4e07SDavid E. O'Brien free(fmfname); 3419b50d902SRodney W. Grimes login_tty(slave); 3429b91846cSDavid E. O'Brien setenv("SCRIPT", fname, 1); 343b1c66970SDag-Erling Smørgrav if (av[0]) { 3440e604e98SPeter Wemm execvp(av[0], av); 345b7ffba17SKris Kennaway warn("%s", av[0]); 346b1c66970SDag-Erling Smørgrav } else { 3477bc6d015SBrian Somers execl(shell, shell, "-i", (char *)NULL); 348b7ffba17SKris Kennaway warn("%s", shell); 349b1c66970SDag-Erling Smørgrav } 3509b50d902SRodney W. Grimes fail(); 3519b50d902SRodney W. Grimes } 3529b50d902SRodney W. Grimes 353e8efeec6SEd Schouten static void 354f4ac32deSDavid Malone fail(void) 3559b50d902SRodney W. Grimes { 3569b50d902SRodney W. Grimes (void)kill(0, SIGTERM); 357b1c66970SDag-Erling Smørgrav done(1); 3589b50d902SRodney W. Grimes } 3599b50d902SRodney W. Grimes 360e8efeec6SEd Schouten static void 361f4ac32deSDavid Malone done(int eno) 3629b50d902SRodney W. Grimes { 3639b50d902SRodney W. Grimes time_t tvec; 3649b50d902SRodney W. Grimes 365e292a0e3SColin Percival if (ttyflg) 366e8eb82a8SPeter Wemm (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 3679b50d902SRodney W. Grimes tvec = time(NULL); 368df53360cSBrian Somers if (rawout) 369df53360cSBrian Somers record(fscript, NULL, 0, 'e'); 370e8eb82a8SPeter Wemm if (!qflg) { 371df53360cSBrian Somers if (!rawout) 372df53360cSBrian Somers (void)fprintf(fscript,"\nScript done on %s", 373df53360cSBrian Somers ctime(&tvec)); 374e8eb82a8SPeter Wemm (void)printf("\nScript done, output file is %s\n", fname); 3756cff4e07SDavid E. O'Brien if (fflg) { 3766cff4e07SDavid E. O'Brien (void)printf("Filemon done, output file is %s\n", 3776cff4e07SDavid E. O'Brien fmfname); 3786cff4e07SDavid E. O'Brien } 379e8eb82a8SPeter Wemm } 3809b50d902SRodney W. Grimes (void)fclose(fscript); 3819b50d902SRodney W. Grimes (void)close(master); 382b1c66970SDag-Erling Smørgrav exit(eno); 3839b50d902SRodney W. Grimes } 384df53360cSBrian Somers 385df53360cSBrian Somers static void 386df53360cSBrian Somers record(FILE *fp, char *buf, size_t cc, int direction) 387df53360cSBrian Somers { 388df53360cSBrian Somers struct iovec iov[2]; 389df53360cSBrian Somers struct stamp stamp; 390df53360cSBrian Somers struct timeval tv; 391df53360cSBrian Somers 392df53360cSBrian Somers (void)gettimeofday(&tv, NULL); 393df53360cSBrian Somers stamp.scr_len = cc; 394df53360cSBrian Somers stamp.scr_sec = tv.tv_sec; 395df53360cSBrian Somers stamp.scr_usec = tv.tv_usec; 396df53360cSBrian Somers stamp.scr_direction = direction; 397df53360cSBrian Somers iov[0].iov_len = sizeof(stamp); 398df53360cSBrian Somers iov[0].iov_base = &stamp; 399df53360cSBrian Somers iov[1].iov_len = cc; 400df53360cSBrian Somers iov[1].iov_base = buf; 401df53360cSBrian Somers if (writev(fileno(fp), &iov[0], 2) == -1) 402df53360cSBrian Somers err(1, "writev"); 403df53360cSBrian Somers } 404df53360cSBrian Somers 405df53360cSBrian Somers static void 406df53360cSBrian Somers consume(FILE *fp, off_t len, char *buf, int reg) 407df53360cSBrian Somers { 408df53360cSBrian Somers size_t l; 409df53360cSBrian Somers 410df53360cSBrian Somers if (reg) { 411df53360cSBrian Somers if (fseeko(fp, len, SEEK_CUR) == -1) 412df53360cSBrian Somers err(1, NULL); 413df53360cSBrian Somers } 414df53360cSBrian Somers else { 415df53360cSBrian Somers while (len > 0) { 416df53360cSBrian Somers l = MIN(DEF_BUF, len); 417df53360cSBrian Somers if (fread(buf, sizeof(char), l, fp) != l) 418df53360cSBrian Somers err(1, "cannot read buffer"); 419df53360cSBrian Somers len -= l; 420df53360cSBrian Somers } 421df53360cSBrian Somers } 422df53360cSBrian Somers } 423df53360cSBrian Somers 424df53360cSBrian Somers #define swapstamp(stamp) do { \ 425df53360cSBrian Somers if (stamp.scr_direction > 0xff) { \ 426df53360cSBrian Somers stamp.scr_len = bswap64(stamp.scr_len); \ 427df53360cSBrian Somers stamp.scr_sec = bswap64(stamp.scr_sec); \ 428df53360cSBrian Somers stamp.scr_usec = bswap32(stamp.scr_usec); \ 429df53360cSBrian Somers stamp.scr_direction = bswap32(stamp.scr_direction); \ 430df53360cSBrian Somers } \ 431df53360cSBrian Somers } while (0/*CONSTCOND*/) 432df53360cSBrian Somers 433df53360cSBrian Somers static void 434df53360cSBrian Somers playback(FILE *fp) 435df53360cSBrian Somers { 436df53360cSBrian Somers struct timespec tsi, tso; 437df53360cSBrian Somers struct stamp stamp; 438df53360cSBrian Somers struct stat pst; 439df53360cSBrian Somers char buf[DEF_BUF]; 440df53360cSBrian Somers off_t nread, save_len; 441df53360cSBrian Somers size_t l; 442df53360cSBrian Somers time_t tclock; 443df53360cSBrian Somers int reg; 444df53360cSBrian Somers 445df53360cSBrian Somers if (fstat(fileno(fp), &pst) == -1) 446df53360cSBrian Somers err(1, "fstat failed"); 447df53360cSBrian Somers 448df53360cSBrian Somers reg = S_ISREG(pst.st_mode); 449df53360cSBrian Somers 450df53360cSBrian Somers for (nread = 0; !reg || nread < pst.st_size; nread += save_len) { 451df53360cSBrian Somers if (fread(&stamp, sizeof(stamp), 1, fp) != 1) { 452df53360cSBrian Somers if (reg) 453df53360cSBrian Somers err(1, "reading playback header"); 454df53360cSBrian Somers else 455df53360cSBrian Somers break; 456df53360cSBrian Somers } 457df53360cSBrian Somers swapstamp(stamp); 458df53360cSBrian Somers save_len = sizeof(stamp); 459df53360cSBrian Somers 460df53360cSBrian Somers if (reg && stamp.scr_len > 461df53360cSBrian Somers (uint64_t)(pst.st_size - save_len) - nread) 462df53360cSBrian Somers errx(1, "invalid stamp"); 463df53360cSBrian Somers 464df53360cSBrian Somers save_len += stamp.scr_len; 465df53360cSBrian Somers tclock = stamp.scr_sec; 466df53360cSBrian Somers tso.tv_sec = stamp.scr_sec; 467df53360cSBrian Somers tso.tv_nsec = stamp.scr_usec * 1000; 468df53360cSBrian Somers 469df53360cSBrian Somers switch (stamp.scr_direction) { 470df53360cSBrian Somers case 's': 471df53360cSBrian Somers if (!qflg) 472df53360cSBrian Somers (void)printf("Script started on %s", 473df53360cSBrian Somers ctime(&tclock)); 474df53360cSBrian Somers tsi = tso; 475df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg); 476df53360cSBrian Somers break; 477df53360cSBrian Somers case 'e': 478df53360cSBrian Somers if (!qflg) 479df53360cSBrian Somers (void)printf("\nScript done on %s", 480df53360cSBrian Somers ctime(&tclock)); 481df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg); 482df53360cSBrian Somers break; 483df53360cSBrian Somers case 'i': 484df53360cSBrian Somers /* throw input away */ 485df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg); 486df53360cSBrian Somers break; 487df53360cSBrian Somers case 'o': 488df53360cSBrian Somers tsi.tv_sec = tso.tv_sec - tsi.tv_sec; 489df53360cSBrian Somers tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec; 490df53360cSBrian Somers if (tsi.tv_nsec < 0) { 491df53360cSBrian Somers tsi.tv_sec -= 1; 492df53360cSBrian Somers tsi.tv_nsec += 1000000000; 493df53360cSBrian Somers } 494df53360cSBrian Somers if (usesleep) 495df53360cSBrian Somers (void)nanosleep(&tsi, NULL); 496df53360cSBrian Somers tsi = tso; 497df53360cSBrian Somers while (stamp.scr_len > 0) { 498df53360cSBrian Somers l = MIN(DEF_BUF, stamp.scr_len); 499df53360cSBrian Somers if (fread(buf, sizeof(char), l, fp) != l) 500df53360cSBrian Somers err(1, "cannot read buffer"); 501df53360cSBrian Somers 502df53360cSBrian Somers (void)write(STDOUT_FILENO, buf, l); 503df53360cSBrian Somers stamp.scr_len -= l; 504df53360cSBrian Somers } 505df53360cSBrian Somers break; 506df53360cSBrian Somers default: 507df53360cSBrian Somers errx(1, "invalid direction"); 508df53360cSBrian Somers } 509df53360cSBrian Somers } 510df53360cSBrian Somers (void)fclose(fp); 511df53360cSBrian Somers exit(0); 512df53360cSBrian Somers } 513