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.
This commit is contained in:
David Schultz 2004-04-28 01:27:36 +00:00
parent 06afcd9d10
commit 87a99f5ff2

View File

@ -28,12 +28,11 @@ __FBSDID("$FreeBSD$");
#include <sys/types.h> #include <sys/types.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <err.h> #include <err.h>
#include <kenv.h> #include <kenv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
static void usage(void); static void usage(void);
@ -92,9 +91,11 @@ main(int argc, char **argv)
usage(); usage();
if ((argc > 0) || (uflag && (env == NULL))) if ((argc > 0) || (uflag && (env == NULL)))
usage(); usage();
if (env == NULL) if (env == NULL) {
kdumpenv(); error = kdumpenv();
else if (val == NULL) { if (error)
warn("kdumpenv");
} else if (val == NULL) {
if (uflag) { if (uflag) {
error = kunsetenv(env); error = kunsetenv(env);
if (error) if (error)
@ -116,16 +117,28 @@ static int
kdumpenv() kdumpenv()
{ {
char *buf, *cp; char *buf, *cp;
int len; int buflen, envlen;
len = kenv(KENV_DUMP, NULL, NULL, 0); envlen = kenv(KENV_DUMP, NULL, NULL, 0);
len = len * 120 / 100; if (envlen < 0)
buf = malloc(len);
if (buf == NULL)
return (-1); return (-1);
/* Be defensive */ for (;;) {
memset(buf, 0, len); buflen = envlen * 120 / 100;
kenv(KENV_DUMP, NULL, buf, len); 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) { for (; *buf != '\0'; buf += strlen(buf) + 1) {
if (hflag) { if (hflag) {
if (strncmp(buf, "hint.", 5) != 0) if (strncmp(buf, "hint.", 5) != 0)