xref: /freebsd/sys/kern/kern_environment.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
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$
27  */
28 
29 /*
30  * The unified bootloader passes us a pointer to a preserved copy of
31  * bootstrap/kernel environment variables.
32  * We make these available using sysctl for both in-kernel and
33  * out-of-kernel consumers.
34  *
35  * Note that the current sysctl infrastructure doesn't allow
36  * dynamic insertion or traversal through handled spaces.  Grr.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
43 #include <sys/libkern.h>
44 
45 char	*kern_envp;
46 
47 static char	*kernenv_next(char *cp);
48 
49 /*
50  * Look up an environment variable by name.
51  */
52 char *
53 getenv(const char *name)
54 {
55     char	*cp, *ep;
56     int		len;
57 
58     for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
59 	for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
60 	    ;
61 	len = ep - cp;
62 	if (*ep == '=')
63 	    ep++;
64 	if (!strncmp(name, cp, len))
65 	    return(ep);
66     }
67     return(NULL);
68 }
69 
70 /*
71  * Return a string value from an environment variable.
72  */
73 int
74 getenv_string(const char *name, char *data, int size)
75 {
76     char *tmp;
77 
78     tmp = getenv(name);
79     if (tmp != NULL) {
80 	strncpy(data, tmp, size);
81 	data[size - 1] = 0;
82 	return (1);
83     } else
84 	return (0);
85 }
86 
87 /*
88  * Return an integer value from an environment variable.
89  */
90 int
91 getenv_int(const char *name, int *data)
92 {
93     quad_t tmp;
94     int rval;
95 
96     rval = getenv_quad(name, &tmp);
97     if (rval) {
98 	*data = (int) tmp;
99     }
100     return (rval);
101 }
102 
103 /*
104  * Return a quad_t value from an environment variable.
105  */
106 int
107 getenv_quad(const char *name, quad_t *data)
108 {
109     const char	*value;
110     char	*vtp;
111     quad_t	iv;
112 
113     if ((value = getenv(name)) == NULL)
114 	return(0);
115 
116     iv = strtoq(value, &vtp, 0);
117     if ((vtp == value) || (*vtp != '\0'))
118 	return(0);
119 
120     *data = iv;
121     return(1);
122 }
123 
124 /*
125  * Export for userland.  See kenv(1) specifically.
126  */
127 static int
128 sysctl_kernenv(SYSCTL_HANDLER_ARGS)
129 {
130     int		*name = (int *)arg1;
131     u_int	namelen = arg2;
132     char	*cp;
133     int		i, error;
134 
135     if (kern_envp == NULL)
136 	return(ENOENT);
137 
138     name++;
139     namelen--;
140 
141     if (namelen != 1)
142 	return(EINVAL);
143 
144     cp = kern_envp;
145     for (i = 0; i < name[0]; i++) {
146 	cp = kernenv_next(cp);
147 	if (cp == NULL)
148 	    break;
149     }
150 
151     if (cp == NULL)
152 	return(ENOENT);
153 
154     error = SYSCTL_OUT(req, cp, strlen(cp) + 1);
155     return (error);
156 }
157 
158 SYSCTL_NODE(_kern, OID_AUTO, environment, CTLFLAG_RD, sysctl_kernenv, "kernel environment space");
159 
160 /*
161  * Find the next entry after the one which (cp) falls within, return a
162  * pointer to its start or NULL if there are no more.
163  */
164 static char *
165 kernenv_next(char *cp)
166 {
167     if (cp != NULL) {
168 	while (*cp != 0)
169 	    cp++;
170 	cp++;
171 	if (*cp == 0)
172 	    cp = NULL;
173     }
174     return(cp);
175 }
176 
177 void
178 tunable_int_init(void *data)
179 {
180 	struct tunable_int *d = (struct tunable_int *)data;
181 
182 	TUNABLE_INT_FETCH(d->path, d->var);
183 }
184 
185 void
186 tunable_quad_init(void *data)
187 {
188 	struct tunable_quad *d = (struct tunable_quad *)data;
189 
190 	TUNABLE_QUAD_FETCH(d->path, d->var);
191 }
192 
193 void
194 tunable_str_init(void *data)
195 {
196 	struct tunable_str *d = (struct tunable_str *)data;
197 
198 	TUNABLE_STR_FETCH(d->path, d->var, d->size);
199 }
200