[BACK]Return to script.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / script

Annotation of src/usr.bin/script/script.c, Revision 1.25

1.25    ! deraadt     1: /*     $OpenBSD: script.c,v 1.24 2005/12/12 20:10:53 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: #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:
1.5       deraadt    75: #include <util.h>
1.7       mickey     76: #include <err.h>
1.5       deraadt    77:
1.1       deraadt    78: FILE   *fscript;
                     79: int    master, slave;
1.20      deraadt    80: volatile sig_atomic_t child;
                     81: pid_t  subchild;
1.1       deraadt    82: char   *fname;
                     83:
1.16      deraadt    84: volatile sig_atomic_t dead;
                     85: volatile sig_atomic_t sigdeadstatus;
                     86: volatile sig_atomic_t flush;
                     87:
1.1       deraadt    88: struct termios tt;
                     89:
1.17      millert    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);
1.1       deraadt    97:
                     98: int
1.19      deraadt    99: main(int argc, char *argv[])
1.1       deraadt   100: {
1.21      mickey    101:        extern char *__progname;
1.16      deraadt   102:        struct sigaction sa;
1.1       deraadt   103:        struct termios rtt;
                    104:        struct winsize win;
1.16      deraadt   105:        char ibuf[BUFSIZ];
                    106:        ssize_t cc, off;
1.1       deraadt   107:        int aflg, ch;
                    108:
                    109:        aflg = 0;
1.3       millert   110:        while ((ch = getopt(argc, argv, "a")) != -1)
1.1       deraadt   111:                switch(ch) {
                    112:                case 'a':
                    113:                        aflg = 1;
                    114:                        break;
                    115:                default:
1.21      mickey    116:                        fprintf(stderr, "usage: %s [-a] [file]\n", __progname);
1.1       deraadt   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)
1.13      millert   128:                err(1, "%s", fname);
1.1       deraadt   129:
                    130:        (void)tcgetattr(STDIN_FILENO, &tt);
                    131:        (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
                    132:        if (openpty(&master, &slave, NULL, &tt, &win) == -1)
1.7       mickey    133:                err(1, "openpty");
1.1       deraadt   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:
1.16      deraadt   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:
1.1       deraadt   150:        child = fork();
                    151:        if (child < 0) {
1.21      mickey    152:                warn("fork");
1.1       deraadt   153:                fail();
                    154:        }
                    155:        if (child == 0) {
                    156:                subchild = child = fork();
                    157:                if (child < 0) {
1.21      mickey    158:                        warn("fork");
1.1       deraadt   159:                        fail();
                    160:                }
                    161:                if (child)
                    162:                        dooutput();
                    163:                else
                    164:                        doshell();
                    165:        }
                    166:
                    167:        (void)fclose(fscript);
1.16      deraadt   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);
1.24      deraadt   178:                        if (n == -1 && errno != EAGAIN)
                    179:                                break;
1.16      deraadt   180:                        if (n == 0)
                    181:                                break;  /* skip writing */
                    182:                        if (n > 0)
                    183:                                off += n;
                    184:                }
                    185:        }
                    186:        done(sigdeadstatus);
1.1       deraadt   187: }
                    188:
1.20      deraadt   189: /* ARGSUSED */
1.1       deraadt   190: void
1.19      deraadt   191: finish(int signo)
1.1       deraadt   192: {
1.8       deraadt   193:        int save_errno = errno;
1.16      deraadt   194:        int status, e = 1;
                    195:        pid_t pid;
1.1       deraadt   196:
1.16      deraadt   197:        while ((pid = wait3(&status, WNOHANG, 0)) > 0) {
1.20      deraadt   198:                if (pid == (pid_t)child) {
1.11      ericj     199:                        if (WIFEXITED(status))
1.16      deraadt   200:                                e = WEXITSTATUS(status);
1.11      ericj     201:                }
1.16      deraadt   202:        }
                    203:        dead = 1;
                    204:        sigdeadstatus = e;
1.12      espie     205:        errno = save_errno;
                    206: }
                    207:
