Home | History | Annotate | Line # | Download | only in script
script.c revision 1.19
      1  1.19  christos /*	$NetBSD: script.c,v 1.19 2009/10/17 22:36:23 christos Exp $	*/
      2   1.3       jtc 
      3   1.1       cgd /*
      4   1.3       jtc  * Copyright (c) 1980, 1992, 1993
      5   1.3       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.9       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.5     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.16     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\
     35  1.16     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39   1.3       jtc #if 0
     40   1.3       jtc static char sccsid[] = "@(#)script.c	8.1 (Berkeley) 6/6/93";
     41   1.3       jtc #endif
     42  1.19  christos __RCSID("$NetBSD: script.c,v 1.19 2009/10/17 22:36:23 christos Exp $");
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/types.h>
     46   1.3       jtc #include <sys/wait.h>
     47   1.1       cgd #include <sys/stat.h>
     48   1.1       cgd #include <sys/ioctl.h>
     49   1.1       cgd #include <sys/time.h>
     50  1.12  liamjfoy #include <sys/param.h>
     51   1.8    atatat #include <sys/uio.h>
     52   1.3       jtc 
     53   1.5     lukem #include <err.h>
     54   1.3       jtc #include <errno.h>
     55   1.3       jtc #include <fcntl.h>
     56   1.3       jtc #include <paths.h>
     57   1.3       jtc #include <signal.h>
     58   1.1       cgd #include <stdio.h>
     59   1.3       jtc #include <stdlib.h>
     60   1.3       jtc #include <string.h>
     61   1.3       jtc #include <termios.h>
     62   1.6    kleink #include <time.h>
     63   1.3       jtc #include <tzfile.h>
     64   1.3       jtc #include <unistd.h>
     65   1.5     lukem #include <util.h>
     66   1.1       cgd 
     67  1.12  liamjfoy #define	DEF_BUF	65536
     68  1.12  liamjfoy 
     69   1.8    atatat struct stamp {
     70   1.8    atatat 	uint64_t scr_len;	/* amount of data */
     71   1.8    atatat 	uint64_t scr_sec;	/* time it arrived in seconds... */
     72   1.8    atatat 	uint32_t scr_usec;	/* ...and microseconds */
     73   1.8    atatat 	uint32_t scr_direction;	/* 'i', 'o', etc (also indicates endianness) */
     74   1.8    atatat };
     75   1.8    atatat 
     76   1.1       cgd FILE	*fscript;
     77   1.3       jtc int	master, slave;
     78   1.3       jtc int	child, subchild;
     79   1.3       jtc int	outcc;
     80   1.8    atatat int	usesleep, rawout;
     81  1.18  christos int	quiet, flush;
     82  1.17     lukem const char *fname;
     83   1.1       cgd 
     84   1.1       cgd struct	termios tt;
     85   1.1       cgd 
     86  1.11    rpaulo void	done(void);
     87  1.11    rpaulo void	dooutput(void);
     88  1.18  christos void	doshell(const char *);
     89  1.11    rpaulo void	fail(void);
     90  1.11    rpaulo void	finish(int);
     91  1.11    rpaulo int	main(int, char **);
     92  1.11    rpaulo void	scriptflush(int);
     93  1.11    rpaulo void	record(FILE *, char *, size_t, int);
     94  1.14  christos void	consume(FILE *, off_t, char *, int);
     95  1.11    rpaulo void	playback(FILE *);
     96   1.3       jtc 
     97   1.3       jtc int
     98  1.11    rpaulo main(int argc, char *argv[])
     99   1.1       cgd {
    100   1.5     lukem 	int cc;
    101   1.3       jtc 	struct termios rtt;
    102   1.3       jtc 	struct winsize win;
    103   1.8    atatat 	int aflg, pflg, ch;
    104   1.3       jtc 	char ibuf[BUFSIZ];
    105  1.18  christos 	const char *command;
    106   1.1       cgd 
    107   1.3       jtc 	aflg = 0;
    108   1.8    atatat 	pflg = 0;
    109   1.8    atatat 	usesleep = 1;
    110   1.8    atatat 	rawout = 0;
    111  1.18  christos 	quiet = 0;
    112  1.18  christos 	flush = 0;
    113  1.18  christos 	command = NULL;
    114  1.18  christos 	while ((ch = getopt(argc, argv, "ac:dfpqr")) != -1)
    115   1.3       jtc 		switch(ch) {
    116   1.1       cgd 		case 'a':
    117   1.3       jtc 			aflg = 1;
    118   1.1       cgd 			break;
    119  1.18  christos 		case 'c':
    120  1.18  christos 			command = optarg;
    121  1.18  christos 			break;
    122   1.8    atatat 		case 'd':
    123   1.8    atatat 			usesleep = 0;
    124   1.8    atatat 			break;
    125  1.18  christos 		case 'f':
    126  1.18  christos 			flush = 1;
    127  1.18  christos 			break;
    128   1.8    atatat 		case 'p':
    129   1.8    atatat 			pflg = 1;
    130   1.8    atatat 			break;
    131  1.18  christos 		case 'q':
    132  1.18  christos 			quiet = 1;
    133  1.18  christos 			break;
    134   1.8    atatat 		case 'r':
    135   1.8    atatat 			rawout = 1;
    136   1.8    atatat 			break;
    137   1.1       cgd 		case '?':
    138   1.1       cgd 		default:
    139  1.18  christos 			(void)fprintf(stderr,
    140  1.18  christos 			    "Usage: %s [-c <command>][-adfpqr] [file]\n",
    141  1.10       wiz 			    getprogname());
    142   1.1       cgd 			exit(1);
    143   1.1       cgd 		}
    144   1.1       cgd 	argc -= optind;
    145   1.1       cgd 	argv += optind;
    146   1.1       cgd 
    147   1.1       cgd 	if (argc > 0)
    148   1.1       cgd 		fname = argv[0];
    149   1.1       cgd 	else
    150   1.1       cgd 		fname = "typescript";
    151   1.1       cgd 
    152   1.8    atatat 	if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
    153   1.5     lukem 		err(1, "fopen %s", fname);
    154   1.3       jtc 
    155   1.8    atatat 	if (pflg)
    156   1.8    atatat 		playback(fscript);
    157   1.8    atatat 
    158   1.3       jtc 	(void)tcgetattr(STDIN_FILENO, &tt);
    159   1.3       jtc 	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
    160   1.3       jtc 	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
    161   1.5     lukem 		err(1, "openpty");
    162   1.1       cgd 
    163  1.18  christos 	if (!quiet)
    164  1.18  christos 		(void)printf("Script started, output file is %s\n", fname);
    165   1.3       jtc 	rtt = tt;
    166   1.3       jtc 	cfmakeraw(&rtt);
    167   1.3       jtc 	rtt.c_lflag &= ~ECHO;
    168   1.3       jtc 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
    169   1.1       cgd 
    170   1.3       jtc 	(void)signal(SIGCHLD, finish);
    171   1.1       cgd 	child = fork();
    172   1.1       cgd 	if (child < 0) {
    173   1.5     lukem 		warn("fork");
    174   1.1       cgd 		fail();
    175   1.1       cgd 	}
    176   1.1       cgd 	if (child == 0) {
    177   1.1       cgd 		subchild = child = fork();
    178   1.1       cgd 		if (child < 0) {
    179   1.5     lukem 			warn("fork");
    180   1.1       cgd 			fail();
    181   1.1       cgd 		}
    182   1.1       cgd 		if (child)
    183   1.1       cgd 			dooutput();
    184   1.1       cgd 		else
    185  1.18  christos 			doshell(command);
    186   1.1       cgd 	}
    187   1.1       cgd 
    188   1.8    atatat 	if (!rawout)
    189   1.8    atatat 		(void)fclose(fscript);
    190   1.8    atatat 	while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
    191   1.8    atatat 		if (rawout)
    192   1.8    atatat 			record(fscript, ibuf, cc, 'i');
    193   1.3       jtc 		(void)write(master, ibuf, cc);
    194   1.8    atatat 	}
    195   1.1       cgd 	done();
    196   1.5     lukem 	/* NOTREACHED */
    197   1.5     lukem 	return (0);
    198   1.1       cgd }
    199   1.1       cgd 
    200   1.1       cgd void
    201  1.11    rpaulo finish(int signo)
    202   1.1       cgd {
    203   1.7  christos 	int die, pid, status;
    204   1.1       cgd 
    205   1.3       jtc 	die = 0;
    206   1.7  christos 	while ((pid = wait3(&status, WNOHANG, 0)) > 0)
    207   1.1       cgd 		if (pid == child)
    208   1.1       cgd 			die = 1;
    209   1.1       cgd 
    210   1.1       cgd 	if (die)
    211   1.1       cgd 		done();
    212   1.1       cgd }
    213   1.1       cgd 
    214   1.3       jtc void
    215  1.18  christos dooutput(void)
    216   1.1       cgd {
    217   1.3       jtc 	struct itimerval value;
    218   1.5     lukem 	int cc;
    219   1.3       jtc 	time_t tvec;
    220   1.3       jtc 	char obuf[BUFSIZ];
    221   1.1       cgd 
    222   1.3       jtc 	(void)close(STDIN_FILENO);
    223   1.3       jtc 	tvec = time(NULL);
    224   1.8    atatat 	if (rawout)
    225   1.8    atatat 		record(fscript, NULL, 0, 's');
    226  1.18  christos 	else if (!quiet)
    227   1.8    atatat 		(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
    228   1.3       jtc 
    229   1.3       jtc 	(void)signal(SIGALRM, scriptflush);
    230   1.3       jtc 	value.it_interval.tv_sec = SECSPERMIN / 2;
    231   1.3       jtc 	value.it_interval.tv_usec = 0;
    232   1.3       jtc 	value.it_value = value.it_interval;
    233   1.3       jtc 	(void)setitimer(ITIMER_REAL, &value, NULL);
    234   1.1       cgd 	for (;;) {
    235   1.1       cgd 		cc = read(master, obuf, sizeof (obuf));
    236   1.1       cgd 		if (cc <= 0)
    237   1.1       cgd 			break;
    238   1.3       jtc 		(void)write(1, obuf, cc);
    239   1.8    atatat 		if (rawout)
    240   1.8    atatat 			record(fscript, obuf, cc, 'o');
    241   1.8    atatat 		else
    242   1.8    atatat 			(void)fwrite(obuf, 1, cc, fscript);
    243   1.3       jtc 		outcc += cc;
    244  1.18  christos 		if (flush)
    245  1.18  christos 			(void)fflush(fscript);
    246   1.1       cgd 	}
    247   1.1       cgd 	done();
    248   1.1       cgd }
    249   1.1       cgd 
    250   1.3       jtc void
    251  1.11    rpaulo scriptflush(int signo)
    252   1.1       cgd {
    253   1.3       jtc 	if (outcc) {
    254   1.3       jtc 		(void)fflush(fscript);
    255   1.3       jtc 		outcc = 0;
    256   1.1       cgd 	}
    257   1.1       cgd }
    258   1.1       cgd 
    259   1.3       jtc void
    260  1.18  christos doshell(const char *command)
    261   1.1       cgd {
    262  1.17     lukem 	const char *shell;
    263   1.3       jtc 
    264   1.3       jtc 	(void)close(master);
    265   1.3       jtc 	(void)fclose(fscript);
    266   1.3       jtc 	login_tty(slave);
    267  1.18  christos 	if (command == NULL) {
    268  1.18  christos 		shell = getenv("SHELL");
    269  1.18  christos 		if (shell == NULL)
    270  1.18  christos 			shell = _PATH_BSHELL;
    271  1.18  christos 		execl(shell, shell, "-i", NULL);
    272  1.19  christos 		warn("execl `%s'", shell);
    273  1.19  christos 	} else {
    274  1.19  christos 		if (system(command) == -1)
    275  1.19  christos 			warn("system `%s'", command);
    276  1.19  christos 	}
    277  1.18  christos 
    278   1.3       jtc 	fail();
    279   1.1       cgd }
    280   1.1       cgd 
    281   1.3       jtc void
    282   1.1       cgd fail()
    283   1.1       cgd {
    284   1.1       cgd 
    285   1.3       jtc 	(void)kill(0, SIGTERM);
    286   1.1       cgd 	done();
    287   1.1       cgd }
    288   1.1       cgd 
    289   1.3       jtc void
    290   1.1       cgd done()
    291   1.1       cgd {
    292   1.3       jtc 	time_t tvec;
    293   1.1       cgd 
    294   1.1       cgd 	if (subchild) {
    295   1.3       jtc 		tvec = time(NULL);
    296   1.8    atatat 		if (rawout)
    297   1.8    atatat 			record(fscript, NULL, 0, 'e');
    298  1.18  christos 		else if (!quiet)
    299   1.8    atatat 			(void)fprintf(fscript,"\nScript done on %s",
    300   1.8    atatat 			    ctime(&tvec));
    301   1.3       jtc 		(void)fclose(fscript);
    302   1.3       jtc 		(void)close(master);
    303   1.1       cgd 	} else {
    304   1.3       jtc 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
    305  1.18  christos 		if (!quiet)
    306  1.18  christos 			(void)printf("Script done, output file is %s\n", fname);
    307   1.1       cgd 	}
    308   1.8    atatat 	exit(0);
    309   1.8    atatat }
    310   1.8    atatat 
    311   1.8    atatat void
    312  1.17     lukem record(FILE *fp, char *buf, size_t cc, int direction)
    313   1.8    atatat {
    314   1.8    atatat 	struct iovec iov[2];
    315   1.8    atatat 	struct stamp stamp;
    316   1.8    atatat 	struct timeval tv;
    317   1.8    atatat 
    318   1.8    atatat 	(void)gettimeofday(&tv, NULL);
    319   1.8    atatat 	stamp.scr_len = cc;
    320   1.8    atatat 	stamp.scr_sec = tv.tv_sec;
    321   1.8    atatat 	stamp.scr_usec = tv.tv_usec;
    322   1.8    atatat 	stamp.scr_direction = direction;
    323   1.8    atatat 	iov[0].iov_len = sizeof(stamp);
    324   1.8    atatat 	iov[0].iov_base = &stamp;
    325   1.8    atatat 	iov[1].iov_len = cc;
    326   1.8    atatat 	iov[1].iov_base = buf;
    327  1.17     lukem 	if (writev(fileno(fp), &iov[0], 2) == -1)
    328   1.8    atatat 		err(1, "writev");
    329   1.8    atatat }
    330   1.8    atatat 
    331  1.14  christos void
    332  1.17     lukem consume(FILE *fp, off_t len, char *buf, int reg)
    333  1.14  christos {
    334  1.14  christos 	size_t l;
    335  1.14  christos 
    336  1.14  christos 	if (reg) {
    337  1.17     lukem 		if (fseeko(fp, len, SEEK_CUR) == -1)
    338  1.14  christos 			err(1, NULL);
    339  1.14  christos 	}
    340  1.14  christos 	else {
    341  1.14  christos 		while (len > 0) {
    342  1.14  christos 			l = MIN(DEF_BUF, len);
    343  1.17     lukem 			if (fread(buf, sizeof(char), l, fp) != l)
    344  1.14  christos 				err(1, "cannot read buffer");
    345  1.14  christos 			len -= l;
    346  1.14  christos 		}
    347  1.14  christos 	}
    348  1.14  christos }
    349  1.14  christos 
    350   1.8    atatat #define swapstamp(stamp) do { \
    351   1.8    atatat 	if (stamp.scr_direction > 0xff) { \
    352   1.8    atatat 		stamp.scr_len = bswap64(stamp.scr_len); \
    353   1.8    atatat 		stamp.scr_sec = bswap64(stamp.scr_sec); \
    354   1.8    atatat 		stamp.scr_usec = bswap32(stamp.scr_usec); \
    355   1.8    atatat 		stamp.scr_direction = bswap32(stamp.scr_direction); \
    356   1.8    atatat 	} \
    357   1.8    atatat } while (0/*CONSTCOND*/)
    358   1.8    atatat 
    359   1.8    atatat void
    360  1.17     lukem playback(FILE *fp)
    361   1.8    atatat {
    362   1.8    atatat 	struct timespec tsi, tso;
    363   1.8    atatat 	struct stamp stamp;
    364  1.13  christos 	struct stat pst;
    365  1.12  liamjfoy 	char buf[DEF_BUF];
    366  1.12  liamjfoy 	off_t nread, save_len;
    367   1.8    atatat 	size_t l;
    368  1.17     lukem 	time_t tclock;
    369  1.13  christos 	int reg;
    370   1.8    atatat 
    371  1.17     lukem 	if (fstat(fileno(fp), &pst) == -1)
    372  1.12  liamjfoy 		err(1, "fstat failed");
    373  1.12  liamjfoy 
    374  1.13  christos 	reg = S_ISREG(pst.st_mode);
    375  1.13  christos 
    376  1.13  christos 	for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
    377  1.17     lukem 		if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
    378  1.13  christos 			if (reg)
    379  1.13  christos 				err(1, "reading playback header");
    380  1.13  christos 			else
    381  1.13  christos 				break;
    382  1.13  christos 		}
    383  1.12  liamjfoy 		swapstamp(stamp);
    384  1.12  liamjfoy 		save_len = sizeof(stamp);
    385   1.8    atatat 
    386  1.13  christos 		if (reg && stamp.scr_len >
    387  1.13  christos 		    (uint64_t)(pst.st_size - save_len) - nread)
    388  1.12  liamjfoy 			err(1, "invalid stamp");
    389  1.12  liamjfoy 
    390  1.12  liamjfoy 		save_len += stamp.scr_len;
    391  1.17     lukem 		tclock = stamp.scr_sec;
    392   1.8    atatat 		tso.tv_sec = stamp.scr_sec;
    393   1.8    atatat 		tso.tv_nsec = stamp.scr_usec * 1000;
    394   1.8    atatat 
    395   1.8    atatat 		switch (stamp.scr_direction) {
    396   1.8    atatat 		case 's':
    397  1.18  christos 			if (!quiet)
    398  1.18  christos 			    (void)printf("Script started on %s",
    399  1.18  christos 				ctime(&tclock));
    400   1.8    atatat 			tsi = tso;
    401  1.17     lukem 			(void)consume(fp, stamp.scr_len, buf, reg);
    402   1.8    atatat 			break;
    403   1.8    atatat 		case 'e':
    404  1.18  christos 			if (!quiet)
    405  1.18  christos 				(void)printf("\nScript done on %s",
    406  1.18  christos 				    ctime(&tclock));
    407  1.17     lukem 			(void)consume(fp, stamp.scr_len, buf, reg);
    408   1.8    atatat 			break;
    409   1.8    atatat 		case 'i':
    410   1.8    atatat 			/* throw input away */
    411  1.17     lukem 			(void)consume(fp, stamp.scr_len, buf, reg);
    412   1.8    atatat 			break;
    413   1.8    atatat 		case 'o':
    414   1.8    atatat 			tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
    415   1.8    atatat 			tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
    416   1.8    atatat 			if (tsi.tv_nsec < 0) {
    417   1.8    atatat 				tsi.tv_sec -= 1;
    418   1.8    atatat 				tsi.tv_nsec += 1000000000;
    419   1.8    atatat 			}
    420   1.8    atatat 			if (usesleep)
    421   1.8    atatat 				(void)nanosleep(&tsi, NULL);
    422   1.8    atatat 			tsi = tso;
    423  1.12  liamjfoy 			while (stamp.scr_len > 0) {
    424  1.12  liamjfoy 				l = MIN(DEF_BUF, stamp.scr_len);
    425  1.17     lukem 				if (fread(buf, sizeof(char), l, fp) != l)
    426  1.12  liamjfoy 					err(1, "cannot read buffer");
    427  1.12  liamjfoy 
    428  1.12  liamjfoy 				(void)write(STDOUT_FILENO, buf, l);
    429  1.12  liamjfoy 				stamp.scr_len -= l;
    430  1.12  liamjfoy 			}
    431   1.8    atatat 			break;
    432  1.12  liamjfoy 		default:
    433  1.12  liamjfoy 			err(1, "invalid direction");
    434   1.8    atatat 		}
    435  1.12  liamjfoy 	}
    436  1.17     lukem 	(void)fclose(fp);
    437   1.1       cgd 	exit(0);
    438   1.1       cgd }
    439