summaryrefslogtreecommitdiffstats
path: root/xlators/features/changelog/src/changelog-notifier.c
blob: 1f8b312538e6b05d036935dcbe7809cb83ee2526 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
   Copyright (c) 2013 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 "changelog-notifier.h"

#include <pthread.h>

inline static void
changelog_notify_clear_fd (changelog_notify_t *cn, int i)
{
        cn->client_fd[i] = -1;
}

inline static void
changelog_notify_save_fd (changelog_notify_t *cn, int i, int fd)
{
        cn->client_fd[i] = fd;
}

static int
changelog_notify_insert_fd (xlator_t *this, changelog_notify_t *cn, int fd)
{
        int i   = 0;
        int ret = 0;

        for (; i < CHANGELOG_MAX_CLIENTS; i++) {
                if (cn->client_fd[i] == -1)
                        break;
        }

        if (i == CHANGELOG_MAX_CLIENTS) {
                /**
                 * this case should not be hit as listen() would limit
                 * the number of completely established connections.
                 */
                gf_log (this->name, GF_LOG_WARNING,
                        "hit max client limit (%d)", CHANGELOG_MAX_CLIENTS);
                ret = -1;
        }
        else
                changelog_notify_save_fd (cn, i, fd);

        return ret;
}

static void
changelog_notify_fill_rset (changelog_notify_t *cn, fd_set *rset, int *maxfd)
{
        int i = 0;

        FD_ZERO (rset);

        FD_SET (cn->socket_fd, rset);
        *maxfd = cn->socket_fd;

        FD_SET (cn->rfd, rset);
        *maxfd = max (*maxfd, cn->rfd);

        for (; i < CHANGELOG_MAX_CLIENTS; i++) {
                if (cn->client_fd[i] != -1) {
                        FD_SET (cn->client_fd[i], rset);
                        *maxfd = max (*maxfd, cn->client_fd[i]);
                }
        }

        *maxfd = *maxfd + 1;
}

static int
changelog_notify_client (changelog_notify_t *cn, char *path, ssize_t len)
{
        int i = 0;
        int ret = 0;

        for (; i < CHANGELOG_MAX_CLIENTS; i++) {
                if (cn->client_fd[i] == -1)
                        continue;

                if (changelog_write (cn->client_fd[i],
                                     path, len)) {
                        ret = -1;

                        close (cn->client_fd[i]);
                        changelog_notify_clear_fd (cn, i);
                }
        }

        return ret;
}

static void
changelog_notifier_init (changelog_notify_t *cn)
{
        int i = 0;

        cn->socket_fd = -1;

        for (; i < CHANGELOG_MAX_CLIENTS; i++) {
                changelog_notify_clear_fd (cn, i);
        }
}

static void
changelog_close_client_conn (changelog_notify_t *cn)
{
        int i = 0;

        for (; i < CHANGELOG_MAX_CLIENTS; i++) {
                if (cn->client_fd[i] == -1)
                        continue;

                close (cn->client_fd[i]);
                changelog_notify_clear_fd (cn, i);
        }
}

static void
changelog_notifier_cleanup (void *arg)
{
        changelog_notify_t *cn = NULL;

        cn = (changelog_notify_t *) arg;

        changelog_close_client_conn (cn);

        if (cn->socket_fd != -1)
                close (cn->socket_fd);

        if (cn->rfd)
                close (cn->rfd);

        if (unlink (cn->sockpath))
                gf_log ("", GF_LOG_WARNING,
                        "could not unlink changelog socket file"
                        " %s (reason: %s", cn->sockpath, strerror (errno));
}

