11590Srgrimes/* 2242138Sobrien * Copyright (c) 2010, 2012 David E. O'Brien 31590Srgrimes * Copyright (c) 1980, 1992, 1993 41590Srgrimes * The Regents of the University of California. All rights reserved. 51590Srgrimes * 61590Srgrimes * Redistribution and use in source and binary forms, with or without 71590Srgrimes * modification, are permitted provided that the following conditions 81590Srgrimes * are met: 91590Srgrimes * 1. Redistributions of source code must retain the above copyright 101590Srgrimes * notice, this list of conditions and the following disclaimer. 111590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer in the 131590Srgrimes * documentation and/or other materials provided with the distribution. 141590Srgrimes * 4. Neither the name of the University nor the names of its contributors 151590Srgrimes * may be used to endorse or promote products derived from this software 161590Srgrimes * without specific prior written permission. 171590Srgrimes * 181590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 191590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 201590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 211590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 221590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 231590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 241590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 251590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 261590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 271590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 281590Srgrimes * SUCH DAMAGE. 291590Srgrimes */ 301590Srgrimes 31241972Sobrien#include <sys/param.h> 3287768Smarkm__FBSDID("$FreeBSD$"); 331590Srgrimes#ifndef lint 3427980Scharnierstatic const char copyright[] = 351590Srgrimes"@(#) Copyright (c) 1980, 1992, 1993\n\ 361590Srgrimes The Regents of the University of California. All rights reserved.\n"; 3787768Smarkm#endif 381590Srgrimes#ifndef lint 3987768Smarkmstatic const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 4027980Scharnier#endif 411590Srgrimes 421590Srgrimes#include <sys/wait.h> 431590Srgrimes#include <sys/stat.h> 441590Srgrimes#include <sys/ioctl.h> 451590Srgrimes#include <sys/time.h> 46238896Sbrian#include <sys/uio.h> 47238896Sbrian#include <sys/endian.h> 48242138Sobrien#include <dev/filemon/filemon.h> 491590Srgrimes 5027980Scharnier#include <err.h> 5134295Speter#include <errno.h> 521590Srgrimes#include <fcntl.h> 5327980Scharnier#include <libutil.h> 541590Srgrimes#include <paths.h> 551590Srgrimes#include <signal.h> 561590Srgrimes#include <stdio.h> 571590Srgrimes#include <stdlib.h> 581590Srgrimes#include <string.h> 591590Srgrimes#include <termios.h> 601590Srgrimes#include <unistd.h> 611590Srgrimes 62238896Sbrian#define DEF_BUF 65536 63238896Sbrian 64238896Sbrianstruct stamp { 65238896Sbrian uint64_t scr_len; /* amount of data */ 66238896Sbrian uint64_t scr_sec; /* time it arrived in seconds... */ 67238896Sbrian uint32_t scr_usec; /* ...and microseconds */ 68238896Sbrian uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */ 69238896Sbrian}; 70238896Sbrian 71211394Sedstatic FILE *fscript; 72211394Sedstatic int master, slave; 73211394Sedstatic int child; 74211394Sedstatic const char *fname; 75242138Sobrienstatic char *fmfname; 76242138Sobrienstatic int fflg, qflg, ttyflg; 77297352Sbdrewerystatic int usesleep, rawout, showexit; 781590Srgrimes 79211394Sedstatic struct termios tt; 801590Srgrimes 81211394Sedstatic void done(int) __dead2; 82211394Sedstatic void doshell(char **); 83211394Sedstatic void finish(void); 84238896Sbrianstatic void record(FILE *, char *, size_t, int); 85238896Sbrianstatic void consume(FILE *, off_t, char *, int); 86238896Sbrianstatic void playback(FILE *) __dead2; 8792922Simpstatic void usage(void); 881590Srgrimes 891590Srgrimesint 90102944Sdwmalonemain(int argc, char *argv[]) 911590Srgrimes{ 9287768Smarkm int cc; 9334295Speter struct termios rtt, stt; 941590Srgrimes struct winsize win; 9534295Speter struct timeval tv, *tvp; 9634295Speter time_t tvec, start; 9734295Speter char obuf[BUFSIZ]; 981590Srgrimes char ibuf[BUFSIZ]; 9934295Speter fd_set rfd; 100242138Sobrien int aflg, kflg, pflg, ch, k, n; 101242138Sobrien int flushtime, readstdin; 102242138Sobrien int fm_fd, fm_log; 1031590Srgrimes 104238896Sbrian aflg = kflg = pflg = 0; 105238896Sbrian usesleep = 1; 106238896Sbrian rawout = 0; 107242138Sobrien flushtime = 30; 108242138Sobrien fm_fd = -1; /* Shut up stupid "may be used uninitialized" GCC 109242138Sobrien warning. (not needed w/clang) */ 110297352Sbdrewery showexit = 0; 111238896Sbrian 112242138Sobrien while ((ch = getopt(argc, argv, "adfkpqrt:")) != -1) 1131590Srgrimes switch(ch) { 1141590Srgrimes case 'a': 1151590Srgrimes aflg = 1; 1161590Srgrimes break; 117238896Sbrian case 'd': 118238896Sbrian usesleep = 0; 11932083Speter break; 120242138Sobrien case 'f': 121242138Sobrien fflg = 1; 122242138Sobrien break; 12334295Speter case 'k': 12434295Speter kflg = 1; 12534295Speter break; 126238896Sbrian case 'p': 127238896Sbrian pflg = 1; 128238896Sbrian break; 129238896Sbrian case 'q': 130238896Sbrian qflg = 1; 131238896Sbrian break; 132238896Sbrian case 'r': 133238896Sbrian rawout = 1; 134238896Sbrian break; 13534295Speter case 't': 13634295Speter flushtime = atoi(optarg); 13734295Speter if (flushtime < 0) 13834295Speter err(1, "invalid flush time %d", flushtime); 13934295Speter break; 1401590Srgrimes case '?': 1411590Srgrimes default: 14227980Scharnier usage(); 1431590Srgrimes } 1441590Srgrimes argc -= optind; 1451590Srgrimes argv += optind; 1461590Srgrimes 14732083Speter if (argc > 0) { 1481590Srgrimes fname = argv[0]; 14932083Speter argv++; 15032083Speter argc--; 15132083Speter } else 1521590Srgrimes fname = "typescript"; 1531590Srgrimes 154238896Sbrian if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL) 15527980Scharnier err(1, "%s", fname); 1561590Srgrimes 157242138Sobrien if (fflg) { 158242138Sobrien asprintf(&fmfname, "%s.filemon", fname); 159242138Sobrien if (!fmfname) 160242138Sobrien err(1, "%s.filemon", fname); 161298510Sbapt if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1) 162242138Sobrien err(1, "open(\"/dev/filemon\", O_RDWR)"); 163298510Sbapt if ((fm_log = open(fmfname, 164298510Sbapt O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 165242138Sobrien S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) 166242138Sobrien err(1, "open(%s)", fmfname); 167242138Sobrien if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0) 168242138Sobrien err(1, "Cannot set filemon log file descriptor"); 169242138Sobrien } 170242138Sobrien 171238896Sbrian if (pflg) 172238896Sbrian playback(fscript); 173238896Sbrian 174201384Sed if ((ttyflg = isatty(STDIN_FILENO)) != 0) { 175124845Scperciva if (tcgetattr(STDIN_FILENO, &tt) == -1) 176124845Scperciva err(1, "tcgetattr"); 177124845Scperciva if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) 178124845Scperciva err(1, "ioctl"); 179124845Scperciva if (openpty(&master, &slave, NULL, &tt, &win) == -1) 180124845Scperciva err(1, "openpty"); 181124845Scperciva } else { 182124845Scperciva if (openpty(&master, &slave, NULL, NULL, NULL) == -1) 183124845Scperciva err(1, "openpty"); 184124845Scperciva } 1851590Srgrimes 186238896Sbrian if (rawout) 187238896Sbrian record(fscript, NULL, 0, 's'); 188238896Sbrian 18934295Speter if (!qflg) { 19034295Speter tvec = time(NULL); 19132083Speter (void)printf("Script started, output file is %s\n", fname); 192238896Sbrian if (!rawout) { 193238896Sbrian (void)fprintf(fscript, "Script started on %s", 194242138Sobrien ctime(&tvec)); 195238896Sbrian if (argv[0]) { 196297352Sbdrewery showexit = 1; 197297352Sbdrewery fprintf(fscript, "Command: "); 198238896Sbrian for (k = 0 ; argv[k] ; ++k) 199238896Sbrian fprintf(fscript, "%s%s", k ? " " : "", 200238896Sbrian argv[k]); 201238896Sbrian fprintf(fscript, "\n"); 202238896Sbrian } 203238896Sbrian } 20434295Speter fflush(fscript); 205242138Sobrien if (fflg) { 206242138Sobrien (void)printf("Filemon started, output file is %s\n", 207242138Sobrien fmfname); 208242138Sobrien } 20934295Speter } 210124845Scperciva if (ttyflg) { 211124845Scperciva rtt = tt; 212124845Scperciva cfmakeraw(&rtt); 213124845Scperciva rtt.c_lflag &= ~ECHO; 214124845Scperciva (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 215124845Scperciva } 2161590Srgrimes 2171590Srgrimes child = fork(); 2181590Srgrimes if (child < 0) { 21927980Scharnier warn("fork"); 22039481Sdes done(1); 2211590Srgrimes } 222296911Sbdrewery if (child == 0) { 223296911Sbdrewery if (fflg) { 224296911Sbdrewery int pid; 225296911Sbdrewery 226296911Sbdrewery pid = getpid(); 227296911Sbdrewery if (ioctl(fm_fd, FILEMON_SET_PID, &pid) < 0) 228296911Sbdrewery err(1, "Cannot set filemon PID"); 229296911Sbdrewery } 230296911Sbdrewery 23134295Speter doshell(argv); 232296911Sbdrewery } 233205009Sed close(slave); 23434295Speter 235225809Strociny start = tvec = time(0); 236225809Strociny readstdin = 1; 23734295Speter for (;;) { 238225809Strociny FD_ZERO(&rfd); 23934295Speter FD_SET(master, &rfd); 240225809Strociny if (readstdin) 241225809Strociny FD_SET(STDIN_FILENO, &rfd); 242260924Strociny if (!readstdin && ttyflg) { 243260924Strociny tv.tv_sec = 1; 24434295Speter tv.tv_usec = 0; 245225809Strociny tvp = &tv; 246225809Strociny readstdin = 1; 247260924Strociny } else if (flushtime > 0) { 248260924Strociny tv.tv_sec = flushtime - (tvec - start); 249260924Strociny tv.tv_usec = 0; 250260924Strociny tvp = &tv; 251225809Strociny } else { 252225809Strociny tvp = NULL; 2531590Srgrimes } 25434295Speter n = select(master + 1, &rfd, 0, 0, tvp); 25534295Speter if (n < 0 && errno != EINTR) 25634295Speter break; 25734295Speter if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { 25834295Speter cc = read(STDIN_FILENO, ibuf, BUFSIZ); 259125848Scperciva if (cc < 0) 26034295Speter break; 261225809Strociny if (cc == 0) { 262225809Strociny if (tcgetattr(master, &stt) == 0 && 263225809Strociny (stt.c_lflag & ICANON) != 0) { 264225809Strociny (void)write(master, &stt.c_cc[VEOF], 1); 265225809Strociny } 266225809Strociny readstdin = 0; 267225809Strociny } 26834295Speter if (cc > 0) { 269238896Sbrian if (rawout) 270238896Sbrian record(fscript, ibuf, cc, 'i'); 27134295Speter (void)write(master, ibuf, cc); 27234295Speter if (kflg && tcgetattr(master, &stt) >= 0 && 27334295Speter ((stt.c_lflag & ECHO) == 0)) { 27434295Speter (void)fwrite(ibuf, 1, cc, fscript); 27534295Speter } 27634295Speter } 27734295Speter } 27834295Speter if (n > 0 && FD_ISSET(master, &rfd)) { 27934295Speter cc = read(master, obuf, sizeof (obuf)); 28034295Speter if (cc <= 0) 28134295Speter break; 28280381Ssheldonh (void)write(STDOUT_FILENO, obuf, cc); 283238896Sbrian if (rawout) 284238896Sbrian record(fscript, obuf, cc, 'o'); 285238896Sbrian else 286238896Sbrian (void)fwrite(obuf, 1, cc, fscript); 28734295Speter } 28834295Speter tvec = time(0); 28934295Speter if (tvec - start >= flushtime) { 29034295Speter fflush(fscript); 29134295Speter start = tvec; 29234295Speter } 2931590Srgrimes } 29439481Sdes finish(); 29539481Sdes done(0); 2961590Srgrimes} 2971590Srgrimes 29827980Scharnierstatic void 299102944Sdwmaloneusage(void) 30027980Scharnier{ 30134295Speter (void)fprintf(stderr, 302242138Sobrien "usage: script [-adfkpqr] [-t time] [file [command ...]]\n"); 30327980Scharnier exit(1); 30427980Scharnier} 30527980Scharnier 306211394Sedstatic void 307102944Sdwmalonefinish(void) 3081590Srgrimes{ 309207453Sed int e, status; 3101590Srgrimes 311207453Sed if (waitpid(child, &status, 0) == child) { 312207453Sed if (WIFEXITED(status)) 313207453Sed e = WEXITSTATUS(status); 314207453Sed else if (WIFSIGNALED(status)) 315207453Sed e = WTERMSIG(status); 316207453Sed else /* can't happen */ 317207453Sed e = 1; 31839481Sdes done(e); 319207453Sed } 3201590Srgrimes} 3211590Srgrimes 322211394Sedstatic void 323102944Sdwmalonedoshell(char **av) 3241590Srgrimes{ 32587296Sdwmalone const char *shell; 3261590Srgrimes 3271590Srgrimes shell = getenv("SHELL"); 3281590Srgrimes if (shell == NULL) 3291590Srgrimes shell = _PATH_BSHELL; 3301590Srgrimes 3311590Srgrimes (void)close(master); 3321590Srgrimes (void)fclose(fscript); 333242138Sobrien free(fmfname); 3341590Srgrimes login_tty(slave); 335212770Sobrien setenv("SCRIPT", fname, 1); 33639481Sdes if (av[0]) { 33732108Speter execvp(av[0], av); 33862897Skris warn("%s", av[0]); 33939481Sdes } else { 34079452Sbrian execl(shell, shell, "-i", (char *)NULL); 34162897Skris warn("%s", shell); 34239481Sdes } 343296912Sbdrewery exit(1); 3441590Srgrimes} 3451590Srgrimes 346211394Sedstatic void 347102944Sdwmalonedone(int eno) 3481590Srgrimes{ 3491590Srgrimes time_t tvec; 3501590Srgrimes 351124845Scperciva if (ttyflg) 352124845Scperciva (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 35334295Speter tvec = time(NULL); 354238896Sbrian if (rawout) 355238896Sbrian record(fscript, NULL, 0, 'e'); 35634295Speter if (!qflg) { 357297352Sbdrewery if (!rawout) { 358297352Sbdrewery if (showexit) 359297352Sbdrewery (void)fprintf(fscript, "\nCommand exit status:" 360297352Sbdrewery " %d", eno); 361238896Sbrian (void)fprintf(fscript,"\nScript done on %s", 362242138Sobrien ctime(&tvec)); 363297352Sbdrewery } 36434295Speter (void)printf("\nScript done, output file is %s\n", fname); 365242138Sobrien if (fflg) { 366242138Sobrien (void)printf("Filemon done, output file is %s\n", 367242138Sobrien fmfname); 368242138Sobrien } 3691590Srgrimes } 37034295Speter (void)fclose(fscript); 37134295Speter (void)close(master); 37239481Sdes exit(eno); 3731590Srgrimes} 374238896Sbrian 375238896Sbrianstatic void 376238896Sbrianrecord(FILE *fp, char *buf, size_t cc, int direction) 377238896Sbrian{ 378238896Sbrian struct iovec iov[2]; 379238896Sbrian struct stamp stamp; 380238896Sbrian struct timeval tv; 381238896Sbrian 382238896Sbrian (void)gettimeofday(&tv, NULL); 383238896Sbrian stamp.scr_len = cc; 384238896Sbrian stamp.scr_sec = tv.tv_sec; 385238896Sbrian stamp.scr_usec = tv.tv_usec; 386238896Sbrian stamp.scr_direction = direction; 387238896Sbrian iov[0].iov_len = sizeof(stamp); 388238896Sbrian iov[0].iov_base = &stamp; 389238896Sbrian iov[1].iov_len = cc; 390238896Sbrian iov[1].iov_base = buf; 391238896Sbrian if (writev(fileno(fp), &iov[0], 2) == -1) 392238896Sbrian err(1, "writev"); 393238896Sbrian} 394238896Sbrian 395238896Sbrianstatic void 396238896Sbrianconsume(FILE *fp, off_t len, char *buf, int reg) 397238896Sbrian{ 398238896Sbrian size_t l; 399238896Sbrian 400238896Sbrian if (reg) { 401238896Sbrian if (fseeko(fp, len, SEEK_CUR) == -1) 402238896Sbrian err(1, NULL); 403238896Sbrian } 404238896Sbrian else { 405238896Sbrian while (len > 0) { 406238896Sbrian l = MIN(DEF_BUF, len); 407238896Sbrian if (fread(buf, sizeof(char), l, fp) != l) 408238896Sbrian err(1, "cannot read buffer"); 409238896Sbrian len -= l; 410238896Sbrian } 411238896Sbrian } 412238896Sbrian} 413238896Sbrian 414238896Sbrian#define swapstamp(stamp) do { \ 415238896Sbrian if (stamp.scr_direction > 0xff) { \ 416238896Sbrian stamp.scr_len = bswap64(stamp.scr_len); \ 417238896Sbrian stamp.scr_sec = bswap64(stamp.scr_sec); \ 418238896Sbrian stamp.scr_usec = bswap32(stamp.scr_usec); \ 419238896Sbrian stamp.scr_direction = bswap32(stamp.scr_direction); \ 420238896Sbrian } \ 421238896Sbrian} while (0/*CONSTCOND*/) 422238896Sbrian 423238896Sbrianstatic void 424238896Sbrianplayback(FILE *fp) 425238896Sbrian{ 426238896Sbrian struct timespec tsi, tso; 427238896Sbrian struct stamp stamp; 428238896Sbrian struct stat pst; 429238896Sbrian char buf[DEF_BUF]; 430238896Sbrian off_t nread, save_len; 431238896Sbrian size_t l; 432238896Sbrian time_t tclock; 433238896Sbrian int reg; 434238896Sbrian 435238896Sbrian if (fstat(fileno(fp), &pst) == -1) 436238896Sbrian err(1, "fstat failed"); 437238896Sbrian 438238896Sbrian reg = S_ISREG(pst.st_mode); 439238896Sbrian 440238896Sbrian for (nread = 0; !reg || nread < pst.st_size; nread += save_len) { 441238896Sbrian if (fread(&stamp, sizeof(stamp), 1, fp) != 1) { 442238896Sbrian if (reg) 443238896Sbrian err(1, "reading playback header"); 444238896Sbrian else 445238896Sbrian break; 446238896Sbrian } 447238896Sbrian swapstamp(stamp); 448238896Sbrian save_len = sizeof(stamp); 449238896Sbrian 450238896Sbrian if (reg && stamp.scr_len > 451238896Sbrian (uint64_t)(pst.st_size - save_len) - nread) 452238896Sbrian errx(1, "invalid stamp"); 453238896Sbrian 454238896Sbrian save_len += stamp.scr_len; 455238896Sbrian tclock = stamp.scr_sec; 456238896Sbrian tso.tv_sec = stamp.scr_sec; 457238896Sbrian tso.tv_nsec = stamp.scr_usec * 1000; 458238896Sbrian 459238896Sbrian switch (stamp.scr_direction) { 460238896Sbrian case 's': 461238896Sbrian if (!qflg) 462238896Sbrian (void)printf("Script started on %s", 463238896Sbrian ctime(&tclock)); 464238896Sbrian tsi = tso; 465238896Sbrian (void)consume(fp, stamp.scr_len, buf, reg); 466238896Sbrian break; 467238896Sbrian case 'e': 468238896Sbrian if (!qflg) 469238896Sbrian (void)printf("\nScript done on %s", 470238896Sbrian ctime(&tclock)); 471238896Sbrian (void)consume(fp, stamp.scr_len, buf, reg); 472238896Sbrian break; 473238896Sbrian case 'i': 474238896Sbrian /* throw input away */ 475238896Sbrian (void)consume(fp, stamp.scr_len, buf, reg); 476238896Sbrian break; 477238896Sbrian case 'o': 478238896Sbrian tsi.tv_sec = tso.tv_sec - tsi.tv_sec; 479238896Sbrian tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec; 480238896Sbrian if (tsi.tv_nsec < 0) { 481238896Sbrian tsi.tv_sec -= 1; 482238896Sbrian tsi.tv_nsec += 1000000000; 483238896Sbrian } 484238896Sbrian if (usesleep) 485238896Sbrian (void)nanosleep(&tsi, NULL); 486238896Sbrian tsi = tso; 487238896Sbrian while (stamp.scr_len > 0) { 488238896Sbrian l = MIN(DEF_BUF, stamp.scr_len); 489238896Sbrian if (fread(buf, sizeof(char), l, fp) != l) 490238896Sbrian err(1, "cannot read buffer"); 491238896Sbrian 492238896Sbrian (void)write(STDOUT_FILENO, buf, l); 493238896Sbrian stamp.scr_len -= l; 494238896Sbrian } 495238896Sbrian break; 496238896Sbrian default: 497238896Sbrian errx(1, "invalid direction"); 498238896Sbrian } 499238896Sbrian } 500238896Sbrian (void)fclose(fp); 501238896Sbrian exit(0); 502238896Sbrian} 503