[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.27

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