summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/syscall.c
blob: a7d4402808d7af154aa816a474657375edb1e54b (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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*
  Copyright (c) 2008-2012 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 "syscall.h"
#include "compat.h"
#include "mem-pool.h"

#include <sys/types.h>
#include <utime.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>

int
sys_lstat (const char *path, struct stat *buf)
{
        return lstat (path, buf);
}


int
sys_stat (const char *path, struct stat *buf)
{
        return stat (path, buf);
}


int
sys_fstat (int fd, struct stat *buf)
{
        return fstat (fd, buf);
}


int
sys_fstatat(int dirfd, const char *pathname, struct stat *buf, int flags)
{
#ifdef GF_DARWIN_HOST_OS
        if (fchdir(dirfd) < 0)
                return -1;
        if(flags & AT_SYMLINK_NOFOLLOW)
                return lstat(pathname, buf);
        else
                return stat(pathname, buf);
#else
        return fstatat (dirfd, pathname, buf, flags);
#endif
}


int
sys_openat(int dirfd, const char *pathname, int flags, int mode)
{
        int fd;

#ifdef GF_DARWIN_HOST_OS
        if (fchdir(dirfd) < 0)
                return -1;
        fd = open (pathname, flags, mode);
        /* TODO: Shouldn't we restore the old current directory */
#else /* GF_DARWIN_HOST_OS */
        fd = openat (dirfd, pathname, flags, mode);
#ifdef __FreeBSD__
        /* On FreeBSD S_ISVTX flag is ignored for an open() with O_CREAT set.
         * We need to force the flag using fchmod(). */
        if ((fd >= 0) &&
            ((flags & O_CREAT) != 0) && ((mode & S_ISVTX) != 0)) {
                sys_fchmod(fd, mode);
                /* TODO: It's unlikely that fchmod could fail here. However,
                         if it fails we cannot always restore the old state
                         (if the file existed, we cannot recover it). We would
                         need many more system calls to correctly handle all
                         possible cases and it doesn't worth it. For now we
                         simply ignore the error. */
        }
#endif /* __FreeBSD__ */
#endif /* !GF_DARWIN_HOST_OS */

        return fd;
}


int
sys_open(const char *pathname, int flags, int mode)
{
        return sys_openat(AT_FDCWD, pathname, flags, mode);
}


DIR *
sys_opendir (const char *name)
{
        return opendir (name);
}

int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
{
#ifdef GF_DARWIN_HOST_OS
        if(fchdir(dirfd) < 0)
                return -1;
        return mkdir(pathname, mode);
#else
        return mkdirat (dirfd, pathname, mode);
#endif
}

struct dirent *
sys_readdir (DIR *dir, struct dirent *de)
{
#if !defined(__GLIBC__)
        /*
         * World+Dog says glibc's readdir(3) is MT-SAFE as long as
         * two threads are not accessing the same DIR; there's a
         * potential buffer overflow in glibc's readdir_r(3); and
         * glibc's readdir_r(3) is deprecated after version 2.22
         * with presumed eventual removal.
         * Given all that, World+Dog says everyone should just use
         * readdir(3). But it's unknown, unclear whether the same
         * is also true for *BSD, MacOS, and, etc.
        */
        struct dirent *entry = NULL;

        (void) readdir_r (dir, de, &entry);
        return entry;
#else
        return readdir (dir);
#endif
}


ssize_t
sys_readlink (const char *path, char *buf, size_t bufsiz)
{
        return readlink (path, buf, bufsiz);
}


int
sys_closedir (DIR *dir)
{
        return closedir (dir);
}


int
sys_mknod (const char *pathname, mode_t mode, dev_t dev)
{
        return mknod (pathname, mode, dev);
}


int
sys_mkdir (const char *pathname, mode_t mode)
{
        return mkdir (pathname, mode);
}


int
sys_unlink (const char *pathname)
{
#ifdef GF_SOLARIS_HOST_OS
        return solaris_unlink (pathname);
#endif
        return unlink (pathname);
}


int
sys_rmdir (const char *pathname)
{
        return rmdir (pathname);
}


int
sys_symlink (const char *oldpath, const char *newpath)
{
        return symlink (oldpath, newpath);
}


int
sys_rename (const char *oldpath, const char *newpath)
{
#ifdef GF_SOLARIS_HOST_OS
        return solaris_rename (oldpath, newpath);
#endif
        return rename (oldpath, newpath);
}


int
sys_link (const char *oldpath, const char *newpath)
{
#ifdef HAVE_LINKAT
	/*
	 * On most systems (Linux being the notable exception), link(2)
	 * first resolves symlinks. If the target is a directory or
	 * is nonexistent, it will fail. linkat(2) operates on the
	 * symlink instead of its target when the AT_SYMLINK_FOLLOW
	 * flag is not supplied.
	 */
        return linkat (AT_FDCWD, oldpath, AT_FDCWD, newpath, 0);
#else
        return link (oldpath, newpath);
#endif
}


int
sys_chmod (const char *path, mode_t mode)
{
        return chmod (path, mode);
}


int
sys_fchmod (int fd, mode_t mode)
{
        return fchmod (fd, mode);
}


int
sys_chown (const char *path, uid_t owner, gid_t group)
{
        return chown (path, owner, group);
}


int
sys_fchown (int fd, uid_t owner, gid_t group)
{
        return fchown (fd, owner, group);
}


int
sys_lchown (const char *path, uid_t owner, gid_t group)
{
        return lchown (path, owner, group);
}


int
sys_truncate (const char *path, off_t length)
{
        return truncate (path, length);
}


int
sys_ftruncate (int fd, off_t length)
{
        return ftruncate (fd, length);
}


int
sys_utimes (const char *filename, const struct timeval times[2])
{
        return utimes (filename, times);
}


#if defined(HAVE_UTIMENSAT)
int
sys_utimensat (int dirfd, const char *filename, const struct timespec times[2],
               int flags)
{
        return utimensat (dirfd, filename, times, flags);
}
#endif


int
sys_creat (const char *pathname, mode_t mode)
{
        return sys_open(pathname, O_CREAT | O_TRUNC | O_WRONLY, mode);
}


ssize_t
sys_readv (int fd, const struct iovec *iov, int iovcnt)
{
        return readv (fd, iov, iovcnt);
}


ssize_t
sys_writev (int fd, const struct iovec *iov, int iovcnt)
{
        return writev (fd, iov, iovcnt);
}


ssize_t
sys_read (int fd, void *buf, size_t count)
{
        return read (fd, buf, count);
}


ssize_t
sys_write (int fd, const void *buf, size_t count)
{
        return write (fd, buf, count);
}


ssize_t
sys_preadv (int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
        return preadv (fd, iov, iovcnt, offset);
}


ssize_t
sys_pwritev (int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
        return pwritev (fd, iov, iovcnt, offset);
}


ssize_t
sys_pread (int fd, void *buf, size_t count, off_t offset)
{
        return pread (fd, buf, count, offset);
}


ssize_t
sys_pwrite (int fd, const void *buf, size_t count, off_t offset)
{
        return pwrite (fd, buf, count, offset);
}


off_t
sys_lseek (int fd, off_t offset, int whence)
{
        return lseek (fd, offset, whence);
}


int
sys_statvfs (const char *path, struct statvfs *buf)
{
        int ret;

        ret = statvfs (path, buf);
#ifdef __FreeBSD__
        /* FreeBSD doesn't return the expected vaule in buf->f_bsize. It
         * contains the optimal I/O size instead of the file system block
         * size. Gluster expects that this field contains the block size.
         */
        if (ret == 0) {
                buf->f_bsize = buf->f_frsize;
        }
#endif /* __FreeBSD__ */

        return ret;
}


int
sys_fstatvfs (int fd, struct statvfs *buf)
{
        int ret;

        ret = fstatvfs (fd, buf);
#ifdef __FreeBSD__
        /* FreeBSD doesn't return the expected vaule in buf->f_bsize. It
         * contains the optimal I/O size instead of the file system block
         * size. Gluster expects this field to contain the block size.
         */
        if (ret == 0) {
                buf->f_bsize = buf->f_frsize;
        }
#endif /* __FreeBSD__ */

        return ret;
}


int
sys_close (int fd)
{
        int ret = -1;

        if (fd >= 0)
                ret = close (fd);

        return ret;
}


int
sys_fsync (int fd)
{
        return fsync (fd);
}


int
sys_fdatasync (int fd)
{
#ifdef GF_DARWIN_HOST_OS
        return fcntl (fd, F_FULLFSYNC);
#elif __FreeBSD__
	return fsync (fd);
#else
        return fdatasync (fd);
#endif
}

void
gf_add_prefix(const char *ns, const char *key, char **newkey)
{
        /* if we dont have any namespace, append USER NS */
        if (strncmp(key, XATTR_USER_PREFIX,     XATTR_USER_PREFIX_LEN) &&
            strncmp(key, XATTR_TRUSTED_PREFIX,  XATTR_TRUSTED_PREFIX_LEN) &&
            strncmp(key, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
            strncmp(key, XATTR_SYSTEM_PREFIX,   XATTR_SYSTEM_PREFIX_LEN)) {
                int ns_length =  strlen(ns);
                *newkey = GF_MALLOC(ns_length + strlen(key) + 10,
                                    gf_common_mt_char);
                strcpy(*newkey, ns);
                strcat(*newkey, key);
        } else {
                *newkey = gf_strdup(key);
        }
}

void
gf_remove_prefix(const char *ns, const char *key, char **newkey)
{
        int ns_length =  strlen(ns);
        if (strncmp(key, ns, ns_length) == 0) {
                *newkey = GF_MALLOC(-ns_length + strlen(key) + 10,
                                    gf_common_mt_char);
                strcpy(*newkey, key + ns_length);
        } else {
                *newkey = gf_strdup(key);
        }
}

int
sys_lsetxattr (const char *path, const char *name, const void *value,
               size_t size, int flags)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return lsetxattr (path, name, value, size, flags);
#endif

#ifdef GF_BSD_HOST_OS
        return extattr_set_link (path, EXTATTR_NAMESPACE_USER,
                                 name, value, size);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return solaris_setxattr (path, name, value, size, flags);
#endif

#ifdef GF_DARWIN_HOST_OS
        /* OS X clients will carry other flags, which will be used on a
           OS X host, but masked out on others. GF assume NOFOLLOW on Linux,
           enforcing  */
        return setxattr (path, name, value, size, 0,
                         (flags & ~XATTR_NOSECURITY) | XATTR_NOFOLLOW);
#endif

}


ssize_t
sys_llistxattr (const char *path, char *list, size_t size)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return llistxattr (path, list, size);
#endif

#ifdef GF_BSD_HOST_OS
        return extattr_list_link (path, EXTATTR_NAMESPACE_USER, list, size);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return solaris_listxattr (path, list, size);
#endif

#ifdef GF_DARWIN_HOST_OS
        return listxattr (path, list, size, XATTR_NOFOLLOW);
#endif
}

