xref: /freebsd/share/man/man9/getenv.9 (revision cba446e2c28589878f09214341629349df25a090)
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.\"
28fa24b620SJohn Baldwin.\" $FreeBSD$
29fa24b620SJohn Baldwin.\"
30*cba446e2SMitchell Horne.Dd September 21, 2020
31457a378fSJoel Dahl.Dt GETENV 9
32fa24b620SJohn Baldwin.Os
33fa24b620SJohn Baldwin.Sh NAME
34fa24b620SJohn Baldwin.Nm freeenv ,
35c8de781dSEric van Gyzen.Nm kern_getenv ,
36fa24b620SJohn Baldwin.Nm getenv_int ,
37fa24b620SJohn Baldwin.Nm getenv_long ,
38fa24b620SJohn Baldwin.Nm getenv_string ,
39fa24b620SJohn Baldwin.Nm getenv_quad ,
40fa24b620SJohn Baldwin.Nm getenv_uint ,
41fa24b620SJohn Baldwin.Nm getenv_ulong ,
42*cba446e2SMitchell Horne.Nm getenv_bool ,
43*cba446e2SMitchell Horne.Nm getenv_is_true ,
44*cba446e2SMitchell Horne.Nm getenv_is_false ,
45c8de781dSEric van Gyzen.Nm kern_setenv ,
46fa24b620SJohn Baldwin.Nm testenv ,
47c8de781dSEric van Gyzen.Nm kern_unsetenv
48fa24b620SJohn Baldwin.Nd kernel environment variable functions
49fa24b620SJohn Baldwin.Sh SYNOPSIS
50fa24b620SJohn Baldwin.In sys/param.h
51fa24b620SJohn Baldwin.In sys/systm.h
52fa24b620SJohn Baldwin.Ft void
53fa24b620SJohn Baldwin.Fn freeenv "char *env"
54fa24b620SJohn Baldwin.Ft char *
55c8de781dSEric van Gyzen.Fn kern_getenv "const char *name"
56fa24b620SJohn Baldwin.Ft int
57fa24b620SJohn Baldwin.Fn getenv_int "const char *name" "int *data"
58fa24b620SJohn Baldwin.Ft int
59fa24b620SJohn Baldwin.Fn getenv_long "const char *name" "long *data"
60fa24b620SJohn Baldwin.Ft int
61fa24b620SJohn Baldwin.Fn getenv_string "const char *name" "char *data" "int size"
62fa24b620SJohn Baldwin.Ft int
63fa24b620SJohn Baldwin.Fn getenv_quad "const char *name" "quad_t *data"
64fa24b620SJohn Baldwin.Ft int
65fa24b620SJohn Baldwin.Fn getenv_uint "const char *name" "unsigned int *data"
66fa24b620SJohn Baldwin.Ft int
67fa24b620SJohn Baldwin.Fn getenv_ulong "const char *name" "unsigned long *data"
68fa24b620SJohn Baldwin.Ft int
69*cba446e2SMitchell Horne.Fn getenv_bool "const char *name" "bool *data"
70*cba446e2SMitchell Horne.Ft bool
71*cba446e2SMitchell Horne.Fn getenv_is_true "const char *name"
72*cba446e2SMitchell Horne.Ft bool
73*cba446e2SMitchell Horne.Fn getenv_is_false "const char *name"
74*cba446e2SMitchell Horne.Ft int
75c8de781dSEric van Gyzen.Fn kern_setenv "const char *name" "const char *value"
76fa24b620SJohn Baldwin.Ft int
77fa24b620SJohn Baldwin.Fn testenv "const char *name"
78fa24b620SJohn Baldwin.Ft int
79c8de781dSEric van Gyzen.Fn kern_unsetenv "const char *name"
80fa24b620SJohn Baldwin.Sh DESCRIPTION
81fa24b620SJohn BaldwinThese functions set, unset, fetch, and parse variables from the kernel's
82fa24b620SJohn Baldwinenvironment.
83fa24b620SJohn Baldwin.Pp
84fa24b620SJohn BaldwinThe
85c8de781dSEric van Gyzen.Fn kern_getenv
86fa24b620SJohn Baldwinfunction obtains the current value of the kernel environment variable
87fa24b620SJohn Baldwin.Fa name
88fa24b620SJohn Baldwinand returns a pointer to the string value.
89fa24b620SJohn BaldwinThe caller should not modify the string pointed to by the return value.
90fa24b620SJohn BaldwinThe
91c8de781dSEric van Gyzen.Fn kern_getenv
92fa24b620SJohn Baldwinfunction may allocate temporary storage,
93fa24b620SJohn Baldwinso the
94fa24b620SJohn Baldwin.Fn freeenv
95fa24b620SJohn Baldwinfunction must be called to release any allocated resources when the value
96fa24b620SJohn Baldwinreturned by
97c8de781dSEric van Gyzen.Fn kern_getenv
98fa24b620SJohn Baldwinis no longer needed.
999f2eb740SConrad Meyer.Pp
1009f2eb740SConrad MeyerThe
1019f2eb740SConrad Meyer.Fn freeenv
1029f2eb740SConrad Meyerfunction is used to release the resources allocated by a previous call to
103c8de781dSEric van Gyzen.Fn kern_getenv .
104fa24b620SJohn BaldwinThe
105fa24b620SJohn Baldwin.Fa env
106fa24b620SJohn Baldwinargument passed to
107fa24b620SJohn Baldwin.Fn freeenv
108fa24b620SJohn Baldwinis the pointer returned by the earlier call to
109c8de781dSEric van Gyzen.Fn kern_getenv .
1109f2eb740SConrad MeyerLike
1119f2eb740SConrad Meyer.Xr free 3 ,
1129f2eb740SConrad Meyerthe
1139f2eb740SConrad Meyer.Fa env
1149f2eb740SConrad Meyerargument can be
1159f2eb740SConrad Meyer.Va NULL ,
1169f2eb740SConrad Meyerin which case no action occurs.
117fa24b620SJohn Baldwin.Pp
118fa24b620SJohn BaldwinThe
119c8de781dSEric van Gyzen.Fn kern_setenv
120fa24b620SJohn Baldwinfunction inserts or resets the kernel environment variable
121fa24b620SJohn Baldwin.Fa name
122fa24b620SJohn Baldwinto
123fa24b620SJohn Baldwin.Fa value .
124fa24b620SJohn BaldwinIf the variable
125fa24b620SJohn Baldwin.Fa name
126fa24b620SJohn Baldwinalready exists,
127fa24b620SJohn Baldwinits value is replaced.
128fa24b620SJohn BaldwinThis function can fail if an internal limit on the number of environment
129fa24b620SJohn Baldwinvariables is exceeded.
130fa24b620SJohn Baldwin.Pp
131fa24b620SJohn BaldwinThe
132c8de781dSEric van Gyzen.Fn kern_unsetenv
133fa24b620SJohn Baldwinfunction deletes the kernel environment variable
134fa24b620SJohn Baldwin.Fa name .
135fa24b620SJohn Baldwin.Pp
136fa24b620SJohn BaldwinThe
137fa24b620SJohn Baldwin.Fn testenv
138fa24b620SJohn Baldwinfunction is used to determine if a kernel environment variable exists.
139fa24b620SJohn BaldwinIt returns a non-zero value if the variable
140fa24b620SJohn Baldwin.Fa name
141fa24b620SJohn Baldwinexists and zero if it does not.
142fa24b620SJohn Baldwin.Pp
143fa24b620SJohn BaldwinThe
144fa24b620SJohn Baldwin.Fn getenv_int ,
145fa24b620SJohn Baldwin.Fn getenv_long ,
146fa24b620SJohn Baldwin.Fn getenv_quad ,
147fa24b620SJohn Baldwin.Fn getenv_uint ,
148fa24b620SJohn Baldwinand
149fa24b620SJohn Baldwin.Fn getenv_ulong
150fa24b620SJohn Baldwinfunctions look for a kernel environment variable
151fa24b620SJohn Baldwin.Fa name
152fa24b620SJohn Baldwinand parse it as a signed integer,
153fa24b620SJohn Baldwinlong integer,
154fa24b620SJohn Baldwinsigned 64-bit integer,
155fa24b620SJohn Baldwinunsigned integer,
156fa24b620SJohn Baldwinor an unsigned long integer,
157fa24b620SJohn Baldwinrespectively.
158fa24b620SJohn BaldwinThese functions fail and return zero if
159fa24b620SJohn Baldwin.Fa name
160fa24b620SJohn Baldwindoes not exist or if any invalid characters are present in its value.
161fa24b620SJohn BaldwinOn success,
162fa24b620SJohn Baldwinthese function store the parsed value in the integer variable pointed to
163fa24b620SJohn Baldwinby
164fa24b620SJohn Baldwin.Fa data .
165fa24b620SJohn BaldwinIf the parsed value overflows the integer type,
166fa24b620SJohn Baldwina truncated value is stored in
167fa24b620SJohn Baldwin.Fa data
168fa24b620SJohn Baldwinand zero is returned.
169fa24b620SJohn BaldwinIf the value begins with a prefix of
170fa24b620SJohn Baldwin.Dq 0x
171fa24b620SJohn Baldwinit is interpreted as hexadecimal.
172fa24b620SJohn BaldwinIf it begins with a prefix of
173fa24b620SJohn Baldwin.Dq 0
174fa24b620SJohn Baldwinit is interpreted as octal.
175fa24b620SJohn BaldwinOtherwise,
176fa24b620SJohn Baldwinthe value is interpreted as decimal.
177fa24b620SJohn BaldwinThe value may contain a single character suffix specifying a unit for
178fa24b620SJohn Baldwinthe value.
1799f2eb740SConrad MeyerThe interpreted value is multiplied by the unit's magnitude before being
1809f2eb740SConrad Meyerreturned.
181fa24b620SJohn BaldwinThe following unit suffixes are supported:
182fa24b620SJohn Baldwin.Bl -column -offset indent ".Sy Unit" ".Sy Magnitude"
183fa24b620SJohn Baldwin.It Sy Unit Ta Sy Magnitude
184fa24b620SJohn Baldwin.It k Ta 2^10
185fa24b620SJohn Baldwin.It m Ta 2^20
186fa24b620SJohn Baldwin.It g Ta 2^30
187fa24b620SJohn Baldwin.It t Ta 2^40
188fa24b620SJohn Baldwin.El
189fa24b620SJohn Baldwin.Pp
190fa24b620SJohn BaldwinThe
191fa24b620SJohn Baldwin.Fn getenv_string
192fa24b620SJohn Baldwinfunction stores a copy of the kernel environment variable
193fa24b620SJohn Baldwin.Fa name
194fa24b620SJohn Baldwinin the buffer described by
195fa24b620SJohn Baldwin.Fa data
196fa24b620SJohn Baldwinand
197fa24b620SJohn Baldwin.Fa size .
198fa24b620SJohn BaldwinIf the variable does not exist,
199fa24b620SJohn Baldwinzero is returned.
200fa24b620SJohn BaldwinIf the variable exists,
201fa24b620SJohn Baldwinup to
202fa24b620SJohn Baldwin.Fa size - 1
2039f2eb740SConrad Meyercharacters of its value are copied to the buffer pointed to by
204fa24b620SJohn Baldwin.Fa data
205fa24b620SJohn Baldwinfollowed by a null character and a non-zero value is returned.
206*cba446e2SMitchell Horne.Pp
207*cba446e2SMitchell HorneThe
208*cba446e2SMitchell Horne.Fn getenv_bool
209*cba446e2SMitchell Hornefunction interprets the value of the kernel environment variable
210*cba446e2SMitchell Horne.Fa name
211*cba446e2SMitchell Horneas a boolean value by performing a case-insensitive comparison against the
212*cba446e2SMitchell Hornestrings "1",
213*cba446e2SMitchell Horne"0",
214*cba446e2SMitchell Horne"true",
215*cba446e2SMitchell Horneand "false".
216*cba446e2SMitchell HorneIf the environment variable exists and has a valid boolean value, then that
217*cba446e2SMitchell Hornevalue will be copied to the variable pointed to by
218*cba446e2SMitchell Horne.Fa data .
219*cba446e2SMitchell HorneIf the environment variable exists but is not a boolean value, then a warning
220*cba446e2SMitchell Hornewill be printed to the kernel message buffer.
221*cba446e2SMitchell HorneThe
222*cba446e2SMitchell Horne.Fn getenv_is_true
223*cba446e2SMitchell Horneand
224*cba446e2SMitchell Horne.Fn getenv_is_false
225*cba446e2SMitchell Hornefunctions are wrappers around
226*cba446e2SMitchell Horne.Fn getenv_bool
227*cba446e2SMitchell Hornethat simplify testing for a desired boolean value.
228fa24b620SJohn Baldwin.Sh RETURN VALUES
229fa24b620SJohn BaldwinThe
230c8de781dSEric van Gyzen.Fn kern_getenv
231fa24b620SJohn Baldwinfunction returns a pointer to an environment variable's value on success or
232fa24b620SJohn Baldwin.Dv NULL
233fa24b620SJohn Baldwinif the variable does not exist.
234fa24b620SJohn Baldwin.Pp
235fa24b620SJohn BaldwinThe
236c8de781dSEric van Gyzen.Fn kern_setenv
237fa24b620SJohn Baldwinand
238c8de781dSEric van Gyzen.Fn kern_unsetenv
239fa24b620SJohn Baldwinfunctions return zero on success and -1 on failure.
240fa24b620SJohn Baldwin.Pp
241fa24b620SJohn BaldwinThe
242fa24b620SJohn Baldwin.Fn testenv
243fa24b620SJohn Baldwinfunction returns zero if the specified environment variable does not exist and
244fa24b620SJohn Baldwina non-zero value if it does exist.
245*cba446e2SMitchell Horne.Pp
246fa24b620SJohn BaldwinThe
247fa24b620SJohn Baldwin.Fn getenv_int ,
248fa24b620SJohn Baldwin.Fn getenv_long ,
249fa24b620SJohn Baldwin.Fn getenv_string ,
250fa24b620SJohn Baldwin.Fn getenv_quad ,
251fa24b620SJohn Baldwin.Fn getenv_uint ,
252*cba446e2SMitchell Horne.Fn getenv_ulong ,
253fa24b620SJohn Baldwinand
254*cba446e2SMitchell Horne.Fn getenv_bool
255fa24b620SJohn Baldwinfunctions return a non-zero value on success and zero on failure.
256*cba446e2SMitchell Horne.Pp
257*cba446e2SMitchell HorneThe
258*cba446e2SMitchell Horne.Fn getenv_is_true
259*cba446e2SMitchell Horneand
260*cba446e2SMitchell Horne.Fn getenv_is_false
261*cba446e2SMitchell Hornefunctions return
262*cba446e2SMitchell Horne.Dv true
263*cba446e2SMitchell Horneif the specified environment variable exists and its value matches the desired
264*cba446e2SMitchell Horneboolean condition, and
265*cba446e2SMitchell Horne.Dv false
266*cba446e2SMitchell Horneotherwise.
267