From owner-svn-src-all@freebsd.org Mon Sep 21 15:24:46 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 0D9363F40CB; Mon, 21 Sep 2020 15:24:46 +0000 (UTC) (envelope-from mhorne@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Bw7XY6TK3z4Nnd; Mon, 21 Sep 2020 15:24:45 +0000 (UTC) (envelope-from mhorne@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id BD3F11033A; Mon, 21 Sep 2020 15:24:45 +0000 (UTC) (envelope-from mhorne@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 08LFOjnb091113; Mon, 21 Sep 2020 15:24:45 GMT (envelope-from mhorne@FreeBSD.org) Received: (from mhorne@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 08LFOiCh091108; Mon, 21 Sep 2020 15:24:44 GMT (envelope-from mhorne@FreeBSD.org) Message-Id: <202009211524.08LFOiCh091108@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mhorne set sender to mhorne@FreeBSD.org using -f From: Mitchell Horne Date: Mon, 21 Sep 2020 15:24:44 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r365945 - in head: share/man/man9 sys/kern sys/sys X-SVN-Group: head X-SVN-Commit-Author: mhorne X-SVN-Commit-Paths: in head: share/man/man9 sys/kern sys/sys X-SVN-Commit-Revision: 365945 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Sep 2020 15:24:46 -0000 Author: mhorne Date: Mon Sep 21 15:24:44 2020 New Revision: 365945 URL: https://svnweb.freebsd.org/changeset/base/365945 Log: Add getenv(9) boolean parsing functions This adds the getenv_bool() function, to parse a boolean value from a kernel environment variable or tunable. This works for traditional boolean values like "0" and "1", and also "true" and "false" (case-insensitive). These semantics do not yet apply to sysctls declared using SYSCTL_BOOL with CTLFLAG_TUN (they still only parse 1 and 0). Also added are two wrapper functions, getenv_is_true() and getenv_is_false(). These are slightly simpler for callers wishing to perform a single check of a configuration variable. Reviewed by: jhb (slightly earlier version) Sponsored by: NetApp, Inc. Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D26270 Modified: head/share/man/man9/Makefile head/share/man/man9/getenv.9 head/sys/kern/kern_environment.c head/sys/sys/kernel.h head/sys/sys/systm.h Modified: head/share/man/man9/Makefile ============================================================================== --- head/share/man/man9/Makefile Mon Sep 21 12:37:41 2020 (r365944) +++ head/share/man/man9/Makefile Mon Sep 21 15:24:44 2020 (r365945) @@ -1092,6 +1092,9 @@ MLINKS+=getenv.9 freeenv.9 \ getenv.9 getenv_quad.9 \ getenv.9 getenv_uint.9 \ getenv.9 getenv_ulong.9 \ + getenv.9 getenv_bool.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 \ Modified: head/share/man/man9/getenv.9 ============================================================================== --- head/share/man/man9/getenv.9 Mon Sep 21 12:37:41 2020 (r365944) +++ head/share/man/man9/getenv.9 Mon Sep 21 15:24:44 2020 (r365945) @@ -27,7 +27,7 @@ .\" .\" $FreeBSD$ .\" -.Dd June 1, 2017 +.Dd September 21, 2020 .Dt GETENV 9 .Os .Sh NAME @@ -39,6 +39,9 @@ .Nm getenv_quad , .Nm getenv_uint , .Nm getenv_ulong , +.Nm getenv_bool , +.Nm getenv_is_true , +.Nm getenv_is_false , .Nm kern_setenv , .Nm testenv , .Nm kern_unsetenv @@ -63,6 +66,12 @@ .Ft int .Fn getenv_ulong "const char *name" "unsigned long *data" .Ft int +.Fn getenv_bool "const char *name" "bool *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 .Fn testenv "const char *name" @@ -194,6 +203,28 @@ up to 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_bool +function interprets the value of the kernel environment variable +.Fa name +as a boolean value by performing a case-insensitive comparison against the +strings "1", +"0", +"true", +and "false". +If the environment variable exists and has a valid boolean value, then that +value will be copied to the variable pointed to by +.Fa data . +If the environment variable exists but is not a boolean value, then a warning +will be printed to the kernel message buffer. +The +.Fn getenv_is_true +and +.Fn getenv_is_false +functions are wrappers around +.Fn getenv_bool +that simplify testing for a desired boolean value. .Sh RETURN VALUES The .Fn kern_getenv @@ -211,12 +242,25 @@ The .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 , .Fn getenv_string , .Fn getenv_quad , .Fn getenv_uint , +.Fn getenv_ulong , and -.Fn getenv_ulong +.Fn getenv_bool 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. Modified: head/sys/kern/kern_environment.c ============================================================================== --- head/sys/kern/kern_environment.c Mon Sep 21 12:37:41 2020 (r365944) +++ head/sys/kern/kern_environment.c Mon Sep 21 15:24:44 2020 (r365945) @@ -942,6 +942,65 @@ error: } /* + * 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") == 0)) { + *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. */ @@ -1005,6 +1064,14 @@ tunable_quad_init(void *data) struct tunable_quad *d = (struct tunable_quad *)data; 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 Modified: head/sys/sys/kernel.h ============================================================================== --- head/sys/sys/kernel.h Mon Sep 21 12:37:41 2020 (r365944) +++ head/sys/sys/kernel.h Mon Sep 21 15:24:44 2020 (r365945) @@ -421,6 +421,25 @@ struct tunable_quad { #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; Modified: head/sys/sys/systm.h ============================================================================== --- head/sys/sys/systm.h Mon Sep 21 12:37:41 2020 (r365944) +++ head/sys/sys/systm.h Mon Sep 21 15:24:44 2020 (r365945) @@ -461,6 +461,9 @@ int getenv_string(const char *name, char *data, int si 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);