Annotation of researchv10no/cmd/worm/scsi/jukebox.c, revision 1.1

1.1     ! root        1: #define        _POSIX_SOURCE
        !             2: #include       <stddef.h>
        !             3: #include       <stdio.h>
        !             4: #include       <stdlib.h>
        !             5: #include       <unistd.h>
        !             6: #include       <string.h>
        !             7: #include       "scsi.h"
        !             8: #include       "jukeface.h"
        !             9: #include       "jukebox.h"
        !            10: #include       "arg.h"
        !            11: 
        !            12: Jukebox jukebox;
        !            13: char *argv0;
        !            14: static void prstatus(void);
        !            15: static void usage(void);
        !            16: static void errexit(char *errbuf);
        !            17: static void unload(int);
        !            18: static eject(Jukebox *j, char *vol_id, char *errbuf);
        !            19: 
        !            20: main(int argc, char *argv[])
        !            21: {
        !            22:        int c;
        !            23:        int aflag = 0, eflag = 0, mflag = 0, pflag = 0;
        !            24:        int rflag = 0, sflag = 0, uflag = 0, Uflag = 0;
        !            25:        int reset = 0;
        !            26:        int warm = 0;
        !            27:        long secs = 3600L*24*183;       /* half a year is enough */
        !            28:        char *vol_id;
        !            29:        char errbuf[1024];
        !            30: 
        !            31:        setvbuf(stderr, (char *)0, _IOLBF, 4096);
        !            32:        setvbuf(stdout, (char *)0, _IOLBF, 4096);
        !            33:        ARGBEGIN{
        !            34:        case 'a':       aflag = 1; break;
        !            35:        case 'e':       eflag = 1; break;
        !            36:        case 'm':       mflag = 1; break;
        !            37:        case 'p':       pflag = 1; break;
        !            38:        case 'r':       rflag = 1; break;
        !            39:        case 'R':       reset = 1; break;
        !            40:        case 's':       sflag = 1; break;
        !            41:        case 'u':       uflag = 1; break;
        !            42:        case 'U':       Uflag = 1; break;
        !            43:        case 'w':       secs = atol(ARGF()); break;
        !            44:        case 'W':       warm = 1; break;
        !            45:        default:        usage(); break;
        !            46:        }ARGEND
        !            47:        s_id = 2;
        !            48:        j_init(&jukebox);
        !            49:        if(j_config(&jukebox, errbuf) < 0)
        !            50:                goto scram;
        !            51:        if(!aflag&&!eflag&&!mflag&&!pflag&&!rflag&&!uflag&&!Uflag)
        !            52:                sflag = 1;
        !            53:        vol_id = argc? argv[0] : 0;
        !            54:        if(reset)
        !            55:                soft_reset();
        !            56:        if(uflag || Uflag)
        !            57:                unload(Uflag);
        !            58:        if(eflag){
        !            59:                if(vol_id == 0){
        !            60:                        strcpy(errbuf, "-e needs a vol_id");
        !            61:                        goto scram;
        !            62:                }
        !            63:                if(eject(&jukebox, vol_id, errbuf))
        !            64:                        goto scram;
        !            65:        }
        !            66:        if(rflag){
        !            67:                if(j_cold(&jukebox, vol_id? vol_id : "u", errbuf) < 0)
        !            68:                        goto scram;
        !            69:        }
        !            70:        if(warm){
        !            71:                if(j_warm(&jukebox, errbuf) < 0)
        !            72:                        goto scram;
        !            73:        }
        !            74:        if(aflag){
        !            75:                if(vol_id == 0){
        !            76:                        strcpy(errbuf, "-a needs a vol_id");
        !            77:                        goto scram;
        !            78:                }
        !            79:                if(allocate(&jukebox, vol_id, errbuf))
        !            80:                        goto scram;
        !            81:        }
        !            82:        if(mflag){
        !            83:                if((c = j_load(&jukebox, vol_id, errbuf, secs)) < 0)
        !            84:                        goto scram;
        !            85:                if(j_start(c, errbuf) < 0)
        !            86:                        fprintf(stderr, "jukebox: %s\n", errbuf);
        !            87:                if(j_stop(c, errbuf) < 0)
        !            88:                        fprintf(stderr, "jukebox: %s\n", errbuf);
        !            89:                printf("%d\n", c);
        !            90:        }
        !            91:        if(sflag)
        !            92:                prstatus();
        !            93:        if(pflag){
        !            94:                if(j_rdshelves(&jukebox, errbuf) < 0)
        !            95:                        goto scram;
        !            96:                for(c = 0; c < jukebox.nshelves; c++)
        !            97:                        if(jukebox.names[c])
        !            98:                                printf("%d: %s\n", c, jukebox.names[c]);
        !            99:        }
        !           100:        if(j_wrshelf)
        !           101:                if(j_wrshelves(&jukebox, errbuf))
        !           102:                        errexit(errbuf);
        !           103:        exit(0);
        !           104: scram:
        !           105:        if(j_wrshelf)
        !           106:                j_wrshelves(&jukebox, errbuf);
        !           107:        errexit(errbuf);
        !           108:        return(1);                      /* shut up compiler */
        !           109: }
        !           110: 
        !           111: static void
        !           112: usage(void)
        !           113: {
        !           114:        fprintf(stderr, "Usage: jukebox [-aemprsuU] [-w secs] [vol_id\n");
        !           115:        exit(1);
        !           116: }
        !           117: 
        !           118: static void
        !           119: errexit(char *errbuf)
        !           120: {
        !           121:        fprintf(stderr, "jukebox: %s\n", errbuf);
        !           122:        exit(1);
        !           123: }
        !           124: 
        !           125: static void
        !           126: prstatus(void)
        !           127: {
        !           128:        struct lun *lun;
        !           129:        int c;
        !           130:        char errbuf[1024];
        !           131: 
        !           132:        if(j_drstatus(&jukebox, errbuf)){
        !           133:                fprintf(stderr, "jukebox: %s\n", errbuf);
        !           134:                exit(1);
        !           135:        }
        !           136:        for(c = 0, lun = jukebox.luns; c < jukebox.nluns; c++, lun++){
        !           137:                if(!lun->occupied)
        !           138:                        continue;
        !           139:                printf("lun %d(%s,%sline): ", c, lun->desc, lun->spunup?"on":"off");
        !           140:                if(lun->shelf >= 0){
        !           141:                        printf("%s%c", j_name(&jukebox, lun->shelf), "ab"[lun->side]);
        !           142:                } else
        !           143:                        printf("<unknown shelf??>");
        !           144:                printf("\n");
        !           145:        }
        !           146: }
        !           147: 
        !           148: static void
        !           149: unload(int force)
        !           150: {
        !           151:        struct lun *l;
        !           152:        int c;
        !           153:        char errbuf[1024];
        !           154: 
        !           155:        if(j_drstatus(&jukebox, errbuf)){
        !           156:                fprintf(stderr, "jukebox: %s\n", errbuf);
        !           157:                exit(1);
        !           158:        }
        !           159:        for(c = 0, l = jukebox.luns; c < 8; c++, l++){
        !           160:                if(l->occupied && (force || !l->spunup))
        !           161:                        if(j_dr_to_sh(c, -1, SIDEA, errbuf))
        !           162:                                fprintf(stderr, "jukebox: %s\n", errbuf);
        !           163:        }
        !           164: }
        !           165: 
        !           166: static
        !           167: eject(Jukebox *j, char *vol_id, char *errbuf)
        !           168: {
        !           169:        int sh;
        !           170:        int dr;
        !           171: 
        !           172:        if(j_rdshelves(j, errbuf))
        !           173:                return(-1);
        !           174:        if(j_drstatus(j, errbuf)){
        !           175:                fprintf(stderr, "jukebox: %s\n", errbuf);
        !           176:                exit(1);
        !           177:        }
        !           178:        sh = j_shelfof(j, vol_id);
        !           179:        if(sh < 0){
        !           180:                sprintf(errbuf, "no vol_id %s", vol_id);
        !           181:                return(-1);
        !           182:        }
        !           183:        j_wrshelf = 1;
        !           184:        if((dr = j_driveof(j, vol_id)) >= 0){
        !           185:                j_wrshelf = 1;
        !           186:                j->shelves[sh] = 0;
        !           187:                j->names[sh] = 0;
        !           188:                return(j_eject(dr, errbuf));
        !           189:        }
        !           190:        dr = j->nluns-1;
        !           191:        if(j_sh_to_dr(sh, SIDEA, dr, errbuf) < 0)
        !           192:                return(-1);
        !           193:        if(j_eject(dr, errbuf))
        !           194:                return(-1);
        !           195:        j_wrshelf = 1;
        !           196:        j->shelves[sh] = 0;
        !           197:        j->names[sh] = 0;
        !           198:        return(0);
        !           199: }

unix.superglobalmegacorp.com

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