void *
changelog_notifier (void *data)
{
        int                 i         = 0;
        int                 fd        = 0;
        int                 max_fd    = 0;
        int                 len       = 0;
        ssize_t             readlen   = 0;
        xlator_t           *this      = NULL;
        changelog_priv_t   *priv      = NULL;
        changelog_notify_t *cn        = NULL;
        struct sockaddr_un  local     = {0,};
        char path[PATH_MAX]           = {0,};
        char abspath[PATH_MAX]        = {0,};

        char buffer;
        fd_set rset;

        priv = (changelog_priv_t *) data;

        cn = &priv->cn;
        this = cn->this;

        pthread_cleanup_push (changelog_notifier_cleanup, cn);

        changelog_notifier_init (cn);

        cn->socket_fd = socket (AF_UNIX, SOCK_STREAM, 0);
        if (cn->socket_fd < 0) {
                gf_log (this->name, GF_LOG_ERROR,
                        "changelog socket error (reason: %s)",
                        strerror (errno));
                goto out;
        }

        CHANGELOG_MAKE_SOCKET_PATH (priv->changelog_brick,
                                    cn->sockpath, PATH_MAX);
        if (unlink (cn->sockpath) < 0) {
                if (errno != ENOENT) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "Could not unlink changelog socket file (%s)"
                                " (reason: %s)",
                                CHANGELOG_UNIX_SOCK, strerror (errno));
                        goto cleanup;
                }
        }

        local.sun_family = AF_UNIX;
        strcpy (local.sun_path, cn->sockpath);

        len = strlen (local.sun_path) + sizeof (local.sun_family);

        /* bind to the unix domain socket */
        if (bind (cn->socket_fd, (struct sockaddr *) &local, len) < 0) {
                gf_log (this->name, GF_LOG_ERROR,
                        "Could not bind to changelog socket (reason: %s)",
                        strerror (errno));
                goto cleanup;
        }

        /* listen for incoming connections */
        if (listen (cn->socket_fd, CHANGELOG_MAX_CLIENTS) < 0) {
                gf_log (this->name, GF_LOG_ERROR,
                        "listen() error on changelog socket (reason: %s)",
                        strerror (errno));
                goto cleanup;
        }

        /**
         * simple select() on all to-be-read file descriptors. This method
         * though old school works pretty well when you have a handfull of
         * fd's to be watched (clients).
         *
         * Future TODO: move this to epoll based notification facility if
         *              number of clients increase.
         */
        for (;;) {
                changelog_notify_fill_rset (cn, &rset, &max_fd);

                if (select (max_fd, &rset, NULL, NULL, NULL) < 0) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "select() returned -1 (reason: %s)",
                                strerror (errno));
                        sleep (2);
                        continue;
                }

                if (FD_ISSET (cn->socket_fd, &rset)) {
                        fd = accept (cn->socket_fd, NULL, NULL);
                        if (fd < 0) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "accept error on changelog socket"
                                        " (reason: %s)", strerror (errno));
                        } else if (changelog_notify_insert_fd (this, cn, fd)) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "hit max client limit");
                        }
                }

                if (FD_ISSET (cn->rfd, &rset)) {
                        /**
                         * read changelog filename and notify all connected
                         * clients.
                         */
                        readlen = 0;
                        while (readlen < PATH_MAX) {
                                len = read (cn->rfd, &path[readlen++], 1);
                                if (len == -1) {
                                        break;
                                }

                                if (len == 0) {
                                        gf_log (this->name, GF_LOG_ERROR,
                                                "rollover thread sent EOF"
                                                " on pipe - possibly a crash.");
                                        /* be blunt and close all connections */
                                        pthread_exit(NULL);
                                }

                                if (path[readlen - 1] == '\0')
                                        break;
                        }

                        /* should we close all client connections here too? */
                        if (len < 0 || readlen == PATH_MAX) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "Could not get pathname from rollover"
                                        " thread or pathname too long");
                                goto process_rest;
                        }

                        (void) snprintf (abspath, PATH_MAX,
                                         "%s/%s", priv->changelog_dir, path);
                        if (changelog_notify_client (cn, abspath,
                                                     strlen (abspath) + 1))
                                gf_log (this->name, GF_LOG_ERROR,
                                        "could not notify some clients with new"
                                        " changelogs");
                }

        process_rest:
                for (i = 0; i < CHANGELOG_MAX_CLIENTS; i++) {
                        if ( (fd = cn->client_fd[i]) == -1 )
                                continue;

                        if (FD_ISSET (fd, &rset)) {
                                /**
                                 * the only data we accept from the client is a
                                 * disconnect. Anything else is treated as bogus
                                 * and is silently discarded (also warned!!!).
                                 */
                                if ( (readlen = read (fd, &buffer, 1)) <= 0 ) {
                                        close (fd);
                                        changelog_notify_clear_fd (cn, i);
                                } else {
                                        /* silently discard data and log */
                                        gf_log (this->name, GF_LOG_WARNING,
                                                "misbehaving changelog client");
                                }
                        }
                }

        }

 cleanup:;
        pthread_cleanup_pop (1);

 out:
        return NULL;
}