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 2005 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 #define USAGE "[-xNUM] [-uNUM]"
33 #define MAXGRADE 52
34
35 struct m {
36 char mach[15];
37 char jgrade[2*MAXGRADE+1];
38 } M[UUSTAT_TBL+2];
39
40 short Uopt;
41 void cleanup(), exuucico();
42
logent()43 void logent(){} /* to load ulockf.c */
44
45 int
main(argc,argv,envp)46 main(argc, argv, envp)
47 int argc;
48 char *argv[];
49 char **envp;
50 {
51 struct m *m, *machine();
52 DIR *spooldir, *subdir, *gradedir;
53 char f[256], g[256], fg[256], subf[256];
54 int numgrade;
55 char *gradelist, *gradeptr[MAXGRADE+1];
56 short num, snumber;
57 char lckname[MAXFULLNAME];
58 struct limits limitval;
59 int i, maxnumb;
60 FILE *fp;
61
62 Uopt = 0;
63 Env = envp;
64
65 (void) strcpy(Progname, "uusched");
66 while ((i = getopt(argc, argv, "u:x:")) != EOF) {
67 switch(i){
68 case 'x':
69 Debug = atoi(optarg);
70 if (Debug <= 0) {
71 fprintf(stderr,
72 "WARNING: %s: invalid debug level %s ignored, using level 1\n",
73 Progname, optarg);
74 Debug = 1;
75 }
76 #ifdef SMALL
77 fprintf(stderr,
78 "WARNING: uusched built with SMALL flag defined -- no debug info available\n");
79 #endif /* SMALL */
80 break;
81 case 'u':
82 Uopt = atoi(optarg);
83 if (Uopt <= 0) {
84 fprintf(stderr,
85 "WARNING: %s: invalid debug level %s ignored, using level 1\n",
86 Progname, optarg);
87 Uopt = 1;
88 }
89 break;
90 default:
91 (void) fprintf(stderr, "\tusage: %s %s\n",
92 Progname, USAGE);
93 cleanup(1);
94 }
95 }
96 if (argc != optind) {
97 (void) fprintf(stderr, "\tusage: %s %s\n", Progname, USAGE);
98 cleanup(1);
99 }
100
101 DEBUG(9, "Progname (%s): STARTED\n", Progname);
102 if (scanlimit("uusched", &limitval) == FAIL) {
103 DEBUG(1, "No limits for uusched in %s\n", LIMITS);
104 maxnumb = -1;
105 } else {
106 maxnumb = limitval.totalmax;
107 if (maxnumb < 0) {
108 DEBUG(4, "Non-positive limit for uusched in %s\n", LIMITS);
109 DEBUG(1, "No limits for uusched\n%s", "");
110 } else {
111 DEBUG(4, "Uusched limit %d -- ", maxnumb);
112 i = cuantos(S_LOCKPRE, X_LOCKDIR);
113 if (i >= maxnumb) {
114 DEBUG(4, "found %d -- cleaning up\n", i);
115 cleanup(0);
116 }
117 DEBUG(4, "continuing\n", maxnumb);
118 }
119 }
120
121 if (chdir(SPOOL) != 0 || (spooldir = opendir(SPOOL)) == NULL)
122 cleanup(101); /* good old code 101 */
123 while (gdirf(spooldir, f, SPOOL) == TRUE) {
124 subdir = opendir(f);
125 ASSERT(subdir != NULL, Ct_OPEN, f, errno);
126 while (gdirf(subdir, g, f) == TRUE) {
127 (void) sprintf(fg, "%s/%s", f, g);
128 gradedir = opendir(fg);
129 ASSERT(gradedir != NULL, Ct_OPEN, g, errno);
130 while (gnamef(gradedir, subf) == TRUE) {
131 if (subf[1] == '.') {
132 if (subf[0] == CMDPRE) {
133 /* Note - we can break now, since we
134 * have found a job grade with at least
135 * one C. file.
136 */
137 (void) strncat(machine(f)->jgrade, g, strlen(g));
138 break;
139 }
140 }
141 }
142 closedir(gradedir);
143 }
144 closedir(subdir);
145 }
146
147 /* Make sure the overflow entry is null since it may be incorrect */
148 M[UUSTAT_TBL].mach[0] = NULLCHAR;
149
150 /* count the number of systems */
151 for (num=0, m=M; m->mach[0] != '\0'; m++, num++) {
152 DEBUG(5, "machine: %s, ", M[num].mach);
153 DEBUG(5, "job grade list: %s\n", M[num].jgrade);
154 }
155 DEBUG(5, "Execute num=%d \n", num);
156 while (num > 0) {
157 /*
158 * create lock file once we have work to do
159 * (but only if there is a job limit)
160 */
161 if (maxnumb > 0) {
162 for (i = 0; i < maxnumb; i++) {
163 (void) sprintf(lckname, "%s.%d", S_LOCK, i);
164 if (mklock(lckname) == SUCCESS)
165 break;
166 }
167 if (i == maxnumb) {
168 DEBUG(4, "found %d -- cleaning up\n", i);
169 cleanup(0);
170 }
171 }
172 snumber = (time((time_t *) 0) % num); /* random num */
173 (void) strcpy(Rmtname, M[snumber].mach);
174 gradelist = M[snumber].jgrade;
175 DEBUG(5, "num=%d, ", num);
176 DEBUG(5, "snumber=%d, ", snumber);
177 DEBUG(5, "Rmtname=%s, ", Rmtname);
178 DEBUG(5, "job grade list= %s\n", gradelist);
179
180 numgrade = getargs(gradelist, gradeptr, MAXGRADE);
181 for (i=0; i<numgrade; i++) {
182 (void) sprintf(lckname, "%s.%s.%s", LOCKPRE, Rmtname, gradeptr[i]);
183 if (cklock(lckname) != FAIL && callok(Rmtname) == 0) {
184 /* no lock file and status time ok */
185 DEBUG(5, "call exuucico(%s)\n", Rmtname);
186 exuucico(Rmtname);
187 break;
188 }
189 else {
190 /* job grade locked - look for the next one */
191 DEBUG(5, "job grade %s locked or inappropriate status\n",
192 gradeptr[i]);
193 }
194 }
195
196 M[snumber] = M[num-1];
197 num--;
198 }
199 cleanup(0);
200
201 /* NOTREACHED */
202 return (0);
203 }
204
205 struct m *
machine(name)206 machine(name)
207 char *name;
208 {
209 struct m *m;
210 size_t namelen;
211
212 namelen = strlen(name);
213 DEBUG(9, "machine(%s) called\n", name);
214 for (m = M; m->mach[0] != '\0'; m++)
215 /* match on overlap? */
216 if (EQUALSN(name, m->mach, MAXBASENAME)) {
217 /* check for job grade */
218 if (m->jgrade[0] != NULLCHAR)
219 (void) strncat(m->jgrade, " ", 1);
220
221 /* use longest name */
222 if (namelen > strlen(m->mach))
223 (void) strcpy(m->mach, name);
224 return(m);
225 }
226
227 /*
228 * The table is set up with 2 extra entries
229 * When we go over by one, output error to errors log
230 * When more than one over, just reuse the previous entry
231 */
232 if (m-M >= UUSTAT_TBL) {
233 if (m-M == UUSTAT_TBL) {
234 errent("MACHINE TABLE FULL", "", UUSTAT_TBL,
235 __FILE__, __LINE__);
236 }
237 else
238 /* use the last entry - overwrite it */
239 m = &M[UUSTAT_TBL];
240 }
241
242 (void) strcpy(m->mach, name);
243 m->jgrade[0] = NULLCHAR;
244 return(m);
245 }
246
247 void
exuucico(name)248 exuucico(name)
249 char *name;
250 {
251 char cmd[BUFSIZ];
252 int status;
253 pid_t pid, ret;
254 char uopt[5];
255 char sopt[BUFSIZ];
256
257 (void) sprintf(sopt, "-s%s", name);
258 if (Uopt)
259 (void) sprintf(uopt, "-x%.1d", Uopt);
260
261 if ((pid = vfork()) == 0) {
262 if (Uopt)
263 (void) execle(UUCICO, "UUCICO", "-r1", uopt, sopt, (char *) 0, Env);
264 else
265 (void) execle(UUCICO, "UUCICO", "-r1", sopt, (char *) 0, Env);
266
267 cleanup(100);
268 }
269 while ((ret = wait(&status)) != pid)
270 if (ret == -1 && errno != EINTR)
271 break;
272
273 DEBUG(3, "ret=%ld, ", (ret == pid ? (long) status : (long) ret));
274 return;
275 }
276
277
278 void
cleanup(code)279 cleanup(code)
280 int code;
281 {
282 rmlock(CNULL);
283 exit(code);
284 }
285