diff options
author | das <das@FreeBSD.org> | 2004-04-28 01:27:36 +0000 |
---|---|---|
committer | das <das@FreeBSD.org> | 2004-04-28 01:27:36 +0000 |
commit | c0899a25b2675b0dbea96ea34dc56a670bbcbb22 (patch) | |
tree | 971455053e3adf5ce3370b320b021a1839d7a37e /bin/kenv/kenv.c | |
parent | 9df402daa402e2e21ffef311a8c008dc0981c94f (diff) | |
download | FreeBSD-src-c0899a25b2675b0dbea96ea34dc56a670bbcbb22.zip FreeBSD-src-c0899a25b2675b0dbea96ea34dc56a670bbcbb22.tar.gz |
Various quibbles:
- Print a diagnostic if kdumpenv() fails. This can occur due to MAC
restrictions or lack of memory. Catch all kenv(2) failures as well.
- Just of the heck of it, DTRT if the kernel environment size changes
at the wrong time. The old code could fail silently or fail to
null-terminate a buffer if you got exceptionally unlucky.
- Sort and GC the #includes.
Diffstat (limited to 'bin/kenv/kenv.c')
-rw-r--r-- | bin/kenv/kenv.c | 43 |
1 files changed, 28 insertions, 15 deletions
diff --git a/bin/kenv/kenv.c b/bin/kenv/kenv.c index edd022c..6e1a759 100644 --- a/bin/kenv/kenv.c +++ b/bin/kenv/kenv.c @@ -28,12 +28,11 @@ __FBSDID("$FreeBSD$"); #include <sys/types.h> #include <sys/sysctl.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <errno.h> #include <err.h> #include <kenv.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <unistd.h> static void usage(void); @@ -92,9 +91,11 @@ main(int argc, char **argv) usage(); if ((argc > 0) || (uflag && (env == NULL))) usage(); - if (env == NULL) - kdumpenv(); - else if (val == NULL) { + if (env == NULL) { + error = kdumpenv(); + if (error) + warn("kdumpenv"); + } else if (val == NULL) { if (uflag) { error = kunsetenv(env); if (error) @@ -116,16 +117,28 @@ static int kdumpenv() { char *buf, *cp; - int len; + int buflen, envlen; - len = kenv(KENV_DUMP, NULL, NULL, 0); - len = len * 120 / 100; - buf = malloc(len); - if (buf == NULL) + envlen = kenv(KENV_DUMP, NULL, NULL, 0); + if (envlen < 0) return (-1); - /* Be defensive */ - memset(buf, 0, len); - kenv(KENV_DUMP, NULL, buf, len); + for (;;) { + buflen = envlen * 120 / 100; + buf = malloc(buflen + 1); + if (buf == NULL) + return (-1); + memset(buf, 0, buflen + 1); /* Be defensive */ + envlen = kenv(KENV_DUMP, NULL, buf, buflen); + if (envlen < 0) { + free(buf); + return (-1); + } + if (envlen > buflen) + free(buf); + else + break; + } + for (; *buf != '\0'; buf += strlen(buf) + 1) { if (hflag) { if (strncmp(buf, "hint.", 5) != 0) |