Home | History | Annotate | Line # | Download | only in script
script.c revision 1.17
      1  1.17     lukem /*	$NetBSD: script.c,v 1.17 2009/04/13 07:15:32 lukem 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.17     lukem __RCSID("$NetBSD: script.c,v 1.17 2009/04/13 07:15:32 lukem 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.17     lukem const char *fname;
     82   1.1       cgd 
     83   1.1       cgd struct	termios tt;
     84   1.1       cgd 
     85  1.11    rpaulo void	done(void);
     86  1.11    rpaulo void	dooutput(void);
     87  1.11    rpaulo void	doshell(void);
     88  1.11    rpaulo void	fail(void);
     89  1.11    rpaulo void	finish(int);
     90  1.11    rpaulo int	main(int, char **);
     91  1.11    rpaulo void	scriptflush(int);
     92  1.11    rpaulo void	record(FILE *, char *, size_t, int);
     93  1.14  christos void	consume(FILE *, off_t, char *, int);
     94  1.11    rpaulo void	playback(FILE *);
     95   1.3       jtc 
     96   1.3       jtc int
     97  1.11    rpaulo main(int argc, char *argv[])
     98   1.1       cgd {
     99   1.5     lukem 	int cc;
    100   1.3       jtc 	struct termios rtt;
    101   1.3       jtc 	struct winsize win;
    102   1.8    atatat 	int aflg, pflg, ch;
    103   1.3       jtc 	char ibuf[BUFSIZ];
    104   1.1       cgd 
    105   1.3       jtc 	aflg = 0;
    106   1.8    atatat 	pflg = 0;
    107   1.8    atatat 	usesleep = 1;
    108   1.8    atatat 	rawout = 0;
    109   1.8    atatat 	while ((ch = getopt(argc, argv, "adpr")) != -1)
    110   1.3       jtc 		switch(ch) {
    111   1.1       cgd 		case 'a':
    112   1.3       jtc 			aflg = 1;
    113   1.1       cgd 			break;
    114   1.8    atatat 		case 'd':
    115   1.8    atatat 			usesleep = 0;
    116   1.8    atatat 			break;
    117   1.8    atatat 		case 'p':
    118   1.8    atatat 			pflg = 1;
    119   1.8    atatat 			break;
    120   1.8    atatat 		case 'r':
    121   1.8    atatat 			rawout = 1;
    122   1.8    atatat 			break;
    123   1.1       cgd 		case '?':
    124   1.1       cgd 		default:
    125  1.10       wiz 			(void)fprintf(stderr, "usage: %s [-adpr] [file]\n",
    126  1.10       wiz 			    getprogname());
    127   1.1       cgd 			exit(1);
    128   1.1       cgd 		}
    129   1.1       cgd 	argc -= optind;
    130   1.1       cgd 	argv += optind;
    131   1.1       cgd 
    132   1.1       cgd 	if (argc > 0)
    133   1.1       cgd 		fname = argv[0];
    134   1.1       cgd 	else
    135   1.1       cgd 		fname = "typescript";
    136   1.1       cgd 
    137   1.8    atatat 	if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
    138   1.5     lukem 		err(1, "fopen %s", fname);
    139   1.3       jtc 
    140   1.8    atatat 	if (pflg)
    141   1.8    atatat 		playback(fscript);
    142   1.8    atatat 
    143   1.3       jtc 	(void)tcgetattr(STDIN_FILENO, &tt);
    144   1.3       jtc 	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
    145   1.3       jtc 	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
    146   1.5     lukem 		err(1, "openpty");
    147   1.1       cgd 
    148   1.3       jtc 	(void)printf("Script started, output file is %s\n", fname);
    149   1.3       jtc 	rtt = tt;
    150   1.3       jtc 	cfmakeraw(&rtt);
    151   1.3       jtc 	rtt.c_lflag &= ~ECHO;
    152   1.3       jtc 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
    153   1.1       cgd 
    154   1.3       jtc 	(void)signal(SIGCHLD, finish);
    155   1.1       cgd 	child = fork();
    156   1.1       cgd 	if (child < 0) {
    157   1.5     lukem 		warn("fork");
    158   1.1       cgd 		fail();
    159   1.1       cgd 	}
    160   1.1       cgd 	if (child == 0) {
    161   1.1       cgd 		subchild = child = fork();
    162   1.1       cgd 		if (child < 0) {
    163   1.5     lukem 			warn("fork");
    164   1.1       cgd 			fail();
    165   1.1       cgd 		}
    166   1.1       cgd 		if (child)
    167   1.1       cgd 			dooutput();
    168   1.1       cgd 		else
    169   1.1       cgd 			doshell();
    170   1.1       cgd 	}
    171   1.1       cgd 
    172   1.8    atatat 	if (!rawout)
    173   1.8    atatat 		(void)fclose(fscript);
    174   1.8    atatat 	while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
    175   1.8    atatat 		if (rawout)
    176   1.8    atatat 			record(fscript, ibuf, cc, 'i');
    177   1.3       jtc 		(void)write(master, ibuf, cc);
    178   1.8    atatat 	}
    179   1.1       cgd 	done();
    180   1.5     lukem 	/* NOTREACHED */
    181   1.5     lukem 	return (0);
    182   1.1       cgd }
    183   1.1       cgd 
    184   1.1       cgd void
    185  1.11    rpaulo finish(int signo)
    186   1.1       cgd {
    187   1.7  christos 	int die, pid, status;
    188   1.1       cgd 
    189   1.3       jtc 	die = 0;
    190   1.7  christos 	while ((pid = wait3(&status, WNOHANG, 0)) > 0)
    191   1.1       cgd 		if (pid == child)
    192   1.1       cgd 			die = 1;
    193   1.1       cgd 
    194   1.1       cgd 	if (die)
    195   1.1       cgd 		done();
    196   1.1       cgd }
    197   1.1       cgd 
    198   1.3       jtc void
    199   1.1       cgd dooutput()
    200   1.1       cgd {
    201   1.3       jtc 	struct itimerval value;
    202   1.5     lukem 	int cc;
    203   1.3       jtc 	time_t tvec;
    204   1.3       jtc 	char obuf[BUFSIZ];
    205   1.1       cgd 
    206   1.3       jtc 	(void)close(STDIN_FILENO);
    207   1.3       jtc 	tvec = time(NULL);
    208   1.8    atatat 	if (rawout)
    209   1.8    atatat 		record(fscript, NULL, 0, 's');
    210   1.8    atatat 	else
    211   1.8    atatat 		(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
    212   1.3       jtc 
    213   1.3       jtc 	(void)signal(SIGALRM, scriptflush);
    214   1.3       jtc 	value.it_interval.tv_sec = SECSPERMIN / 2;
    215   1.3       jtc 	value.it_interval.tv_usec = 0;
    216   1.3       jtc 	value.it_value = value.it_interval;
    217   1.3       jtc 	(void)setitimer(ITIMER_REAL, &value, NULL);
    218   1.1       cgd 	for (;;) {
    219   1.1       cgd 		cc = read(master, obuf, sizeof (obuf));
    220   1.1       cgd 		if (cc <= 0)
    221   1.1       cgd 			break;
    222   1.3       jtc 		(void)write(1, obuf, cc);
    223   1.8    atatat 		if (rawout)
    224   1.8    atatat 			record(fscript, obuf, cc, 'o');
    225   1.8    atatat 		else
    226   1.8    atatat 			(void)fwrite(obuf, 1, cc, fscript);
    227   1.3       jtc 		outcc += cc;
    228   1.1       cgd 	}
    229   1.1       cgd 	done();
    230   1.1       cgd }
    231   1.1       cgd 
    232   1.3       jtc void
    233  1.11    rpaulo scriptflush(int signo)
    234   1.1       cgd {
    235   1.3       jtc 	if (outcc) {
    236   1.3       jtc 		(void)fflush(fscript);
    237   1.3       jtc 		outcc = 0;
    238   1.1       cgd 	}
    239   1.1       cgd }
    240   1.1       cgd 
    241   1.3       jtc void
    242   1.3       jtc doshell()
    243   1.1       cgd {
    244  1.17     lukem 	const char *shell;
    245   1.3       jtc 
    246   1.3       jtc 	shell = getenv("SHELL");
    247   1.3       jtc 	if (shell == NULL)
    248   1.3       jtc 		shell = _PATH_BSHELL;
    249   1.1       cgd 
    250   1.3       jtc 	(void)close(master);
    251   1.3       jtc 	(void)fclose(fscript);
    252   1.3       jtc 	login_tty(slave);
    253   1.4     mikel 	execl(shell, shell, "-i", NULL);
    254   1.5     lukem 	warn("execl %s", shell);
    255   1.3       jtc 	fail();
    256   1.1       cgd }
    257   1.1       cgd 
    258   1.3       jtc void
    259   1.1       cgd fail()
    260   1.1       cgd {
    261   1.1       cgd 
    262   1.3       jtc 	(void)kill(0, SIGTERM);
    263   1.1       cgd 	done();
    264   1.1       cgd }
    265   1.1       cgd 
    266   1.3       jtc void
    267   1.1       cgd done()
    268   1.1       cgd {
    269   1.3       jtc 	time_t tvec;
    270   1.1       cgd 
    271   1.1       cgd 	if (subchild) {
    272   1.3       jtc 		tvec = time(NULL);
    273   1.8    atatat 		if (rawout)
    274   1.8    atatat 			record(fscript, NULL, 0, 'e');
    275   1.8    atatat 		else
    276   1.8    atatat 			(void)fprintf(fscript,"\nScript done on %s",
    277   1.8    atatat 			    ctime(&tvec));
    278   1.3       jtc 		(void)fclose(fscript);
    279   1.3       jtc 		(void)close(master);
    280   1.1       cgd 	} else {
    281   1.3       jtc 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
    282   1.3       jtc 		(void)printf("Script done, output file is %s\n", fname);
    283   1.1       cgd 	}
    284   1.8    atatat 	exit(0);
    285   1.8    atatat }
    286   1.8    atatat 
    287   1.8    atatat void
    288  1.17     lukem record(FILE *fp, char *buf, size_t cc, int direction)
    289   1.8    atatat {
    290   1.8    atatat 	struct iovec iov[2];
    291   1.8    atatat 	struct stamp stamp;
    292   1.8    atatat 	struct timeval tv;
    293   1.8    atatat 
    294   1.8    atatat 	(void)gettimeofday(&tv, NULL);
    295   1.8    atatat 	stamp.scr_len = cc;
    296   1.8    atatat 	stamp.scr_sec = tv.tv_sec;
    297   1.8    atatat 	stamp.scr_usec = tv.tv_usec;
    298   1.8    atatat 	stamp.scr_direction = direction;
    299   1.8    atatat 	iov[0].iov_len = sizeof(stamp);
    300   1.8    atatat 	iov[0].iov_base = &stamp;
    301   1.8    atatat 	iov[1].iov_len = cc;
    302   1.8    atatat 	iov[1].iov_base = buf;
    303  1.17     lukem 	if (writev(fileno(fp), &iov[0], 2) == -1)
    304   1.8    atatat 		err(1, "writev");
    305   1.8    atatat }
    306   1.8    atatat 
    307  1.14  christos void
    308  1.17     lukem consume(FILE *fp, off_t len, char *buf, int reg)
    309  1.14  christos {
    310  1.14  christos 	size_t l;
    311  1.14  christos 
    312  1.14  christos 	if (reg) {
    313  1.17     lukem 		if (fseeko(fp, len, SEEK_CUR) == -1)
    314  1.14  christos 			err(1, NULL);
    315  1.14  christos 	}
    316  1.14  christos 	else {
    317  1.14  christos 		while (len > 0) {
    318  1.14  christos 			l = MIN(DEF_BUF, len);
    319  1.17     lukem 			if (fread(buf, sizeof(char), l, fp) != l)
    320  1.14  christos 				err(1, "cannot read buffer");
    321  1.14  christos 			len -= l;
    322  1.14  christos 		}
    323  1.14  christos 	}
    324  1.14  christos }
    325  1.14  christos 
    326   1.8    atatat #define swapstamp(stamp) do { \
    327   1.8    atatat 	if (stamp.scr_direction > 0xff) { \
    328   1.8    atatat 		stamp.scr_len = bswap64(stamp.scr_len); \
    329   1.8    atatat 		stamp.scr_sec = bswap64(stamp.scr_sec); \
    330   1.8    atatat 		stamp.scr_usec = bswap32(stamp.scr_usec); \
    331   1.8    atatat 		stamp.scr_direction = bswap32(stamp.scr_direction); \
    332   1.8    atatat 	} \
    333   1.8    atatat } while (0/*CONSTCOND*/)
    334   1.8    atatat 
    335   1.8    atatat void
    336  1.17     lukem playback(FILE *fp)
    337   1.8    atatat {
    338   1.8    atatat 	struct timespec tsi, tso;
    339   1.8    atatat 	struct stamp stamp;
    340  1.13  christos 	struct stat pst;
    341  1.12  liamjfoy 	char buf[DEF_BUF];
    342  1.12  liamjfoy 	off_t nread, save_len;
    343   1.8    atatat 	size_t l;
    344  1.17     lukem 	time_t tclock;
    345  1.13  christos 	int reg;
    346   1.8    atatat 
    347  1.17     lukem 	if (fstat(fileno(fp), &pst) == -1)
    348  1.12  liamjfoy 		err(1, "fstat failed");
    349  1.12  liamjfoy 
    350  1.13  christos 	reg = S_ISREG(pst.st_mode);
    351  1.13  christos 
    352  1.13  christos 	for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
    353  1.17     lukem 		if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
    354  1.13  christos 			if (reg)
    355  1.13  christos 				err(1, "reading playback header");
    356  1.13  christos 			else
    357  1.13  christos 				break;
    358  1.13  christos 		}
    359  1.12  liamjfoy 		swapstamp(stamp);
    360  1.12  liamjfoy 		save_len = sizeof(stamp);
    361   1.8    atatat 
    362  1.13  christos 		if (reg && stamp.scr_len >
    363  1.13  christos 		    (uint64_t)(pst.st_size - save_len) - nread)
    364  1.12  liamjfoy 			err(1, "invalid stamp");
    365  1.12  liamjfoy 
    366  1.12  liamjfoy 		save_len += stamp.scr_len;
    367  1.17     lukem 		tclock = stamp.scr_sec;
    368   1.8    atatat 		tso.tv_sec = stamp.scr_sec;
    369   1.8    atatat 		tso.tv_nsec = stamp.scr_usec * 1000;
    370   1.8    atatat 
    371   1.8    atatat 		switch (stamp.scr_direction) {
    372   1.8    atatat 		case 's':
    373  1.17     lukem 			(void)printf("Script started on %s", ctime(&tclock));
    374   1.8    atatat 			tsi = tso;
    375  1.17     lukem 			(void)consume(fp, stamp.scr_len, buf, reg);
    376   1.8    atatat 			break;
    377   1.8    atatat 		case 'e':
    378  1.17     lukem 			(void)printf("\nScript done on %s", ctime(&tclock));
    379  1.17     lukem 			(void)consume(fp, stamp.scr_len, buf, reg);
    380   1.8    atatat 			break;
    381   1.8    atatat 		case 'i':
    382   1.8    atatat 			/* throw input away */
    383  1.17     lukem 			(void)consume(fp, stamp.scr_len, buf, reg);
    384   1.8    atatat 			break;
    385   1.8    atatat 		case 'o':
    386   1.8    atatat 			tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
    387   1.8    atatat 			tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
    388   1.8    atatat 			if (tsi.tv_nsec < 0) {
    389   1.8    atatat 				tsi.tv_sec -= 1;
    390   1.8    atatat 				tsi.tv_nsec += 1000000000;
    391   1.8    atatat 			}
    392   1.8    atatat 			if (usesleep)
    393   1.8    atatat 				(void)nanosleep(&tsi, NULL);
    394   1.8    atatat 			tsi = tso;
    395  1.12  liamjfoy 			while (stamp.scr_len > 0) {
    396  1.12  liamjfoy 				l = MIN(DEF_BUF, stamp.scr_len);
    397  1.17     lukem 				if (fread(buf, sizeof(char), l, fp) != l)
    398  1.12  liamjfoy 					err(1, "cannot read buffer");
    399  1.12  liamjfoy 
    400  1.12  liamjfoy 				(void)write(STDOUT_FILENO, buf, l);
    401  1.12  liamjfoy 				stamp.scr_len -= l;
    402  1.12  liamjfoy 			}
    403   1.8    atatat 			break;
    404  1.12  liamjfoy 		default:
    405  1.12  liamjfoy 			err(1, "invalid direction");
    406   1.8    atatat 		}
    407  1.12  liamjfoy 	}
    408  1.17     lukem 	(void)fclose(fp);
    409   1.1       cgd 	exit(0);
    410   1.1       cgd }
    411