155714Skris/* 255714Skris * Copyright (c) 2010, 2012 David E. O'Brien 355714Skris * Copyright (c) 1980, 1992, 1993 455714Skris * The Regents of the University of California. All rights reserved. 555714Skris * 655714Skris * Redistribution and use in source and binary forms, with or without 755714Skris * modification, are permitted provided that the following conditions 8296465Sdelphij * are met: 955714Skris * 1. Redistributions of source code must retain the above copyright 1055714Skris * notice, this list of conditions and the following disclaimer. 1155714Skris * 2. Redistributions in binary form must reproduce the above copyright 1255714Skris * notice, this list of conditions and the following disclaimer in the 1355714Skris * documentation and/or other materials provided with the distribution. 1455714Skris * 4. Neither the name of the University nor the names of its contributors 15296465Sdelphij * may be used to endorse or promote products derived from this software 1655714Skris * without specific prior written permission. 1755714Skris * 1855714Skris * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1955714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2055714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2155714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22296465Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2355714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2455714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2555714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2655714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2755714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2855714Skris * SUCH DAMAGE. 2955714Skris */ 3055714Skris 3155714Skris#include <sys/param.h> 3255714Skris__FBSDID("$FreeBSD$"); 3355714Skris#ifndef lint 3455714Skrisstatic const char copyright[] = 3555714Skris"@(#) Copyright (c) 1980, 1992, 1993\n\ 3655714Skris The Regents of the University of California. All rights reserved.\n"; 37296465Sdelphij#endif 3855714Skris#ifndef lint 3955714Skrisstatic const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 40296465Sdelphij#endif 4155714Skris 4255714Skris#include <sys/wait.h> 4355714Skris#include <sys/stat.h> 4455714Skris#include <sys/ioctl.h> 4555714Skris#include <sys/time.h> 4655714Skris#include <sys/uio.h> 4755714Skris#include <sys/endian.h> 4855714Skris#include <dev/filemon/filemon.h> 4955714Skris 5055714Skris#include <err.h> 5155714Skris#include <errno.h> 52296465Sdelphij#include <fcntl.h> 5355714Skris#include <libutil.h> 5455714Skris#include <paths.h> 5555714Skris#include <signal.h> 5655714Skris#include <stdio.h> 5755714Skris#include <stdlib.h> 5855714Skris#include <string.h> 5955714Skris#include <termios.h> 6055714Skris#include <unistd.h> 61109998Smarkm 62296465Sdelphij#define DEF_BUF 65536 63296465Sdelphij 64296465Sdelphijstruct stamp { 65296465Sdelphij uint64_t scr_len; /* amount of data */ 66296465Sdelphij uint64_t scr_sec; /* time it arrived in seconds... */ 67296465Sdelphij uint32_t scr_usec; /* ...and microseconds */ 68296465Sdelphij uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */ 69296465Sdelphij}; 7055714Skris 71296465Sdelphijstatic FILE *fscript; 72296465Sdelphijstatic int master, slave; 73296465Sdelphijstatic int child; 74296465Sdelphijstatic const char *fname; 75296465Sdelphijstatic char *fmfname; 76296465Sdelphijstatic int fflg, qflg, ttyflg; 77296465Sdelphijstatic int usesleep, rawout, showexit; 78296465Sdelphij 79296465Sdelphijstatic struct termios tt; 80296465Sdelphij 81296465Sdelphijstatic void done(int) __dead2; 82296465Sdelphijstatic void doshell(char **); 83296465Sdelphijstatic void finish(void); 84296465Sdelphijstatic void record(FILE *, char *, size_t, int); 85296465Sdelphijstatic void consume(FILE *, off_t, char *, int); 86296465Sdelphijstatic void playback(FILE *) __dead2; 87296465Sdelphijstatic void usage(void); 88296465Sdelphij 89296465Sdelphijint 90296465Sdelphijmain(int argc, char *argv[]) 91296465Sdelphij{ 92296465Sdelphij int cc; 93296465Sdelphij struct termios rtt, stt; 94296465Sdelphij struct winsize win; 95296465Sdelphij struct timeval tv, *tvp; 96296465Sdelphij time_t tvec, start; 97296465Sdelphij char obuf[BUFSIZ]; 98296465Sdelphij char ibuf[BUFSIZ]; 99296465Sdelphij fd_set rfd; 100296465Sdelphij int aflg, kflg, pflg, ch, k, n; 101296465Sdelphij int flushtime, readstdin; 102296465Sdelphij int fm_fd, fm_log; 103296465Sdelphij 104 aflg = kflg = pflg = 0; 105 usesleep = 1; 106 rawout = 0; 107 flushtime = 30; 108 fm_fd = -1; /* Shut up stupid "may be used uninitialized" GCC 109 warning. (not needed w/clang) */ 110 showexit = 0; 111 112 while ((ch = getopt(argc, argv, "adfkpqrt:")) != -1) 113 switch(ch) { 114 case 'a': 115 aflg = 1; 116 break; 117 case 'd': 118 usesleep = 0; 119 break; 120 case 'f': 121 fflg = 1; 122 break; 123 case 'k': 124 kflg = 1; 125 break; 126 case 'p': 127 pflg = 1; 128 break; 129 case 'q': 130 qflg = 1; 131 break; 132 case 'r': 133 rawout = 1; 134 break; 135 case 't': 136 flushtime = atoi(optarg); 137 if (flushtime < 0) 138 err(1, "invalid flush time %d", flushtime); 139 break; 140 case '?': 141 default: 142 usage(); 143 } 144 argc -= optind; 145 argv += optind; 146 147 if (argc > 0) { 148 fname = argv[0]; 149 argv++; 150 argc--; 151 } else 152 fname = "typescript"; 153 154 if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL) 155 err(1, "%s", fname); 156 157 if (fflg) { 158 asprintf(&fmfname, "%s.filemon", fname); 159 if (!fmfname) 160 err(1, "%s.filemon", fname); 161 if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1) 162 err(1, "open(\"/dev/filemon\", O_RDWR)"); 163 if ((fm_log = open(fmfname, 164 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 165 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) 166 err(1, "open(%s)", fmfname); 167 if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0) 168 err(1, "Cannot set filemon log file descriptor"); 169 } 170 171 if (pflg) 172 playback(fscript); 173 174 if ((ttyflg = isatty(STDIN_FILENO)) != 0) { 175 if (tcgetattr(STDIN_FILENO, &tt) == -1) 176 err(1, "tcgetattr"); 177 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) 178 err(1, "ioctl"); 179 if (openpty(&master, &slave, NULL, &tt, &win) == -1) 180 err(1, "openpty"); 181 } else { 182 if (openpty(&master, &slave, NULL, NULL, NULL) == -1) 183 err(1, "openpty"); 184 } 185 186 if (rawout) 187 record(fscript, NULL, 0, 's'); 188 189 if (!qflg) { 190 tvec = time(NULL); 191 (void)printf("Script started, output file is %s\n", fname); 192 if (!rawout) { 193 (void)fprintf(fscript, "Script started on %s", 194 ctime(&tvec)); 195 if (argv[0]) { 196 showexit = 1; 197 fprintf(fscript, "Command: "); 198 for (k = 0 ; argv[k] ; ++k) 199 fprintf(fscript, "%s%s", k ? " " : "", 200 argv[k]); 201 fprintf(fscript, "\n"); 202 } 203 } 204 fflush(fscript); 205 if (fflg) { 206 (void)printf("Filemon started, output file is %s\n", 207 fmfname); 208 } 209 } 210 if (ttyflg) { 211 rtt = tt; 212 cfmakeraw(&rtt); 213 rtt.c_lflag &= ~ECHO; 214 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 215 } 216 217 child = fork(); 218 if (child < 0) { 219 warn("fork"); 220 done(1); 221 } 222 if (child == 0) { 223 if (fflg) { 224 int pid; 225 226 pid = getpid(); 227 if (ioctl(fm_fd, FILEMON_SET_PID, &pid) < 0) 228 err(1, "Cannot set filemon PID"); 229 } 230 231 doshell(argv); 232 } 233 close(slave); 234 235 start = tvec = time(0); 236 readstdin = 1; 237 for (;;) { 238 FD_ZERO(&rfd); 239 FD_SET(master, &rfd); 240 if (readstdin) 241 FD_SET(STDIN_FILENO, &rfd); 242 if (!readstdin && ttyflg) { 243 tv.tv_sec = 1; 244 tv.tv_usec = 0; 245 tvp = &tv; 246 readstdin = 1; 247 } else if (flushtime > 0) { 248 tv.tv_sec = flushtime - (tvec - start); 249 tv.tv_usec = 0; 250 tvp = &tv; 251 } else { 252 tvp = NULL; 253 } 254 n = select(master + 1, &rfd, 0, 0, tvp); 255 if (n < 0 && errno != EINTR) 256 break; 257 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { 258 cc = read(STDIN_FILENO, ibuf, BUFSIZ); 259 if (cc < 0) 260 break; 261 if (cc == 0) { 262 if (tcgetattr(master, &stt) == 0 && 263 (stt.c_lflag & ICANON) != 0) { 264 (void)write(master, &stt.c_cc[VEOF], 1); 265 } 266 readstdin = 0; 267 } 268 if (cc > 0) { 269 if (rawout) 270 record(fscript, ibuf, cc, 'i'); 271 (void)write(master, ibuf, cc); 272 if (kflg && tcgetattr(master, &stt) >= 0 && 273 ((stt.c_lflag & ECHO) == 0)) { 274 (void)fwrite(ibuf, 1, cc, fscript); 275 } 276 } 277 } 278 if (n > 0 && FD_ISSET(master, &rfd)) { 279 cc = read(master, obuf, sizeof (obuf)); 280 if (cc <= 0) 281 break; 282 (void)write(STDOUT_FILENO, obuf, cc); 283 if (rawout) 284 record(fscript, obuf, cc, 'o'); 285 else 286 (void)fwrite(obuf, 1, cc, fscript); 287 } 288 tvec = time(0); 289 if (tvec - start >= flushtime) { 290 fflush(fscript); 291 start = tvec; 292 } 293 } 294 finish(); 295 done(0); 296} 297 298static void 299usage(void) 300{ 301 (void)fprintf(stderr, 302 "usage: script [-adfkpqr] [-t time] [file [command ...]]\n"); 303 exit(1); 304} 305 306static void 307finish(void) 308{ 309 int e, status; 310 311 if (waitpid(child, &status, 0) == child) { 312 if (WIFEXITED(status)) 313 e = WEXITSTATUS(status); 314 else if (WIFSIGNALED(status)) 315 e = WTERMSIG(status); 316 else /* can't happen */ 317 e = 1; 318 done(e); 319 } 320} 321 322static void 323doshell(char **av) 324{ 325 const char *shell; 326 327 shell = getenv("SHELL"); 328 if (shell == NULL) 329 shell = _PATH_BSHELL; 330 331 (void)close(master); 332 (void)fclose(fscript); 333 free(fmfname); 334 login_tty(slave); 335 setenv("SCRIPT", fname, 1); 336 if (av[0]) { 337 execvp(av[0], av); 338 warn("%s", av[0]); 339 } else { 340 execl(shell, shell, "-i", (char *)NULL); 341 warn("%s", shell); 342 } 343 exit(1); 344} 345 346static void 347done(int eno) 348{ 349 time_t tvec; 350 351 if (ttyflg) 352 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 353 tvec = time(NULL); 354 if (rawout) 355 record(fscript, NULL, 0, 'e'); 356 if (!qflg) { 357 if (!rawout) { 358 if (showexit) 359 (void)fprintf(fscript, "\nCommand exit status:" 360 " %d", eno); 361 (void)fprintf(fscript,"\nScript done on %s", 362 ctime(&tvec)); 363 } 364 (void)printf("\nScript done, output file is %s\n", fname); 365 if (fflg) { 366 (void)printf("Filemon done, output file is %s\n", 367 fmfname); 368 } 369 } 370 (void)fclose(fscript); 371 (void)close(master); 372 exit(eno); 373} 374 375static void 376record(FILE *fp, char *buf, size_t cc, int direction) 377{ 378 struct iovec iov[2]; 379 struct stamp stamp; 380 struct timeval tv; 381 382 (void)gettimeofday(&tv, NULL); 383 stamp.scr_len = cc; 384 stamp.scr_sec = tv.tv_sec; 385 stamp.scr_usec = tv.tv_usec; 386 stamp.scr_direction = direction; 387 iov[0].iov_len = sizeof(stamp); 388 iov[0].iov_base = &stamp; 389 iov[1].iov_len = cc; 390 iov[1].iov_base = buf; 391 if (writev(fileno(fp), &iov[0], 2) == -1) 392 err(1, "writev"); 393} 394 395static void 396consume(FILE *fp, off_t len, char *buf, int reg) 397{ 398 size_t l; 399 400 if (reg) { 401 if (fseeko(fp, len, SEEK_CUR) == -1) 402 err(1, NULL); 403 } 404 else { 405 while (len > 0) { 406 l = MIN(DEF_BUF, len); 407 if (fread(buf, sizeof(char), l, fp) != l) 408 err(1, "cannot read buffer"); 409 len -= l; 410 } 411 } 412} 413 414#define swapstamp(stamp) do { \ 415 if (stamp.scr_direction > 0xff) { \ 416 stamp.scr_len = bswap64(stamp.scr_len); \ 417 stamp.scr_sec = bswap64(stamp.scr_sec); \ 418 stamp.scr_usec = bswap32(stamp.scr_usec); \ 419 stamp.scr_direction = bswap32(stamp.scr_direction); \ 420 } \ 421} while (0/*CONSTCOND*/) 422 423static void 424playback(FILE *fp) 425{ 426 struct timespec tsi, tso; 427 struct stamp stamp; 428 struct stat pst; 429 char buf[DEF_BUF]; 430 off_t nread, save_len; 431 size_t l; 432 time_t tclock; 433 int reg; 434 435 if (fstat(fileno(fp), &pst) == -1) 436 err(1, "fstat failed"); 437 438 reg = S_ISREG(pst.st_mode); 439 440 for (nread = 0; !reg || nread < pst.st_size; nread += save_len) { 441 if (fread(&stamp, sizeof(stamp), 1, fp) != 1) { 442 if (reg) 443 err(1, "reading playback header"); 444 else 445 break; 446 } 447 swapstamp(stamp); 448 save_len = sizeof(stamp); 449 450 if (reg && stamp.scr_len > 451 (uint64_t)(pst.st_size - save_len) - nread) 452 errx(1, "invalid stamp"); 453 454 save_len += stamp.scr_len; 455 tclock = stamp.scr_sec; 456 tso.tv_sec = stamp.scr_sec; 457 tso.tv_nsec = stamp.scr_usec * 1000; 458 459 switch (stamp.scr_direction) { 460 case 's': 461 if (!qflg) 462 (void)printf("Script started on %s", 463 ctime(&tclock)); 464 tsi = tso; 465 (void)consume(fp, stamp.scr_len, buf, reg); 466 break; 467 case 'e': 468 if (!qflg) 469 (void)printf("\nScript done on %s", 470 ctime(&tclock)); 471 (void)consume(fp, stamp.scr_len, buf, reg); 472 break; 473 case 'i': 474 /* throw input away */ 475 (void)consume(fp, stamp.scr_len, buf, reg); 476 break; 477 case 'o': 478 tsi.tv_sec = tso.tv_sec - tsi.tv_sec; 479 tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec; 480 if (tsi.tv_nsec < 0) { 481 tsi.tv_sec -= 1; 482 tsi.tv_nsec += 1000000000; 483 } 484 if (usesleep) 485 (void)nanosleep(&tsi, NULL); 486 tsi = tso; 487 while (stamp.scr_len > 0) { 488 l = MIN(DEF_BUF, stamp.scr_len); 489 if (fread(buf, sizeof(char), l, fp) != l) 490 err(1, "cannot read buffer"); 491 492 (void)write(STDOUT_FILENO, buf, l); 493 stamp.scr_len -= l; 494 } 495 break; 496 default: 497 errx(1, "invalid direction"); 498 } 499 } 500 (void)fclose(fp); 501 exit(0); 502} 503