1 /* $NetBSD: script.c,v 1.26 2020/08/08 16:01:35 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\ 35 The Regents of the University of California. All rights reserved."); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 41 #endif 42 __RCSID("$NetBSD: script.c,v 1.26 2020/08/08 16:01:35 christos Exp $"); 43 #endif /* not lint */ 44 45 #include <sys/types.h> 46 #include <sys/wait.h> 47 #include <sys/stat.h> 48 #include <sys/ioctl.h> 49 #include <sys/time.h> 50 #include <sys/param.h> 51 #include <sys/uio.h> 52 53 #include <err.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <paths.h> 57 #include <signal.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <termios.h> 62 #include <time.h> 63 #include <tzfile.h> 64 #include <unistd.h> 65 #include <util.h> 66 67 #define DEF_BUF 65536 68 69 struct stamp { 70 uint64_t scr_len; /* amount of data */ 71 uint64_t scr_sec; /* time it arrived in seconds... */ 72 uint32_t scr_usec; /* ...and microseconds */ 73 uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */ 74 }; 75 76 static FILE *fscript; 77 static int master, slave; 78 static int child, subchild; 79 static size_t outcc; 80 static int usesleep, rawout; 81 static int quiet, flush; 82 static const char *fname; 83 84 static int isterm; 85 static struct termios tt; 86 87 __dead static void done(void); 88 __dead static void dooutput(void); 89 __dead static void doshell(const char *); 90 __dead static void fail(void); 91 static void finish(int); 92 static void scriptflush(int); 93 static void record(FILE *, char *, size_t, int); 94 static void consume(FILE *, off_t, char *, int); 95 __dead static void playback(FILE *); 96 97 int 98 main(int argc, char *argv[]) 99 { 100 ssize_t scc; 101 size_t cc; 102 struct termios rtt; 103 struct winsize win; 104 int aflg, pflg, ch; 105 char ibuf[BUFSIZ]; 106 const char *command; 107 108 aflg = 0; 109 pflg = 0; 110 usesleep = 1; 111 rawout = 0; 112 quiet = 0; 113 flush = 0; 114 command = NULL; 115 while ((ch = getopt(argc, argv, "ac:dfpqr")) != -1) 116 switch(ch) { 117 case 'a': 118 aflg = 1; 119 break; 120 case 'c': 121 command = optarg; 122 break; 123 case 'd': 124 usesleep = 0; 125 break; 126 case 'f': 127 flush = 1; 128 break; 129 case 'p': 130 pflg = 1; 131 break; 132 case 'q': 133 quiet = 1; 134 break; 135 case 'r': 136 rawout = 1; 137 break; 138 case '?': 139 default: 140 (void)fprintf(stderr, 141 "Usage: %s [-c <command>][-adfpqr] [file]\n", 142 getprogname()); 143 exit(EXIT_FAILURE); 144 } 145 argc -= optind; 146 argv += optind; 147 148 if (argc > 0) 149 fname = argv[0]; 150 else 151 fname = "typescript"; 152 153 if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL) 154 err(EXIT_FAILURE, "fopen %s", fname); 155 156 if (pflg) 157 playback(fscript); 158 159 if (tcgetattr(STDIN_FILENO, &tt) == -1 || 160 ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) { 161 if (errno != ENOTTY) /* For debugger. */ 162 err(EXIT_FAILURE, "tcgetattr/ioctl"); 163 if (openpty(&master, &slave, NULL, NULL, NULL) == -1) 164 err(EXIT_FAILURE, "openpty"); 165 } else { 166 if (openpty(&master, &slave, NULL, &tt, &win) == -1) 167 err(EXIT_FAILURE, "openpty"); 168 isterm = 1; 169 } 170 171 if (!quiet) 172 (void)printf("Script started, output file is %s\n", fname); 173 174 if (isterm) { 175 rtt = tt; 176 cfmakeraw(&rtt); 177 rtt.c_lflag &= ~ECHO; 178 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 179 } 180 181 (void)signal(SIGCHLD, finish); 182 child = fork(); 183 if (child == -1) { 184 warn("fork"); 185 fail(); 186 } 187 if (child == 0) { 188 subchild = child = fork(); 189 if (child == -1) { 190 warn("fork"); 191 fail(); 192 } 193 if (child) 194 dooutput(); 195 else 196 doshell(command); 197 } 198 199 if (!rawout) 200 (void)fclose(fscript); 201 while ((scc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) { 202 cc = (size_t)scc; 203 if (rawout) 204 record(fscript, ibuf, cc, 'i'); 205 (void)write(master, ibuf, cc); 206 } 207 done(); 208 /* NOTREACHED */ 209 return EXIT_SUCCESS; 210 } 211 212 static void 213 finish(int signo) 214 { 215 int die, pid, status; 216 217 die = 0; 218 while ((pid = wait3(&status, WNOHANG, 0)) > 0) 219 if (pid == child) 220 die = 1; 221 222 if (die) 223 done(); 224 } 225 226 static void 227 dooutput(void) 228 { 229 struct itimerval value; 230 ssize_t scc; 231 size_t cc; 232 time_t tvec; 233 char obuf[BUFSIZ]; 234 235 (void)close(STDIN_FILENO); 236 tvec = time(NULL); 237 if (rawout) 238 record(fscript, NULL, 0, 's'); 239 else if (!quiet) 240 (void)fprintf(fscript, "Script started on %s", ctime(&tvec)); 241 242 (void)signal(SIGALRM, scriptflush); 243 value.it_interval.tv_sec = SECSPERMIN / 2; 244 value.it_interval.tv_usec = 0; 245 value.it_value = value.it_interval; 246 (void)setitimer(ITIMER_REAL, &value, NULL); 247 for (;;) { 248 scc = read(master, obuf, sizeof(obuf)); 249 if (scc <= 0) 250 break; 251 cc = (size_t)scc; 252 (void)write(1, obuf, cc); 253 if (rawout) 254 record(fscript, obuf, cc, 'o'); 255 else 256 (void)fwrite(obuf, 1, cc, fscript); 257 outcc += cc; 258 if (flush) 259 (void)fflush(fscript); 260 } 261 done(); 262 } 263 264 static void 265 scriptflush(int signo) 266 { 267 if (outcc) { 268 (void)fflush(fscript); 269 outcc = 0; 270 } 271 } 272 273 static void 274 doshell(const char *command) 275 { 276 const char *shell; 277 278 (void)close(master); 279 (void)fclose(fscript); 280 login_tty(slave); 281 if (command == NULL) { 282 shell = getenv("SHELL"); 283 if (shell == NULL) 284 shell = _PATH_BSHELL; 285 execl(shell, shell, "-i", NULL); 286 warn("execl `%s'", shell); 287 } else { 288 if (system(command) == -1) 289 warn("system `%s'", command); 290 } 291 292 fail(); 293 } 294 295 static void 296 fail(void) 297 { 298 299 (void)kill(0, SIGTERM); 300 done(); 301 } 302 303 static void 304 done(void) 305 { 306 time_t tvec; 307 308 if (subchild) { 309 tvec = time(NULL); 310 if (rawout) 311 record(fscript, NULL, 0, 'e'); 312 else if (!quiet) 313 (void)fprintf(fscript,"\nScript done on %s", 314 ctime(&tvec)); 315 (void)fclose(fscript); 316 (void)close(master); 317 } else { 318 if (isterm) 319 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 320 if (!quiet) 321 (void)printf("Script done, output file is %s\n", fname); 322 } 323 exit(EXIT_SUCCESS); 324 } 325 326 static void 327 record(FILE *fp, char *buf, size_t cc, int direction) 328 { 329 struct iovec iov[2]; 330 struct stamp stamp; 331 struct timeval tv; 332 333 (void)gettimeofday(&tv, NULL); 334 stamp.scr_len = cc; 335 stamp.scr_sec = tv.tv_sec; 336 stamp.scr_usec = tv.tv_usec; 337 stamp.scr_direction = direction; 338 iov[0].iov_len = sizeof(stamp); 339 iov[0].iov_base = &stamp; 340 iov[1].iov_len = cc; 341 iov[1].iov_base = buf; 342 if (writev(fileno(fp), &iov[0], 2) == -1) 343 err(EXIT_FAILURE, "writev"); 344 } 345 346 static void 347 consume(FILE *fp, off_t len, char *buf, int reg) 348 { 349 size_t l; 350 351 if (reg) { 352 if (fseeko(fp, len, SEEK_CUR) == -1) 353 err(EXIT_FAILURE, NULL); 354 } 355 else { 356 while (len > 0) { 357 l = MIN(DEF_BUF, len); 358 if (fread(buf, sizeof(char), l, fp) != l) 359 err(EXIT_FAILURE, "cannot read buffer"); 360 len -= l; 361 } 362 } 363 } 364 365 #define swapstamp(stamp) do { \ 366 if (stamp.scr_direction > 0xff) { \ 367 stamp.scr_len = bswap64(stamp.scr_len); \ 368 stamp.scr_sec = bswap64(stamp.scr_sec); \ 369 stamp.scr_usec = bswap32(stamp.scr_usec); \ 370 stamp.scr_direction = bswap32(stamp.scr_direction); \ 371 } \ 372 } while (0/*CONSTCOND*/) 373 374 static void 375 termset(void) 376 { 377 struct termios traw; 378 379 if (tcgetattr(STDOUT_FILENO, &tt) == -1) { 380 if (errno != ENOTTY) /* For debugger. */ 381 err(EXIT_FAILURE, "tcgetattr"); 382 return; 383 } 384 isterm = 1; 385 traw = tt; 386 cfmakeraw(&traw); 387 traw.c_lflag |= ISIG; 388 (void)tcsetattr(STDOUT_FILENO, TCSANOW, &traw); 389 } 390 391 static void 392 termreset(void) 393 { 394 if (isterm) 395 (void)tcsetattr(STDOUT_FILENO, TCSADRAIN, &tt); 396 397 isterm = 0; 398 } 399 400 static void 401 playback(FILE *fp) 402 { 403 struct timespec tsi, tso; 404 struct stamp stamp; 405 struct stat pst; 406 char buf[DEF_BUF]; 407 off_t nread, save_len; 408 size_t l; 409 time_t tclock; 410 int reg; 411 412 if (fstat(fileno(fp), &pst) == -1) 413 err(EXIT_FAILURE, "fstat failed"); 414 415 reg = S_ISREG(pst.st_mode); 416 417 for (nread = 0; !reg || nread < pst.st_size; nread += save_len) { 418 if (fread(&stamp, sizeof(stamp), 1, fp) != 1) { 419 if (reg) 420 err(EXIT_FAILURE, "reading playback header"); 421 else 422 break; 423 } 424 swapstamp(stamp); 425 save_len = sizeof(stamp); 426 427 if (reg && stamp.scr_len > 428 (uint64_t)(pst.st_size - save_len) - nread) 429 errx(EXIT_FAILURE, "invalid stamp"); 430 431 save_len += stamp.scr_len; 432 tclock = stamp.scr_sec; 433 tso.tv_sec = stamp.scr_sec; 434 tso.tv_nsec = stamp.scr_usec * 1000; 435 436 switch (stamp.scr_direction) { 437 case 's': 438 if (!quiet) 439 (void)printf("Script started on %s", 440 ctime(&tclock)); 441 tsi = tso; 442 (void)consume(fp, stamp.scr_len, buf, reg); 443 termset(); 444 atexit(termreset); 445 break; 446 case 'e': 447 termreset(); 448 if (!quiet) 449 (void)printf("\nScript done on %s", 450 ctime(&tclock)); 451 (void)consume(fp, stamp.scr_len, buf, reg); 452 break; 453 case 'i': 454 /* throw input away */ 455 (void)consume(fp, stamp.scr_len, buf, reg); 456 break; 457 case 'o': 458 tsi.tv_sec = tso.tv_sec - tsi.tv_sec; 459 tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec; 460 if (tsi.tv_nsec < 0) { 461 tsi.tv_sec -= 1; 462 tsi.tv_nsec += 1000000000; 463 } 464 if (usesleep) 465 (void)nanosleep(&tsi, NULL); 466 tsi = tso; 467 while (stamp.scr_len > 0) { 468 l = MIN(DEF_BUF, stamp.scr_len); 469 if (fread(buf, sizeof(char), l, fp) != l) 470 err(EXIT_FAILURE, "cannot read buffer"); 471 472 (void)write(STDOUT_FILENO, buf, l); 473 stamp.scr_len -= l; 474 } 475 break; 476 default: 477 errx(EXIT_FAILURE, "invalid direction %u", 478 stamp.scr_direction); 479 } 480 } 481 (void)fclose(fp); 482 exit(EXIT_SUCCESS); 483 } 484