1/*-
2 * Copyright (c) 1998 Michael Smith
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/kern/kern_environment.c 105354 2002-10-17 20:03:38Z robert $
27 */
28
29/*
30 * The unified bootloader passes us a pointer to a preserved copy of
31 * bootstrap/kernel environment variables. We convert them to a
32 * dynamic array of strings later when the VM subsystem is up.
33 *
34 * We make these available through the kenv(2) syscall for userland
35 * and through getenv()/freeenv() setenv() unsetenv() testenv() for
36 * the kernel.
37 */
38
39#include <sys/types.h>
40#include <sys/param.h>
41#include <sys/proc.h>
42#include <sys/queue.h>
43#include <sys/lock.h>
44#include <sys/malloc.h>
45#include <sys/mutex.h>
46#include <sys/kernel.h>
47#include <sys/sx.h>
48#include <sys/systm.h>
49#include <sys/sysent.h>
50#include <sys/sysproto.h>
51#include <sys/libkern.h>
52#include <sys/kenv.h>
53
54MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
55
56#define KENV_SIZE 512 /* Maximum number of environment strings */
57
58/* pointer to the static environment */
59char *kern_envp;
60static char *kernenv_next(char *);
61
62/* dynamic environment variables */
63char **kenvp;
64struct sx kenv_lock;
65
66/*
67 * No need to protect this with a mutex
68 * since SYSINITS are single threaded.
69 */
70int dynamic_kenv = 0;
71
72#define KENV_CHECK if (!dynamic_kenv) \
73 panic("%s: called before SI_SUB_KMEM", __func__)
74
75int
76kenv(td, uap)
77 struct thread *td;
78 struct kenv_args /* {
79 syscallarg(int) what;
80 syscallarg(const char *) name;
81 syscallarg(char *) value;
82 syscallarg(int) len;
83 } */ *uap;
84{
85 char *name, *value;
86 size_t len, done;
87 int error, i;
88
89 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
90
91 error = 0;
92 if (SCARG(uap, what) == KENV_DUMP) {
93 len = 0;
94 /* Return the size if called with a NULL buffer */
95 if (SCARG(uap, value) == NULL) {
96 sx_slock(&kenv_lock);
97 for (i = 0; kenvp[i] != NULL; i++)
98 len += strlen(kenvp[i]) + 1;
99 sx_sunlock(&kenv_lock);
100 td->td_retval[0] = len;
101 return (0);
102 }
103 done = 0;
104 sx_slock(&kenv_lock);
105 for (i = 0; kenvp[i] != NULL && done < SCARG(uap, len); i++) {
106 len = min(strlen(kenvp[i]) + 1, SCARG(uap, len) - done);
107 error = copyout(kenvp[i], SCARG(uap, value) + done,
108 len);
109 if (error) {
110 sx_sunlock(&kenv_lock);
111 return (error);
112 }
113 done += len;
114 }
115 sx_sunlock(&kenv_lock);
116 return (0);
117 }
118
119 if ((SCARG(uap, what) == KENV_SET) ||
120 (SCARG(uap, what) == KENV_UNSET)) {
121 error = suser(td);
122 if (error)
123 return (error);
124 }
125
126 name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
127
128 error = copyinstr(SCARG(uap, name), name, KENV_MNAMELEN, NULL);
129 if (error)
130 goto done;
131
132 switch (SCARG(uap, what)) {
133 case KENV_GET:
134 value = getenv(name);
135 if (value == NULL) {
136 error = ENOENT;
137 goto done;
138 }
139 len = strlen(value) + 1;
140 if (len > SCARG(uap, len))
141 len = SCARG(uap, len);
142 error = copyout(value, SCARG(uap, value), len);
143 freeenv(value);
144 if (error)
145 goto done;
146 td->td_retval[0] = len;
147 break;
148 case KENV_SET:
149 len = SCARG(uap, len);
150 if (len < 1) {
151 error = EINVAL;
152 goto done;
153 }
154 if (len > KENV_MVALLEN)
155 len = KENV_MVALLEN;
156 value = malloc(len, M_TEMP, M_WAITOK);
157 error = copyinstr(SCARG(uap, value), value, len, NULL);
158 if (error) {
159 free(value, M_TEMP);
160 goto done;
161 }
162 setenv(name, value);
163 free(value, M_TEMP);
164 break;
165 case KENV_UNSET:
166 error = unsetenv(name);
167 if (error)
168 error = ENOENT;
169 break;
170 default:
171 error = EINVAL;
172 break;
173 }
174done:
175 free(name, M_TEMP);
176 return (error);
177}
178
179/*
180 * Setup the dynamic kernel environment.
181 */
182static void
183init_dynamic_kenv(void *data __unused)
184{
185 char *cp;
186 int len, i;
187
188 kenvp = malloc(KENV_SIZE * sizeof(char *), M_KENV, M_WAITOK | M_ZERO);
189 i = 0;
190 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
191 len = strlen(cp) + 1;
192 kenvp[i] = malloc(len, M_KENV, M_WAITOK);
193 strcpy(kenvp[i++], cp);
194 }
195 kenvp[i] = NULL;
196
197 sx_init(&kenv_lock, "kernel environment");
198 dynamic_kenv = 1;
199}
200SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
201
202void
203freeenv(char *env)
204{
205
206 if (dynamic_kenv)
207 free(env, M_KENV);
208}
209
210/*
211 * Internal functions for string lookup.
212 */
213static char *
214_getenv_dynamic(const char *name, int *idx)
215{
216 char *cp;
217 int len, i;
218
219 sx_assert(&kenv_lock, SX_LOCKED);
220 len = strlen(name);
221 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
222 if ((cp[len] == '=') &&
223 (strncmp(cp, name, len) == 0)) {
224 if (idx != NULL)
225 *idx = i;
226 return (cp + len + 1);
227 }
228 }
229 return (NULL);
230}
231
232static char *
233_getenv_static(const char *name)
234{
235 char *cp, *ep;
236 int len;
237
238 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
239 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
240 ;
241 if (*ep != '=')
242 continue;
243 len = ep - cp;
244 ep++;
245 if (!strncmp(name, cp, len) && name[len] == 0)
246 return (ep);
247 }
248 return (NULL);
249}
250
251/*
252 * Look up an environment variable by name.
253 * Return a pointer to the string if found.
254 * The pointer has to be freed with freeenv()
255 * after use.
256 */
257char *
258getenv(const char *name)
259{
260 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
261 char *ret, *cp;
262 int len;
263
264 if (dynamic_kenv) {
265 sx_slock(&kenv_lock);
266 cp = _getenv_dynamic(name, NULL);
267 if (cp != NULL) {
268 strcpy(buf, cp);
269 sx_sunlock(&kenv_lock);
270 len = strlen(buf) + 1;
271 ret = malloc(len, M_KENV, M_WAITOK);
272 strcpy(ret, buf);
273 } else {
274 sx_sunlock(&kenv_lock);
275 ret = NULL;
276 }
277 } else
278 ret = _getenv_static(name);
279 return (ret);
280}
281
282/*
283 * Test if an environment variable is defined.
284 */
285int
286testenv(const char *name)
287{
288 char *cp;
289
290 if (dynamic_kenv) {
291 sx_slock(&kenv_lock);
292 cp = _getenv_dynamic(name, NULL);
293 sx_sunlock(&kenv_lock);
294 } else
295 cp = _getenv_static(name);
296 if (cp != NULL)
297 return (1);
298 return (0);
299}
300
301/*
302 * Set an environment variable by name.
303 */
304int
305setenv(const char *name, const char *value)
306{
307 char *buf, *cp, *oldenv;
308 int namelen, vallen, i;
309
310 KENV_CHECK;
311
312 namelen = strlen(name) + 1;
313 if (namelen > KENV_MNAMELEN)
314 return (-1);
315 vallen = strlen(value) + 1;
316 if (vallen > KENV_MVALLEN)
317 return (-1);
318 buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
319 sprintf(buf, "%s=%s", name, value);
320
321 sx_xlock(&kenv_lock);
322 cp = _getenv_dynamic(name, &i);
323 if (cp != NULL) {
324 oldenv = kenvp[i];
325 kenvp[i] = buf;
326 sx_xunlock(&kenv_lock);
327 free(oldenv, M_KENV);
328 } else {
329 /* We add the option if it wasn't found */
330 for (i = 0; (cp = kenvp[i]) != NULL; i++)
331 ;
332 kenvp[i] = buf;
333 kenvp[i + 1] = NULL;
334 sx_xunlock(&kenv_lock);
335 }
336 return (0);
337}
338
339/*
340 * Unset an environment variable string.
341 */
342int
343unsetenv(const char *name)
344{
345 char *cp, *oldenv;
346 int i, j;
347
348 KENV_CHECK;
349
350 sx_xlock(&kenv_lock);
351 cp = _getenv_dynamic(name, &i);
352 if (cp != NULL) {
353 oldenv = kenvp[i];
354 for (j = i + 1; kenvp[j] != NULL; j++)
355 kenvp[i++] = kenvp[j];
356 kenvp[i] = NULL;
357 sx_xunlock(&kenv_lock);
358 free(oldenv, M_KENV);
359 return (0);
360 }
361 sx_xunlock(&kenv_lock);
362 return (-1);
363}
364
365/*
366 * Return a string value from an environment variable.
367 */
368int
369getenv_string(const char *name, char *data, int size)
370{
371 char *tmp;
372
373 tmp = getenv(name);
374 if (tmp != NULL) {
375 strlcpy(data, tmp, size);
376 freeenv(tmp);
377 return (1);
378 } else
379 return (0);
380}
381
382/*
383 * Return an integer value from an environment variable.
384 */
385int
386getenv_int(const char *name, int *data)
387{
388 quad_t tmp;
389 int rval;
390
391 rval = getenv_quad(name, &tmp);
392 if (rval)
393 *data = (int) tmp;
394 return (rval);
395}
396
397/*
398 * Return a quad_t value from an environment variable.
399 */
400int
401getenv_quad(const char *name, quad_t *data)
402{
403 char *value;
404 char *vtp;
405 quad_t iv;
406
407 value = getenv(name);
408 if (value == NULL)
409 return (0);
410 iv = strtoq(value, &vtp, 0);
411 if ((vtp == value) || (*vtp != '\0')) {
412 freeenv(value);
413 return (0);
414 }
415 freeenv(value);
416 *data = iv;
417 return (1);
418}
419
420/*
421 * Find the next entry after the one which (cp) falls within, return a
422 * pointer to its start or NULL if there are no more.
423 */
424static char *
425kernenv_next(char *cp)
426{
427
428 if (cp != NULL) {
429 while (*cp != 0)
430 cp++;
431 cp++;
432 if (*cp == 0)
433 cp = NULL;
434 }
435 return (cp);
436}
437
438void
439tunable_int_init(void *data)
440{
441 struct tunable_int *d = (struct tunable_int *)data;
442
443 TUNABLE_INT_FETCH(d->path, d->var);
444}
445
446void
447tunable_quad_init(void *data)
448{
449 struct tunable_quad *d = (struct tunable_quad *)data;
450
451 TUNABLE_QUAD_FETCH(d->path, d->var);
452}
453
454void
455tunable_str_init(void *data)
456{
457 struct tunable_str *d = (struct tunable_str *)data;
458
459 TUNABLE_STR_FETCH(d->path, d->var, d->size);
460}
2 * Copyright (c) 1998 Michael Smith
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/kern/kern_environment.c 105354 2002-10-17 20:03:38Z robert $
27 */
28
29/*
30 * The unified bootloader passes us a pointer to a preserved copy of
31 * bootstrap/kernel environment variables. We convert them to a
32 * dynamic array of strings later when the VM subsystem is up.
33 *
34 * We make these available through the kenv(2) syscall for userland
35 * and through getenv()/freeenv() setenv() unsetenv() testenv() for
36 * the kernel.
37 */
38
39#include <sys/types.h>
40#include <sys/param.h>
41#include <sys/proc.h>
42#include <sys/queue.h>
43#include <sys/lock.h>
44#include <sys/malloc.h>
45#include <sys/mutex.h>
46#include <sys/kernel.h>
47#include <sys/sx.h>
48#include <sys/systm.h>
49#include <sys/sysent.h>
50#include <sys/sysproto.h>
51#include <sys/libkern.h>
52#include <sys/kenv.h>
53
54MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
55
56#define KENV_SIZE 512 /* Maximum number of environment strings */
57
58/* pointer to the static environment */
59char *kern_envp;
60static char *kernenv_next(char *);
61
62/* dynamic environment variables */
63char **kenvp;
64struct sx kenv_lock;
65
66/*
67 * No need to protect this with a mutex
68 * since SYSINITS are single threaded.
69 */
70int dynamic_kenv = 0;
71
72#define KENV_CHECK if (!dynamic_kenv) \
73 panic("%s: called before SI_SUB_KMEM", __func__)
74
75int
76kenv(td, uap)
77 struct thread *td;
78 struct kenv_args /* {
79 syscallarg(int) what;
80 syscallarg(const char *) name;
81 syscallarg(char *) value;
82 syscallarg(int) len;
83 } */ *uap;
84{
85 char *name, *value;
86 size_t len, done;
87 int error, i;
88
89 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
90
91 error = 0;
92 if (SCARG(uap, what) == KENV_DUMP) {
93 len = 0;
94 /* Return the size if called with a NULL buffer */
95 if (SCARG(uap, value) == NULL) {
96 sx_slock(&kenv_lock);
97 for (i = 0; kenvp[i] != NULL; i++)
98 len += strlen(kenvp[i]) + 1;
99 sx_sunlock(&kenv_lock);
100 td->td_retval[0] = len;
101 return (0);
102 }
103 done = 0;
104 sx_slock(&kenv_lock);
105 for (i = 0; kenvp[i] != NULL && done < SCARG(uap, len); i++) {
106 len = min(strlen(kenvp[i]) + 1, SCARG(uap, len) - done);
107 error = copyout(kenvp[i], SCARG(uap, value) + done,
108 len);
109 if (error) {
110 sx_sunlock(&kenv_lock);
111 return (error);
112 }
113 done += len;
114 }
115 sx_sunlock(&kenv_lock);
116 return (0);
117 }
118
119 if ((SCARG(uap, what) == KENV_SET) ||
120 (SCARG(uap, what) == KENV_UNSET)) {
121 error = suser(td);
122 if (error)
123 return (error);
124 }
125
126 name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
127
128 error = copyinstr(SCARG(uap, name), name, KENV_MNAMELEN, NULL);
129 if (error)
130 goto done;
131
132 switch (SCARG(uap, what)) {
133 case KENV_GET:
134 value = getenv(name);
135 if (value == NULL) {
136 error = ENOENT;
137 goto done;
138 }
139 len = strlen(value) + 1;
140 if (len > SCARG(uap, len))
141 len = SCARG(uap, len);
142 error = copyout(value, SCARG(uap, value), len);
143 freeenv(value);
144 if (error)
145 goto done;
146 td->td_retval[0] = len;
147 break;
148 case KENV_SET:
149 len = SCARG(uap, len);
150 if (len < 1) {
151 error = EINVAL;
152 goto done;
153 }
154 if (len > KENV_MVALLEN)
155 len = KENV_MVALLEN;
156 value = malloc(len, M_TEMP, M_WAITOK);
157 error = copyinstr(SCARG(uap, value), value, len, NULL);
158 if (error) {
159 free(value, M_TEMP);
160 goto done;
161 }
162 setenv(name, value);
163 free(value, M_TEMP);
164 break;
165 case KENV_UNSET:
166 error = unsetenv(name);
167 if (error)
168 error = ENOENT;
169 break;
170 default:
171 error = EINVAL;
172 break;
173 }
174done:
175 free(name, M_TEMP);
176 return (error);
177}
178
179/*
180 * Setup the dynamic kernel environment.
181 */
182static void
183init_dynamic_kenv(void *data __unused)
184{
185 char *cp;
186 int len, i;
187
188 kenvp = malloc(KENV_SIZE * sizeof(char *), M_KENV, M_WAITOK | M_ZERO);
189 i = 0;
190 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
191 len = strlen(cp) + 1;
192 kenvp[i] = malloc(len, M_KENV, M_WAITOK);
193 strcpy(kenvp[i++], cp);
194 }
195 kenvp[i] = NULL;
196
197 sx_init(&kenv_lock, "kernel environment");
198 dynamic_kenv = 1;
199}
200SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
201
202void
203freeenv(char *env)
204{
205
206 if (dynamic_kenv)
207 free(env, M_KENV);
208}
209
210/*
211 * Internal functions for string lookup.
212 */
213static char *
214_getenv_dynamic(const char *name, int *idx)
215{
216 char *cp;
217 int len, i;
218
219 sx_assert(&kenv_lock, SX_LOCKED);
220 len = strlen(name);
221 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
222 if ((cp[len] == '=') &&
223 (strncmp(cp, name, len) == 0)) {
224 if (idx != NULL)
225 *idx = i;
226 return (cp + len + 1);
227 }
228 }
229 return (NULL);
230}
231
232static char *
233_getenv_static(const char *name)
234{
235 char *cp, *ep;
236 int len;
237
238 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
239 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
240 ;
241 if (*ep != '=')
242 continue;
243 len = ep - cp;
244 ep++;
245 if (!strncmp(name, cp, len) && name[len] == 0)
246 return (ep);
247 }
248 return (NULL);
249}
250
251/*
252 * Look up an environment variable by name.
253 * Return a pointer to the string if found.
254 * The pointer has to be freed with freeenv()
255 * after use.
256 */
257char *
258getenv(const char *name)
259{
260 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
261 char *ret, *cp;
262 int len;
263
264 if (dynamic_kenv) {
265 sx_slock(&kenv_lock);
266 cp = _getenv_dynamic(name, NULL);
267 if (cp != NULL) {
268 strcpy(buf, cp);
269 sx_sunlock(&kenv_lock);
270 len = strlen(buf) + 1;
271 ret = malloc(len, M_KENV, M_WAITOK);
272 strcpy(ret, buf);
273 } else {
274 sx_sunlock(&kenv_lock);
275 ret = NULL;
276 }
277 } else
278 ret = _getenv_static(name);
279 return (ret);
280}
281
282/*
283 * Test if an environment variable is defined.
284 */
285int
286testenv(const char *name)
287{
288 char *cp;
289
290 if (dynamic_kenv) {
291 sx_slock(&kenv_lock);
292 cp = _getenv_dynamic(name, NULL);
293 sx_sunlock(&kenv_lock);
294 } else
295 cp = _getenv_static(name);
296 if (cp != NULL)
297 return (1);
298 return (0);
299}
300
301/*
302 * Set an environment variable by name.
303 */
304int
305setenv(const char *name, const char *value)
306{
307 char *buf, *cp, *oldenv;
308 int namelen, vallen, i;
309
310 KENV_CHECK;
311
312 namelen = strlen(name) + 1;
313 if (namelen > KENV_MNAMELEN)
314 return (-1);
315 vallen = strlen(value) + 1;
316 if (vallen > KENV_MVALLEN)
317 return (-1);
318 buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
319 sprintf(buf, "%s=%s", name, value);
320
321 sx_xlock(&kenv_lock);
322 cp = _getenv_dynamic(name, &i);
323 if (cp != NULL) {
324 oldenv = kenvp[i];
325 kenvp[i] = buf;
326 sx_xunlock(&kenv_lock);
327 free(oldenv, M_KENV);
328 } else {
329 /* We add the option if it wasn't found */
330 for (i = 0; (cp = kenvp[i]) != NULL; i++)
331 ;
332 kenvp[i] = buf;
333 kenvp[i + 1] = NULL;
334 sx_xunlock(&kenv_lock);
335 }
336 return (0);
337}
338
339/*
340 * Unset an environment variable string.
341 */
342int
343unsetenv(const char *name)
344{
345 char *cp, *oldenv;
346 int i, j;
347
348 KENV_CHECK;
349
350 sx_xlock(&kenv_lock);
351 cp = _getenv_dynamic(name, &i);
352 if (cp != NULL) {
353 oldenv = kenvp[i];
354 for (j = i + 1; kenvp[j] != NULL; j++)
355 kenvp[i++] = kenvp[j];
356 kenvp[i] = NULL;
357 sx_xunlock(&kenv_lock);
358 free(oldenv, M_KENV);
359 return (0);
360 }
361 sx_xunlock(&kenv_lock);
362 return (-1);
363}
364
365/*
366 * Return a string value from an environment variable.
367 */
368int
369getenv_string(const char *name, char *data, int size)
370{
371 char *tmp;
372
373 tmp = getenv(name);
374 if (tmp != NULL) {
375 strlcpy(data, tmp, size);
376 freeenv(tmp);
377 return (1);
378 } else
379 return (0);
380}
381
382/*
383 * Return an integer value from an environment variable.
384 */
385int
386getenv_int(const char *name, int *data)
387{
388 quad_t tmp;
389 int rval;
390
391 rval = getenv_quad(name, &tmp);
392 if (rval)
393 *data = (int) tmp;
394 return (rval);
395}
396
397/*
398 * Return a quad_t value from an environment variable.
399 */
400int
401getenv_quad(const char *name, quad_t *data)
402{
403 char *value;
404 char *vtp;
405 quad_t iv;
406
407 value = getenv(name);
408 if (value == NULL)
409 return (0);
410 iv = strtoq(value, &vtp, 0);
411 if ((vtp == value) || (*vtp != '\0')) {
412 freeenv(value);
413 return (0);
414 }
415 freeenv(value);
416 *data = iv;
417 return (1);
418}
419
420/*
421 * Find the next entry after the one which (cp) falls within, return a
422 * pointer to its start or NULL if there are no more.
423 */
424static char *
425kernenv_next(char *cp)
426{
427
428 if (cp != NULL) {
429 while (*cp != 0)
430 cp++;
431 cp++;
432 if (*cp == 0)
433 cp = NULL;
434 }
435 return (cp);
436}
437
438void
439tunable_int_init(void *data)
440{
441 struct tunable_int *d = (struct tunable_int *)data;
442
443 TUNABLE_INT_FETCH(d->path, d->var);
444}
445
446void
447tunable_quad_init(void *data)
448{
449 struct tunable_quad *d = (struct tunable_quad *)data;
450
451 TUNABLE_QUAD_FETCH(d->path, d->var);
452}
453
454void
455tunable_str_init(void *data)
456{
457 struct tunable_str *d = (struct tunable_str *)data;
458
459 TUNABLE_STR_FETCH(d->path, d->var, d->size);
460}