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