xref: /openbsd-src/usr.bin/script/script.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /*	$OpenBSD: script.c,v 1.25 2009/10/27 23:59:43 deraadt Exp $	*/
2 /*	$NetBSD: script.c,v 1.3 1994/12/21 08:55:43 jtc Exp $	*/
3 
4 /*
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 /*
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.
41  * 3. Neither the name of the University nor the names of its contributors
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 #include <sys/types.h>
59 #include <sys/wait.h>
60 #include <sys/stat.h>
61 #include <sys/ioctl.h>
62 #include <sys/time.h>
63 
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <paths.h>
67 #include <signal.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <termios.h>
72 #include <tzfile.h>
73 #include <unistd.h>
74 
75 #include <util.h>
76 #include <err.h>
77 
78 FILE	*fscript;
79 int	master, slave;
80 volatile sig_atomic_t child;
81 pid_t	subchild;
82 char	*fname;
83 
84 volatile sig_atomic_t dead;
85 volatile sig_atomic_t sigdeadstatus;
86 volatile sig_atomic_t flush;
87 
88 struct	termios tt;
89 
90 __dead void done(int);
91 void dooutput(void);
92 void doshell(void);
93 void fail(void);
94 void finish(int);
95 void scriptflush(int);
96 void handlesigwinch(int);
97 
98 int
99 main(int argc, char *argv[])
100 {
101 	extern char *__progname;
102 	struct sigaction sa;
103 	struct termios rtt;
104 	struct winsize win;
105 	char ibuf[BUFSIZ];
106 	ssize_t cc, off;
107 	int aflg, ch;
108 
109 	aflg = 0;
110 	while ((ch = getopt(argc, argv, "a")) != -1)
111 		switch(ch) {
112 		case 'a':
113 			aflg = 1;
114 			break;
115 		default:
116 			fprintf(stderr, "usage: %s [-a] [file]\n", __progname);
117 			exit(1);
118 		}
119 	argc -= optind;
120 	argv += optind;
121 
122 	if (argc > 0)
123 		fname = argv[0];
124 	else
125 		fname = "typescript";
126 
127 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
128 		err(1, "%s", fname);
129 
130 	(void)tcgetattr(STDIN_FILENO, &tt);
131 	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
132 	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
133 		err(1, "openpty");
134 
135 	(void)printf("Script started, output file is %s\n", fname);
136 	rtt = tt;
137 	cfmakeraw(&rtt);
138 	rtt.c_lflag &= ~ECHO;
139 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
140 
141 	bzero(&sa, sizeof sa);
142 	sigemptyset(&sa.sa_mask);
143 	sa.sa_handler = finish;
144 	(void)sigaction(SIGCHLD, &sa, NULL);
145 
146 	sa.sa_handler = handlesigwinch;
147 	sa.sa_flags = SA_RESTART;
148 	(void)sigaction(SIGWINCH, &sa, NULL);
149 
150 	child = fork();
151 	if (child < 0) {
152 		warn("fork");
153 		fail();
154 	}
155 	if (child == 0) {
156 		subchild = child = fork();
157 		if (child < 0) {
158 			warn("fork");
159 			fail();
160 		}
161 		if (child)
162 			dooutput();
163 		else
164 			doshell();
165 	}
166 
167 	(void)fclose(fscript);
168 	while (1) {
169 		if (dead)
170 			break;
171 		cc = read(STDIN_FILENO, ibuf, BUFSIZ);
172 		if (cc == -1 && errno == EINTR)
173 			continue;
174 		if (cc <= 0)
175 			break;
176 		for (off = 0; off < cc; ) {
177 			ssize_t n = write(master, ibuf + off, cc - off);
178 			if (n == -1 && errno != EAGAIN)
179 				break;
180 			if (n == 0)
181 				break;	/* skip writing */
182 			if (n > 0)
183 				off += n;
184 		}
185 	}
186 	done(sigdeadstatus);
187 }
188 
189 /* ARGSUSED */
190 void
191 finish(int signo)
192 {
193 	int save_errno = errno;
194 	int status, e = 1;
195 	pid_t pid;
196 
197 	while ((pid = wait3(&status, WNOHANG, 0)) > 0) {
198 		if (pid == (pid_t)child) {
199 			if (WIFEXITED(status))
200 				e = WEXITSTATUS(status);
201 		}
202 	}
203 	dead = 1;
204 	sigdeadstatus = e;
205 	errno = save_errno;
206 }
207 
208 /* ARGSUSED */
209 void
210 handlesigwinch(int signo)
211 {
212 	int save_errno = errno;
213 	struct winsize win;
214 	pid_t pgrp;
215 
216 	if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) != -1) {
217 		ioctl(slave, TIOCSWINSZ, &win);
218 		if (ioctl(slave, TIOCGPGRP, &pgrp) != -1)
219 			killpg(pgrp, SIGWINCH);
220 	}
221 	errno = save_errno;
222 }
223 
224 void
225 dooutput(void)
226 {
227 	struct sigaction sa;
228 	struct itimerval value;
229 	sigset_t blkalrm;
230 	char obuf[BUFSIZ];
231 	time_t tvec;
232 	ssize_t outcc = 0, cc, off;
233 
234 	(void)close(STDIN_FILENO);
235 	tvec = time(NULL);
236 	(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
237 
238 	sigemptyset(&blkalrm);
239 	sigaddset(&blkalrm, SIGALRM);
240 	bzero(&sa, sizeof sa);
241 	sigemptyset(&sa.sa_mask);
242 	sa.sa_handler = scriptflush;
243 	(void)sigaction(SIGALRM, &sa, NULL);
244 
245 	value.it_interval.tv_sec = SECSPERMIN / 2;
246 	value.it_interval.tv_usec = 0;
247 	value.it_value = value.it_interval;
248 	(void)setitimer(ITIMER_REAL, &value, NULL);
249 	for (;;) {
250 		if (flush) {
251 			if (outcc) {
252 				(void)fflush(fscript);
253 				outcc = 0;
254 			}
255 			flush = 0;
256 		}
257 		cc = read(master, obuf, sizeof (obuf));
258 		if (cc == -1 && errno == EINTR)
259 			continue;
260 		if (cc <= 0)
261 			break;
262 		sigprocmask(SIG_BLOCK, &blkalrm, NULL);
263 		for (off = 0; off < cc; ) {
264 			ssize_t n = write(STDOUT_FILENO, obuf + off, cc - off);
265 			if (n == -1 && errno != EAGAIN)
266 				break;
267 			if (n == 0)
268 				break;	/* skip writing */
269 			if (n > 0)
270 				off += n;
271 		}
272 		(void)fwrite(obuf, 1, cc, fscript);
273 		outcc += cc;
274 		sigprocmask(SIG_UNBLOCK, &blkalrm, NULL);
275 	}
276 	done(0);
277 }
278 
279 /* ARGSUSED */
280 void
281 scriptflush(int signo)
282 {
283 	flush = 1;
284 }
285 
286 void
287 doshell(void)
288 {
289 	char *shell;
290 
291 	shell = getenv("SHELL");
292 	if (shell == NULL)
293 		shell = _PATH_BSHELL;
294 
295 	(void)close(master);
296 	(void)fclose(fscript);
297 	login_tty(slave);
298 	execl(shell, shell, "-i", (char *)NULL);
299 	warn("%s", shell);
300 	fail();
301 }
302 
303 void
304 fail(void)
305 {
306 
307 	(void)kill(0, SIGTERM);
308 	done(1);
309 }
310 
311 void
312 done(int eval)
313 {
314 	time_t tvec;
315 
316 	if (subchild) {
317 		tvec = time(NULL);
318 		(void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
319 		(void)fclose(fscript);
320 		(void)close(master);
321 	} else {
322 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
323 		(void)printf("Script done, output file is %s\n", fname);
324 	}
325 	exit(eval);
326 }
327