aboutsummaryrefslogtreecommitdiff
path: root/bin/kenv
diff options
context:
space:
mode:
authorDavid Schultz <das@FreeBSD.org>2004-04-28 01:27:36 +0000
committerDavid Schultz <das@FreeBSD.org>2004-04-28 01:27:36 +0000
commit87a99f5ff2ef9a20daa1a62949a4af688c01ce9f (patch)
tree971455053e3adf5ce3370b320b021a1839d7a37e /bin/kenv
parent06afcd9d10d89ee6dc3d48f4077ca0c2d0b9c941 (diff)
downloadsrc-87a99f5ff2ef9a20daa1a62949a4af688c01ce9f.tar.gz
src-87a99f5ff2ef9a20daa1a62949a4af688c01ce9f.zip
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.
Notes
Notes: svn path=/head/; revision=128698
Diffstat (limited to 'bin/kenv')
-rw-r--r--bin/kenv/kenv.c43
1 files changed, 28 insertions, 15 deletions
diff --git a/bin/kenv/kenv.c b/bin/kenv/kenv.c
index edd022cc3f53..6e1a75936169 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)