summaryrefslogtreecommitdiffstats
path: root/xlators/features/changelog/lib/src/gf-changelog.c
blob: 4e60bb276a79c68809e6795d02755680784d97f4 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
   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 <errno.h>
#include <dirent.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <string.h>

#include "globals.h"
#include "glusterfs.h"
#include "logging.h"

#include "gf-changelog-helpers.h"

/* from the changelog translator */
#include "changelog-misc.h"
#include "changelog-mem-types.h"

int byebye = 0;

static void
gf_changelog_cleanup (gf_changelog_t *gfc)
{
        /* socket */
        if (gfc->gfc_sockfd != -1)
                close (gfc->gfc_sockfd);
        /* tracker fd */
        if (gfc->gfc_fd != -1)
                close (gfc->gfc_fd);
        /* processing dir */
        if (gfc->gfc_dir)
                closedir (gfc->gfc_dir);

        if (gfc->gfc_working_dir)
                free (gfc->gfc_working_dir); /* allocated by realpath */
}

void
__attribute__ ((constructor)) gf_changelog_ctor (void)
{
        glusterfs_ctx_t *ctx = NULL;

        ctx = glusterfs_ctx_new ();
        if (!ctx)
                return;

        if (glusterfs_globals_init (ctx)) {
                free (ctx);
                ctx = NULL;
                return;
        }

        THIS->ctx = ctx;
}

void
__attribute__ ((destructor)) gf_changelog_dtor (void)
{
        xlator_t        *this = NULL;
        glusterfs_ctx_t *ctx  = NULL;
        gf_changelog_t  *gfc  = NULL;

        this = THIS;
        if (!this)
                return;

        ctx = this->ctx;
        gfc = this->private;

        if (gfc) {
                gf_changelog_cleanup (gfc);
                GF_FREE (gfc);
        }

        if (ctx) {
                pthread_mutex_destroy (&ctx->lock);
                free (ctx);
                ctx = NULL;
        }
}


