-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsyscalls.c
1967 lines (1709 loc) · 49.8 KB
/
syscalls.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
/* This is part of pure_libc (a project related to ViewOS and Virtual Square)
*
* syscalls.c: syscall mgmt
*
* Copyright 2006-2023 Renzo Davoli University of Bologna - Italy
* Copyright 2005 Andrea Gasparini University of Bologna - Italy
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* The GNU C 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 the GNU C Library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <config.h>
#include <stdarg.h>
#include <endian.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/syscall.h>
#include <sys/ptrace.h>
#include <sys/uio.h>
#include <sys/times.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/poll.h>
#include <sys/vfs.h>
#include <sys/wait.h>
#include <sys/sysinfo.h>
#include <sys/utsname.h>
#include <sys/timex.h>
#include <sys/sendfile.h>
#include <sys/xattr.h>
#include <sys/timeb.h>
#include <sys/mman.h>
#include <sys/epoll.h>
#include <sys/sysmacros.h>
#include <bits/wordsize.h>
#include <utime.h>
#include <stdarg.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <string.h>
#include <time.h>
#include <grp.h>
#include <limits.h>
#include <sched.h>
#include <stdlib.h>
#include "purelibc.h"
/* in case of pre-init call, use syscall */
sfun _pure_syscall = syscall;
sfun _pure_native_syscall;
#if defined(__NR_mmap2)
static int _pageshift()
{
static int ps=0;
if (ps == 0) {
long pagesize=pagesize = sysconf(_SC_PAGESIZE);
for (ps = -1;pagesize > 0; ps++, pagesize >>= 1)
;
}
return ps;
}
#endif //defined(__NR_mmap2)
long _pure_debug_printf(const char *format, ...)
{
char *s;
int rv;
va_list ap;
va_start(ap, format);
rv=vasprintf(&s, format, ap);
if (rv>0)
_pure_native_syscall(__NR_write,2,s,strlen(s));
free(s);
va_end(ap);
return rv;
}
// open must consider two mode of calling: with two or three arguments
static int __purelibc_open_2(const char* pathname,int flags){
#if defined(__NR_openat) && ! defined(__NR_open)
return _pure_syscall(__NR_openat,AT_FDCWD,pathname,flags);
#else
return _pure_syscall(__NR_open,pathname,flags);
#endif
}
#ifndef __USE_FILE_OFFSET64
int __open_2(const char* pathname,int flags){
return __purelibc_open_2(pathname, flags);
}
#endif
static int __purelibc_open_3(const char* pathname,int flags, mode_t mode) {
#if defined(__NR_openat) && ! defined(__NR_open)
return _pure_syscall(__NR_openat,AT_FDCWD,pathname,flags,mode);
#else
return _pure_syscall(__NR_open,pathname,flags,mode);
#endif
}
#ifndef __USE_FILE_OFFSET64
int open(const char* pathname,int flags,...){
va_list arg_list;
if( flags & O_CREAT ){
mode_t mode;
va_start(arg_list,flags);
mode = va_arg(arg_list,mode_t);
va_end(arg_list);
return __purelibc_open_3(pathname,flags,mode);
}
else
return __purelibc_open_2(pathname,flags);
}
#endif // ! __USE_FILE_OFFSET64
int open64(const char* pathname,int flags,...){
va_list arg_list;
if( flags & O_CREAT ){
mode_t mode;
va_start(arg_list,flags);
mode = va_arg(arg_list,mode_t);
va_end(arg_list);
return __purelibc_open_3(pathname,flags|O_LARGEFILE,mode);
}
else
return __purelibc_open_2(pathname,flags|O_LARGEFILE);
}
int __open64_2 (const char* pathname,int flags){
return __purelibc_open_2(pathname,flags|O_LARGEFILE);
}
#ifndef __USE_FILE_OFFSET64
int creat(const char *pathname, mode_t mode)
{
return __purelibc_open_3(pathname,O_CREAT|O_WRONLY|O_TRUNC,mode);
}
#endif
int creat64(const char *pathname, mode_t mode)
{
return __purelibc_open_3(pathname,O_CREAT|O_WRONLY|O_TRUNC|O_LARGEFILE,mode);
}
int close(int fd){
return _pure_syscall(__NR_close,fd);
}
ssize_t read(int fd,void* buf,size_t count){
return _pure_syscall(__NR_read,fd,buf,count);
}
ssize_t write(int fd,const void* buf,size_t count){
return _pure_syscall(__NR_write,fd,buf,count);
}
ssize_t readv(int filedes, const struct iovec *vector,
int count)
{
return _pure_syscall(__NR_readv, filedes, vector, count);
}
ssize_t writev(int filedes, const struct iovec *vector,
int count)
{
return _pure_syscall(__NR_writev, filedes, vector, count);
}
int dup(int oldfd){
return _pure_syscall(__NR_dup, oldfd);
}
int dup2(int oldfd, int newfd){
#if defined(__NR_dup3) && ! defined(__NR_dup2)
return _pure_syscall(__NR_dup3, oldfd, newfd, 0);
#else
return _pure_syscall(__NR_dup2, oldfd, newfd);
#endif
}
#ifdef __NR_dup3
int dup3(int oldfd, int newfd, int flags){
return _pure_syscall(__NR_dup3, oldfd, newfd, flags);
}
#endif
/* When <sys/stat.h> is included, inline #defines for {,l,f}stat{,64} are
* inserted and they make calls to __{,l,f}xstat{,64}. So we don't need
* to define them.
*/
/* Since libc developers seem to be quite sadic in writing unreadable code,
* making me go crazy trying to understand it, I decided to have some fun
* myself. The following not-so-readable stuff takes care of calling the
* correct 64-bit function on both 32 bit and 64 bit architectures.*/
/* update: glibc 2.33 has a simpler API, no more __ functions */
#ifdef __NR_fstatat64
#define __NR_FSTATAT64 __NR_fstatat64
#endif
#ifdef __NR_newfstatat
#define __NR_FSTATAT64 __NR_newfstatat
#endif
#if defined(__NR_statx) && ! defined(__NR_FSTATAT64)
#define __NR_FSTATAT64 __NR_statx
int __purelibc_newstat64(int dirfd, const char *restrict pathname,
struct stat *restrict statbuf, int flags) {
struct statx buf[1];
int rv = _pure_syscall(__NR_statx, dirfd, pathname, flags, STATX_BASIC_STATS, buf);
if (rv == 0) {
statbuf->st_dev = makedev(buf->stx_dev_major, buf->stx_dev_minor);
statbuf->st_ino = buf->stx_ino;
statbuf->st_mode = buf->stx_mode;
statbuf->st_nlink = buf->stx_nlink;
statbuf->st_uid = buf->stx_uid;
statbuf->st_gid = buf->stx_gid;
statbuf->st_rdev = makedev(buf->stx_rdev_major, buf->stx_rdev_minor);
statbuf->st_size = buf->stx_size;
statbuf->st_blksize = buf->stx_blksize;
statbuf->st_blocks = buf->stx_blocks;
statbuf->st_atim.tv_sec = buf->stx_atime.tv_sec;
statbuf->st_atim.tv_nsec = buf->stx_atime.tv_nsec;
statbuf->st_mtim.tv_sec = buf->stx_mtime.tv_sec;
statbuf->st_mtim.tv_nsec = buf->stx_mtime.tv_nsec;
statbuf->st_ctim.tv_sec = buf->stx_ctime.tv_sec;
statbuf->st_ctim.tv_nsec = buf->stx_ctime.tv_nsec;
}
return rv;
}
#else // defined(__NR_statx) && ! defined(__NR_FSTATAT64)
#define __purelibc_newstat64(dirfd, pathname, statbuf, flags) \
_pure_syscall(__NR_FSTATAT64, (dirfd), (pathname), (statbuf), (flags))
#endif // defined(__NR_statx) && ! defined(__NR_FSTATAT64)
#if __WORDSIZE == 64 || defined(__ILP32__)
# if defined(__NR_FSTATAT64) && ! defined(__NR_stat)
# define __USE_NEWSTAT64_STAT
# endif
# if defined(__NR_FSTATAT64) && ! defined(__NR_fstat)
# define __USE_NEWSTAT64_FSTAT
# endif
# define arch_stat64 stat
# define IFNOT64(x)
#else // __WORDSIZE == 64 || defined(__ILP32__)
# if defined(__NR_FSTATAT64) && ! defined(__NR_stat64)
# define __USE_NEWSTAT64_STAT
# endif
# if defined(__NR_FSTATAT64) && ! defined(__NR_fstat64)
# define __USE_NEWSTAT64_FSTAT
# endif
# define arch_stat64 stat64
# define IFNOT64(x) x
#endif // __WORDSIZE == 64 || defined(__ILP32__)
#define INTERNAL_MAKE_NAME(a, b) a ## b
#define MAKE_NAME(a, b) INTERNAL_MAKE_NAME(a, b)
#ifndef __USE_TIME_BITS64
static void arch_stat64_2_stat(struct arch_stat64 *from, struct stat *to)
{
if ((void*)from == (void*)to)
return;
to->st_dev = from->st_dev;
to->st_ino = from->st_ino;
to->st_mode = from->st_mode;
to->st_nlink = from->st_nlink;
to->st_uid = from->st_uid;
to->st_gid = from->st_gid;
to->st_rdev = from->st_rdev;
to->st_size = from->st_size;
to->st_blksize = from->st_blksize;
to->st_blocks = from->st_blocks;
to->st_atim = from->st_atim;
to->st_mtim = from->st_mtim;
to->st_ctim = from->st_ctim;
return;
}
#endif // __USE_TIME_BITS64
#if __GNUC_PREREQ (2,33)
#ifndef __USE_TIME_BITS64
int stat(const char* pathname, struct stat* buf_stat)
{
IFNOT64(struct stat64 *buf_stat64 = alloca(sizeof(struct stat64)));
int rv;
#ifdef __USE_NEWSTAT64_STAT
rv = __purelibc_newstat64(AT_FDCWD, pathname, MAKE_NAME(buf_, arch_stat64), 0);
#else
rv = _pure_syscall(MAKE_NAME(__NR_, arch_stat64), pathname, MAKE_NAME(buf_, arch_stat64));
#endif
if (rv >= 0)
arch_stat64_2_stat(MAKE_NAME(buf_, arch_stat64), buf_stat);
return rv;
}
int lstat(const char* pathname, struct stat* buf_stat)
{
IFNOT64(struct stat64 *buf_stat64 = alloca(sizeof(struct stat64)));
int rv;
#ifdef __USE_NEWSTAT64_STAT
rv = __purelibc_newstat64(AT_FDCWD, pathname, MAKE_NAME(buf_, arch_stat64), AT_SYMLINK_NOFOLLOW);
#else
rv = _pure_syscall(MAKE_NAME(__NR_l, arch_stat64), pathname, MAKE_NAME(buf_, arch_stat64));
#endif
if (rv >= 0)
arch_stat64_2_stat(MAKE_NAME(buf_, arch_stat64), buf_stat);
return rv;
}
int fstat(int fildes, struct stat* buf_stat)
{
IFNOT64(struct stat64 *buf_stat64 = alloca(sizeof(struct stat64)));
int rv;
#ifdef __USE_NEWSTAT64_FSTAT
rv = __purelibc_newstat64(fildes, "", MAKE_NAME(buf_, arch_stat64), AT_EMPTY_PATH);
#else
rv = _pure_syscall(MAKE_NAME(__NR_f, arch_stat64), fildes, MAKE_NAME(buf_, arch_stat64));
#endif
if (rv >= 0)
arch_stat64_2_stat(MAKE_NAME(buf_, arch_stat64), buf_stat);
return rv;
}
#else // ! __USE_TIME_BITS64
int stat(const char* pathname,struct stat* buf){
#ifdef __USE_NEWSTAT64_STAT
return __purelibc_newstat64(AT_FDCWD, pathname, buf, 0);
#else
return _pure_syscall(MAKE_NAME(__NR_, arch_stat64), pathname, buf);
#endif
}
int lstat(const char* pathname,struct stat* buf){
#ifdef __USE_NEWSTAT64_STAT
return __purelibc_newstat64(AT_FDCWD, pathname, buf, AT_SYMLINK_NOFOLLOW);
#else
return _pure_syscall(MAKE_NAME(__NR_l, arch_stat64), pathname, buf);
#endif
}
int fstat (int fildes, struct stat *buf){
#ifdef __USE_NEWSTAT64_FSTAT
return __purelibc_newstat64(fildes, "", buf, AT_EMPTY_PATH);
#else
return _pure_syscall(MAKE_NAME(__NR_f, arch_stat64), fildes, buf);
#endif
}
#endif // ! __USE_TIME_BITS64
int stat64(const char* pathname,struct stat64* buf){
#ifdef __USE_NEWSTAT64_STAT
return __purelibc_newstat64(AT_FDCWD, pathname, buf, 0);
#else
return _pure_syscall(MAKE_NAME(__NR_, arch_stat64), pathname, buf);
#endif
}
int lstat64(const char* pathname,struct stat64* buf){
#ifdef __USE_NEWSTAT64_STAT
return __purelibc_newstat64(AT_FDCWD, pathname, buf, AT_SYMLINK_NOFOLLOW);
#else
return _pure_syscall(MAKE_NAME(__NR_l, arch_stat64), pathname, buf);
#endif
}
int fstat64 (int fildes, struct stat64 *buf){
#ifdef __USE_NEWSTAT64_FSTAT
return __purelibc_newstat64(fildes, "", buf, AT_EMPTY_PATH);
#else
return _pure_syscall(MAKE_NAME(__NR_f, arch_stat64), fildes, buf);
#endif
}
int mknod(const char *pathname, mode_t mode, dev_t dev) {
#if defined(__NR_mknodat) && ! defined(__NR_mknod)
return _pure_syscall(__NR_mknodat,AT_FDCWD,pathname,mode,dev);
#else
return _pure_syscall(__NR_mknod,pathname,mode,dev);
#endif
}
#else // __GNUC_PREREQ (2,33)
int __xstat(int ver, const char* pathname, struct stat* buf_stat)
{
IFNOT64(struct stat64 *buf_stat64 = alloca(sizeof(struct stat64)));
int rv;
switch(ver)
{
case _STAT_VER_LINUX:
#ifdef __USE_NEWSTAT64_STAT
rv = __purelibc_newstat64(AT_FDCWD, pathname, MAKE_NAME(buf_, arch_stat64), 0);
#else
rv = _pure_syscall(MAKE_NAME(__NR_, arch_stat64), pathname, MAKE_NAME(buf_, arch_stat64));
#endif
break;
default:
_pure_debug_printf("*** BUG! *** __xstat can't manage version %d!\n", ver);
abort();
}
if (rv >= 0)
arch_stat64_2_stat(MAKE_NAME(buf_, arch_stat64), buf_stat);
return rv;
}
int __lxstat(int ver, const char* pathname, struct stat* buf_stat)
{
IFNOT64(struct stat64 *buf_stat64 = alloca(sizeof(struct stat64)));
int rv;
switch(ver)
{
case _STAT_VER_LINUX:
#ifdef __USE_NEWSTAT64_STAT
rv = __purelibc_newstat64(AT_FDCWD, pathname, MAKE_NAME(buf_, arch_stat64), AT_SYMLINK_NOFOLLOW);
#else
rv = _pure_syscall(MAKE_NAME(__NR_l, arch_stat64), pathname, MAKE_NAME(buf_, arch_stat64));
#endif
break;
default:
_pure_debug_printf("*** BUG! *** __lxstat can't manage version %d!\n", ver);
abort();
}
if (rv >= 0)
arch_stat64_2_stat(MAKE_NAME(buf_, arch_stat64), buf_stat);
return rv;
}
int __fxstat(int ver, int fildes, struct stat* buf_stat)
{
IFNOT64(struct stat64 *buf_stat64 = alloca(sizeof(struct stat64)));
int rv;
switch(ver)
{
case _STAT_VER_LINUX:
#ifdef __USE_NEWSTAT64_FSTAT
rv = __purelibc_newstat64(fildes, "", MAKE_NAME(buf_, arch_stat64), AT_EMPTY_PATH);
#else
rv = _pure_syscall(MAKE_NAME(__NR_f, arch_stat64), fildes, MAKE_NAME(buf_, arch_stat64));
#endif
break;
default:
_pure_debug_printf("*** BUG! *** __fxstat can't manage version %d!\n", ver);
abort();
}
if (rv >= 0)
arch_stat64_2_stat(MAKE_NAME(buf_, arch_stat64), buf_stat);
return rv;
}
int __xstat64(int ver,const char* pathname,struct stat64* buf){
#ifdef __USE_NEWSTAT64_STAT
return __purelibc_newstat64(AT_FDCWD, pathname, buf, 0);
#else
return _pure_syscall(MAKE_NAME(__NR_, arch_stat64), pathname, buf);
#endif
}
int __lxstat64(int ver,const char* pathname,struct stat64* buf){
#ifdef __USE_NEWSTAT64_STAT
return __purelibc_newstat64(AT_FDCWD, pathname, buf, AT_SYMLINK_NOFOLLOW);
#else
return _pure_syscall(MAKE_NAME(__NR_l, arch_stat64), pathname, buf);
#endif
}
int __fxstat64 (int ver, int fildes, struct stat64 *buf){
#ifdef __USE_NEWSTAT64_FSTAT
return __purelibc_newstat64(fildes, "", buf, AT_EMPTY_PATH);
#else
return _pure_syscall(MAKE_NAME(__NR_f, arch_stat64), fildes, buf);
#endif
}
#ifdef __NR_FSTATAT64
int __fxstatat64 (int ver, int dirfd, const char *pathname, struct stat64 *buf, int flags){
return __purelibc_newstat64(dirfd,pathname,buf,flags);
}
int __fxstatat(int ver, int fildes, const char *pathname, struct stat* buf_stat,int flags)
{
IFNOT64(struct stat64 *buf_stat64 = alloca(sizeof(struct stat64)));
int rv;
switch(ver)
{
case _STAT_VER_LINUX:
rv = __purelibc_newstat64(fildes, pathname, MAKE_NAME(buf_, arch_stat64), flags);
break;
default:
_pure_debug_printf("*** BUG! *** __fxstatat can't manage version %d!\n", ver);
abort();
}
if (rv >= 0)
arch_stat64_2_stat(MAKE_NAME(buf_, arch_stat64), buf_stat);
return rv;
}
#endif
int __xmknod (int ver, const char *path, mode_t mode, dev_t *dev) {
#if defined(__NR_mknodat) && ! defined(__NR_mknod)
return _pure_syscall(__NR_mknodat,AT_FDCWD,path,mode,dev);
#else
return _pure_syscall(__NR_mknod,path,mode,dev);
#endif
}
#endif // __GNUC_PREREQ (2,33)
/* end of unreadable code */
#ifdef __NR_FSTATAT64
int fstatat(int dirfd, const char *pathname, struct stat *buf, int flags) {
return __purelibc_newstat64(dirfd,pathname,buf,flags);
}
#endif
#ifdef __NR_statx
int statx(int dirfd, const char *pathname, int flags,
unsigned int mask, struct statx *statxbuf) {
return _pure_syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
}
#endif
int access(const char* pathname,int mode){
#if defined(__NR_faccessat) && ! defined(__NR_access)
return _pure_syscall(__NR_faccessat,AT_FDCWD,pathname,mode,0);
#else
return _pure_syscall(__NR_access,pathname,mode);
#endif
}
int __access(const char* pathname,int mode){
return access(pathname,mode);
}
ssize_t readlink(const char* pathname,char* buf, size_t bufsize){
#if defined(__NR_readlinkat) && ! defined(__NR_readlink)
return _pure_syscall(__NR_readlinkat,AT_FDCWD,pathname,buf,bufsize);
#else
return _pure_syscall(__NR_readlink,pathname,buf,bufsize);
#endif
}
int mkdir(const char* pathname,mode_t mode){
#if defined(__NR_mkdirat) && ! defined(__NR_mkdir)
return _pure_syscall(__NR_mkdirat,AT_FDCWD,pathname,mode);
#else
return _pure_syscall(__NR_mkdir,pathname,mode);
#endif
}
int rmdir(const char* pathname){
#if defined(__NR_unlinkat) && ! defined(__NR_rmdir)
return _pure_syscall(__NR_unlinkat,AT_FDCWD,pathname,AT_REMOVEDIR);
#else
return _pure_syscall(__NR_rmdir,pathname);
#endif
}
int chmod(const char* pathname,mode_t mode){
#if defined(__NR_fchownat) && ! defined(__NR_chmod)
return _pure_syscall(__NR_fchmodat,AT_FDCWD,pathname,mode,0);
#else
return _pure_syscall(__NR_chmod,pathname,mode);
#endif
}
int fchmod(int fd,mode_t mode){
return _pure_syscall(__NR_fchmod,fd,mode);
}
int chown(const char* pathname,uid_t owner,gid_t group){
#if defined(__NR_fchownat) && ! defined(__NR_chown)
return _pure_syscall(__NR_fchownat,AT_FDCWD,pathname,owner,group,0);
#else
return _pure_syscall(__NR_chown,pathname,owner,group);
#endif
}
int lchown(const char* pathname,uid_t owner,gid_t group){
#if defined(__NR_fchownat) && ! defined(__NR_lchown)
return _pure_syscall(__NR_fchownat,AT_FDCWD,pathname,owner,group,AT_SYMLINK_NOFOLLOW);
#else
return _pure_syscall(__NR_lchown,pathname,owner,group);
#endif
}
int fchown(int fd,uid_t owner,gid_t group){
return _pure_syscall(__NR_fchown,fd,owner,group);
}
int link(const char* pathname,const char*newpath){
#if defined(__NR_linkat) && ! defined(__NR_link)
return _pure_syscall(__NR_linkat,AT_FDCWD,pathname,AT_FDCWD,newpath,0);
#else
return _pure_syscall(__NR_link,pathname,newpath);
#endif
}
int unlink(const char* pathname){
#if defined(__NR_unlinkat) && ! defined(__NR_unlink)
return _pure_syscall(__NR_unlinkat,AT_FDCWD,pathname,0);
#else
return _pure_syscall(__NR_unlink,pathname);
#endif
}
int symlink(const char* pathname,const char* newpath){
#if defined(__NR_symlinkat) && ! defined(__NR_symlink)
return _pure_syscall(__NR_symlinkat,pathname,AT_FDCWD,newpath,0);
#else
return _pure_syscall(__NR_symlink,pathname,newpath);
#endif
}
int rename(const char *oldpath, const char *newpath){
#if defined(__NR_renameat2) && ! defined(__NR_rename)
return _pure_syscall(__NR_renameat2,AT_FDCWD,oldpath,AT_FDCWD,newpath,0);
#else
return _pure_syscall(__NR_rename,oldpath,newpath);
#endif
}
int chdir(const char *path) {
return _pure_syscall(__NR_chdir,path);
}
int fchdir(int fd) {
return _pure_syscall(__NR_fchdir,fd);
}
int utimes(const char* pathname,const struct timeval tv[2]){
#if defined(__NR_utimensat) && ! defined(__NR_utimes)
struct timespec ts[2] = {
{tv[0].tv_sec, tv[0].tv_usec * 1000},
{tv[1].tv_sec, tv[1].tv_usec * 1000}};
return _pure_syscall(__NR_utimensat, AT_FDCWD, pathname, ts, 0);
#else
return _pure_syscall(__NR_utimes,pathname,tv);
#endif
}
int utime(const char* pathname,const struct utimbuf *buf){
#ifdef __NR_utime
return _pure_syscall(__NR_utime,pathname,buf);
#else
struct timeval tv[2];
tv[0].tv_sec = buf->actime;
tv[1].tv_sec = buf->modtime;
tv[0].tv_usec = tv[1].tv_usec = 0;
return utimes(pathname, tv);
#endif
}
#ifdef __NR_pread
#ifndef __USE_FILE_OFFSET64
ssize_t pread(int fs,void* buf, size_t count, __off_t offset){
return _pure_syscall(__NR_pread,fs,buf,count,offset);
}
#endif
#endif
#ifdef __NR_pwrite
#ifndef __USE_FILE_OFFSET64
ssize_t pwrite(int fs,const void* buf, size_t count, __off_t offset){
return _pure_syscall(__NR_pwrite,fs,buf,count,offset);
}
#endif
#endif
#ifdef __NR_pread64
ssize_t pread64(int fs,void* buf, size_t count, __off64_t offset){
return _pure_syscall(__NR_pread64,fs,buf,count,
#if defined(__powerpc__) || defined(__arm__)
0,
#endif
__LONG_LONG_PAIR((__off_t)(offset>>32),(__off_t)(offset&0xffffffff)));
}
#ifndef __USE_FILE_OFFSET64
ssize_t pread(int fs,void* buf, size_t count, __off_t offset){
return pread64(fs,buf,count,(__off64_t)offset);
}
#endif
#endif // __NR_pread64
#ifdef __NR_pwrite64
ssize_t pwrite64(int fs,const void* buf, size_t count, __off64_t offset){
return _pure_syscall(__NR_pwrite64,fs,buf,count,
#if defined(__powerpc__) || defined(__arm__)
0,
#endif
__LONG_LONG_PAIR((__off_t)(offset>>32),(__off_t)(offset&0xffffffff)));
}
#ifndef __USE_FILE_OFFSET64
ssize_t pwrite(int fs,const void* buf, size_t count, __off_t offset){
return pwrite64(fs,buf,count,(__off64_t)offset);
}
#endif
#endif // __NR_pwrite64
#ifdef __NR_preadv
ssize_t preadv64(int fs,const struct iovec *iov, int iovcnt, __off64_t offset){
ssize_t rv=_pure_syscall(__NR_preadv,fs,iov,iovcnt,
#ifdef __NR_pread64
#if defined(__powerpc__) || defined(__arm__)
0,
#endif
__LONG_LONG_PAIR((__off_t)(offset>>32),(__off_t)(offset&0xffffffff))
#else
offset
#endif
);
if (rv==-1 && errno==ENOSYS) {
ssize_t totalsize;
unsigned char *buf,*scan;
int i;
for (i=totalsize=0; i<iovcnt; i++)
totalsize+=iov[i].iov_len;
buf=malloc(totalsize);
if (buf==NULL) {
errno=ENOMEM;
return -1;
}
totalsize=pread64(fs, buf, totalsize, offset);
rv=totalsize;
for (i=0,scan=buf; i<iovcnt && totalsize>0; i++,
scan+=iov[i].iov_len,
totalsize-=iov[i].iov_len)
memcpy(iov[i].iov_base, scan, iov[i].iov_len);
free(buf);
}
return rv;
}
#ifndef __USE_FILE_OFFSET64
ssize_t preadv(int fs,const struct iovec *iov, int iovcnt, __off_t offset){
return preadv64(fs,iov,iovcnt,(__off64_t)offset);
}
#endif
#endif // __NR_preadv
#ifdef __NR_pwritev
ssize_t pwritev64(int fs,const struct iovec *iov, int iovcnt, __off64_t offset){
ssize_t rv=_pure_syscall(__NR_pwritev,fs,iov,iovcnt,
#ifdef __NR_pwrite64
#if defined(__powerpc__) || defined(__arm__)
0,
#endif
__LONG_LONG_PAIR((__off_t)(offset>>32),(__off_t)(offset&0xffffffff))
#else
offset
#endif
);
if (rv==-1 && errno==ENOSYS) {
ssize_t totalsize;
unsigned char *buf,*scan;
int i;
for (i=totalsize=0; i<iovcnt; i++)
totalsize+=iov[i].iov_len;
buf=malloc(totalsize);
if (buf==NULL) {
errno=ENOMEM;
return -1;
}
for (i=0,scan=buf; i<iovcnt; i++, scan+=iov[i].iov_len)
memcpy(scan, iov[i].iov_base, iov[i].iov_len);
rv=pwrite64(fs, buf, totalsize, offset);
free(buf);
}
return rv;
}
#ifndef __USE_FILE_OFFSET64
ssize_t pwritev(int fs,const struct iovec *iov, int iovcnt, __off_t offset){
return pwritev64(fs,iov,iovcnt,(__off64_t)offset);
}
#endif
#endif // __NR_pwritev
/* getdents has no libc wrapper.
libc uses getdents64 only on 64 bit archs */
int getdents(int fd, void *dirp, unsigned int count){
#ifdef __NR_getdents
return _pure_syscall(__NR_getdents, fd, dirp, count);
#else
errno = ENOSYS;
return -1;
#endif
}
__ssize_t getdents64(int fd, void *dirp, size_t count){
return _pure_syscall(__NR_getdents64, fd, dirp, count);
}
#ifndef __USE_FILE_OFFSET64
__off_t lseek(int fd,__off_t offset,int whence){
return _pure_syscall(__NR_lseek,fd,offset,whence);
}
#endif
#ifndef __NR__llseek
off64_t lseek64(int fd, off64_t offset, int whence) {
return lseek(fd, offset, whence);
}
#endif
#ifdef __NR__llseek
__off64_t lseek64(int fd, __off64_t offset, int whence){
unsigned long offset_high=offset >> (sizeof(unsigned long) * 8);
unsigned long offset_low=offset;
__off64_t result;
int rv=_pure_syscall(__NR__llseek,fd,offset_high,offset_low,&result,whence);
if (rv<0)
return rv;
else
return result;
}
__off64_t llseek(int fd, __off64_t offset, int whence){
return lseek64(fd,offset,whence);
}
int _llseek(unsigned int fd, unsigned long offset_high, unsigned long offset_low, loff_t
*result, unsigned int whence){
return _pure_syscall(__NR__llseek,fd,offset_high,offset_low,result,whence);
}
#endif
int fsync(int fd){
return _pure_syscall(__NR_fsync,fd);
}
#if defined(__powerpc__)
/* DAMNED! Another kernel specific structure needs lib conversion! */
#include<sys/ioctl.h>
#include<termios.h>
#include"kernel_termios.ppc.h"
static void termios_k2l(struct termios *dst,struct __kernel_termios *src) {
register int i;
dst->c_iflag = src->c_iflag;
dst->c_oflag = src->c_oflag;
dst->c_cflag = src->c_cflag;
dst->c_lflag = src->c_lflag;
dst->c_line = src->c_line;
dst->c_ispeed = src->c_ispeed;
dst->c_ospeed = src->c_ospeed;
for (i=0; i<__KERNEL_NCCS; i++)
dst->c_cc[i]=src->c_cc[i];
for (;i<NCCS;i++)
dst->c_cc[i]=_POSIX_VDISABLE;
}
static void termios_l2k(struct __kernel_termios *dst,struct termios *src) {
register int i;
dst->c_iflag = src->c_iflag;
dst->c_oflag = src->c_oflag;
dst->c_cflag = src->c_cflag;
dst->c_lflag = src->c_lflag;
dst->c_line = src->c_line;
dst->c_ispeed = src->c_ispeed;
dst->c_ospeed = src->c_ospeed;
for (i=0; i<__KERNEL_NCCS; i++)
dst->c_cc[i]=src->c_cc[i];
}
static int ioctl_ppc(int fd,unsigned long int request, long int arg){
int result;
switch (request) {
case TCGETS: {
struct __kernel_termios kt;
result = _pure_syscall (__NR_ioctl, fd, request, &kt);
termios_k2l((struct termios *) arg, &kt);
break;
}
case TCSETS:
case TCSETSW:
case TCSETSF: {
struct __kernel_termios kt;
termios_l2k(&kt,(struct termios *) arg);
result = _pure_syscall (__NR_ioctl, fd, request, &kt);
break;
}
default:
result = _pure_syscall (__NR_ioctl, fd, request, arg);
break;
}
return result;
}
#endif // defined(__powerpc__)
int ioctl(int fd,unsigned long int request, ...){
va_list ap;
long int arg;
va_start(ap, request);
arg=va_arg(ap, long int);
va_end(ap);
int rv=0;
#if defined(__powerpc__)
ioctl_ppc(fd,request,arg);
#else
rv= _pure_syscall(__NR_ioctl,fd,request,arg);
#endif
return rv;
}
#ifndef __USE_TIME_BITS64
int fcntl(int fd, int cmd, ...){
va_list ap;
long int arg1;
long int arg2;
va_start(ap, cmd);
arg1=va_arg(ap, long int);
arg2=va_arg(ap, long int);
va_end(ap);
/* XXX Check the fcntl->fcntl64 conversion */
#ifdef __NR_fcntl64
switch (cmd) {
case F_GETLK:
case F_SETLK:
case F_SETLKW:
return _pure_syscall(__NR_fcntl,fd,cmd,arg1,arg2);
default:
return _pure_syscall(__NR_fcntl64,fd,cmd,arg1,arg2);
}
#else
return _pure_syscall(__NR_fcntl,fd,cmd,arg1,arg2);
#endif
}
#endif
#ifdef __NR_fcntl64
int fcntl64(int fd, int cmd, ...){
va_list ap;
long int arg1;
long int arg2;
va_start(ap, cmd);
arg1=va_arg(ap, long int);
arg2=va_arg(ap, long int);
va_end(ap);
return _pure_syscall(__NR_fcntl64,fd,cmd,arg1,arg2);
}
#endif
int mount(const char *source, const char *target, const char *filesystemtype, unsigned mountflags, const void *data){
return _pure_syscall(__NR_mount,source,target,filesystemtype,mountflags,data);
}
#ifndef __NR_umount
#define __NR_umount __NR_umount2
#endif
int umount(const char *target){
// umount ignore the last argument, is only for umount2
return _pure_syscall(__NR_umount,target,0);
}
int umount2(const char *target, int flags){
return _pure_syscall(__NR_umount,target,flags);
}
pid_t getpid(void){
return _pure_syscall(__NR_getpid);