summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/events.c
blob: 9d781874a8a32badeeacb1b044c33c375e5a3090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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;
}