Print this page
patch tsoome-feedback
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/cmd-inet/sbin/dhcpagent/defaults.c
+++ new/usr/src/cmd/cmd-inet/sbin/dhcpagent/defaults.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 #include <sys/types.h>
27 27 #include <stdlib.h>
28 28 #include <string.h>
29 29 #include <ctype.h>
30 30 #include <dhcpmsg.h>
31 31 #include <stdio.h>
32 32 #include <sys/stat.h>
33 33 #include <libnvpair.h>
34 34
35 35 #include "common.h"
36 36 #include "defaults.h"
37 37
38 38 struct dhcp_default {
39 39
40 40 const char *df_name; /* parameter name */
41 41 const char *df_default; /* default value */
42 42 int df_min; /* min value if type DF_INTEGER */
43 43 int df_max; /* max value if type DF_INTEGER */
44 44 };
45 45
46 46 /*
47 47 * note: keep in the same order as tunable parameter constants in defaults.h
48 48 */
49 49
50 50 static struct dhcp_default defaults[] = {
51 51
52 52 { "RELEASE_ON_SIGTERM", "0", 0, 0 },
53 53 { "IGNORE_FAILED_ARP", "1", 0, -1 },
54 54 { "OFFER_WAIT", "3", 1, 20 },
55 55 { "ARP_WAIT", "1000", 0, -1 },
56 56 { "CLIENT_ID", NULL, 0, 0 },
57 57 { "PARAM_REQUEST_LIST", NULL, 0, 0 },
58 58 { "REQUEST_HOSTNAME", "1", 0, 0 },
59 59 { "DEBUG_LEVEL", "0", 0, 3 },
60 60 { "VERBOSE", "0", 0, 0 },
61 61 { "VERIFIED_LEASE_ONLY", "0", 0, 0 },
62 62 { "PARAM_IGNORE_LIST", NULL, 0, 0 }
63 63 };
64 64
65 65 /*
66 66 * df_build_cache(): builds the defaults nvlist cache
67 67 *
68 68 * input: void
69 69 * output: a pointer to an nvlist of the current defaults, or NULL on failure
70 70 */
71 71
72 72 static nvlist_t *
73 73 df_build_cache(void)
74 74 {
75 75 char entry[1024];
76 76 int i;
77 77 char *param, *pastv6, *value, *end;
78 78 FILE *fp;
79 79 nvlist_t *nvlist;
80 80 struct dhcp_default *defp;
81 81
82 82 if ((fp = fopen(DHCP_AGENT_DEFAULTS, "r")) == NULL)
83 83 return (NULL);
84 84
85 85 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
86 86 dhcpmsg(MSG_WARNING, "cannot build default value cache; "
87 87 "using built-in defaults");
88 88 (void) fclose(fp);
89 89 return (NULL);
90 90 }
91 91
92 92 while (fgets(entry, sizeof (entry), fp) != NULL) {
93 93 for (i = 0; entry[i] == ' '; i++)
94 94 ;
95 95
96 96 end = strrchr(entry, '\n');
97 97 value = strchr(entry, '=');
98 98 if (end == NULL || value == NULL || entry[i] == '#')
99 99 continue;
100 100
101 101 *end = '\0';
102 102 *value++ = '\0';
103 103
104 104 /*
105 105 * to be compatible with the old defread()-based code
106 106 * which ignored case, store the parameters (except for the
107 107 * leading interface name) in upper case.
108 108 */
109 109
110 110 if ((param = strchr(entry, '.')) == NULL) {
111 111 pastv6 = param = entry;
112 112 } else {
113 113 pastv6 = ++param;
114 114 if (strncasecmp(param, "v6.", 3) == 0)
115 115 pastv6 += 3;
116 116 }
117 117
118 118 for (defp = defaults;
119 119 (char *)defp < (char *)defaults + sizeof (defaults);
120 120 defp++) {
121 121 if (strcasecmp(pastv6, defp->df_name) == 0) {
122 122 if (defp->df_max == -1) {
123 123 dhcpmsg(MSG_WARNING, "parameter %s is "
124 124 "obsolete; ignored", defp->df_name);
125 125 }
126 126 break;
127 127 }
128 128 }
129 129
130 130 for (; *param != '\0'; param++)
131 131 *param = toupper(*param);
132 132
133 133 if (nvlist_add_string(nvlist, &entry[i], value) != 0) {
134 134 dhcpmsg(MSG_WARNING, "cannot build default value cache;"
135 135 " using built-in defaults");
136 136 nvlist_free(nvlist);
137 137 nvlist = NULL;
138 138 break;
139 139 }
140 140 }
141 141
142 142 (void) fclose(fp);
143 143 return (nvlist);
144 144 }
145 145
146 146 /*
147 147 * df_get_string(): gets the string value of a given user-tunable parameter
148 148 *
149 149 * input: const char *: the interface the parameter applies to
150 150 * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
151 151 * uint_t: the parameter number to look up
152 152 * output: const char *: the parameter's value, or default if not set
153 153 * (must be copied by caller to be kept)
154 154 * NOTE: df_get_string() is both used by functions outside this source
155 155 * file to retrieve strings from the defaults file, *and*
156 156 * internally by other df_get_*() functions.
157 157 */
158 158
159 159 const char *
160 160 df_get_string(const char *if_name, boolean_t isv6, uint_t param)
161 161 {
162 162 char *value;
163 163 char paramstr[256];
164 164 char name[256];
165 165 struct stat statbuf;
166 166 static struct stat df_statbuf;
167 167 static boolean_t df_unavail_msg = B_FALSE;
168 168 static nvlist_t *df_nvlist = NULL;
169 169
170 170 if (param >= (sizeof (defaults) / sizeof (*defaults)))
171 171 return (NULL);
172 172
173 173 if (stat(DHCP_AGENT_DEFAULTS, &statbuf) != 0) {
174 174 if (!df_unavail_msg) {
175 175 dhcpmsg(MSG_WARNING, "cannot access %s; using "
176 176 "built-in defaults", DHCP_AGENT_DEFAULTS);
177 177 df_unavail_msg = B_TRUE;
178 178 }
↓ open down ↓ |
178 lines elided |
↑ open up ↑ |
179 179 return (defaults[param].df_default);
180 180 }
181 181
182 182 /*
183 183 * if our cached parameters are stale, rebuild.
184 184 */
185 185
186 186 if (statbuf.st_mtime != df_statbuf.st_mtime ||
187 187 statbuf.st_size != df_statbuf.st_size) {
188 188 df_statbuf = statbuf;
189 - if (df_nvlist != NULL)
190 - nvlist_free(df_nvlist);
189 + nvlist_free(df_nvlist);
191 190 df_nvlist = df_build_cache();
192 191 }
193 192
194 193 if (isv6) {
195 194 (void) snprintf(name, sizeof (name), ".V6.%s",
196 195 defaults[param].df_name);
197 196 (void) snprintf(paramstr, sizeof (paramstr), "%s%s", if_name,
198 197 name);
199 198 } else {
200 199 (void) strlcpy(name, defaults[param].df_name, sizeof (name));
201 200 (void) snprintf(paramstr, sizeof (paramstr), "%s.%s", if_name,
202 201 name);
203 202 }
204 203
205 204 /*
206 205 * first look for `if_name.[v6.]param', then `[v6.]param'. if neither
207 206 * has been set, use the built-in default.
208 207 */
209 208
210 209 if (nvlist_lookup_string(df_nvlist, paramstr, &value) == 0 ||
211 210 nvlist_lookup_string(df_nvlist, name, &value) == 0)
212 211 return (value);
213 212
214 213 return (defaults[param].df_default);
215 214 }
216 215
217 216 /*
218 217 * df_get_int(): gets the integer value of a given user-tunable parameter
219 218 *
220 219 * input: const char *: the interface the parameter applies to
221 220 * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
222 221 * uint_t: the parameter number to look up
223 222 * output: int: the parameter's value, or default if not set
224 223 */
225 224
226 225 int
227 226 df_get_int(const char *if_name, boolean_t isv6, uint_t param)
228 227 {
229 228 const char *value;
230 229 int value_int;
231 230
232 231 if (param >= (sizeof (defaults) / sizeof (*defaults)))
233 232 return (0);
234 233
235 234 value = df_get_string(if_name, isv6, param);
236 235 if (value == NULL || !isdigit(*value))
237 236 goto failure;
238 237
239 238 value_int = atoi(value);
240 239 if (value_int > defaults[param].df_max ||
241 240 value_int < defaults[param].df_min)
242 241 goto failure;
243 242
244 243 return (value_int);
245 244
246 245 failure:
247 246 dhcpmsg(MSG_WARNING, "df_get_int: parameter `%s' is not between %d and "
248 247 "%d, defaulting to `%s'", defaults[param].df_name,
249 248 defaults[param].df_min, defaults[param].df_max,
250 249 defaults[param].df_default);
251 250 return (atoi(defaults[param].df_default));
252 251 }
253 252
254 253 /*
255 254 * df_get_bool(): gets the boolean value of a given user-tunable parameter
256 255 *
257 256 * input: const char *: the interface the parameter applies to
258 257 * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
259 258 * uint_t: the parameter number to look up
260 259 * output: boolean_t: B_TRUE if true, B_FALSE if false, default if not set
261 260 */
262 261
263 262 boolean_t
264 263 df_get_bool(const char *if_name, boolean_t isv6, uint_t param)
265 264 {
266 265 const char *value;
267 266
268 267 if (param >= (sizeof (defaults) / sizeof (*defaults)))
269 268 return (0);
270 269
271 270 value = df_get_string(if_name, isv6, param);
272 271 if (value != NULL) {
273 272
274 273 if (strcasecmp(value, "true") == 0 ||
275 274 strcasecmp(value, "yes") == 0 || strcmp(value, "1") == 0)
276 275 return (B_TRUE);
277 276
278 277 if (strcasecmp(value, "false") == 0 ||
279 278 strcasecmp(value, "no") == 0 || strcmp(value, "0") == 0)
280 279 return (B_FALSE);
281 280 }
282 281
283 282 dhcpmsg(MSG_WARNING, "df_get_bool: parameter `%s' has invalid value "
284 283 "`%s', defaulting to `%s'", defaults[param].df_name,
285 284 value != NULL ? value : "NULL", defaults[param].df_default);
286 285
287 286 return ((atoi(defaults[param].df_default) == 0) ? B_FALSE : B_TRUE);
288 287 }
↓ open down ↓ |
88 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX