xref: /illumos-gate/usr/src/cmd/bnu/utility.c (revision 3b296559)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include "uucp.h"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate static void logError();
287c478bd9Sstevel@tonic-gate extern int cuantos(), gnamef();
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #define TY_ASSERT	1
317c478bd9Sstevel@tonic-gate #define TY_ERROR	2
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  *	produce an assert error message
357c478bd9Sstevel@tonic-gate  * input:
367c478bd9Sstevel@tonic-gate  *	s1 - string 1
377c478bd9Sstevel@tonic-gate  *	s2 - string 2
387c478bd9Sstevel@tonic-gate  *	i1 - integer 1 (usually errno)
397c478bd9Sstevel@tonic-gate  *	file - __FILE of calling module
407c478bd9Sstevel@tonic-gate  *	line - __LINE__ of calling module
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate void
assert(char * s1,char * s2,int i1,char * file,int line)43*3b296559SToomas Soome assert(char *s1, char *s2, int i1, char *file, int line)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	logError(s1, s2, i1, TY_ASSERT, file, line);
467c478bd9Sstevel@tonic-gate 	return;
477c478bd9Sstevel@tonic-gate }
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  *	produce an assert error message
527c478bd9Sstevel@tonic-gate  * input: -- same as assert
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate void
errent(char * s1,char * s2,int i1,char * file,int line)55*3b296559SToomas Soome errent(char *s1, char *s2, int i1, char *file, int line)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	logError(s1, s2, i1, TY_ERROR, file, line);
587c478bd9Sstevel@tonic-gate 	return;
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define EFORMAT	"%sERROR (%.9s)  pid: %ld (%s) %s %s (%d) [FILE: %s, LINE: %d]\n"
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static void
logError(char * s1,char * s2,int i1,int type,char * file,int line)64*3b296559SToomas Soome logError(char *s1, char *s2, int i1, int type, char *file, int line)
657c478bd9Sstevel@tonic-gate {
66*3b296559SToomas Soome 	FILE *errlog;
677c478bd9Sstevel@tonic-gate 	char text[BUFSIZ];
687c478bd9Sstevel@tonic-gate 	pid_t pid;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if (Debug)
717c478bd9Sstevel@tonic-gate 		errlog = stderr;
727c478bd9Sstevel@tonic-gate 	else {
737c478bd9Sstevel@tonic-gate 		errlog = fopen(ERRLOG, "a");
747c478bd9Sstevel@tonic-gate 		(void) chmod(ERRLOG, PUB_FILEMODE);
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 	if (errlog == NULL)
777c478bd9Sstevel@tonic-gate 		return;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	pid = getpid();
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	(void) fprintf(errlog, EFORMAT, type == TY_ASSERT ? "ASSERT " : " ",
827c478bd9Sstevel@tonic-gate 	    Progname, (long) pid, timeStamp(), s1, s2, i1, file, line);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (!Debug)
857c478bd9Sstevel@tonic-gate 		(void) fclose(errlog);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	(void) sprintf(text, " %sERROR %.100s %.100s (%.9s)",
887c478bd9Sstevel@tonic-gate 	    type == TY_ASSERT ? "ASSERT " : " ",
897c478bd9Sstevel@tonic-gate 	    s1, s2, Progname);
907c478bd9Sstevel@tonic-gate 	if (type == TY_ASSERT)
917c478bd9Sstevel@tonic-gate 	    systat(Rmtname, SS_ASSERT_ERROR, text, Retrytime);
927c478bd9Sstevel@tonic-gate 	return;
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* timeStamp - create standard time string
977c478bd9Sstevel@tonic-gate  * return
987c478bd9Sstevel@tonic-gate  *	pointer to time string
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate char *
timeStamp()1027c478bd9Sstevel@tonic-gate timeStamp()
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	register struct tm *tp;
1057c478bd9Sstevel@tonic-gate 	time_t clock;
1067c478bd9Sstevel@tonic-gate 	static char str[20];
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	(void) time(&clock);
1097c478bd9Sstevel@tonic-gate 	tp = localtime(&clock);
1107c478bd9Sstevel@tonic-gate 	(void) sprintf(str, "%d/%d-%d:%2.2d:%2.2d", tp->tm_mon + 1,
1117c478bd9Sstevel@tonic-gate 	    tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
1127c478bd9Sstevel@tonic-gate 	return(str);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * Function:	countProcs - Count Process to Stay Within Limits
1187c478bd9Sstevel@tonic-gate *
1197c478bd9Sstevel@tonic-gate * There are a number of cases in BNU when we want to limit the number
1207c478bd9Sstevel@tonic-gate * of processes of a certain type that are running at a given time.  This
1217c478bd9Sstevel@tonic-gate * process is used to check the number of existing processes, to determine
1227c478bd9Sstevel@tonic-gate * if a new one is allowed.
1237c478bd9Sstevel@tonic-gate *
1247c478bd9Sstevel@tonic-gate * The strategy is that all processes of a given type will place a lock
1257c478bd9Sstevel@tonic-gate * file with a specific prefix in a single directory, usually
1267c478bd9Sstevel@tonic-gate * /var/spool/locks.  The caller of this function must provide a full
1277c478bd9Sstevel@tonic-gate * path prefix, and countProcs will count the number of files that begin
1287c478bd9Sstevel@tonic-gate * with the prefix and compare the count to the allowable maximum.
1297c478bd9Sstevel@tonic-gate *
1307c478bd9Sstevel@tonic-gate * Parameters:
1317c478bd9Sstevel@tonic-gate *
1327c478bd9Sstevel@tonic-gate *	prefix -	A full path prefix for lock files that identify
1337c478bd9Sstevel@tonic-gate *			processes that are to be counted.
1347c478bd9Sstevel@tonic-gate *	maxCount -	Maximum number of allowable processes.
1357c478bd9Sstevel@tonic-gate *
1367c478bd9Sstevel@tonic-gate * Returns:
1377c478bd9Sstevel@tonic-gate *
1387c478bd9Sstevel@tonic-gate *	TRUE is returned if this process is allowed to continue, and
1397c478bd9Sstevel@tonic-gate *	FALSE is returned if the maximum is exceeded.
1407c478bd9Sstevel@tonic-gate */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate int
countProcs(prefix,maxCount)1437c478bd9Sstevel@tonic-gate countProcs (prefix, maxCount)
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate char *	prefix;
1467c478bd9Sstevel@tonic-gate int	maxCount;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	register char *	namePrefix;		/* Points to file name part */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	char		directory[MAXNAMESIZE];
1527c478bd9Sstevel@tonic-gate 	register int	processes;		/* Count of processes. */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/* Separate prefix into directory part and file name part. */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	strncpy(directory, prefix, MAXNAMESIZE);
1577c478bd9Sstevel@tonic-gate 	directory[MAXNAMESIZE-1] = NULLCHAR;
1587c478bd9Sstevel@tonic-gate 	namePrefix = strrchr(directory, '/');
1597c478bd9Sstevel@tonic-gate 	ASSERT(namePrefix  != NULL, "No file name in", prefix, 0);
1607c478bd9Sstevel@tonic-gate 	*namePrefix++ = NULLCHAR;		/* Terminate directory part */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/* Check to see if we can continue. */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	processes = cuantos(namePrefix, directory);
1657c478bd9Sstevel@tonic-gate 	if (processes <= maxCount)
1667c478bd9Sstevel@tonic-gate 		return TRUE;
1677c478bd9Sstevel@tonic-gate 	else
1687c478bd9Sstevel@tonic-gate 		return FALSE;
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * return the number of files in directory <dir> who's names
1747c478bd9Sstevel@tonic-gate  * begin with <prefix>
1757c478bd9Sstevel@tonic-gate  * This is used to count the number of processes of a certain
1767c478bd9Sstevel@tonic-gate  * type that are currently running.
1777c478bd9Sstevel@tonic-gate  *
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate int
cuantos(prefix,dir)1807c478bd9Sstevel@tonic-gate cuantos(prefix, dir)
1817c478bd9Sstevel@tonic-gate char *prefix, *dir;
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	int i = 0;
1847c478bd9Sstevel@tonic-gate 	DIR	*pdir;
1857c478bd9Sstevel@tonic-gate 	char fullname[MAXNAMESIZE], file[MAXNAMESIZE];
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	pdir = opendir(dir);
1887c478bd9Sstevel@tonic-gate 	ASSERT(pdir != NULL, Ct_OPEN, dir, errno);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	while (gnamef(pdir, file) == TRUE)
1917c478bd9Sstevel@tonic-gate 		if (PREFIX(prefix, file)) {
1927c478bd9Sstevel@tonic-gate 		    (void) sprintf(fullname, "%s/%s", dir, file);
1937c478bd9Sstevel@tonic-gate 		    if (cklock(fullname))
1947c478bd9Sstevel@tonic-gate 			i++;
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 	closedir(pdir);
1977c478bd9Sstevel@tonic-gate 	return(i);
1987c478bd9Sstevel@tonic-gate }
199