diff options
author | Kyle Evans <kevans@FreeBSD.org> | 2021-06-20 19:36:10 +0000 |
---|---|---|
committer | Kyle Evans <kevans@FreeBSD.org> | 2021-07-19 04:06:19 +0000 |
commit | db0f26439357b78863e61985acd1e5acf75ce73d (patch) | |
tree | 9a30a6481206e81ec1b726294398fdd41c88896c /bin/kenv/kenv.c | |
parent | 7a129c973b5ba0fa916dfa658d523bec66dbd02d (diff) | |
download | src-db0f26439357b78863e61985acd1e5acf75ce73d.tar.gz src-db0f26439357b78863e61985acd1e5acf75ce73d.zip |
kenv: allow listing of static kernel environments
The early environment is typically cleared, so these new options
need the PRESERVE_EARLY_KENV kernel config(8) option. These environments
are reported as missing by kenv(1) if the option is not present in the
running kernel.
Reviewed by: imp
Differential Revision: https://reviews.freebsd.org/D30835
Diffstat (limited to 'bin/kenv/kenv.c')
-rw-r--r-- | bin/kenv/kenv.c | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/bin/kenv/kenv.c b/bin/kenv/kenv.c index 77caeaf5bca2..ecf30ee6b617 100644 --- a/bin/kenv/kenv.c +++ b/bin/kenv/kenv.c @@ -29,6 +29,7 @@ __FBSDID("$FreeBSD$"); #include <sys/types.h> #include <sys/sysctl.h> #include <err.h> +#include <errno.h> #include <kenv.h> #include <stdio.h> #include <stdlib.h> @@ -36,14 +37,16 @@ __FBSDID("$FreeBSD$"); #include <unistd.h> static void usage(void); -static int kdumpenv(void); +static int kdumpenv(int dump_type); static int kgetenv(const char *); static int ksetenv(const char *, char *); static int kunsetenv(const char *); static int hflag = 0; +static int lflag = 0; static int Nflag = 0; static int qflag = 0; +static int sflag = 0; static int uflag = 0; static int vflag = 0; @@ -51,7 +54,7 @@ static void usage(void) { (void)fprintf(stderr, "%s\n%s\n%s\n", - "usage: kenv [-hNq]", + "usage: kenv [-l|-s] [-hNq]", " kenv [-qv] variable[=value]", " kenv [-q] -u variable"); exit(1); @@ -65,17 +68,23 @@ main(int argc, char **argv) val = NULL; env = NULL; - while ((ch = getopt(argc, argv, "hNquv")) != -1) { + while ((ch = getopt(argc, argv, "hlNqsuv")) != -1) { switch (ch) { case 'h': hflag++; break; + case 'l': + lflag++; + break; case 'N': Nflag++; break; case 'q': qflag++; break; + case 's': + sflag++; + break; case 'u': uflag++; break; @@ -100,12 +109,23 @@ main(int argc, char **argv) } if ((hflag || Nflag) && env != NULL) usage(); + if (lflag && sflag) + usage(); if (argc > 0 || ((uflag || vflag) && env == NULL)) usage(); if (env == NULL) { - error = kdumpenv(); - if (error && !qflag) - warn("kdumpenv"); + if (lflag) + error = kdumpenv(KENV_DUMP_LOADER); + else if (sflag) + error = kdumpenv(KENV_DUMP_STATIC); + else + error = kdumpenv(KENV_DUMP); + if (error && !qflag) { + if (errno == ENOENT) + warnx("requested environment is unavailable"); + else + warn("kdumpenv"); + } } else if (val == NULL) { if (uflag) { error = kunsetenv(env); @@ -125,12 +145,12 @@ main(int argc, char **argv) } static int -kdumpenv(void) +kdumpenv(int dump_type) { char *buf, *bp, *cp; int buflen, envlen; - envlen = kenv(KENV_DUMP, NULL, NULL, 0); + envlen = kenv(dump_type, NULL, NULL, 0); if (envlen < 0) return (-1); for (;;) { @@ -138,7 +158,7 @@ kdumpenv(void) buf = calloc(1, buflen + 1); if (buf == NULL) return (-1); - envlen = kenv(KENV_DUMP, NULL, buf, buflen); + envlen = kenv(dump_type, NULL, buf, buflen); if (envlen < 0) { free(buf); return (-1); |