xref: /freebsd/share/man/man9/getenv.9 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
1fa24b620SJohn Baldwin.\" -*- nroff -*-
2fa24b620SJohn Baldwin.\"
3179fa75eSJohn Baldwin.\" Copyright (c) 2013 Hudson River Trading LLC
4fa24b620SJohn Baldwin.\" Written by: John H. Baldwin <jhb@FreeBSD.org>
5fa24b620SJohn Baldwin.\" All rights reserved.
6fa24b620SJohn Baldwin.\"
7fa24b620SJohn Baldwin.\" Redistribution and use in source and binary forms, with or without
8fa24b620SJohn Baldwin.\" modification, are permitted provided that the following conditions
9fa24b620SJohn Baldwin.\" are met:
10fa24b620SJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright
11fa24b620SJohn Baldwin.\"    notice, this list of conditions and the following disclaimer.
12fa24b620SJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright
13fa24b620SJohn Baldwin.\"    notice, this list of conditions and the following disclaimer in the
14fa24b620SJohn Baldwin.\"    documentation and/or other materials provided with the distribution.
15fa24b620SJohn Baldwin.\"
16fa24b620SJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17fa24b620SJohn Baldwin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18fa24b620SJohn Baldwin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19fa24b620SJohn Baldwin.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20fa24b620SJohn Baldwin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21fa24b620SJohn Baldwin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22fa24b620SJohn Baldwin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23fa24b620SJohn Baldwin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24fa24b620SJohn Baldwin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25fa24b620SJohn Baldwin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26fa24b620SJohn Baldwin.\" SUCH DAMAGE.
27fa24b620SJohn Baldwin.\"
28*cba446e2SMitchell Horne.Dd September 21, 2020
29457a378fSJoel Dahl.Dt GETENV 9
30fa24b620SJohn Baldwin.Os
31fa24b620SJohn Baldwin.Sh NAME
32fa24b620SJohn Baldwin.Nm freeenv ,
33c8de781dSEric van Gyzen.Nm kern_getenv ,
34fa24b620SJohn Baldwin.Nm getenv_int ,
35fa24b620SJohn Baldwin.Nm getenv_long ,
36fa24b620SJohn Baldwin.Nm getenv_string ,
37fa24b620SJohn Baldwin.Nm getenv_quad ,
38fa24b620SJohn Baldwin.Nm getenv_uint ,
39fa24b620SJohn Baldwin.Nm getenv_ulong ,
40*cba446e2SMitchell Horne.Nm getenv_bool ,
41*cba446e2SMitchell Horne.Nm getenv_is_true ,
42*cba446e2SMitchell Horne.Nm getenv_is_false ,
43c8de781dSEric van Gyzen.Nm kern_setenv ,
44fa24b620SJohn Baldwin.Nm testenv ,
45c8de781dSEric van Gyzen.Nm kern_unsetenv
46fa24b620SJohn Baldwin.Nd kernel environment variable functions
47fa24b620SJohn Baldwin.Sh SYNOPSIS
48fa24b620SJohn Baldwin.In sys/param.h
49fa24b620SJohn Baldwin.In sys/systm.h
50fa24b620SJohn Baldwin.Ft void
51fa24b620SJohn Baldwin.Fn freeenv "char *env"
52fa24b620SJohn Baldwin.Ft char *
53c8de781dSEric van Gyzen.Fn kern_getenv "const char *name"
54fa24b620SJohn Baldwin.Ft int
55fa24b620SJohn Baldwin.Fn getenv_int "const char *name" "int *data"
56fa24b620SJohn Baldwin.Ft int
57fa24b620SJohn Baldwin.Fn getenv_long "const char *name" "long *data"
58fa24b620SJohn Baldwin.Ft int
59fa24b620SJohn Baldwin.Fn getenv_string "const char *name" "char *data" "int size"
60fa24b620SJohn Baldwin.Ft int
61fa24b620SJohn Baldwin.Fn getenv_quad "const char *name" "quad_t *data"
62fa24b620SJohn Baldwin.Ft int
63fa24b620SJohn Baldwin.Fn getenv_uint "const char *name" "unsigned int *data"
64fa24b620SJohn Baldwin.Ft int
65fa24b620SJohn Baldwin.Fn getenv_ulong "const char *name" "unsigned long *data"
66fa24b620SJohn Baldwin.Ft int
67*cba446e2SMitchell Horne.Fn getenv_bool "const char *name" "bool *data"
68*cba446e2SMitchell Horne.Ft bool
69*cba446e2SMitchell Horne.Fn getenv_is_true "const char *name"
70*cba446e2SMitchell Horne.Ft bool
71*cba446e2SMitchell Horne.Fn getenv_is_false "const char *name"
72*cba446e2SMitchell Horne.Ft int
73c8de781dSEric van Gyzen.Fn kern_setenv "const char *name" "const char *value"
74fa24b620SJohn Baldwin.Ft int
75fa24b620SJohn Baldwin.Fn testenv "const char *name"
76fa24b620SJohn Baldwin.Ft int
77c8de781dSEric van Gyzen.Fn kern_unsetenv "const char *name"
78fa24b620SJohn Baldwin.Sh DESCRIPTION
79fa24b620SJohn BaldwinThese functions set, unset, fetch, and parse variables from the kernel's
80fa24b620SJohn Baldwinenvironment.
81fa24b620SJohn Baldwin.Pp
82fa24b620SJohn BaldwinThe
83c8de781dSEric van Gyzen.Fn kern_getenv
84fa24b620SJohn Baldwinfunction obtains the current value of the kernel environment variable
85fa24b620SJohn Baldwin.Fa name
86fa24b620SJohn Baldwinand returns a pointer to the string value.
87fa24b620SJohn BaldwinThe caller should not modify the string pointed to by the return value.
88fa24b620SJohn BaldwinThe
89c8de781dSEric van Gyzen.Fn kern_getenv
90fa24b620SJohn Baldwinfunction may allocate temporary storage,
91fa24b620SJohn Baldwinso the
92fa24b620SJohn Baldwin.Fn freeenv
93fa24b620SJohn Baldwinfunction must be called to release any allocated resources when the value
94fa24b620SJohn Baldwinreturned by
95c8de781dSEric van Gyzen.Fn kern_getenv
96fa24b620SJohn Baldwinis no longer needed.
979f2eb740SConrad Meyer.Pp
989f2eb740SConrad MeyerThe
999f2eb740SConrad Meyer.Fn freeenv
1009f2eb740SConrad Meyerfunction is used to release the resources allocated by a previous call to
101c8de781dSEric van Gyzen.Fn kern_getenv .
102fa24b620SJohn BaldwinThe
103fa24b620SJohn Baldwin.Fa env
104fa24b620SJohn Baldwinargument passed to
105fa24b620SJohn Baldwin.Fn freeenv
106fa24b620SJohn Baldwinis the pointer returned by the earlier call to
107c8de781dSEric van Gyzen.Fn kern_getenv .
1089f2eb740SConrad MeyerLike
1099f2eb740SConrad Meyer.Xr free 3 ,
1109f2eb740SConrad Meyerthe
1119f2eb740SConrad Meyer.Fa env
1129f2eb740SConrad Meyerargument can be
1139f2eb740SConrad Meyer.Va NULL ,
1149f2eb740SConrad Meyerin which case no action occurs.
115fa24b620SJohn Baldwin.Pp
116fa24b620SJohn BaldwinThe
117c8de781dSEric van Gyzen.Fn kern_setenv
118fa24b620SJohn Baldwinfunction inserts or resets the kernel environment variable
119fa24b620SJohn Baldwin.Fa name
120fa24b620SJohn Baldwinto
121fa24b620SJohn Baldwin.Fa value .
122fa24b620SJohn BaldwinIf the variable
123fa24b620SJohn Baldwin.Fa name
124fa24b620SJohn Baldwinalready exists,
125fa24b620SJohn Baldwinits value is replaced.
126fa24b620SJohn BaldwinThis function can fail if an internal limit on the number of environment
127fa24b620SJohn Baldwinvariables is exceeded.
128fa24b620SJohn Baldwin.Pp
129fa24b620SJohn BaldwinThe
130c8de781dSEric van Gyzen.Fn kern_unsetenv
131fa24b620SJohn Baldwinfunction deletes the kernel environment variable
132fa24b620SJohn Baldwin.Fa name .
133fa24b620SJohn Baldwin.Pp
134fa24b620SJohn BaldwinThe
135fa24b620SJohn Baldwin.Fn testenv
136fa24b620SJohn Baldwinfunction is used to determine if a kernel environment variable exists.
137fa24b620SJohn BaldwinIt returns a non-zero value if the variable
138fa24b620SJohn Baldwin.Fa name
139fa24b620SJohn Baldwinexists and zero if it does not.
140fa24b620SJohn Baldwin.Pp
141fa24b620SJohn BaldwinThe
142fa24b620SJohn Baldwin.Fn getenv_int ,
143fa24b620SJohn Baldwin.Fn getenv_long ,
144fa24b620SJohn Baldwin.Fn getenv_quad ,
145fa24b620SJohn Baldwin.Fn getenv_uint ,
146fa24b620SJohn Baldwinand
147fa24b620SJohn Baldwin.Fn getenv_ulong
148fa24b620SJohn Baldwinfunctions look for a kernel environment variable
149fa24b620SJohn Baldwin.Fa name
150fa24b620SJohn Baldwinand parse it as a signed integer,
151fa24b620SJohn Baldwinlong integer,
152fa24b620SJohn Baldwinsigned 64-bit integer,
153fa24b620SJohn Baldwinunsigned integer,
154fa24b620SJohn Baldwinor an unsigned long integer,
155fa24b620SJohn Baldwinrespectively.
156fa24b620SJohn BaldwinThese functions fail and return zero if
157fa24b620SJohn Baldwin.Fa name
158fa24b620SJohn Baldwindoes not exist or if any invalid characters are present in its value.
159fa24b620SJohn BaldwinOn success,
160fa24b620SJohn Baldwinthese function store the parsed value in the integer variable pointed to
161fa24b620SJohn Baldwinby
162fa24b620SJohn Baldwin.Fa data .
163fa24b620SJohn BaldwinIf the parsed value overflows the integer type,
164fa24b620SJohn Baldwina truncated value is stored in
165fa24b620SJohn Baldwin.Fa data
166fa24b620SJohn Baldwinand zero is returned.
167fa24b620SJohn BaldwinIf the value begins with a prefix of
168fa24b620SJohn Baldwin.Dq 0x
169fa24b620SJohn Baldwinit is interpreted as hexadecimal.
170fa24b620SJohn BaldwinIf it begins with a prefix of
171fa24b620SJohn Baldwin.Dq 0
172fa24b620SJohn Baldwinit is interpreted as octal.
173fa24b620SJohn BaldwinOtherwise,
174fa24b620SJohn Baldwinthe value is interpreted as decimal.
175fa24b620SJohn BaldwinThe value may contain a single character suffix specifying a unit for
176fa24b620SJohn Baldwinthe value.
1779f2eb740SConrad MeyerThe interpreted value is multiplied by the unit's magnitude before being
1789f2eb740SConrad Meyerreturned.
179fa24b620SJohn BaldwinThe following unit suffixes are supported:
180fa24b620SJohn Baldwin.Bl -column -offset indent ".Sy Unit" ".Sy Magnitude"
181fa24b620SJohn Baldwin.It Sy Unit Ta Sy Magnitude
182fa24b620SJohn Baldwin.It k Ta 2^10
183fa24b620SJohn Baldwin.It m Ta 2^20
184fa24b620SJohn Baldwin.It g Ta 2^30
185fa24b620SJohn Baldwin.It t Ta 2^40
186fa24b620SJohn Baldwin.El
187fa24b620SJohn Baldwin.Pp
188fa24b620SJohn BaldwinThe
189fa24b620SJohn Baldwin.Fn getenv_string
190fa24b620SJohn Baldwinfunction stores a copy of the kernel environment variable
191fa24b620SJohn Baldwin.Fa name
192fa24b620SJohn Baldwinin the buffer described by
193fa24b620SJohn Baldwin.Fa data
194fa24b620SJohn Baldwinand
195fa24b620SJohn Baldwin.Fa size .
196fa24b620SJohn BaldwinIf the variable does not exist,
197fa24b620SJohn Baldwinzero is returned.
198fa24b620SJohn BaldwinIf the variable exists,
199fa24b620SJohn Baldwinup to
200fa24b620SJohn Baldwin.Fa size - 1
2019f2eb740SConrad Meyercharacters of its value are copied to the buffer pointed to by
202fa24b620SJohn Baldwin.Fa data
203fa24b620SJohn Baldwinfollowed by a null character and a non-zero value is returned.
204*cba446e2SMitchell Horne.Pp
205*cba446e2SMitchell HorneThe
206*cba446e2SMitchell Horne.Fn getenv_bool
207*cba446e2SMitchell Hornefunction interprets the value of the kernel environment variable
208*cba446e2SMitchell Horne.Fa name
209*cba446e2SMitchell Horneas a boolean value by performing a case-insensitive comparison against the
210*cba446e2SMitchell Hornestrings "1",
211*cba446e2SMitchell Horne"0",
212*cba446e2SMitchell Horne"true",
213*cba446e2SMitchell Horneand "false".
214*cba446e2SMitchell HorneIf the environment variable exists and has a valid boolean value, then that
215*cba446e2SMitchell Hornevalue will be copied to the variable pointed to by
216*cba446e2SMitchell Horne.Fa data .
217*cba446e2SMitchell HorneIf the environment variable exists but is not a boolean value, then a warning
218*cba446e2SMitchell Hornewill be printed to the kernel message buffer.
219*cba446e2SMitchell HorneThe
220*cba446e2SMitchell Horne.Fn getenv_is_true
221*cba446e2SMitchell Horneand
222*cba446e2SMitchell Horne.Fn getenv_is_false
223*cba446e2SMitchell Hornefunctions are wrappers around
224*cba446e2SMitchell Horne.Fn getenv_bool
225*cba446e2SMitchell Hornethat simplify testing for a desired boolean value.
226fa24b620SJohn Baldwin.Sh RETURN VALUES
227fa24b620SJohn BaldwinThe
228c8de781dSEric van Gyzen.Fn kern_getenv
229fa24b620SJohn Baldwinfunction returns a pointer to an environment variable's value on success or
230fa24b620SJohn Baldwin.Dv NULL
231fa24b620SJohn Baldwinif the variable does not exist.
232fa24b620SJohn Baldwin.Pp
233fa24b620SJohn BaldwinThe
234c8de781dSEric van Gyzen.Fn kern_setenv
235fa24b620SJohn Baldwinand
236c8de781dSEric van Gyzen.Fn kern_unsetenv
237fa24b620SJohn Baldwinfunctions return zero on success and -1 on failure.
238fa24b620SJohn Baldwin.Pp
239fa24b620SJohn BaldwinThe
240fa24b620SJohn Baldwin.Fn testenv
241fa24b620SJohn Baldwinfunction returns zero if the specified environment variable does not exist and
242fa24b620SJohn Baldwina non-zero value if it does exist.
243*cba446e2SMitchell Horne.Pp
244fa24b620SJohn BaldwinThe
245fa24b620SJohn Baldwin.Fn getenv_int ,
246fa24b620SJohn Baldwin.Fn getenv_long ,
247fa24b620SJohn Baldwin.Fn getenv_string ,
248fa24b620SJohn Baldwin.Fn getenv_quad ,
249fa24b620SJohn Baldwin.Fn getenv_uint ,
250*cba446e2SMitchell Horne.Fn getenv_ulong ,
251fa24b620SJohn Baldwinand
252*cba446e2SMitchell Horne.Fn getenv_bool
253fa24b620SJohn Baldwinfunctions return a non-zero value on success and zero on failure.
254*cba446e2SMitchell Horne.Pp
255*cba446e2SMitchell HorneThe
256*cba446e2SMitchell Horne.Fn getenv_is_true
257*cba446e2SMitchell Horneand
258*cba446e2SMitchell Horne.Fn getenv_is_false
259*cba446e2SMitchell Hornefunctions return
260*cba446e2SMitchell Horne.Dv true
261*cba446e2SMitchell Horneif the specified environment variable exists and its value matches the desired
262*cba446e2SMitchell Horneboolean condition, and
263*cba446e2SMitchell Horne.Dv false
264*cba446e2SMitchell Horneotherwise.
265