Page MenuHomeFreeBSD

D26010.diff
No OneTemporary

D26010.diff

Index: head/sys/kern/kern_environment.c
===================================================================
--- head/sys/kern/kern_environment.c
+++ head/sys/kern/kern_environment.c
@@ -59,6 +59,9 @@
static char *_getenv_dynamic_locked(const char *name, int *idx);
static char *_getenv_dynamic(const char *name, int *idx);
+static char *kenv_acquire(const char *name);
+static void kenv_release(const char *buf);
+
static MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
#define KENV_SIZE 512 /* Maximum number of environment strings */
@@ -88,8 +91,6 @@
#define KENV_CHECK if (!dynamic_kenv) \
panic("%s: called before SI_SUB_KMEM", __func__)
-static char *getenv_string_buffer(const char *);
-
int
sys_kenv(td, uap)
struct thread *td;
@@ -482,16 +483,24 @@
char *
kern_getenv(const char *name)
{
- char *ret;
+ char *cp, *ret;
+ int len;
if (dynamic_kenv) {
- ret = getenv_string_buffer(name);
- if (ret == NULL) {
- WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
- "getenv");
+ len = KENV_MNAMELEN + 1 + kenv_mvallen + 1;
+ ret = uma_zalloc(kenv_zone, M_WAITOK | M_ZERO);
+ mtx_lock(&kenv_lock);
+ cp = _getenv_dynamic(name, NULL);
+ if (cp != NULL)
+ strlcpy(ret, cp, len);
+ mtx_unlock(&kenv_lock);
+ if (cp == NULL) {
+ uma_zfree(kenv_zone, ret);
+ ret = NULL;
}
} else
ret = _getenv_static(name);
+
return (ret);
}
@@ -503,12 +512,9 @@
{
char *cp;
- if (dynamic_kenv) {
- mtx_lock(&kenv_lock);
- cp = _getenv_dynamic(name, NULL);
- mtx_unlock(&kenv_lock);
- } else
- cp = _getenv_static(name);
+ cp = kenv_acquire(name);
+ kenv_release(cp);
+
if (cp != NULL)
return (1);
return (0);
@@ -615,30 +621,33 @@
}
/*
- * Return a buffer containing the string value from an environment variable
+ * Return the internal kenv buffer for the variable name, if it exists.
+ * If the dynamic kenv is initialized and the name is present, return
+ * with kenv_lock held.
*/
static char *
-getenv_string_buffer(const char *name)
+kenv_acquire(const char *name)
{
- char *cp, *ret;
- int len;
+ char *value;
if (dynamic_kenv) {
- len = KENV_MNAMELEN + 1 + kenv_mvallen + 1;
- ret = uma_zalloc(kenv_zone, M_WAITOK | M_ZERO);
mtx_lock(&kenv_lock);
- cp = _getenv_dynamic(name, NULL);
- if (cp != NULL)
- strlcpy(ret, cp, len);
- mtx_unlock(&kenv_lock);
- if (cp == NULL) {
- uma_zfree(kenv_zone, ret);
- ret = NULL;
- }
+ value = _getenv_dynamic(name, NULL);
+ if (value == NULL)
+ mtx_unlock(&kenv_lock);
+ return (value);
} else
- ret = _getenv_static(name);
+ return (_getenv_static(name));
+}
- return (ret);
+/*
+ * Undo a previous kenv_acquire() operation
+ */
+static void
+kenv_release(const char *buf)
+{
+ if ((buf != NULL) && dynamic_kenv)
+ mtx_unlock(&kenv_lock);
}
/*
@@ -649,17 +658,13 @@
{
char *cp;
- if (dynamic_kenv) {
- mtx_lock(&kenv_lock);
- cp = _getenv_dynamic(name, NULL);
- if (cp != NULL)
- strlcpy(data, cp, size);
- mtx_unlock(&kenv_lock);
- } else {
- cp = _getenv_static(name);
- if (cp != NULL)
- strlcpy(data, cp, size);
- }
+ cp = kenv_acquire(name);
+
+ if (cp != NULL)
+ strlcpy(data, cp, size);
+
+ kenv_release(cp);
+
return (cp != NULL);
}
@@ -673,16 +678,18 @@
uint8_t shift;
int64_t value;
int64_t old;
- char *buf;
+ const char *buf;
char *end;
- char *ptr;
+ const char *ptr;
int n;
int rc;
- if ((buf = getenv_string_buffer(name)) == NULL)
- return (0);
-
rc = 0; /* assume failure */
+
+ buf = kenv_acquire(name);
+ if (buf == NULL)
+ goto error;
+
/* get maximum number of elements */
size /= type_size;
@@ -797,8 +804,7 @@
if (n != 0)
rc = 1; /* success */
error:
- if (dynamic_kenv)
- uma_zfree(kenv_zone, buf);
+ kenv_release(buf);
return (rc);
}
@@ -898,18 +904,21 @@
int
getenv_quad(const char *name, quad_t *data)
{
- char *value, *vtp;
- quad_t iv;
+ const char *value;
+ char suffix, *vtp;
+ quad_t iv;
- value = getenv_string_buffer(name);
- if (value == NULL)
- return (0);
+ value = kenv_acquire(name);
+ if (value == NULL) {
+ goto error;
+ }
iv = strtoq(value, &vtp, 0);
if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
- freeenv(value);
- return (0);
+ goto error;
}
- switch (vtp[0]) {
+ suffix = vtp[0];
+ kenv_release(value);
+ switch (suffix) {
case 't': case 'T':
iv *= 1024;
/* FALLTHROUGH */
@@ -924,12 +933,13 @@
case '\0':
break;
default:
- freeenv(value);
return (0);
}
- freeenv(value);
*data = iv;
return (1);
+error:
+ kenv_release(value);
+ return (0);
}
/*

File Metadata

Mime Type
text/plain
Expires
Fri, Jan 17, 7:41 AM (20 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
15835954
Default Alt Text
D26010.diff (4 KB)

Event Timeline