Annotation of researchv10dc/dk/libc/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 <errno.h>
                      8: #include "dkdial.h"
                      9: 
                     10: #define SUBR "/etc/dkdialsub"
                     11: 
                     12: int    dkverbose;
                     13: int    dkrchan;
                     14: int    dkerrno;
                     15: char   *dkerror;
                     16: 
                     17: static char *msgs[] = {
                     18:        NULL,                                   /* NORM */
                     19:        "bad call to dk dialler",               /* ILLEG */
                     20:        "can't tell who you are",               /* NOUID */
                     21:        "remote system doesn't respond",        /* NOREP */
                     22:        "can't find dial program",              /* NODIAL */
                     23:        "can't find outgoing channel",          /* NOOCHAN */
                     24:        "can't open manager channel",           /* NOMGR */
                     25:        "can't get dial tone",                  /* NODIALT */
                     26:        "DK controller system error",
                     27:        "destination busy",
                     28:        "remote node not answering",
                     29:        "destination not answering",
                     30:        "unassigned destination",
                     31:        "DK system overload",
                     32:        "server already exists",
                     33:        "permission denied by destination",
                     34: };
                     35: 
                     36: static char    *argbuf;
                     37: static char    **bufptr;
                     38: extern int     rdk_ld, dkp_ld;
                     39: extern         int     errno ;
                     40: 
                     41: int
                     42: tdkdial(dialstr, traffic)
                     43: int traffic ;
                     44: char * dialstr ;
                     45: {
                     46:        return (_tdkdial(1, traffic, dialstr)) ;
                     47: }
                     48: 
                     49: int
                     50: tdkserv(srvname, maxtraf)
                     51: int maxtraf ;
                     52: char * srvname ;
                     53: {
                     54:        return (_tdkdial(2, maxtraf, srvname)) ;
                     55: }
                     56: 
                     57: 
                     58: int
                     59: _tdkdial (type, traffic, dialstr)
                     60: int type ;
                     61: int traffic ;
                     62: char * dialstr ;
                     63: {
                     64:        register int fd, pid, rc;
                     65:        char args[100];
                     66:        char *argptrs[10];
                     67:        int status;
                     68:        struct dialout reply;
                     69:        int pipefd[2];
                     70:        register i;
                     71:        char outname[64];
                     72:        int popld ;
                     73:        int oldchdie ;
                     74:        int n3dig = 0, n2dig = 0;
                     75:        extern errno;
                     76:        static char alph[] = "0123456789abcdefghijklmnopqrstuvwxyz";
                     77: 
                     78:        /* establish the initial connection */
                     79:        if (traffic)
                     80:                traffic = 2;
                     81:        for (i=3; i<256; i+=2) {
                     82:                if (n3dig<3) {
                     83:                        sprintf(outname, "/dev/dk/dk%d%c%d", traffic,
                     84:                           alph[i/10], i%10);
                     85:                        if ((fd = open(outname, 2)) >= 0)
                     86:                                break;
                     87:                        if (errno==ENOENT)
                     88:                                n3dig++;
                     89:                }
                     90:                if (n2dig < 5) {
                     91:                        sprintf(outname, "/dev/dk/dk%c%d", alph[i/10], i%10);
                     92:                        if ((fd = open(outname, 2)) >= 0)
                     93:                                break;
                     94:                        if (errno==ENOENT)
                     95:                                ++n2dig;
                     96:                }
                     97:        }
                     98:        if (fd < 0) {
                     99:                dkerrno = NOOCHAN ;
                    100:                dkerror = msgs[NOOCHAN];
                    101:                return fd;
                    102:        }
                    103:        /* Install URP now if traffic-type is 2, to permit */
                    104:        /*  window-size setting in dkdialsub */
                    105:        if (traffic==2)
                    106:                dkproto(fd, dkp_ld);
                    107:        ioctl(fd, FIOPUSHLD, &rdk_ld) ;
                    108: 
                    109:        /* create a pipe to the sub-process */
                    110:        if (pipe (pipefd) < 0) {
                    111:                dkerrno = NODIALT ;
                    112:                dkerror =  "Can't pipe to dialler";
                    113:                close (fd);
                    114:                return -1;
                    115:        }
                    116: 
                    117:        /* build the arg list for the sub-process */
                    118:        arginit (args, argptrs);
                    119:        strarg ("tdkdial");
                    120:        intarg (fd);
                    121:        intarg (pipefd[1]);
                    122:        intarg (type);
                    123:        intarg(traffic) ;
                    124:        strarg(dialstr) ;
                    125:        argend();
                    126:        oldchdie = (int)signal(SIGCHLD, SIG_IGN) ;
                    127: 
                    128:        /* start up a sub-process */
                    129:        switch (pid = fork()) {
                    130: 
                    131:                /* child */
                    132:        case 0:
                    133:                close (pipefd[0]);
                    134:                setuid (geteuid());
                    135:                execv (SUBR, argptrs);
                    136:                exit (NODIAL);
                    137: 
                    138:                /* error */
                    139:        case -1:
                    140:                close (pipefd[0]);
                    141:                close (pipefd[1]);
                    142:                close(fd);
                    143:                dkerrno = NODIAL ;
                    144:                dkerror = "DK dialler can't fork";
                    145:                signal(SIGCHLD, oldchdie) ;
                    146:                return -1;
                    147: 
                    148:                /* parent */
                    149:        default:
                    150:                close (pipefd[1]);
                    151: 
                    152:                /* wait for the child to complete */
                    153:                do 
                    154:                        rc = wait (&status);
                    155:                while (rc > 0 && rc != pid);
                    156:                signal(SIGCHLD, oldchdie) ;
                    157:                /* figure out what happened */
                    158:                if (rc < 0 || status&0xff) {
                    159:                        dkerrno = NODIAL ;
                    160:                        dkerror = "DK dialler exited abnormally";
                    161:                        goto bad;
                    162:                }
                    163:                /* read the reply */
                    164:                rc = read (pipefd[0], &reply, sizeof (reply));
                    165:                if (rc != sizeof (reply)) {
                    166:                        dkerrno = NODIAL ;
                    167:                        dkerror = "Bad reply from DK dialler";
                    168:                        goto bad;
                    169:                }
                    170:                close (pipefd[0]);
                    171:                if ((status & 0xFF00) == 0) {
                    172:                        ioctl(fd, FIOPOPLD, 0) ;
                    173:                        dkerrno = 0 ;
                    174:                        dkerror = NULL ;
                    175:                        return fd;
                    176:                }
                    177:                status >>= 8;
                    178:                dkerrno = status ;
                    179:                if (status < sizeof(msgs)/sizeof(msgs[0]))
                    180:                        dkerror = msgs[status];
                    181:                else {
                    182:                        static char msg[32];
                    183:                        sprintf(msg, "DK dial error %d", status-8);
                    184:                        dkerror = msg;
                    185:                }
                    186:                goto bad;
                    187:        }
                    188: bad:
                    189:        close (pipefd[0]);
                    190:        close (fd);
                    191:        return -1;
                    192: }
                    193: 
                    194: 
                    195: tdkerrmess (s)
                    196:        register char   *s;
                    197: {
                    198:        if (dkverbose)
                    199:                write (2, s, strlen(s));
                    200: }
                    201: 
                    202: 
                    203: /*
                    204:  *     The following subroutines help build argument lists
                    205:  */
                    206: 
                    207: static
                    208: arginit (x, y)
                    209:        char    *x;
                    210:        char    **y;
                    211: {
                    212:        argbuf = x;
                    213:        bufptr = y;
                    214: }
                    215: 
                    216: 
                    217: static
                    218: strarg (x)
                    219:        register char   *x;
                    220: {
                    221:        *bufptr++ = x;
                    222: }
                    223: 
                    224: 
                    225: static
                    226: intarg (x)
                    227:        int     x;
                    228: {
                    229:        *bufptr++ = argbuf;
                    230:        if (x < 0)
                    231:                *argbuf++ = '-';
                    232:        else
                    233:                x = -x;
                    234:        convint (x);
                    235:        *argbuf++ = '\0';
                    236: }
                    237: 
                    238: 
                    239: static
                    240: convint (x)
                    241:        register int    x;
                    242: {
                    243:        if (x <= -10)
                    244:                convint (x / 10);
                    245:        *argbuf++ = '0' - x % 10;
                    246: }
                    247: 
                    248: 
                    249: static
                    250: argend()
                    251: {
                    252:        *bufptr++ = 0;
                    253: }
                    254: 
                    255: 

unix.superglobalmegacorp.com

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