script.c revision 1.11 1 1.11 rpaulo /* $NetBSD: script.c,v 1.11 2006/03/29 15:40:49 rpaulo 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.11 rpaulo __RCSID("$NetBSD: script.c,v 1.11 2006/03/29 15:40:49 rpaulo 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.8 atatat #include <sys/uio.h>
51 1.3 jtc
52 1.5 lukem #include <err.h>
53 1.3 jtc #include <errno.h>
54 1.3 jtc #include <fcntl.h>
55 1.3 jtc #include <paths.h>
56 1.3 jtc #include <signal.h>
57 1.1 cgd #include <stdio.h>
58 1.3 jtc #include <stdlib.h>
59 1.3 jtc #include <string.h>
60 1.3 jtc #include <termios.h>
61 1.6 kleink #include <time.h>
62 1.3 jtc #include <tzfile.h>
63 1.3 jtc #include <unistd.h>
64 1.5 lukem #include <util.h>
65 1.1 cgd
66 1.8 atatat struct stamp {
67 1.8 atatat uint64_t scr_len; /* amount of data */
68 1.8 atatat uint64_t scr_sec; /* time it arrived in seconds... */
69 1.8 atatat uint32_t scr_usec; /* ...and microseconds */
70 1.8 atatat uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */
71 1.8 atatat };
72 1.8 atatat
73 1.1 cgd FILE *fscript;
74 1.3 jtc int master, slave;
75 1.3 jtc int child, subchild;
76 1.3 jtc int outcc;
77 1.8 atatat int usesleep, rawout;
78 1.1 cgd char *fname;
79 1.1 cgd
80 1.1 cgd struct termios tt;
81 1.1 cgd
82 1.11 rpaulo void done(void);
83 1.11 rpaulo void dooutput(void);
84 1.11 rpaulo void doshell(void);
85 1.11 rpaulo void fail(void);
86 1.11 rpaulo void finish(int);
87 1.11 rpaulo int main(int, char **);
88 1.11 rpaulo void scriptflush(int);
89 1.11 rpaulo void record(FILE *, char *, size_t, int);
90 1.11 rpaulo void playback(FILE *);
91 1.3 jtc
92 1.3 jtc int
93 1.11 rpaulo main(int argc, char *argv[])
94 1.1 cgd {
95 1.5 lukem int cc;
96 1.3 jtc struct termios rtt;
97 1.3 jtc struct winsize win;
98 1.8 atatat int aflg, pflg, ch;
99 1.3 jtc char ibuf[BUFSIZ];
100 1.1 cgd
101 1.3 jtc aflg = 0;
102 1.8 atatat pflg = 0;
103 1.8 atatat usesleep = 1;
104 1.8 atatat rawout = 0;
105 1.8 atatat while ((ch = getopt(argc, argv, "adpr")) != -1)
106 1.3 jtc switch(ch) {
107 1.1 cgd case 'a':
108 1.3 jtc aflg = 1;
109 1.1 cgd break;
110 1.8 atatat case 'd':
111 1.8 atatat usesleep = 0;
112 1.8 atatat break;
113 1.8 atatat case 'p':
114 1.8 atatat pflg = 1;
115 1.8 atatat break;
116 1.8 atatat case 'r':
117 1.8 atatat rawout = 1;
118 1.8 atatat break;
119 1.1 cgd case '?':
120 1.1 cgd default:
121 1.10 wiz (void)fprintf(stderr, "usage: %s [-adpr] [file]\n",
122 1.10 wiz getprogname());
123 1.1 cgd exit(1);
124 1.1 cgd }
125 1.1 cgd argc -= optind;
126 1.1 cgd argv += optind;
127 1.1 cgd
128 1.1 cgd if (argc > 0)
129 1.1 cgd fname = argv[0];
130 1.1 cgd else
131 1.1 cgd fname = "typescript";
132 1.1 cgd
133 1.8 atatat if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
134 1.5 lukem err(1, "fopen %s", fname);
135 1.3 jtc
136 1.8 atatat if (pflg)
137 1.8 atatat playback(fscript);
138 1.8 atatat
139 1.3 jtc (void)tcgetattr(STDIN_FILENO, &tt);
140 1.3 jtc (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
141 1.3 jtc if (openpty(&master, &slave, NULL, &tt, &win) == -1)
142 1.5 lukem err(1, "openpty");
143 1.1 cgd
144 1.3 jtc (void)printf("Script started, output file is %s\n", fname);
145 1.3 jtc rtt = tt;
146 1.3 jtc cfmakeraw(&rtt);
147 1.3 jtc rtt.c_lflag &= ~ECHO;
148 1.3 jtc (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
149 1.1 cgd
150 1.3 jtc (void)signal(SIGCHLD, finish);
151 1.1 cgd child = fork();
152 1.1 cgd if (child < 0) {
153 1.5 lukem warn("fork");
154 1.1 cgd fail();
155 1.1 cgd }
156 1.1 cgd if (child == 0) {
157 1.1 cgd subchild = child = fork();
158 1.1 cgd if (child < 0) {
159 1.5 lukem warn("fork");
160 1.1 cgd fail();
161 1.1 cgd }
162 1.1 cgd if (child)
163 1.1 cgd dooutput();
164 1.1 cgd else
165 1.1 cgd doshell();
166 1.1 cgd }
167 1.1 cgd
168 1.8 atatat if (!rawout)
169 1.8 atatat (void)fclose(fscript);
170 1.8 atatat while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
171 1.8 atatat if (rawout)
172 1.8 atatat record(fscript, ibuf, cc, 'i');
173 1.3 jtc (void)write(master, ibuf, cc);
174 1.8 atatat }
175 1.1 cgd done();
176 1.5 lukem /* NOTREACHED */
177 1.5 lukem return (0);
178 1.1 cgd }
179 1.1 cgd
180 1.1 cgd void
181 1.11 rpaulo finish(int signo)
182 1.1 cgd {
183 1.7 christos int die, pid, status;
184 1.1 cgd
185 1.3 jtc die = 0;
186 1.7 christos while ((pid = wait3(&status, WNOHANG, 0)) > 0)
187 1.1 cgd if (pid == child)
188 1.1 cgd die = 1;
189 1.1 cgd
190 1.1 cgd if (die)
191 1.1 cgd done();
192 1.1 cgd }
193 1.1 cgd
194 1.3 jtc void
195 1.1 cgd dooutput()
196 1.1 cgd {
197 1.3 jtc struct itimerval value;
198 1.5 lukem int cc;
199 1.3 jtc time_t tvec;
200 1.3 jtc char obuf[BUFSIZ];
201 1.1 cgd
202 1.3 jtc (void)close(STDIN_FILENO);
203 1.3 jtc tvec = time(NULL);
204 1.8 atatat if (rawout)
205 1.8 atatat record(fscript, NULL, 0, 's');
206 1.8 atatat else
207 1.8 atatat (void)fprintf(fscript, "Script started on %s", ctime(&tvec));
208 1.3 jtc
209 1.3 jtc (void)signal(SIGALRM, scriptflush);
210 1.3 jtc value.it_interval.tv_sec = SECSPERMIN / 2;
211 1.3 jtc value.it_interval.tv_usec = 0;
212 1.3 jtc value.it_value = value.it_interval;
213 1.3 jtc (void)setitimer(ITIMER_REAL, &value, NULL);
214 1.1 cgd for (;;) {
215 1.1 cgd cc = read(master, obuf, sizeof (obuf));
216 1.1 cgd if (cc <= 0)
217 1.1 cgd break;
218 1.3 jtc (void)write(1, obuf, cc);
219 1.8 atatat if (rawout)
220 1.8 atatat record(fscript, obuf, cc, 'o');
221 1.8 atatat else
222 1.8 atatat (void)fwrite(obuf, 1, cc, fscript);
223 1.3 jtc outcc += cc;
224 1.1 cgd }
225 1.1 cgd done();
226 1.1 cgd }
227 1.1 cgd
228 1.3 jtc void
229 1.11 rpaulo scriptflush(int signo)
230 1.1 cgd {
231 1.3 jtc if (outcc) {
232 1.3 jtc (void)fflush(fscript);
233 1.3 jtc outcc = 0;
234 1.1 cgd }
235 1.1 cgd }
236 1.1 cgd
237 1.3 jtc void
238 1.3 jtc doshell()
239 1.1 cgd {
240 1.3 jtc char *shell;
241 1.3 jtc
242 1.3 jtc shell = getenv("SHELL");
243 1.3 jtc if (shell == NULL)
244 1.3 jtc shell = _PATH_BSHELL;
245 1.1 cgd
246 1.3 jtc (void)close(master);
247 1.3 jtc (void)fclose(fscript);
248 1.3 jtc login_tty(slave);
249 1.4 mikel execl(shell, shell, "-i", NULL);
250 1.5 lukem warn("execl %s", shell);
251 1.3 jtc fail();
252 1.1 cgd }
253 1.1 cgd
254 1.3 jtc void
255 1.1 cgd fail()
256 1.1 cgd {
257 1.1 cgd
258 1.3 jtc (void)kill(0, SIGTERM);
259 1.1 cgd done();
260 1.1 cgd }
261 1.1 cgd
262 1.3 jtc void
263 1.1 cgd done()
264 1.1 cgd {
265 1.3 jtc time_t tvec;
266 1.1 cgd
267 1.1 cgd if (subchild) {
268 1.3 jtc tvec = time(NULL);
269 1.8 atatat if (rawout)
270 1.8 atatat record(fscript, NULL, 0, 'e');
271 1.8 atatat else
272 1.8 atatat (void)fprintf(fscript,"\nScript done on %s",
273 1.8 atatat ctime(&tvec));
274 1.3 jtc (void)fclose(fscript);
275 1.3 jtc (void)close(master);
276 1.1 cgd } else {
277 1.3 jtc (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
278 1.3 jtc (void)printf("Script done, output file is %s\n", fname);
279 1.1 cgd }
280 1.8 atatat exit(0);
281 1.8 atatat }
282 1.8 atatat
283 1.8 atatat void
284 1.11 rpaulo record(FILE *fscript, char *buf, size_t cc, int direction)
285 1.8 atatat {
286 1.8 atatat struct iovec iov[2];
287 1.8 atatat struct stamp stamp;
288 1.8 atatat struct timeval tv;
289 1.8 atatat
290 1.8 atatat (void)gettimeofday(&tv, NULL);
291 1.8 atatat stamp.scr_len = cc;
292 1.8 atatat stamp.scr_sec = tv.tv_sec;
293 1.8 atatat stamp.scr_usec = tv.tv_usec;
294 1.8 atatat stamp.scr_direction = direction;
295 1.8 atatat iov[0].iov_len = sizeof(stamp);
296 1.8 atatat iov[0].iov_base = &stamp;
297 1.8 atatat iov[1].iov_len = cc;
298 1.8 atatat iov[1].iov_base = buf;
299 1.8 atatat if (writev(fileno(fscript), &iov[0], 2) == -1)
300 1.8 atatat err(1, "writev");
301 1.8 atatat }
302 1.8 atatat
303 1.8 atatat #define swapstamp(stamp) do { \
304 1.8 atatat if (stamp.scr_direction > 0xff) { \
305 1.8 atatat stamp.scr_len = bswap64(stamp.scr_len); \
306 1.8 atatat stamp.scr_sec = bswap64(stamp.scr_sec); \
307 1.8 atatat stamp.scr_usec = bswap32(stamp.scr_usec); \
308 1.8 atatat stamp.scr_direction = bswap32(stamp.scr_direction); \
309 1.8 atatat } \
310 1.8 atatat } while (0/*CONSTCOND*/)
311 1.8 atatat
312 1.8 atatat void
313 1.11 rpaulo playback(FILE *fscript)
314 1.8 atatat {
315 1.8 atatat struct timespec tsi, tso;
316 1.8 atatat struct stamp stamp;
317 1.8 atatat char buf[BUFSIZ];
318 1.8 atatat size_t l;
319 1.8 atatat time_t clock;
320 1.8 atatat
321 1.8 atatat do {
322 1.8 atatat if (fread(&stamp, sizeof(stamp), 1, fscript) != 1)
323 1.8 atatat err(1, "reading playback header");
324 1.8 atatat
325 1.8 atatat swapstamp(stamp);
326 1.8 atatat l = fread(buf, 1, stamp.scr_len, fscript);
327 1.8 atatat clock = stamp.scr_sec;
328 1.8 atatat tso.tv_sec = stamp.scr_sec;
329 1.8 atatat tso.tv_nsec = stamp.scr_usec * 1000;
330 1.8 atatat
331 1.8 atatat switch (stamp.scr_direction) {
332 1.8 atatat case 's':
333 1.8 atatat (void)printf("Script started on %s", ctime(&clock));
334 1.8 atatat tsi = tso;
335 1.8 atatat break;
336 1.8 atatat case 'e':
337 1.8 atatat (void)printf("\nScript done on %s", ctime(&clock));
338 1.8 atatat break;
339 1.8 atatat case 'i':
340 1.8 atatat /* throw input away */
341 1.8 atatat break;
342 1.8 atatat case 'o':
343 1.8 atatat tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
344 1.8 atatat tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
345 1.8 atatat if (tsi.tv_nsec < 0) {
346 1.8 atatat tsi.tv_sec -= 1;
347 1.8 atatat tsi.tv_nsec += 1000000000;
348 1.8 atatat }
349 1.8 atatat if (usesleep)
350 1.8 atatat (void)nanosleep(&tsi, NULL);
351 1.8 atatat tsi = tso;
352 1.8 atatat (void)write(STDOUT_FILENO, buf, l);
353 1.8 atatat break;
354 1.8 atatat }
355 1.8 atatat } while (stamp.scr_direction != 'e');
356 1.8 atatat
357 1.8 atatat (void)fclose(fscript);
358 1.1 cgd exit(0);
359 1.1 cgd }
360