static int
gf_changelog_open_dirs (gf_changelog_t *gfc)
{
        int  ret                    = -1;
        DIR *dir                    = NULL;
        int  tracker_fd             = 0;
        char tracker_path[PATH_MAX] = {0,};

        (void) snprintf (gfc->gfc_current_dir, PATH_MAX,
                         "%s/"GF_CHANGELOG_CURRENT_DIR"/",
                         gfc->gfc_working_dir);
        ret = mkdir_p (gfc->gfc_current_dir, 0600, _gf_false);
        if (ret)
                goto out;

        (void) snprintf (gfc->gfc_processed_dir, PATH_MAX,
                         "%s/"GF_CHANGELOG_PROCESSED_DIR"/",
                         gfc->gfc_working_dir);
        ret = mkdir_p (gfc->gfc_processed_dir, 0600, _gf_false);
        if (ret)
                goto out;

        (void) snprintf (gfc->gfc_processing_dir, PATH_MAX,
                         "%s/"GF_CHANGELOG_PROCESSING_DIR"/",
                         gfc->gfc_working_dir);
        ret = mkdir_p (gfc->gfc_processing_dir, 0600, _gf_false);
        if (ret)
                goto out;

        dir = opendir (gfc->gfc_processing_dir);
        if (!dir) {
                gf_log ("", GF_LOG_ERROR,
                        "opendir() error [reason: %s]", strerror (errno));
                goto out;
        }

        gfc->gfc_dir = dir;

        (void) snprintf (tracker_path, PATH_MAX,
                         "%s/"GF_CHANGELOG_TRACKER, gfc->gfc_working_dir);

        tracker_fd = open (tracker_path, O_CREAT | O_APPEND | O_RDWR,
                           S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
        if (tracker_fd < 0) {
                closedir (gfc->gfc_dir);
                ret = -1;
                goto out;
        }

        gfc->gfc_fd = tracker_fd;
        ret = 0;
 out:
        return ret;
}

int
gf_changelog_notification_init (xlator_t *this, gf_changelog_t *gfc)
{
        int                ret    = 0;
        int                len    = 0;
        int                tries  = 0;
        int                sockfd = 0;
        struct sockaddr_un remote;

        this = gfc->this;

        if (gfc->gfc_sockfd != -1) {
                gf_log (this->name, GF_LOG_INFO,
                        "Reconnecting...");
                close (gfc->gfc_sockfd);
        }

        sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
        if (sockfd < 0) {
                ret = -1;
                goto out;
        }

        CHANGELOG_MAKE_SOCKET_PATH (gfc->gfc_brickpath,
                                    gfc->gfc_sockpath, PATH_MAX);
        gf_log (this->name, GF_LOG_INFO,
                "connecting to changelog socket: %s (brick: %s)",
                gfc->gfc_sockpath, gfc->gfc_brickpath);

        remote.sun_family = AF_UNIX;
        strcpy (remote.sun_path, gfc->gfc_sockpath);

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

        while (tries < gfc->gfc_connretries) {
                gf_log (this->name, GF_LOG_WARNING,
                        "connection attempt %d/%d...",
                        tries + 1, gfc->gfc_connretries);

                /* initiate a connect */
                if (connect (sockfd, (struct sockaddr *) &remote, len) == 0) {
                        gfc->gfc_sockfd = sockfd;
                        break;
                }

                tries++;
                sleep (2);
        }

        if (tries == gfc->gfc_connretries) {
                gf_log (this->name, GF_LOG_ERROR,
                        "could not connect to changelog socket!"
                        " bailing out...");
                ret = -1;
        } else
                gf_log (this->name, GF_LOG_INFO,
                        "connection successful");

 out:
        return ret;
}

int
gf_changelog_done (char *file)
{
        int             ret    = -1;
        char           *buffer = NULL;
        xlator_t       *this   = NULL;
        gf_changelog_t *gfc    = NULL;
        char to_path[PATH_MAX] = {0,};

        errno = EINVAL;

        this = THIS;
        if (!this)
                goto out;

        gfc = (gf_changelog_t *) this->private;
        if (!gfc)
                goto out;

        if (!file || !strlen (file))
                goto out;

        /* make sure 'file' is inside ->gfc_working_dir */
        buffer = realpath (file, NULL);
        if (!buffer)
                goto out;

        if (strncmp (gfc->gfc_working_dir,
                     buffer, strlen (gfc->gfc_working_dir)))
                goto out;

        (void) snprintf (to_path, PATH_MAX, "%s%s",
                         gfc->gfc_processed_dir, basename (buffer));
        gf_log (this->name, GF_LOG_DEBUG,
                "moving %s to processed directory", file);
        ret = rename (buffer, to_path);
        if (ret) {
                gf_log (this->name, GF_LOG_ERROR,
                        "cannot move %s to %s (reason: %s)",
                        file, to_path, strerror (errno));
                goto out;
        }

        ret = 0;

 out:
        if (buffer)
                free (buffer); /* allocated by realpath() */
        return ret;
}

/**
 * @API
 *  for a set of changelogs, start from the begining
 */
int
gf_changelog_start_fresh ()
{
        xlator_t *this = NULL;
        gf_changelog_t *gfc = NULL;

        this = THIS;
        if (!this)
                goto out;

        errno = EINVAL;

        gfc = (gf_changelog_t *) this->private;
        if (!gfc)
                goto out;

        if (gf_ftruncate (gfc->gfc_fd, 0))
                goto out;

        return 0;

 out:
        return -1;
}

/**
 * @API
 * return the next changelog file entry. zero means all chanelogs
 * consumed.
 */
ssize_t
gf_changelog_next_change (char *bufptr, size_t maxlen)
{
        ssize_t         size       = 0;
        int             tracker_fd = 0;
        xlator_t       *this       = NULL;
        gf_changelog_t *gfc        = NULL;
        char buffer[PATH_MAX]      = {0,};

        errno = EINVAL;

        this = THIS;
        if (!this)
                goto out;

        gfc = (gf_changelog_t *) this->private;
        if (!gfc)
                goto out;

        tracker_fd = gfc->gfc_fd;

        size = gf_readline (tracker_fd, buffer, maxlen);
        if (size < 0)
                goto out;
        if (size == 0)
                return 0;

        memcpy (bufptr, buffer, size - 1);
        *(buffer + size) = '\0';

        return size;

 out:
        return -1;
}

/**
 * @API
 *  gf_changelog_scan() - scan and generate a list of change entries
 *
 * calling this api multiple times (without calling gf_changlog_done())
 * would result new changelogs(s) being refreshed in the tracker file.
 * This call also acts as a cancellation point for the consumer.
 */
ssize_t
gf_changelog_scan ()
{
        int             ret        = 0;
        int             tracker_fd = 0;
        size_t          len        = 0;
        size_t          off        = 0;
        xlator_t       *this       = NULL;
        size_t          nr_entries = 0;
        gf_changelog_t *gfc        = NULL;
        struct dirent  *entryp     = NULL;
        struct dirent  *result     = NULL;
        char buffer[PATH_MAX]      = {0,};

        this = THIS;
        if (!this)
                goto out;

        gfc = (gf_changelog_t *) this->private;
        if (!gfc)
                goto out;

        /**
         * do we need to protect 'byebye' with locks? worst, the
         * consumer would get notified during next scan().
         */
        if (byebye) {
                errno = ECONNREFUSED;
                goto out;
        }

        errno = EINVAL;

        tracker_fd = gfc->gfc_fd;

        if (gf_ftruncate (tracker_fd, 0))
                goto out;

        len = offsetof(struct dirent, d_name)
                + pathconf(gfc->gfc_processing_dir, _PC_NAME_MAX) + 1;
        entryp = GF_CALLOC (1, len,
                            gf_changelog_mt_libgfchangelog_dirent_t);
        if (!entryp)
                goto out;

        rewinddir (gfc->gfc_dir);
        while (1) {
                ret = readdir_r (gfc->gfc_dir, entryp, &result);
                if (ret || !result)
                        break;

                if ( !strcmp (basename (entryp->d_name), ".")
                     || !strcmp (basename (entryp->d_name), "..") )
                        continue;

                nr_entries++;

                GF_CHANGELOG_FILL_BUFFER (gfc->gfc_processing_dir,
                                          buffer, off,
                                          strlen (gfc->gfc_processing_dir));
                GF_CHANGELOG_FILL_BUFFER (entryp->d_name, buffer,
                                          off, strlen (entryp->d_name));
                GF_CHANGELOG_FILL_BUFFER ("\n", buffer, off, 1);

                if (gf_changelog_write (tracker_fd, buffer, off) != off) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "error writing changelog filename"
                                " to tracker file");
                        break;
                }
                off = 0;
        }

        GF_FREE (entryp);

        if (!result) {
                if (gf_lseek (tracker_fd, 0, SEEK_SET) != -1)
                        return nr_entries;
        }
 out:
        return -1;
}

