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