1 /* $NetBSD: script.c,v 1.8 2002/06/21 18:46:31 atatat 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 45 #endif 46 __RCSID("$NetBSD: script.c,v 1.8 2002/06/21 18:46:31 atatat Exp $"); 47 #endif /* not lint */ 48 49 #include <sys/types.h> 50 #include <sys/wait.h> 51 #include <sys/stat.h> 52 #include <sys/ioctl.h> 53 #include <sys/time.h> 54 #include <sys/uio.h> 55 56 #include <err.h> 57 #include <errno.h> 58 #include <fcntl.h> 59 #include <paths.h> 60 #include <signal.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <termios.h> 65 #include <time.h> 66 #include <tzfile.h> 67 #include <unistd.h> 68 #include <util.h> 69 70 struct stamp { 71 uint64_t scr_len; /* amount of data */ 72 uint64_t scr_sec; /* time it arrived in seconds... */ 73 uint32_t scr_usec; /* ...and microseconds */ 74 uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */ 75 }; 76 77 FILE *fscript; 78 int master, slave; 79 int child, subchild; 80 int outcc; 81 int usesleep, rawout; 82 char *fname; 83 84 struct termios tt; 85 86 void done __P((void)); 87 void dooutput __P((void)); 88 void doshell __P((void)); 89 void fail __P((void)); 90 void finish __P((int)); 91 int main __P((int, char **)); 92 void scriptflush __P((int)); 93 void record __P((FILE *, char *, size_t, int)); 94 void playback __P((FILE *)); 95 96 int 97 main(argc, argv) 98 int argc; 99 char *argv[]; 100 { 101 int cc; 102 struct termios rtt; 103 struct winsize win; 104 int aflg, pflg, ch; 105 char ibuf[BUFSIZ]; 106 107 aflg = 0; 108 pflg = 0; 109 usesleep = 1; 110 rawout = 0; 111 while ((ch = getopt(argc, argv, "adpr")) != -1) 112 switch(ch) { 113 case 'a': 114 aflg = 1; 115 break; 116 case 'd': 117 usesleep = 0; 118 break; 119 case 'p': 120 pflg = 1; 121 break; 122 case 'r': 123 rawout = 1; 124 break; 125 case '?': 126 default: 127 (void)fprintf(stderr, "usage: script [-apr] [file]\n"); 128 exit(1); 129 } 130 argc -= optind; 131 argv += optind; 132 133 if (argc > 0) 134 fname = argv[0]; 135 else 136 fname = "typescript"; 137 138 if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL) 139 err(1, "fopen %s", fname); 140 141 if (pflg) 142 playback(fscript); 143 144 (void)tcgetattr(STDIN_FILENO, &tt); 145 (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win); 146 if (openpty(&master, &slave, NULL, &tt, &win) == -1) 147 err(1, "openpty"); 148 149 (void)printf("Script started, output file is %s\n", fname); 150 rtt = tt; 151 cfmakeraw(&rtt); 152 rtt.c_lflag &= ~ECHO; 153 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 154 155 (void)signal(SIGCHLD, finish); 156 child = fork(); 157 if (child < 0) { 158 warn("fork"); 159 fail(); 160 } 161 if (child == 0) { 162 subchild = child = fork(); 163 if (child < 0) { 164 warn("fork"); 165 fail(); 166 } 167 if (child) 168 dooutput(); 169 else 170 doshell(); 171 } 172 173 if (!rawout) 174 (void)fclose(fscript); 175 while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) { 176 if (rawout) 177 record(fscript, ibuf, cc, 'i'); 178 (void)write(master, ibuf, cc); 179 } 180 done(); 181 /* NOTREACHED */ 182 return (0); 183 } 184 185 void 186 finish(signo) 187 int signo; 188 { 189 int die, pid, status; 190 191 die = 0; 192 while ((pid = wait3(&status, WNOHANG, 0)) > 0) 193 if (pid == child) 194 die = 1; 195 196 if (die) 197 done(); 198 } 199 200 void 201 dooutput() 202 { 203 struct itimerval value; 204 int cc; 205 time_t tvec; 206 char obuf[BUFSIZ]; 207 208 (void)close(STDIN_FILENO); 209 tvec = time(NULL); 210 if (rawout) 211 record(fscript, NULL, 0, 's'); 212 else 213 (void)fprintf(fscript, "Script started on %s", ctime(&tvec)); 214 215 (void)signal(SIGALRM, scriptflush); 216 value.it_interval.tv_sec = SECSPERMIN / 2; 217 value.it_interval.tv_usec = 0; 218 value.it_value = value.it_interval; 219 (void)setitimer(ITIMER_REAL, &value, NULL); 220 for (;;) { 221 cc = read(master, obuf, sizeof (obuf)); 222 if (cc <= 0) 223 break; 224 (void)write(1, obuf, cc); 225 if (rawout) 226 record(fscript, obuf, cc, 'o'); 227 else 228 (void)fwrite(obuf, 1, cc, fscript); 229 outcc += cc; 230 } 231 done(); 232 } 233 234 void 235 scriptflush(signo) 236 int signo; 237 { 238 if (outcc) { 239 (void)fflush(fscript); 240 outcc = 0; 241 } 242 } 243 244 void 245 doshell() 246 { 247 char *shell; 248 249 shell = getenv("SHELL"); 250 if (shell == NULL) 251 shell = _PATH_BSHELL; 252 253 (void)close(master); 254 (void)fclose(fscript); 255 login_tty(slave); 256 execl(shell, shell, "-i", NULL); 257 warn("execl %s", shell); 258 fail(); 259 } 260 261 void 262 fail() 263 { 264 265 (void)kill(0, SIGTERM); 266 done(); 267 } 268 269 void 270 done() 271 { 272 time_t tvec; 273 274 if (subchild) { 275 tvec = time(NULL); 276 if (rawout) 277 record(fscript, NULL, 0, 'e'); 278 else 279 (void)fprintf(fscript,"\nScript done on %s", 280 ctime(&tvec)); 281 (void)fclose(fscript); 282 (void)close(master); 283 } else { 284 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 285 (void)printf("Script done, output file is %s\n", fname); 286 } 287 exit(0); 288 } 289 290 void 291 record(fscript, buf, cc, direction) 292 FILE *fscript; 293 char *buf; 294 size_t cc; 295 int direction; 296 { 297 struct iovec iov[2]; 298 struct stamp stamp; 299 struct timeval tv; 300 301 (void)gettimeofday(&tv, NULL); 302 stamp.scr_len = cc; 303 stamp.scr_sec = tv.tv_sec; 304 stamp.scr_usec = tv.tv_usec; 305 stamp.scr_direction = direction; 306 iov[0].iov_len = sizeof(stamp); 307 iov[0].iov_base = &stamp; 308 iov[1].iov_len = cc; 309 iov[1].iov_base = buf; 310 if (writev(fileno(fscript), &iov[0], 2) == -1) 311 err(1, "writev"); 312 } 313 314 #define swapstamp(stamp) do { \ 315 if (stamp.scr_direction > 0xff) { \ 316 stamp.scr_len = bswap64(stamp.scr_len); \ 317 stamp.scr_sec = bswap64(stamp.scr_sec); \ 318 stamp.scr_usec = bswap32(stamp.scr_usec); \ 319 stamp.scr_direction = bswap32(stamp.scr_direction); \ 320 } \ 321 } while (0/*CONSTCOND*/) 322 323 void 324 playback(fscript) 325 FILE *fscript; 326 { 327 struct timespec tsi, tso; 328 struct stamp stamp; 329 char buf[BUFSIZ]; 330 size_t l; 331 time_t clock; 332 333 do { 334 if (fread(&stamp, sizeof(stamp), 1, fscript) != 1) 335 err(1, "reading playback header"); 336 337 swapstamp(stamp); 338 l = fread(buf, 1, stamp.scr_len, fscript); 339 clock = stamp.scr_sec; 340 tso.tv_sec = stamp.scr_sec; 341 tso.tv_nsec = stamp.scr_usec * 1000; 342 343 switch (stamp.scr_direction) { 344 case 's': 345 (void)printf("Script started on %s", ctime(&clock)); 346 tsi = tso; 347 break; 348 case 'e': 349 (void)printf("\nScript done on %s", ctime(&clock)); 350 break; 351 case 'i': 352 /* throw input away */ 353 break; 354 case 'o': 355 tsi.tv_sec = tso.tv_sec - tsi.tv_sec; 356 tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec; 357 if (tsi.tv_nsec < 0) { 358 tsi.tv_sec -= 1; 359 tsi.tv_nsec += 1000000000; 360 } 361 if (usesleep) 362 (void)nanosleep(&tsi, NULL); 363 tsi = tso; 364 (void)write(STDOUT_FILENO, buf, l); 365 break; 366 } 367 } while (stamp.scr_direction != 'e'); 368 369 (void)fclose(fscript); 370 exit(0); 371 } 372