ssize_t
sys_lgetxattr (const char *path, const char *name, void *value, size_t size)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return lgetxattr (path, name, value, size);
#endif

#ifdef GF_BSD_HOST_OS
        return extattr_get_link (path, EXTATTR_NAMESPACE_USER, name, value,
                                 size);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return solaris_getxattr (path, name, value, size);
#endif

#ifdef GF_DARWIN_HOST_OS
         return getxattr (path, name, value, size, 0, XATTR_NOFOLLOW);
#endif

}


ssize_t
sys_fgetxattr (int filedes, const char *name, void *value, size_t size)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return fgetxattr (filedes, name, value, size);
#endif

#ifdef GF_BSD_HOST_OS
        return extattr_get_fd (filedes, EXTATTR_NAMESPACE_USER, name,
                               value, size);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return solaris_fgetxattr (filedes, name, value, size);
#endif

#ifdef GF_DARWIN_HOST_OS
        return fgetxattr (filedes, name, value, size, 0, 0);
#endif

}

int
sys_fremovexattr (int filedes, const char *name)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return fremovexattr (filedes, name);
#endif

#ifdef GF_BSD_HOST_OS
        return extattr_delete_fd (filedes, EXTATTR_NAMESPACE_USER, name);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return solaris_fremovexattr (filedes, name);