/**
 * @API
 *  gf_changelog_register() - register a client for updates.
 */
int
gf_changelog_register (char *brick_path, char *scratch_dir,
                       char *log_file, int log_level, int max_reconnects)
{
        int             i    = 0;
        int             ret  = -1;
        int             errn = 0;
        xlator_t       *this = NULL;
        gf_changelog_t *gfc  = NULL;

        this = THIS;
        if (!this->ctx)
                goto out;

        errno = ENOMEM;

        gfc = GF_CALLOC (1, sizeof (*gfc),
                         gf_changelog_mt_libgfchangelog_t);
        if (!gfc)
                goto out;

        gfc->this = this;

        gfc->gfc_dir = NULL;
        gfc->gfc_fd = gfc->gfc_sockfd = -1;

        gfc->gfc_working_dir = realpath (scratch_dir, NULL);
        if (!gfc->gfc_working_dir) {
                errn = errno;
                goto cleanup;
        }

        ret = gf_changelog_open_dirs (gfc);
        if (ret) {
                errn = errno;
                gf_log (this->name, GF_LOG_ERROR,
                        "could not create entries in scratch dir");
                goto cleanup;
        }

        if (gf_log_init (this->ctx, log_file))
                goto cleanup;

        gf_log_set_loglevel ((log_level == -1) ? GF_LOG_INFO :
                             log_level);

        gfc->gfc_connretries = (max_reconnects <= 0) ? 1 : max_reconnects;
        (void) strncpy (gfc->gfc_brickpath, brick_path, PATH_MAX);

        ret = gf_changelog_notification_init (this, gfc);
        if (ret) {
                errn = errno;
                goto cleanup;
        }

        ret = pthread_create (&gfc->gfc_changelog_processor,
                              NULL, gf_changelog_process, gfc);
        if (ret) {
                errn = errno;
                gf_log (this->name, GF_LOG_ERROR,
                        "error creating changelog processor thread"
                        " new changes won't be recorded!!!");
                goto cleanup;
        }

        for (; i < 256; i++) {
                gfc->rfc3986[i] =
                        (isalnum(i) || i == '~' ||
                        i == '-' || i == '.' || i == '_') ? i : 0;
        }

        ret = 0;
        this->private = gfc;

        goto out;

 cleanup:
        gf_changelog_cleanup (gfc);
        GF_FREE (gfc);
        this->private = NULL;
        errno = errn;

 out:
        return ret;
}