1.20      deraadt   208: /* ARGSUSED */
1.12      espie     209: void
1.19      deraadt   210: handlesigwinch(int signo)
1.12      espie     211: {
1.16      deraadt   212:        int save_errno = errno;
1.12      espie     213:        struct winsize win;
                    214:        pid_t pgrp;
                    215:
                    216:        if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) != -1) {
1.16      deraadt   217:                ioctl(slave, TIOCSWINSZ, &win);
                    218:                if (ioctl(slave, TIOCGPGRP, &pgrp) != -1)
                    219:                        killpg(pgrp, SIGWINCH);
1.12      espie     220:        }
1.8       deraadt   221:        errno = save_errno;
1.1       deraadt   222: }
                    223:
                    224: void
1.19      deraadt   225: dooutput(void)
1.1       deraadt   226: {
1.16      deraadt   227:        struct sigaction sa;
1.1       deraadt   228:        struct itimerval value;
1.22      millert   229:        sigset_t blkalrm;
1.16      deraadt   230:        char obuf[BUFSIZ];
1.1       deraadt   231:        time_t tvec;
1.16      deraadt   232:        ssize_t outcc = 0, cc, off;
1.1       deraadt   233:
                    234:        (void)close(STDIN_FILENO);
                    235:        tvec = time(NULL);
                    236:        (void)fprintf(fscript, "Script started on %s", ctime(&tvec));
                    237:
1.22      millert   238:        sigemptyset(&blkalrm);
                    239:        sigaddset(&blkalrm, SIGALRM);
1.16      deraadt   240:        bzero(&sa, sizeof sa);
                    241:        sigemptyset(&sa.sa_mask);
                    242:        sa.sa_handler = scriptflush;
                    243:        (void)sigaction(SIGALRM, &sa, NULL);
                    244:
1.1       deraadt   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 (;;) {
1.16      deraadt   250:                if (flush) {
                    251:                        if (outcc) {
                    252:                                (void)fflush(fscript);
                    253:                                outcc = 0;
                    254:                        }
                    255:                        flush = 0;
                    256:                }
1.1       deraadt   257:                cc = read(master, obuf, sizeof (obuf));
1.16      deraadt   258:                if (cc == -1 && errno == EINTR)
                    259:                        continue;
1.1       deraadt   260:                if (cc <= 0)
                    261:                        break;
1.22      millert   262:                sigprocmask(SIG_BLOCK, &blkalrm, NULL);
1.16      deraadt   263:                for (off = 0; off < cc; ) {
1.23      deraadt   264:                        ssize_t n = write(STDOUT_FILENO, obuf + off, cc - off);
1.24      deraadt   265:                        if (n == -1 && errno != EAGAIN)
                    266:                                break;
1.16      deraadt   267:                        if (n == 0)
                    268:                                break;  /* skip writing */
                    269:                        if (n > 0)
                    270:                                off += n;
                    271:                }
1.1       deraadt   272:                (void)fwrite(obuf, 1, cc, fscript);
                    273:                outcc += cc;
1.22      millert   274:                sigprocmask(SIG_UNBLOCK, &blkalrm, NULL);
1.1       deraadt   275:        }
1.11      ericj     276:        done(0);
1.1       deraadt   277: }
                    278:
1.20      deraadt   279: /* ARGSUSED */
1.1       deraadt   280: void
1.19      deraadt   281: scriptflush(int signo)
1.1       deraadt   282: {
1.16      deraadt   283:        flush = 1;
1.1       deraadt   284: }
                    285:
                    286: void
1.19      deraadt   287: doshell(void)
1.1       deraadt   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);
1.15      deraadt   298:        execl(shell, shell, "-i", (char *)NULL);
1.21      mickey    299:        warn("%s", shell);
1.1       deraadt   300:        fail();
                    301: }
                    302:
                    303: void
1.19      deraadt   304: fail(void)
1.1       deraadt   305: {
                    306:
                    307:        (void)kill(0, SIGTERM);
1.11      ericj     308:        done(1);
1.1       deraadt   309: }
                    310:
                    311: void
1.19      deraadt   312: done(int eval)
1.1       deraadt   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:        }
1.11      ericj     325:        exit(eval);
1.1       deraadt   326: }