From a7c5ac70677339dda39f1849cee1c309ade3b70d Mon Sep 17 00:00:00 2001
From: Aravinda VK <avishwan@redhat.com>
Date: Thu, 5 Jan 2017 11:28:44 +0530
Subject: [PATCH 262/267] eventsapi: Use `getaddrinfo` instead of
`gethostbyname`
`gethostbyname` is not thread safe. Use `getaddrinfo` to avoid
any race or segfault while sending events
> Reviewed-on: http://review.gluster.org/16327
> Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
> Smoke: Gluster Build System <jenkins@build.gluster.org>
> Reviewed-by: Raghavendra G <rgowdapp@redhat.com>
BUG: 1409472
Change-Id: I164af1f8eb72501fb0ed47445e68d896f7c3e908
Signed-off-by: Aravinda VK <avishwan@redhat.com>
Reviewed-on: https://code.engineering.redhat.com/gerrit/94316
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
---
libglusterfs/src/common-utils.h | 3 +++
libglusterfs/src/events.c | 25 +++++++++++++++++++------
2 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h
index 62195ed..2a6d02c 100644
--- a/libglusterfs/src/common-utils.h
+++ b/libglusterfs/src/common-utils.h
@@ -859,4 +859,7 @@ gf_bits_count (uint64_t n);
int32_t
gf_bits_index (uint64_t n);
+char *
+get_ip_from_addrinfo (struct addrinfo *addr, char **ip);
+
#endif /* _COMMON_UTILS_H */
diff --git a/libglusterfs/src/events.c b/libglusterfs/src/events.c
index 6b3a73d..27c421a 100644
--- a/libglusterfs/src/events.c
+++ b/libglusterfs/src/events.c
@@ -17,7 +17,6 @@
#include <stdarg.h>
#include <string.h>
#include <netinet/in.h>
-#include <arpa/inet.h>
#include <netdb.h>
#include "syscall.h"
@@ -41,8 +40,9 @@ _gf_event (eventtypes_t event, char *fmt, ...)
va_list arguments;
char *msg = NULL;
glusterfs_ctx_t *ctx = NULL;
- struct hostent *host_data;
char *host = NULL;
+ struct addrinfo hints;
+ struct addrinfo *result = NULL;
/* Global context */
ctx = THIS->ctx;
@@ -59,19 +59,26 @@ _gf_event (eventtypes_t event, char *fmt, ...)
goto out;
}
+ memset (&hints, 0, sizeof (hints));
+ hints.ai_family = AF_UNSPEC;
+
/* Get Host name to send message */
if (ctx && ctx->cmd_args.volfile_server) {
/* If it is client code then volfile_server is set
use that information to push the events. */
- host_data = gethostbyname (ctx->cmd_args.volfile_server);
- if (host_data == NULL) {
+ if ((getaddrinfo (ctx->cmd_args.volfile_server,
+ NULL, &hints, &result)) != 0) {
+ ret = EVENT_ERROR_RESOLVE;
+ goto out;
+ }
+
+ if (get_ip_from_addrinfo (result, &host) == NULL) {
ret = EVENT_ERROR_RESOLVE;
goto out;
}
- host = inet_ntoa (*(struct in_addr *)(host_data->h_addr));
} else {
/* Localhost, Use the defined IP for localhost */
- host = EVENT_HOST;
+ host = gf_strdup (EVENT_HOST);
}
/* Socket Configurations */
@@ -118,5 +125,11 @@ _gf_event (eventtypes_t event, char *fmt, ...)
if (eventstr)
GF_FREE (eventstr);
+ if (host)
+ GF_FREE (host);
+
+ if (result)
+ freeaddrinfo (result);
+
return ret;
}
--
2.9.3