From fa56155c34eed72740d375208a63571d5f9d59e0 Mon Sep 17 00:00:00 2001 From: "campbellsean@google.com" Date: Fri, 6 Oct 2017 13:44:38 -0400 Subject: [PATCH] Adds setter for hostname_g and eliminates hostname_g_size variable. --- src/daemon/collectd.c | 6 +++--- src/daemon/globals.c | 12 +++++++----- src/daemon/globals.h | 6 ++++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/daemon/collectd.c b/src/daemon/collectd.c index 5dfe7c34..8f671b35 100644 --- a/src/daemon/collectd.c +++ b/src/daemon/collectd.c @@ -83,11 +83,11 @@ static int init_hostname(void) { str = global_option_get("Hostname"); if ((str != NULL) && (str[0] != 0)) { - sstrncpy(hostname_g, str, hostname_g_size); + hostname_set(str); return 0; } - if (gethostname(hostname_g, hostname_g_size) != 0) { + if (gethostname(hostname_g, sizeof(hostname_g)) != 0) { fprintf(stderr, "`gethostname' failed and no " "hostname was configured.\n"); return -1; @@ -115,7 +115,7 @@ static int init_hostname(void) { if (ai_ptr->ai_canonname == NULL) continue; - sstrncpy(hostname_g, ai_ptr->ai_canonname, hostname_g_size); + hostname_set(ai_ptr->ai_canonname); break; } diff --git a/src/daemon/globals.c b/src/daemon/globals.c index 398e2dd2..bde7eb22 100644 --- a/src/daemon/globals.c +++ b/src/daemon/globals.c @@ -21,13 +21,15 @@ * DEALINGS IN THE SOFTWARE. **/ +#include "common.h" #include "globals.h" #include "plugin.h" -/* - * Global variables - */ -char hostname_g[DATA_MAX_NAME_LEN]; -const int hostname_g_size = sizeof (hostname_g); + +void hostname_set(char const *hostname) { + sstrncpy(hostname_g, hostname, sizeof(hostname_g)); +} + +char *hostname_g; cdtime_t interval_g; int pidfile_from_cli = 0; int timeout_g; diff --git a/src/daemon/globals.h b/src/daemon/globals.h index 9ccac582..58700316 100644 --- a/src/daemon/globals.h +++ b/src/daemon/globals.h @@ -29,8 +29,10 @@ /* Type for time as used by "utils_time.h" */ typedef uint64_t cdtime_t; -extern char hostname_g[]; -extern const int hostname_g_size; +/* hostname_set updates hostname_g */ +void hostname_set(char const *hostname); + +extern char *hostname_g; extern cdtime_t interval_g; extern int pidfile_from_cli; extern int timeout_g; -- 2.11.0