#endif

#ifdef GF_DARWIN_HOST_OS
        return fremovexattr (filedes, name, 0);
#endif
}


int
sys_fsetxattr (int filedes, const char *name, const void *value,
               size_t size, int flags)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return fsetxattr (filedes, name, value, size, flags);
#endif

#ifdef GF_BSD_HOST_OS
        return extattr_set_fd (filedes, EXTATTR_NAMESPACE_USER, name,
                               value, size);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return solaris_fsetxattr (filedes, name, value, size, flags);
#endif

#ifdef GF_DARWIN_HOST_OS
        return fsetxattr (filedes, name, value, size, 0,
                          flags & ~XATTR_NOSECURITY);
#endif

}


ssize_t
sys_flistxattr (int filedes, char *list, size_t size)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return flistxattr (filedes, list, size);
#endif

#ifdef GF_BSD_HOST_OS
        return extattr_list_fd (filedes, EXTATTR_NAMESPACE_USER, list, size);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return solaris_flistxattr (filedes, list, size);
#endif

#ifdef GF_DARWIN_HOST_OS
        return flistxattr (filedes, list, size, XATTR_NOFOLLOW);
#endif

}


int
sys_lremovexattr (const char *path, const char *name)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return lremovexattr (path, name);
#endif

#ifdef GF_BSD_HOST_OS
        return extattr_delete_link (path, EXTATTR_NAMESPACE_USER, name);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return solaris_removexattr (path, name);
