19b50d902SRodney W. Grimes /* 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 46cff4e07SDavid E. O'Brien * Copyright (c) 2010, 2012 David E. O'Brien 59b50d902SRodney W. Grimes * Copyright (c) 1980, 1992, 1993 69b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 79b50d902SRodney W. Grimes * 89b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 99b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 109b50d902SRodney W. Grimes * are met: 119b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 139b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 159b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 179b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 189b50d902SRodney W. Grimes * without specific prior written permission. 199b50d902SRodney W. Grimes * 209b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 219b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 229b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 239b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 249b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 259b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 269b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 279b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 289b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 299b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 309b50d902SRodney W. Grimes * SUCH DAMAGE. 319b50d902SRodney W. Grimes */ 329b50d902SRodney W. Grimes 3313f02f28SDavid E. O'Brien #include <sys/param.h> 34d20f95e1SMark Murray __FBSDID("$FreeBSD$"); 359b50d902SRodney W. Grimes #ifndef lint 36236d2f55SPhilippe Charnier static const char copyright[] = 379b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1992, 1993\n\ 389b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 39d20f95e1SMark Murray #endif 409b50d902SRodney W. Grimes #ifndef lint 41d20f95e1SMark Murray static const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 42236d2f55SPhilippe Charnier #endif 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #include <sys/wait.h> 459b50d902SRodney W. Grimes #include <sys/stat.h> 469b50d902SRodney W. Grimes #include <sys/ioctl.h> 479b50d902SRodney W. Grimes #include <sys/time.h> 48c0ba4c2eSKonstantin Belousov #include <sys/queue.h> 49df53360cSBrian Somers #include <sys/uio.h> 50df53360cSBrian Somers #include <sys/endian.h> 516cff4e07SDavid E. O'Brien #include <dev/filemon/filemon.h> 529b50d902SRodney W. Grimes 53236d2f55SPhilippe Charnier #include <err.h> 54e8eb82a8SPeter Wemm #include <errno.h> 559b50d902SRodney W. Grimes #include <fcntl.h> 56236d2f55SPhilippe Charnier #include <libutil.h> 579b50d902SRodney W. Grimes #include <paths.h> 589b50d902SRodney W. Grimes #include <signal.h> 599b50d902SRodney W. Grimes #include <stdio.h> 609b50d902SRodney W. Grimes #include <stdlib.h> 619b50d902SRodney W. Grimes #include <string.h> 629b50d902SRodney W. Grimes #include <termios.h> 639b50d902SRodney W. Grimes #include <unistd.h> 649b50d902SRodney W. Grimes 65df53360cSBrian Somers #define DEF_BUF 65536 66df53360cSBrian Somers 67df53360cSBrian Somers struct stamp { 68df53360cSBrian Somers uint64_t scr_len; /* amount of data */ 69df53360cSBrian Somers uint64_t scr_sec; /* time it arrived in seconds... */ 70df53360cSBrian Somers uint32_t scr_usec; /* ...and microseconds */ 71df53360cSBrian Somers uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */ 72df53360cSBrian Somers }; 73df53360cSBrian Somers 74c0ba4c2eSKonstantin Belousov struct buf_elm { 75c0ba4c2eSKonstantin Belousov TAILQ_ENTRY(buf_elm) link; 76c0ba4c2eSKonstantin Belousov int rpos; 77c0ba4c2eSKonstantin Belousov int len; 78c0ba4c2eSKonstantin Belousov char ibuf[]; 79c0ba4c2eSKonstantin Belousov }; 80c0ba4c2eSKonstantin Belousov 81e8efeec6SEd Schouten static FILE *fscript; 82e8efeec6SEd Schouten static int master, slave; 83e8efeec6SEd Schouten static int child; 84e8efeec6SEd Schouten static const char *fname; 856cff4e07SDavid E. O'Brien static char *fmfname; 866cff4e07SDavid E. O'Brien static int fflg, qflg, ttyflg; 8766d93438SBryan Drewery static int usesleep, rawout, showexit; 88c0ba4c2eSKonstantin Belousov static TAILQ_HEAD(, buf_elm) obuf_list = TAILQ_HEAD_INITIALIZER(obuf_list); 899b50d902SRodney W. Grimes 90e8efeec6SEd Schouten static struct termios tt; 919b50d902SRodney W. Grimes 92*6c4afed5SSimon J. Gerraty #ifndef TSTAMP_FMT 93*6c4afed5SSimon J. Gerraty /* useful for tool and human reading */ 94*6c4afed5SSimon J. Gerraty # define TSTAMP_FMT "%n@ %s [%Y-%m-%d %T] " 95*6c4afed5SSimon J. Gerraty #endif 96*6c4afed5SSimon J. Gerraty static const char *tstamp_fmt = TSTAMP_FMT; 97*6c4afed5SSimon J. Gerraty static int tflg; 98*6c4afed5SSimon J. Gerraty 99e8efeec6SEd Schouten static void done(int) __dead2; 100e8efeec6SEd Schouten static void doshell(char **); 101e8efeec6SEd Schouten static void finish(void); 102df53360cSBrian Somers static void record(FILE *, char *, size_t, int); 103df53360cSBrian Somers static void consume(FILE *, off_t, char *, int); 104df53360cSBrian Somers static void playback(FILE *) __dead2; 1053f330d7dSWarner Losh static void usage(void); 1069b50d902SRodney W. Grimes 1079b50d902SRodney W. Grimes int 108f4ac32deSDavid Malone main(int argc, char *argv[]) 1099b50d902SRodney W. Grimes { 110d20f95e1SMark Murray int cc; 111e8eb82a8SPeter Wemm struct termios rtt, stt; 1129b50d902SRodney W. Grimes struct winsize win; 113e8eb82a8SPeter Wemm struct timeval tv, *tvp; 114e8eb82a8SPeter Wemm time_t tvec, start; 115e8eb82a8SPeter Wemm char obuf[BUFSIZ]; 1169b50d902SRodney W. Grimes char ibuf[BUFSIZ]; 117c0ba4c2eSKonstantin Belousov fd_set rfd, wfd; 118c0ba4c2eSKonstantin Belousov struct buf_elm *be; 119c0ba4c2eSKonstantin Belousov int aflg, Fflg, kflg, pflg, ch, k, n, fcm; 1206cff4e07SDavid E. O'Brien int flushtime, readstdin; 1216cff4e07SDavid E. O'Brien int fm_fd, fm_log; 1229b50d902SRodney W. Grimes 1238d105abcSTom Rhodes aflg = Fflg = kflg = pflg = 0; 124df53360cSBrian Somers usesleep = 1; 125df53360cSBrian Somers rawout = 0; 1266cff4e07SDavid E. O'Brien flushtime = 30; 1276cff4e07SDavid E. O'Brien fm_fd = -1; /* Shut up stupid "may be used uninitialized" GCC 1286cff4e07SDavid E. O'Brien warning. (not needed w/clang) */ 12966d93438SBryan Drewery showexit = 0; 130df53360cSBrian Somers 131*6c4afed5SSimon J. Gerraty while ((ch = getopt(argc, argv, "adeFfkpqrT:t:")) != -1) 1329b50d902SRodney W. Grimes switch(ch) { 1339b50d902SRodney W. Grimes case 'a': 1349b50d902SRodney W. Grimes aflg = 1; 1359b50d902SRodney W. Grimes break; 136df53360cSBrian Somers case 'd': 137df53360cSBrian Somers usesleep = 0; 13851afb8dfSPeter Wemm break; 13984961358SWarner Losh case 'e': /* Default behavior, accepted for linux compat */ 140f9177b6cSWarner Losh break; 1418d105abcSTom Rhodes case 'F': 1428d105abcSTom Rhodes Fflg = 1; 1438d105abcSTom Rhodes break; 1446cff4e07SDavid E. O'Brien case 'f': 1456cff4e07SDavid E. O'Brien fflg = 1; 1466cff4e07SDavid E. O'Brien break; 147e8eb82a8SPeter Wemm case 'k': 148e8eb82a8SPeter Wemm kflg = 1; 149e8eb82a8SPeter Wemm break; 150df53360cSBrian Somers case 'p': 151df53360cSBrian Somers pflg = 1; 152df53360cSBrian Somers break; 153df53360cSBrian Somers case 'q': 154df53360cSBrian Somers qflg = 1; 155df53360cSBrian Somers break; 156df53360cSBrian Somers case 'r': 157df53360cSBrian Somers rawout = 1; 158df53360cSBrian Somers break; 159e8eb82a8SPeter Wemm case 't': 160e8eb82a8SPeter Wemm flushtime = atoi(optarg); 161e8eb82a8SPeter Wemm if (flushtime < 0) 162e8eb82a8SPeter Wemm err(1, "invalid flush time %d", flushtime); 163e8eb82a8SPeter Wemm break; 164*6c4afed5SSimon J. Gerraty case 'T': 165*6c4afed5SSimon J. Gerraty tflg = pflg = 1; 166*6c4afed5SSimon J. Gerraty if (strchr(optarg, '%')) 167*6c4afed5SSimon J. Gerraty tstamp_fmt = optarg; 168*6c4afed5SSimon J. Gerraty break; 1699b50d902SRodney W. Grimes case '?': 1709b50d902SRodney W. Grimes default: 171236d2f55SPhilippe Charnier usage(); 1729b50d902SRodney W. Grimes } 1739b50d902SRodney W. Grimes argc -= optind; 1749b50d902SRodney W. Grimes argv += optind; 1759b50d902SRodney W. Grimes 17651afb8dfSPeter Wemm if (argc > 0) { 1779b50d902SRodney W. Grimes fname = argv[0]; 17851afb8dfSPeter Wemm argv++; 17951afb8dfSPeter Wemm argc--; 18051afb8dfSPeter Wemm } else 1819b50d902SRodney W. Grimes fname = "typescript"; 1829b50d902SRodney W. Grimes 183df53360cSBrian Somers if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL) 184236d2f55SPhilippe Charnier err(1, "%s", fname); 1859b50d902SRodney W. Grimes 1866cff4e07SDavid E. O'Brien if (fflg) { 1876cff4e07SDavid E. O'Brien asprintf(&fmfname, "%s.filemon", fname); 1886cff4e07SDavid E. O'Brien if (!fmfname) 1896cff4e07SDavid E. O'Brien err(1, "%s.filemon", fname); 19026e736b2SBaptiste Daroussin if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1) 1916cff4e07SDavid E. O'Brien err(1, "open(\"/dev/filemon\", O_RDWR)"); 19226e736b2SBaptiste Daroussin if ((fm_log = open(fmfname, 19326e736b2SBaptiste Daroussin O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 1946cff4e07SDavid E. O'Brien S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) 1956cff4e07SDavid E. O'Brien err(1, "open(%s)", fmfname); 1966cff4e07SDavid E. O'Brien if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0) 1976cff4e07SDavid E. O'Brien err(1, "Cannot set filemon log file descriptor"); 1986cff4e07SDavid E. O'Brien } 1996cff4e07SDavid E. O'Brien 200df53360cSBrian Somers if (pflg) 201df53360cSBrian Somers playback(fscript); 202df53360cSBrian Somers 2038bc30d2aSMark Johnston if (tcgetattr(STDIN_FILENO, &tt) == -1 || 2048bc30d2aSMark Johnston ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) { 2058bc30d2aSMark Johnston if (errno != ENOTTY) /* For debugger. */ 2068bc30d2aSMark Johnston err(1, "tcgetattr/ioctl"); 207e292a0e3SColin Percival if (openpty(&master, &slave, NULL, NULL, NULL) == -1) 208e292a0e3SColin Percival err(1, "openpty"); 2098bc30d2aSMark Johnston } else { 2108bc30d2aSMark Johnston if (openpty(&master, &slave, NULL, &tt, &win) == -1) 2118bc30d2aSMark Johnston err(1, "openpty"); 2128bc30d2aSMark Johnston ttyflg = 1; 213e292a0e3SColin Percival } 214c0ba4c2eSKonstantin Belousov fcm = fcntl(master, F_GETFL); 215c0ba4c2eSKonstantin Belousov if (fcm == -1) 216c0ba4c2eSKonstantin Belousov err(1, "master F_GETFL"); 217c0ba4c2eSKonstantin Belousov fcm |= O_NONBLOCK; 218c0ba4c2eSKonstantin Belousov if (fcntl(master, F_SETFL, fcm) == -1) 219c0ba4c2eSKonstantin Belousov err(1, "master F_SETFL"); 2209b50d902SRodney W. Grimes 221df53360cSBrian Somers if (rawout) 222df53360cSBrian Somers record(fscript, NULL, 0, 's'); 223df53360cSBrian Somers 224e8eb82a8SPeter Wemm if (!qflg) { 225e8eb82a8SPeter Wemm tvec = time(NULL); 2269b50d902SRodney W. Grimes (void)printf("Script started, output file is %s\n", fname); 227df53360cSBrian Somers if (!rawout) { 228df53360cSBrian Somers (void)fprintf(fscript, "Script started on %s", 229df53360cSBrian Somers ctime(&tvec)); 230df53360cSBrian Somers if (argv[0]) { 23166d93438SBryan Drewery showexit = 1; 23266d93438SBryan Drewery fprintf(fscript, "Command: "); 233df53360cSBrian Somers for (k = 0 ; argv[k] ; ++k) 234df53360cSBrian Somers fprintf(fscript, "%s%s", k ? " " : "", 235df53360cSBrian Somers argv[k]); 236df53360cSBrian Somers fprintf(fscript, "\n"); 237df53360cSBrian Somers } 238df53360cSBrian Somers } 239e8eb82a8SPeter Wemm fflush(fscript); 2406cff4e07SDavid E. O'Brien if (fflg) { 2416cff4e07SDavid E. O'Brien (void)printf("Filemon started, output file is %s\n", 2426cff4e07SDavid E. O'Brien fmfname); 2436cff4e07SDavid E. O'Brien } 244e8eb82a8SPeter Wemm } 245e292a0e3SColin Percival if (ttyflg) { 2469b50d902SRodney W. Grimes rtt = tt; 2479b50d902SRodney W. Grimes cfmakeraw(&rtt); 2489b50d902SRodney W. Grimes rtt.c_lflag &= ~ECHO; 2499b50d902SRodney W. Grimes (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 250e292a0e3SColin Percival } 2519b50d902SRodney W. Grimes 2529b50d902SRodney W. Grimes child = fork(); 2539b50d902SRodney W. Grimes if (child < 0) { 254236d2f55SPhilippe Charnier warn("fork"); 255b1c66970SDag-Erling Smørgrav done(1); 2569b50d902SRodney W. Grimes } 2577b245cb8SBryan Drewery if (child == 0) { 2587b245cb8SBryan Drewery if (fflg) { 2597b245cb8SBryan Drewery int pid; 2609b50d902SRodney W. Grimes 2617b245cb8SBryan Drewery pid = getpid(); 2627b245cb8SBryan Drewery if (ioctl(fm_fd, FILEMON_SET_PID, &pid) < 0) 2636cff4e07SDavid E. O'Brien err(1, "Cannot set filemon PID"); 2647b245cb8SBryan Drewery } 2657b245cb8SBryan Drewery 2667b245cb8SBryan Drewery doshell(argv); 2677b245cb8SBryan Drewery } 2687b245cb8SBryan Drewery close(slave); 2696cff4e07SDavid E. O'Brien 27029da7547SMikolaj Golub start = tvec = time(0); 27129da7547SMikolaj Golub readstdin = 1; 272e8eb82a8SPeter Wemm for (;;) { 27329da7547SMikolaj Golub FD_ZERO(&rfd); 274c0ba4c2eSKonstantin Belousov FD_ZERO(&wfd); 275e8eb82a8SPeter Wemm FD_SET(master, &rfd); 27629da7547SMikolaj Golub if (readstdin) 277e8eb82a8SPeter Wemm FD_SET(STDIN_FILENO, &rfd); 278c0ba4c2eSKonstantin Belousov if (!TAILQ_EMPTY(&obuf_list)) 279c0ba4c2eSKonstantin Belousov FD_SET(master, &wfd); 28029f4384aSMikolaj Golub if (!readstdin && ttyflg) { 28129f4384aSMikolaj Golub tv.tv_sec = 1; 282e8eb82a8SPeter Wemm tv.tv_usec = 0; 28329da7547SMikolaj Golub tvp = &tv; 28429da7547SMikolaj Golub readstdin = 1; 28529f4384aSMikolaj Golub } else if (flushtime > 0) { 28629f4384aSMikolaj Golub tv.tv_sec = flushtime - (tvec - start); 28729f4384aSMikolaj Golub tv.tv_usec = 0; 28829f4384aSMikolaj Golub tvp = &tv; 28929da7547SMikolaj Golub } else { 29029da7547SMikolaj Golub tvp = NULL; 291e8eb82a8SPeter Wemm } 292c0ba4c2eSKonstantin Belousov n = select(master + 1, &rfd, &wfd, NULL, tvp); 293e8eb82a8SPeter Wemm if (n < 0 && errno != EINTR) 294e8eb82a8SPeter Wemm break; 295e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { 296e8eb82a8SPeter Wemm cc = read(STDIN_FILENO, ibuf, BUFSIZ); 297d6a68195SColin Percival if (cc < 0) 298e8eb82a8SPeter Wemm break; 29929da7547SMikolaj Golub if (cc == 0) { 30029da7547SMikolaj Golub if (tcgetattr(master, &stt) == 0 && 30129da7547SMikolaj Golub (stt.c_lflag & ICANON) != 0) { 30229da7547SMikolaj Golub (void)write(master, &stt.c_cc[VEOF], 1); 30329da7547SMikolaj Golub } 30429da7547SMikolaj Golub readstdin = 0; 30529da7547SMikolaj Golub } 306e8eb82a8SPeter Wemm if (cc > 0) { 307df53360cSBrian Somers if (rawout) 308df53360cSBrian Somers record(fscript, ibuf, cc, 'i'); 309c0ba4c2eSKonstantin Belousov be = malloc(sizeof(*be) + cc); 310c0ba4c2eSKonstantin Belousov be->rpos = 0; 311c0ba4c2eSKonstantin Belousov be->len = cc; 312c0ba4c2eSKonstantin Belousov memcpy(be->ibuf, ibuf, cc); 313c0ba4c2eSKonstantin Belousov TAILQ_INSERT_TAIL(&obuf_list, be, link); 314c0ba4c2eSKonstantin Belousov } 315c0ba4c2eSKonstantin Belousov } 316c0ba4c2eSKonstantin Belousov if (n > 0 && FD_ISSET(master, &wfd)) { 317c0ba4c2eSKonstantin Belousov while ((be = TAILQ_FIRST(&obuf_list)) != NULL) { 318c0ba4c2eSKonstantin Belousov cc = write(master, be->ibuf + be->rpos, 319c0ba4c2eSKonstantin Belousov be->len); 320c0ba4c2eSKonstantin Belousov if (cc == -1) { 321c0ba4c2eSKonstantin Belousov if (errno == EWOULDBLOCK || 322c0ba4c2eSKonstantin Belousov errno == EINTR) 323c0ba4c2eSKonstantin Belousov break; 324c0ba4c2eSKonstantin Belousov warn("write master"); 325c0ba4c2eSKonstantin Belousov done(1); 326c0ba4c2eSKonstantin Belousov } 327c0ba4c2eSKonstantin Belousov if (cc == 0) 328c0ba4c2eSKonstantin Belousov break; /* retry later ? */ 329e8eb82a8SPeter Wemm if (kflg && tcgetattr(master, &stt) >= 0 && 330e8eb82a8SPeter Wemm ((stt.c_lflag & ECHO) == 0)) { 331c0ba4c2eSKonstantin Belousov (void)fwrite(be->ibuf + be->rpos, 332c0ba4c2eSKonstantin Belousov 1, cc, fscript); 333c0ba4c2eSKonstantin Belousov } 334c0ba4c2eSKonstantin Belousov be->len -= cc; 335c0ba4c2eSKonstantin Belousov if (be->len == 0) { 336c0ba4c2eSKonstantin Belousov TAILQ_REMOVE(&obuf_list, be, link); 337c0ba4c2eSKonstantin Belousov free(be); 338c0ba4c2eSKonstantin Belousov } else { 339c0ba4c2eSKonstantin Belousov be->rpos += cc; 340e8eb82a8SPeter Wemm } 341e8eb82a8SPeter Wemm } 342e8eb82a8SPeter Wemm } 343e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(master, &rfd)) { 344e8eb82a8SPeter Wemm cc = read(master, obuf, sizeof (obuf)); 345e8eb82a8SPeter Wemm if (cc <= 0) 346e8eb82a8SPeter Wemm break; 347e1b4d8d0SSheldon Hearn (void)write(STDOUT_FILENO, obuf, cc); 348df53360cSBrian Somers if (rawout) 349df53360cSBrian Somers record(fscript, obuf, cc, 'o'); 350df53360cSBrian Somers else 351e8eb82a8SPeter Wemm (void)fwrite(obuf, 1, cc, fscript); 352e8eb82a8SPeter Wemm } 353e8eb82a8SPeter Wemm tvec = time(0); 354e8eb82a8SPeter Wemm if (tvec - start >= flushtime) { 355e8eb82a8SPeter Wemm fflush(fscript); 356e8eb82a8SPeter Wemm start = tvec; 357e8eb82a8SPeter Wemm } 3588d105abcSTom Rhodes if (Fflg) 3598d105abcSTom Rhodes fflush(fscript); 360e8eb82a8SPeter Wemm } 361b1c66970SDag-Erling Smørgrav finish(); 362b1c66970SDag-Erling Smørgrav done(0); 3639b50d902SRodney W. Grimes } 3649b50d902SRodney W. Grimes 365236d2f55SPhilippe Charnier static void 366f4ac32deSDavid Malone usage(void) 367236d2f55SPhilippe Charnier { 368e8eb82a8SPeter Wemm (void)fprintf(stderr, 3696cff4e07SDavid E. O'Brien "usage: script [-adfkpqr] [-t time] [file [command ...]]\n"); 370236d2f55SPhilippe Charnier exit(1); 371236d2f55SPhilippe Charnier } 372236d2f55SPhilippe Charnier 373e8efeec6SEd Schouten static void 374f4ac32deSDavid Malone finish(void) 3759b50d902SRodney W. Grimes { 376e1e9ba33SEd Schouten int e, status; 3779b50d902SRodney W. Grimes 378e1e9ba33SEd Schouten if (waitpid(child, &status, 0) == child) { 379b1c66970SDag-Erling Smørgrav if (WIFEXITED(status)) 380b1c66970SDag-Erling Smørgrav e = WEXITSTATUS(status); 381b1c66970SDag-Erling Smørgrav else if (WIFSIGNALED(status)) 382b1c66970SDag-Erling Smørgrav e = WTERMSIG(status); 383b1c66970SDag-Erling Smørgrav else /* can't happen */ 384b1c66970SDag-Erling Smørgrav e = 1; 385b1c66970SDag-Erling Smørgrav done(e); 3869b50d902SRodney W. Grimes } 387e1e9ba33SEd Schouten } 3889b50d902SRodney W. Grimes 389e8efeec6SEd Schouten static void 390f4ac32deSDavid Malone doshell(char **av) 3919b50d902SRodney W. Grimes { 392b139689cSDavid Malone const char *shell; 3939b50d902SRodney W. Grimes 3949b50d902SRodney W. Grimes shell = getenv("SHELL"); 3959b50d902SRodney W. Grimes if (shell == NULL) 3969b50d902SRodney W. Grimes shell = _PATH_BSHELL; 3979b50d902SRodney W. Grimes 3989b50d902SRodney W. Grimes (void)close(master); 3999b50d902SRodney W. Grimes (void)fclose(fscript); 4006cff4e07SDavid E. O'Brien free(fmfname); 4019b50d902SRodney W. Grimes login_tty(slave); 4029b91846cSDavid E. O'Brien setenv("SCRIPT", fname, 1); 403b1c66970SDag-Erling Smørgrav if (av[0]) { 4040e604e98SPeter Wemm execvp(av[0], av); 405b7ffba17SKris Kennaway warn("%s", av[0]); 406b1c66970SDag-Erling Smørgrav } else { 4077bc6d015SBrian Somers execl(shell, shell, "-i", (char *)NULL); 408b7ffba17SKris Kennaway warn("%s", shell); 409b1c66970SDag-Erling Smørgrav } 41095d13d1bSBryan Drewery exit(1); 4119b50d902SRodney W. Grimes } 4129b50d902SRodney W. Grimes 413e8efeec6SEd Schouten static void 414f4ac32deSDavid Malone done(int eno) 4159b50d902SRodney W. Grimes { 4169b50d902SRodney W. Grimes time_t tvec; 4179b50d902SRodney W. Grimes 418e292a0e3SColin Percival if (ttyflg) 419e8eb82a8SPeter Wemm (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 4209b50d902SRodney W. Grimes tvec = time(NULL); 421df53360cSBrian Somers if (rawout) 422df53360cSBrian Somers record(fscript, NULL, 0, 'e'); 423e8eb82a8SPeter Wemm if (!qflg) { 42466d93438SBryan Drewery if (!rawout) { 42566d93438SBryan Drewery if (showexit) 42666d93438SBryan Drewery (void)fprintf(fscript, "\nCommand exit status:" 42766d93438SBryan Drewery " %d", eno); 428df53360cSBrian Somers (void)fprintf(fscript,"\nScript done on %s", 429df53360cSBrian Somers ctime(&tvec)); 43066d93438SBryan Drewery } 431e8eb82a8SPeter Wemm (void)printf("\nScript done, output file is %s\n", fname); 4326cff4e07SDavid E. O'Brien if (fflg) { 4336cff4e07SDavid E. O'Brien (void)printf("Filemon done, output file is %s\n", 4346cff4e07SDavid E. O'Brien fmfname); 4356cff4e07SDavid E. O'Brien } 436e8eb82a8SPeter Wemm } 4379b50d902SRodney W. Grimes (void)fclose(fscript); 4389b50d902SRodney W. Grimes (void)close(master); 439b1c66970SDag-Erling Smørgrav exit(eno); 4409b50d902SRodney W. Grimes } 441df53360cSBrian Somers 442df53360cSBrian Somers static void 443df53360cSBrian Somers record(FILE *fp, char *buf, size_t cc, int direction) 444df53360cSBrian Somers { 445df53360cSBrian Somers struct iovec iov[2]; 446df53360cSBrian Somers struct stamp stamp; 447df53360cSBrian Somers struct timeval tv; 448df53360cSBrian Somers 449df53360cSBrian Somers (void)gettimeofday(&tv, NULL); 450df53360cSBrian Somers stamp.scr_len = cc; 451df53360cSBrian Somers stamp.scr_sec = tv.tv_sec; 452df53360cSBrian Somers stamp.scr_usec = tv.tv_usec; 453df53360cSBrian Somers stamp.scr_direction = direction; 454df53360cSBrian Somers iov[0].iov_len = sizeof(stamp); 455df53360cSBrian Somers iov[0].iov_base = &stamp; 456df53360cSBrian Somers iov[1].iov_len = cc; 457df53360cSBrian Somers iov[1].iov_base = buf; 458df53360cSBrian Somers if (writev(fileno(fp), &iov[0], 2) == -1) 459df53360cSBrian Somers err(1, "writev"); 460df53360cSBrian Somers } 461df53360cSBrian Somers 462df53360cSBrian Somers static void 463df53360cSBrian Somers consume(FILE *fp, off_t len, char *buf, int reg) 464df53360cSBrian Somers { 465df53360cSBrian Somers size_t l; 466df53360cSBrian Somers 467df53360cSBrian Somers if (reg) { 468df53360cSBrian Somers if (fseeko(fp, len, SEEK_CUR) == -1) 469df53360cSBrian Somers err(1, NULL); 470df53360cSBrian Somers } 471df53360cSBrian Somers else { 472df53360cSBrian Somers while (len > 0) { 473df53360cSBrian Somers l = MIN(DEF_BUF, len); 474df53360cSBrian Somers if (fread(buf, sizeof(char), l, fp) != l) 475df53360cSBrian Somers err(1, "cannot read buffer"); 476df53360cSBrian Somers len -= l; 477df53360cSBrian Somers } 478df53360cSBrian Somers } 479df53360cSBrian Somers } 480df53360cSBrian Somers 481df53360cSBrian Somers #define swapstamp(stamp) do { \ 482df53360cSBrian Somers if (stamp.scr_direction > 0xff) { \ 483df53360cSBrian Somers stamp.scr_len = bswap64(stamp.scr_len); \ 484df53360cSBrian Somers stamp.scr_sec = bswap64(stamp.scr_sec); \ 485df53360cSBrian Somers stamp.scr_usec = bswap32(stamp.scr_usec); \ 486df53360cSBrian Somers stamp.scr_direction = bswap32(stamp.scr_direction); \ 487df53360cSBrian Somers } \ 488df53360cSBrian Somers } while (0/*CONSTCOND*/) 489df53360cSBrian Somers 490df53360cSBrian Somers static void 49169bc4fa9SMark Johnston termset(void) 49269bc4fa9SMark Johnston { 49369bc4fa9SMark Johnston struct termios traw; 49469bc4fa9SMark Johnston 49569bc4fa9SMark Johnston if (tcgetattr(STDOUT_FILENO, &tt) == -1) { 4968bc30d2aSMark Johnston if (errno != ENOTTY) /* For debugger. */ 4978bc30d2aSMark Johnston err(1, "tcgetattr"); 49869bc4fa9SMark Johnston return; 49969bc4fa9SMark Johnston } 50069bc4fa9SMark Johnston ttyflg = 1; 50169bc4fa9SMark Johnston traw = tt; 50269bc4fa9SMark Johnston cfmakeraw(&traw); 50369bc4fa9SMark Johnston traw.c_lflag |= ISIG; 50469bc4fa9SMark Johnston (void)tcsetattr(STDOUT_FILENO, TCSANOW, &traw); 50569bc4fa9SMark Johnston } 50669bc4fa9SMark Johnston 50769bc4fa9SMark Johnston static void 50869bc4fa9SMark Johnston termreset(void) 50969bc4fa9SMark Johnston { 51069bc4fa9SMark Johnston if (ttyflg) { 51169bc4fa9SMark Johnston tcsetattr(STDOUT_FILENO, TCSADRAIN, &tt); 51269bc4fa9SMark Johnston ttyflg = 0; 51369bc4fa9SMark Johnston } 51469bc4fa9SMark Johnston } 51569bc4fa9SMark Johnston 51669bc4fa9SMark Johnston static void 517df53360cSBrian Somers playback(FILE *fp) 518df53360cSBrian Somers { 519df53360cSBrian Somers struct timespec tsi, tso; 520df53360cSBrian Somers struct stamp stamp; 521df53360cSBrian Somers struct stat pst; 522df53360cSBrian Somers char buf[DEF_BUF]; 523df53360cSBrian Somers off_t nread, save_len; 524df53360cSBrian Somers size_t l; 525df53360cSBrian Somers time_t tclock; 526df53360cSBrian Somers int reg; 527df53360cSBrian Somers 528df53360cSBrian Somers if (fstat(fileno(fp), &pst) == -1) 529df53360cSBrian Somers err(1, "fstat failed"); 530df53360cSBrian Somers 531df53360cSBrian Somers reg = S_ISREG(pst.st_mode); 532df53360cSBrian Somers 533df53360cSBrian Somers for (nread = 0; !reg || nread < pst.st_size; nread += save_len) { 534df53360cSBrian Somers if (fread(&stamp, sizeof(stamp), 1, fp) != 1) { 535df53360cSBrian Somers if (reg) 536df53360cSBrian Somers err(1, "reading playback header"); 537df53360cSBrian Somers else 538df53360cSBrian Somers break; 539df53360cSBrian Somers } 540df53360cSBrian Somers swapstamp(stamp); 541df53360cSBrian Somers save_len = sizeof(stamp); 542df53360cSBrian Somers 543df53360cSBrian Somers if (reg && stamp.scr_len > 544df53360cSBrian Somers (uint64_t)(pst.st_size - save_len) - nread) 545df53360cSBrian Somers errx(1, "invalid stamp"); 546df53360cSBrian Somers 547df53360cSBrian Somers save_len += stamp.scr_len; 548df53360cSBrian Somers tclock = stamp.scr_sec; 549df53360cSBrian Somers tso.tv_sec = stamp.scr_sec; 550df53360cSBrian Somers tso.tv_nsec = stamp.scr_usec * 1000; 551df53360cSBrian Somers 552df53360cSBrian Somers switch (stamp.scr_direction) { 553df53360cSBrian Somers case 's': 554df53360cSBrian Somers if (!qflg) 555df53360cSBrian Somers (void)printf("Script started on %s", 556df53360cSBrian Somers ctime(&tclock)); 557df53360cSBrian Somers tsi = tso; 558df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg); 55969bc4fa9SMark Johnston termset(); 56069bc4fa9SMark Johnston atexit(termreset); 561df53360cSBrian Somers break; 562df53360cSBrian Somers case 'e': 56369bc4fa9SMark Johnston termreset(); 564df53360cSBrian Somers if (!qflg) 565df53360cSBrian Somers (void)printf("\nScript done on %s", 566df53360cSBrian Somers ctime(&tclock)); 567df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg); 568df53360cSBrian Somers break; 569df53360cSBrian Somers case 'i': 570df53360cSBrian Somers /* throw input away */ 571df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg); 572df53360cSBrian Somers break; 573df53360cSBrian Somers case 'o': 574*6c4afed5SSimon J. Gerraty if (tflg) { 575*6c4afed5SSimon J. Gerraty if (stamp.scr_len == 0) 576*6c4afed5SSimon J. Gerraty continue; 577*6c4afed5SSimon J. Gerraty l = strftime(buf, sizeof buf, tstamp_fmt, 578*6c4afed5SSimon J. Gerraty localtime(&tclock)); 579*6c4afed5SSimon J. Gerraty (void)write(STDOUT_FILENO, buf, l); 580*6c4afed5SSimon J. Gerraty } else { 581df53360cSBrian Somers tsi.tv_sec = tso.tv_sec - tsi.tv_sec; 582df53360cSBrian Somers tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec; 583df53360cSBrian Somers if (tsi.tv_nsec < 0) { 584df53360cSBrian Somers tsi.tv_sec -= 1; 585df53360cSBrian Somers tsi.tv_nsec += 1000000000; 586df53360cSBrian Somers } 587df53360cSBrian Somers if (usesleep) 588df53360cSBrian Somers (void)nanosleep(&tsi, NULL); 589df53360cSBrian Somers tsi = tso; 590*6c4afed5SSimon J. Gerraty } 591df53360cSBrian Somers while (stamp.scr_len > 0) { 592df53360cSBrian Somers l = MIN(DEF_BUF, stamp.scr_len); 593df53360cSBrian Somers if (fread(buf, sizeof(char), l, fp) != l) 594df53360cSBrian Somers err(1, "cannot read buffer"); 595df53360cSBrian Somers 596df53360cSBrian Somers (void)write(STDOUT_FILENO, buf, l); 597df53360cSBrian Somers stamp.scr_len -= l; 598df53360cSBrian Somers } 599df53360cSBrian Somers break; 600df53360cSBrian Somers default: 601df53360cSBrian Somers errx(1, "invalid direction"); 602df53360cSBrian Somers } 603df53360cSBrian Somers } 604df53360cSBrian Somers (void)fclose(fp); 605df53360cSBrian Somers exit(0); 606df53360cSBrian Somers } 607