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