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: src/sys/kern/kern_environment.c,v 1.10.2.7 2002/05/07 09:57:16 bde Exp $ 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 dynamic array 32 * of strings later when the VM subsystem is up. 33 * We make these available using sysctl for both in-kernel and 34 * out-of-kernel consumers, as well as the k{get,set,unset,free,test}env() 35 * functions for in-kernel consumers. 36 * 37 * Note that the current sysctl infrastructure doesn't allow 38 * dynamic insertion or traversal through handled spaces. Grr. 39 * 40 * TODO: implement a sysctl handler to provide the functionality mentioned 41 * above. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/kernel.h> 46 #include <sys/libkern.h> 47 #include <sys/malloc.h> 48 #include <sys/spinlock.h> 49 #include <sys/spinlock2.h> 50 #include <sys/systm.h> 51 #include <sys/sysctl.h> 52 53 /* exported variables */ 54 char *kern_envp; /* <sys/systm.h> */ 55 56 /* local variables */ 57 char **kenv_dynp; 58 int kenv_isdynamic; 59 struct spinlock kenv_dynlock; 60 61 /* constants */ 62 MALLOC_DEFINE(M_KENV, "kenv", "kernel environment dynamic storage"); 63 #define KENV_DYNMAXNUM 512 64 #define KENV_MAXNAMELEN 128 65 #define KENV_MAXVALLEN 128 66 67 /* local prototypes */ 68 static char *kenv_getstring_dynamic(const char *name, int *idx); 69 static char *kenv_getstring_static(const char *name); 70 static char *kernenv_next(char *cp); 71 72 /* 73 * Look up a string in the dynamic environment array. Must be called with 74 * kenv_dynlock held. 75 */ 76 static char * 77 kenv_getstring_dynamic(const char *name, int *idx) 78 { 79 char *cp; 80 int len, i; 81 82 len = strlen(name); 83 /* note: kunsetenv() never leaves NULL holes in the array */ 84 for (i = 0; (cp = kenv_dynp[i]) != NULL; i++) { 85 if ((strncmp(cp, name, len) == 0) && (cp[len] == '=')) { 86 if (idx != NULL) 87 *idx = i; 88 return(cp + len + 1); 89 } 90 } 91 return(NULL); 92 } 93 94 /* 95 * Look up a string in the static environment array. 96 */ 97 static char * 98 kenv_getstring_static(const char *name) 99 { 100 char *cp, *ep; 101 int len; 102 103 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) { 104 for (ep = cp; (*ep != '=') && (*ep != 0); ep++) 105 ; 106 if (*ep != '=') 107 continue; 108 len = ep - cp; 109 ep++; 110 if (!strncmp(name, cp, len) && name[len] == 0) 111 return(ep); 112 } 113 return(NULL); 114 } 115 116 /* 117 * Look up an environment variable by name. 118 */ 119 char * 120 kgetenv(const char *name) 121 { 122 char buf[KENV_MAXNAMELEN + 1 + KENV_MAXVALLEN + 1]; 123 char *cp, *ret; 124 int len; 125 126 if (kenv_isdynamic) { 127 spin_lock(&kenv_dynlock); 128 cp = kenv_getstring_dynamic(name, NULL); 129 if (cp != NULL) { 130 strcpy(buf, cp); 131 spin_unlock(&kenv_dynlock); 132 len = strlen(buf) + 1; 133 ret = kmalloc(len, M_KENV, M_WAITOK); 134 strcpy(ret, buf); 135 } else { 136 spin_unlock(&kenv_dynlock); 137 ret = NULL; 138 } 139 } else 140 ret = kenv_getstring_static(name); 141 return(ret); 142 } 143 144 /* 145 * Set an environment variable by name. 146 */ 147 int 148 ksetenv(const char *name, const char *value) 149 { 150 char *cp, *buf, *oldenv; 151 int namelen, vallen, i; 152 153 if (kenv_isdynamic) { 154 namelen = strlen(name) + 1; 155 vallen = strlen(value) + 1; 156 if ((namelen > KENV_MAXNAMELEN) || (vallen > KENV_MAXVALLEN)) 157 return(-1); 158 buf = kmalloc(namelen + vallen, M_KENV, M_WAITOK); 159 ksprintf(buf, "%s=%s", name, value); 160 spin_lock(&kenv_dynlock); 161 cp = kenv_getstring_dynamic(name, &i); 162 if (cp != NULL) { 163 /* replace existing environment variable */ 164 oldenv = kenv_dynp[i]; 165 kenv_dynp[i] = buf; 166 spin_unlock(&kenv_dynlock); 167 kfree(oldenv, M_KENV); 168 } else { 169 /* append new environment variable */ 170 for (i = 0; (cp = kenv_dynp[i]) != NULL; i++) 171 ; 172 /* bounds checking */ 173 if (i < 0 || i >= (KENV_DYNMAXNUM - 1)) { 174 kfree(buf, M_KENV); 175 spin_unlock(&kenv_dynlock); 176 return(-1); 177 } 178 kenv_dynp[i] = buf; 179 kenv_dynp[i + 1] = NULL; 180 spin_unlock(&kenv_dynlock); 181 } 182 return(0); 183 } else { 184 kprintf("WARNING: ksetenv: dynamic array not created yet\n"); 185 return(-1); 186 } 187 } 188 189 /* 190 * Unset an environment variable by name. 191 */ 192 int 193 kunsetenv(const char *name) 194 { 195 char *cp, *oldenv; 196 int i, j; 197 198 if (kenv_isdynamic) { 199 spin_lock(&kenv_dynlock); 200 cp = kenv_getstring_dynamic(name, &i); 201 if (cp != NULL) { 202 oldenv = kenv_dynp[i]; 203 /* move all pointers beyond the unset one down 1 step */ 204 for (j = i + 1; kenv_dynp[j] != NULL; j++) 205 kenv_dynp[i++] = kenv_dynp[j]; 206 kenv_dynp[i] = NULL; 207 spin_unlock(&kenv_dynlock); 208 kfree(oldenv, M_KENV); 209 return(0); 210 } 211 spin_unlock(&kenv_dynlock); 212 return(-1); 213 } else { 214 kprintf("WARNING: kunsetenv: dynamic array not created yet\n"); 215 return(-1); 216 } 217 } 218 219 /* 220 * Free an environment variable that has been copied for a consumer. 221 */ 222 void 223 kfreeenv(char *env) 224 { 225 if (kenv_isdynamic) 226 kfree(env, M_KENV); 227 } 228 229 /* 230 * Test if an environment variable is defined. 231 */ 232 int 233 ktestenv(const char *name) 234 { 235 char *cp; 236 237 if (kenv_isdynamic) { 238 spin_lock(&kenv_dynlock); 239 cp = kenv_getstring_dynamic(name, NULL); 240 spin_unlock(&kenv_dynlock); 241 } else 242 cp = kenv_getstring_static(name); 243 if (cp != NULL) 244 return(1); 245 return(0); 246 } 247 248 /* 249 * Return a string value from an environment variable. 250 */ 251 int 252 kgetenv_string(const char *name, char *data, int size) 253 { 254 char *tmp; 255 256 tmp = kgetenv(name); 257 if (tmp != NULL) { 258 strncpy(data, tmp, size); 259 data[size - 1] = 0; 260 kfreeenv(tmp); 261 return (1); 262 } else 263 return (0); 264 } 265 266 /* 267 * Return an integer value from an environment variable. 268 */ 269 int 270 kgetenv_int(const char *name, int *data) 271 { 272 quad_t tmp; 273 int rval; 274 275 rval = kgetenv_quad(name, &tmp); 276 if (rval) 277 *data = (int) tmp; 278 return (rval); 279 } 280 281 /* 282 * Return a long value from an environment variable. 283 */ 284 int 285 kgetenv_long(const char *name, long *data) 286 { 287 quad_t tmp; 288 int rval; 289 290 rval = kgetenv_quad(name, &tmp); 291 if (rval) 292 *data = (long)tmp; 293 return (rval); 294 } 295 296 /* 297 * Return an unsigned long value from an environment variable. 298 */ 299 int 300 kgetenv_ulong(const char *name, unsigned long *data) 301 { 302 quad_t tmp; 303 int rval; 304 305 rval = kgetenv_quad(name, &tmp); 306 if (rval) 307 *data = (unsigned long) tmp; 308 return (rval); 309 } 310 311 /* 312 * Return a quad_t value from an environment variable. 313 * 314 * A single character kmgtKMGT extension multiplies the value 315 * by 1024, 1024*1024, etc. 316 */ 317 int 318 kgetenv_quad(const char *name, quad_t *data) 319 { 320 char* value; 321 char* vtp; 322 quad_t iv; 323 324 if ((value = kgetenv(name)) == NULL) 325 return(0); 326 327 iv = strtoq(value, &vtp, 0); 328 switch(*vtp) { 329 case 't': 330 case 'T': 331 iv <<= 10; 332 /* fall through */ 333 case 'g': 334 case 'G': 335 iv <<= 10; 336 /* fall through */ 337 case 'm': 338 case 'M': 339 iv <<= 10; 340 /* fall through */ 341 case 'k': 342 case 'K': 343 iv <<= 10; 344 ++vtp; 345 break; 346 default: 347 break; 348 } 349 350 if ((vtp == value) || (*vtp != '\0')) { 351 kfreeenv(value); 352 return(0); 353 } 354 355 *data = iv; 356 kfreeenv(value); 357 return(1); 358 } 359 360 /* 361 * Boottime (static) kernel environment sysctl handler. 362 */ 363 static int 364 sysctl_kenv_boot(SYSCTL_HANDLER_ARGS) 365 { 366 int *name = (int *)arg1; 367 u_int namelen = arg2; 368 char *cp; 369 int i, error; 370 371 if (kern_envp == NULL) 372 return(ENOENT); 373 374 name++; 375 namelen--; 376 377 if (namelen != 1) 378 return(EINVAL); 379 380 cp = kern_envp; 381 for (i = 0; i < name[0]; i++) { 382 cp = kernenv_next(cp); 383 if (cp == NULL) 384 break; 385 } 386 387 if (cp == NULL) 388 return(ENOENT); 389 390 error = SYSCTL_OUT(req, cp, strlen(cp) + 1); 391 return (error); 392 } 393 394 SYSCTL_NODE(_kern, OID_AUTO, environment, CTLFLAG_RD, sysctl_kenv_boot, 395 "boottime (static) kernel environment space"); 396 397 /* 398 * Find the next entry after the one which (cp) falls within, return a 399 * pointer to its start or NULL if there are no more. 400 */ 401 static char * 402 kernenv_next(char *cp) 403 { 404 if (cp != NULL) { 405 while (*cp != 0) 406 cp++; 407 cp++; 408 if (*cp == 0) 409 cp = NULL; 410 } 411 return(cp); 412 } 413 414 /* 415 * TUNABLE_INT init functions. 416 */ 417 void 418 tunable_int_init(void *data) 419 { 420 struct tunable_int *d = (struct tunable_int *)data; 421 422 TUNABLE_INT_FETCH(d->path, d->var); 423 } 424 425 void 426 tunable_long_init(void *data) 427 { 428 struct tunable_long *d = (struct tunable_long *)data; 429 430 TUNABLE_LONG_FETCH(d->path, d->var); 431 } 432 433 void 434 tunable_ulong_init(void *data) 435 { 436 struct tunable_ulong *d = (struct tunable_ulong *)data; 437 438 TUNABLE_ULONG_FETCH(d->path, d->var); 439 } 440 441 void 442 tunable_quad_init(void *data) 443 { 444 struct tunable_quad *d = (struct tunable_quad *)data; 445 446 TUNABLE_QUAD_FETCH(d->path, d->var); 447 } 448 449 void 450 tunable_str_init(void *data) 451 { 452 struct tunable_str *d = (struct tunable_str *)data; 453 454 TUNABLE_STR_FETCH(d->path, d->var, d->size); 455 } 456 457 /* 458 * Create the dynamic environment array, and copy in the values from the static 459 * environment as passed by the bootloader. 460 */ 461 static void 462 kenv_init(void *dummy) 463 { 464 char *cp; 465 int len, i; 466 467 kenv_dynp = kmalloc(KENV_DYNMAXNUM * sizeof(char *), M_KENV, 468 M_WAITOK | M_ZERO); 469 470 /* copy the static environment to our dynamic environment */ 471 for (i = 0, cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) { 472 len = strlen(cp) + 1; 473 if (i < (KENV_DYNMAXNUM - 1)) { 474 kenv_dynp[i] = kmalloc(len, M_KENV, M_WAITOK); 475 strcpy(kenv_dynp[i++], cp); 476 } else 477 kprintf("WARNING: kenv: exhausted dynamic storage, " 478 "ignoring string %s\n", cp); 479 } 480 kenv_dynp[i] = NULL; 481 482 spin_init(&kenv_dynlock, "kenvdynlock"); 483 kenv_isdynamic = 1; 484 } 485 SYSINIT(kenv, SI_BOOT1_POST, SI_ORDER_ANY, kenv_init, NULL); 486