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