xref: /titanic_52/usr/src/cmd/bnu/limits.c (revision 84ab085a13f931bc78e7415e7ce921dbaa14fcb3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* from SVR4 bnu:limits.c 1.4 */
27 
28 #include "uucp.h"
29 
30 /* add sitedep() and sysmatch() to list when ifdefs are removed below */
31 static int get_tokens(), siteindep();
32 
33 /*  field array indexes for LIMIT parameters */
34 
35 #define U_SERVICE	0
36 #define U_MAX		1
37 #define U_SYSTEM	2
38 #define U_MODE		3
39 
40 static char * _Lwords[] = {"service", "max", "system", "mode"};
41 
42 #define NUMFLDS		4
43 
44 struct name_value
45 {
46 	char *name;
47 	char *value;
48 };
49 
50 /*
51  * manage limits file.
52  */
53 
54 /*
55  * scan the Limits file and get the limit for the given service.
56  * return SUCCESS if the limit was found, else return FAIL.
57  */
58 int
59 scanlimit(service, limitval)
60 char *service;
61 struct limits *limitval;
62 {
63 	char buf[BUFSIZ];
64 	char *tokens[NUMFLDS];	/* fields found in LIMITS */
65 	char msgbuf[BUFSIZ];	/* place to build messages */
66 	FILE *Fp = NULL;	/* file pointer for LIMITS */
67 	int SIflag, SDflag;
68 
69 	if ((Fp = fopen(LIMITS, "r")) == NULL) {
70 	    DEBUG(5, "cannot open %s\n", LIMITS);
71 	    sprintf(msgbuf, "fopen of %s failed with errno=%%d\n", LIMITS);
72 	    DEBUG(5, msgbuf, errno);
73 	    return(FAIL);
74 	}
75 
76 	SIflag = FALSE;
77 	SDflag = TRUE;
78 
79 /* The following #if (0 == 1) and #endif lines should be deleted when
80  * we expand the functionality of the Limits file to include the
81  * limit per site, and the mode for uucico.
82  */
83 #if (0 == 1)
84 	if (strcmp(service, "uucico") == SAME)
85 	    SDflag = FALSE;
86 #endif
87 
88 	while ((getuline(Fp, buf) > 0) && ((SIflag && SDflag) == FALSE)) {
89 	    if (get_tokens(buf, tokens) == FAIL)
90 		continue;
91 
92 	    if (SIflag == FALSE) {
93 		if (siteindep(tokens, service, limitval) == SUCCESS)
94 		    SIflag = TRUE;
95 	    }
96 
97 /* The following #if (0 == 1) and #endif lines should be deleted when
98  * we expand the functionality of the Limits file to include the
99  * limit per site, and the mode for uucico.
100  */
101 #if (0 == 1)
102 	    if (SDflag == FALSE) {
103 		if (sitedep(tokens, limitval) == SUCCESS)
104 		    SDflag = TRUE;
105 	    }
106 #endif
107 	}
108 
109 	fclose(Fp);
110 	if ((SIflag && SDflag) == TRUE)
111 	    return(SUCCESS);
112 	else return(FAIL);
113 }
114 
115 /*
116  * parse a line in LIMITS and return a vector
117  * of fields (flds)
118  *
119  * return:
120  *	SUCCESS - token pairs name match with the key words
121  */
122 static int
123 get_tokens (line,flds)
124 char *line;
125 char *flds[];
126 {
127 	register i;
128 	register char *p;
129 	struct name_value pair;
130 
131 	/* initialize defaults  in case parameter is not specified */
132 	for (i=0;i<NUMFLDS;i++)
133 		flds[i] = CNULL;
134 
135 	for (p=line;p && *p;) {
136 		p = next_token (p, &pair);
137 
138 		for (i=0; i<NUMFLDS; i++) {
139 			if (EQUALS(pair.name, _Lwords[i])) {
140 				flds[i] = pair.value;
141 				break;
142 			}
143 			if (i == NUMFLDS-1) /* pair.name is not key */
144 				return FAIL;
145 		}
146 	}
147 	return(SUCCESS);
148 }
149 /*
150  * this function can only handle the following format
151  *
152  *	service=uucico max=5
153  *
154  * return:
155  *	SUCCESS - OK
156  *	FAIL - service's value does not match or wrong format
157  */
158 static int
159 siteindep(flds, service, limitval)
160 char *flds[];
161 char *service;
162 struct limits *limitval;
163 {
164 
165 	if (!EQUALS(flds[U_SERVICE], service) || (flds[U_MAX] == CNULL))
166 		return(FAIL);
167 	if (sscanf(flds[U_MAX],"%d",&limitval->totalmax)==0)
168 		limitval->totalmax = -1; /* type conflict*/
169 	return(SUCCESS);
170 }
171 
172 /* The following #if (0 == 1) and #endif lines should be deleted when
173  * we expand the functionality of the Limits file to include the
174  * limit per site, and the mode for uucico.
175  */
176 #if (0 == 1)
177 /*
178  * this function can only handle the following format
179  *
180  *	service=uucico system=ihnp1:ihnp3 [max=5] [mode=master]
181  *
182  * return:
183  *	SUCCESS - OK
184  *	FAIL - not uucico, no system name in Limits,
185  *		system's name does not match
186  */
187 static int
188 sitedep(flds, limitval)
189 char *flds[];
190 struct limits *limitval;
191 {
192 
193 	if (!EQUALS(flds[U_SERVICE],"uucico"))
194 		return FAIL;
195 	if ((flds[U_SYSTEM] == CNULL) || (sysmatch(flds[U_SYSTEM]) != 0))
196 		return FAIL;
197 	if (flds[U_MAX] == CNULL)
198 		limitval->sitemax = 1; /* default value */
199 	else if (sscanf(flds[U_MAX],"%d",&limitval->sitemax)==0)
200 			limitval->sitemax = -1; /* type conflict*/
201 
202 	if (flds[U_MODE] == CNULL)
203 		strcpy(limitval->mode,"master:slave");
204 	else
205 		strncpy(limitval->mode,flds[U_MODE],strlen(flds[U_MODE]));
206 	return(SUCCESS);
207 }
208 
209 /*
210  * this function checks if system in system's list
211  * system=ihnp1:ihnp3:...
212  *
213  * return:
214  *	SUCCESS - OK
215  */
216 static int
217 sysmatch(p)
218 char *p;
219 {
220 	char *arg;
221 
222 	while (p && *p) {
223 		p = nextarg(p, &arg);
224 	    	if (EQUALS(arg, Rmtname))
225 			return(SUCCESS);
226 	}
227 	return FAIL;
228 }
229 
230 #endif
231