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