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