kern_environment.c revision 143319
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1998 Michael Smith
31590Srgrimes * All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes *
141590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241590Srgrimes * SUCH DAMAGE.
251590Srgrimes */
261590Srgrimes
271590Srgrimes/*
281590Srgrimes * The unified bootloader passes us a pointer to a preserved copy of
291590Srgrimes * bootstrap/kernel environment variables.  We convert them to a
301590Srgrimes * dynamic array of strings later when the VM subsystem is up.
3154162Scharnier *
3254162Scharnier * We make these available through the kenv(2) syscall for userland
3354162Scharnier * and through getenv()/freeenv() setenv() unsetenv() testenv() for
341590Srgrimes * the kernel.
3599112Sobrien */
3699112Sobrien
371590Srgrimes#include <sys/cdefs.h>
381590Srgrimes__FBSDID("$FreeBSD: head/sys/kern/kern_environment.c 143319 2005-03-09 12:16:45Z des $");
39112212Srobert
401590Srgrimes#include "opt_mac.h"
41200462Sdelphij
421590Srgrimes#include <sys/types.h>
4387212Smarkm#include <sys/param.h>
4487212Smarkm#include <sys/proc.h>
451590Srgrimes#include <sys/queue.h>
46112212Srobert#include <sys/lock.h>
471590Srgrimes#include <sys/mac.h>
48112212Srobert#include <sys/malloc.h>
4987212Smarkm#include <sys/mutex.h>
50112212Srobert#include <sys/kernel.h>
5187212Smarkm#include <sys/sx.h>
521590Srgrimes#include <sys/systm.h>
531590Srgrimes#include <sys/sysent.h>
541590Srgrimes#include <sys/sysproto.h>
551590Srgrimes#include <sys/libkern.h>
561590Srgrimes#include <sys/kenv.h>
571590Srgrimes
581590Srgrimesstatic MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
5987212Smarkm
601590Srgrimes#define KENV_SIZE	512	/* Maximum number of environment strings */
6187212Smarkm
621590Srgrimes/* pointer to the static environment */
631590Srgrimeschar		*kern_envp;
6487212Smarkmstatic char	*kernenv_next(char *);
651590Srgrimes
66112212Srobert/* dynamic environment variables */
671590Srgrimeschar		**kenvp;
6887212Smarkmstruct sx	kenv_lock;
6987212Smarkm
701590Srgrimes/*
7187212Smarkm * No need to protect this with a mutex
721590Srgrimes * since SYSINITS are single threaded.
73112212Srobert */
741590Srgrimesint	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, needed;
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		done = needed = 0;
103		sx_slock(&kenv_lock);
104		for (i = 0; kenvp[i] != NULL; i++) {
105			len = strlen(kenvp[i]) + 1;
106			needed += len;
107			len = min(len, uap->len - done);
108			/*
109			 * If called with a NULL or insufficiently large
110			 * buffer, just keep computing the required size.
111			 */
112			if (uap->value != NULL && len > 0) {
113				error = copyout(kenvp[i], uap->value + done,
114				    len);
115				if (error)
116					break;
117				done += len;
118			}
119		}
120		sx_sunlock(&kenv_lock);
121		td->td_retval[0] = ((done == needed) ? 0 : needed);
122		return (error);
123	}
124
125	if ((uap->what == KENV_SET) ||
126	    (uap->what == KENV_UNSET)) {
127		error = suser(td);
128		if (error)
129			return (error);
130	}
131
132	name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
133
134	error = copyinstr(uap->name, name, KENV_MNAMELEN, NULL);
135	if (error)
136		goto done;
137
138	switch (uap->what) {
139	case KENV_GET:
140#ifdef MAC
141		error = mac_check_kenv_get(td->td_ucred, name);
142		if (error)
143			goto done;
144#endif
145		value = getenv(name);
146		if (value == NULL) {
147			error = ENOENT;
148			goto done;
149		}
150		len = strlen(value) + 1;
151		if (len > uap->len)
152			len = uap->len;
153		error = copyout(value, uap->value, len);
154		freeenv(value);
155		if (error)
156			goto done;
157		td->td_retval[0] = len;
158		break;
159	case KENV_SET:
160		len = uap->len;
161		if (len < 1) {
162			error = EINVAL;
163			goto done;
164		}
165		if (len > KENV_MVALLEN)
166			len = KENV_MVALLEN;
167		value = malloc(len, M_TEMP, M_WAITOK);
168		error = copyinstr(uap->value, value, len, NULL);
169		if (error) {
170			free(value, M_TEMP);
171			goto done;
172		}
173#ifdef MAC
174		error = mac_check_kenv_set(td->td_ucred, name, value);
175		if (error == 0)
176#endif
177			setenv(name, value);
178		free(value, M_TEMP);
179		break;
180	case KENV_UNSET:
181#ifdef MAC
182		error = mac_check_kenv_unset(td->td_ucred, name);
183		if (error)
184			goto done;
185#endif
186		error = unsetenv(name);
187		if (error)
188			error = ENOENT;
189		break;
190	default:
191		error = EINVAL;
192		break;
193	}
194done:
195	free(name, M_TEMP);
196	return (error);
197}
198
199/*
200 * Setup the dynamic kernel environment.
201 */
202static void
203init_dynamic_kenv(void *data __unused)
204{
205	char *cp;
206	int len, i;
207
208	kenvp = malloc(KENV_SIZE * sizeof(char *), M_KENV, M_WAITOK | M_ZERO);
209	i = 0;
210	for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
211		len = strlen(cp) + 1;
212		kenvp[i] = malloc(len, M_KENV, M_WAITOK);
213		strcpy(kenvp[i++], cp);
214	}
215	kenvp[i] = NULL;
216
217	sx_init(&kenv_lock, "kernel environment");
218	dynamic_kenv = 1;
219}
220SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
221
222void
223freeenv(char *env)
224{
225
226	if (dynamic_kenv)
227		free(env, M_KENV);
228}
229
230/*
231 * Internal functions for string lookup.
232 */
233static char *
234_getenv_dynamic(const char *name, int *idx)
235{
236	char *cp;
237	int len, i;
238
239	sx_assert(&kenv_lock, SX_LOCKED);
240	len = strlen(name);
241	for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
242		if ((cp[len] == '=') &&
243		    (strncmp(cp, name, len) == 0)) {
244			if (idx != NULL)
245				*idx = i;
246			return (cp + len + 1);
247		}
248	}
249	return (NULL);
250}
251
252static char *
253_getenv_static(const char *name)
254{
255	char *cp, *ep;
256	int len;
257
258	for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
259		for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
260			;
261		if (*ep != '=')
262			continue;
263		len = ep - cp;
264		ep++;
265		if (!strncmp(name, cp, len) && name[len] == 0)
266			return (ep);
267	}
268	return (NULL);
269}
270
271/*
272 * Look up an environment variable by name.
273 * Return a pointer to the string if found.
274 * The pointer has to be freed with freeenv()
275 * after use.
276 */
277char *
278getenv(const char *name)
279{
280	char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
281	char *ret, *cp;
282	int len;
283
284	if (dynamic_kenv) {
285		sx_slock(&kenv_lock);
286		cp = _getenv_dynamic(name, NULL);
287		if (cp != NULL) {
288			strcpy(buf, cp);
289			sx_sunlock(&kenv_lock);
290			len = strlen(buf) + 1;
291			ret = malloc(len, M_KENV, M_WAITOK);
292			strcpy(ret, buf);
293		} else {
294			sx_sunlock(&kenv_lock);
295			ret = NULL;
296		}
297	} else
298		ret = _getenv_static(name);
299	return (ret);
300}
301
302/*
303 * Test if an environment variable is defined.
304 */
305int
306testenv(const char *name)
307{
308	char *cp;
309
310	if (dynamic_kenv) {
311		sx_slock(&kenv_lock);
312		cp = _getenv_dynamic(name, NULL);
313		sx_sunlock(&kenv_lock);
314	} else
315		cp = _getenv_static(name);
316	if (cp != NULL)
317		return (1);
318	return (0);
319}
320
321/*
322 * Set an environment variable by name.
323 */
324int
325setenv(const char *name, const char *value)
326{
327	char *buf, *cp, *oldenv;
328	int namelen, vallen, i;
329
330	KENV_CHECK;
331
332	namelen = strlen(name) + 1;
333	if (namelen > KENV_MNAMELEN)
334		return (-1);
335	vallen = strlen(value) + 1;
336	if (vallen > KENV_MVALLEN)
337		return (-1);
338	buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
339	sprintf(buf, "%s=%s", name, value);
340
341	sx_xlock(&kenv_lock);
342	cp = _getenv_dynamic(name, &i);
343	if (cp != NULL) {
344		oldenv = kenvp[i];
345		kenvp[i] = buf;
346		sx_xunlock(&kenv_lock);
347		free(oldenv, M_KENV);
348	} else {
349		/* We add the option if it wasn't found */
350		for (i = 0; (cp = kenvp[i]) != NULL; i++)
351			;
352		kenvp[i] = buf;
353		kenvp[i + 1] = NULL;
354		sx_xunlock(&kenv_lock);
355	}
356	return (0);
357}
358
359/*
360 * Unset an environment variable string.
361 */
362int
363unsetenv(const char *name)
364{
365	char *cp, *oldenv;
366	int i, j;
367
368	KENV_CHECK;
369
370	sx_xlock(&kenv_lock);
371	cp = _getenv_dynamic(name, &i);
372	if (cp != NULL) {
373		oldenv = kenvp[i];
374		for (j = i + 1; kenvp[j] != NULL; j++)
375			kenvp[i++] = kenvp[j];
376		kenvp[i] = NULL;
377		sx_xunlock(&kenv_lock);
378		free(oldenv, M_KENV);
379		return (0);
380	}
381	sx_xunlock(&kenv_lock);
382	return (-1);
383}
384
385/*
386 * Return a string value from an environment variable.
387 */
388int
389getenv_string(const char *name, char *data, int size)
390{
391	char *tmp;
392
393	tmp = getenv(name);
394	if (tmp != NULL) {
395		strlcpy(data, tmp, size);
396		freeenv(tmp);
397		return (1);
398	} else
399		return (0);
400}
401
402/*
403 * Return an integer value from an environment variable.
404 */
405int
406getenv_int(const char *name, int *data)
407{
408	quad_t tmp;
409	int rval;
410
411	rval = getenv_quad(name, &tmp);
412	if (rval)
413		*data = (int) tmp;
414	return (rval);
415}
416
417/*
418 * Return a long value from an environment variable.
419 */
420long
421getenv_long(const char *name, long *data)
422{
423	quad_t tmp;
424	long rval;
425
426	rval = getenv_quad(name, &tmp);
427	if (rval)
428		*data = (long) tmp;
429	return (rval);
430}
431
432/*
433 * Return an unsigned long value from an environment variable.
434 */
435unsigned long
436getenv_ulong(const char *name, unsigned long *data)
437{
438	quad_t tmp;
439	long rval;
440
441	rval = getenv_quad(name, &tmp);
442	if (rval)
443		*data = (unsigned long) tmp;
444	return (rval);
445}
446
447/*
448 * Return a quad_t value from an environment variable.
449 */
450int
451getenv_quad(const char *name, quad_t *data)
452{
453	char	*value;
454	char	*vtp;
455	quad_t	iv;
456
457	value = getenv(name);
458	if (value == NULL)
459		return (0);
460	iv = strtoq(value, &vtp, 0);
461	if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
462		freeenv(value);
463		return (0);
464	}
465	switch (vtp[0]) {
466	case 't': case 'T':
467		iv *= 1024;
468	case 'g': case 'G':
469		iv *= 1024;
470	case 'm': case 'M':
471		iv *= 1024;
472	case 'k': case 'K':
473		iv *= 1024;
474	case '\0':
475		break;
476	default:
477		freeenv(value);
478		return (0);
479	}
480	*data = iv;
481	freeenv(value);
482	return (1);
483}
484
485/*
486 * Find the next entry after the one which (cp) falls within, return a
487 * pointer to its start or NULL if there are no more.
488 */
489static char *
490kernenv_next(char *cp)
491{
492
493	if (cp != NULL) {
494		while (*cp != 0)
495			cp++;
496		cp++;
497		if (*cp == 0)
498			cp = NULL;
499	}
500	return (cp);
501}
502
503void
504tunable_int_init(void *data)
505{
506	struct tunable_int *d = (struct tunable_int *)data;
507
508	TUNABLE_INT_FETCH(d->path, d->var);
509}
510
511void
512tunable_long_init(void *data)
513{
514	struct tunable_long *d = (struct tunable_long *)data;
515
516	TUNABLE_LONG_FETCH(d->path, d->var);
517}
518
519void
520tunable_ulong_init(void *data)
521{
522	struct tunable_ulong *d = (struct tunable_ulong *)data;
523
524	TUNABLE_ULONG_FETCH(d->path, d->var);
525}
526
527void
528tunable_str_init(void *data)
529{
530	struct tunable_str *d = (struct tunable_str *)data;
531
532	TUNABLE_STR_FETCH(d->path, d->var, d->size);
533}
534