Annotation of researchv9/ipc/src/mgrs/dkmgr/dkctlchan.c, revision 1.1

1.1     ! root        1: #include <sys/ioctl.h>
        !             2: #include <stdio.h>
        !             3: #include <errno.h>
        !             4: #include <sys/dkio.h>
        !             5: 
        !             6: extern int errno;
        !             7: extern int rdk_ld;
        !             8: extern int dkp_ld;
        !             9: 
        !            10: static char *cnames[] = {
        !            11:        "/dev/dk/dkctl0",
        !            12:        "/dev/dk/dkctl2",
        !            13:        "/dev/dk/dkctl",
        !            14:        0
        !            15: };
        !            16: static char *names[] = {
        !            17:        "/dev/dk/dk0%c%d",
        !            18:        "/dev/dk/dk2%c%d",
        !            19:        "/dev/dk/dk%c%d",
        !            20:        0
        !            21: };
        !            22: 
        !            23: static int namefmt=-1;
        !            24: static char alph[] = "0123456789abcdefghijklmnopqrstuvwxyz";
        !            25: 
        !            26: static int high[] = {256, 256};
        !            27: static int low[] = {3, 3 };
        !            28: 
        !            29: /*
        !            30:  *     Open a free dk channel and remember the `type' in namefmt.
        !            31:  */
        !            32: int
        !            33: dkchan(traffic, unit)
        !            34:        int traffic;
        !            35:        int unit;
        !            36: {
        !            37:        int missing=0;
        !            38:        char outname[64];
        !            39:        register i,j;
        !            40:        int ai;
        !            41:        int fd;
        !            42: 
        !            43:        switch(unit) {
        !            44:        case '0':
        !            45:                ai = 0;
        !            46:                break;
        !            47:        case '2':
        !            48:                ai = 2;
        !            49:                break;
        !            50:        case 'b':
        !            51:                ai = traffic;
        !            52:                break;
        !            53:        default:
        !            54:                ai = -1;
        !            55:                break;
        !            56:        }
        !            57:        for (j=0; j<2; j++) {
        !            58:                for (i=low[j]; i<high[j]; i+=2) {
        !            59:                        if (ai>=0) {
        !            60:                                sprintf(outname, "/dev/dk/dk%d%c%d", ai,
        !            61:                                        alph[i/10], i%10);
        !            62:                                if ((fd = open(outname, 2)) >= 0) {
        !            63:                                        namefmt = ai ? 1 : 0;
        !            64:                                        break;
        !            65:                                }
        !            66:                        } else {
        !            67:                                sprintf(outname, "/dev/dk/dk%c%d",
        !            68:                                        alph[i/10], i%10);
        !            69:                                if ((fd = open(outname, 2)) >= 0) {
        !            70:                                        namefmt = 2;
        !            71:                                        break;
        !            72:                                }
        !            73:                        }
        !            74:                        if (errno==ENOENT) 
        !            75:                                if (++missing>5)
        !            76:                                        break;
        !            77:                }
        !            78:                if (fd>=0)
        !            79:                        break;
        !            80:        }
        !            81:        if (fd < 0)
        !            82:                return -1;
        !            83:        low[0] = high[1] = i+2;
        !            84:        return fd;
        !            85: }
        !            86: 
        !            87: /*
        !            88:  * open common control channel
        !            89:  */
        !            90: dkctlchan()
        !            91: {
        !            92:        register i ;
        !            93:        char dkctlfile[32];
        !            94: 
        !            95:        sprintf(dkctlfile, cnames[namefmt]);
        !            96:        i = open(dkctlfile, 2);
        !            97:        if (i < 0) { 
        !            98:                sprintf(dkctlfile, names[namefmt], alph[0], 1);
        !            99:                i = open(dkctlfile, 2);
        !           100:                if (i < 0) 
        !           101:                        return -1;
        !           102:        }
        !           103:        ioctl(i, DIOCNXCL, 0);
        !           104:        if (ioctl(i, FIOLOOKLD, 0) != rdk_ld) {
        !           105:                if (dkproto(i, dkp_ld) < 0) {
        !           106:                        fprintf(stderr, "can't push dkp_ld on %s\n", dkctlfile) ;
        !           107:                        close(i) ;
        !           108:                        return -1;
        !           109:                }
        !           110:                if (ioctl(i, FIOPUSHLD, &rdk_ld) < 0) {
        !           111:                        fprintf(stderr, "can't push rdk_ld on %s\n", dkctlfile) ;
        !           112:                        close(i) ;
        !           113:                        return -1;
        !           114:                }
        !           115:                if (ioctl(i, DIOCLHN, (char *)0) < 0) {
        !           116:                        fprintf(stderr, "can't be manager on %s\n", dkctlfile) ;
        !           117:                        close(i);
        !           118:                        return -1;
        !           119:                }
        !           120:        }
        !           121:        return i;
        !           122: }
        !           123: 
        !           124: /*
        !           125:  * find the file name corresponding to a given channel.
        !           126:  * assumes dkctlchan has been called.
        !           127:  * Used for incoming calls.
        !           128:  */
        !           129: 
        !           130: char *
        !           131: dkfilename(chan)
        !           132: {
        !           133:        static char name[64];
        !           134: 
        !           135:        if (namefmt==-1)
        !           136:                return(NULL);
        !           137:        sprintf(name, names[namefmt], alph[chan/10], chan%10);
        !           138:        return(name);
        !           139: }
        !           140: 
        !           141: /*
        !           142:  * Make sure that URP protocol is enabled on a datakit file.
        !           143:  */
        !           144: int
        !           145: dkproto(file, linedis)
        !           146: {
        !           147:        if (ioctl(file, KIOCISURP, (char *)0) < 0)
        !           148:                return(ioctl(file, FIOPUSHLD, &linedis));
        !           149:        ioctl(file, KIOCINIT, (char *)0);
        !           150:        return(0);
        !           151: }

unix.superglobalmegacorp.com

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