Annotation of 43BSDReno/contrib/isode-beta/others/quipu/uips/dish/pipe.c, revision 1.1.1.1

1.1       root        1: /* pipe.c - Dish shell command handler */
                      2: 
                      3: #ifndef        lint
                      4: static char *rcsid = "$Header: /f/osi/others/quipu/uips/dish/RCS/pipe.c,v 7.1 90/03/15 11:20:36 mrose Exp $";
                      5: #endif
                      6: 
                      7: /* 
                      8:  * $Header: /f/osi/others/quipu/uips/dish/RCS/pipe.c,v 7.1 90/03/15 11:20:36 mrose Exp $
                      9:  *
                     10:  *
                     11:  * $Log:       pipe.c,v $
                     12:  * Revision 7.1  90/03/15  11:20:36  mrose
                     13:  * quipu-sync
                     14:  * 
                     15:  * Revision 7.0  89/11/23  22:08:32  mrose
                     16:  * Release 6.0
                     17:  * 
                     18:  */
                     19: 
                     20: /*
                     21:  *                               NOTICE
                     22:  *
                     23:  *    Acquisition, use, and distribution of this module and related
                     24:  *    materials are subject to the restrictions of a license agreement.
                     25:  *    Consult the Preface in the User's Manual for the full terms of
                     26:  *    this agreement.
                     27:  *
                     28:  */
                     29: 
                     30: 
                     31: #include <stdio.h>
                     32: #include "quipu/util.h"
                     33: #include "tailor.h"
                     34: #include "general.h"
                     35: #include "usr.dirent.h"
                     36: 
                     37: extern int      errno;
                     38: 
                     39: #ifdef SOCKETS         /* USE INTERNET SOCKETS */
                     40: 
                     41: #include "internet.h"
                     42: 
                     43: main(argc,argv)
                     44: int    argc;
                     45: char   *argv[];
                     46: {
                     47:        int                     sd,res;
                     48:        struct sockaddr_in      sin_buf;
                     49:        struct sockaddr_in      * sin = &sin_buf;
                     50:        char                    buffer [BUFSIZ];
                     51:        char                    dishname [BUFSIZ];
                     52:        char                    * ptr;
                     53:        
                     54:        isodetailor (argv[0], 1);
                     55: 
                     56:        if ((sd = start_tcp_client ((struct sockaddr_in *) 0, 0)) == NOTOK) {
                     57:                perror("start_tcp_client");
                     58:                exit(-20);
                     59:        }
                     60: 
                     61:        if (get_dish_sock (sin, 0) != 0)
                     62:                exit (-21);
                     63: 
                     64:        if (join_tcp_server (sd, sin) == NOTOK) {
                     65:                int     pid;
                     66:                (void) close_tcp_socket (sd);
                     67: 
                     68: fork_again: ;
                     69:                switch (pid = vfork ()) {
                     70:                case 0:
                     71:                        /* child */
                     72:                        (void) close_tcp_socket (sd);
                     73:                        (void) strcpy (dishname,
                     74:                                       _isodefile (isodebinpath, "dish"));
                     75:                        {
                     76:                                int i, nfds = getdtablesize ();
                     77: 
                     78:                                for (i = fileno(stderr) + 1; i < nfds; i++)
                     79:                                        (void) close (i);
                     80:                        }
                     81:                        execl (dishname, "dish","-pipe",NULLCP);
                     82:                        (void) fprintf (stderr, "unable to exec ");
                     83:                        perror (dishname);
                     84:                        _exit (-21);
                     85:                case -1:
                     86:                        perror ("fork");
                     87:                        (void) close_tcp_socket (sd);
                     88:                        exit (-22);
                     89:                default:
                     90:                        /* parent */
                     91:                        for (;;) {
                     92:                            if ((sd = start_tcp_client ((struct sockaddr_in *) 0,
                     93:                                                        0)) == NOTOK) {
                     94:                                perror("start_tcp_client");
                     95:                                exit(-23);
                     96:                            }
                     97:                            if (join_tcp_server (sd, sin) != NOTOK)
                     98:                                break;
                     99: 
                    100:                            /* need to introduce a timeout !!! */
                    101:                            (void) close_tcp_socket (sd);
                    102: 
                    103:                            sleep (5);
                    104: 
                    105:                            if (kill (pid, 0) == NOTOK) {
                    106:                                (void) fprintf (stderr,"Trying again...\n");
                    107:                                goto fork_again;
                    108:                            }
                    109:                        }
                    110:                        break;
                    111:                }
                    112:        }
                    113: 
                    114:        if ((ptr = rindex (argv[0], '/')) == NULLCP)
                    115:                (void) strcpy (buffer,argv[0]);
                    116:        else
                    117:                (void) strcpy (buffer,++ptr);
                    118: 
                    119:        argc--,argv++;
                    120: 
                    121:        while (argc--) {
                    122:                (void) strcat (buffer, " \"");
                    123:                (void) strcat (buffer, *argv++);
                    124:                (void) strcat (buffer, "\"");
                    125:        }
                    126: 
                    127:        if (send(sd, buffer, strlen(buffer), 0) == -1) {
                    128:                perror("send");
                    129:                (void) close_tcp_socket (sd);
                    130:                exit (-25);
                    131:        }
                    132: 
                    133:        for (;;) {
                    134:                if ((res = recv(sd, buffer, BUFSIZ - 1, 0)) == -1) {
                    135:                        perror ("recv");
                    136:                        (void) close_tcp_socket (sd);
                    137:                        exit (-26);
                    138:                }
                    139:                *(buffer + res) = 0;
                    140:                if (res == 0) {
                    141:                        (void) close_tcp_socket (sd);
                    142:                        exit (0);
                    143:                }
                    144: 
                    145:                if (*buffer == '2') {
                    146:                        if (res > 1)
                    147:                                (void) write (2,&buffer[1],--res);
                    148:                        while ( (res = recv(sd, buffer, BUFSIZ, 0)) > 0)
                    149:                                (void) write (2,buffer,res);
                    150:                        (void) close_tcp_socket (sd);
                    151:                        exit (1);                       
                    152:                } else if ((*buffer == '1') || (*buffer == '3')) {
                    153:                        int eval;
                    154:                        eval = (*buffer == '1' ? 0 : 2);
                    155:                        if (res > 1)
                    156:                                (void) write (1,&buffer[1],--res);
                    157:                         while ( (res = recv(sd, buffer, BUFSIZ, 0)) > 0)
                    158:                                 (void) write (1,buffer,res);
                    159:                         (void) close_tcp_socket (sd);
                    160:                         exit (eval);
                    161:                 } else {               /* 'e', 'y', or 'p' */
                    162:                        char            where[BUFSIZ];
                    163: 
                    164:                        if (*buffer == 'e') {
                    165:                                if (system (&buffer[1]))
                    166:                                    (void) strcpy (where, "e");
                    167:                                else
                    168:                                    (void) getcwd (where, sizeof where);
                    169:                        } else if (*buffer == 'y') {
                    170:                                (void) fprintf (stderr,"%s",buffer + 1);
                    171:                                (void) fgets (where, sizeof where, stdin);
                    172:                        } else {
                    173:                                (void) sprintf (where,
                    174:                                                "Enter password for \"%s\": ",
                    175:                                                buffer + 1);
                    176:                                (void) strcpy (where, getpassword (where));
                    177:                        }
                    178: 
                    179:                        if (send(sd, where, strlen(where), 0) == -1) {
                    180:                                perror("send");
                    181:                                (void) close_tcp_socket (sd);
                    182:                                exit (-27);
                    183:                        }
                    184: 
                    185:                }
                    186: 
                    187:        }
                    188: }
                    189: 
                    190: 
                    191: #else  /* USE UNIX NAMED PIPES */
                    192: 
                    193: #include <signal.h>
                    194: #include <fcntl.h>
                    195: #include <sys/types.h>
                    196: #include <sys/stat.h>
                    197: 
                    198: char            retfile[BUFSIZ];
                    199: int             fd;
                    200: int            wfd;
                    201: 
                    202: main (argc, argv)
                    203: int             argc;
                    204: char          **argv;
                    205: {
                    206:        int             res;
                    207:        char            buffer[BUFSIZ];
                    208:        char            sendfile[BUFSIZ];
                    209:        char            dishname[BUFSIZ];
                    210:        int             i;
                    211:        char           *ptr, *getenv(), *sprintf(), *getpassword ();
                    212:        void            pipe_quit ();
                    213: 
                    214: 
                    215:        (void) umask (0);
                    216:        (void) sprintf (retfile, "/tmp/dish%d", getpid ());
                    217:        if ((ptr = getenv ("DISHPROC")) == NULLCP) {
                    218:            (void) sprintf (sendfile, "/tmp/dish-%d", getppid ());
                    219:            (void) setenv ("DISHPROC", sendfile);
                    220:        }
                    221:        else
                    222:            (void) strcpy (sendfile, ptr);
                    223: 
                    224:        setbuf (stdout,NULLCP);
                    225:        setbuf (stderr,NULLCP);
                    226: 
                    227:        if (mknod (retfile, S_IFIFO | 0600, 0) == -1) {
                    228:                (void) fprintf (stderr,"Can't create pipe '%s'\n",retfile);
                    229:                exit (-12);
                    230:        }
                    231:        
                    232:        for (i = 1; i <= 15; i++)
                    233:                (void) signal (i, pipe_quit);
                    234: 
                    235:        if ((fd = open (sendfile, O_WRONLY | O_NDELAY)) == -1) {
                    236:                (void) fprintf (stderr, "(Dish starting)\n");
                    237:                if (mknod (sendfile, S_IFIFO | 0600, 0) == -1) {
                    238:                        (void) fprintf (stderr,"Can't create pipe '%s'\n",sendfile);
                    239:                        exit (-11);
                    240:                }
                    241:                (void) strcpy (dishname, _isodefile (isodebinpath, "dish"));
                    242:                if (vfork () == 0) {
                    243:                        /* child */
                    244:                        execl (dishname, "dish","-pipe",NULLCP);
                    245:                        (void) fprintf (stderr, "unable to exec ");
                    246:                        perror (dishname);
                    247:                        (void) unlink (retfile);
                    248:                        _exit (-2);
                    249:                } else {
                    250:                        fd = open (sendfile, O_WRONLY);
                    251:                        setbuf (stderr,NULLCP);
                    252:                }
                    253:        }
                    254:        argc--;
                    255:        if ((ptr = rindex (argv[0], '/')) == NULLCP)
                    256:                (void) sprintf (buffer, "%s:%s", retfile, argv[0]);
                    257:        else
                    258:                (void) sprintf (buffer, "%s:%s", retfile, ++ptr);
                    259:        argv++;
                    260: 
                    261:        while (argc--) {
                    262:                (void) strcat (buffer, " \"");
                    263:                (void) strcat (buffer, *argv++);
                    264:                (void) strcat (buffer, "\"");
                    265:        }
                    266: 
                    267:        if ((res = write (fd, buffer, strlen (buffer))) == -1) {
                    268:                (void) fprintf (stderr, "Write to DUA failed... Please retry\n");
                    269:                (void) close (fd);
                    270:                (void) unlink (retfile);
                    271:                exit (-3);
                    272:        }
                    273:        (void) close (fd);
                    274: 
                    275:        /* get results */
                    276:        if ((fd = open (retfile, O_RDONLY)) < 0) {
                    277:                (void) fprintf (stderr, "Can't read results\n");
                    278:                (void) unlink (retfile);
                    279:                exit (-4);
                    280:        }
                    281: 
                    282:        if ((wfd = open (retfile, O_WRONLY)) < 0) {
                    283:                (void) fprintf (stderr, "Can't lock results failed\n");
                    284:                (void) unlink (retfile);
                    285:                (void) close (fd);
                    286:                exit (-5);
                    287:        }
                    288: 
                    289:        for (;;) {
                    290:                if ((res = read (fd, buffer, BUFSIZ - 1)) == -1) {
                    291:                        (void) fprintf (stderr, "Read failed (%d)\n", errno);
                    292:                        (void) unlink (retfile);
                    293:                        (void) close (fd);
                    294:                        (void) close (wfd);
                    295:                        exit (-6);
                    296:                }
                    297:                *(buffer + res) = 0;
                    298: 
                    299:                if (*buffer == '2') {
                    300:                        (void) write (2,&buffer[1],--res);
                    301:                        while ( (res = read (fd, buffer, BUFSIZ)) > 0)
                    302:                                (void) write (2,buffer,res);
                    303:                        (void) close (fd);
                    304:                        (void) close (wfd);
                    305:                        (void) unlink (retfile);
                    306:                        exit (-1);                      
                    307:                } else if ((*buffer == '1') || (*buffer == '3')) {
                    308:                        int eval;
                    309:                        eval = (*buffer == '1' ? 0 : 2);
                    310:                         (void) write (1,&buffer[1],--res);
                    311:                         while ( (res = read (fd, buffer, BUFSIZ)) > 0)
                    312:                                 (void) write (1,buffer,res);
                    313:                        (void) close (fd);
                    314:                        (void) close (wfd);
                    315:                        (void) unlink (retfile);
                    316:                         exit (eval);
                    317:                else {          /* 'e', 'y' or 'p' */
                    318:                        int             res2,
                    319:                                        nfd;
                    320:                        char            where[BUFSIZ];
                    321: 
                    322:                        if (*buffer == 'e') {
                    323:                                (void) getwd (where);
                    324:                                (void) system (&buffer[1]);
                    325:                        } else if (*buffer == 'y') {
                    326:                                (void) fprintf (stderr,"%s",buffer + 1);
                    327:                                (void) fgets (where, sizeof where, stdin);
                    328:                        } else {
                    329:                                (void) sprintf (where,
                    330:                                                "Enter password for \"%s\": ",
                    331:                                                buffer + 1);
                    332:                                (void) strcpy (where, getpassword (where));
                    333:                        }
                    334: 
                    335: 
                    336:                        if ((nfd = open (sendfile, O_WRONLY)) == -1) {
                    337:                                (void) fprintf (stderr, "re-write open error\n");
                    338:                                (void) close (wfd);
                    339:                                (void) close (fd);
                    340:                                (void) unlink (retfile);
                    341:                                exit (-9);
                    342:                        }
                    343:                        if ((res2 = write (nfd, where, strlen (where))) == -1) {
                    344:                                (void) fprintf (stderr, "Write to DUA failed (%d)... Please retry\n", res2);
                    345:                                (void) close (fd);
                    346:                                (void) close (nfd);
                    347:                                (void) close (wfd);
                    348:                                (void) unlink (retfile);
                    349:                                exit (-10);
                    350:                        }
                    351:                        (void) close (nfd);
                    352:                }
                    353:        }
                    354: }
                    355: 
                    356: void pipe_quit (sig)
                    357: int     sig;
                    358: {
                    359:        (void) unlink (retfile);
                    360:        (void) fprintf (stderr,"(signal %d) exiting...\n",sig);
                    361:        exit(0);
                    362: }
                    363: #endif

unix.superglobalmegacorp.com

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