2 * Copyright (c) 2010, 2012 David E. O'Brien
3 * Copyright (c) 1980, 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 __FBSDID("$FreeBSD$");
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1992, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
39 static const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/ioctl.h>
47 #include <sys/endian.h>
48 #include <dev/filemon/filemon.h>
65 uint64_t scr_len; /* amount of data */
66 uint64_t scr_sec; /* time it arrived in seconds... */
67 uint32_t scr_usec; /* ...and microseconds */
68 uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */
72 static int master, slave;
74 static const char *fname;
76 static int fflg, qflg, ttyflg;
77 static int usesleep, rawout;
79 static struct termios tt;
81 static void done(int) __dead2;
82 static void doshell(char **);
83 static void fail(void);
84 static void finish(void);
85 static void record(FILE *, char *, size_t, int);
86 static void consume(FILE *, off_t, char *, int);
87 static void playback(FILE *) __dead2;
88 static void usage(void);
91 main(int argc, char *argv[])
94 struct termios rtt, stt;
96 struct timeval tv, *tvp;
101 int aflg, Fflg, kflg, pflg, ch, k, n;
102 int flushtime, readstdin;
105 aflg = Fflg = kflg = pflg = 0;
109 fm_fd = -1; /* Shut up stupid "may be used uninitialized" GCC
110 warning. (not needed w/clang) */
112 while ((ch = getopt(argc, argv, "adFfkpqrt:")) != -1)
139 flushtime = atoi(optarg);
141 err(1, "invalid flush time %d", flushtime);
155 fname = "typescript";
157 if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
161 asprintf(&fmfname, "%s.filemon", fname);
163 err(1, "%s.filemon", fname);
164 if ((fm_fd = open("/dev/filemon", O_RDWR)) == -1)
165 err(1, "open(\"/dev/filemon\", O_RDWR)");
166 if ((fm_log = open(fmfname, O_WRONLY | O_CREAT | O_TRUNC,
167 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
168 err(1, "open(%s)", fmfname);
169 if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0)
170 err(1, "Cannot set filemon log file descriptor");
172 /* Set up these two fd's to close on exec. */
173 (void)fcntl(fm_fd, F_SETFD, FD_CLOEXEC);
174 (void)fcntl(fm_log, F_SETFD, FD_CLOEXEC);
180 if ((ttyflg = isatty(STDIN_FILENO)) != 0) {
181 if (tcgetattr(STDIN_FILENO, &tt) == -1)
183 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
185 if (openpty(&master, &slave, NULL, &tt, &win) == -1)
188 if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
193 record(fscript, NULL, 0, 's');
197 (void)printf("Script started, output file is %s\n", fname);
199 (void)fprintf(fscript, "Script started on %s",
202 fprintf(fscript, "command: ");
203 for (k = 0 ; argv[k] ; ++k)
204 fprintf(fscript, "%s%s", k ? " " : "",
206 fprintf(fscript, "\n");
211 (void)printf("Filemon started, output file is %s\n",
218 rtt.c_lflag &= ~ECHO;
219 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
231 if (fflg && ioctl(fm_fd, FILEMON_SET_PID, &child) < 0)
232 err(1, "Cannot set filemon PID");
234 start = tvec = time(0);
238 FD_SET(master, &rfd);
240 FD_SET(STDIN_FILENO, &rfd);
241 if (!readstdin && ttyflg) {
246 } else if (flushtime > 0) {
247 tv.tv_sec = flushtime - (tvec - start);
253 n = select(master + 1, &rfd, 0, 0, tvp);
254 if (n < 0 && errno != EINTR)
256 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
257 cc = read(STDIN_FILENO, ibuf, BUFSIZ);
261 if (tcgetattr(master, &stt) == 0 &&
262 (stt.c_lflag & ICANON) != 0) {
263 (void)write(master, &stt.c_cc[VEOF], 1);
269 record(fscript, ibuf, cc, 'i');
270 (void)write(master, ibuf, cc);
271 if (kflg && tcgetattr(master, &stt) >= 0 &&
272 ((stt.c_lflag & ECHO) == 0)) {
273 (void)fwrite(ibuf, 1, cc, fscript);
277 if (n > 0 && FD_ISSET(master, &rfd)) {
278 cc = read(master, obuf, sizeof (obuf));
281 (void)write(STDOUT_FILENO, obuf, cc);
283 record(fscript, obuf, cc, 'o');
285 (void)fwrite(obuf, 1, cc, fscript);
288 if (tvec - start >= flushtime) {
302 (void)fprintf(stderr,
303 "usage: script [-adfkpqr] [-t time] [file [command ...]]\n");
312 if (waitpid(child, &status, 0) == child) {
313 if (WIFEXITED(status))
314 e = WEXITSTATUS(status);
315 else if (WIFSIGNALED(status))
316 e = WTERMSIG(status);
317 else /* can't happen */
328 shell = getenv("SHELL");
330 shell = _PATH_BSHELL;
333 (void)fclose(fscript);
336 setenv("SCRIPT", fname, 1);
341 execl(shell, shell, "-i", (char *)NULL);
350 (void)kill(0, SIGTERM);
360 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
363 record(fscript, NULL, 0, 'e');
366 (void)fprintf(fscript,"\nScript done on %s",
368 (void)printf("\nScript done, output file is %s\n", fname);
370 (void)printf("Filemon done, output file is %s\n",
374 (void)fclose(fscript);
380 record(FILE *fp, char *buf, size_t cc, int direction)
386 (void)gettimeofday(&tv, NULL);
388 stamp.scr_sec = tv.tv_sec;
389 stamp.scr_usec = tv.tv_usec;
390 stamp.scr_direction = direction;
391 iov[0].iov_len = sizeof(stamp);
392 iov[0].iov_base = &stamp;
394 iov[1].iov_base = buf;
395 if (writev(fileno(fp), &iov[0], 2) == -1)
400 consume(FILE *fp, off_t len, char *buf, int reg)
405 if (fseeko(fp, len, SEEK_CUR) == -1)
410 l = MIN(DEF_BUF, len);
411 if (fread(buf, sizeof(char), l, fp) != l)
412 err(1, "cannot read buffer");
418 #define swapstamp(stamp) do { \
419 if (stamp.scr_direction > 0xff) { \
420 stamp.scr_len = bswap64(stamp.scr_len); \
421 stamp.scr_sec = bswap64(stamp.scr_sec); \
422 stamp.scr_usec = bswap32(stamp.scr_usec); \
423 stamp.scr_direction = bswap32(stamp.scr_direction); \
425 } while (0/*CONSTCOND*/)
430 struct timespec tsi, tso;
434 off_t nread, save_len;
439 if (fstat(fileno(fp), &pst) == -1)
440 err(1, "fstat failed");
442 reg = S_ISREG(pst.st_mode);
444 for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
445 if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
447 err(1, "reading playback header");
452 save_len = sizeof(stamp);
454 if (reg && stamp.scr_len >
455 (uint64_t)(pst.st_size - save_len) - nread)
456 errx(1, "invalid stamp");
458 save_len += stamp.scr_len;
459 tclock = stamp.scr_sec;
460 tso.tv_sec = stamp.scr_sec;
461 tso.tv_nsec = stamp.scr_usec * 1000;
463 switch (stamp.scr_direction) {
466 (void)printf("Script started on %s",
469 (void)consume(fp, stamp.scr_len, buf, reg);
473 (void)printf("\nScript done on %s",
475 (void)consume(fp, stamp.scr_len, buf, reg);
478 /* throw input away */
479 (void)consume(fp, stamp.scr_len, buf, reg);
482 tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
483 tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
484 if (tsi.tv_nsec < 0) {
486 tsi.tv_nsec += 1000000000;
489 (void)nanosleep(&tsi, NULL);
491 while (stamp.scr_len > 0) {
492 l = MIN(DEF_BUF, stamp.scr_len);
493 if (fread(buf, sizeof(char), l, fp) != l)
494 err(1, "cannot read buffer");
496 (void)write(STDOUT_FILENO, buf, l);
501 errx(1, "invalid direction");