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 #include <machine/bootinfo.h>
45
46 char *kern_envp;
47
48 static char *kernenv_next(char *cp);
49
50 char *
51 getenv(char *name)
52 {
53 char *cp, *ep;
54 int len;
55
56 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
57 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
58 ;
59 len = ep - cp;
60 if (*ep == '=')
61 ep++;
62 if (!strncmp(name, cp, len))
63 return(ep);
64 }
65 return(NULL);
66 }
67
68 /*
69 * Return an integer value from an environment variable.
70 */
71 int
72 getenv_int(char *name, int *data)
73 {
74 quad_t tmp;
75 int rval;
76
77 rval = getenv_quad(name, &tmp);
78 if (rval) {
79 *data = (int) tmp;
80 }
81 return (rval);
82 }
83
84 /*
85 * Return a quad_t value from an environment variable.
86 */
87 quad_t
88 getenv_quad(char *name, quad_t *data)
89 {
90 const char *value;
91 char *vtp;
92 quad_t iv;
93
94 if ((value = getenv(name)) == NULL)
95 return(0);
96
97 iv = strtoq(value, &vtp, 0);
98 if ((vtp == value) || (*vtp != 0))
99 return(0);
100
101 *data = iv;
102 return(1);
103 }
104
105 static int
106 sysctl_kernenv SYSCTL_HANDLER_ARGS
107 {
108 int *name = (int *)arg1;
109 u_int namelen = arg2;
110 char *cp;
111 int i, error;
112
113 if (kern_envp == NULL)
114 return(ENOENT);
115
116 name++;
117 namelen--;
118
119 if (namelen != 1)
120 return(EINVAL);
121
122 cp = kern_envp;
123 for (i = 0; i < name[0]; i++) {
124 cp = kernenv_next(cp);
125 if (cp == NULL)
126 break;
127 }
128
129 if (cp == NULL)
130 return(ENOENT);
131
132 error = SYSCTL_OUT(req, cp, strlen(cp) + 1);
133 return (error);
134 }
135
136 SYSCTL_NODE(_kern, OID_AUTO, environment, CTLFLAG_RD, sysctl_kernenv, "kernel environment space");
137
138 /*
139 * Find the next entry after the one which (cp) falls within, return a
140 * pointer to its start or NULL if there are no more.
141 */
142 static char *
143 kernenv_next(char *cp)
144 {
145 if (cp != NULL) {
146 while (*cp != 0)
147 cp++;
148 cp++;
149 if (*cp == 0)
150 cp = NULL;
151 }
152 return(cp);
153 }
154
Cache object: b5721f6cab33e897dd734e664948a802
|