1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 1997 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 #include "uucp.h" 31 32 static struct { 33 char sys[NAMESIZE]; 34 int job; 35 int subjob; 36 } syslst[30]; /* no more than 30 systems per job */ 37 38 static int nsys = 0; 39 static int sysseq(); 40 41 /* generate file name 42 * pre -> file prefix 43 * sys -> system name 44 * grade -> service grade 45 * file -> buffer to return filename must be of size MAXBASENAME+1 46 * return: 47 * none 48 */ 49 void 50 gename(pre, sys, grade, file) 51 char pre, *sys, grade, *file; 52 { 53 int n; 54 55 DEBUG(9, "gename(%c, ", pre); 56 DEBUG(9, "%s, ", sys); 57 DEBUG(9, "%c)\n", grade); 58 if (*sys == '\0') { 59 sys = Myname; 60 DEBUG(9, "null sys -> %s\n", sys); 61 } 62 n = sysseq(sys); 63 if (pre == CMDPRE || pre == XQTPRE) { 64 (void) sprintf(file, "%c.%.*s%c%.4x", 65 pre, SYSNSIZE, sys, grade, syslst[n].job); 66 } else 67 (void) sprintf(file, "%c.%.5s%.4x%.3x", 68 pre, sys, syslst[n].job & 0xffff, 69 ++syslst[n].subjob & 0xfff); 70 DEBUG(4, "file - %s\n", file); 71 return; 72 } 73 74 75 #define SLOCKTIME 10 76 #define SLOCKTRIES 25 77 #define SEQLEN 4 78 79 /* 80 * get next sequence number 81 * returns: 82 * number between 1 and 0xffff 83 * 84 * sequence number 0 is reserved for polling 85 */ 86 static int 87 getseq(sys) 88 char *sys; 89 { 90 register FILE *fp; 91 register int i; 92 unsigned int n; 93 time_t seed; 94 char seqlock[MAXFULLNAME], seqfile[MAXFULLNAME]; 95 96 ASSERT(nsys < sizeof (syslst)/ sizeof (syslst[0]), 97 "SYSLST OVERFLOW", "", sizeof (syslst)); 98 99 (void) time(&seed); /* crank up the sequence initializer */ 100 srand((unsigned)seed); 101 102 (void) sprintf(seqlock, "%s%s", SEQLOCK, sys); 103 BASENAME(seqlock, '/')[MAXBASENAME] = '\0'; 104 for (i = 1; i < SLOCKTRIES; i++) { 105 if ( mklock(seqlock) == SUCCESS ) 106 break; 107 sleep(5); 108 } 109 110 ASSERT(i < SLOCKTRIES, Ct_LOCK, seqlock, 0); 111 112 (void) sprintf(seqfile, "%s/%s", SEQDIR, sys); 113 if ((fp = fopen(seqfile, "r")) != NULL) { 114 /* read sequence number file */ 115 if (fscanf(fp, "%4x", &n) != 1) { 116 n = rand(); 117 clearerr(fp); 118 } 119 fp = freopen(seqfile, "w", fp); 120 ASSERT(fp != NULL, Ct_OPEN, seqfile, errno); 121 (void) chmod(seqfile, PUB_FILEMODE); 122 } else { 123 /* can not read file - create a new one */ 124 ASSERT((fp = fopen(seqfile, "w")) != NULL, 125 Ct_CREATE, seqfile, errno); 126 (void) chmod(seqfile, PUB_FILEMODE); 127 n = rand(); 128 } 129 130 n++; 131 n &= 0xffff; /* 4 byte sequence numbers */ 132 (void) fprintf(fp, "%.4x\n", n); 133 ASSERT(ferror(fp) == 0, Ct_WRITE, seqfile, errno); 134 (void) fclose(fp); 135 ASSERT(ferror(fp) == 0, Ct_CLOSE, seqfile, errno); 136 rmlock(seqlock); 137 DEBUG(6, "%s seq ", sys); DEBUG(6, "now %x\n", n); 138 (void) strcpy(syslst[nsys].sys, sys); 139 syslst[nsys].job = n; 140 syslst[nsys].subjob = rand() & 0xfff; /* random initial value */ 141 return(nsys++); 142 } 143 144 /* 145 * initSeq() exists because it is sometimes important to forget any 146 * cached work files. for example, when processing a bunch of spooled X. 147 * files, we must not re-use any C. files used to send back output. 148 */ 149 150 void 151 initSeq() 152 { 153 nsys = 0; 154 return; 155 } 156 157 /* 158 * retseq() is used to get the sequence number of a job 159 * for functions outside of this file. 160 * 161 * returns 162 * 163 * the sequence number of the job for the system. 164 */ 165 166 int 167 retseq(sys) 168 char *sys; 169 { 170 int i; 171 172 for (i = 0; i < nsys; i++) 173 if (EQUALSN(syslst[i].sys, sys, MAXBASENAME)) 174 break; 175 176 return(syslst[i].job); 177 } 178 179 static int 180 sysseq(sys) 181 char *sys; 182 { 183 int i; 184 185 for (i = 0; i < nsys; i++) 186 if (strncmp(syslst[i].sys, sys, MAXBASENAME) == SAME) 187 return(i); 188 189 return(getseq(sys)); 190 } 191