summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAravinda VK <avishwan@redhat.com>2017-01-05 11:28:44 +0530
committerRaghavendra G <rgowdapp@redhat.com>2017-01-05 20:24:18 -0800
commitaa053b228e01ab079f86d24f3444b2389895effd (patch)
tree765fd27aaeea5bbc2f2bf129890fe346253400c6
parentc455c6030f1f1678ae66752e5e11ea988d062df4 (diff)
eventsapi: Use `getaddrinfo` instead of `gethostbyname`
`gethostbyname` is not thread safe. Use `getaddrinfo` to avoid any race or segfault while sending events BUG: 1410313 Change-Id: I164af1f8eb72501fb0ed47445e68d896f7c3e908 Signed-off-by: Aravinda VK <avishwan@redhat.com> 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>
-rw-r--r--libglusterfs/src/common-utils.h4
-rw-r--r--libglusterfs/src/events.c25
2 files changed, 23 insertions, 6 deletions
diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h
index 324555b34f5..b77b7ad97de 100644
--- a/libglusterfs/src/common-utils.h
+++ b/libglusterfs/src/common-utils.h
@@ -868,4 +868,8 @@ gf_bits_index (uint64_t n);
const char*
gf_fop_string (glusterfs_fop_t fop);
+
+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 6b3a73d8ae4..27c421a7c74 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;
}