script.c revision 1.27 1 1.27 christos /* $NetBSD: script.c,v 1.27 2020/08/28 17:10:15 christos Exp $ */
2 1.3 jtc
3 1.1 cgd /*
4 1.3 jtc * Copyright (c) 1980, 1992, 1993
5 1.3 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.9 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.5 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.16 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\
35 1.16 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.3 jtc #if 0
40 1.3 jtc static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
41 1.3 jtc #endif
42 1.27 christos __RCSID("$NetBSD: script.c,v 1.27 2020/08/28 17:10:15 christos Exp $");
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/types.h>
46 1.3 jtc #include <sys/wait.h>
47 1.1 cgd #include <sys/stat.h>
48 1.1 cgd #include <sys/ioctl.h>
49 1.1 cgd #include <sys/time.h>
50 1.12 liamjfoy #include <sys/param.h>
51 1.8 atatat #include <sys/uio.h>
52 1.3 jtc
53 1.5 lukem #include <err.h>
54 1.3 jtc #include <errno.h>
55 1.3 jtc #include <fcntl.h>
56 1.3 jtc #include <paths.h>
57 1.3 jtc #include <signal.h>
58 1.1 cgd #include <stdio.h>
59 1.3 jtc #include <stdlib.h>
60 1.3 jtc #include <string.h>
61 1.3 jtc #include <termios.h>
62 1.6 kleink #include <time.h>
63 1.3 jtc #include <tzfile.h>
64 1.3 jtc #include <unistd.h>
65 1.5 lukem #include <util.h>
66 1.1 cgd
67 1.12 liamjfoy #define DEF_BUF 65536
68 1.12 liamjfoy
69 1.8 atatat struct stamp {
70 1.8 atatat uint64_t scr_len; /* amount of data */
71 1.8 atatat uint64_t scr_sec; /* time it arrived in seconds... */
72 1.8 atatat uint32_t scr_usec; /* ...and microseconds */
73 1.8 atatat uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */
74 1.8 atatat };
75 1.8 atatat
76 1.21 joerg static FILE *fscript;
77 1.21 joerg static int master, slave;
78 1.21 joerg static int child, subchild;
79 1.23 christos static size_t outcc;
80 1.21 joerg static int usesleep, rawout;
81 1.21 joerg static int quiet, flush;
82 1.21 joerg static const char *fname;
83 1.21 joerg
84 1.22 christos static int isterm;
85 1.21 joerg static struct termios tt;
86 1.21 joerg
87 1.21 joerg __dead static void done(void);
88 1.21 joerg __dead static void dooutput(void);
89 1.21 joerg __dead static void doshell(const char *);
90 1.21 joerg __dead static void fail(void);
91 1.21 joerg static void finish(int);
92 1.21 joerg static void scriptflush(int);
93 1.21 joerg static void record(FILE *, char *, size_t, int);
94 1.21 joerg static void consume(FILE *, off_t, char *, int);
95 1.27 christos static void childwait(void);
96 1.21 joerg __dead static void playback(FILE *);
97 1.3 jtc
98 1.3 jtc int
99 1.11 rpaulo main(int argc, char *argv[])
100 1.1 cgd {
101 1.23 christos ssize_t scc;
102 1.23 christos size_t cc;
103 1.3 jtc struct termios rtt;
104 1.3 jtc struct winsize win;
105 1.8 atatat int aflg, pflg, ch;
106 1.3 jtc char ibuf[BUFSIZ];
107 1.18 christos const char *command;
108 1.1 cgd
109 1.3 jtc aflg = 0;
110 1.8 atatat pflg = 0;
111 1.8 atatat usesleep = 1;
112 1.8 atatat rawout = 0;
113 1.18 christos quiet = 0;
114 1.18 christos flush = 0;
115 1.18 christos command = NULL;
116 1.18 christos while ((ch = getopt(argc, argv, "ac:dfpqr")) != -1)
117 1.3 jtc switch(ch) {
118 1.1 cgd case 'a':
119 1.3 jtc aflg = 1;
120 1.1 cgd break;
121 1.18 christos case 'c':
122 1.18 christos command = optarg;
123 1.18 christos break;
124 1.8 atatat case 'd':
125 1.8 atatat usesleep = 0;
126 1.8 atatat break;
127 1.18 christos case 'f':
128 1.18 christos flush = 1;
129 1.18 christos break;
130 1.8 atatat case 'p':
131 1.8 atatat pflg = 1;
132 1.8 atatat break;
133 1.18 christos case 'q':
134 1.18 christos quiet = 1;
135 1.18 christos break;
136 1.8 atatat case 'r':
137 1.8 atatat rawout = 1;
138 1.8 atatat break;
139 1.1 cgd case '?':
140 1.1 cgd default:
141 1.18 christos (void)fprintf(stderr,
142 1.18 christos "Usage: %s [-c <command>][-adfpqr] [file]\n",
143 1.10 wiz getprogname());
144 1.23 christos exit(EXIT_FAILURE);
145 1.1 cgd }
146 1.1 cgd argc -= optind;
147 1.1 cgd argv += optind;
148 1.1 cgd
149 1.1 cgd if (argc > 0)
150 1.1 cgd fname = argv[0];
151 1.1 cgd else
152 1.1 cgd fname = "typescript";
153 1.1 cgd
154 1.8 atatat if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
155 1.23 christos err(EXIT_FAILURE, "fopen %s", fname);
156 1.3 jtc
157 1.8 atatat if (pflg)
158 1.8 atatat playback(fscript);
159 1.8 atatat
160 1.25 christos if (tcgetattr(STDIN_FILENO, &tt) == -1 ||
161 1.25 christos ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) {
162 1.26 christos if (errno != ENOTTY) /* For debugger. */
163 1.26 christos err(EXIT_FAILURE, "tcgetattr/ioctl");
164 1.26 christos if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
165 1.26 christos err(EXIT_FAILURE, "openpty");
166 1.25 christos } else {
167 1.23 christos if (openpty(&master, &slave, NULL, &tt, &win) == -1)
168 1.23 christos err(EXIT_FAILURE, "openpty");
169 1.25 christos isterm = 1;
170 1.23 christos }
171 1.1 cgd
172 1.18 christos if (!quiet)
173 1.18 christos (void)printf("Script started, output file is %s\n", fname);
174 1.23 christos
175 1.23 christos if (isterm) {
176 1.23 christos rtt = tt;
177 1.23 christos cfmakeraw(&rtt);
178 1.23 christos rtt.c_lflag &= ~ECHO;
179 1.23 christos (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
180 1.23 christos }
181 1.1 cgd
182 1.3 jtc (void)signal(SIGCHLD, finish);
183 1.1 cgd child = fork();
184 1.23 christos if (child == -1) {
185 1.5 lukem warn("fork");
186 1.1 cgd fail();
187 1.1 cgd }
188 1.1 cgd if (child == 0) {
189 1.1 cgd subchild = child = fork();
190 1.23 christos if (child == -1) {
191 1.5 lukem warn("fork");
192 1.1 cgd fail();
193 1.1 cgd }
194 1.1 cgd if (child)
195 1.1 cgd dooutput();
196 1.1 cgd else
197 1.18 christos doshell(command);
198 1.1 cgd }
199 1.1 cgd
200 1.8 atatat if (!rawout)
201 1.8 atatat (void)fclose(fscript);
202 1.23 christos while ((scc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
203 1.24 christos cc = (size_t)scc;
204 1.8 atatat if (rawout)
205 1.8 atatat record(fscript, ibuf, cc, 'i');
206 1.3 jtc (void)write(master, ibuf, cc);
207 1.8 atatat }
208 1.27 christos childwait();
209 1.5 lukem /* NOTREACHED */
210 1.23 christos return EXIT_SUCCESS;
211 1.1 cgd }
212 1.1 cgd
213 1.21 joerg static void
214 1.27 christos childwait(void)
215 1.27 christos {
216 1.27 christos sigset_t set;
217 1.27 christos
218 1.27 christos sigemptyset(&set);
219 1.27 christos sigsuspend(&set);
220 1.27 christos }
221 1.27 christos
222 1.27 christos static void
223 1.11 rpaulo finish(int signo)
224 1.1 cgd {
225 1.7 christos int die, pid, status;
226 1.1 cgd
227 1.3 jtc die = 0;
228 1.7 christos while ((pid = wait3(&status, WNOHANG, 0)) > 0)
229 1.1 cgd if (pid == child)
230 1.1 cgd die = 1;
231 1.1 cgd
232 1.1 cgd if (die)
233 1.1 cgd done();
234 1.1 cgd }
235 1.1 cgd
236 1.21 joerg static void
237 1.18 christos dooutput(void)
238 1.1 cgd {
239 1.3 jtc struct itimerval value;
240 1.23 christos ssize_t scc;
241 1.23 christos size_t cc;
242 1.3 jtc time_t tvec;
243 1.3 jtc char obuf[BUFSIZ];
244 1.1 cgd
245 1.3 jtc (void)close(STDIN_FILENO);
246 1.3 jtc tvec = time(NULL);
247 1.8 atatat if (rawout)
248 1.8 atatat record(fscript, NULL, 0, 's');
249 1.18 christos else if (!quiet)
250 1.8 atatat (void)fprintf(fscript, "Script started on %s", ctime(&tvec));
251 1.3 jtc
252 1.3 jtc (void)signal(SIGALRM, scriptflush);
253 1.3 jtc value.it_interval.tv_sec = SECSPERMIN / 2;
254 1.3 jtc value.it_interval.tv_usec = 0;
255 1.3 jtc value.it_value = value.it_interval;
256 1.3 jtc (void)setitimer(ITIMER_REAL, &value, NULL);
257 1.1 cgd for (;;) {
258 1.23 christos scc = read(master, obuf, sizeof(obuf));
259 1.23 christos if (scc <= 0)
260 1.1 cgd break;
261 1.23 christos cc = (size_t)scc;
262 1.27 christos (void)write(STDOUT_FILENO, obuf, cc);
263 1.8 atatat if (rawout)
264 1.8 atatat record(fscript, obuf, cc, 'o');
265 1.8 atatat else
266 1.8 atatat (void)fwrite(obuf, 1, cc, fscript);
267 1.3 jtc outcc += cc;
268 1.18 christos if (flush)
269 1.18 christos (void)fflush(fscript);
270 1.1 cgd }
271 1.27 christos childwait();
272 1.1 cgd }
273 1.1 cgd
274 1.21 joerg static void
275 1.11 rpaulo scriptflush(int signo)
276 1.1 cgd {
277 1.3 jtc if (outcc) {
278 1.3 jtc (void)fflush(fscript);
279 1.3 jtc outcc = 0;
280 1.1 cgd }
281 1.1 cgd }
282 1.1 cgd
283 1.21 joerg static void
284 1.18 christos doshell(const char *command)
285 1.1 cgd {
286 1.17 lukem const char *shell;
287 1.3 jtc
288 1.3 jtc (void)close(master);
289 1.3 jtc (void)fclose(fscript);
290 1.3 jtc login_tty(slave);
291 1.18 christos if (command == NULL) {
292 1.18 christos shell = getenv("SHELL");
293 1.18 christos if (shell == NULL)
294 1.18 christos shell = _PATH_BSHELL;
295 1.18 christos execl(shell, shell, "-i", NULL);
296 1.19 christos warn("execl `%s'", shell);
297 1.19 christos } else {
298 1.19 christos if (system(command) == -1)
299 1.19 christos warn("system `%s'", command);
300 1.19 christos }
301 1.18 christos
302 1.3 jtc fail();
303 1.1 cgd }
304 1.1 cgd
305 1.21 joerg static void
306 1.21 joerg fail(void)
307 1.1 cgd {
308 1.1 cgd
309 1.3 jtc (void)kill(0, SIGTERM);
310 1.1 cgd done();
311 1.1 cgd }
312 1.1 cgd
313 1.21 joerg static void
314 1.21 joerg done(void)
315 1.1 cgd {
316 1.3 jtc time_t tvec;
317 1.1 cgd
318 1.1 cgd if (subchild) {
319 1.3 jtc tvec = time(NULL);
320 1.8 atatat if (rawout)
321 1.8 atatat record(fscript, NULL, 0, 'e');
322 1.18 christos else if (!quiet)
323 1.8 atatat (void)fprintf(fscript,"\nScript done on %s",
324 1.8 atatat ctime(&tvec));
325 1.3 jtc (void)fclose(fscript);
326 1.3 jtc (void)close(master);
327 1.1 cgd } else {
328 1.23 christos if (isterm)
329 1.23 christos (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
330 1.18 christos if (!quiet)
331 1.18 christos (void)printf("Script done, output file is %s\n", fname);
332 1.1 cgd }
333 1.23 christos exit(EXIT_SUCCESS);
334 1.8 atatat }
335 1.8 atatat
336 1.21 joerg static void
337 1.17 lukem record(FILE *fp, char *buf, size_t cc, int direction)
338 1.8 atatat {
339 1.8 atatat struct iovec iov[2];
340 1.8 atatat struct stamp stamp;
341 1.8 atatat struct timeval tv;
342 1.8 atatat
343 1.8 atatat (void)gettimeofday(&tv, NULL);
344 1.8 atatat stamp.scr_len = cc;
345 1.8 atatat stamp.scr_sec = tv.tv_sec;
346 1.8 atatat stamp.scr_usec = tv.tv_usec;
347 1.8 atatat stamp.scr_direction = direction;
348 1.8 atatat iov[0].iov_len = sizeof(stamp);
349 1.8 atatat iov[0].iov_base = &stamp;
350 1.8 atatat iov[1].iov_len = cc;
351 1.8 atatat iov[1].iov_base = buf;
352 1.17 lukem if (writev(fileno(fp), &iov[0], 2) == -1)
353 1.23 christos err(EXIT_FAILURE, "writev");
354 1.8 atatat }
355 1.8 atatat
356 1.21 joerg static void
357 1.17 lukem consume(FILE *fp, off_t len, char *buf, int reg)
358 1.14 christos {
359 1.14 christos size_t l;
360 1.14 christos
361 1.14 christos if (reg) {
362 1.17 lukem if (fseeko(fp, len, SEEK_CUR) == -1)
363 1.23 christos err(EXIT_FAILURE, NULL);
364 1.14 christos }
365 1.14 christos else {
366 1.14 christos while (len > 0) {
367 1.14 christos l = MIN(DEF_BUF, len);
368 1.17 lukem if (fread(buf, sizeof(char), l, fp) != l)
369 1.23 christos err(EXIT_FAILURE, "cannot read buffer");
370 1.14 christos len -= l;
371 1.14 christos }
372 1.14 christos }
373 1.14 christos }
374 1.14 christos
375 1.8 atatat #define swapstamp(stamp) do { \
376 1.8 atatat if (stamp.scr_direction > 0xff) { \
377 1.8 atatat stamp.scr_len = bswap64(stamp.scr_len); \
378 1.8 atatat stamp.scr_sec = bswap64(stamp.scr_sec); \
379 1.8 atatat stamp.scr_usec = bswap32(stamp.scr_usec); \
380 1.8 atatat stamp.scr_direction = bswap32(stamp.scr_direction); \
381 1.8 atatat } \
382 1.8 atatat } while (0/*CONSTCOND*/)
383 1.8 atatat
384 1.21 joerg static void
385 1.22 christos termset(void)
386 1.22 christos {
387 1.22 christos struct termios traw;
388 1.22 christos
389 1.25 christos if (tcgetattr(STDOUT_FILENO, &tt) == -1) {
390 1.26 christos if (errno != ENOTTY) /* For debugger. */
391 1.26 christos err(EXIT_FAILURE, "tcgetattr");
392 1.22 christos return;
393 1.25 christos }
394 1.25 christos isterm = 1;
395 1.22 christos traw = tt;
396 1.22 christos cfmakeraw(&traw);
397 1.22 christos traw.c_lflag |= ISIG;
398 1.25 christos (void)tcsetattr(STDOUT_FILENO, TCSANOW, &traw);
399 1.22 christos }
400 1.22 christos
401 1.22 christos static void
402 1.22 christos termreset(void)
403 1.22 christos {
404 1.22 christos if (isterm)
405 1.23 christos (void)tcsetattr(STDOUT_FILENO, TCSADRAIN, &tt);
406 1.22 christos
407 1.22 christos isterm = 0;
408 1.22 christos }
409 1.22 christos
410 1.22 christos static void
411 1.17 lukem playback(FILE *fp)
412 1.8 atatat {
413 1.8 atatat struct timespec tsi, tso;
414 1.8 atatat struct stamp stamp;
415 1.13 christos struct stat pst;
416 1.12 liamjfoy char buf[DEF_BUF];
417 1.12 liamjfoy off_t nread, save_len;
418 1.8 atatat size_t l;
419 1.17 lukem time_t tclock;
420 1.13 christos int reg;
421 1.8 atatat
422 1.17 lukem if (fstat(fileno(fp), &pst) == -1)
423 1.24 christos err(EXIT_FAILURE, "fstat failed");
424 1.12 liamjfoy
425 1.13 christos reg = S_ISREG(pst.st_mode);
426 1.13 christos
427 1.13 christos for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
428 1.17 lukem if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
429 1.13 christos if (reg)
430 1.23 christos err(EXIT_FAILURE, "reading playback header");
431 1.13 christos else
432 1.13 christos break;
433 1.13 christos }
434 1.12 liamjfoy swapstamp(stamp);
435 1.12 liamjfoy save_len = sizeof(stamp);
436 1.8 atatat
437 1.13 christos if (reg && stamp.scr_len >
438 1.13 christos (uint64_t)(pst.st_size - save_len) - nread)
439 1.23 christos errx(EXIT_FAILURE, "invalid stamp");
440 1.12 liamjfoy
441 1.12 liamjfoy save_len += stamp.scr_len;
442 1.17 lukem tclock = stamp.scr_sec;
443 1.8 atatat tso.tv_sec = stamp.scr_sec;
444 1.8 atatat tso.tv_nsec = stamp.scr_usec * 1000;
445 1.8 atatat
446 1.8 atatat switch (stamp.scr_direction) {
447 1.8 atatat case 's':
448 1.18 christos if (!quiet)
449 1.18 christos (void)printf("Script started on %s",
450 1.18 christos ctime(&tclock));
451 1.8 atatat tsi = tso;
452 1.17 lukem (void)consume(fp, stamp.scr_len, buf, reg);
453 1.22 christos termset();
454 1.22 christos atexit(termreset);
455 1.8 atatat break;
456 1.8 atatat case 'e':
457 1.23 christos termreset();
458 1.18 christos if (!quiet)
459 1.18 christos (void)printf("\nScript done on %s",
460 1.18 christos ctime(&tclock));
461 1.17 lukem (void)consume(fp, stamp.scr_len, buf, reg);
462 1.8 atatat break;
463 1.8 atatat case 'i':
464 1.8 atatat /* throw input away */
465 1.17 lukem (void)consume(fp, stamp.scr_len, buf, reg);
466 1.8 atatat break;
467 1.8 atatat case 'o':
468 1.8 atatat tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
469 1.8 atatat tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
470 1.8 atatat if (tsi.tv_nsec < 0) {
471 1.8 atatat tsi.tv_sec -= 1;
472 1.8 atatat tsi.tv_nsec += 1000000000;
473 1.8 atatat }
474 1.8 atatat if (usesleep)
475 1.8 atatat (void)nanosleep(&tsi, NULL);
476 1.8 atatat tsi = tso;
477 1.12 liamjfoy while (stamp.scr_len > 0) {
478 1.12 liamjfoy l = MIN(DEF_BUF, stamp.scr_len);
479 1.17 lukem if (fread(buf, sizeof(char), l, fp) != l)
480 1.23 christos err(EXIT_FAILURE, "cannot read buffer");
481 1.12 liamjfoy
482 1.12 liamjfoy (void)write(STDOUT_FILENO, buf, l);
483 1.12 liamjfoy stamp.scr_len -= l;
484 1.12 liamjfoy }
485 1.8 atatat break;
486 1.12 liamjfoy default:
487 1.23 christos errx(EXIT_FAILURE, "invalid direction %u",
488 1.23 christos stamp.scr_direction);
489 1.8 atatat }
490 1.12 liamjfoy }
491 1.17 lukem (void)fclose(fp);
492 1.23 christos exit(EXIT_SUCCESS);
493 1.1 cgd }
494