Annotation of src/usr.bin/script/script.c, Revision 1.20
1.20 ! deraadt 1: /* $OpenBSD: script.c,v 1.19 2003/06/10 22:20:50 deraadt 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.20 ! deraadt 68: static char rcsid[] = "$OpenBSD: script.c,v 1.19 2003/06/10 22:20:50 deraadt 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.20 ! deraadt 93: volatile sig_atomic_t child;
! 94: pid_t subchild;
1.1 deraadt 95: char *fname;
96:
1.16 deraadt 97: volatile sig_atomic_t dead;
98: volatile sig_atomic_t sigdeadstatus;
99: volatile sig_atomic_t flush;
100:
1.1 deraadt 101: struct termios tt;
102:
1.17 millert 103: __dead void done(int);
104: void dooutput(void);
105: void doshell(void);
106: void fail(void);
107: void finish(int);
108: void scriptflush(int);
109: void handlesigwinch(int);
1.1 deraadt 110:
111: int
1.19 deraadt 112: main(int argc, char *argv[])
1.1 deraadt 113: {
1.16 deraadt 114: struct sigaction sa;
1.1 deraadt 115: struct termios rtt;
116: struct winsize win;
1.16 deraadt 117: char ibuf[BUFSIZ];
118: ssize_t cc, off;
1.1 deraadt 119: int aflg, ch;
120:
121: aflg = 0;
1.3 millert 122: while ((ch = getopt(argc, argv, "a")) != -1)
1.1 deraadt 123: switch(ch) {
124: case 'a':
125: aflg = 1;
126: break;
127: default:
128: (void)fprintf(stderr, "usage: script [-a] [file]\n");
129: exit(1);
130: }
131: argc -= optind;
132: argv += optind;
133:
134: if (argc > 0)
135: fname = argv[0];
136: else
137: fname = "typescript";
138:
139: if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
1.13 millert 140: err(1, "%s", fname);
1.1 deraadt 141:
142: (void)tcgetattr(STDIN_FILENO, &tt);
143: (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
144: if (openpty(&master, &slave, NULL, &tt, &win) == -1)
1.7 mickey 145: err(1, "openpty");
1.1 deraadt 146:
147: (void)printf("Script started, output file is %s\n", fname);
148: rtt = tt;
149: cfmakeraw(&rtt);
150: rtt.c_lflag &= ~ECHO;
151: (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
152:
1.16 deraadt 153: bzero(&sa, sizeof sa);
154: sigemptyset(&sa.sa_mask);
155: sa.sa_handler = finish;
156: (void)sigaction(SIGCHLD, &sa, NULL);
157:
158: sa.sa_handler = handlesigwinch;
159: sa.sa_flags = SA_RESTART;
160: (void)sigaction(SIGWINCH, &sa, NULL);
161:
1.1 deraadt 162: child = fork();
163: if (child < 0) {
164: perror("fork");
165: fail();
166: }
167: if (child == 0) {
168: subchild = child = fork();
169: if (child < 0) {
170: perror("fork");
171: fail();
172: }
173: if (child)
174: dooutput();
175: else
176: doshell();
177: }
178:
179: (void)fclose(fscript);
1.16 deraadt 180: while (1) {
181: if (dead)
182: break;
183: cc = read(STDIN_FILENO, ibuf, BUFSIZ);
184: if (cc == -1 && errno == EINTR)
185: continue;
186: if (cc <= 0)
187: break;
188: for (off = 0; off < cc; ) {
189: ssize_t n = write(master, ibuf + off, cc - off);
190: if (n == 0)
191: break; /* skip writing */
192: if (n > 0)
193: off += n;
194: }
195: }
196: done(sigdeadstatus);
1.1 deraadt 197: }
198:
1.20 ! deraadt 199: /* ARGSUSED */
1.1 deraadt 200: void
1.19 deraadt 201: finish(int signo)
1.1 deraadt 202: {
1.8 deraadt 203: int save_errno = errno;
1.16 deraadt 204: int status, e = 1;
205: pid_t pid;
1.1 deraadt 206:
1.16 deraadt 207: while ((pid = wait3(&status, WNOHANG, 0)) > 0) {
1.20 ! deraadt 208: if (pid == (pid_t)child) {
1.11 ericj 209: if (WIFEXITED(status))
1.16 deraadt 210: e = WEXITSTATUS(status);
1.11 ericj 211: }
1.16 deraadt 212: }
213: dead = 1;
214: sigdeadstatus = e;
1.12 espie 215: errno = save_errno;
216: }
217:
1.20 ! deraadt 218: /* ARGSUSED */
1.12 espie 219: void
1.19 deraadt 220: handlesigwinch(int signo)
1.12 espie 221: {
1.16 deraadt 222: int save_errno = errno;
1.12 espie 223: struct winsize win;
224: pid_t pgrp;
225:
226: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) != -1) {
1.16 deraadt 227: ioctl(slave, TIOCSWINSZ, &win);
228: if (ioctl(slave, TIOCGPGRP, &pgrp) != -1)
229: killpg(pgrp, SIGWINCH);
1.12 espie 230: }
1.8 deraadt 231: errno = save_errno;
1.1 deraadt 232: }
233:
234: void
1.19 deraadt 235: dooutput(void)
1.1 deraadt 236: {
1.16 deraadt 237: struct sigaction sa;
1.1 deraadt 238: struct itimerval value;
1.16 deraadt 239: char obuf[BUFSIZ];
1.1 deraadt 240: time_t tvec;
1.16 deraadt 241: ssize_t outcc = 0, cc, off;
1.1 deraadt 242:
243: (void)close(STDIN_FILENO);
244: tvec = time(NULL);
245: (void)fprintf(fscript, "Script started on %s", ctime(&tvec));
246:
1.16 deraadt 247: bzero(&sa, sizeof sa);
248: sigemptyset(&sa.sa_mask);
249: sa.sa_handler = scriptflush;
250: (void)sigaction(SIGALRM, &sa, NULL);
251:
1.1 deraadt 252: value.it_interval.tv_sec = SECSPERMIN / 2;
253: value.it_interval.tv_usec = 0;
254: value.it_value = value.it_interval;
255: (void)setitimer(ITIMER_REAL, &value, NULL);
256: for (;;) {
1.16 deraadt 257: if (flush) {
258: if (outcc) {
259: (void)fflush(fscript);
260: outcc = 0;
261: }
262: flush = 0;
263: }
1.1 deraadt 264: cc = read(master, obuf, sizeof (obuf));
1.16 deraadt 265: if (cc == -1 && errno == EINTR)
266: continue;
1.1 deraadt 267: if (cc <= 0)
268: break;
1.16 deraadt 269: for (off = 0; off < cc; ) {
270: ssize_t n = write(1, obuf + off, cc - off);
271: if (n == 0)
272: break; /* skip writing */
273: if (n > 0)
274: off += n;
275: }
1.1 deraadt 276: (void)fwrite(obuf, 1, cc, fscript);
277: outcc += cc;
278: }
1.11 ericj 279: done(0);
1.1 deraadt 280: }
281:
1.20 ! deraadt 282: /* ARGSUSED */
1.1 deraadt 283: void
1.19 deraadt 284: scriptflush(int signo)
1.1 deraadt 285: {
1.16 deraadt 286: flush = 1;
1.1 deraadt 287: }
288:
289: void
1.19 deraadt 290: doshell(void)
1.1 deraadt 291: {
292: char *shell;
293:
294: shell = getenv("SHELL");
295: if (shell == NULL)
296: shell = _PATH_BSHELL;
297:
298: (void)close(master);
299: (void)fclose(fscript);
300: login_tty(slave);
1.15 deraadt 301: execl(shell, shell, "-i", (char *)NULL);
1.1 deraadt 302: perror(shell);
303: fail();
304: }
305:
306: void
1.19 deraadt 307: fail(void)
1.1 deraadt 308: {
309:
310: (void)kill(0, SIGTERM);
1.11 ericj 311: done(1);
1.1 deraadt 312: }
313:
314: void
1.19 deraadt 315: done(int eval)
1.1 deraadt 316: {
317: time_t tvec;
318:
319: if (subchild) {
320: tvec = time(NULL);
321: (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
322: (void)fclose(fscript);
323: (void)close(master);
324: } else {
325: (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
326: (void)printf("Script done, output file is %s\n", fname);
327: }
1.11 ericj 328: exit(eval);
1.1 deraadt 329: }