xref: /titanic_51/usr/src/cmd/bnu/uusched.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include	"uucp.h"
34 
35 #define USAGE	"[-xNUM] [-uNUM]"
36 #define MAXGRADE	52
37 
38 struct m {
39 	char	mach[15];
40 	char	jgrade[2*MAXGRADE+1];
41 } M[UUSTAT_TBL+2];
42 
43 short Uopt;
44 void cleanup(), exuucico();
45 
46 void logent(){}		/* to load ulockf.c */
47 
48 main(argc, argv, envp)
49 char *argv[];
50 char **envp;
51 {
52 	struct m *m, *machine();
53 	DIR *spooldir, *subdir, *gradedir;
54 	char f[256], g[256], fg[256], subf[256];
55 	int numgrade;
56 	char *gradelist, *gradeptr[MAXGRADE+1];
57 	short num, snumber;
58 	char lckname[MAXFULLNAME];
59 	struct limits limitval;
60 	int i, maxnumb;
61 	FILE *fp;
62 
63 	Uopt = 0;
64 	Env = envp;
65 
66 	(void) strcpy(Progname, "uusched");
67 	while ((i = getopt(argc, argv, "u:x:")) != EOF) {
68 		switch(i){
69 		case 'x':
70 			Debug = atoi(optarg);
71 			if (Debug <= 0) {
72 				fprintf(stderr,
73 				"WARNING: %s: invalid debug level %s ignored, using level 1\n",
74 				Progname, optarg);
75 				Debug = 1;
76 			}
77 #ifdef SMALL
78 			fprintf(stderr,
79 			"WARNING: uusched built with SMALL flag defined -- no debug info available\n");
80 #endif /* SMALL */
81 			break;
82 		case 'u':
83 			Uopt = atoi(optarg);
84 			if (Uopt <= 0) {
85 				fprintf(stderr,
86 				"WARNING: %s: invalid debug level %s ignored, using level 1\n",
87 				Progname, optarg);
88 				Uopt = 1;
89 			}
90 			break;
91 		default:
92 			(void) fprintf(stderr, "\tusage: %s %s\n",
93 			    Progname, USAGE);
94 			cleanup(1);
95 		}
96 	}
97 	if (argc != optind) {
98 		(void) fprintf(stderr, "\tusage: %s %s\n", Progname, USAGE);
99 		cleanup(1);
100 	}
101 
102 	DEBUG(9, "Progname (%s): STARTED\n", Progname);
103 	if (scanlimit("uusched", &limitval) == FAIL) {
104 	    DEBUG(1, "No limits for uusched in %s\n", LIMITS);
105 	    maxnumb = -1;
106 	} else {
107 	    maxnumb = limitval.totalmax;
108 	    if (maxnumb < 0) {
109 		DEBUG(4, "Non-positive limit for uusched in %s\n", LIMITS);
110 		DEBUG(1, "No limits for uusched\n%s", "");
111 	    } else {
112 		DEBUG(4, "Uusched limit %d -- ", maxnumb);
113 		i = cuantos(S_LOCKPRE, X_LOCKDIR);
114 		if (i >= maxnumb) {
115 			DEBUG(4, "found %d -- cleaning up\n", i);
116 			cleanup(0);
117 		}
118 		DEBUG(4, "continuing\n", maxnumb);
119 	    }
120 	}
121 
122 	if (chdir(SPOOL) != 0 || (spooldir = opendir(SPOOL)) == NULL)
123 		cleanup(101);		/* good old code 101 */
124 	while (gdirf(spooldir, f, SPOOL) == TRUE) {
125 	    subdir = opendir(f);
126 	    ASSERT(subdir != NULL, Ct_OPEN, f, errno);
127 	    while (gdirf(subdir, g, f) == TRUE) {
128 		(void) sprintf(fg, "%s/%s", f, g);
129 		gradedir = opendir(fg);
130 		ASSERT(gradedir != NULL, Ct_OPEN, g, errno);
131 		while (gnamef(gradedir, subf) == TRUE) {
132 		    if (subf[1] == '.') {
133 		        if (subf[0] == CMDPRE) {
134 			    /* Note - we can break now, since we
135 			     * have found a job grade with at least
136 			     * one C. file.
137 			    */
138 			    (void) strncat(machine(f)->jgrade, g, strlen(g));
139 			    break;
140 			}
141 		    }
142 		}
143 		closedir(gradedir);
144 	    }
145 	    closedir(subdir);
146 	}
147 
148 	/* Make sure the overflow entry is null since it may be incorrect */
149 	M[UUSTAT_TBL].mach[0] = NULLCHAR;
150 
151 	/* count the number of systems */
152 	for (num=0, m=M; m->mach[0] != '\0'; m++, num++) {
153 	    DEBUG(5, "machine: %s, ", M[num].mach);
154 	    DEBUG(5, "job grade list: %s\n", M[num].jgrade);
155 	}
156 	DEBUG(5, "Execute num=%d \n", num);
157 	while (num > 0) {
158 	    /*
159 	     * create lock file once we have work to do
160 	     * (but only if there is a job limit)
161 	     */
162 	    if (maxnumb > 0) {
163 	    	    for (i = 0; i < maxnumb; i++) {
164 			    (void) sprintf(lckname, "%s.%d", S_LOCK, i);
165 			    if (mklock(lckname) == SUCCESS)
166 			    	    break;
167 		    }
168 		    if (i == maxnumb) {
169 			    DEBUG(4, "found %d -- cleaning up\n", i);
170 			    cleanup(0);
171 		    }
172 	    }
173 	    snumber = (time((time_t *) 0) % num);  /* random num */
174 	    (void) strcpy(Rmtname, M[snumber].mach);
175 	    gradelist = M[snumber].jgrade;
176 	    DEBUG(5, "num=%d, ", num);
177 	    DEBUG(5, "snumber=%d, ", snumber);
178 	    DEBUG(5, "Rmtname=%s, ", Rmtname);
179 	    DEBUG(5, "job grade list= %s\n", gradelist);
180 
181 	    numgrade = getargs(gradelist, gradeptr, MAXGRADE);
182 	    for (i=0; i<numgrade; i++) {
183 		(void) sprintf(lckname, "%s.%s.%s", LOCKPRE, Rmtname, gradeptr[i]);
184 		if (cklock(lckname) != FAIL && callok(Rmtname) == 0) {
185 		    /* no lock file and status time ok */
186 		    DEBUG(5, "call exuucico(%s)\n", Rmtname);
187 		    exuucico(Rmtname);
188 		    break;
189 		}
190 		else {
191 		    /* job grade locked - look for the next one */
192 		    DEBUG(5, "job grade %s locked or inappropriate status\n",
193 			gradeptr[i]);
194 		}
195 	    }
196 
197 	    M[snumber] = M[num-1];
198 	    num--;
199 	}
200 	cleanup(0);
201 
202 	/* NOTREACHED */
203 }
204 
205 struct m	*
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
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
279 cleanup(code)
280 int	code;
281 {
282 	rmlock(CNULL);
283 	exit(code);
284 }
285