Annotation of 43BSD/contrib/jove/portsrv.c, revision 1.1.1.1

1.1       root        1: /*************************************************************************
                      2:  * This program is copyright (C) 1985, 1986 by Jonathan Payne.  It is    *
                      3:  * provided to you without charge for use only on a licensed Unix        *
                      4:  * system.  You may copy JOVE provided that this notice is included with *
                      5:  * the copy.  You may not sell copies of this program or versions        *
                      6:  * modified for use on microcomputer systems, unless the copies are      *
                      7:  * included with a Unix system distribution and the source is provided.  *
                      8:  *************************************************************************/
                      9: 
                     10: /* This is a server for jove sub processes.  It runs the command and
                     11:    signals jove when there is some output ready to send to jove. By the
                     12:    time we get here, out standard output goes to jove's process input. */
                     13: 
                     14: #include "tune.h"
                     15: 
                     16: #ifdef PIPEPROCS       /* the whole file! */
                     17: 
                     18: #include "jove.h"
                     19: 
                     20: #include <signal.h>
                     21: #include <sys/ioctl.h>
                     22: #ifdef BSD4_2
                     23: #   include <sys/wait.h>
                     24: #else
                     25: #   include <wait.h>
                     26: #endif
                     27: 
                     28: struct header {
                     29:        int     pid;
                     30:        int     nbytes;
                     31:        char    buf[512];
                     32: } header;
                     33: 
                     34: #define HEADSIZE       ((sizeof header.pid) + sizeof (header.nbytes))
                     35: 
                     36: error(str)
                     37: char   *str;
                     38: {
                     39:        header.pid = getpid();
                     40:        header.nbytes = strlen(str);
                     41:        strcpy(header.buf, str);
                     42:        proc_write(&header, header.nbytes + 8);
                     43:        exit(-2);
                     44: }
                     45: 
                     46: int    ppid,
                     47:        InputFD,
                     48:        JovesInput;
                     49: 
                     50: p_inform()
                     51: {
                     52:        long    nbytes;
                     53: 
                     54:        ioctl(JovesInput, FIONREAD, (char *) &nbytes);
                     55:        if (nbytes > 0)
                     56:                kill(ppid, INPUT_SIG);
                     57: }
                     58: 
                     59: proc_write(ptr, n)
                     60: char   *ptr;
                     61: {
                     62:        long    nbytes;
                     63: 
                     64:        ioctl(1, FIONREAD, (char *) &nbytes);
                     65:        
                     66:        if (nbytes == 0)
                     67:                kill(ppid, INPUT_SIG);
                     68: 
                     69:        (void) write(1, ptr, n);
                     70:        alarm(1);
                     71: }
                     72: 
                     73: read_pipe()
                     74: {
                     75:        register int    n;
                     76:        
                     77:        (void) signal(SIGALRM, p_inform);
                     78: 
                     79:        while ((header.nbytes = read(InputFD, header.buf, sizeof header.buf)) > 0) {
                     80:                n = HEADSIZE + header.nbytes;
                     81:                proc_write(&header, n);
                     82:        }
                     83: }
                     84: 
                     85: /* ARGSUSED */
                     86: main(argc, argv)
                     87: char   *argv[];
                     88: {
                     89:        int     p[2];
                     90:        int     pid;
                     91: 
                     92:        if (pipe(p) == -1)
                     93:                error("Cannot pipe jove portsrv.\n");
                     94: 
                     95:        ppid = getppid();
                     96:        switch (pid = fork()) {
                     97:        case -1:
                     98:                error("portsrv: cannot fork.\n");
                     99: 
                    100:        case 0:
                    101:                /* We'll intercept childs output in p[0] */
                    102:                (void) dup2(p[1], 1);
                    103:                (void) dup2(p[1], 2);
                    104:                (void) close(p[0]);
                    105:                (void) close(p[1]);
                    106:                        
                    107:                (void) setpgrp(getpid(), getpid());
                    108:                execv(argv[2], &argv[3]);
                    109:                _exit(-4);
                    110: 
                    111:        default:
                    112:                (void) close(0);
                    113:                                /* Don't want this guy to read anything
                    114:                                   jove sends to our soon to be created
                    115:                                   child */
                    116: 
                    117:                JovesInput = atoi(argv[1]);
                    118:                (void) signal(SIGINT, SIG_IGN);
                    119:                (void) signal(SIGQUIT, SIG_IGN);
                    120:                (void) close(p[1]);
                    121: 
                    122:                /* Tell jove the pid of the real child as opposed to us. */
                    123:                header.pid = getpid();
                    124:                header.nbytes = sizeof (int);
                    125:                *(int *) header.buf = pid;
                    126:                (void) write(1, (char *) &header, sizeof pid + HEADSIZE);
                    127:                p_inform();     /* Inform jove */
                    128: 
                    129:                /* Read proc's output and send it to jove */
                    130:                InputFD = p[0];
                    131:                read_pipe();
                    132:                (void) close(p[0]);
                    133:                header.pid = getpid();
                    134:                header.nbytes = EOF;    /* Tell jove we are finished */
                    135:                (void) write(1, (char *) &header, HEADSIZE);
                    136:                p_inform();
                    137:                /* Try to exit like our child did ... */
                    138:                {
                    139:                        union wait      w;
                    140: 
                    141: #ifndef VMUNIX
                    142:                        while (wait2(&w.w_status, 0) != pid)
                    143: #else
                    144:                        while (wait3(&w.w_status, 0, 0) != pid)
                    145: #endif
                    146:                                ;
                    147:                        if (WIFEXITED(w))
                    148:                                exit(w.w_retcode);
                    149:                        else if (WIFSIGNALED(w))
                    150:                                kill(getpid(), w.w_termsig);
                    151:                }
                    152:        }
                    153: }
                    154: 
                    155: #else PIPEPROCS
                    156: main()
                    157: {
                    158: }
                    159: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.