]> git.saurik.com Git - apple/shell_cmds.git/blame - script/script.c
shell_cmds-207.11.1.tar.gz
[apple/shell_cmds.git] / script / script.c
CommitLineData
44bd5ea7 1/*
b5fe885e 2 * Copyright (c) 2010, 2012 David E. O'Brien
44bd5ea7
A
3 * Copyright (c) 1980, 1992, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
44bd5ea7
A
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.
17 *
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
28 * SUCH DAMAGE.
29 */
30
b5fe885e
A
31#include <sys/param.h>
32__FBSDID("$FreeBSD$");
44bd5ea7 33#ifndef lint
e1a085ba
A
34static const char copyright[] =
35"@(#) Copyright (c) 1980, 1992, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
37#endif
44bd5ea7 38#ifndef lint
e1a085ba 39static const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
44bd5ea7 40#endif
44bd5ea7 41
44bd5ea7
A
42#include <sys/wait.h>
43#include <sys/stat.h>
44#include <sys/ioctl.h>
45#include <sys/time.h>
b5fe885e
A
46#include <sys/uio.h>
47#ifdef __APPLE__
48#include <libkern/OSByteOrder.h>
49#else
50#include <sys/endian.h>
51#endif
52#ifdef ENABLE_FILEMON
53#include <dev/filemon/filemon.h>
54#endif /* ENABLE_FILEMON */
44bd5ea7
A
55
56#include <err.h>
57#include <errno.h>
58#include <fcntl.h>
b5fe885e
A
59#ifdef __APPLE__
60#include <util.h>
61#else
e1a085ba 62#include <libutil.h>
b5fe885e 63#endif
44bd5ea7
A
64#include <paths.h>
65#include <signal.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
69#include <termios.h>
44bd5ea7 70#include <unistd.h>
44bd5ea7 71
b5fe885e 72#define DEF_BUF 65536
44bd5ea7 73
b5fe885e
A
74struct stamp {
75 uint64_t scr_len; /* amount of data */
76 uint64_t scr_sec; /* time it arrived in seconds... */
77 uint32_t scr_usec; /* ...and microseconds */
78 uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */
79};
44bd5ea7 80
b5fe885e
A
81static FILE *fscript;
82static int master, slave;
83static int child;
84static const char *fname;
85static char *fmfname;
86#ifdef ENABLE_FILEMON
87static int fflg, qflg, ttyflg;
88#else /* !ENABLE_FILEMON */
89static int qflg, ttyflg;
90#endif /* ENABLE_FILEMON */
91static int usesleep, rawout;
92
93static struct termios tt;
94
95static void done(int) __dead2;
96static void doshell(char **);
97static void fail(void);
98static void finish(void);
99static void record(FILE *, char *, size_t, int);
100static void consume(FILE *, off_t, char *, int);
101static void playback(FILE *) __dead2;
e1a085ba 102static void usage(void);
44bd5ea7
A
103
104int
e1a085ba 105main(int argc, char *argv[])
44bd5ea7
A
106{
107 int cc;
e1a085ba 108 struct termios rtt, stt;
44bd5ea7 109 struct winsize win;
e1a085ba
A
110 struct timeval tv, *tvp;
111 time_t tvec, start;
112 char obuf[BUFSIZ];
44bd5ea7 113 char ibuf[BUFSIZ];
e1a085ba 114 fd_set rfd;
b5fe885e
A
115 int aflg, Fflg, kflg, pflg, ch, k, n;
116 int flushtime, readstdin;
117#ifdef ENABLE_FILEMON
118 int fm_fd, fm_log;
119#endif /* ENABLE_FILEMON */
120
121 aflg = Fflg = kflg = pflg = 0;
122 usesleep = 1;
123 rawout = 0;
124 flushtime = 30;
125#ifdef ENABLE_FILEMON
126 fm_fd = -1; /* Shut up stupid "may be used uninitialized" GCC
127 warning. (not needed w/clang) */
128#endif /* ENABLE_FILEMON */
44bd5ea7 129
b5fe885e
A
130#ifdef ENABLE_FILEMON
131 while ((ch = getopt(argc, argv, "adFfkpqrt:")) != -1)
132#else /* !ENABLE_FILEMON */
133 while ((ch = getopt(argc, argv, "adFkpqrt:")) != -1)
134#endif /* ENABLE_FILEMON */
44bd5ea7
A
135 switch(ch) {
136 case 'a':
137 aflg = 1;
138 break;
b5fe885e
A
139 case 'd':
140 usesleep = 0;
e1a085ba 141 break;
b5fe885e
A
142 case 'F':
143 Fflg = 1;
144 break;
145#ifdef ENABLE_FILEMON
146 case 'f':
147 fflg = 1;
148 break;
149#endif /* ENABLE_FILEMON */
e1a085ba
A
150 case 'k':
151 kflg = 1;
152 break;
b5fe885e
A
153 case 'p':
154 pflg = 1;
155 break;
156 case 'q':
157 qflg = 1;
158 break;
159 case 'r':
160 rawout = 1;
161 break;
e1a085ba
A
162 case 't':
163 flushtime = atoi(optarg);
164 if (flushtime < 0)
165 err(1, "invalid flush time %d", flushtime);
166 break;
44bd5ea7
A
167 case '?':
168 default:
e1a085ba 169 usage();
44bd5ea7
A
170 }
171 argc -= optind;
172 argv += optind;
173
e1a085ba 174 if (argc > 0) {
44bd5ea7 175 fname = argv[0];
e1a085ba
A
176 argv++;
177 argc--;
178 } else
44bd5ea7
A
179 fname = "typescript";
180
b5fe885e 181 if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
e1a085ba 182 err(1, "%s", fname);
44bd5ea7 183
b5fe885e
A
184#ifdef ENABLE_FILEMON
185 if (fflg) {
186 asprintf(&fmfname, "%s.filemon", fname);
187 if (!fmfname)
188 err(1, "%s.filemon", fname);
189 if ((fm_fd = open("/dev/filemon", O_RDWR)) == -1)
190 err(1, "open(\"/dev/filemon\", O_RDWR)");
191 if ((fm_log = open(fmfname, O_WRONLY | O_CREAT | O_TRUNC,
192 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
193 err(1, "open(%s)", fmfname);
194 if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0)
195 err(1, "Cannot set filemon log file descriptor");
196
197 /* Set up these two fd's to close on exec. */
198 (void)fcntl(fm_fd, F_SETFD, FD_CLOEXEC);
199 (void)fcntl(fm_log, F_SETFD, FD_CLOEXEC);
200 }
201#endif /* ENABLE_FILEMON */
202
203 if (pflg)
204 playback(fscript);
205
206 if ((ttyflg = isatty(STDIN_FILENO)) != 0) {
e1a085ba
A
207 if (tcgetattr(STDIN_FILENO, &tt) == -1)
208 err(1, "tcgetattr");
209 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
210 err(1, "ioctl");
211 if (openpty(&master, &slave, NULL, &tt, &win) == -1)
212 err(1, "openpty");
213 } else {
214 if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
215 err(1, "openpty");
216 }
44bd5ea7 217
b5fe885e
A
218 if (rawout)
219 record(fscript, NULL, 0, 's');
220
e1a085ba
A
221 if (!qflg) {
222 tvec = time(NULL);
223 (void)printf("Script started, output file is %s\n", fname);
b5fe885e
A
224 if (!rawout) {
225 (void)fprintf(fscript, "Script started on %s",
226 ctime(&tvec));
227 if (argv[0]) {
228 fprintf(fscript, "command: ");
229 for (k = 0 ; argv[k] ; ++k)
230 fprintf(fscript, "%s%s", k ? " " : "",
231 argv[k]);
232 fprintf(fscript, "\n");
233 }
234 }
e1a085ba 235 fflush(fscript);
b5fe885e
A
236#ifdef ENABLE_FILEMON
237 if (fflg) {
238 (void)printf("Filemon started, output file is %s\n",
239 fmfname);
240 }
241#endif /* ENABLE_FILEMON */
e1a085ba
A
242 }
243 if (ttyflg) {
244 rtt = tt;
245 cfmakeraw(&rtt);
246 rtt.c_lflag &= ~ECHO;
247 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
248 }
44bd5ea7 249
44bd5ea7
A
250 child = fork();
251 if (child < 0) {
252 warn("fork");
e1a085ba 253 done(1);
44bd5ea7 254 }
e1a085ba
A
255 if (child == 0)
256 doshell(argv);
b5fe885e 257 close(slave);
e1a085ba 258
b5fe885e
A
259#ifdef ENABLE_FILEMON
260 if (fflg && ioctl(fm_fd, FILEMON_SET_PID, &child) < 0)
261 err(1, "Cannot set filemon PID");
262#endif /* ENABLE_FILEMON */
e1a085ba 263
b5fe885e
A
264 start = tvec = time(0);
265 readstdin = 1;
e1a085ba 266 for (;;) {
b5fe885e 267 FD_ZERO(&rfd);
e1a085ba 268 FD_SET(master, &rfd);
b5fe885e
A
269 if (readstdin)
270 FD_SET(STDIN_FILENO, &rfd);
271 if (!readstdin && ttyflg) {
272 tv.tv_sec = 1;
273 tv.tv_usec = 0;
274 tvp = &tv;
275 readstdin = 1;
276 } else if (flushtime > 0) {
277 tv.tv_sec = flushtime - (tvec - start);
e1a085ba 278 tv.tv_usec = 0;
b5fe885e
A
279 tvp = &tv;
280 } else {
281 tvp = NULL;
e1a085ba
A
282 }
283 n = select(master + 1, &rfd, 0, 0, tvp);
284 if (n < 0 && errno != EINTR)
285 break;
286 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
287 cc = read(STDIN_FILENO, ibuf, BUFSIZ);
288 if (cc < 0)
289 break;
b5fe885e
A
290 if (cc == 0) {
291 if (tcgetattr(master, &stt) == 0 &&
292 (stt.c_lflag & ICANON) != 0) {
293 (void)write(master, &stt.c_cc[VEOF], 1);
294 }
295 readstdin = 0;
296 }
e1a085ba 297 if (cc > 0) {
b5fe885e
A
298 if (rawout)
299 record(fscript, ibuf, cc, 'i');
e1a085ba
A
300 (void)write(master, ibuf, cc);
301 if (kflg && tcgetattr(master, &stt) >= 0 &&
302 ((stt.c_lflag & ECHO) == 0)) {
303 (void)fwrite(ibuf, 1, cc, fscript);
304 }
305 }
306 }
307 if (n > 0 && FD_ISSET(master, &rfd)) {
308 cc = read(master, obuf, sizeof (obuf));
309 if (cc <= 0)
310 break;
311 (void)write(STDOUT_FILENO, obuf, cc);
b5fe885e
A
312 if (rawout)
313 record(fscript, obuf, cc, 'o');
314 else
315 (void)fwrite(obuf, 1, cc, fscript);
e1a085ba
A
316 }
317 tvec = time(0);
318 if (tvec - start >= flushtime) {
319 fflush(fscript);
320 start = tvec;
44bd5ea7 321 }
b5fe885e
A
322 if (Fflg)
323 fflush(fscript);
44bd5ea7 324 }
e1a085ba
A
325 finish();
326 done(0);
44bd5ea7
A
327}
328
e1a085ba
A
329static void
330usage(void)
44bd5ea7 331{
e1a085ba 332 (void)fprintf(stderr,
b5fe885e
A
333#ifdef ENABLE_FILEMON
334 "usage: script [-adfkpqr] [-t time] [file [command ...]]\n");
335#else /* !ENABLE_FILEMON */
336 "usage: script [-adkpqr] [-t time] [file [command ...]]\n");
337#endif /* ENABLE_FILEMON */
e1a085ba 338 exit(1);
44bd5ea7
A
339}
340
b5fe885e 341static void
e1a085ba 342finish(void)
44bd5ea7 343{
b5fe885e 344 int e, status;
44bd5ea7 345
b5fe885e
A
346 if (waitpid(child, &status, 0) == child) {
347 if (WIFEXITED(status))
348 e = WEXITSTATUS(status);
349 else if (WIFSIGNALED(status))
350 e = WTERMSIG(status);
351 else /* can't happen */
352 e = 1;
e1a085ba 353 done(e);
b5fe885e 354 }
44bd5ea7
A
355}
356
b5fe885e 357static void
e1a085ba 358doshell(char **av)
44bd5ea7 359{
e1a085ba 360 const char *shell;
44bd5ea7
A
361
362 shell = getenv("SHELL");
363 if (shell == NULL)
364 shell = _PATH_BSHELL;
365
366 (void)close(master);
367 (void)fclose(fscript);
b5fe885e 368 free(fmfname);
44bd5ea7 369 login_tty(slave);
b5fe885e 370 setenv("SCRIPT", fname, 1);
e1a085ba
A
371 if (av[0]) {
372 execvp(av[0], av);
373 warn("%s", av[0]);
374 } else {
375 execl(shell, shell, "-i", (char *)NULL);
376 warn("%s", shell);
377 }
44bd5ea7
A
378 fail();
379}
380
b5fe885e 381static void
e1a085ba 382fail(void)
44bd5ea7 383{
44bd5ea7 384 (void)kill(0, SIGTERM);
e1a085ba 385 done(1);
44bd5ea7
A
386}
387
b5fe885e 388static void
e1a085ba 389done(int eno)
44bd5ea7
A
390{
391 time_t tvec;
392
e1a085ba 393 if (ttyflg)
44bd5ea7 394 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
e1a085ba 395 tvec = time(NULL);
b5fe885e
A
396 if (rawout)
397 record(fscript, NULL, 0, 'e');
e1a085ba 398 if (!qflg) {
b5fe885e
A
399 if (!rawout)
400 (void)fprintf(fscript,"\nScript done on %s",
401 ctime(&tvec));
e1a085ba 402 (void)printf("\nScript done, output file is %s\n", fname);
b5fe885e
A
403#ifdef ENABLE_FILEMON
404 if (fflg) {
405 (void)printf("Filemon done, output file is %s\n",
406 fmfname);
407 }
408#endif /* ENABLE_FILEMON */
44bd5ea7 409 }
e1a085ba
A
410 (void)fclose(fscript);
411 (void)close(master);
412 exit(eno);
44bd5ea7 413}
b5fe885e
A
414
415static void
416record(FILE *fp, char *buf, size_t cc, int direction)
417{
418 struct iovec iov[2];
419 struct stamp stamp;
420 struct timeval tv;
421
422 (void)gettimeofday(&tv, NULL);
423 stamp.scr_len = cc;
424 stamp.scr_sec = tv.tv_sec;
425 stamp.scr_usec = tv.tv_usec;
426 stamp.scr_direction = direction;
427 iov[0].iov_len = sizeof(stamp);
428 iov[0].iov_base = &stamp;
429 iov[1].iov_len = cc;
430 iov[1].iov_base = buf;
431 if (writev(fileno(fp), &iov[0], 2) == -1)
432 err(1, "writev");
433}
434
435static void
436consume(FILE *fp, off_t len, char *buf, int reg)
437{
438 size_t l;
439
440 if (reg) {
441 if (fseeko(fp, len, SEEK_CUR) == -1)
442 err(1, NULL);
443 }
444 else {
445 while (len > 0) {
446 l = MIN(DEF_BUF, len);
447 if (fread(buf, sizeof(char), l, fp) != l)
448 err(1, "cannot read buffer");
449 len -= l;
450 }
451 }
452}
453
454#ifdef __APPLE__
455#define bswap32 OSSwapInt32
456#define bswap64 OSSwapInt64
457#endif /* __APPLE__ */
458#define swapstamp(stamp) do { \
459 if (stamp.scr_direction > 0xff) { \
460 stamp.scr_len = bswap64(stamp.scr_len); \
461 stamp.scr_sec = bswap64(stamp.scr_sec); \
462 stamp.scr_usec = bswap32(stamp.scr_usec); \
463 stamp.scr_direction = bswap32(stamp.scr_direction); \
464 } \
465} while (0/*CONSTCOND*/)
466
467static void
468playback(FILE *fp)
469{
470 struct timespec tsi, tso;
471 struct stamp stamp;
472 struct stat pst;
473 char buf[DEF_BUF];
474 off_t nread, save_len;
475 size_t l;
476 time_t tclock;
477 int reg;
478
479 if (fstat(fileno(fp), &pst) == -1)
480 err(1, "fstat failed");
481
482 reg = S_ISREG(pst.st_mode);
483
484 for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
485 if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
486 if (reg)
487 err(1, "reading playback header");
488 else
489 break;
490 }
491 swapstamp(stamp);
492 save_len = sizeof(stamp);
493
494 if (reg && stamp.scr_len >
495 (uint64_t)(pst.st_size - save_len) - nread)
496 errx(1, "invalid stamp");
497
498 save_len += stamp.scr_len;
499 tclock = stamp.scr_sec;
500 tso.tv_sec = stamp.scr_sec;
501 tso.tv_nsec = stamp.scr_usec * 1000;
502
503 switch (stamp.scr_direction) {
504 case 's':
505 if (!qflg)
506 (void)printf("Script started on %s",
507 ctime(&tclock));
508 tsi = tso;
509 (void)consume(fp, stamp.scr_len, buf, reg);
510 break;
511 case 'e':
512 if (!qflg)
513 (void)printf("\nScript done on %s",
514 ctime(&tclock));
515 (void)consume(fp, stamp.scr_len, buf, reg);
516 break;
517 case 'i':
518 /* throw input away */
519 (void)consume(fp, stamp.scr_len, buf, reg);
520 break;
521 case 'o':
522 tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
523 tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
524 if (tsi.tv_nsec < 0) {
525 tsi.tv_sec -= 1;
526 tsi.tv_nsec += 1000000000;
527 }
528 if (usesleep)
529 (void)nanosleep(&tsi, NULL);
530 tsi = tso;
531 while (stamp.scr_len > 0) {
532 l = MIN(DEF_BUF, stamp.scr_len);
533 if (fread(buf, sizeof(char), l, fp) != l)
534 err(1, "cannot read buffer");
535
536 (void)write(STDOUT_FILENO, buf, l);
537 stamp.scr_len -= l;
538 }
539 break;
540 default:
541 errx(1, "invalid direction");
542 }
543 }
544 (void)fclose(fp);
545 exit(0);
546}