xref: /titanic_41/usr/src/lib/libslp/clib/slp_config.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 (c) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Provides accessors to configuration properties.
31  *
32  * slp_readConfig:	attempts to locate slp.conf, and reads in all
33  *				properties specified therein.
34  * slp_get_mtu:		returns the MTU
35  * slp_get_next_onlist:	parses a comma separated list of integers (in
36  *				string form), returning one at a time.
37  * slp_parse_static_das: parses the list of DAs given in the DAAddresses
38  *				property.
39  *
40  * Also see the config wrapper macros in slp-internal.h.
41  */
42 
43 #include <stdio.h>
44 #include <syslog.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <ctype.h>
48 #include <slp-internal.h>
49 
50 /*
51  * Reads from fp and dynamically reallocates the buffer if necessary.
52  * Returns 1 on success, 0 on read completion, and -1 on failure.
53  */
54 static int super_fgets(char **buf, size_t *bufsize, FILE *fp) {
55 	char *r, *p;
56 	size_t real_bufsize, readlen = 0;
57 
58 	p = *buf;
59 	real_bufsize = *bufsize;
60 	for (;;) {
61 		r = fgets(p, (int)real_bufsize, fp);
62 		if (feof(fp) && !r)
63 			return (0);
64 		if (!r)
65 			return (-1);
66 		readlen += strlen(r);
67 		if ((*buf)[readlen - 1] == '\n')
68 			return (1);
69 
70 		/* else	buf is too small */
71 		*bufsize *= 2;
72 		if (!(*buf = realloc(*buf, *bufsize))) {
73 			slp_err(LOG_CRIT, 0, "super_fgets", "out of memory");
74 			return (-1);
75 		}
76 		p = *buf + readlen;
77 		real_bufsize = *bufsize - readlen;
78 	}
79 }
80 
81 static void skip_space(char **p) {
82 	while (*p && **p != '\n' && isspace(**p))
83 		(*p)++;
84 }
85 
86 static void null_space(char *p) {
87 	for (; *p; p++)
88 		if (isspace(*p))
89 			*p = 0;
90 }
91 
92 /*
93  * Reads into the local property store all properties defined in
94  * the config file.
95  */
96 void slp_readConfig() {
97 	char *cfile, *buf;
98 	FILE *fp;
99 	size_t buflen = 512;
100 
101 	/* check env for alternate config file */
102 	fp = NULL;
103 	if (cfile = getenv("SLP_CONF_FILE"))
104 		fp = fopen(cfile, "r");
105 	if (!fp)
106 		if (!(fp = fopen(SLP_DEFAULT_CONFIG_FILE, "r"))) {
107 			slp_err(LOG_INFO, 0, "readConfig",
108 				"cannot open config file");
109 			return;
110 		}
111 
112 	if (!(buf = malloc(buflen))) {
113 		slp_err(LOG_CRIT, 0, "readConfig", "out of memory");
114 		(void) fclose(fp);
115 		return;
116 	}
117 
118 	while (!feof(fp)) {
119 		char *val, *p;
120 		int err;
121 
122 		/* read a line */
123 		err = super_fgets(&buf, &buflen, fp);
124 		if (err == 0) continue;
125 		if (err == -1) {
126 			slp_err(LOG_INFO, 0, "readConfig",
127 				"error reading file: %d",
128 				ferror(fp));
129 			(void) fclose(fp);
130 			free(buf);
131 			return;
132 		}
133 
134 		/* skip comments and newlines */
135 		p = buf;
136 		skip_space(&p);
137 		if (*p == '#' || *p == ';' || *p == '\n')
138 			continue;
139 
140 		/* get property and value */
141 		if (val = strchr(p, '=')) {
142 			*val++ = 0;
143 			skip_space(&val);
144 			/* remove the trailing newline */
145 			val[strlen(val) - 1] = 0;
146 		}
147 		null_space(p);
148 
149 		SLPSetProperty(p, val ? val : "");
150 	}
151 
152 	(void) fclose(fp);
153 	free(buf);
154 }
155 
156 /*
157  * Config convenience wrappers
158  */
159 size_t slp_get_mtu() {
160 	size_t size;
161 	size = atoi(SLPGetProperty(SLP_CONFIG_MTU));
162 	size = size ? size : SLP_DEFAULT_SENDMTU;
163 
164 	return (size);
165 }
166 
167 /*
168  * On the first invocation, *state should == the value of the property
169  * to parse.
170  * If there are no more timeouts, returns -1, otherwise the timeout.
171  * If the value in the property is invalid, returns the default 2000.
172  */
173 int slp_get_next_onlist(char **state) {
174 	char *p, buf[33];
175 	size_t l;
176 	int answer;
177 
178 	if (!*state)
179 		return (-1);
180 
181 	if (**state == ',') {
182 		(*state)++;	/* skip the ',' */
183 	}
184 	p = *state;
185 	*state = slp_utf_strchr(*state, ',');
186 	if (!*state)
187 		l = strlen(p);
188 	else {
189 		l = *state - p;
190 		l = (l > 32 ? 32 : l);
191 	}
192 	(void) strncpy(buf, p, l);
193 	buf[l] = 0;
194 	answer = atoi(buf);
195 
196 	return (answer != 0 ? answer : 2000);
197 }
198 
199 int slp_get_maxResults() {
200 	int num = atoi(SLPGetProperty(SLP_CONFIG_MAXRESULTS));
201 
202 	return (num <= 0 ? -1 : num);
203 }
204