1: /* netq - print the netq send queue */ 2: /* netq [-a] [mach] */ 3: /* must be setuid root */ 4: 5: # include "defs.h" 6: # define STSIZE 150 7: 8: static FILE *df; 9: static char jname[16], printlong; 10: static struct table { 11: char name[16]; 12: long filesize; 13: } stack[STSIZE], temp; 14: static int stptr = 0; 15: static char mach,visit[26]; 16: static char netcmd1[] = NETCMD1; 17: static int hisuid,sumj; 18: static long sumb; 19: static struct stat statbuf; 20: static struct direct dirbuf; 21: 22: char _sobuf[BUFSIZ]; 23: main(argc,argv) 24: char **argv; { 25: int i; 26: setbuf(stdout,_sobuf); 27: hisuid = getuid(); 28: hisuid = uidmask(hisuid); 29: if(stat(netcmd,&statbuf) >= 0) 30: if((statbuf.st_mode & 07) == 0){ 31: printf("Network is down\n"); 32: exit(EX_UNAVAILABLE); 33: } 34: else if(stat(netcmd1,&statbuf) >= 0) 35: if((statbuf.st_mode & 07) == 0){ 36: printf("Network is down\n"); 37: exit(EX_UNAVAILABLE); 38: } 39: while(argc > 1){ 40: switch(argv[1][0]){ 41: case '-': printlong++; break; 42: default: mach = lookup(argv[1]); 43: if(mach > 0 && machtype[mach-'a'] == 0)mach = 0; 44: break; 45: } 46: argc--, argv++; 47: } 48: if(mach){ 49: mach = gothru(local,mach); /* list to directly conn. machine */ 50: if(mach == 0){ 51: fprintf(stderr,"That machine not directly connected.\n"); 52: exit(EX_USAGE); 53: } 54: senddir[strlen(senddir)-1] = mach; 55: if(chdir(senddir) < 0){ 56: perror(senddir); 57: exit(EX_OSFILE); 58: } 59: pdir(senddir); 60: } 61: else for(i = 'a'; i <= 'z'; i++) 62: if((mach = gothru(local,i)) && !visit[mach - 'a']){ 63: visit[mach - 'a'] = 1; 64: senddir[strlen(senddir)-1] = mach; 65: if(chdir(senddir) < 0)continue; 66: pdir(senddir); 67: printf("---\n"); 68: } 69: fflush(stdout); 70: } 71: static pdir(str) 72: char *str; { 73: int i; 74: char more = 0, *cp; 75: int (*compar)(); 76: df = fopen(str,"r"); 77: if(df == NULL){ 78: perror(str); 79: exit(EX_OSFILE); 80: } 81: stptr = 0; 82: while(fread(&dirbuf,1,sizeof dirbuf,df)==sizeof dirbuf){ 83: if(dirbuf.d_ino == 0 84: || dirbuf.d_name[0] != 'c' 85: || dirbuf.d_name[1] != 'f' 86: || stat(dirbuf.d_name,&statbuf) < 0) 87: continue; 88: if(mach != dirbuf.d_name[2])continue; 89: dirbuf.d_name[0] = 'd'; 90: if(stat(dirbuf.d_name,&statbuf) < 0)continue; 91: 92: if(!insert(dirbuf.d_name,getsize(&statbuf))){ 93: more++; 94: break; 95: } 96: } 97: if(stptr == 0){ 98: printf("Network queue to/thru %s is empty.\n",longname(mach)); 99: return; 100: } 101: cp = (char *)&(stack[0].name[0]); 102: sort(cp,stptr,sizeof temp,compar); 103: printf("Network queue to/thru %s:\n",longname(mach)); 104: printf( 105: "From To Len Code Time Command\n"); 106: for(i = 0; i < stptr; i++){ /* screen size */ 107: strcpy(jname,stack[i].name); 108: jname[0] = 'd'; 109: if(stat(jname,&statbuf) < 0) 110: continue; 111: if(printlong || guid(statbuf.st_uid,statbuf.st_gid) == hisuid) 112: process(); 113: else summarize(i); 114: } 115: printsum(); 116: if(more)printf(" ... more ...\n"); 117: } 118: summarize(i){ 119: sumb += stack[i].filesize; 120: sumj++; 121: } 122: printsum(){ 123: if(sumj != 0){ 124: printf("%d request%s, %ld bytes\n", 125: sumj,(sumj > 1 ? "s" : ""),sumb); 126: sumj = 0; 127: sumb = 0L; 128: } 129: } 130: process(){ 131: static struct header hd; 132: FILE *look; 133: char *cp; 134: 135: printsum(); 136: look = fopen(jname,"r"); 137: if(look == NULL) 138: return; 139: readhdfd(&hd,look); 140: fclose(look); 141: if(hd.hd_snfrom[0] == 0)strcat(hd.hd_addrfrom,"Internal"); 142: expandcc(hd.hd_sttyname); 143: cp = ctime(&statbuf.st_mtime); 144: cp[strlen(cp)-9] = 0; 145: jname[3] = jname[2]; 146: printf("%-10s %-10s %5ld %s %s %-.27s\n", 147: hd.hd_addrfrom,hd.hd_addrto,getsize(&statbuf), 148: jname+3,cp+4,hd.hd_scmdvirt); 149: } 150: readhdfd(phd,fd) 151: register struct header *phd; 152: FILE *fd; 153: { 154: char sbuf[BUFSIZ], parmlist[PARMLIST]; 155: char *s, cflag; 156: int c, i; 157: phd->hd_code = phd->hd_mchto = phd->hd_snto[0] = phd->hd_sinfile[0] = 0; 158: phd->hd_soutfile[0] = phd->hd_srespfile[0] = phd->hd_snfrom[0] = 0; 159: phd->hd_scmdact[0] = 0; 160: for(i=0;i<20;i++)phd->hd_sttyname[i] = 0; 161: 162: phd->hd_code = ngetc(fd); 163: if(phd->hd_code == 0)return; 164: phd->hd_mchto = ngetc(fd); 165: phd->hd_mchfrom = ngetc(fd); /* from machine */ 166: phd->hd_vmajor = ngetc(fd); 167: phd->hd_vminor = ngetc(fd); 168: ngets(phd->hd_snto,NS,fd); 169: ngets(phd->hd_spasswd,20,fd); /* passwd */ 170: ngets(phd->hd_sinfile,FNS,fd); 171: ngets(phd->hd_soutfile,FNS,fd); 172: ngets(phd->hd_srespfile,FNS,fd); 173: ngets(phd->hd_snfrom,NS,fd); 174: ngets(phd->hd_sttyname,20,fd); 175: cflag = ngetc(fd); 176: ngets(sbuf,BUFSIZ,fd); /* lttytime */ 177: ngets(parmlist,PARMLIST,fd); /* jobno */ 178: parseparmlist(parmlist); 179: ngets(sbuf,BUFSIZ,fd); /* timesent */ 180: s = phd->hd_scmdact; 181: while((c = getc(fd)) != EOF && c != '\n'){ 182: if(c == '\\')c = getc(fd); 183: *s++ = c; 184: } 185: *s = 0; 186: s = phd->hd_scmdvirt; 187: while((c = getc(fd)) != EOF && c != '\n'){ 188: if(c == '\\')c = getc(fd); 189: *s++ = c; 190: } 191: *s = 0; 192: if(phd->hd_scmdvirt[0] == 0)strcpy(phd->hd_scmdvirt,phd->hd_scmdact); 193: sprintf(phd->hd_addrfrom,"%c:%s",phd->hd_mchfrom,phd->hd_snfrom); 194: sprintf(phd->hd_addrto ,"%c:%s",phd->hd_mchto ,phd->hd_snto ); 195: } 196: ngetc(fd) 197: FILE *fd; 198: { 199: char b[3]; 200: if(feof(fd))return(0); 201: if(fread(b,1,3,fd) != 3) return(0); 202: return(b[0]); 203: } 204: /* read a string s of max length maxlen out of queue file */ 205: ngets(s,maxlen,fd) 206: int maxlen; 207: char *s; 208: FILE *fd; 209: { 210: int i; 211: if(feof(fd))return; 212: for(;;){ 213: i = getc(fd); 214: if(i == EOF){ 215: *s = 0; 216: return; 217: } 218: *s = i; 219: if(*s == '\\')*s = getc(fd); 220: if(*s == ' ')break; 221: if(maxlen-- > 0)s++; 222: } 223: *s = 0; 224: getc(fd); 225: } 226: insert(f,t) 227: char *f; 228: long t; { 229: strcpy(stack[stptr].name,f); 230: stack[stptr++].filesize = t; 231: return(stptr <= STSIZE); 232: } 233: compar(a,b) 234: register struct table *a,*b; { 235: if(a->filesize < b->filesize)return(-1); 236: if(a->filesize > b->filesize)return(1); 237: return(0); 238: } 239: sort(){ /* use this cause qsort doesn't work */ 240: register int i,j; 241: for(i=0; i< stptr-1; i++) 242: for(j=i+1;j<stptr;j++) 243: if(compar(&stack[i],&stack[j]) > 0) 244: swap(&stack[i],&stack[j]); 245: } 246: swap(a,b) 247: register struct table *a, *b; { 248: char str[16]; 249: long t; 250: strcpy(str,a->name); 251: t = a->filesize; 252: strcpy(a->name,b->name); 253: a->filesize = b->filesize; 254: strcpy(b->name,str); 255: b->filesize = t; 256: }