2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2010, 2012 David E. O'Brien
5 * Copyright (c) 1980, 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 __FBSDID("$FreeBSD$");
36 static const char copyright[] =
37 "@(#) Copyright (c) 1980, 1992, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/ioctl.h>
48 #include <sys/queue.h>
50 #include <sys/endian.h>
51 #include <dev/filemon/filemon.h>
68 uint64_t scr_len; /* amount of data */
69 uint64_t scr_sec; /* time it arrived in seconds... */
70 uint32_t scr_usec; /* ...and microseconds */
71 uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */
75 TAILQ_ENTRY(buf_elm) link;
82 static int master, slave;
84 static const char *fname;
86 static int fflg, qflg, ttyflg;
87 static int usesleep, rawout, showexit;
88 static TAILQ_HEAD(, buf_elm) obuf_list = TAILQ_HEAD_INITIALIZER(obuf_list);
90 static struct termios tt;
93 /* useful for tool and human reading */
94 # define TSTAMP_FMT "%n@ %s [%Y-%m-%d %T]%n"
96 static const char *tstamp_fmt = TSTAMP_FMT;
99 static void done(int) __dead2;
100 static void doshell(char **);
101 static void finish(void);
102 static void record(FILE *, char *, size_t, int);
103 static void consume(FILE *, off_t, char *, int);
104 static void playback(FILE *) __dead2;
105 static void usage(void);
108 main(int argc, char *argv[])
110 struct termios rtt, stt;
112 struct timeval tv, *tvp;
119 int aflg, Fflg, kflg, pflg, ch, k, n, fcm;
120 int flushtime, readstdin;
123 aflg = Fflg = kflg = pflg = 0;
127 fm_fd = -1; /* Shut up stupid "may be used uninitialized" GCC
128 warning. (not needed w/clang) */
131 while ((ch = getopt(argc, argv, "adeFfkpqrT:t:")) != -1)
139 case 'e': /* Default behavior, accepted for linux compat */
160 flushtime = atoi(optarg);
162 err(1, "invalid flush time %d", flushtime);
166 if (strchr(optarg, '%'))
181 fname = "typescript";
183 if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
187 asprintf(&fmfname, "%s.filemon", fname);
189 err(1, "%s.filemon", fname);
190 if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1)
191 err(1, "open(\"/dev/filemon\", O_RDWR)");
192 if ((fm_log = open(fmfname,
193 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
194 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
195 err(1, "open(%s)", fmfname);
196 if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0)
197 err(1, "Cannot set filemon log file descriptor");
203 if (tcgetattr(STDIN_FILENO, &tt) == -1 ||
204 ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) {
205 if (errno != ENOTTY) /* For debugger. */
206 err(1, "tcgetattr/ioctl");
207 if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
210 if (openpty(&master, &slave, NULL, &tt, &win) == -1)
214 fcm = fcntl(master, F_GETFL);
216 err(1, "master F_GETFL");
218 if (fcntl(master, F_SETFL, fcm) == -1)
219 err(1, "master F_SETFL");
222 record(fscript, NULL, 0, 's');
226 (void)printf("Script started, output file is %s\n", fname);
228 (void)fprintf(fscript, "Script started on %s",
232 fprintf(fscript, "Command: ");
233 for (k = 0 ; argv[k] ; ++k)
234 fprintf(fscript, "%s%s", k ? " " : "",
236 fprintf(fscript, "\n");
241 (void)printf("Filemon started, output file is %s\n",
248 rtt.c_lflag &= ~ECHO;
249 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
262 if (ioctl(fm_fd, FILEMON_SET_PID, &pid) < 0)
263 err(1, "Cannot set filemon PID");
270 start = tvec = time(0);
275 FD_SET(master, &rfd);
277 FD_SET(STDIN_FILENO, &rfd);
278 if (!TAILQ_EMPTY(&obuf_list))
279 FD_SET(master, &wfd);
280 if (!readstdin && ttyflg) {
285 } else if (flushtime > 0) {
286 tv.tv_sec = flushtime - (tvec - start);
292 n = select(master + 1, &rfd, &wfd, NULL, tvp);
293 if (n < 0 && errno != EINTR)
295 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
296 cc = read(STDIN_FILENO, ibuf, BUFSIZ);
300 if (tcgetattr(master, &stt) == 0 &&
301 (stt.c_lflag & ICANON) != 0) {
302 (void)write(master, &stt.c_cc[VEOF], 1);
308 record(fscript, ibuf, cc, 'i');
309 be = malloc(sizeof(*be) + cc);
312 memcpy(be->ibuf, ibuf, cc);
313 TAILQ_INSERT_TAIL(&obuf_list, be, link);
316 if (n > 0 && FD_ISSET(master, &wfd)) {
317 while ((be = TAILQ_FIRST(&obuf_list)) != NULL) {
318 cc = write(master, be->ibuf + be->rpos,
321 if (errno == EWOULDBLOCK ||
324 warn("write master");
328 break; /* retry later ? */
329 if (kflg && tcgetattr(master, &stt) >= 0 &&
330 ((stt.c_lflag & ECHO) == 0)) {
331 (void)fwrite(be->ibuf + be->rpos,
336 TAILQ_REMOVE(&obuf_list, be, link);
343 if (n > 0 && FD_ISSET(master, &rfd)) {
344 cc = read(master, obuf, sizeof (obuf));
347 (void)write(STDOUT_FILENO, obuf, cc);
349 record(fscript, obuf, cc, 'o');
351 (void)fwrite(obuf, 1, cc, fscript);
354 if (tvec - start >= flushtime) {
368 (void)fprintf(stderr,
369 "usage: script [-aeFfkpqr] [-t time] [file [command ...]]\n");
370 (void)fprintf(stderr,
371 " script -p [-deq] [-T fmt] [file]\n");
380 if (waitpid(child, &status, 0) == child) {
381 if (WIFEXITED(status))
382 e = WEXITSTATUS(status);
383 else if (WIFSIGNALED(status))
384 e = WTERMSIG(status);
385 else /* can't happen */
396 shell = getenv("SHELL");
398 shell = _PATH_BSHELL;
401 (void)fclose(fscript);
404 setenv("SCRIPT", fname, 1);
409 execl(shell, shell, "-i", (char *)NULL);
421 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
424 record(fscript, NULL, 0, 'e');
428 (void)fprintf(fscript, "\nCommand exit status:"
430 (void)fprintf(fscript,"\nScript done on %s",
433 (void)printf("\nScript done, output file is %s\n", fname);
435 (void)printf("Filemon done, output file is %s\n",
439 (void)fclose(fscript);
445 record(FILE *fp, char *buf, size_t cc, int direction)
451 (void)gettimeofday(&tv, NULL);
453 stamp.scr_sec = tv.tv_sec;
454 stamp.scr_usec = tv.tv_usec;
455 stamp.scr_direction = direction;
456 iov[0].iov_len = sizeof(stamp);
457 iov[0].iov_base = &stamp;
459 iov[1].iov_base = buf;
460 if (writev(fileno(fp), &iov[0], 2) == -1)
465 consume(FILE *fp, off_t len, char *buf, int reg)
470 if (fseeko(fp, len, SEEK_CUR) == -1)
475 l = MIN(DEF_BUF, len);
476 if (fread(buf, sizeof(char), l, fp) != l)
477 err(1, "cannot read buffer");
483 #define swapstamp(stamp) do { \
484 if (stamp.scr_direction > 0xff) { \
485 stamp.scr_len = bswap64(stamp.scr_len); \
486 stamp.scr_sec = bswap64(stamp.scr_sec); \
487 stamp.scr_usec = bswap32(stamp.scr_usec); \
488 stamp.scr_direction = bswap32(stamp.scr_direction); \
490 } while (0/*CONSTCOND*/)
497 if (tcgetattr(STDOUT_FILENO, &tt) == -1) {
498 if (errno != ENOTTY) /* For debugger. */
505 traw.c_lflag |= ISIG;
506 (void)tcsetattr(STDOUT_FILENO, TCSANOW, &traw);
513 tcsetattr(STDOUT_FILENO, TCSADRAIN, &tt);
521 struct timespec tsi, tso;
525 off_t nread, save_len;
531 if (fstat(fileno(fp), &pst) == -1)
532 err(1, "fstat failed");
534 reg = S_ISREG(pst.st_mode);
537 for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
538 if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
540 err(1, "reading playback header");
545 save_len = sizeof(stamp);
547 if (reg && stamp.scr_len >
548 (uint64_t)(pst.st_size - save_len) - nread)
549 errx(1, "invalid stamp");
551 save_len += stamp.scr_len;
552 tclock = stamp.scr_sec;
553 tso.tv_sec = stamp.scr_sec;
554 tso.tv_nsec = stamp.scr_usec * 1000;
558 switch (stamp.scr_direction) {
561 (void)printf("Script started on %s",
564 (void)consume(fp, stamp.scr_len, buf, reg);
571 (void)printf("\nScript done on %s",
573 (void)consume(fp, stamp.scr_len, buf, reg);
576 /* throw input away */
577 (void)consume(fp, stamp.scr_len, buf, reg);
581 if (stamp.scr_len == 0)
583 if (tclock - lclock > 0) {
584 l = strftime(buf, sizeof buf, tstamp_fmt,
586 (void)write(STDOUT_FILENO, buf, l);
590 tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
591 tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
592 if (tsi.tv_nsec < 0) {
594 tsi.tv_nsec += 1000000000;
597 (void)nanosleep(&tsi, NULL);
600 while (stamp.scr_len > 0) {
601 l = MIN(DEF_BUF, stamp.scr_len);
602 if (fread(buf, sizeof(char), l, fp) != l)
603 err(1, "cannot read buffer");
605 (void)write(STDOUT_FILENO, buf, l);
610 errx(1, "invalid direction");