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