diff options
Diffstat (limited to 'libglusterfs/src/events.c')
| -rw-r--r-- | libglusterfs/src/events.c | 91 |
1 files changed, 47 insertions, 44 deletions
diff --git a/libglusterfs/src/events.c b/libglusterfs/src/events.c index b1fa057b81e..33157549897 100644 --- a/libglusterfs/src/events.c +++ b/libglusterfs/src/events.c @@ -19,11 +19,11 @@ #include <netinet/in.h> #include <netdb.h> -#include "syscall.h" -#include "mem-pool.h" -#include "glusterfs.h" -#include "globals.h" -#include "events.h" +#include "glusterfs/syscall.h" +#include "glusterfs/mem-pool.h" +#include "glusterfs/glusterfs.h" +#include "glusterfs/globals.h" +#include "glusterfs/events.h" #define EVENT_HOST "127.0.0.1" #define EVENT_PORT 24009 @@ -34,56 +34,66 @@ _gf_event(eventtypes_t event, const char *fmt, ...) int ret = 0; int sock = -1; char *eventstr = NULL; - struct sockaddr_in server; va_list arguments; char *msg = NULL; glusterfs_ctx_t *ctx = NULL; char *host = NULL; struct addrinfo hints; struct addrinfo *result = NULL; + struct addrinfo *iter_result_ptr = NULL; + xlator_t *this = THIS; + char *volfile_server_transport = NULL; /* Global context */ - ctx = THIS->ctx; + ctx = this->ctx; if (event < 0 || event >= EVENT_LAST) { ret = EVENT_ERROR_INVALID_INPUTS; goto out; } - /* Initialize UDP socket */ - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) { - ret = EVENT_ERROR_SOCKET; - goto out; + if (ctx) { + volfile_server_transport = ctx->cmd_args.volfile_server_transport; + } + if (!volfile_server_transport) { + volfile_server_transport = "tcp"; + } + + /* host = NULL returns localhost */ + if (ctx && ctx->cmd_args.volfile_server && + (strcmp(volfile_server_transport, "unix"))) { + /* If it is client code then volfile_server is set + use that information to push the events. */ + host = ctx->cmd_args.volfile_server; } memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_flags = AI_ADDRCONFIG; - /* 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. */ - if ((getaddrinfo(ctx->cmd_args.volfile_server, NULL, &hints, - &result)) != 0) { - ret = EVENT_ERROR_RESOLVE; - goto out; - } + if ((getaddrinfo(host, TOSTRING(EVENT_PORT), &hints, &result)) != 0) { + ret = EVENT_ERROR_RESOLVE; + goto out; + } - if (get_ip_from_addrinfo(result, &host) == NULL) { - ret = EVENT_ERROR_RESOLVE; - goto out; + // iterate over the result and break when socket creation is success. + for (iter_result_ptr = result; iter_result_ptr != NULL; + iter_result_ptr = iter_result_ptr->ai_next) { + sock = socket(iter_result_ptr->ai_family, iter_result_ptr->ai_socktype, + iter_result_ptr->ai_protocol); + if (sock != -1) { + break; } - } else { - /* Localhost, Use the defined IP for localhost */ - host = gf_strdup(EVENT_HOST); } - - /* Socket Configurations */ - server.sin_family = AF_INET; - server.sin_port = htons(EVENT_PORT); - server.sin_addr.s_addr = inet_addr(host); - memset(&server.sin_zero, '\0', sizeof(server.sin_zero)); + /* + * If none of the addrinfo structures lead to a successful socket + * creation, socket creation has failed. + */ + if (sock < 0) { + ret = EVENT_ERROR_SOCKET; + goto out; + } va_start(arguments, fmt); ret = gf_vasprintf(&msg, fmt, arguments); @@ -94,16 +104,16 @@ _gf_event(eventtypes_t event, const char *fmt, ...) goto out; } - ret = gf_asprintf(&eventstr, "%u %d %s", (unsigned)time(NULL), event, msg); - + ret = gf_asprintf(&eventstr, "%u %d %s", (unsigned)gf_time(), event, msg); + GF_FREE(msg); if (ret <= 0) { ret = EVENT_ERROR_MSG_FORMAT; goto out; } /* Send Message */ - if (sendto(sock, eventstr, strlen(eventstr), 0, (struct sockaddr *)&server, - sizeof(server)) <= 0) { + if (sendto(sock, eventstr, strlen(eventstr), 0, result->ai_addr, + result->ai_addrlen) <= 0) { ret = EVENT_ERROR_SEND; goto out; } @@ -115,17 +125,10 @@ out: sys_close(sock); } - /* Allocated by gf_vasprintf */ - if (msg) - GF_FREE(msg); - /* Allocated by gf_asprintf */ if (eventstr) GF_FREE(eventstr); - if (host) - GF_FREE(host); - if (result) freeaddrinfo(result); |
