diff options
Diffstat (limited to 'libglusterfs/src')
| -rw-r--r-- | libglusterfs/src/Makefile.am | 6 | ||||
| -rw-r--r-- | libglusterfs/src/events.c | 83 | ||||
| -rw-r--r-- | libglusterfs/src/events.h.in | 23 | ||||
| -rw-r--r-- | libglusterfs/src/eventtypes.h | 22 | ||||
| -rw-r--r-- | libglusterfs/src/glusterfs.h | 4 | 
5 files changed, 138 insertions, 0 deletions
diff --git a/libglusterfs/src/Makefile.am b/libglusterfs/src/Makefile.am index 2ec0f34a670..1dbea92b2bb 100644 --- a/libglusterfs/src/Makefile.am +++ b/libglusterfs/src/Makefile.am @@ -71,6 +71,12 @@ libglusterfs_la_SOURCES += $(CONTRIBDIR)/uuid/clear.c \  	$(CONTRIBDIR)/uuid/unpack.c  endif +if BUILD_EVENTS +libglusterfs_la_SOURCES += events.c + +libglusterfs_la_HEADERS += events.h eventtypes.h +endif +  libgfchangelog_HEADERS = changelog.h  EXTRA_DIST = graph.l graph.y defaults-tmpl.c diff --git a/libglusterfs/src/events.c b/libglusterfs/src/events.c new file mode 100644 index 00000000000..9d781874a8a --- /dev/null +++ b/libglusterfs/src/events.c @@ -0,0 +1,83 @@ +/* +  Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com> +  This file is part of GlusterFS. + +  This file is licensed to you under your choice of the GNU Lesser +  General Public License, version 3 or any later version (LGPLv3 or +  later), or the GNU General Public License, version 2 (GPLv2), in all +  cases as published by the Free Software Foundation. +*/ + +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <stdio.h> +#include <unistd.h> +#include <time.h> +#include <stdarg.h> +#include <string.h> +#include "syscall.h" +#include "mem-pool.h" +#include "events.h" + +int +gf_event (int event, char *fmt, ...) +{ +        int      sock                     = -1; +        char     eventstr[EVENTS_MSG_MAX] = ""; +        struct   sockaddr_un server; +        va_list  arguments; +        char     *msg                     = NULL; +        int      ret                      = 0; +        size_t   eventstr_size            = 0; + +        if (event < 0 || event >= EVENT_LAST) { +                ret = EVENT_ERROR_INVALID_INPUTS; +                goto out; +        } + +        sock = socket(AF_UNIX, SOCK_STREAM, 0); +        if (sock < 0) { +                ret = EVENT_ERROR_SOCKET; +                goto out; +        } +        server.sun_family = AF_UNIX; +        strcpy(server.sun_path, EVENT_PATH); + +        if (connect(sock, +                    (struct sockaddr *) &server, +                    sizeof(struct sockaddr_un)) < 0) { +                ret = EVENT_ERROR_CONNECT; +                goto out; +        } + +        va_start (arguments, fmt); +        ret = gf_vasprintf (&msg, fmt, arguments); +        va_end (arguments); +        if (ret < 0) { +                ret = EVENT_ERROR_INVALID_INPUTS; +                goto out; +        } + +        eventstr_size = snprintf(NULL, 0, "%u %d %s", (unsigned)time(NULL), +                                 event, msg); + +        if (eventstr_size + 1 > EVENTS_MSG_MAX) { +                eventstr_size = EVENTS_MSG_MAX - 1; +        } + +        snprintf(eventstr, eventstr_size+1, "%u %d %s", +                 (unsigned)time(NULL), event, msg); + +        if (sys_write(sock, eventstr, strlen(eventstr)) <= 0) { +                ret = EVENT_ERROR_SEND; +                goto out; +        } + +        ret = EVENT_SEND_OK; + + out: +        sys_close(sock); +        GF_FREE(msg); +        return ret; +} diff --git a/libglusterfs/src/events.h.in b/libglusterfs/src/events.h.in new file mode 100644 index 00000000000..37692bef732 --- /dev/null +++ b/libglusterfs/src/events.h.in @@ -0,0 +1,23 @@ +/* +  Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com> +  This file is part of GlusterFS. + +  This file is licensed to you under your choice of the GNU Lesser +  General Public License, version 3 or any later version (LGPLv3 or +  later), or the GNU General Public License, version 2 (GPLv2), in all +  cases as published by the Free Software Foundation. +*/ + +#ifndef __EVENTS_H__ +#define __EVENTS_H__ + +#include <stdio.h> + +#include "eventtypes.h" + +#define EVENT_PATH "@localstatedir@/run/gluster/events.sock" +#define EVENTS_MSG_MAX 2048 + +extern int gf_event(int key, char *fmt, ...); + +#endif /* __EVENTS_H__ */ diff --git a/libglusterfs/src/eventtypes.h b/libglusterfs/src/eventtypes.h new file mode 100644 index 00000000000..874f8ccf114 --- /dev/null +++ b/libglusterfs/src/eventtypes.h @@ -0,0 +1,22 @@ +#ifndef __EVENTTYPES_H__ +#define __EVENTTYPES_H__ + +typedef enum { +    EVENT_SEND_OK, +    EVENT_ERROR_INVALID_INPUTS, +    EVENT_ERROR_SOCKET, +    EVENT_ERROR_CONNECT, +    EVENT_ERROR_SEND, +} event_errors_t; + +typedef enum { +    EVENT_PEER_ATTACH, +    EVENT_PEER_DETACH, +    EVENT_VOLUME_CREATE, +    EVENT_VOLUME_START, +    EVENT_VOLUME_STOP, +    EVENT_VOLUME_DELETE, +    EVENT_LAST +} eventtypes_t; + +#endif /* __EVENTTYPES_H__ */ diff --git a/libglusterfs/src/glusterfs.h b/libglusterfs/src/glusterfs.h index 8d387bafb3b..d11cbdcb8ee 100644 --- a/libglusterfs/src/glusterfs.h +++ b/libglusterfs/src/glusterfs.h @@ -37,6 +37,10 @@  #include "lkowner.h"  #include "compat-uuid.h" +#if (USE_EVENTS) +#include "events.h" +#endif +  #define GF_YES 1  #define GF_NO  0  | 
