1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23*0Sstevel@tonic-gate /* All Rights Reserved */
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI" /* from SVR4 bnu:utility.c 2.9 */
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate #include "uucp.h"
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate static void logError();
32*0Sstevel@tonic-gate extern int cuantos(), gnamef();
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate #define TY_ASSERT 1
35*0Sstevel@tonic-gate #define TY_ERROR 2
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate * produce an assert error message
39*0Sstevel@tonic-gate * input:
40*0Sstevel@tonic-gate * s1 - string 1
41*0Sstevel@tonic-gate * s2 - string 2
42*0Sstevel@tonic-gate * i1 - integer 1 (usually errno)
43*0Sstevel@tonic-gate * file - __FILE of calling module
44*0Sstevel@tonic-gate * line - __LINE__ of calling module
45*0Sstevel@tonic-gate */
46*0Sstevel@tonic-gate void
assert(s1,s2,i1,file,line)47*0Sstevel@tonic-gate assert(s1, s2, i1, file, line)
48*0Sstevel@tonic-gate char *s1, *s2, *file;
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate logError(s1, s2, i1, TY_ASSERT, file, line);
51*0Sstevel@tonic-gate return;
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate * produce an assert error message
57*0Sstevel@tonic-gate * input: -- same as assert
58*0Sstevel@tonic-gate */
59*0Sstevel@tonic-gate void
errent(s1,s2,i1,file,line)60*0Sstevel@tonic-gate errent(s1, s2, i1, file, line)
61*0Sstevel@tonic-gate char *s1, *s2, *file;
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate logError(s1, s2, i1, TY_ERROR, file, line);
64*0Sstevel@tonic-gate return;
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate #define EFORMAT "%sERROR (%.9s) pid: %ld (%s) %s %s (%d) [FILE: %s, LINE: %d]\n"
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate static void
logError(s1,s2,i1,type,file,line)70*0Sstevel@tonic-gate logError(s1, s2, i1, type, file, line)
71*0Sstevel@tonic-gate char *s1, *s2, *file;
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate register FILE *errlog;
74*0Sstevel@tonic-gate char text[BUFSIZ];
75*0Sstevel@tonic-gate pid_t pid;
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate if (Debug)
78*0Sstevel@tonic-gate errlog = stderr;
79*0Sstevel@tonic-gate else {
80*0Sstevel@tonic-gate errlog = fopen(ERRLOG, "a");
81*0Sstevel@tonic-gate (void) chmod(ERRLOG, PUB_FILEMODE);
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate if (errlog == NULL)
84*0Sstevel@tonic-gate return;
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate pid = getpid();
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate (void) fprintf(errlog, EFORMAT, type == TY_ASSERT ? "ASSERT " : " ",
89*0Sstevel@tonic-gate Progname, (long) pid, timeStamp(), s1, s2, i1, file, line);
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate if (!Debug)
92*0Sstevel@tonic-gate (void) fclose(errlog);
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate (void) sprintf(text, " %sERROR %.100s %.100s (%.9s)",
95*0Sstevel@tonic-gate type == TY_ASSERT ? "ASSERT " : " ",
96*0Sstevel@tonic-gate s1, s2, Progname);
97*0Sstevel@tonic-gate if (type == TY_ASSERT)
98*0Sstevel@tonic-gate systat(Rmtname, SS_ASSERT_ERROR, text, Retrytime);
99*0Sstevel@tonic-gate return;
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate /* timeStamp - create standard time string
104*0Sstevel@tonic-gate * return
105*0Sstevel@tonic-gate * pointer to time string
106*0Sstevel@tonic-gate */
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate char *
timeStamp()109*0Sstevel@tonic-gate timeStamp()
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate register struct tm *tp;
112*0Sstevel@tonic-gate time_t clock;
113*0Sstevel@tonic-gate static char str[20];
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate (void) time(&clock);
116*0Sstevel@tonic-gate tp = localtime(&clock);
117*0Sstevel@tonic-gate (void) sprintf(str, "%d/%d-%d:%2.2d:%2.2d", tp->tm_mon + 1,
118*0Sstevel@tonic-gate tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
119*0Sstevel@tonic-gate return(str);
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate /*
124*0Sstevel@tonic-gate * Function: countProcs - Count Process to Stay Within Limits
125*0Sstevel@tonic-gate *
126*0Sstevel@tonic-gate * There are a number of cases in BNU when we want to limit the number
127*0Sstevel@tonic-gate * of processes of a certain type that are running at a given time. This
128*0Sstevel@tonic-gate * process is used to check the number of existing processes, to determine
129*0Sstevel@tonic-gate * if a new one is allowed.
130*0Sstevel@tonic-gate *
131*0Sstevel@tonic-gate * The strategy is that all processes of a given type will place a lock
132*0Sstevel@tonic-gate * file with a specific prefix in a single directory, usually
133*0Sstevel@tonic-gate * /var/spool/locks. The caller of this function must provide a full
134*0Sstevel@tonic-gate * path prefix, and countProcs will count the number of files that begin
135*0Sstevel@tonic-gate * with the prefix and compare the count to the allowable maximum.
136*0Sstevel@tonic-gate *
137*0Sstevel@tonic-gate * Parameters:
138*0Sstevel@tonic-gate *
139*0Sstevel@tonic-gate * prefix - A full path prefix for lock files that identify
140*0Sstevel@tonic-gate * processes that are to be counted.
141*0Sstevel@tonic-gate * maxCount - Maximum number of allowable processes.
142*0Sstevel@tonic-gate *
143*0Sstevel@tonic-gate * Returns:
144*0Sstevel@tonic-gate *
145*0Sstevel@tonic-gate * TRUE is returned if this process is allowed to continue, and
146*0Sstevel@tonic-gate * FALSE is returned if the maximum is exceeded.
147*0Sstevel@tonic-gate */
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate int
countProcs(prefix,maxCount)150*0Sstevel@tonic-gate countProcs (prefix, maxCount)
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate char * prefix;
153*0Sstevel@tonic-gate int maxCount;
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate register char * namePrefix; /* Points to file name part */
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate char directory[MAXNAMESIZE];
159*0Sstevel@tonic-gate register int processes; /* Count of processes. */
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate /* Separate prefix into directory part and file name part. */
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate strncpy(directory, prefix, MAXNAMESIZE);
164*0Sstevel@tonic-gate directory[MAXNAMESIZE-1] = NULLCHAR;
165*0Sstevel@tonic-gate namePrefix = strrchr(directory, '/');
166*0Sstevel@tonic-gate ASSERT(namePrefix != NULL, "No file name in", prefix, 0);
167*0Sstevel@tonic-gate *namePrefix++ = NULLCHAR; /* Terminate directory part */
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate /* Check to see if we can continue. */
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate processes = cuantos(namePrefix, directory);
172*0Sstevel@tonic-gate if (processes <= maxCount)
173*0Sstevel@tonic-gate return TRUE;
174*0Sstevel@tonic-gate else
175*0Sstevel@tonic-gate return FALSE;
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate /*
180*0Sstevel@tonic-gate * return the number of files in directory <dir> who's names
181*0Sstevel@tonic-gate * begin with <prefix>
182*0Sstevel@tonic-gate * This is used to count the number of processes of a certain
183*0Sstevel@tonic-gate * type that are currently running.
184*0Sstevel@tonic-gate *
185*0Sstevel@tonic-gate */
186*0Sstevel@tonic-gate int
cuantos(prefix,dir)187*0Sstevel@tonic-gate cuantos(prefix, dir)
188*0Sstevel@tonic-gate char *prefix, *dir;
189*0Sstevel@tonic-gate {
190*0Sstevel@tonic-gate int i = 0;
191*0Sstevel@tonic-gate DIR *pdir;
192*0Sstevel@tonic-gate char fullname[MAXNAMESIZE], file[MAXNAMESIZE];
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate pdir = opendir(dir);
195*0Sstevel@tonic-gate ASSERT(pdir != NULL, Ct_OPEN, dir, errno);
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate while (gnamef(pdir, file) == TRUE)
198*0Sstevel@tonic-gate if (PREFIX(prefix, file)) {
199*0Sstevel@tonic-gate (void) sprintf(fullname, "%s/%s", dir, file);
200*0Sstevel@tonic-gate if (cklock(fullname))
201*0Sstevel@tonic-gate i++;
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate closedir(pdir);
204*0Sstevel@tonic-gate return(i);
205*0Sstevel@tonic-gate }
206