Annotation of researchv8dc/netfs/tdkdial.c, revision 1.1.1.1

1.1       root        1: #define DKMSGS 1
                      2: #include <dk.h>
                      3: #include <dkerr.h>
                      4: #include <sgtty.h>
                      5: #include <stdio.h>
                      6: #include <signal.h>
                      7: #include "dkdial.h"
                      8: 
                      9: #define SUBR "/etc/dkdialsub"
                     10: 
                     11: int    dkverbose;
                     12: int    dkrchan;
                     13: 
                     14: static char    *argbuf;
                     15: static char    **bufptr;
                     16: extern int     rdk_ld;
                     17: extern         int     errno ;
                     18: 
                     19: int
                     20: tdkdial(dialstr, traffic)
                     21: int traffic ;
                     22: char * dialstr ;
                     23: {
                     24:        return (_tdkdial(1, traffic, dialstr)) ;
                     25: }
                     26: 
                     27: int
                     28: tdkserv(srvname, maxtraf)
                     29: int maxtraf ;
                     30: char * srvname ;
                     31: {
                     32:        return (_tdkdial(2, maxtraf, srvname)) ;
                     33: }
                     34: 
                     35: 
                     36: int
                     37: _tdkdial (type, traffic, dialstr)
                     38: int type ;
                     39: int traffic ;
                     40: char * dialstr ;
                     41: {
                     42:        register int    fd, pid, rc;
                     43:        char    args[100];
                     44:        char    *argptrs[10];
                     45:        int     status;
                     46:        struct dialout reply;
                     47:        int     pipefd[2];
                     48:        register i;
                     49:        char outname[64];
                     50:        int     popld ;
                     51:        int     oldchdie ;
                     52: 
                     53:        /* establish the initial connection */
                     54:        strcpy(outname, "/dev/dk/dkxx");
                     55:        for (i=3; i<64; i+=2) {
                     56:                outname[10] = i/10 + '0';
                     57:                outname[11] = i%10 + '0';
                     58:                if ((fd = open(outname, 2)) >= 0)
                     59:                        break;
                     60:        }
                     61:        if (fd < 0) {
                     62:                tdkerrmess ("dk busy or no listener\n");
                     63:                return fd;
                     64:        }
                     65:        ioctl(fd, FIOPUSHLD, &rdk_ld) ;
                     66: 
                     67:        /* create a pipe to the sub-process */
                     68:        if (pipe (pipefd) < 0) {
                     69:                tdkerrmess ("cannot create a pipe\n");
                     70:                close (fd);
                     71:                return -1;
                     72:        }
                     73: 
                     74:        /* build the arg list for the sub-process */
                     75:        arginit (args, argptrs);
                     76:        strarg ("tdkdial");
                     77:        intarg (fd);
                     78:        intarg (pipefd[PIPEWRITE]);
                     79:        intarg (type);
                     80:        intarg(traffic) ;
                     81:        strarg(dialstr) ;
                     82:        argend();
                     83:        oldchdie = (int)signal(SIGCHLD, SIG_IGN) ;
                     84: 
                     85:        /* start up a sub-process */
                     86:        switch (pid = fork()) {
                     87: 
                     88:                /* child */
                     89:        case 0:
                     90:                close (pipefd[PIPEREAD]);
                     91:                setuid (geteuid());
                     92:                execv (SUBR, argptrs);
                     93:                tdkerrmess ("cannot execute dialer\n");
                     94:                exit (GENERR);
                     95: 
                     96:                /* error */
                     97:        case -1:
                     98:                close (pipefd[PIPEREAD]);
                     99:                close (pipefd[PIPEWRITE]);
                    100:                close(fd);
                    101:                tdkerrmess ("cannot fork\n");
                    102:                signal(SIGCHLD, oldchdie) ;
                    103:                return -1;
                    104: 
                    105:                /* parent */
                    106:        default:
                    107:                close (pipefd[PIPEWRITE]);
                    108: 
                    109:                /* wait for the child to complete */
                    110:                do 
                    111:                        rc = wait (&status);
                    112:                while (rc > 0 && rc != pid);
                    113: 
                    114:                signal(SIGCHLD, oldchdie) ;
                    115:                /* figure out what happened */
                    116:                if (rc < 0) {
                    117:                        tdkerrmess ("tdkdial process disappeared\n");
                    118:                        goto bad;
                    119:                }
                    120: 
                    121:                /* read the reply */
                    122:                rc = read (pipefd[PIPEREAD], &reply, sizeof (reply));
                    123:                if (rc != sizeof (reply)) {
                    124:                        tdkerrmess ("wrong length in reading pipe\n");
                    125:                        goto bad;
                    126:                }
                    127:                close (pipefd[PIPEREAD]);
                    128: 
                    129:                /* save the real channel number in case someone wants it */
                    130:                dkrchan = reply.param2;
                    131: 
                    132:                /* pretty normal return -- figure out what happened */
                    133:                switch (status) {
                    134: 
                    135:                        /* successfully connected */
                    136:                case NORM << 8:
                    137:                        ioctl(fd, FIOPOPLD, 0) ;
                    138:                        return fd;
                    139: 
                    140: 
                    141:                        /* couldn't connect */
                    142:                default:
                    143:                        if ((status & 0xff) == 0) {
                    144:                                register int    x = status >> 8;
                    145:                                if (x > ERRBASE && x < ERRBASE + NDKERR)
                    146:                                        tdkerrmess(dkmsgs[x-ERRBASE]);
                    147:                        }
                    148:                        goto bad;
                    149:                }
                    150:        }
                    151: 
                    152: bad:
                    153:        close (pipefd[PIPEREAD]);
                    154:        close (fd);
                    155:        return -1;
                    156: }
                    157: 
                    158: 
                    159: tdkerrmess (s)
                    160:        register char   *s;
                    161: {
                    162:        if (dkverbose)
                    163:                write (2, s, strlen(s));
                    164: }
                    165: 
                    166: 
                    167: /*
                    168:  *     The following subroutines help build argument lists
                    169:  */
                    170: 
                    171: static
                    172: arginit (x, y)
                    173:        char    *x;
                    174:        char    **y;
                    175: {
                    176:        argbuf = x;
                    177:        bufptr = y;
                    178: }
                    179: 
                    180: 
                    181: static
                    182: strarg (x)
                    183:        register char   *x;
                    184: {
                    185:        *bufptr++ = x;
                    186: }
                    187: 
                    188: 
                    189: static
                    190: intarg (x)
                    191:        int     x;
                    192: {
                    193:        *bufptr++ = argbuf;
                    194:        if (x < 0)
                    195:                *argbuf++ = '-';
                    196:        else
                    197:                x = -x;
                    198:        convint (x);
                    199:        *argbuf++ = '\0';
                    200: }
                    201: 
                    202: 
                    203: static
                    204: convint (x)
                    205:        register int    x;
                    206: {
                    207:        if (x <= -10)
                    208:                convint (x / 10);
                    209:        *argbuf++ = '0' - x % 10;
                    210: }
                    211: 
                    212: 
                    213: static
                    214: argend()
                    215: {
                    216:        *bufptr++ = 0;
                    217: }
                    218: 
                    219: 

unix.superglobalmegacorp.com

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