summaryrefslogtreecommitdiff
path: root/src/lib/rib.c
blob: 0418252b6f2b45a5afd208006c04b841ba53ab2f (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
/*
 * Ouroboros - Copyright (C) 2016 - 2021
 *
 * RIB export using FUSE
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#define _POSIX_C_SOURCE 200112L

#include "config.h"

#if defined (__FreeBSD__)
#define __XSI_VISIBLE 500
#endif

#include <ouroboros/errno.h>
#include <ouroboros/list.h>
#include <ouroboros/rib.h>
#include <ouroboros/utils.h>

#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef HAVE_FUSE
#define _FILE_OFFSET_BITS 64
#define FUSE_USE_VERSION  26
#if defined (__linux__)
#define __USE_XOPEN
#endif /* __linux__ */
#include <fuse.h>

#ifndef CLOCK_REALTIME_COARSE
#define CLOCK_REALTIME_COARSE CLOCK_REALTIME
#endif

#define RT "/"

struct reg_comp {
        struct list_head next;

        char             path[RIB_PATH_LEN + 1];
        struct rib_ops * ops;
};

struct {
        struct list_head   reg_comps;

        char               mnt[RIB_PATH_LEN + 1];

        struct fuse *      fuse;
        struct fuse_chan * ch;

        pthread_rwlock_t   lock;

        pthread_t          fuse_thr;
} rib;

static int rib_open(const char *            path,
                    struct fuse_file_info * info)
{
        (void) path;

        info->nonseekable = 1;

        return 0;
}

static int rib_opendir(const char *         path,
                    struct fuse_file_info * info)
{
        (void) path;
        (void) info;

        return 0;
}

static int rib_read(const char *            path,
                    char *                  buf,
                    size_t                  size,
                    off_t                   offset,
                    struct fuse_file_info * info)
{
        struct list_head * p;
        char               comp[RIB_PATH_LEN + 1];
        char *             c;

        if (strlen(path) > RIB_PATH_LEN)
                return -1;

        strcpy(comp, path + 1);

        c = strstr(comp, "/");

        if (c != NULL)
                *c = '\0';

        (void) info;
        (void) offset;

        pthread_rwlock_wrlock(&rib.lock);

        list_for_each(p, &rib.reg_comps) {
                struct reg_comp * r = list_entry(p, struct reg_comp, next);
                if (strcmp(comp, r->path) == 0) {
                        int ret = r->ops->read(c + 1, buf, size);
                        pthread_rwlock_unlock(&rib.lock);
                        return ret;
                }
        }

        pthread_rwlock_unlock(&rib.lock);

        return -1;
}

static int rib_readdir(const char *            path,
                       void *                  buf,
                       fuse_fill_dir_t         filler,
                       off_t                   offset,
                       struct fuse_file_info * info)
{
        struct list_head * p;

        (void) offset;
        (void) info;

        filler(buf, ".", NULL, 0);
        filler(buf, "..", NULL, 0);

        pthread_rwlock_rdlock(&rib.lock);

        if (strcmp(path, RT) == 0) {
                list_for_each(p, &rib.reg_comps) {
                        struct reg_comp * c;
                        c = list_entry(p, struct reg_comp, next);
                        filler(buf, c->path, NULL, 0);
                }
        } else {
                list_for_each(p, &rib.reg_comps) {
                        char **           dir_entries;
                        ssize_t           len;
                        ssize_t           i;
                        struct reg_comp * c;
                        c = list_entry(p, struct reg_comp, next);

                        if (strcmp(path + 1, c->path) != 0)
                                continue;

                        assert(c->ops->readdir != NULL);

                        len = c->ops->readdir(&dir_entries);
                        if (len < 0)
                                break;
                        for (i = 0; i < len; ++i)
                                filler(buf, dir_entries[i], NULL, 0);
                        freepp(char, dir_entries, len);
                }
        }

        pthread_rwlock_unlock(&rib.lock);

        return 0;
}

static size_t __getattr(const char *  path,
                        struct stat * st)
{
        struct list_head * p;
        char               comp[RIB_PATH_LEN + 1];
        char *             c;

        if (strlen(path) > RIB_PATH_LEN)
                return -1;

        strcpy(comp, path + 1);

        c = strstr(comp, "/");

        if (c != NULL)
                *c = '\0';

        pthread_rwlock_rdlock(&rib.lock);

        list_for_each(p, &rib.reg_comps) {
                struct reg_comp * r = list_entry(p, struct reg_comp, next);
                if (strcmp(comp, r->path) == 0) {
                        size_t ret = r->ops->getattr(c + 1, st);
                        pthread_rwlock_unlock(&rib.lock);
                        return ret;
                }
        }

        pthread_rwlock_unlock(&rib.lock);

        return -1;
}

static int rib_getattr(const char *  path,
                       struct stat * st)
{
        struct list_head * p;
        struct timespec    now;

        memset(st, 0, sizeof(*st));

        if (strcmp(path, RT) == 0)
                goto finish_dir;

        pthread_rwlock_rdlock(&rib.lock);

        list_for_each(p, &rib.reg_comps) {
                struct reg_comp * rc = list_entry(p, struct reg_comp, next);
                if (strcmp(path + 1, rc->path) == 0) {
                        pthread_rwlock_unlock(&rib.lock);
                        goto finish_dir;
                }
        }

        pthread_rwlock_unlock(&rib.lock);

        assert(st->st_mode == 0);

        return __getattr(path, st);

 finish_dir:
        clock_gettime(CLOCK_REALTIME_COARSE, &now);
        st->st_mode  = S_IFDIR | 0755;
        st->st_nlink = 2;
        st->st_uid   = getuid();
        st->st_gid   = getgid();
        st->st_mtime = now.tv_sec;
        return 0;
}

static struct fuse_operations r_ops = {
        .getattr = rib_getattr,
        .open    = rib_open,
        .opendir = rib_opendir,
        .read    = rib_read,
        .readdir = rib_readdir
};

static void * fuse_thr(void * o)
{
        if (fuse_loop((struct fuse *) o) < 0)
                return (void *) -1;

        return (void *) 0;
}
#endif /* HAVE_FUSE */

int rib_init(const char * mountpt)
{
#ifdef HAVE_FUSE
        struct stat      st;
        char *           argv[] = {"-f",
                                   "-o",
                                   "ro,"
                                   "default_permissions,"
                                   "allow_other,"
                                   "fsname=rib",
                                   NULL};
        struct fuse_args args = FUSE_ARGS_INIT(3, argv);

        if (stat(FUSE_PREFIX, &st) == -1)
                return -1;

        sprintf(rib.mnt, FUSE_PREFIX "/%s", mountpt);

        if (stat(rib.mnt, &st) == -1)
                switch(errno) {
                case ENOENT:
                        if (mkdir(rib.mnt, 0777))
                                return -1;
                        break;
                case ENOTCONN:
                        fuse_unmount(rib.mnt, rib.ch);
                        break;
                default:
                        return -1;
                }

        fuse_opt_parse(&args, NULL, NULL, NULL);

        list_head_init(&rib.reg_comps);

        rib.ch = fuse_mount(rib.mnt,  &args);
        if (rib.ch == NULL)
                goto fail_mount;

        rib.fuse = fuse_new(rib.ch, &args, &r_ops, sizeof(r_ops), NULL);
        if (rib.fuse == NULL)
                goto fail_fuse;

        if (pthread_rwlock_init(&rib.lock, NULL))
                goto fail_rwlock_init;

        if (pthread_create(&rib.fuse_thr, NULL, fuse_thr, rib.fuse))
                goto fail_fuse_thr;

        fuse_opt_free_args(&args);

        return 0;

 fail_fuse_thr:
        pthread_rwlock_destroy(&rib.lock);
 fail_rwlock_init:
        fuse_destroy(rib.fuse);
 fail_fuse:
        fuse_unmount(rib.mnt, rib.ch);
 fail_mount:
        fuse_opt_free_args(&args);
        rmdir(rib.mnt);
        return -1;
#else
        (void) mountpt;
        return 0;
#endif
}

void rib_fini(void)
{
#ifdef HAVE_FUSE
        struct list_head * p;
        struct list_head * h;

        fuse_exit(rib.fuse);

        fuse_unmount(rib.mnt, rib.ch);

        pthread_join(rib.fuse_thr, NULL);

        fuse_destroy(rib.fuse);

        rmdir(rib.mnt);

        pthread_rwlock_wrlock(&rib.lock);

        list_for_each_safe(p, h, &rib.reg_comps) {
                struct reg_comp * c = list_entry(p, struct reg_comp, next);
                list_del(&c->next);
                free(c);
        }

        pthread_rwlock_unlock(&rib.lock);

        pthread_rwlock_destroy(&rib.lock);
#endif
}

int rib_reg(const char *     path,
            struct rib_ops * ops)
{
#ifdef HAVE_FUSE
        struct reg_comp *  rc;
        struct list_head * p;

        if (strlen(rib.mnt) == 0)
                return 0;

        pthread_rwlock_wrlock(&rib.lock);

        list_for_each(p, &rib.reg_comps) {
                struct reg_comp * r = list_entry(p, struct reg_comp, next);
                if (strcmp(r->path, path) == 0) {
                        pthread_rwlock_unlock(&rib.lock);
                        return -EPERM;
                }

                if (strcmp(r->path, path) > 0)
                        break;
        }

        rc = malloc(sizeof(*rc));
        if (rc == NULL) {
                pthread_rwlock_unlock(&rib.lock);
                return -ENOMEM;
        }

        if (strlen(path) > RIB_PATH_LEN) {
                pthread_rwlock_unlock(&rib.lock);
                free(rc);
                return -1;
        }

        strcpy(rc->path, path);
        rc->ops = ops;

        list_add_tail(&rc->next, p);

        pthread_rwlock_unlock(&rib.lock);
#else
        (void) path;
        (void) ops;
#endif
        return 0;
}

void rib_unreg(const char * path)
{
#ifdef HAVE_FUSE
        struct list_head * p;
        struct list_head * h;

        if (strlen(rib.mnt) == 0)
                return;

        pthread_rwlock_wrlock(&rib.lock);

        list_for_each_safe(p, h, &rib.reg_comps) {
                struct reg_comp * r = list_entry(p, struct reg_comp, next);
                if (strcmp(r->path, path) == 0) {
                        list_del(&r->next);
                        free(r);
                        break;
                }
        }

        pthread_rwlock_unlock(&rib.lock);
#else
        (void) path;
#endif
}