glusterfs

Форк
0
135 строк · 3.4 Кб
1
/*
2
  Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com>
3
  This file is part of GlusterFS.
4

5
  This file is licensed to you under your choice of the GNU Lesser
6
  General Public License, version 3 or any later version (LGPLv3 or
7
  later), or the GNU General Public License, version 2 (GPLv2), in all
8
  cases as published by the Free Software Foundation.
9
*/
10

11
#include <sys/types.h>
12
#include <sys/socket.h>
13
#include <sys/un.h>
14
#include <stdio.h>
15
#include <unistd.h>
16
#include <time.h>
17
#include <stdarg.h>
18
#include <string.h>
19
#include <netinet/in.h>
20
#include <netdb.h>
21

22
#include "glusterfs/syscall.h"
23
#include "glusterfs/mem-pool.h"
24
#include "glusterfs/globals.h"
25
#include "glusterfs/events.h"
26

27
#define EVENT_HOST "127.0.0.1"
28
#define EVENT_PORT 55555
29

30
int
31
_gf_event(eventtypes_t event, const char *fmt, ...)
32
{
33
    int ret = 0;
34
    int sock = -1;
35
    char *eventstr = NULL;
36
    va_list arguments;
37
    char *msg = NULL;
38
    glusterfs_ctx_t *ctx = NULL;
39
    char *host = NULL;
40
    struct addrinfo hints;
41
    struct addrinfo *result = NULL;
42
    struct addrinfo *iter_result_ptr = NULL;
43
    xlator_t *this = THIS;
44
    char *volfile_server_transport = NULL;
45

46
    /* Global context */
47
    ctx = this->ctx;
48

49
    if (event < 0 || event >= EVENT_LAST) {
50
        ret = EVENT_ERROR_INVALID_INPUTS;
51
        goto out;
52
    }
53

54
    if (ctx) {
55
        volfile_server_transport = ctx->cmd_args.volfile_server_transport;
56
    }
57
    if (!volfile_server_transport) {
58
        volfile_server_transport = "tcp";
59
    }
60

61
    /* host = NULL returns localhost */
62
    if (ctx && ctx->cmd_args.volfile_server &&
63
        (strcmp(volfile_server_transport, "unix"))) {
64
        /* If it is client code then volfile_server is set
65
           use that information to push the events. */
66
        host = ctx->cmd_args.volfile_server;
67
    }
68

69
    memset(&hints, 0, sizeof(hints));
70
    hints.ai_family = AF_UNSPEC;
71
    hints.ai_socktype = SOCK_DGRAM;
72
    hints.ai_flags = AI_ADDRCONFIG;
73

74
    if ((getaddrinfo(host, TOSTRING(EVENT_PORT), &hints, &result)) != 0) {
75
        ret = EVENT_ERROR_RESOLVE;
76
        goto out;
77
    }
78

79
    // iterate over the result and break when socket creation is success.
80
    for (iter_result_ptr = result; iter_result_ptr != NULL;
81
         iter_result_ptr = iter_result_ptr->ai_next) {
82
        sock = socket(iter_result_ptr->ai_family, iter_result_ptr->ai_socktype,
83
                      iter_result_ptr->ai_protocol);
84
        if (sock != -1) {
85
            break;
86
        }
87
    }
88
    /*
89
     * If none of the addrinfo structures lead to a successful socket
90
     * creation, socket creation has failed.
91
     */
92
    if (sock < 0) {
93
        ret = EVENT_ERROR_SOCKET;
94
        goto out;
95
    }
96

97
    va_start(arguments, fmt);
98
    ret = gf_vasprintf(&msg, fmt, arguments);
99
    va_end(arguments);
100

101
    if (ret < 0) {
102
        ret = EVENT_ERROR_INVALID_INPUTS;
103
        goto out;
104
    }
105

106
    ret = gf_asprintf(&eventstr, "%u %d %s", (unsigned)gf_time(), event, msg);
107
    GF_FREE(msg);
108
    if (ret <= 0) {
109
        ret = EVENT_ERROR_MSG_FORMAT;
110
        goto out;
111
    }
112

113
    /* Send Message */
114
    if (sendto(sock, eventstr, strlen(eventstr), 0, result->ai_addr,
115
               result->ai_addrlen) <= 0) {
116
        ret = EVENT_ERROR_SEND;
117
        goto out;
118
    }
119

120
    ret = EVENT_SEND_OK;
121

122
out:
123
    if (sock >= 0) {
124
        sys_close(sock);
125
    }
126

127
    /* Allocated by gf_asprintf */
128
    if (eventstr)
129
        GF_FREE(eventstr);
130

131
    if (result)
132
        freeaddrinfo(result);
133

134
    return ret;
135
}
136

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.