Deleted Added
full compact
kern_environment.c (83744) kern_environment.c (84783)
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 *
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: head/sys/kern/kern_environment.c 83744 2001-09-21 02:25:53Z peter $
26 * $FreeBSD: head/sys/kern/kern_environment.c 84783 2001-10-10 23:06:54Z ps $
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
45char *kern_envp;
46
47static char *kernenv_next(char *cp);
48
49char *
50getenv(const char *name)
51{
52 char *cp, *ep;
53 int len;
54
55 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
56 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
57 ;
58 len = ep - cp;
59 if (*ep == '=')
60 ep++;
61 if (!strncmp(name, cp, len))
62 return(ep);
63 }
64 return(NULL);
65}
66
67/*
68 * Return an integer value from an environment variable.
69 */
70int
71getenv_int(const char *name, int *data)
72{
73 quad_t tmp;
74 int rval;
75
76 rval = getenv_quad(name, &tmp);
77 if (rval) {
78 *data = (int) tmp;
79 }
80 return (rval);
81}
82
83/*
84 * Return a quad_t value from an environment variable.
85 */
86quad_t
87getenv_quad(const char *name, quad_t *data)
88{
89 const char *value;
90 char *vtp;
91 quad_t iv;
92
93 if ((value = getenv(name)) == NULL)
94 return(0);
95
96 iv = strtoq(value, &vtp, 0);
97 if ((vtp == value) || (*vtp != '\0'))
98 return(0);
99
100 *data = iv;
101 return(1);
102}
103
104/*
105 * Export for userland. See kenv(1) specifically.
106 */
107static int
108sysctl_kernenv(SYSCTL_HANDLER_ARGS)
109{
110 int *name = (int *)arg1;
111 u_int namelen = arg2;
112 char *cp;
113 int i, error;
114
115 if (kern_envp == NULL)
116 return(ENOENT);
117
118 name++;
119 namelen--;
120
121 if (namelen != 1)
122 return(EINVAL);
123
124 cp = kern_envp;
125 for (i = 0; i < name[0]; i++) {
126 cp = kernenv_next(cp);
127 if (cp == NULL)
128 break;
129 }
130
131 if (cp == NULL)
132 return(ENOENT);
133
134 error = SYSCTL_OUT(req, cp, strlen(cp) + 1);
135 return (error);
136}
137
138SYSCTL_NODE(_kern, OID_AUTO, environment, CTLFLAG_RD, sysctl_kernenv, "kernel environment space");
139
140/*
141 * Find the next entry after the one which (cp) falls within, return a
142 * pointer to its start or NULL if there are no more.
143 */
144static char *
145kernenv_next(char *cp)
146{
147 if (cp != NULL) {
148 while (*cp != 0)
149 cp++;
150 cp++;
151 if (*cp == 0)
152 cp = NULL;
153 }
154 return(cp);
155}
156
157void
158tunable_int_init(void *data)
159{
160 struct tunable_int *d = (struct tunable_int *)data;
161
162 TUNABLE_INT_FETCH(d->path, d->var);
163}
164
165void
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
45char *kern_envp;
46
47static char *kernenv_next(char *cp);
48
49char *
50getenv(const char *name)
51{
52 char *cp, *ep;
53 int len;
54
55 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
56 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
57 ;
58 len = ep - cp;
59 if (*ep == '=')
60 ep++;
61 if (!strncmp(name, cp, len))
62 return(ep);
63 }
64 return(NULL);
65}
66
67/*
68 * Return an integer value from an environment variable.
69 */
70int
71getenv_int(const char *name, int *data)
72{
73 quad_t tmp;
74 int rval;
75
76 rval = getenv_quad(name, &tmp);
77 if (rval) {
78 *data = (int) tmp;
79 }
80 return (rval);
81}
82
83/*
84 * Return a quad_t value from an environment variable.
85 */
86quad_t
87getenv_quad(const char *name, quad_t *data)
88{
89 const char *value;
90 char *vtp;
91 quad_t iv;
92
93 if ((value = getenv(name)) == NULL)
94 return(0);
95
96 iv = strtoq(value, &vtp, 0);
97 if ((vtp == value) || (*vtp != '\0'))
98 return(0);
99
100 *data = iv;
101 return(1);
102}
103
104/*
105 * Export for userland. See kenv(1) specifically.
106 */
107static int
108sysctl_kernenv(SYSCTL_HANDLER_ARGS)
109{
110 int *name = (int *)arg1;
111 u_int namelen = arg2;
112 char *cp;
113 int i, error;
114
115 if (kern_envp == NULL)
116 return(ENOENT);
117
118 name++;
119 namelen--;
120
121 if (namelen != 1)
122 return(EINVAL);
123
124 cp = kern_envp;
125 for (i = 0; i < name[0]; i++) {
126 cp = kernenv_next(cp);
127 if (cp == NULL)
128 break;
129 }
130
131 if (cp == NULL)
132 return(ENOENT);
133
134 error = SYSCTL_OUT(req, cp, strlen(cp) + 1);
135 return (error);
136}
137
138SYSCTL_NODE(_kern, OID_AUTO, environment, CTLFLAG_RD, sysctl_kernenv, "kernel environment space");
139
140/*
141 * Find the next entry after the one which (cp) falls within, return a
142 * pointer to its start or NULL if there are no more.
143 */
144static char *
145kernenv_next(char *cp)
146{
147 if (cp != NULL) {
148 while (*cp != 0)
149 cp++;
150 cp++;
151 if (*cp == 0)
152 cp = NULL;
153 }
154 return(cp);
155}
156
157void
158tunable_int_init(void *data)
159{
160 struct tunable_int *d = (struct tunable_int *)data;
161
162 TUNABLE_INT_FETCH(d->path, d->var);
163}
164
165void
166tunable_quad_init(void *data)
167{
168 struct tunable_quad *d = (struct tunable_quad *)data;
169
170 TUNABLE_QUAD_FETCH(d->path, d->var);
171}
172
173void
166tunable_str_init(void *data)
167{
168 struct tunable_str *d = (struct tunable_str *)data;
169
170 TUNABLE_STR_FETCH(d->path, d->var, d->size);
171}
174tunable_str_init(void *data)
175{
176 struct tunable_str *d = (struct tunable_str *)data;
177
178 TUNABLE_STR_FETCH(d->path, d->var, d->size);
179}