Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F135941935
D26270.id76926.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
5 KB
Referenced Files
None
Subscribers
None
D26270.id76926.diff
View Options
Index: share/man/man9/Makefile
===================================================================
--- share/man/man9/Makefile
+++ share/man/man9/Makefile
@@ -1093,6 +1093,8 @@
getenv.9 getenv_quad.9 \
getenv.9 getenv_uint.9 \
getenv.9 getenv_ulong.9 \
+ getenv.9 getenv_is_true.9 \
+ getenv.9 getenv_is_false.9 \
getenv.9 kern_getenv.9 \
getenv.9 kern_setenv.9 \
getenv.9 kern_unsetenv.9 \
Index: share/man/man9/getenv.9
===================================================================
--- share/man/man9/getenv.9
+++ share/man/man9/getenv.9
@@ -27,7 +27,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd June 1, 2017
+.Dd August 31, 2020
.Dt GETENV 9
.Os
.Sh NAME
@@ -39,6 +39,8 @@
.Nm getenv_quad ,
.Nm getenv_uint ,
.Nm getenv_ulong ,
+.Nm getenv_is_true ,
+.Nm getenv_is_false ,
.Nm kern_setenv ,
.Nm testenv ,
.Nm kern_unsetenv
@@ -62,6 +64,10 @@
.Fn getenv_uint "const char *name" "unsigned int *data"
.Ft int
.Fn getenv_ulong "const char *name" "unsigned long *data"
+.Ft bool
+.Fn getenv_is_true "const char *name"
+.Ft bool
+.Fn getenv_is_false "const char *name"
.Ft int
.Fn kern_setenv "const char *name" "const char *value"
.Ft int
@@ -194,6 +200,23 @@
characters of its value are copied to the buffer pointed to by
.Fa data
followed by a null character and a non-zero value is returned.
+.Pp
+The
+.Fn getenv_is_true
+and
+.Fn getenv_is_false
+functions are wrappers around
+.Fn kern_getenv
+that simplify the parsing of boolean values.
+The
+.Fn getenv_is_true
+function performs a case-insensitive comparison of the value of the kernel
+environment variable
+.Fa name
+to the strings "1" and "true".
+The
+.Fn getenv_is_false
+function works similarly for the values "0" and "false".
.Sh RETURN VALUES
The
.Fn kern_getenv
@@ -211,6 +234,7 @@
.Fn testenv
function returns zero if the specified environment variable does not exist and
a non-zero value if it does exist.
+.Pp
The
.Fn getenv_int ,
.Fn getenv_long ,
@@ -220,3 +244,14 @@
and
.Fn getenv_ulong
functions return a non-zero value on success and zero on failure.
+.Pp
+The
+.Fn getenv_is_true
+and
+.Fn getenv_is_false
+functions return
+.Dv true
+if the specified environment variable exists and its value matches the desired
+boolean condition, and
+.Dv false
+otherwise.
Index: sys/kern/kern_environment.c
===================================================================
--- sys/kern/kern_environment.c
+++ sys/kern/kern_environment.c
@@ -952,6 +952,65 @@
return (0);
}
+/*
+ * Return a boolean value from an environment variable. This can be in
+ * numerical or string form, i.e. "1" or "true".
+ */
+int
+getenv_bool(const char *name, bool *data)
+{
+ char *val;
+ int ret = 0;
+
+ if (name == NULL)
+ return (0);
+
+ val = kern_getenv(name);
+ if (val == NULL)
+ return (0);
+
+ if ((strcmp(val, "1") == 0) || (strcasecmp(val, "true")) == 0) {
+ *data = true;
+ ret = 1;
+ } else if ((strcmp(val, "0") == 0) || (strcasecmp(val, "false"))) {
+ *data = false;
+ ret = 1;
+ } else {
+ /* Spit out a warning for malformed boolean variables. */
+ printf("Environment variable %s has non-boolean value %s\n",
+ name, val);
+ }
+ freeenv(val);
+
+ return (ret);
+}
+
+/*
+ * Wrapper around getenv_bool to easily check for true.
+ */
+bool
+getenv_is_true(const char *name)
+{
+ bool val;
+
+ if (getenv_bool(name, &val) != 0)
+ return (val);
+ return (false);
+}
+
+/*
+ * Wrapper around getenv_bool to easily check for false.
+ */
+bool
+getenv_is_false(const char *name)
+{
+ bool val;
+
+ if (getenv_bool(name, &val) != 0)
+ return (!val);
+ return (false);
+}
+
/*
* Find the next entry after the one which (cp) falls within, return a
* pointer to its start or NULL if there are no more.
@@ -1018,6 +1077,14 @@
TUNABLE_QUAD_FETCH(d->path, d->var);
}
+void
+tunable_bool_init(void *data)
+{
+ struct tunable_bool *d = (struct tunable_bool *)data;
+
+ TUNABLE_BOOL_FETCH(d->path, d->var);
+}
+
void
tunable_str_init(void *data)
{
Index: sys/sys/kernel.h
===================================================================
--- sys/sys/kernel.h
+++ sys/sys/kernel.h
@@ -421,6 +421,25 @@
#define TUNABLE_QUAD_FETCH(path, var) getenv_quad((path), (var))
+/*
+ * bool
+ */
+extern void tunable_bool_init(void *);
+struct tunable_bool {
+ const char *path;
+ bool *var;
+};
+#define TUNABLE_BOOL(path, var) \
+ static struct tunable_bool __CONCAT(__tunable_bool_, __LINE__) = { \
+ (path), \
+ (var), \
+ }; \
+ SYSINIT(__CONCAT(__Tunable_init_, __LINE__), \
+ SI_SUB_TUNABLES, SI_ORDER_MIDDLE, tunable_bool_init, \
+ &__CONCAT(__tunable_bool_, __LINE__))
+
+#define TUNABLE_BOOL_FETCH(path, var) getenv_bool((path), (var))
+
extern void tunable_str_init(void *);
struct tunable_str {
const char *path;
Index: sys/sys/systm.h
===================================================================
--- sys/sys/systm.h
+++ sys/sys/systm.h
@@ -461,6 +461,9 @@
int getenv_int64(const char *name, int64_t *data);
int getenv_uint64(const char *name, uint64_t *data);
int getenv_quad(const char *name, quad_t *data);
+int getenv_bool(const char *name, bool *data);
+bool getenv_is_true(const char *name);
+bool getenv_is_false(const char *name);
int kern_setenv(const char *name, const char *value);
int kern_unsetenv(const char *name);
int testenv(const char *name);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Nov 15, 9:39 AM (2 h, 50 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
25322821
Default Alt Text
D26270.id76926.diff (5 KB)
Attached To
Mode
D26270: Add getenv_is_true() and getenv_is_false()
Attached
Detach File
Event Timeline
Log In to Comment