1 /* $NetBSD: script.c,v 1.33 2022/02/13 19:40:14 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.33 2022/02/13 19:40:14 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 volatile sig_atomic_t die = 0; /* exit if 1 */ 85 static int cstat = EXIT_SUCCESS; /* cmd. exit status */ 86 static int eflag; 87 static int isterm; 88 static struct termios tt; 89 90 __dead static void done(int); 91 __dead static void doshell(const char *); 92 __dead static void fail(void); 93 static sig_t xsignal(int, sig_t); 94 __dead static void dooutput(void); 95 static void finish(int); 96 static void scriptflush(int); 97 static void record(FILE *, char *, size_t, int); 98 static void consume(FILE *, off_t, char *, int); 99 __dead static void playback(FILE *); 100 101 int 102 main(int argc, char *argv[]) 103 { 104 ssize_t scc; 105 size_t cc; 106 struct termios rtt; 107 struct winsize win; 108 int aflg, pflg, ch; 109 char ibuf[BUFSIZ]; 110 const char *command; 111 112 aflg = 0; 113 pflg = 0; 114 usesleep = 1; 115 rawout = 0; 116 quiet = 0; 117 flush = 0; 118 command = NULL; 119 while ((ch = getopt(argc, argv, "ac:defpqr")) != -1) 120 switch(ch) { 121 case 'a': 122 aflg = 1; 123 break; 124 case 'c': 125 command = optarg; 126 break; 127 case 'd': 128 usesleep = 0; 129 break; 130 case 'e': 131 eflag = 1; 132 break; 133 case 'f': 134 flush = 1; 135 break; 136 case 'p': 137 pflg = 1; 138 break; 139 case 'q': 140 quiet = 1; 141 break; 142 case 'r': 143 rawout = 1; 144 break; 145 case '?': 146 default: 147 (void)fprintf(stderr, 148 "Usage: %s [-c <command>][-adefpqr] [file]\n", 149 getprogname()); 150 exit(EXIT_FAILURE); 151 } 152 argc -= optind; 153 argv += optind; 154 155 if (argc > 0) 156 fname = argv[0]; 157 else 158 fname = "typescript"; 159 160 if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL) 161 err(EXIT_FAILURE, "fopen %s", fname); 162 163 if (pflg) 164 playback(fscript); 165 166 if (tcgetattr(STDIN_FILENO, &tt) == -1 || 167 ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) { 168 if (errno != ENOTTY) /* For debugger. */ 169 err(EXIT_FAILURE, "tcgetattr/ioctl"); 170 if (openpty(&master, &slave, NULL, NULL, NULL) == -1) 171 err(EXIT_FAILURE, "openpty"); 172 } else { 173 if (openpty(&master, &slave, NULL, &tt, &win) == -1) 174 err(EXIT_FAILURE, "openpty"); 175 isterm = 1; 176 } 177 178 if (!quiet) 179 (void)printf("Script started, output file is %s\n", fname); 180 181 if (isterm) { 182 rtt = tt; 183 cfmakeraw(&rtt); 184 rtt.c_lflag &= ~ECHO; 185 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 186 } 187 188 (void)xsignal(SIGCHLD, finish); 189 child = fork(); 190 if (child == -1) { 191 warn("fork"); 192 fail(); 193 } 194 if (child == 0) { 195 subchild = child = fork(); 196 if (child == -1) { 197 warn("fork"); 198 fail(); 199 } 200 if (child) 201 dooutput(); 202 else 203 doshell(command); 204 } 205 206 if (!rawout) 207 (void)fclose(fscript); 208 while (!die && (scc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) { 209 cc = (size_t)scc; 210 if (rawout) 211 record(fscript, ibuf, cc, 'i'); 212 (void)write(master, ibuf, cc); 213 } 214 done(cstat); 215 } 216 217 /** 218 * wrapper around sigaction() because we want POSIX semantics: 219 * no auto-restarting of interrupted slow syscalls. 220 */ 221 static sig_t 222 xsignal(int signo, sig_t handler) 223 { 224 struct sigaction sa, osa; 225 226 sa.sa_handler = handler; 227 sa.sa_flags = 0; 228 sigemptyset(&sa.sa_mask); 229 if (sigaction(signo, &sa, &osa) == -1) 230 return SIG_ERR; 231 return osa.sa_handler; 232 } 233 234 static int 235 getshellstatus(int status) 236 { 237 if (WIFEXITED(status)) 238 return WEXITSTATUS(status); 239 if (WIFSIGNALED(status)) 240 return 128 + WTERMSIG(status); 241 return EXIT_FAILURE; 242 } 243 244 static void 245 finish(int signo) 246 { 247 int pid, status; 248 249 die = 0; 250 while ((pid = wait(&status)) > 0) 251 if (pid == child) { 252 die = 1; 253 } 254 255 if (!die) 256 return; 257 done(eflag ? getshellstatus(status) : EXIT_SUCCESS); 258 } 259 260 static void 261 dooutput(void) 262 { 263 struct itimerval value; 264 ssize_t scc; 265 size_t cc; 266 time_t tvec; 267 char obuf[BUFSIZ]; 268 269 (void)close(STDIN_FILENO); 270 tvec = time(NULL); 271 if (rawout) 272 record(fscript, NULL, 0, 's'); 273 else if (!quiet) 274 (void)fprintf(fscript, "Script started on %s", ctime(&tvec)); 275 276 (void)signal(SIGALRM, scriptflush); 277 value.it_interval.tv_sec = SECSPERMIN / 2; 278 value.it_interval.tv_usec = 0; 279 value.it_value = value.it_interval; 280 (void)setitimer(ITIMER_REAL, &value, NULL); 281 for (;;) { 282 scc = read(master, obuf, sizeof(obuf)); 283 if (scc <= 0) 284 break; 285 cc = (size_t)scc; 286 (void)write(STDOUT_FILENO, obuf, cc); 287 if (rawout) 288 record(fscript, obuf, cc, 'o'); 289 else 290 (void)fwrite(obuf, 1, cc, fscript); 291 outcc += cc; 292 if (flush) 293 (void)fflush(fscript); 294 } 295 done(cstat); 296 } 297 298 static void 299 scriptflush(int signo) 300 { 301 if (outcc) { 302 (void)fflush(fscript); 303 outcc = 0; 304 } 305 } 306 307 static void 308 doshell(const char *command) 309 { 310 const char *shell; 311 312 (void)close(master); 313 (void)fclose(fscript); 314 login_tty(slave); 315 if (command == NULL) { 316 shell = getenv("SHELL"); 317 if (shell == NULL) 318 shell = _PATH_BSHELL; 319 execl(shell, shell, "-i", NULL); 320 warn("execl `%s'", shell); 321 } else { 322 int ret = system(command); 323 if (ret == -1) 324 warn("system `%s'", command); 325 else 326 exit(eflag ? getshellstatus(ret) : EXIT_FAILURE); 327 } 328 329 fail(); 330 } 331 332 static void 333 fail(void) 334 { 335 336 (void)kill(0, SIGTERM); 337 done(EXIT_FAILURE); 338 } 339 340 static void 341 done(int status) 342 { 343 time_t tvec; 344 345 if (subchild) { 346 tvec = time(NULL); 347 if (rawout) 348 record(fscript, NULL, 0, 'e'); 349 else if (!quiet) 350 (void)fprintf(fscript,"\nScript done on %s", 351 ctime(&tvec)); 352 (void)fclose(fscript); 353 (void)close(master); 354 } else { 355 if (isterm) 356 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 357 if (!quiet) 358 (void)printf("Script done, output file is %s\n", fname); 359 } 360 exit(status); 361 } 362 363 static void 364 record(FILE *fp, char *buf, size_t cc, int direction) 365 { 366 struct iovec iov[2]; 367 struct stamp stamp; 368 struct timeval tv; 369 370 (void)gettimeofday(&tv, NULL); 371 stamp.scr_len = cc; 372 stamp.scr_sec = tv.tv_sec; 373 stamp.scr_usec = tv.tv_usec; 374 stamp.scr_direction = direction; 375 iov[0].iov_len = sizeof(stamp); 376 iov[0].iov_base = &stamp; 377 iov[1].iov_len = cc; 378 iov[1].iov_base = buf; 379 if (writev(fileno(fp), &iov[0], 2) == -1) 380 err(EXIT_FAILURE, "writev"); 381 } 382 383 static void 384 consume(FILE *fp, off_t len, char *buf, int reg) 385 { 386 size_t l; 387 388 if (reg) { 389 if (fseeko(fp, len, SEEK_CUR) == -1) 390 err(EXIT_FAILURE, NULL); 391 } 392 else { 393 while (len > 0) { 394 l = MIN(DEF_BUF, len); 395 if (fread(buf, sizeof(char), l, fp) != l) 396 err(EXIT_FAILURE, "cannot read buffer"); 397 len -= l; 398 } 399 } 400 } 401 402 #define swapstamp(stamp) do { \ 403 if (stamp.scr_direction > 0xff) { \ 404 stamp.scr_len = bswap64(stamp.scr_len); \ 405 stamp.scr_sec = bswap64(stamp.scr_sec); \ 406 stamp.scr_usec = bswap32(stamp.scr_usec); \ 407 stamp.scr_direction = bswap32(stamp.scr_direction); \ 408 } \ 409 } while (0/*CONSTCOND*/) 410 411 static void 412 termset(void) 413 { 414 struct termios traw; 415 416 if (tcgetattr(STDOUT_FILENO, &tt) == -1) { 417 if (errno != ENOTTY) /* For debugger. */ 418 err(EXIT_FAILURE, "tcgetattr"); 419 return; 420 } 421 isterm = 1; 422 traw = tt; 423 cfmakeraw(&traw); 424 traw.c_lflag |= ISIG; 425 (void)tcsetattr(STDOUT_FILENO, TCSANOW, &traw); 426 } 427 428 static void 429 termreset(void) 430 { 431 if (isterm) 432 (void)tcsetattr(STDOUT_FILENO, TCSADRAIN, &tt); 433 434 isterm = 0; 435 } 436 437 static void 438 playback(FILE *fp) 439 { 440 struct timespec tsi, tso; 441 struct stamp stamp; 442 struct stat pst; 443 char buf[DEF_BUF]; 444 off_t nread, save_len; 445 size_t l; 446 time_t tclock; 447 int reg; 448 449 if (fstat(fileno(fp), &pst) == -1) 450 err(EXIT_FAILURE, "fstat failed"); 451 452 reg = S_ISREG(pst.st_mode); 453 454 for (nread = 0; !reg || nread < pst.st_size; nread += save_len) { 455 if (fread(&stamp, sizeof(stamp), 1, fp) != 1) { 456 if (reg) 457 err(EXIT_FAILURE, "reading playback header"); 458 else 459 break; 460 } 461 swapstamp(stamp); 462 save_len = sizeof(stamp); 463 464 if (reg && stamp.scr_len > 465 (uint64_t)(pst.st_size - save_len) - nread) 466 errx(EXIT_FAILURE, "invalid stamp"); 467 468 save_len += stamp.scr_len; 469 tclock = stamp.scr_sec; 470 tso.tv_sec = stamp.scr_sec; 471 tso.tv_nsec = stamp.scr_usec * 1000; 472 473 switch (stamp.scr_direction) { 474 case 's': 475 if (!quiet) 476 (void)printf("Script started on %s", 477 ctime(&tclock)); 478 tsi = tso; 479 (void)consume(fp, stamp.scr_len, buf, reg); 480 termset(); 481 atexit(termreset); 482 break; 483 case 'e': 484 termreset(); 485 if (!quiet) 486 (void)printf("\nScript done on %s", 487 ctime(&tclock)); 488 (void)consume(fp, stamp.scr_len, buf, reg); 489 break; 490 case 'i': 491 /* throw input away */ 492 (void)consume(fp, stamp.scr_len, buf, reg); 493 break; 494 case 'o': 495 tsi.tv_sec = tso.tv_sec - tsi.tv_sec; 496 tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec; 497 if (tsi.tv_nsec < 0) { 498 tsi.tv_sec -= 1; 499 tsi.tv_nsec += 1000000000; 500 } 501 if (usesleep) 502 (void)nanosleep(&tsi, NULL); 503 tsi = tso; 504 while (stamp.scr_len > 0) { 505 l = MIN(DEF_BUF, stamp.scr_len); 506 if (fread(buf, sizeof(char), l, fp) != l) 507 err(EXIT_FAILURE, "cannot read buffer"); 508 509 (void)write(STDOUT_FILENO, buf, l); 510 stamp.scr_len -= l; 511 } 512 break; 513 default: 514 errx(EXIT_FAILURE, "invalid direction %u", 515 stamp.scr_direction); 516 } 517 } 518 (void)fclose(fp); 519 exit(EXIT_SUCCESS); 520 } 521