script.c revision 1.18 1 1.18 christos /* $NetBSD: script.c,v 1.18 2009/10/17 19:05:54 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.18 christos __RCSID("$NetBSD: script.c,v 1.18 2009/10/17 19:05:54 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.1 cgd FILE *fscript;
77 1.3 jtc int master, slave;
78 1.3 jtc int child, subchild;
79 1.3 jtc int outcc;
80 1.8 atatat int usesleep, rawout;
81 1.18 christos int quiet, flush;
82 1.17 lukem const char *fname;
83 1.1 cgd
84 1.1 cgd struct termios tt;
85 1.1 cgd
86 1.11 rpaulo void done(void);
87 1.11 rpaulo void dooutput(void);
88 1.18 christos void doshell(const char *);
89 1.11 rpaulo void fail(void);
90 1.11 rpaulo void finish(int);
91 1.11 rpaulo int main(int, char **);
92 1.11 rpaulo void scriptflush(int);
93 1.11 rpaulo void record(FILE *, char *, size_t, int);
94 1.14 christos void consume(FILE *, off_t, char *, int);
95 1.11 rpaulo void playback(FILE *);
96 1.3 jtc
97 1.3 jtc int
98 1.11 rpaulo main(int argc, char *argv[])
99 1.1 cgd {
100 1.5 lukem int cc;
101 1.3 jtc struct termios rtt;
102 1.3 jtc struct winsize win;
103 1.8 atatat int aflg, pflg, ch;
104 1.3 jtc char ibuf[BUFSIZ];
105 1.18 christos const char *command;
106 1.1 cgd
107 1.3 jtc aflg = 0;
108 1.8 atatat pflg = 0;
109 1.8 atatat usesleep = 1;
110 1.8 atatat rawout = 0;
111 1.18 christos quiet = 0;
112 1.18 christos flush = 0;
113 1.18 christos command = NULL;
114 1.18 christos while ((ch = getopt(argc, argv, "ac:dfpqr")) != -1)
115 1.3 jtc switch(ch) {
116 1.1 cgd case 'a':
117 1.3 jtc aflg = 1;
118 1.1 cgd break;
119 1.18 christos case 'c':
120 1.18 christos command = optarg;
121 1.18 christos break;
122 1.8 atatat case 'd':
123 1.8 atatat usesleep = 0;
124 1.8 atatat break;
125 1.18 christos case 'f':
126 1.18 christos flush = 1;
127 1.18 christos break;
128 1.8 atatat case 'p':
129 1.8 atatat pflg = 1;
130 1.8 atatat break;
131 1.18 christos case 'q':
132 1.18 christos quiet = 1;
133 1.18 christos break;
134 1.8 atatat case 'r':
135 1.8 atatat rawout = 1;
136 1.8 atatat break;
137 1.1 cgd case '?':
138 1.1 cgd default:
139 1.18 christos (void)fprintf(stderr,
140 1.18 christos "Usage: %s [-c <command>][-adfpqr] [file]\n",
141 1.10 wiz getprogname());
142 1.1 cgd exit(1);
143 1.1 cgd }
144 1.1 cgd argc -= optind;
145 1.1 cgd argv += optind;
146 1.1 cgd
147 1.1 cgd if (argc > 0)
148 1.1 cgd fname = argv[0];
149 1.1 cgd else
150 1.1 cgd fname = "typescript";
151 1.1 cgd
152 1.8 atatat if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
153 1.5 lukem err(1, "fopen %s", fname);
154 1.3 jtc
155 1.8 atatat if (pflg)
156 1.8 atatat playback(fscript);
157 1.8 atatat
158 1.3 jtc (void)tcgetattr(STDIN_FILENO, &tt);
159 1.3 jtc (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
160 1.3 jtc if (openpty(&master, &slave, NULL, &tt, &win) == -1)
161 1.5 lukem err(1, "openpty");
162 1.1 cgd
163 1.18 christos if (!quiet)
164 1.18 christos (void)printf("Script started, output file is %s\n", fname);
165 1.3 jtc rtt = tt;
166 1.3 jtc cfmakeraw(&rtt);
167 1.3 jtc rtt.c_lflag &= ~ECHO;
168 1.3 jtc (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
169 1.1 cgd
170 1.3 jtc (void)signal(SIGCHLD, finish);
171 1.1 cgd child = fork();
172 1.1 cgd if (child < 0) {
173 1.5 lukem warn("fork");
174 1.1 cgd fail();
175 1.1 cgd }
176 1.1 cgd if (child == 0) {
177 1.1 cgd subchild = child = fork();
178 1.1 cgd if (child < 0) {
179 1.5 lukem warn("fork");
180 1.1 cgd fail();
181 1.1 cgd }
182 1.1 cgd if (child)
183 1.1 cgd dooutput();
184 1.1 cgd else
185 1.18 christos doshell(command);
186 1.1 cgd }
187 1.1 cgd
188 1.8 atatat if (!rawout)
189 1.8 atatat (void)fclose(fscript);
190 1.8 atatat while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
191 1.8 atatat if (rawout)
192 1.8 atatat record(fscript, ibuf, cc, 'i');
193 1.3 jtc (void)write(master, ibuf, cc);
194 1.8 atatat }
195 1.1 cgd done();
196 1.5 lukem /* NOTREACHED */
197 1.5 lukem return (0);
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd void
201 1.11 rpaulo finish(int signo)
202 1.1 cgd {
203 1.7 christos int die, pid, status;
204 1.1 cgd
205 1.3 jtc die = 0;
206 1.7 christos while ((pid = wait3(&status, WNOHANG, 0)) > 0)
207 1.1 cgd if (pid == child)
208 1.1 cgd die = 1;
209 1.1 cgd
210 1.1 cgd if (die)
211 1.1 cgd done();
212 1.1 cgd }
213 1.1 cgd
214 1.3 jtc void
215 1.18 christos dooutput(void)
216 1.1 cgd {
217 1.3 jtc struct itimerval value;
218 1.5 lukem int cc;
219 1.3 jtc time_t tvec;
220 1.3 jtc char obuf[BUFSIZ];
221 1.1 cgd
222 1.3 jtc (void)close(STDIN_FILENO);
223 1.3 jtc tvec = time(NULL);
224 1.8 atatat if (rawout)
225 1.8 atatat record(fscript, NULL, 0, 's');
226 1.18 christos else if (!quiet)
227 1.8 atatat (void)fprintf(fscript, "Script started on %s", ctime(&tvec));
228 1.3 jtc
229 1.3 jtc (void)signal(SIGALRM, scriptflush);
230 1.3 jtc value.it_interval.tv_sec = SECSPERMIN / 2;
231 1.3 jtc value.it_interval.tv_usec = 0;
232 1.3 jtc value.it_value = value.it_interval;
233 1.3 jtc (void)setitimer(ITIMER_REAL, &value, NULL);
234 1.1 cgd for (;;) {
235 1.1 cgd cc = read(master, obuf, sizeof (obuf));
236 1.1 cgd if (cc <= 0)
237 1.1 cgd break;
238 1.3 jtc (void)write(1, obuf, cc);
239 1.8 atatat if (rawout)
240 1.8 atatat record(fscript, obuf, cc, 'o');
241 1.8 atatat else
242 1.8 atatat (void)fwrite(obuf, 1, cc, fscript);
243 1.3 jtc outcc += cc;
244 1.18 christos if (flush)
245 1.18 christos (void)fflush(fscript);
246 1.1 cgd }
247 1.1 cgd done();
248 1.1 cgd }
249 1.1 cgd
250 1.3 jtc void
251 1.11 rpaulo scriptflush(int signo)
252 1.1 cgd {
253 1.3 jtc if (outcc) {
254 1.3 jtc (void)fflush(fscript);
255 1.3 jtc outcc = 0;
256 1.1 cgd }
257 1.1 cgd }
258 1.1 cgd
259 1.3 jtc void
260 1.18 christos doshell(const char *command)
261 1.1 cgd {
262 1.17 lukem const char *shell;
263 1.3 jtc
264 1.3 jtc (void)close(master);
265 1.3 jtc (void)fclose(fscript);
266 1.3 jtc login_tty(slave);
267 1.18 christos if (command == NULL) {
268 1.18 christos shell = getenv("SHELL");
269 1.18 christos if (shell == NULL)
270 1.18 christos shell = _PATH_BSHELL;
271 1.18 christos execl(shell, shell, "-i", NULL);
272 1.18 christos command = shell;
273 1.18 christos } else
274 1.18 christos execlp(command, command, NULL);
275 1.18 christos
276 1.18 christos warn("execl %s", command);
277 1.3 jtc fail();
278 1.1 cgd }
279 1.1 cgd
280 1.3 jtc void
281 1.1 cgd fail()
282 1.1 cgd {
283 1.1 cgd
284 1.3 jtc (void)kill(0, SIGTERM);
285 1.1 cgd done();
286 1.1 cgd }
287 1.1 cgd
288 1.3 jtc void
289 1.1 cgd done()
290 1.1 cgd {
291 1.3 jtc time_t tvec;
292 1.1 cgd
293 1.1 cgd if (subchild) {
294 1.3 jtc tvec = time(NULL);
295 1.8 atatat if (rawout)
296 1.8 atatat record(fscript, NULL, 0, 'e');
297 1.18 christos else if (!quiet)
298 1.8 atatat (void)fprintf(fscript,"\nScript done on %s",
299 1.8 atatat ctime(&tvec));
300 1.3 jtc (void)fclose(fscript);
301 1.3 jtc (void)close(master);
302 1.1 cgd } else {
303 1.3 jtc (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
304 1.18 christos if (!quiet)
305 1.18 christos (void)printf("Script done, output file is %s\n", fname);
306 1.1 cgd }
307 1.8 atatat exit(0);
308 1.8 atatat }
309 1.8 atatat
310 1.8 atatat void
311 1.17 lukem record(FILE *fp, char *buf, size_t cc, int direction)
312 1.8 atatat {
313 1.8 atatat struct iovec iov[2];
314 1.8 atatat struct stamp stamp;
315 1.8 atatat struct timeval tv;
316 1.8 atatat
317 1.8 atatat (void)gettimeofday(&tv, NULL);
318 1.8 atatat stamp.scr_len = cc;
319 1.8 atatat stamp.scr_sec = tv.tv_sec;
320 1.8 atatat stamp.scr_usec = tv.tv_usec;
321 1.8 atatat stamp.scr_direction = direction;
322 1.8 atatat iov[0].iov_len = sizeof(stamp);
323 1.8 atatat iov[0].iov_base = &stamp;
324 1.8 atatat iov[1].iov_len = cc;
325 1.8 atatat iov[1].iov_base = buf;
326 1.17 lukem if (writev(fileno(fp), &iov[0], 2) == -1)
327 1.8 atatat err(1, "writev");
328 1.8 atatat }
329 1.8 atatat
330 1.14 christos void
331 1.17 lukem consume(FILE *fp, off_t len, char *buf, int reg)
332 1.14 christos {
333 1.14 christos size_t l;
334 1.14 christos
335 1.14 christos if (reg) {
336 1.17 lukem if (fseeko(fp, len, SEEK_CUR) == -1)
337 1.14 christos err(1, NULL);
338 1.14 christos }
339 1.14 christos else {
340 1.14 christos while (len > 0) {
341 1.14 christos l = MIN(DEF_BUF, len);
342 1.17 lukem if (fread(buf, sizeof(char), l, fp) != l)
343 1.14 christos err(1, "cannot read buffer");
344 1.14 christos len -= l;
345 1.14 christos }
346 1.14 christos }
347 1.14 christos }
348 1.14 christos
349 1.8 atatat #define swapstamp(stamp) do { \
350 1.8 atatat if (stamp.scr_direction > 0xff) { \
351 1.8 atatat stamp.scr_len = bswap64(stamp.scr_len); \
352 1.8 atatat stamp.scr_sec = bswap64(stamp.scr_sec); \
353 1.8 atatat stamp.scr_usec = bswap32(stamp.scr_usec); \
354 1.8 atatat stamp.scr_direction = bswap32(stamp.scr_direction); \
355 1.8 atatat } \
356 1.8 atatat } while (0/*CONSTCOND*/)
357 1.8 atatat
358 1.8 atatat void
359 1.17 lukem playback(FILE *fp)
360 1.8 atatat {
361 1.8 atatat struct timespec tsi, tso;
362 1.8 atatat struct stamp stamp;
363 1.13 christos struct stat pst;
364 1.12 liamjfoy char buf[DEF_BUF];
365 1.12 liamjfoy off_t nread, save_len;
366 1.8 atatat size_t l;
367 1.17 lukem time_t tclock;
368 1.13 christos int reg;
369 1.8 atatat
370 1.17 lukem if (fstat(fileno(fp), &pst) == -1)
371 1.12 liamjfoy err(1, "fstat failed");
372 1.12 liamjfoy
373 1.13 christos reg = S_ISREG(pst.st_mode);
374 1.13 christos
375 1.13 christos for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
376 1.17 lukem if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
377 1.13 christos if (reg)
378 1.13 christos err(1, "reading playback header");
379 1.13 christos else
380 1.13 christos break;
381 1.13 christos }
382 1.12 liamjfoy swapstamp(stamp);
383 1.12 liamjfoy save_len = sizeof(stamp);
384 1.8 atatat
385 1.13 christos if (reg && stamp.scr_len >
386 1.13 christos (uint64_t)(pst.st_size - save_len) - nread)
387 1.12 liamjfoy err(1, "invalid stamp");
388 1.12 liamjfoy
389 1.12 liamjfoy save_len += stamp.scr_len;
390 1.17 lukem tclock = stamp.scr_sec;
391 1.8 atatat tso.tv_sec = stamp.scr_sec;
392 1.8 atatat tso.tv_nsec = stamp.scr_usec * 1000;
393 1.8 atatat
394 1.8 atatat switch (stamp.scr_direction) {
395 1.8 atatat case 's':
396 1.18 christos if (!quiet)
397 1.18 christos (void)printf("Script started on %s",
398 1.18 christos ctime(&tclock));
399 1.8 atatat tsi = tso;
400 1.17 lukem (void)consume(fp, stamp.scr_len, buf, reg);
401 1.8 atatat break;
402 1.8 atatat case 'e':
403 1.18 christos if (!quiet)
404 1.18 christos (void)printf("\nScript done on %s",
405 1.18 christos ctime(&tclock));
406 1.17 lukem (void)consume(fp, stamp.scr_len, buf, reg);
407 1.8 atatat break;
408 1.8 atatat case 'i':
409 1.8 atatat /* throw input away */
410 1.17 lukem (void)consume(fp, stamp.scr_len, buf, reg);
411 1.8 atatat break;
412 1.8 atatat case 'o':
413 1.8 atatat tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
414 1.8 atatat tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
415 1.8 atatat if (tsi.tv_nsec < 0) {
416 1.8 atatat tsi.tv_sec -= 1;
417 1.8 atatat tsi.tv_nsec += 1000000000;
418 1.8 atatat }
419 1.8 atatat if (usesleep)
420 1.8 atatat (void)nanosleep(&tsi, NULL);
421 1.8 atatat tsi = tso;
422 1.12 liamjfoy while (stamp.scr_len > 0) {
423 1.12 liamjfoy l = MIN(DEF_BUF, stamp.scr_len);
424 1.17 lukem if (fread(buf, sizeof(char), l, fp) != l)
425 1.12 liamjfoy err(1, "cannot read buffer");
426 1.12 liamjfoy
427 1.12 liamjfoy (void)write(STDOUT_FILENO, buf, l);
428 1.12 liamjfoy stamp.scr_len -= l;
429 1.12 liamjfoy }
430 1.8 atatat break;
431 1.12 liamjfoy default:
432 1.12 liamjfoy err(1, "invalid direction");
433 1.8 atatat }
434 1.12 liamjfoy }
435 1.17 lukem (void)fclose(fp);
436 1.1 cgd exit(0);
437 1.1 cgd }
438