xref: /illumos-gate/usr/src/cmd/bnu/uusched.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*462be471Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include	"uucp.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #define USAGE	"[-xNUM] [-uNUM]"
337c478bd9Sstevel@tonic-gate #define MAXGRADE	52
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate struct m {
367c478bd9Sstevel@tonic-gate 	char	mach[15];
377c478bd9Sstevel@tonic-gate 	char	jgrade[2*MAXGRADE+1];
387c478bd9Sstevel@tonic-gate } M[UUSTAT_TBL+2];
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate short Uopt;
417c478bd9Sstevel@tonic-gate void cleanup(), exuucico();
427c478bd9Sstevel@tonic-gate 
logent()437c478bd9Sstevel@tonic-gate void logent(){}		/* to load ulockf.c */
447c478bd9Sstevel@tonic-gate 
45*462be471Sceastha int
main(argc,argv,envp)467c478bd9Sstevel@tonic-gate main(argc, argv, envp)
47*462be471Sceastha int argc;
487c478bd9Sstevel@tonic-gate char *argv[];
497c478bd9Sstevel@tonic-gate char **envp;
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	struct m *m, *machine();
527c478bd9Sstevel@tonic-gate 	DIR *spooldir, *subdir, *gradedir;
537c478bd9Sstevel@tonic-gate 	char f[256], g[256], fg[256], subf[256];
547c478bd9Sstevel@tonic-gate 	int numgrade;
557c478bd9Sstevel@tonic-gate 	char *gradelist, *gradeptr[MAXGRADE+1];
567c478bd9Sstevel@tonic-gate 	short num, snumber;
577c478bd9Sstevel@tonic-gate 	char lckname[MAXFULLNAME];
587c478bd9Sstevel@tonic-gate 	struct limits limitval;
597c478bd9Sstevel@tonic-gate 	int i, maxnumb;
607c478bd9Sstevel@tonic-gate 	FILE *fp;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	Uopt = 0;
637c478bd9Sstevel@tonic-gate 	Env = envp;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	(void) strcpy(Progname, "uusched");
667c478bd9Sstevel@tonic-gate 	while ((i = getopt(argc, argv, "u:x:")) != EOF) {
677c478bd9Sstevel@tonic-gate 		switch(i){
687c478bd9Sstevel@tonic-gate 		case 'x':
697c478bd9Sstevel@tonic-gate 			Debug = atoi(optarg);
707c478bd9Sstevel@tonic-gate 			if (Debug <= 0) {
717c478bd9Sstevel@tonic-gate 				fprintf(stderr,
727c478bd9Sstevel@tonic-gate 				"WARNING: %s: invalid debug level %s ignored, using level 1\n",
737c478bd9Sstevel@tonic-gate 				Progname, optarg);
747c478bd9Sstevel@tonic-gate 				Debug = 1;
757c478bd9Sstevel@tonic-gate 			}
767c478bd9Sstevel@tonic-gate #ifdef SMALL
777c478bd9Sstevel@tonic-gate 			fprintf(stderr,
787c478bd9Sstevel@tonic-gate 			"WARNING: uusched built with SMALL flag defined -- no debug info available\n");
797c478bd9Sstevel@tonic-gate #endif /* SMALL */
807c478bd9Sstevel@tonic-gate 			break;
817c478bd9Sstevel@tonic-gate 		case 'u':
827c478bd9Sstevel@tonic-gate 			Uopt = atoi(optarg);
837c478bd9Sstevel@tonic-gate 			if (Uopt <= 0) {
847c478bd9Sstevel@tonic-gate 				fprintf(stderr,
857c478bd9Sstevel@tonic-gate 				"WARNING: %s: invalid debug level %s ignored, using level 1\n",
867c478bd9Sstevel@tonic-gate 				Progname, optarg);
877c478bd9Sstevel@tonic-gate 				Uopt = 1;
887c478bd9Sstevel@tonic-gate 			}
897c478bd9Sstevel@tonic-gate 			break;
907c478bd9Sstevel@tonic-gate 		default:
917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\tusage: %s %s\n",
927c478bd9Sstevel@tonic-gate 			    Progname, USAGE);
937c478bd9Sstevel@tonic-gate 			cleanup(1);
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 	if (argc != optind) {
977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\tusage: %s %s\n", Progname, USAGE);
987c478bd9Sstevel@tonic-gate 		cleanup(1);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	DEBUG(9, "Progname (%s): STARTED\n", Progname);
1027c478bd9Sstevel@tonic-gate 	if (scanlimit("uusched", &limitval) == FAIL) {
1037c478bd9Sstevel@tonic-gate 	    DEBUG(1, "No limits for uusched in %s\n", LIMITS);
1047c478bd9Sstevel@tonic-gate 	    maxnumb = -1;
1057c478bd9Sstevel@tonic-gate 	} else {
1067c478bd9Sstevel@tonic-gate 	    maxnumb = limitval.totalmax;
1077c478bd9Sstevel@tonic-gate 	    if (maxnumb < 0) {
1087c478bd9Sstevel@tonic-gate 		DEBUG(4, "Non-positive limit for uusched in %s\n", LIMITS);
1097c478bd9Sstevel@tonic-gate 		DEBUG(1, "No limits for uusched\n%s", "");
1107c478bd9Sstevel@tonic-gate 	    } else {
1117c478bd9Sstevel@tonic-gate 		DEBUG(4, "Uusched limit %d -- ", maxnumb);
1127c478bd9Sstevel@tonic-gate 		i = cuantos(S_LOCKPRE, X_LOCKDIR);
1137c478bd9Sstevel@tonic-gate 		if (i >= maxnumb) {
1147c478bd9Sstevel@tonic-gate 			DEBUG(4, "found %d -- cleaning up\n", i);
1157c478bd9Sstevel@tonic-gate 			cleanup(0);
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 		DEBUG(4, "continuing\n", maxnumb);
1187c478bd9Sstevel@tonic-gate 	    }
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (chdir(SPOOL) != 0 || (spooldir = opendir(SPOOL)) == NULL)
1227c478bd9Sstevel@tonic-gate 		cleanup(101);		/* good old code 101 */
1237c478bd9Sstevel@tonic-gate 	while (gdirf(spooldir, f, SPOOL) == TRUE) {
1247c478bd9Sstevel@tonic-gate 	    subdir = opendir(f);
1257c478bd9Sstevel@tonic-gate 	    ASSERT(subdir != NULL, Ct_OPEN, f, errno);
1267c478bd9Sstevel@tonic-gate 	    while (gdirf(subdir, g, f) == TRUE) {
1277c478bd9Sstevel@tonic-gate 		(void) sprintf(fg, "%s/%s", f, g);
1287c478bd9Sstevel@tonic-gate 		gradedir = opendir(fg);
1297c478bd9Sstevel@tonic-gate 		ASSERT(gradedir != NULL, Ct_OPEN, g, errno);
1307c478bd9Sstevel@tonic-gate 		while (gnamef(gradedir, subf) == TRUE) {
1317c478bd9Sstevel@tonic-gate 		    if (subf[1] == '.') {
1327c478bd9Sstevel@tonic-gate 		        if (subf[0] == CMDPRE) {
1337c478bd9Sstevel@tonic-gate 			    /* Note - we can break now, since we
1347c478bd9Sstevel@tonic-gate 			     * have found a job grade with at least
1357c478bd9Sstevel@tonic-gate 			     * one C. file.
1367c478bd9Sstevel@tonic-gate 			    */
1377c478bd9Sstevel@tonic-gate 			    (void) strncat(machine(f)->jgrade, g, strlen(g));
1387c478bd9Sstevel@tonic-gate 			    break;
1397c478bd9Sstevel@tonic-gate 			}
1407c478bd9Sstevel@tonic-gate 		    }
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 		closedir(gradedir);
1437c478bd9Sstevel@tonic-gate 	    }
1447c478bd9Sstevel@tonic-gate 	    closedir(subdir);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* Make sure the overflow entry is null since it may be incorrect */
1487c478bd9Sstevel@tonic-gate 	M[UUSTAT_TBL].mach[0] = NULLCHAR;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/* count the number of systems */
1517c478bd9Sstevel@tonic-gate 	for (num=0, m=M; m->mach[0] != '\0'; m++, num++) {
1527c478bd9Sstevel@tonic-gate 	    DEBUG(5, "machine: %s, ", M[num].mach);
1537c478bd9Sstevel@tonic-gate 	    DEBUG(5, "job grade list: %s\n", M[num].jgrade);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	DEBUG(5, "Execute num=%d \n", num);
1567c478bd9Sstevel@tonic-gate 	while (num > 0) {
1577c478bd9Sstevel@tonic-gate 	    /*
1587c478bd9Sstevel@tonic-gate 	     * create lock file once we have work to do
1597c478bd9Sstevel@tonic-gate 	     * (but only if there is a job limit)
1607c478bd9Sstevel@tonic-gate 	     */
1617c478bd9Sstevel@tonic-gate 	    if (maxnumb > 0) {
1627c478bd9Sstevel@tonic-gate 	    	    for (i = 0; i < maxnumb; i++) {
1637c478bd9Sstevel@tonic-gate 			    (void) sprintf(lckname, "%s.%d", S_LOCK, i);
1647c478bd9Sstevel@tonic-gate 			    if (mklock(lckname) == SUCCESS)
1657c478bd9Sstevel@tonic-gate 			    	    break;
1667c478bd9Sstevel@tonic-gate 		    }
1677c478bd9Sstevel@tonic-gate 		    if (i == maxnumb) {
1687c478bd9Sstevel@tonic-gate 			    DEBUG(4, "found %d -- cleaning up\n", i);
1697c478bd9Sstevel@tonic-gate 			    cleanup(0);
1707c478bd9Sstevel@tonic-gate 		    }
1717c478bd9Sstevel@tonic-gate 	    }
1727c478bd9Sstevel@tonic-gate 	    snumber = (time((time_t *) 0) % num);  /* random num */
1737c478bd9Sstevel@tonic-gate 	    (void) strcpy(Rmtname, M[snumber].mach);
1747c478bd9Sstevel@tonic-gate 	    gradelist = M[snumber].jgrade;
1757c478bd9Sstevel@tonic-gate 	    DEBUG(5, "num=%d, ", num);
1767c478bd9Sstevel@tonic-gate 	    DEBUG(5, "snumber=%d, ", snumber);
1777c478bd9Sstevel@tonic-gate 	    DEBUG(5, "Rmtname=%s, ", Rmtname);
1787c478bd9Sstevel@tonic-gate 	    DEBUG(5, "job grade list= %s\n", gradelist);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	    numgrade = getargs(gradelist, gradeptr, MAXGRADE);
1817c478bd9Sstevel@tonic-gate 	    for (i=0; i<numgrade; i++) {
1827c478bd9Sstevel@tonic-gate 		(void) sprintf(lckname, "%s.%s.%s", LOCKPRE, Rmtname, gradeptr[i]);
1837c478bd9Sstevel@tonic-gate 		if (cklock(lckname) != FAIL && callok(Rmtname) == 0) {
1847c478bd9Sstevel@tonic-gate 		    /* no lock file and status time ok */
1857c478bd9Sstevel@tonic-gate 		    DEBUG(5, "call exuucico(%s)\n", Rmtname);
1867c478bd9Sstevel@tonic-gate 		    exuucico(Rmtname);
1877c478bd9Sstevel@tonic-gate 		    break;
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 		else {
1907c478bd9Sstevel@tonic-gate 		    /* job grade locked - look for the next one */
1917c478bd9Sstevel@tonic-gate 		    DEBUG(5, "job grade %s locked or inappropriate status\n",
1927c478bd9Sstevel@tonic-gate 			gradeptr[i]);
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 	    }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	    M[snumber] = M[num-1];
1977c478bd9Sstevel@tonic-gate 	    num--;
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	cleanup(0);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
202*462be471Sceastha 	return (0);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate struct m	*
machine(name)2067c478bd9Sstevel@tonic-gate machine(name)
2077c478bd9Sstevel@tonic-gate char	*name;
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	struct m *m;
2107c478bd9Sstevel@tonic-gate 	size_t	namelen;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	namelen = strlen(name);
2137c478bd9Sstevel@tonic-gate 	DEBUG(9, "machine(%s) called\n", name);
2147c478bd9Sstevel@tonic-gate 	for (m = M; m->mach[0] != '\0'; m++)
2157c478bd9Sstevel@tonic-gate 		/* match on overlap? */
2167c478bd9Sstevel@tonic-gate 		if (EQUALSN(name, m->mach, MAXBASENAME)) {
2177c478bd9Sstevel@tonic-gate 			/* check for job grade */
2187c478bd9Sstevel@tonic-gate 			if (m->jgrade[0] != NULLCHAR)
2197c478bd9Sstevel@tonic-gate 				(void) strncat(m->jgrade, " ", 1);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 			/* use longest name */
2227c478bd9Sstevel@tonic-gate 			if (namelen > strlen(m->mach))
2237c478bd9Sstevel@tonic-gate 				(void) strcpy(m->mach, name);
2247c478bd9Sstevel@tonic-gate 			return(m);
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * The table is set up with 2 extra entries
2297c478bd9Sstevel@tonic-gate 	 * When we go over by one, output error to errors log
2307c478bd9Sstevel@tonic-gate 	 * When more than one over, just reuse the previous entry
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	if (m-M >= UUSTAT_TBL) {
2337c478bd9Sstevel@tonic-gate 	    if (m-M == UUSTAT_TBL) {
2347c478bd9Sstevel@tonic-gate 		errent("MACHINE TABLE FULL", "", UUSTAT_TBL,
2357c478bd9Sstevel@tonic-gate 		__FILE__, __LINE__);
2367c478bd9Sstevel@tonic-gate 	    }
2377c478bd9Sstevel@tonic-gate 	    else
2387c478bd9Sstevel@tonic-gate 		/* use the last entry - overwrite it */
2397c478bd9Sstevel@tonic-gate 		m = &M[UUSTAT_TBL];
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	(void) strcpy(m->mach, name);
2437c478bd9Sstevel@tonic-gate 	m->jgrade[0] = NULLCHAR;
2447c478bd9Sstevel@tonic-gate 	return(m);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate void
exuucico(name)2487c478bd9Sstevel@tonic-gate exuucico(name)
2497c478bd9Sstevel@tonic-gate char *name;
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	char cmd[BUFSIZ];
2527c478bd9Sstevel@tonic-gate 	int status;
2537c478bd9Sstevel@tonic-gate 	pid_t pid, ret;
2547c478bd9Sstevel@tonic-gate 	char uopt[5];
2557c478bd9Sstevel@tonic-gate 	char sopt[BUFSIZ];
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	(void) sprintf(sopt, "-s%s", name);
2587c478bd9Sstevel@tonic-gate 	if (Uopt)
2597c478bd9Sstevel@tonic-gate 	    (void) sprintf(uopt, "-x%.1d", Uopt);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if ((pid = vfork()) == 0) {
2627c478bd9Sstevel@tonic-gate 	    if (Uopt)
2637c478bd9Sstevel@tonic-gate 	        (void) execle(UUCICO, "UUCICO", "-r1", uopt, sopt, (char *) 0, Env);
2647c478bd9Sstevel@tonic-gate 	    else
2657c478bd9Sstevel@tonic-gate 	        (void) execle(UUCICO, "UUCICO", "-r1", sopt, (char *) 0, Env);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	    cleanup(100);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 	while ((ret = wait(&status)) != pid)
2707c478bd9Sstevel@tonic-gate 	    if (ret == -1 && errno != EINTR)
2717c478bd9Sstevel@tonic-gate 		break;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	DEBUG(3, "ret=%ld, ", (ret == pid ? (long) status : (long) ret));
2747c478bd9Sstevel@tonic-gate 	return;
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate void
cleanup(code)2797c478bd9Sstevel@tonic-gate cleanup(code)
2807c478bd9Sstevel@tonic-gate int	code;
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	rmlock(CNULL);
2837c478bd9Sstevel@tonic-gate 	exit(code);
2847c478bd9Sstevel@tonic-gate }
285