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