XRootD
Loading...
Searching...
No Matches
XrdPosixPreload32.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d P o s i x P r e l o a d 3 2 . c c */
4/* */
5/* (c) 2005 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* All Rights Reserved */
7/* Produced by Andrew Hanushevsky for Stanford University under contract */
8/* DE-AC02-76-SFO0515 with the Department of Energy */
9/* */
10/* This file is part of the XRootD software suite. */
11/* */
12/* XRootD is free software: you can redistribute it and/or modify it under */
13/* the terms of the GNU Lesser General Public License as published by the */
14/* Free Software Foundation, either version 3 of the License, or (at your */
15/* option) any later version. */
16/* */
17/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20/* License for more details. */
21/* */
22/* You should have received a copy of the GNU Lesser General Public License */
23/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25/* */
26/* The copyright holder's institutional names and contributor's names may not */
27/* be used to endorse or promote products derived from this software without */
28/* specific prior written permission of the institution or contributor. */
29/******************************************************************************/
30
31#if defined(__clang__) && defined(_FORTIFY_SOURCE)
32#undef _FORTIFY_SOURCE
33#endif
34
35#if defined(__LP64__) || defined(_LP64)
36
37#if !defined(MUSL)
38#ifdef _LARGEFILE_SOURCE
39#undef _LARGEFILE_SOURCE
40#endif
41
42#ifdef _LARGEFILE64_SOURCE
43#undef _LARGEFILE64_SOURCE
44#endif
45
46#ifdef _FILE_OFFSET_BITS
47#undef _FILE_OFFSET_BITS
48#endif
49
50#ifdef _TIME_BITS
51#undef _TIME_BITS
52#endif
53#endif
54
55#define XRDPOSIXPRELOAD32
56
57#include <cerrno>
58#include <dirent.h>
59#include <cstdio>
60#include <cstdarg>
61#include <sys/stat.h>
62#include <sys/types.h>
63#include <unistd.h>
64#include <cstdlib>
65
66#if defined(__APPLE__) || defined(__FreeBSD__)
67#include <sys/param.h>
68#include <sys/mount.h>
69#else
70#include <sys/statfs.h>
71#endif
72
79
80/******************************************************************************/
81/* G l o b a l D e c l a r a t i o n s */
82/******************************************************************************/
83
85
86namespace {bool isLite = (getenv("XRD_POSIX_PRELOAD_LITE") != 0);}
87
88/******************************************************************************/
89/* 6 4 - t o 3 2 B i t C o n v e r s i o n s */
90/******************************************************************************/
91/******************************************************************************/
92/* X r d P o s i x _ C o p y D i r e n t */
93/******************************************************************************/
94
95// Macos is a curious beast. It is not an LP64 platform but offsets are
96// defined as 64 bits anyway. So, the dirent structure is 64-bit conformable
97// making CopyDirent() superfluous. In Solaris x86 there are no 32 bit interfaces.
98//
99#if !defined(__LP64__) && !defined(_LP64)
100#if !defined(__APPLE__) && !defined(SUNX86) && !defined(__FreeBSD__) && !(defined(__FreeBSD_kernel__) && defined(__GLIBC__))
101int XrdPosix_CopyDirent(struct dirent *dent, struct dirent64 *dent64)
102{
103 const unsigned long long LLMask = 0xffffffff00000000LL;
104 int isdiff = (dent->d_name-(char *)dent) != (dent64->d_name-(char *)dent64);
105
106#if defined(__GNU__)
107 if (isdiff && (dent64->d_ino & LLMask))
108#else
109 if (isdiff && ((dent64->d_ino & LLMask) || (dent64->d_off & LLMask)))
110#endif
111 {errno = EOVERFLOW; return EOVERFLOW;}
112
113 if (isdiff || (void *)dent != (void *)dent64)
114 {dent->d_ino = dent64->d_ino;
115#if !defined(__GNU__)
116 dent->d_off = dent64->d_off;
117#endif
118 dent->d_reclen = dent64->d_reclen;
119 dent->d_type = dent64->d_type;
120#if defined(__GNU__)
121 dent->d_namlen = dent64->d_namlen;
122#endif
123 strcpy(dent->d_name, dent64->d_name);
124 }
125 return 0;
126}
127#endif
128#endif
129
130/******************************************************************************/
131/* X r d P o s i x _ C o p y S t a t */
132/******************************************************************************/
133
134// Macos is a curious beast. It is not an LP64 platform but stat sizes are
135// defined as 64 bits anyway. So, the stat structure is 64-bit conformable
136// making CopyStat() seemingly superfluous. However, starting in Darwin 10.5
137// stat and stat64 are defined separately making it necessary to use CopyStat().
138// In Solaris x86 there are no 32 bit interfaces.
139//
140#if !defined(__LP64__) && !defined(_LP64)
141#if !defined(SUNX86) && !defined(__FreeBSD__)
142int XrdPosix_CopyStat(struct stat *buf, struct stat64 &buf64)
143{
144 const unsigned long long LLMask = 0xffffffff00000000LL;
145 const int INTMax = 0x7fffffff;
146
147 if (buf64.st_size & LLMask)
148 if (buf64.st_mode & S_IFREG || buf64.st_mode & S_IFDIR)
149 {errno = EOVERFLOW; return -1;}
150 else buf->st_size = INTMax;
151 else buf->st_size = buf64.st_size; /* 64: File size in bytes */
152
153 buf->st_ino = buf64.st_ino & LLMask ? INTMax : buf64.st_ino;
154 buf->st_blocks= buf64.st_blocks & LLMask ? INTMax : buf64.st_blocks;
155 buf->st_mode = buf64.st_mode; /* File mode (see mknod(2)) */
156 buf->st_dev = buf64.st_dev;
157 buf->st_rdev = buf64.st_rdev; /* ID of device */
158 buf->st_nlink = buf64.st_nlink; /* Number of links */
159 buf->st_uid = buf64.st_uid; /* User ID of the file's owner */
160 buf->st_gid = buf64.st_gid; /* Group ID of the file's group */
161 buf->st_atime = buf64.st_atime; /* Time of last access */
162 buf->st_mtime = buf64.st_mtime; /* Time of last data modification */
163 buf->st_ctime = buf64.st_ctime; /* Time of last file status change */
164 buf->st_blksize=buf64.st_blksize; /* Preferred I/O block size */
165 return 0;
166}
167#endif
168#endif
169
170/******************************************************************************/
171/* c r e a t */
172/******************************************************************************/
173
174#if !defined(SUNX86) && !defined(__FreeBSD__)
175extern "C"
176{
177int creat(const char *path, mode_t mode)
178{
179 static int Init = Xunix.Init(&Init);
180
181 return XrdPosix_Open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
182}
183}
184#endif
185
186/******************************************************************************/
187/* f c n t l */
188/******************************************************************************/
189
190extern "C"
191{
192int fcntl(int fd, int cmd, ...)
193{
194 static int Init = Xunix.Init(&Init);
195 va_list ap;
196 void *theArg;
197
198 if (XrdPosixXrootd::myFD(fd)) return 0;
199 va_start(ap, cmd);
200 theArg = va_arg(ap, void *);
201 va_end(ap);
202 return Xunix.Fcntl(fd, cmd, theArg);
203}
204}
205
206/******************************************************************************/
207/* f o p e n */
208/******************************************************************************/
209/*
210extern "C"
211{
212FILE *fopen(const char *path, const char *mode)
213{
214 static int Init = Xunix.Init(&Init);
215
216 return XrdPosix_Fopen(path, mode);
217}
218}
219*/
220
221
222/******************************************************************************/
223/* f s e e k o */
224/******************************************************************************/
225
226#ifndef SUNX86
227extern "C"
228{
229int fseeko(FILE *stream, off_t offset, int whence)
230{
231 static int Init = Xunix.Init(&Init);
232
233 return XrdPosix_Fseeko(stream, offset, whence);
234}
235}
236#endif
237
238/******************************************************************************/
239/* f s t a t */
240/******************************************************************************/
241
242extern "C"
243{
244#if defined(__linux__) && defined(_STAT_VER) && __GNUC__ && __GNUC__ >= 2
245 int __fxstat(int ver, int fildes, struct stat *buf)
246#elif defined(__solaris__) && defined(__i386)
247 int _fxstat(int ver, int fildes, struct stat *buf)
248#else
249 int fstat( int fildes, struct stat *buf)
250#endif
251{
252 static int Init = Xunix.Init(&Init);
253
254#if defined(__linux__) && defined(_STAT_VER) && __GNUC__ && __GNUC__ >= 2
255 if (!XrdPosixXrootd::myFD(fildes)) return Xunix.Fstat(ver, fildes, buf);
256#endif
257#ifdef __APPLE__
258 if (!XrdPosixXrootd::myFD(fildes)) return Xunix.Fstat( fildes, buf);
259#endif
260
261#if defined(__LP64__) || defined(_LP64)
262 return XrdPosix_Fstat(fildes, buf );
263#else
264 int rc;
265 struct stat64 buf64;
266 if ((rc = XrdPosix_Fstat(fildes, (struct stat *)&buf64))) return rc;
267 return XrdPosix_CopyStat(buf, buf64);
268#endif
269}
270}
271
272/******************************************************************************/
273/* f s t a t a t */
274/******************************************************************************/
275
276extern "C"
277{
278 int fstatat(int dirfd, const char* path, struct stat *buf, int flags){
279 static int Init = Xunix.Init(&Init);
280#if defined(__LP64__) || defined(_LP64)
281 return XrdPosix_Fstatat (dirfd, path, buf, flags);
282#else
283 int rc;
284 struct stat64 buf64;
285 if ((rc = XrdPosix_Fstatat(dirfd, path, (struct stat *)&buf64, flags))) return rc;
286 return XrdPosix_CopyStat(buf, buf64);
287#endif
288
289}
290}
291
292/******************************************************************************/
293/* f t e l l o */
294/******************************************************************************/
295
296#ifndef SUNX86
297extern "C"
298{
299off_t ftello(FILE *stream)
300{
301 static int Init = Xunix.Init(&Init);
302
303 return static_cast<off_t>(XrdPosix_Ftello(stream));
304}
305}
306#endif
307
308/******************************************************************************/
309/* f t r u n c a t e */
310/******************************************************************************/
311
312#if !defined(SUNX86) && !defined(__FreeBSD__)
313extern "C"
314{
315int ftruncate(int fildes, off_t offset)
316{
317 static int Init = Xunix.Init(&Init);
318
319 return XrdPosix_Ftruncate(fildes, offset);
320}
321}
322#endif
323
324/******************************************************************************/
325/* l s e e k */
326/******************************************************************************/
327
328#if !defined(SUNX86) && !defined(__FreeBSD__)
329extern "C"
330{
331off_t lseek(int fildes, off_t offset, int whence)
332{
333 static int Init = Xunix.Init(&Init);
334
335 return XrdPosix_Lseek(fildes, offset, whence);
336}
337}
338#endif
339
340/******************************************************************************/
341/* l s t a t */
342/******************************************************************************/
343
344#if !defined(SUNX86) && !defined(__FreeBSD__)
345extern "C"
346{
347#if defined __GNUC__ && defined(_STAT_VER) && __GNUC__ >= 2 && defined(__linux__)
348int __lxstat(int ver, const char *path, struct stat *buf)
349#elif defined(__solaris__) && defined(__i386)
350int _lxstat(int ver, const char *path, struct stat *buf)
351#else
352int lstat( const char *path, struct stat *buf)
353#endif
354{
355 static int Init = Xunix.Init(&Init);
356
357#if defined(__linux__) and defined(_STAT_VER)
358 if (!XrdPosix_isMyPath(path))
359 return Xunix.Lstat(ver, path, buf);
360#endif
361#ifdef __APPLE__
362 if (!XrdPosix_isMyPath(path))
363 return Xunix.Lstat( path, buf);
364#endif
365
366#if defined(__LP64__) || defined(_LP64)
367 return XrdPosix_Lstat(path, buf );
368#else
369 struct stat64 buf64;
370 int rc;
371
372 if ((rc = XrdPosix_Lstat(path, (struct stat *)&buf64))) return rc;
373 return XrdPosix_CopyStat(buf, buf64);
374#endif
375}
376}
377#endif
378
379/******************************************************************************/
380/* o p e n */
381/******************************************************************************/
382
383#if !defined(SUNX86) && !defined(__FreeBSD__)
384extern "C"
385{
386int open(const char *path, int oflag, ...)
387{
388 static int Init = Xunix.Init(&Init);
389 va_list ap;
390 int mode;
391
392 va_start(ap, oflag);
393 mode = va_arg(ap, int);
394 va_end(ap);
395 return XrdPosix_Open(path, oflag, mode);
396}
397}
398#endif
399
400/******************************************************************************/
401/* p r e a d */
402/******************************************************************************/
403
404#if !defined(SUNX86) && !defined(__FreeBSD__)
405extern "C"
406{
407ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
408{
409 static int Init = Xunix.Init(&Init);
410
411 return XrdPosix_Pread(fildes, buf, nbyte, offset);
412}
413}
414#endif
415
416/******************************************************************************/
417/* r e a d d i r */
418/******************************************************************************/
419
420#if !defined(SUNX86) && !defined(__FreeBSD__)
421extern "C"
422{
423struct dirent* readdir(DIR *dirp)
424{
425 static int Init = Xunix.Init(&Init);
426 struct dirent64 *dp64;
427
428 if ( isLite )
429 {
430 if (!(dp64 = Xunix.Readdir64(dirp))) return 0;
431 }
432 else
433 if (!(dp64 = XrdPosix_Readdir64(dirp))) return 0;
434
435#if !defined(__APPLE__) && !defined(_LP64) && !defined(__LP64__) && !(defined(__FreeBSD_kernel__) && defined(__GLIBC__))
436 if (XrdPosix_CopyDirent((struct dirent *)dp64, dp64)) return 0;
437#endif
438
439 return (struct dirent *)dp64;
440}
441}
442#endif
443
444/******************************************************************************/
445/* r e a d d i r _ r */
446/******************************************************************************/
447
448#if !defined(SUNX86) && !defined(__FreeBSD__)
449extern "C"
450{
451int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
452{
453 static int Init = Xunix.Init(&Init);
454#if defined(__APPLE__) || defined(__LP64__) || defined(_LP64) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
455 return XrdPosix_Readdir_r(dirp, entry, result);
456#else
457 char buff[sizeof(struct dirent64) + 2048];
458 struct dirent64 *dp64 = (struct dirent64 *)buff;
459 struct dirent64 *mydirent;
460 int rc;
461
462 if ( isLite )
463 {
464 if ((rc = Xunix.Readdir64_r(dirp, dp64, &mydirent))) return rc;
465 }
466 else
467 if ((rc = XrdPosix_Readdir64_r(dirp, dp64, &mydirent))) return rc;
468
469 if (!mydirent) {*result = 0; return 0;}
470
471 if ((rc = XrdPosix_CopyDirent(entry, dp64))) return rc;
472
473 *result = entry;
474 return 0;
475#endif
476}
477}
478#endif
479
480/******************************************************************************/
481/* p w r i t e */
482/******************************************************************************/
483
484#if !defined(SUNX86) && !defined(__FreeBSD__)
485extern "C"
486{
487ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset)
488{
489 static int Init = Xunix.Init(&Init);
490
491 return XrdPosix_Pwrite(fildes, buf, nbyte, offset);
492}
493}
494#endif
495
496/******************************************************************************/
497/* s t a t */
498/******************************************************************************/
499
500#if !defined(SUNX86) && !defined(__FreeBSD__)
501extern "C"
502{
503#if defined __linux__ && defined(_STAT_VER) && defined __GNUC__ && __GNUC__ >= 2
504int __xstat(int ver, const char *path, struct stat *buf)
505#elif defined(__solaris__) && defined(__i386)
506int _xstat(int ver, const char *path, struct stat *buf)
507#else
508int stat( const char *path, struct stat *buf)
509#endif
510{
511 static int Init = Xunix.Init(&Init);
512#if defined( __linux__) && defined(_STAT_VER)
513 if (!XrdPosix_isMyPath(path))
514 return Xunix.Stat(ver, path, buf);
515#endif
516#ifdef __APPLE__
517 if (!XrdPosix_isMyPath(path))
518 return Xunix.Stat( path, buf);
519#endif
520
521#if defined(__LP64__) || defined(_LP64)
522 return XrdPosix_Stat(path, buf);
523#else
524 struct stat64 buf64;
525 int rc;
526 if ((rc = XrdPosix_Stat(path, (struct stat *)&buf64))) return rc;
527 return XrdPosix_CopyStat(buf, buf64);
528#endif
529}
530}
531#endif
532
533/******************************************************************************/
534/* s t a t f s */
535/******************************************************************************/
536
537#if !defined(__solaris__) && !defined(__APPLE__) && !defined(__FreeBSD__)
538extern "C"
539{
540int statfs( const char *path, struct statfs *buf)
541{
542 static int Init = Xunix.Init(&Init);
543 struct statfs64 buf64;
544 int rc;
545
546 if ((rc = XrdPosix_Statfs(path, (struct statfs *)&buf64))) return rc;
547 memset(buf, 0, sizeof(struct statfs));
548 buf->f_type = buf64.f_type;
549 buf->f_bsize = buf64.f_bsize;
550 buf->f_blocks = buf64.f_blocks;
551 buf->f_bfree = buf64.f_bfree;
552 buf->f_files = buf64.f_files;
553 buf->f_ffree = buf64.f_ffree;
554 buf->f_fsid = buf64.f_fsid;
555#if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
556 buf->f_namemax = buf64.f_namemax;
557#else
558 buf->f_namelen = buf64.f_namelen;
559#endif
560 return 0;
561}
562}
563#endif
564
565/******************************************************************************/
566/* s t a t v f s */
567/******************************************************************************/
568
569#if !defined(__APPLE__) && !defined(SUNX86) && !defined(__FreeBSD__)
570extern "C"
571{
572int statvfs( const char *path, struct statvfs *buf)
573{
574 static int Init = Xunix.Init(&Init);
575 struct statvfs64 buf64;
576 int rc;
577 if ((rc = XrdPosix_Statvfs(path, (struct statvfs *)&buf64))) return rc;
578 memset(buf, 0, sizeof(struct statvfs));
579 buf->f_flag = buf64.f_flag;
580 buf->f_bsize = buf64.f_bsize;
581 buf->f_blocks = buf64.f_blocks;
582 buf->f_bfree = buf64.f_bfree;
583 buf->f_files = buf64.f_files;
584 buf->f_ffree = buf64.f_ffree;
585 buf->f_fsid = buf64.f_fsid;
586 buf->f_namemax = buf64.f_namemax;
587 return 0;
588}
589}
590#endif
591
592/******************************************************************************/
593/* t r u n c a t e */
594/******************************************************************************/
595
596#if !defined(SUNX86) && !defined(__FreeBSD__)
597extern "C"
598{
599int truncate(const char *path, off_t offset)
600{
601 static int Init = Xunix.Init(&Init);
602
603 return XrdPosix_Truncate(path, offset);
604}
605}
606#endif
607
608#endif
int statvfs64(const char *path, struct statvfs64 *buf)
int statfs64(const char *path, struct statfs64 *buf)
int stat64(const char *path, struct stat64 *buf)
int XrdPosix_Statfs(const char *path, struct statfs *buf)
Definition XrdPosix.cc:1133
int XrdPosix_Truncate(const char *path, off_t offset)
Definition XrdPosix.cc:1194
int XrdPosix_isMyPath(const char *path)
Definition XrdPosix.cc:1274
long long XrdPosix_Ftello(FILE *stream)
Definition XrdPosix.cc:577
int XrdPosix_Open(const char *path, int oflag,...)
Definition XrdPosix.cc:747
ssize_t XrdPosix_Pread(int fildes, void *buf, size_t nbyte, off_t offset)
Definition XrdPosix.cc:866
int XrdPosix_Readdir64_r(DIR *dirp, struct dirent64 *entry, struct dirent64 **result)
Definition XrdPosix.cc:966
int XrdPosix_Stat(const char *path, struct stat *buf)
Definition XrdPosix.cc:1065
int XrdPosix_Readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
Definition XrdPosix.cc:957
ssize_t XrdPosix_Pwrite(int fildes, const void *buf, size_t nbyte, off_t offset)
Definition XrdPosix.cc:882
int XrdPosix_Lstat(const char *path, struct stat *buf)
Definition XrdPosix.cc:692
int XrdPosix_Fstatat(int dirfd, const char *path, struct stat *buf, int flags)
Definition XrdPosix.cc:501
int XrdPosix_Statvfs(const char *path, struct statvfs *buf)
Definition XrdPosix.cc:1156
int XrdPosix_Fstat(int fildes, struct stat *buf)
Definition XrdPosix.cc:467
off_t XrdPosix_Lseek(int fildes, off_t offset, int whence)
Definition XrdPosix.cc:676
XrdPosixLinkage Xunix
int XrdPosix_Ftruncate(int fildes, long long offset)
Definition XrdPosix.cc:594
struct dirent64 * XrdPosix_Readdir64(DIR *dirp)
Definition XrdPosix.cc:941
int XrdPosix_Fseeko(FILE *stream, long long offset, int whence)
Definition XrdPosix.cc:449
#define fstatat(a, b, c, d)
Definition XrdPosix.hh:64
#define lseek(a, b, c)
Definition XrdPosix.hh:52
#define fstat(a, b)
Definition XrdPosix.hh:62
#define fseeko(a, b, c)
Definition XrdPosix.hh:60
#define readdir_r(a, b, c)
Definition XrdPosix.hh:93
#define open
Definition XrdPosix.hh:78
#define statvfs(a, b)
Definition XrdPosix.hh:113
#define stat(a, b)
Definition XrdPosix.hh:105
#define ftello(a)
Definition XrdPosix.hh:70
#define readdir(a)
Definition XrdPosix.hh:90
#define ftruncate(a, b)
Definition XrdPosix.hh:72
#define truncate(a, b)
Definition XrdPosix.hh:119
#define pwrite(a, b, c, d)
Definition XrdPosix.hh:115
#define statfs(a, b)
Definition XrdPosix.hh:111
#define pread(a, b, c, d)
Definition XrdPosix.hh:84
#define lstat(a, b)
Definition XrdPosix.hh:107
#define dirfd(x)
static bool myFD(int fd)