#endif

#ifdef GF_DARWIN_HOST_OS
        return removexattr (path, name, XATTR_NOFOLLOW);
#endif

}


int
sys_access (const char *pathname, int mode)
{
        return access (pathname, mode);
}


int
sys_fallocate(int fd, int mode, off_t offset, off_t len)
{
#ifdef HAVE_FALLOCATE
        return fallocate(fd, mode, offset, len);
#endif

#ifdef HAVE_POSIX_FALLOCATE
        if (mode) {
                /* keep size not supported */
                errno = EOPNOTSUPP;
                return -1;
        }

        return posix_fallocate(fd, offset, len);
#endif

#if defined(F_ALLOCATECONTIG) && defined(GF_DARWIN_HOST_OS)
        /* C conversion from C++ implementation for OSX by Mozilla Foundation */
        if (mode) {
                /* keep size not supported */
                errno = EOPNOTSUPP;
                return -1;
        }
        /*
         *   The F_PREALLOCATE command operates on the following structure:
         *
         *    typedef struct fstore {
         *    u_int32_t fst_flags;      // IN: flags word
         *    int       fst_posmode;    // IN: indicates offset field
         *    off_t     fst_offset;     // IN: start of the region
         *    off_t     fst_length;     // IN: size of the region
         *    off_t     fst_bytesalloc; // OUT: number of bytes allocated
         *    } fstore_t;
         *
         * The flags (fst_flags) for the F_PREALLOCATE command are as follows:
         *    F_ALLOCATECONTIG   Allocate contiguous space.
         *    F_ALLOCATEALL      Allocate all requested space or no space at all.
         *
         * The position modes (fst_posmode) for the F_PREALLOCATE command
         * indicate how to use the offset field.  The modes are as follows:
         *    F_PEOFPOSMODE   Allocate from the physical end of file.
         *    F_VOLPOSMODE    Allocate from the volume offset.
         *
         */

        int ret;
        fstore_t store = {F_ALLOCATECONTIG, F_PEOFPOSMODE, offset, len, 0};
        ret = fcntl (fd, F_PREALLOCATE, &store);
        if (ret == -1) {
                store.fst_flags = F_ALLOCATEALL;
                ret = fcntl (fd, F_PREALLOCATE, &store);
        }
        if (ret == -1)
                return ret;
        return ftruncate (fd, offset + len);
#endif
        errno = ENOSYS;
        return -1;
}