summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/umberlog.c20
-rw-r--r--t/test_umberlog.c2
2 files changed, 15 insertions, 7 deletions
diff --git a/lib/umberlog.c b/lib/umberlog.c
index 877818a..fa5934f 100644
--- a/lib/umberlog.c
+++ b/lib/umberlog.c
@@ -73,7 +73,7 @@ static struct
pid_t pid; /* -1 = no value cached */
uid_t uid; /* (uid_t)-1 = no value cached */
gid_t gid; /* (gid_t)-1 = no value cached */
- char hostname[_POSIX_HOST_NAME_MAX + 1];
+ char hostname[_POSIX_HOST_NAME_MAX + 1]; /* "" = no value cached */
} ul_process_data =
{
PTHREAD_MUTEX_INITIALIZER, 0, LOG_USER, NULL,
@@ -122,7 +122,10 @@ ul_openlog (const char *ident, int option, int facility)
ul_process_data.gid = getgid ();
ul_process_data.uid = getuid ();
}
- gethostname (ul_process_data.hostname, _POSIX_HOST_NAME_MAX);
+ if ((ul_process_data.flags & LOG_UL_NOCACHE) != 0)
+ ul_process_data.hostname[0] = '\0';
+ else
+ gethostname (ul_process_data.hostname, _POSIX_HOST_NAME_MAX);
pthread_mutex_unlock (&ul_process_data.lock);
}
@@ -208,11 +211,13 @@ _get_gid (void)
}
static inline const char *
-_get_hostname (void)
+_get_hostname (char *hostname_buffer)
{
- if (ul_process_data.flags & LOG_UL_NOCACHE)
- gethostname (ul_process_data.hostname, _POSIX_HOST_NAME_MAX);
- return ul_process_data.hostname;
+ if (ul_process_data.hostname[0] != '\0')
+ return ul_process_data.hostname;
+
+ gethostname (hostname_buffer, _POSIX_HOST_NAME_MAX);
+ return hostname_buffer;
}
static inline const char *
@@ -520,6 +525,7 @@ _ul_json_append_timestamp (ul_buffer_t *buffer)
static inline ul_buffer_t *
_ul_discover (ul_buffer_t *buffer, int priority)
{
+ char hostname_buffer[_POSIX_HOST_NAME_MAX + 1];
const char *ident;
if (ul_process_data.flags & LOG_UL_NODISCOVER)
@@ -531,7 +537,7 @@ _ul_discover (ul_buffer_t *buffer, int priority)
"priority", "%s", _find_prio (priority),
"uid", "%d", _get_uid (),
"gid", "%d", _get_gid (),
- "host", "%s", _get_hostname (),
+ "host", "%s", _get_hostname (hostname_buffer),
NULL);
if (buffer == NULL)
return buffer;
diff --git a/t/test_umberlog.c b/t/test_umberlog.c
index 8a47ab1..cc437cf 100644
--- a/t/test_umberlog.c
+++ b/t/test_umberlog.c
@@ -295,6 +295,7 @@ START_TEST (test_closelog)
verify_value_differs (jo, "uid", "0");
if (getgid () != 0)
verify_value_differs (jo, "gid", "0");
+ verify_value_differs (jo, "host", "");
json_object_put (jo);
}
@@ -354,6 +355,7 @@ START_TEST (test_openlog_defaults)
verify_value_differs (jo, "uid", "0");
if (getgid () != 0)
verify_value_differs (jo, "gid", "0");
+ verify_value_differs (jo, "host", "");
json_object_put (jo);
closelog ();