summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiloslav Trmač <mitr@redhat.com>2012-07-24 14:08:22 +0200
committerMiloslav Trmač <mitr@redhat.com>2012-07-30 07:42:01 +0200
commite26286847d4325d4c75cb5c661ddfc10af840177 (patch)
tree8924afbb5ace0a473818fadc0f25110eefab22b8
parentc50904d3d0dbc95588eccdf147d9e33b6edece58 (diff)
downloadlibumberlog-e26286847d4325d4c75cb5c661ddfc10af840177.tar.gz
libumberlog-e26286847d4325d4c75cb5c661ddfc10af840177.tar.xz
libumberlog-e26286847d4325d4c75cb5c661ddfc10af840177.zip
Mark missing host name separately from flags.
This simplifies the syslog() path a little, and allows us to express "host name should be cached but no value is set.". Because host name field is already emptied by default and in closelog (), this also means uncached data is used before openlog () and after closelog (). Signed-off-by: Miloslav Trmač <mitr@redhat.com>
-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 ();