-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.c
1673 lines (1354 loc) · 40.2 KB
/
wrapper.c
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2006-2017 Niklas Edmundsson <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE 1
#define _XOPEN_SOURCE 600
#define _ALL_SOURCE 1
#define _LARGEFILE64_SOURCE 1
#define _LARGE_FILE_API 1
/* For directio() */
#ifdef __sun
#define __EXTENSIONS__
#endif /* __sun */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/param.h>
#include <dlfcn.h>
#include <stdio.h>
#include <utime.h>
#ifdef __sun
#include <sys/sendfile.h>
#endif /* __sun */
/* Ugh. Solaris is being moronic by defining a wrapper function in header
files for non-LFS stat on 32bit. Ignore it for now, all our software
is only using the LFS interface anyway */
#if defined(__sun) && !defined(_LP64)
#define WRAPPER_STAT_NOWRAP
#endif
#ifndef MIN
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#endif /* MIN */
#ifndef MAX
#define MAX(X, Y) ((X) < (Y) ? (Y) : (X))
#endif /* MAX */
#include "config.h"
#include "cleanpath.c"
#include "cacheopen.c"
/* Emulate RCS $Id$, simply because it's handy to be able to run ident
on an executable/library/etc and see the version.
*/
static const char rcsid[] = "$Id: libhttpcacheopen " GIT_SOURCE_DESC " $";
#ifdef USE_COPYD
typedef struct cachefdinfo_t {
struct stat64 realst; /* stat of real file */
int complete; /* TRUE if cached file complete, FALSE otherwise */
} cachefdinfo_t;
static cachefdinfo_t cachefdinfo[CACHE_MAXFD];
#endif /* USE_COPYD */
/* Declarations for the real functions that we override */
static int (*_open)(const char *, int, ...);
static FILE *(*_fopen)(const char *, const char *);
static FILE *(*_fopen64)(const char *, const char *);
static int (*_chdir)(const char *);
static char *(*_getcwd)(char *, size_t);
#ifdef _AIX
/* Ugh. There have been different type declarations for readlink()
floating around over the years. AIX native uses the old BSD typing. */
/* FIXME: Should probably catch the internal __readlink64 too on AIX, as
that's what the SUSV3 readlink() wrapper uses */
static int (*_readlink)(const char *, char *, size_t);
#else /* _AIX */
static ssize_t (*_readlink)(const char *, char *, size_t);
#endif /* _AIX */
static int realstat64(const char *, struct stat64 *);
#ifdef __linux
/* Ugh. Linux uses inlined wrappers for *stat*, so we need to catch
__*xstat* instead */
static int (*___xstat)(int, __const char *, struct stat *);
static int (*___lxstat)(int, __const char *, struct stat *);
static int (*___fxstat)(int, int, struct stat *);
static int (*___xstat64)(int, __const char *, struct stat64 *);
static int (*___lxstat64)(int, __const char *, struct stat64 *);
#else /* __linux */
#ifndef WRAPPER_STAT_NOWRAP
static int (*_stat)(const char *, struct stat *);
static int (*_lstat)(const char *, struct stat *);
static int (*_fstat)(int, struct stat *);
#endif /* WRAPPER_STAT_NOWRAP */
static int (*_stat64)(const char *, struct stat64 *);
static int (*_lstat64)(const char *, struct stat64 *);
#endif /* __linux */
#ifdef USE_COPYD
static ssize_t (*_read)(int, void *, size_t);
static int (*_close)(int);
static size_t (*_fread)(void *, size_t, size_t, FILE *);
static int (*_fclose)(FILE *fp);
static ssize_t (*_sendfile64)(int, int, off64_t *, size_t);
#ifdef __linux
static int (*___fxstat64)(int, int, struct stat64 *);
#else /* __linux */
static int (*_fstat64)(int, struct stat64 *);
#endif /* __linux */
static int realfstat64(int, struct stat64 *);
#ifdef _AIX
static ssize_t (*_send_file)(int *, struct sf_parms *, uint_t);
#endif /* _AIX */
#endif /* USE_COPYD */
/* The issue of which types can hold function pointers is messy. xlc complains
on mismatch between void * and actual type of function without a cast,
gcc dies on invalid lvalue assignment with cast ... */
#ifdef _AIX
#define DLSYM_RETURN_CAST (void)
#endif
#ifndef DLSYM_RETURN_CAST
#define DLSYM_RETURN_CAST ;
#endif
#define GET_REAL_SYMBOL(a) \
if(! _##a) { \
DLSYM_RETURN_CAST _##a = dlsym( RTLD_NEXT, #a ); \
if(!_##a) { \
perror("httpcacheopen: " #a "(): Init failed"); \
exit(1); \
} \
}
static char *cachedwd; /* Cached working directory */
static char *chrootdir; /* chroot():ed directory, if any */
/* Returns the cleaned path with an eventual chroot prepended */
/* Assumes buf is PATH_MAX in size */
static int get_full_path(char *buf, const char *path) {
char pathcleaned[PATH_MAX];
/* Copy the absolute path (ie. the one inside the current chroot
and clean it from . .. // */
if(path[0] == '/') {
/* New path is absolute */
if(strlen(path) + 1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
strcpy(pathcleaned, path);
}
else if(cachedwd) {
/* Yikes, relative path */
if(strlen(cachedwd) + strlen(path) + 2 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
strcpy(pathcleaned, cachedwd);
strcat(pathcleaned, "/");
strcat(pathcleaned, path);
}
else {
strcpy(pathcleaned, path);
}
cleanpath(pathcleaned);
/* Prepend current chroot, if any */
if(chrootdir != NULL) {
if(strlen(chrootdir) + strlen(pathcleaned) + 1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
strcpy(buf, chrootdir);
}
else {
buf[0] = '\0';
}
strcat(buf, pathcleaned);
#ifdef DEBUG
fprintf(stderr, "httpcacheopen: get_full_path: path=%s buf=%s\n", path,buf);
#endif
return 0;
}
int open(const char *path, int oflag, /* mode_t mode */...) {
va_list ap;
int realfd, cachefd;
mode_t mode;
struct stat64 realst, cachest;
char realpath[PATH_MAX], cachepath[PATH_MAX];
time_t starttime=0;
#ifdef DEBUG
fprintf(stderr, "open: path=%s\n", path);
#endif
GET_REAL_SYMBOL(open);
if(get_full_path(realpath, path) == -1) {
#ifdef DEBUG
perror("open: get_full_path failed");
#endif
return -1;
}
#ifdef DEBUG
fprintf(stderr, "open: realpath=%s\n", realpath);
#endif
if(oflag & O_CREAT) {
va_start(ap, oflag);
mode = va_arg(ap, mode_t);
va_end(ap);
realfd = _open(realpath, oflag, mode);
}
else {
realfd = _open(realpath, oflag);
}
if(geteuid() == 0) {
#ifdef DEBUG
fprintf(stderr, "open: euid == 0, skipping\n");
#endif
return(realfd);
}
if(cacheopen_check(realpath) == -1) {
#ifdef DEBUG
fprintf(stderr, "open: cacheopen_check failed\n");
#endif
return(realfd);
}
if(realfd == -1 || oflag & (O_WRONLY | O_RDWR)) {
#ifdef DEBUG
if(realfd == -1) {
perror("open realfd");
}
else {
fprintf(stderr, "open: Not read-only, oflag=%x\n", oflag);
}
#endif
return(realfd);
}
if(realfstat64(realfd, &realst) == -1) {
#ifdef DEBUG
perror("Unable to fstat realfd");
#endif
/* Somewhere, something went very wrong */
return -1;
}
/* FIXME: Should probably check for realst->st_size > 0 here to avoid
caching zero-byte files. */
/* If we get here, there are possibillities to use a cached copy of the
file in the httpcache instead */
cacheopen_prepare(&realst, cachepath);
GET_REAL_SYMBOL(close);
cachefd = cacheopen(&cachest, &realst, oflag, cachepath, _open, realfstat64,
_close);
if(cachefd == CACHEOPEN_FAIL || cachefd == CACHEOPEN_STALE) {
/* Either no cached file or stale cached file, initiate
file copy once */
GET_REAL_SYMBOL(read);
if(realst.st_size > MAX_COPY_SIZE) {
#ifdef DEBUG
fprintf(stderr, "open: Filesize over copy sizelimit\n");
#endif
#ifdef USE_COPYD
if(copyd_file(realpath, _read, _close) == -1) {
#ifdef DEBUG
fprintf(stderr, "open: copyd_file failed\n");
#endif
return realfd;
}
#else /* USE_COPYD */
return realfd;
#endif /* USE_COPYD */
}
else if(copy_file(realfd, oflag, realst.st_size, realst.st_mtime,
cachepath, _open, realstat64, realfstat64,
_read, _close)
== COPY_FAIL)
{
#ifdef DEBUG
perror("open: copy_file COPY_FAIL");
#endif
return realfd;
}
cachefd = cacheopen(&cachest, &realst, oflag, cachepath, _open,
realfstat64, _close);
}
/* Loop until we've got either a file with contents or a timeout */
while(1) {
if(cachefd == CACHEOPEN_DECLINED) {
return realfd;
}
if(cachefd < 0 && !starttime) {
starttime = time(NULL);
}
/* cacheopen() fills in cachest for us */
#ifdef USE_COPYD
if(cachefd < 0 || cachest.st_size == 0) {
#else /* USE_COPYD */
if(cachefd < 0 || cachest.st_size != realst.st_size) {
#endif /* USE_COPYD */
struct timespec delay;
time_t timeout = time(NULL) - CACHE_UPDATE_TIMEOUT;
if(cachefd >= 0) {
_close(cachefd);
}
if( (cachefd < 0 && starttime < timeout) ||
(cachefd >= 0 && cachest.st_mtime < timeout) )
{
#ifdef DEBUG
fprintf(stderr,
"open: Timed out waiting for cached file\n");
#endif
/* Caching timed out */
return realfd;
}
delay.tv_sec = 0;
delay.tv_nsec = CACHE_LOOP_SLEEP*1000000;
nanosleep(&delay, NULL);
/* And again! */
cachefd = cacheopen(&cachest, &realst, oflag, cachepath, _open,
realfstat64, _close);
continue;
}
/* We're done */
break;
}
#ifdef USE_COPYD
if(cachefd < CACHE_MAXFD) {
/* Save information needed when doing read-while-caching */
if(realst.st_size == cachest.st_size) {
cachefdinfo[cachefd].complete=1;
}
else {
cachefdinfo[cachefd].complete=0;
}
memcpy(&cachefdinfo[cachefd].realst, &realst, sizeof(realst));
}
else {
/* No place in struct, do the best of the situation */
if(realst.st_size != cachest.st_size) {
_close(cachefd);
return(realfd);
}
}
#endif /* USE_COPYD */
/* Victory! */
_close(realfd);
return(cachefd);
}
int open64(const char *path, int oflag, /* mode_t mode */...){
int tmp;
va_list ap;
mode_t mode;
oflag |= O_LARGEFILE;
if(oflag & O_CREAT) {
va_start(ap, oflag);
mode = va_arg(ap, mode_t);
va_end(ap);
tmp = open(path, oflag, mode);
}
else {
tmp = open(path, oflag);
}
return tmp;
}
int __open(const char *path, int oflag, /* mode_t mode */...){
int tmp;
va_list ap;
mode_t mode;
if(oflag & O_CREAT) {
va_start(ap, oflag);
mode = va_arg(ap, mode_t);
va_end(ap);
tmp = open(path, oflag, mode);
}
else {
tmp = open(path, oflag);
}
return tmp;
}
int __open64(const char *path, int oflag, /* mode_t mode */...){
int tmp;
va_list ap;
mode_t mode;
oflag |= O_LARGEFILE;
if(oflag & O_CREAT) {
va_start(ap, oflag);
mode = va_arg(ap, mode_t);
va_end(ap);
tmp = open(path, oflag, mode);
}
else {
tmp = open(path, oflag);
}
return tmp;
}
/* New in glibc 2.7 */
int __open64_2(const char *path, int oflag) {
int tmp;
oflag |= O_LARGEFILE;
tmp = open(path, oflag);
return tmp;
}
/* New in glibc 2.7 */
int __open_2(const char *path, int oflag) {
int tmp;
tmp = open(path, oflag);
return tmp;
}
FILE *fopen(const char *filename, const char *mode) {
if(!filename || !mode) {
errno = ENOENT;
return NULL;
}
/* The only readonly-mode is r and rb */
if(strncmp(mode,"r+", 2) && strncmp(mode,"rb+", 3) &&
!strncmp(mode, "r", 1))
{
int fd;
fd = open64(filename, O_RDONLY);
if(fd == -1) {
return NULL;
}
return fdopen(fd, mode);
}
GET_REAL_SYMBOL(fopen);
if(chrootdir) {
char fullpath[PATH_MAX];
if(get_full_path(fullpath, filename) == -1) {
#ifdef DEBUG
perror("httpcacheopen: fopen: get_full_path failed");
#endif
return NULL;
}
return _fopen(fullpath, mode);
}
return _fopen(filename, mode);
}
FILE *fopen64(const char *filename, const char *mode) {
if(!filename || !mode) {
errno = ENOENT;
return NULL;
}
/* The only readonly-mode is r and rb */
if(strncmp(mode,"r+", 2) && strncmp(mode,"rb+", 3) &&
!strncmp(mode, "r", 1))
{
int fd;
fd = open64(filename, O_RDONLY);
if(fd == -1) {
return NULL;
}
return fdopen(fd, mode);
}
GET_REAL_SYMBOL(fopen64);
if(chrootdir) {
char fullpath[PATH_MAX];
if(get_full_path(fullpath, filename) == -1) {
#ifdef DEBUG
perror("httpcacheopen: fopen64: get_full_path failed");
#endif
return NULL;
}
return _fopen64(fullpath, mode);
}
return _fopen64(filename, mode);
}
int chdir(const char *path) {
char fullpath[PATH_MAX];
int rc;
GET_REAL_SYMBOL(chdir);
if(get_full_path(fullpath, path) == -1) {
#ifdef DEBUG
perror("httpcacheopen: chdir: get_full_path failed");
#endif
return -1;
}
rc = _chdir(fullpath);
if(rc == -1) {
return -1;
}
if(!cachedwd) {
cachedwd = malloc(PATH_MAX*2);
if(cachedwd == NULL) {
return -1;
}
cachedwd[0] = '\0';
}
/* Rely on _chdir having done max path length checking for us */
if(path[0] == '/') {
strcpy(cachedwd, path);
}
else {
strcat(cachedwd, "/");
strcat(cachedwd, path);
}
/* Remove . .. // */
cleanpath(cachedwd);
#ifdef DEBUG
fprintf(stderr, "httpcacheopen: chdir: Cached %s\n", cachedwd);
#endif
return(rc);
}
char *getcwd(char *buffer, size_t size) {
size_t cwdlen;
/* Check if we have cached the working directory, if not do it the
lazy way by calling our chdir that caches it :) */
if(!cachedwd) {
char mycwd[PATH_MAX];
GET_REAL_SYMBOL(getcwd);
if(_getcwd(mycwd, PATH_MAX) == NULL) {
return NULL;
}
if(chdir(mycwd) == -1) {
return NULL;
}
}
cwdlen = strlen(cachedwd) + 1;
if(!(buffer == NULL && size == 0) && cwdlen > size) {
errno = ERANGE;
return NULL;
}
/* Handle linux extension of allocating when buffer == NULL */
if(buffer == NULL) {
if(size == 0) {
size = cwdlen;
}
buffer = malloc(size);
if(buffer == NULL) {
return NULL;
}
}
strcpy(buffer, cachedwd);
#ifdef DEBUG
fprintf(stderr, "httpcacheopen: getcwd: %s\n", buffer);
#endif
return buffer;
}
/* We emulate chroot in order to support FTP daemons and other thingies that
implements a virtual root by doing chroot. */
int chroot(const char *path) {
struct stat64 st;
char newdir[PATH_MAX];
if(!chrootdir) {
chrootdir = malloc(PATH_MAX);
if(chrootdir == NULL) {
return -1;
}
chrootdir[0] = '\0';
}
if(get_full_path(newdir, path) == -1) {
return(-1);
}
#ifdef DEBUG
fprintf(stderr, "httpcacheopen: chroot: newdir=%s\n", newdir);
#endif
/* Do (very) basic checks */
if((realstat64(newdir, &st)) == -1) {
/* Failure, when your best just isn't good enough */
#ifdef DEBUG
perror("httpcacheopen: chroot: stat");
#endif
return(-1);
}
if(!S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
#ifdef DEBUG
perror("httpcacheopen: chroot");
#endif
return(-1);
}
/* Success, set the new chrootdir and go! */
strcpy(chrootdir, newdir);
/* Need to update the cwd so it's correct. */
/* FIXME: In practice we only need to do this when chdir:ing to "."
since when chdir:ing to some other directory you have to do
"chdir /" directly afterwards. So we cheat. */
if(!strcmp(path, ".")) {
if(chdir("/") < 0) {
perror("httpcacheopen: chdir /");
}
}
#ifdef DEBUG
fprintf(stderr, "httpcacheopen: chroot: chrootdir=%s\n", chrootdir);
#endif
return(0);
}
/* Ugh. Linux uses inlined wrappers for the stat-functions, so we
need to catch __*stat* instead. Code duplication for the win */
#ifndef WRAPPER_STAT_NOWRAP /* FIXME: Solaris kludge */
#ifdef __linux
int __xstat (int __ver, __const char *path, struct stat *buffer) {
char realpath[PATH_MAX];
GET_REAL_SYMBOL(__xstat);
/* Check for ABI mismatch */
if(__ver != _STAT_VER) {
fprintf(stderr, "httpcacheopen: __xstat(): stat version mismatch\n");
exit(2);
}
/* Do it the quick way if not chroot */
if(chrootdir == NULL) {
return ___xstat(__ver, path, buffer);
}
if(strlen(path) +1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
if(get_full_path(realpath, path) == -1) {
return -1;
}
return ___xstat(_STAT_VER, realpath, buffer);
}
int __lxstat (int __ver, __const char *path, struct stat *buffer) {
char realpath[PATH_MAX];
GET_REAL_SYMBOL(__lxstat);
/* Do it the quick way if not chroot */
if(chrootdir == NULL) {
return ___lxstat(__ver, path, buffer);
}
/* Check for ABI mismatch */
if(__ver != _STAT_VER) {
fprintf(stderr, "httpcacheopen: __lxstat(): stat version mismatch\n");
exit(2);
}
if(strlen(path) +1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
if(get_full_path(realpath, path) == -1) {
return -1;
}
return ___lxstat(__ver, realpath, buffer);
}
#else /* __linux */
int stat(const char *path, struct stat *buffer) {
char realpath[PATH_MAX];
GET_REAL_SYMBOL(stat);
/* Do it the quick way if not chroot */
if(chrootdir == NULL) {
return _stat(path, buffer);
}
if(strlen(path) +1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
if(get_full_path(realpath, path) == -1) {
return -1;
}
return _stat(realpath, buffer);
}
int lstat(const char *path, struct stat *buffer) {
char realpath[PATH_MAX];
GET_REAL_SYMBOL(lstat);
/* Do it the quick way if not chroot */
if(chrootdir == NULL) {
return _lstat(path, buffer);
}
if(strlen(path) +1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
if(get_full_path(realpath, path) == -1) {
return -1;
}
return _lstat(realpath, buffer);
}
#endif /* __linux */
#endif /* WRAPPER_STAT_NOWRAP */
#ifdef __linux
int __xstat64 (int __ver, __const char *path, struct stat64 *buffer) {
char realpath[PATH_MAX];
/* Check for ABI mismatch */
if(__ver != _STAT_VER) {
fprintf(stderr, "httpcacheopen: __xstat64(): stat version mismatch\n");
exit(2);
}
/* Do it the quick way if not chroot */
if(chrootdir == NULL) {
return realstat64(path, buffer);
}
if(strlen(path) +1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
if(get_full_path(realpath, path) == -1) {
return -1;
}
return realstat64(realpath, buffer);
}
/* Provide a wrapper for the rest of our code */
static int realstat64(const char *path, struct stat64 *buffer) {
GET_REAL_SYMBOL(__xstat64);
return ___xstat64 (_STAT_VER, path, buffer);
}
int __lxstat64 (int __ver, __const char *path, struct stat64 *buffer) {
char realpath[PATH_MAX];
GET_REAL_SYMBOL(__lxstat64);
/* Do it the quick way if not chroot */
if(chrootdir == NULL) {
return ___lxstat64(__ver, path, buffer);
}
/* Check for ABI mismatch */
if(__ver != _STAT_VER) {
fprintf(stderr, "httpcacheopen: __lxstat64(): stat version mismatch\n");
exit(2);
}
if(strlen(path) +1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
if(get_full_path(realpath, path) == -1) {
return -1;
}
return ___lxstat64(__ver, realpath, buffer);
}
#else /* __linux */
int stat64(const char *path, struct stat64 *buffer) {
char realpath[PATH_MAX];
/* Do it the quick way if not chroot */
if(chrootdir == NULL) {
return realstat64(path, buffer);
}
if(strlen(path) +1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
if(get_full_path(realpath, path) == -1) {
return -1;
}
return realstat64(realpath, buffer);
}
/* Provide a wrapper for the rest of our code */
static int realstat64(const char *path, struct stat64 *buffer) {
GET_REAL_SYMBOL(stat64);
return _stat64(path, buffer);
}
int lstat64(const char *path, struct stat64 *buffer) {
char realpath[PATH_MAX];
GET_REAL_SYMBOL(lstat64);
/* Do it the quick way if not chroot */
if(chrootdir == NULL) {
return _lstat64(path, buffer);
}
if(strlen(path) +1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
if(get_full_path(realpath, path) == -1) {
return -1;
}
return _lstat64(realpath, buffer);
}
#endif /* __linux */
#ifdef _AIX
/* AIX native uses the old BSD typing. See comment at beginning of this file */
int
#else
ssize_t
#endif
readlink(const char *path, char *buffer, size_t buffersize) {
char realpath[PATH_MAX];
GET_REAL_SYMBOL(readlink);
/* Do it the quick way if not chroot */
if(chrootdir == NULL) {
return _readlink(path, buffer, buffersize);
}
if(strlen(path) +1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
if(get_full_path(realpath, path) == -1) {
return -1;
}
return _readlink(path, buffer, buffersize);
}
#ifdef USE_COPYD
/* Check if cachefd is complete.
Returns
-1 on error.
0 if file is NOT complete.
1 if file complete.
*/
static int cache_file_complete(int fd, struct stat64 *st) {
/* fd's that don't fit into the array are always complete */
if(fd >= CACHE_MAXFD || fd < 0) {
return 1;
}
/* Zero size means not a cachefd */
if(cachefdinfo[fd].realst.st_size <= 0) {
return 1;
}
if(cachefdinfo[fd].complete) {
return 1;
}
if(realfstat64(fd, st)) {
#ifdef DEBUG
perror("cache_file_complete: fstat64");
#endif
return -1;
}
if(st->st_size >= cachefdinfo[fd].realst.st_size) {
cachefdinfo[fd].complete = 1;
return 1;
}