Annotation of src/usr.bin/script/script.c, Revision 1.14
1.14 ! deraadt 1: /* $OpenBSD: script.c,v 1.13 2000/06/30 16:00:20 millert Exp $ */
1.1 deraadt 2: /* $NetBSD: script.c,v 1.3 1994/12/21 08:55:43 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1980, 1992, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1980, 1992, 1993\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
46: #endif
1.14 ! deraadt 47: static char rcsid[] = "$OpenBSD: script.c,v 1.13 2000/06/30 16:00:20 millert Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: #include <sys/types.h>
51: #include <sys/wait.h>
52: #include <sys/stat.h>
53: #include <sys/ioctl.h>
54: #include <sys/time.h>
55:
56: #include <errno.h>
57: #include <fcntl.h>
58: #include <paths.h>
59: #include <signal.h>
60: #include <stdio.h>
61: #include <stdlib.h>
62: #include <string.h>
63: #include <termios.h>
64: #include <tzfile.h>
65: #include <unistd.h>
66:
1.5 deraadt 67: #include <util.h>
1.7 mickey 68: #include <err.h>
1.5 deraadt 69:
1.1 deraadt 70: FILE *fscript;
71: int master, slave;
72: int child, subchild;
73: int outcc;
74: char *fname;
75:
76: struct termios tt;
77:
1.11 ericj 78: __dead void done __P((int));
1.1 deraadt 79: void dooutput __P((void));
80: void doshell __P((void));
81: void fail __P((void));
82: void finish __P((int));
83: void scriptflush __P((int));
1.12 espie 84: void handlesigwinch __P((int));
85:
1.1 deraadt 86:
87: int
88: main(argc, argv)
89: int argc;
90: char *argv[];
91: {
92: register int cc;
93: struct termios rtt;
94: struct winsize win;
95: int aflg, ch;
96: char ibuf[BUFSIZ];
97:
98: aflg = 0;
1.3 millert 99: while ((ch = getopt(argc, argv, "a")) != -1)
1.1 deraadt 100: switch(ch) {
101: case 'a':
102: aflg = 1;
103: break;
104: case '?':
105: default:
106: (void)fprintf(stderr, "usage: script [-a] [file]\n");
107: exit(1);
108: }
109: argc -= optind;
110: argv += optind;
111:
112: if (argc > 0)
113: fname = argv[0];
114: else
115: fname = "typescript";
116:
117: if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
1.13 millert 118: err(1, "%s", fname);
1.1 deraadt 119:
120: (void)tcgetattr(STDIN_FILENO, &tt);
121: (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
122: if (openpty(&master, &slave, NULL, &tt, &win) == -1)
1.7 mickey 123: err(1, "openpty");
1.1 deraadt 124:
125: (void)printf("Script started, output file is %s\n", fname);
126: rtt = tt;
127: cfmakeraw(&rtt);
128: rtt.c_lflag &= ~ECHO;
129: (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
130:
1.12 espie 131: (void)signal(SIGWINCH, handlesigwinch);
1.1 deraadt 132: (void)signal(SIGCHLD, finish);
133: child = fork();
134: if (child < 0) {
135: perror("fork");
136: fail();
137: }
138: if (child == 0) {
139: subchild = child = fork();
140: if (child < 0) {
141: perror("fork");
142: fail();
143: }
144: if (child)
145: dooutput();
146: else
147: doshell();
148: }
149:
150: (void)fclose(fscript);
151: while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0)
152: (void)write(master, ibuf, cc);
1.11 ericj 153: done(0);
1.1 deraadt 154: }
155:
156: void
157: finish(signo)
158: int signo;
159: {
160: register int die, pid;
1.8 deraadt 161: int save_errno = errno;
1.11 ericj 162: int status, e;
1.1 deraadt 163:
1.11 ericj 164: die = e = 0;
1.10 deraadt 165: while ((pid = wait3(&status, WNOHANG, 0)) > 0)
1.11 ericj 166: if (pid == child) {
1.1 deraadt 167: die = 1;
1.11 ericj 168: if (WIFEXITED(status))
169: e = WEXITSTATUS(status);
170: else
171: e = 1;
172: }
1.1 deraadt 173:
174: if (die)
1.11 ericj 175: done(e);
1.12 espie 176: errno = save_errno;
177: }
178:
179: void
180: handlesigwinch(signo)
181: int signo;
182: {
183: struct winsize win;
184: pid_t pgrp;
185: int save_errno = errno;
186:
187: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) != -1) {
188: ioctl(slave, TIOCSWINSZ, &win);
189: if (ioctl(slave, TIOCGPGRP, &pgrp) != -1)
190: killpg(pgrp, SIGWINCH);
191: }
1.8 deraadt 192: errno = save_errno;
1.1 deraadt 193: }
194:
195: void
196: dooutput()
197: {
198: struct itimerval value;
199: register int cc;
200: time_t tvec;
201: char obuf[BUFSIZ];
202:
203: (void)close(STDIN_FILENO);
204: tvec = time(NULL);
205: (void)fprintf(fscript, "Script started on %s", ctime(&tvec));
206:
207: (void)signal(SIGALRM, scriptflush);
208: value.it_interval.tv_sec = SECSPERMIN / 2;
209: value.it_interval.tv_usec = 0;
210: value.it_value = value.it_interval;
211: (void)setitimer(ITIMER_REAL, &value, NULL);
212: for (;;) {
213: cc = read(master, obuf, sizeof (obuf));
214: if (cc <= 0)
215: break;
216: (void)write(1, obuf, cc);
217: (void)fwrite(obuf, 1, cc, fscript);
218: outcc += cc;
219: }
1.11 ericj 220: done(0);
1.1 deraadt 221: }
222:
1.14 ! deraadt 223: /* XXX totally illegal race */
1.1 deraadt 224: void
225: scriptflush(signo)
226: int signo;
227: {
1.9 deraadt 228: int save_errno = errno;
229:
1.1 deraadt 230: if (outcc) {
231: (void)fflush(fscript);
232: outcc = 0;
233: }
1.9 deraadt 234: errno = save_errno;
1.1 deraadt 235: }
236:
237: void
238: doshell()
239: {
240: char *shell;
241:
242: shell = getenv("SHELL");
243: if (shell == NULL)
244: shell = _PATH_BSHELL;
245:
246: (void)close(master);
247: (void)fclose(fscript);
248: login_tty(slave);
1.4 deraadt 249: execl(shell, shell, "-i", NULL);
1.1 deraadt 250: perror(shell);
251: fail();
252: }
253:
254: void
255: fail()
256: {
257:
258: (void)kill(0, SIGTERM);
1.11 ericj 259: done(1);
1.1 deraadt 260: }
261:
262: void
1.11 ericj 263: done(eval)
264: int eval;
1.1 deraadt 265: {
266: time_t tvec;
267:
268: if (subchild) {
269: tvec = time(NULL);
270: (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
271: (void)fclose(fscript);
272: (void)close(master);
273: } else {
274: (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
275: (void)printf("Script done, output file is %s\n", fname);
276: }
1.11 ericj 277: exit(eval);
1.1 deraadt 278: }
279: