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.
| 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.
|
39#include "opt_mac.h" 40 41#include <sys/types.h> 42#include <sys/param.h> 43#include <sys/proc.h> 44#include <sys/queue.h> 45#include <sys/lock.h> 46#include <sys/mac.h> 47#include <sys/malloc.h> 48#include <sys/mutex.h> 49#include <sys/kernel.h> 50#include <sys/sx.h> 51#include <sys/systm.h> 52#include <sys/sysent.h> 53#include <sys/sysproto.h> 54#include <sys/libkern.h> 55#include <sys/kenv.h> 56 57MALLOC_DEFINE(M_KENV, "kenv", "kernel environment"); 58 59#define KENV_SIZE 512 /* Maximum number of environment strings */ 60 61/* pointer to the static environment */ 62char *kern_envp; 63static char *kernenv_next(char *); 64 65/* dynamic environment variables */ 66char **kenvp; 67struct sx kenv_lock; 68 69/* 70 * No need to protect this with a mutex 71 * since SYSINITS are single threaded. 72 */ 73int dynamic_kenv = 0; 74 75#define KENV_CHECK if (!dynamic_kenv) \ 76 panic("%s: called before SI_SUB_KMEM", __func__) 77 78int 79kenv(td, uap) 80 struct thread *td; 81 struct kenv_args /* { 82 int what; 83 const char *name; 84 char *value; 85 int len; 86 } */ *uap; 87{ 88 char *name, *value; 89 size_t len, done; 90 int error, i; 91 92 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0")); 93 94 error = 0; 95 if (uap->what == KENV_DUMP) { 96#ifdef MAC 97 error = mac_check_kenv_dump(td->td_ucred); 98 if (error) 99 return (error); 100#endif 101 len = 0; 102 /* Return the size if called with a NULL buffer */ 103 if (uap->value == NULL) { 104 sx_slock(&kenv_lock); 105 for (i = 0; kenvp[i] != NULL; i++) 106 len += strlen(kenvp[i]) + 1; 107 sx_sunlock(&kenv_lock); 108 td->td_retval[0] = len; 109 return (0); 110 } 111 done = 0; 112 sx_slock(&kenv_lock); 113 for (i = 0; kenvp[i] != NULL && done < uap->len; i++) { 114 len = min(strlen(kenvp[i]) + 1, uap->len - done); 115 error = copyout(kenvp[i], uap->value + done, 116 len); 117 if (error) { 118 sx_sunlock(&kenv_lock); 119 return (error); 120 } 121 done += len; 122 } 123 sx_sunlock(&kenv_lock); 124 return (0); 125 } 126 127 if ((uap->what == KENV_SET) || 128 (uap->what == KENV_UNSET)) { 129 error = suser(td); 130 if (error) 131 return (error); 132 } 133 134 name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK); 135 136 error = copyinstr(uap->name, name, KENV_MNAMELEN, NULL); 137 if (error) 138 goto done; 139 140 switch (uap->what) { 141 case KENV_GET: 142#ifdef MAC 143 error = mac_check_kenv_get(td->td_ucred, name); 144 if (error) 145 goto done; 146#endif 147 value = getenv(name); 148 if (value == NULL) { 149 error = ENOENT; 150 goto done; 151 } 152 len = strlen(value) + 1; 153 if (len > uap->len) 154 len = uap->len; 155 error = copyout(value, uap->value, len); 156 freeenv(value); 157 if (error) 158 goto done; 159 td->td_retval[0] = len; 160 break; 161 case KENV_SET: 162 len = uap->len; 163 if (len < 1) { 164 error = EINVAL; 165 goto done; 166 } 167 if (len > KENV_MVALLEN) 168 len = KENV_MVALLEN; 169 value = malloc(len, M_TEMP, M_WAITOK); 170 error = copyinstr(uap->value, value, len, NULL); 171 if (error) { 172 free(value, M_TEMP); 173 goto done; 174 } 175#ifdef MAC 176 error = mac_check_kenv_set(td->td_ucred, name, value); 177 if (error == 0) 178#endif 179 setenv(name, value); 180 free(value, M_TEMP); 181 break; 182 case KENV_UNSET: 183#ifdef MAC 184 error = mac_check_kenv_unset(td->td_ucred, name); 185 if (error) 186 goto done; 187#endif 188 error = unsetenv(name); 189 if (error) 190 error = ENOENT; 191 break; 192 default: 193 error = EINVAL; 194 break; 195 } 196done: 197 free(name, M_TEMP); 198 return (error); 199} 200 201/* 202 * Setup the dynamic kernel environment. 203 */ 204static void 205init_dynamic_kenv(void *data __unused) 206{ 207 char *cp; 208 int len, i; 209 210 kenvp = malloc(KENV_SIZE * sizeof(char *), M_KENV, M_WAITOK | M_ZERO); 211 i = 0; 212 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) { 213 len = strlen(cp) + 1; 214 kenvp[i] = malloc(len, M_KENV, M_WAITOK); 215 strcpy(kenvp[i++], cp); 216 } 217 kenvp[i] = NULL; 218 219 sx_init(&kenv_lock, "kernel environment"); 220 dynamic_kenv = 1; 221} 222SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL); 223 224void 225freeenv(char *env) 226{ 227 228 if (dynamic_kenv) 229 free(env, M_KENV); 230} 231 232/* 233 * Internal functions for string lookup. 234 */ 235static char * 236_getenv_dynamic(const char *name, int *idx) 237{ 238 char *cp; 239 int len, i; 240 241 sx_assert(&kenv_lock, SX_LOCKED); 242 len = strlen(name); 243 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) { 244 if ((cp[len] == '=') && 245 (strncmp(cp, name, len) == 0)) { 246 if (idx != NULL) 247 *idx = i; 248 return (cp + len + 1); 249 } 250 } 251 return (NULL); 252} 253 254static char * 255_getenv_static(const char *name) 256{ 257 char *cp, *ep; 258 int len; 259 260 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) { 261 for (ep = cp; (*ep != '=') && (*ep != 0); ep++) 262 ; 263 if (*ep != '=') 264 continue; 265 len = ep - cp; 266 ep++; 267 if (!strncmp(name, cp, len) && name[len] == 0) 268 return (ep); 269 } 270 return (NULL); 271} 272 273/* 274 * Look up an environment variable by name. 275 * Return a pointer to the string if found. 276 * The pointer has to be freed with freeenv() 277 * after use. 278 */ 279char * 280getenv(const char *name) 281{ 282 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1]; 283 char *ret, *cp; 284 int len; 285 286 if (dynamic_kenv) { 287 sx_slock(&kenv_lock); 288 cp = _getenv_dynamic(name, NULL); 289 if (cp != NULL) { 290 strcpy(buf, cp); 291 sx_sunlock(&kenv_lock); 292 len = strlen(buf) + 1; 293 ret = malloc(len, M_KENV, M_WAITOK); 294 strcpy(ret, buf); 295 } else { 296 sx_sunlock(&kenv_lock); 297 ret = NULL; 298 } 299 } else 300 ret = _getenv_static(name); 301 return (ret); 302} 303 304/* 305 * Test if an environment variable is defined. 306 */ 307int 308testenv(const char *name) 309{ 310 char *cp; 311 312 if (dynamic_kenv) { 313 sx_slock(&kenv_lock); 314 cp = _getenv_dynamic(name, NULL); 315 sx_sunlock(&kenv_lock); 316 } else 317 cp = _getenv_static(name); 318 if (cp != NULL) 319 return (1); 320 return (0); 321} 322 323/* 324 * Set an environment variable by name. 325 */ 326int 327setenv(const char *name, const char *value) 328{ 329 char *buf, *cp, *oldenv; 330 int namelen, vallen, i; 331 332 KENV_CHECK; 333 334 namelen = strlen(name) + 1; 335 if (namelen > KENV_MNAMELEN) 336 return (-1); 337 vallen = strlen(value) + 1; 338 if (vallen > KENV_MVALLEN) 339 return (-1); 340 buf = malloc(namelen + vallen, M_KENV, M_WAITOK); 341 sprintf(buf, "%s=%s", name, value); 342 343 sx_xlock(&kenv_lock); 344 cp = _getenv_dynamic(name, &i); 345 if (cp != NULL) { 346 oldenv = kenvp[i]; 347 kenvp[i] = buf; 348 sx_xunlock(&kenv_lock); 349 free(oldenv, M_KENV); 350 } else { 351 /* We add the option if it wasn't found */ 352 for (i = 0; (cp = kenvp[i]) != NULL; i++) 353 ; 354 kenvp[i] = buf; 355 kenvp[i + 1] = NULL; 356 sx_xunlock(&kenv_lock); 357 } 358 return (0); 359} 360 361/* 362 * Unset an environment variable string. 363 */ 364int 365unsetenv(const char *name) 366{ 367 char *cp, *oldenv; 368 int i, j; 369 370 KENV_CHECK; 371 372 sx_xlock(&kenv_lock); 373 cp = _getenv_dynamic(name, &i); 374 if (cp != NULL) { 375 oldenv = kenvp[i]; 376 for (j = i + 1; kenvp[j] != NULL; j++) 377 kenvp[i++] = kenvp[j]; 378 kenvp[i] = NULL; 379 sx_xunlock(&kenv_lock); 380 free(oldenv, M_KENV); 381 return (0); 382 } 383 sx_xunlock(&kenv_lock); 384 return (-1); 385} 386 387/* 388 * Return a string value from an environment variable. 389 */ 390int 391getenv_string(const char *name, char *data, int size) 392{ 393 char *tmp; 394 395 tmp = getenv(name); 396 if (tmp != NULL) { 397 strlcpy(data, tmp, size); 398 freeenv(tmp); 399 return (1); 400 } else 401 return (0); 402} 403 404/* 405 * Return an integer value from an environment variable. 406 */ 407int 408getenv_int(const char *name, int *data) 409{ 410 quad_t tmp; 411 int rval; 412 413 rval = getenv_quad(name, &tmp); 414 if (rval) 415 *data = (int) tmp; 416 return (rval); 417} 418 419/* 420 * Return a quad_t value from an environment variable. 421 */ 422int 423getenv_quad(const char *name, quad_t *data) 424{ 425 char *value; 426 char *vtp; 427 quad_t iv; 428 429 value = getenv(name); 430 if (value == NULL) 431 return (0); 432 iv = strtoq(value, &vtp, 0); 433 if ((vtp == value) || (*vtp != '\0')) { 434 freeenv(value); 435 return (0); 436 } 437 freeenv(value); 438 *data = iv; 439 return (1); 440} 441 442/* 443 * Find the next entry after the one which (cp) falls within, return a 444 * pointer to its start or NULL if there are no more. 445 */ 446static char * 447kernenv_next(char *cp) 448{ 449 450 if (cp != NULL) { 451 while (*cp != 0) 452 cp++; 453 cp++; 454 if (*cp == 0) 455 cp = NULL; 456 } 457 return (cp); 458} 459 460void 461tunable_int_init(void *data) 462{ 463 struct tunable_int *d = (struct tunable_int *)data; 464 465 TUNABLE_INT_FETCH(d->path, d->var); 466} 467 468void 469tunable_quad_init(void *data) 470{ 471 struct tunable_quad *d = (struct tunable_quad *)data; 472 473 TUNABLE_QUAD_FETCH(d->path, d->var); 474} 475 476void 477tunable_str_init(void *data) 478{ 479 struct tunable_str *d = (struct tunable_str *)data; 480 481 TUNABLE_STR_FETCH(d->path, d->var, d->size); 482}
| 40#include "opt_mac.h" 41 42#include <sys/types.h> 43#include <sys/param.h> 44#include <sys/proc.h> 45#include <sys/queue.h> 46#include <sys/lock.h> 47#include <sys/mac.h> 48#include <sys/malloc.h> 49#include <sys/mutex.h> 50#include <sys/kernel.h> 51#include <sys/sx.h> 52#include <sys/systm.h> 53#include <sys/sysent.h> 54#include <sys/sysproto.h> 55#include <sys/libkern.h> 56#include <sys/kenv.h> 57 58MALLOC_DEFINE(M_KENV, "kenv", "kernel environment"); 59 60#define KENV_SIZE 512 /* Maximum number of environment strings */ 61 62/* pointer to the static environment */ 63char *kern_envp; 64static char *kernenv_next(char *); 65 66/* dynamic environment variables */ 67char **kenvp; 68struct sx kenv_lock; 69 70/* 71 * No need to protect this with a mutex 72 * since SYSINITS are single threaded. 73 */ 74int dynamic_kenv = 0; 75 76#define KENV_CHECK if (!dynamic_kenv) \ 77 panic("%s: called before SI_SUB_KMEM", __func__) 78 79int 80kenv(td, uap) 81 struct thread *td; 82 struct kenv_args /* { 83 int what; 84 const char *name; 85 char *value; 86 int len; 87 } */ *uap; 88{ 89 char *name, *value; 90 size_t len, done; 91 int error, i; 92 93 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0")); 94 95 error = 0; 96 if (uap->what == KENV_DUMP) { 97#ifdef MAC 98 error = mac_check_kenv_dump(td->td_ucred); 99 if (error) 100 return (error); 101#endif 102 len = 0; 103 /* Return the size if called with a NULL buffer */ 104 if (uap->value == NULL) { 105 sx_slock(&kenv_lock); 106 for (i = 0; kenvp[i] != NULL; i++) 107 len += strlen(kenvp[i]) + 1; 108 sx_sunlock(&kenv_lock); 109 td->td_retval[0] = len; 110 return (0); 111 } 112 done = 0; 113 sx_slock(&kenv_lock); 114 for (i = 0; kenvp[i] != NULL && done < uap->len; i++) { 115 len = min(strlen(kenvp[i]) + 1, uap->len - done); 116 error = copyout(kenvp[i], uap->value + done, 117 len); 118 if (error) { 119 sx_sunlock(&kenv_lock); 120 return (error); 121 } 122 done += len; 123 } 124 sx_sunlock(&kenv_lock); 125 return (0); 126 } 127 128 if ((uap->what == KENV_SET) || 129 (uap->what == KENV_UNSET)) { 130 error = suser(td); 131 if (error) 132 return (error); 133 } 134 135 name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK); 136 137 error = copyinstr(uap->name, name, KENV_MNAMELEN, NULL); 138 if (error) 139 goto done; 140 141 switch (uap->what) { 142 case KENV_GET: 143#ifdef MAC 144 error = mac_check_kenv_get(td->td_ucred, name); 145 if (error) 146 goto done; 147#endif 148 value = getenv(name); 149 if (value == NULL) { 150 error = ENOENT; 151 goto done; 152 } 153 len = strlen(value) + 1; 154 if (len > uap->len) 155 len = uap->len; 156 error = copyout(value, uap->value, len); 157 freeenv(value); 158 if (error) 159 goto done; 160 td->td_retval[0] = len; 161 break; 162 case KENV_SET: 163 len = uap->len; 164 if (len < 1) { 165 error = EINVAL; 166 goto done; 167 } 168 if (len > KENV_MVALLEN) 169 len = KENV_MVALLEN; 170 value = malloc(len, M_TEMP, M_WAITOK); 171 error = copyinstr(uap->value, value, len, NULL); 172 if (error) { 173 free(value, M_TEMP); 174 goto done; 175 } 176#ifdef MAC 177 error = mac_check_kenv_set(td->td_ucred, name, value); 178 if (error == 0) 179#endif 180 setenv(name, value); 181 free(value, M_TEMP); 182 break; 183 case KENV_UNSET: 184#ifdef MAC 185 error = mac_check_kenv_unset(td->td_ucred, name); 186 if (error) 187 goto done; 188#endif 189 error = unsetenv(name); 190 if (error) 191 error = ENOENT; 192 break; 193 default: 194 error = EINVAL; 195 break; 196 } 197done: 198 free(name, M_TEMP); 199 return (error); 200} 201 202/* 203 * Setup the dynamic kernel environment. 204 */ 205static void 206init_dynamic_kenv(void *data __unused) 207{ 208 char *cp; 209 int len, i; 210 211 kenvp = malloc(KENV_SIZE * sizeof(char *), M_KENV, M_WAITOK | M_ZERO); 212 i = 0; 213 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) { 214 len = strlen(cp) + 1; 215 kenvp[i] = malloc(len, M_KENV, M_WAITOK); 216 strcpy(kenvp[i++], cp); 217 } 218 kenvp[i] = NULL; 219 220 sx_init(&kenv_lock, "kernel environment"); 221 dynamic_kenv = 1; 222} 223SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL); 224 225void 226freeenv(char *env) 227{ 228 229 if (dynamic_kenv) 230 free(env, M_KENV); 231} 232 233/* 234 * Internal functions for string lookup. 235 */ 236static char * 237_getenv_dynamic(const char *name, int *idx) 238{ 239 char *cp; 240 int len, i; 241 242 sx_assert(&kenv_lock, SX_LOCKED); 243 len = strlen(name); 244 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) { 245 if ((cp[len] == '=') && 246 (strncmp(cp, name, len) == 0)) { 247 if (idx != NULL) 248 *idx = i; 249 return (cp + len + 1); 250 } 251 } 252 return (NULL); 253} 254 255static char * 256_getenv_static(const char *name) 257{ 258 char *cp, *ep; 259 int len; 260 261 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) { 262 for (ep = cp; (*ep != '=') && (*ep != 0); ep++) 263 ; 264 if (*ep != '=') 265 continue; 266 len = ep - cp; 267 ep++; 268 if (!strncmp(name, cp, len) && name[len] == 0) 269 return (ep); 270 } 271 return (NULL); 272} 273 274/* 275 * Look up an environment variable by name. 276 * Return a pointer to the string if found. 277 * The pointer has to be freed with freeenv() 278 * after use. 279 */ 280char * 281getenv(const char *name) 282{ 283 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1]; 284 char *ret, *cp; 285 int len; 286 287 if (dynamic_kenv) { 288 sx_slock(&kenv_lock); 289 cp = _getenv_dynamic(name, NULL); 290 if (cp != NULL) { 291 strcpy(buf, cp); 292 sx_sunlock(&kenv_lock); 293 len = strlen(buf) + 1; 294 ret = malloc(len, M_KENV, M_WAITOK); 295 strcpy(ret, buf); 296 } else { 297 sx_sunlock(&kenv_lock); 298 ret = NULL; 299 } 300 } else 301 ret = _getenv_static(name); 302 return (ret); 303} 304 305/* 306 * Test if an environment variable is defined. 307 */ 308int 309testenv(const char *name) 310{ 311 char *cp; 312 313 if (dynamic_kenv) { 314 sx_slock(&kenv_lock); 315 cp = _getenv_dynamic(name, NULL); 316 sx_sunlock(&kenv_lock); 317 } else 318 cp = _getenv_static(name); 319 if (cp != NULL) 320 return (1); 321 return (0); 322} 323 324/* 325 * Set an environment variable by name. 326 */ 327int 328setenv(const char *name, const char *value) 329{ 330 char *buf, *cp, *oldenv; 331 int namelen, vallen, i; 332 333 KENV_CHECK; 334 335 namelen = strlen(name) + 1; 336 if (namelen > KENV_MNAMELEN) 337 return (-1); 338 vallen = strlen(value) + 1; 339 if (vallen > KENV_MVALLEN) 340 return (-1); 341 buf = malloc(namelen + vallen, M_KENV, M_WAITOK); 342 sprintf(buf, "%s=%s", name, value); 343 344 sx_xlock(&kenv_lock); 345 cp = _getenv_dynamic(name, &i); 346 if (cp != NULL) { 347 oldenv = kenvp[i]; 348 kenvp[i] = buf; 349 sx_xunlock(&kenv_lock); 350 free(oldenv, M_KENV); 351 } else { 352 /* We add the option if it wasn't found */ 353 for (i = 0; (cp = kenvp[i]) != NULL; i++) 354 ; 355 kenvp[i] = buf; 356 kenvp[i + 1] = NULL; 357 sx_xunlock(&kenv_lock); 358 } 359 return (0); 360} 361 362/* 363 * Unset an environment variable string. 364 */ 365int 366unsetenv(const char *name) 367{ 368 char *cp, *oldenv; 369 int i, j; 370 371 KENV_CHECK; 372 373 sx_xlock(&kenv_lock); 374 cp = _getenv_dynamic(name, &i); 375 if (cp != NULL) { 376 oldenv = kenvp[i]; 377 for (j = i + 1; kenvp[j] != NULL; j++) 378 kenvp[i++] = kenvp[j]; 379 kenvp[i] = NULL; 380 sx_xunlock(&kenv_lock); 381 free(oldenv, M_KENV); 382 return (0); 383 } 384 sx_xunlock(&kenv_lock); 385 return (-1); 386} 387 388/* 389 * Return a string value from an environment variable. 390 */ 391int 392getenv_string(const char *name, char *data, int size) 393{ 394 char *tmp; 395 396 tmp = getenv(name); 397 if (tmp != NULL) { 398 strlcpy(data, tmp, size); 399 freeenv(tmp); 400 return (1); 401 } else 402 return (0); 403} 404 405/* 406 * Return an integer value from an environment variable. 407 */ 408int 409getenv_int(const char *name, int *data) 410{ 411 quad_t tmp; 412 int rval; 413 414 rval = getenv_quad(name, &tmp); 415 if (rval) 416 *data = (int) tmp; 417 return (rval); 418} 419 420/* 421 * Return a quad_t value from an environment variable. 422 */ 423int 424getenv_quad(const char *name, quad_t *data) 425{ 426 char *value; 427 char *vtp; 428 quad_t iv; 429 430 value = getenv(name); 431 if (value == NULL) 432 return (0); 433 iv = strtoq(value, &vtp, 0); 434 if ((vtp == value) || (*vtp != '\0')) { 435 freeenv(value); 436 return (0); 437 } 438 freeenv(value); 439 *data = iv; 440 return (1); 441} 442 443/* 444 * Find the next entry after the one which (cp) falls within, return a 445 * pointer to its start or NULL if there are no more. 446 */ 447static char * 448kernenv_next(char *cp) 449{ 450 451 if (cp != NULL) { 452 while (*cp != 0) 453 cp++; 454 cp++; 455 if (*cp == 0) 456 cp = NULL; 457 } 458 return (cp); 459} 460 461void 462tunable_int_init(void *data) 463{ 464 struct tunable_int *d = (struct tunable_int *)data; 465 466 TUNABLE_INT_FETCH(d->path, d->var); 467} 468 469void 470tunable_quad_init(void *data) 471{ 472 struct tunable_quad *d = (struct tunable_quad *)data; 473 474 TUNABLE_QUAD_FETCH(d->path, d->var); 475} 476 477void 478tunable_str_init(void *data) 479{ 480 struct tunable_str *d = (struct tunable_str *)data; 481 482 TUNABLE_STR_FETCH(d->path, d->var, d->size); 483}
|