forked from oracle/graalpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposix.c
More file actions
1229 lines (1066 loc) · 35.9 KB
/
posix.c
File metadata and controls
1229 lines (1066 loc) · 35.9 KB
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 (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// Helper functions that mostly delegate to POSIX functions
// These functions are called from NFIPosixSupport Java class using NFI
// This file uses GNU extensions. Functions that require non-GNU versions (e.g. strerror_r)
// need to go to posix_no_gnu.c
#if defined(__gnu_linux__) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE 1
#endif
#include <arpa/inet.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <semaphore.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <unistd.h>
#include <pwd.h>
int32_t signal_self_segv(void);
#ifdef __APPLE__
#include <util.h>
#else
#include <pty.h>
#endif
#ifndef _WIN32
#include <time.h>
#include <poll.h>
#include <limits.h>
#include <sys/resource.h>
#endif
int64_t call_getpid() {
return getpid();
}
int32_t call_umask(int32_t mask) {
return umask(mask);
}
int32_t get_inheritable(int32_t fd) {
int flags = fcntl(fd, F_GETFD, 0);
if (flags < 0) {
return -1;
}
return !(flags & FD_CLOEXEC);
}
// note: this is also called between fork() and exec()
int32_t set_inheritable(int32_t fd, int32_t inheritable) {
int res = fcntl(fd, F_GETFD);
if (res >= 0) {
int new_flags;
if (inheritable) {
new_flags = res & ~FD_CLOEXEC;
} else {
new_flags = res | FD_CLOEXEC;
}
if (new_flags != res) {
res = fcntl(fd, F_SETFD, new_flags);
}
}
return res;
}
int32_t call_openat(int32_t dirFd, const char *pathname, int32_t flags, int32_t mode) {
return openat(dirFd, pathname, flags, mode);
}
int32_t call_close(int32_t fd) {
return close(fd);
}
int64_t call_read(int32_t fd, void *buf, uint64_t count) {
return read(fd, buf, count);
}
int64_t call_write(int32_t fd, void *buf, uint64_t count) {
return write(fd, buf, count);
}
int32_t call_dup(int32_t fd) {
return fcntl(fd, F_DUPFD_CLOEXEC, 0);
}
int32_t call_dup2(int32_t oldfd, int32_t newfd, int32_t inheritable) {
#ifdef __gnu_linux__
if (!inheritable) {
return dup3(oldfd, newfd, O_CLOEXEC);
}
#endif
int res = dup2(oldfd, newfd);
if (res < 0) {
return res;
}
if (!inheritable) {
if (set_inheritable(res, 0) < 0) {
close(res);
return -1;
}
}
return res;
}
int32_t call_pipe2(int32_t *pipefd) {
#ifdef __gnu_linux__
return pipe2(pipefd, O_CLOEXEC);
#else
int res = pipe(pipefd);
if (res != 0) {
return res;
}
if (set_inheritable(pipefd[0], 0) < 0 || set_inheritable(pipefd[1], 0) < 0) {
close(pipefd[0]);
close(pipefd[1]);
return -1;
}
return 0;
#endif
}
static void fill_select_result(int32_t* fds, int32_t fdsLen, fd_set* set, int8_t* result, int32_t resultOffset) {
for (int32_t i = 0; i < fdsLen; ++i) {
if (FD_ISSET(fds[i], set)) {
result[resultOffset + i] = 1;
}
}
}
static void fill_fd_set(fd_set *set, int32_t* fds, int32_t len) {
FD_ZERO(set);
for (int32_t i = 0; i < len; ++i) {
FD_SET(fds[i], set);
}
}
// selected is output parameter, a non-zero value will be written to
// indices of file descriptors that were selected. The array indices
// should be interpreted as if read/write/err file descriptors were
// concatendated into one array
int32_t call_select(int32_t nfds, int32_t* readfds, int32_t readfdsLen,
int32_t* writefds, int32_t writefdsLen, int32_t* errfds, int32_t errfdsLen,
int64_t timeoutSec, int64_t timeoutUsec, int8_t* selected) {
fd_set readfdsSet, writefdsSet, errfdsSet;
fill_fd_set(&readfdsSet, readfds, readfdsLen);
fill_fd_set(&writefdsSet, writefds, writefdsLen);
fill_fd_set(&errfdsSet, errfds, errfdsLen);
struct timeval timeout = {timeoutSec, timeoutUsec};
int result = select(nfds, &readfdsSet, &writefdsSet, &errfdsSet, timeoutSec >= 0 ? &timeout : NULL);
// fill in the output parameter
fill_select_result(readfds, readfdsLen, &readfdsSet, selected, 0);
fill_select_result(writefds, writefdsLen, &writefdsSet, selected, readfdsLen);
fill_select_result(errfds, errfdsLen, &errfdsSet, selected, readfdsLen + writefdsLen);
return (int32_t) result;
}
int32_t call_poll(int32_t fd, int32_t writing, int64_t timeoutSec, int64_t timeoutUsec) {
#ifdef _WIN32
// for windows, use select() as a worse fallback
int selected[2] = {0, 0};
return call_select(1,
writing ? NULL : &fd, writing ? 0 : 1,
writing ? &fd : NULL, writing ? 1 : 0,
NULL, 0,
timeoutSec, timeoutUsec, &selected);
#else
struct pollfd pollfd;
pollfd.fd = fd;
pollfd.events = writing ? POLLOUT : POLLIN;
int timeout_ms;
if (timeoutSec < 0) {
timeout_ms = -1;
} else if (timeoutSec > INT_MAX / 1000) {
errno = EINVAL;
return -1;
} else {
int64_t timeout_ms_64 = timeoutSec * 1000 + timeoutUsec / 1000;
if (timeout_ms_64 > INT_MAX) {
errno = EINVAL;
return -1;
}
timeout_ms = (int)timeout_ms_64;
}
return poll(&pollfd, 1, timeout_ms);
#endif
}
int64_t call_lseek(int32_t fd, int64_t offset, int32_t whence) {
return lseek(fd, offset, whence);
}
int32_t call_ftruncate(int32_t fd, int64_t length) {
return ftruncate(fd, length);
}
int32_t call_truncate(const char* path, int64_t length) {
return truncate(path, length);
}
int32_t call_fsync(int32_t fd) {
return fsync(fd);
}
int32_t call_flock(int32_t fd, int32_t operation) {
return flock(fd, operation);
}
int32_t call_fcntl_lock(int32_t fd, int32_t blocking, int32_t lockType, int32_t whence, int64_t start, int64_t length) {
struct flock l;
l.l_type = lockType;
l.l_whence = whence;
l.l_start = start;
l.l_len = length;
return fcntl(fd, blocking ? F_SETLKW : F_SETLK, &l);
}
int32_t get_blocking(int32_t fd) {
int flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) {
return -1;
}
return !(flags & O_NONBLOCK);
}
int32_t set_blocking(int32_t fd, int32_t blocking) {
int res = fcntl(fd, F_GETFL);
if (res >= 0) {
int flags;
if (blocking) {
flags = res & ~O_NONBLOCK;
} else {
flags = res | O_NONBLOCK;
}
res = fcntl(fd, F_SETFL, flags);
}
return res;
}
int32_t get_terminal_size(int32_t fd, int32_t *size) {
struct winsize w;
int res = ioctl(fd, TIOCGWINSZ, &w);
if (res == 0) {
size[0] = w.ws_col;
size[1] = w.ws_row;
}
return res;
}
static void stat_struct_to_longs(struct stat *st, int64_t *out) {
// TODO some of these use implementation-defined behaviour of unsigned -> signed conversion
out[0] = st->st_mode;
out[1] = st->st_ino;
out[2] = st->st_dev;
out[3] = st->st_nlink;
out[4] = st->st_uid;
out[5] = st->st_gid;
out[6] = st->st_size;
#ifdef __APPLE__
out[7] = st->st_atimespec.tv_sec;
out[8] = st->st_mtimespec.tv_sec;
out[9] = st->st_ctimespec.tv_sec;
out[10] = st->st_atimespec.tv_nsec;
out[11] = st->st_mtimespec.tv_nsec;
out[12] = st->st_ctimespec.tv_nsec;
#else
out[7] = st->st_atim.tv_sec;
out[8] = st->st_mtim.tv_sec;
out[9] = st->st_ctim.tv_sec;
out[10] = st->st_atim.tv_nsec;
out[11] = st->st_mtim.tv_nsec;
out[12] = st->st_ctim.tv_nsec;
#endif
}
int32_t call_fstatat(int32_t dirFd, const char *path, int32_t followSymlinks, int64_t *out) {
struct stat st;
int result = fstatat(dirFd, path, &st, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
if (result == 0) {
stat_struct_to_longs(&st, out);
}
return result;
}
int32_t call_fstat(int32_t fd, int64_t *out) {
struct stat st;
int result = fstat(fd, &st);
if (result == 0) {
stat_struct_to_longs(&st, out);
}
return result;
}
static void statvfs_struct_to_longs(struct statvfs *st, int64_t *out) {
// TODO some of these use implementation-defined behaviour of unsigned -> signed conversion
out[0] = st->f_bsize;
out[1] = st->f_frsize;
out[2] = st->f_blocks;
out[3] = st->f_bfree;
out[4] = st->f_bavail;
out[5] = st->f_files;
out[6] = st->f_ffree;
out[7] = st->f_favail;
out[8] = st->f_flag;
out[9] = st->f_namemax;
out[10] = st->f_fsid;
}
int32_t call_statvfs(const char *path, int64_t *out) {
struct statvfs st;
int result = statvfs(path, &st);
if (result == 0) {
statvfs_struct_to_longs(&st, out);
}
return result;
}
int32_t call_fstatvfs(int32_t fd, int64_t *out) {
struct statvfs st;
int result = fstatvfs(fd, &st);
if (result == 0) {
statvfs_struct_to_longs(&st, out);
}
return result;
}
int32_t call_uname(char *sysname, char *nodename, char *release, char *version, char *machine, int32_t size) {
struct utsname buf;
int result = uname(&buf);
if (result == 0) {
snprintf(sysname, size, "%s", buf.sysname);
snprintf(nodename, size, "%s", buf.nodename);
snprintf(release, size, "%s", buf.release);
snprintf(version, size, "%s", buf.version);
snprintf(machine, size, "%s", buf.machine);
}
return result;
}
int32_t call_unlinkat(int32_t dirFd, const char *pathname, int32_t rmdir) {
return unlinkat(dirFd, pathname, rmdir ? AT_REMOVEDIR : 0);
}
int32_t call_linkat(int32_t oldDirFd, const char *oldPath, int32_t newDirFd, const char *newPath, int32_t flags) {
return linkat(oldDirFd, oldPath, newDirFd, newPath, flags);
}
int32_t call_symlinkat(const char *target, int32_t dirFd, const char *linkpath) {
return symlinkat(target, dirFd, linkpath);
}
int32_t call_mkdirat(int32_t dirFd, const char *pathname, int32_t mode) {
return mkdirat(dirFd, pathname, mode);
}
int32_t call_getcwd(char *buf, uint64_t size) {
return getcwd(buf, size) == NULL ? -1 : 0;
}
int32_t call_chdir(const char *path) {
return chdir(path);
}
int32_t call_fchdir(int32_t fd) {
return fchdir(fd);
}
int32_t call_fchown(int32_t fd, int64_t owner, int64_t group) {
return fchown(fd, owner, group);
}
int32_t call_fchownat(int32_t dirfd, const char *pathname, int64_t owner, int64_t group, int32_t followSymlinks) {
return fchownat(dirfd, pathname, owner, group, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
}
int32_t call_isatty(int32_t fd) {
return isatty(fd);
}
intptr_t call_opendir(const char *name) {
return (intptr_t) opendir(name);
}
intptr_t call_fdopendir(int32_t fd) {
return (intptr_t) fdopendir(fd);
}
int32_t call_closedir(intptr_t dirp) {
return closedir((DIR *) dirp);
}
int32_t call_readdir(intptr_t dirp, char *nameBuf, uint64_t nameBufSize, int64_t *out) {
errno = 0;
struct dirent *dirEntry = readdir((DIR *) dirp);
if (dirEntry != NULL) {
snprintf(nameBuf, nameBufSize, "%s", dirEntry->d_name);
out[0] = dirEntry->d_ino;
out[1] = dirEntry->d_type;
return 1;
}
return 0;
}
void call_rewinddir(intptr_t dirp) {
rewinddir((DIR *) dirp);
}
#ifdef __gnu_linux__
int32_t call_utimensat(int32_t dirFd, const char *path, int64_t *timespec, int32_t followSymlinks) {
if (!timespec) {
return utimensat(dirFd, path, NULL, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
} else {
struct timespec times[2];
times[0].tv_sec = timespec[0];
times[0].tv_nsec = timespec[1];
times[1].tv_sec = timespec[2];
times[1].tv_nsec = timespec[3];
return utimensat(dirFd, path, times, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
}
}
int32_t call_futimens(int32_t fd, int64_t *timespec) {
if (!timespec) {
return futimens(fd, NULL);
} else {
struct timespec times[2];
times[0].tv_sec = timespec[0];
times[0].tv_nsec = timespec[1];
times[1].tv_sec = timespec[2];
times[1].tv_nsec = timespec[3];
return futimens(fd, times);
}
}
#endif
int32_t call_futimes(int32_t fd, int64_t *timeval) {
if (!timeval) {
return futimes(fd, NULL);
} else {
struct timeval times[2];
times[0].tv_sec = timeval[0];
times[0].tv_usec = timeval[1];
times[1].tv_sec = timeval[2];
times[1].tv_usec = timeval[3];
return futimes(fd, times);
}
}
int32_t call_lutimes(const char *filename, int64_t *timeval) {
if (!timeval) {
return lutimes(filename, NULL);
} else {
struct timeval times[2];
times[0].tv_sec = timeval[0];
times[0].tv_usec = timeval[1];
times[1].tv_sec = timeval[2];
times[1].tv_usec = timeval[3];
return lutimes(filename, times);
}
}
int32_t call_utimes(const char *filename, int64_t *timeval) {
if (!timeval) {
return utimes(filename, NULL);
} else {
struct timeval times[2];
times[0].tv_sec = timeval[0];
times[0].tv_usec = timeval[1];
times[1].tv_sec = timeval[2];
times[1].tv_usec = timeval[3];
return utimes(filename, times);
}
}
int32_t call_renameat(int32_t oldDirFd, const char *oldPath, int32_t newDirFd, const char *newPath) {
return renameat(oldDirFd, oldPath, newDirFd, newPath);
}
int32_t call_faccessat(int32_t dirFd, const char *path, int32_t mode, int32_t effectiveIds, int32_t followSymlinks) {
int flags = 0;
if (!followSymlinks) {
flags |= AT_SYMLINK_NOFOLLOW;
}
if (effectiveIds) {
flags |= AT_EACCESS;
}
return faccessat(dirFd, path, mode, flags);
}
int32_t call_fchmodat(int32_t dirFd, const char *path, int32_t mode, int32_t followSymlinks) {
return fchmodat(dirFd, path, mode, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
}
int32_t call_fchmod(int32_t fd, int32_t mode) {
return fchmod(fd, mode);
}
int64_t call_readlinkat(int32_t dirFd, const char *path, char *buf, uint64_t size) {
return readlinkat(dirFd, path, buf, size);
}
int64_t call_waitpid(int64_t pid, int32_t *status, int32_t options) {
return waitpid(pid, status, options);
}
int32_t call_wcoredump(int32_t status) {
return WCOREDUMP(status) ? 1 : 0;
}
int32_t call_wifcontinued(int32_t status) {
return WIFCONTINUED(status) ? 1 : 0;
}
int32_t call_wifstopped(int32_t status) {
return WIFSTOPPED(status) ? 1 : 0;
}
int32_t call_wifsignaled(int32_t status) {
return WIFSIGNALED(status) ? 1 : 0;
}
int32_t call_wifexited(int32_t status) {
return WIFEXITED(status) ? 1 : 0;
}
int32_t call_wexitstatus(int32_t status) {
return WEXITSTATUS(status);
}
int32_t call_wtermsig(int32_t status) {
return WTERMSIG(status);
}
int32_t call_wstopsig(int32_t status) {
return WSTOPSIG(status);
}
int32_t call_kill(int64_t pid, int32_t signal) {
return kill(pid, signal);
}
int32_t call_raise(int32_t signal) {
return raise(signal);
}
int32_t call_alarm(int32_t seconds) {
return alarm(seconds);
}
static void long_array_to_itimerval(int64_t *src, struct itimerval *dst) {
dst->it_value.tv_sec = src[0];
dst->it_value.tv_usec = src[1];
dst->it_interval.tv_sec = src[2];
dst->it_interval.tv_usec = src[3];
}
static void itimerval_to_long_array(struct itimerval *src, int64_t *dst) {
dst[0] = src->it_value.tv_sec;
dst[1] = src->it_value.tv_usec;
dst[2] = src->it_interval.tv_sec;
dst[3] = src->it_interval.tv_usec;
}
int32_t call_getitimer(int32_t which, int64_t *current_value) {
struct itimerval current;
int32_t result = getitimer(which, ¤t);
if (result == 0) {
itimerval_to_long_array(¤t, current_value);
}
return result;
}
int32_t call_setitimer(int32_t which, int64_t *new_value, int64_t *old_value) {
struct itimerval new_timer;
struct itimerval old_timer;
long_array_to_itimerval(new_value, &new_timer);
int32_t result = setitimer(which, &new_timer, &old_timer);
if (result == 0) {
itimerval_to_long_array(&old_timer, old_value);
}
return result;
}
int32_t signal_self(int32_t signal) {
switch (signal) {
case SIGABRT:
abort();
break;
case SIGSEGV:
return signal_self_segv();
default:
errno = EINVAL;
return -1;
}
_exit(128 + signal);
}
int32_t call_killpg(int64_t pgid, int32_t signal) {
return killpg(pgid, signal);
}
int64_t call_getuid() {
return getuid();
}
int64_t call_geteuid() {
return geteuid();
}
int64_t call_getgid() {
return getgid();
}
int64_t call_getegid() {
return getegid();
}
int64_t call_getppid() {
return getppid();
}
int64_t call_getpgid(int64_t pid) {
return getpgid(pid);
}
int32_t call_setpgid(int64_t pid, int64_t pgid) {
return setpgid(pid, pgid);
}
int64_t call_getpgrp() {
return getpgrp();
}
int64_t call_getsid(int64_t pid) {
return getsid(pid);
}
int64_t call_setsid() {
return setsid();
}
int32_t call_getgroups(int64_t size, int64_t* out) {
if (size > 0) {
// gid_t can be different types, we need to copy the results
gid_t* tmp = calloc(size, sizeof(gid_t));
if (!tmp) {
return -1;
}
int32_t res = getgroups(size, tmp);
for (int64_t i = 0; i < size; i++) {
out[i] = tmp[i];
}
free(tmp);
return res;
} else {
return getgroups(size, NULL);
}
}
int32_t call_getrusage(int32_t who, uint64_t* out) {
#ifndef _WIN32
struct rusage ru;
int result = getrusage(who, &ru);
if (result != 0) {
return result;
}
int offset = 0;
// POSIX prescribes only ru_utime and ru_stime members, macOS and Linux
// have (at least) all those below
# define COPYVAL(v) memcpy(&out[offset++], &v, sizeof(v))
COPYVAL(ru.ru_utime.tv_sec);
COPYVAL(ru.ru_stime.tv_sec);
COPYVAL(ru.ru_maxrss);
COPYVAL(ru.ru_ixrss);
COPYVAL(ru.ru_idrss);
COPYVAL(ru.ru_isrss);
COPYVAL(ru.ru_minflt);
COPYVAL(ru.ru_majflt);
COPYVAL(ru.ru_nswap);
COPYVAL(ru.ru_inblock);
COPYVAL(ru.ru_oublock);
COPYVAL(ru.ru_msgsnd);
COPYVAL(ru.ru_msgrcv);
COPYVAL(ru.ru_nsignals);
COPYVAL(ru.ru_nvcsw);
COPYVAL(ru.ru_nivcsw);
return 0;
# undef COPYVAL
#else
return -1;
#endif
}
int32_t call_openpty(int32_t *outvars) {
return openpty(outvars, outvars + 1, NULL, NULL, NULL);
}
int32_t call_ctermid(char *buf) {
return ctermid(buf) == NULL ? -1 : 0;
}
int32_t call_setenv(char *name, char *value, int overwrite) {
return setenv(name, value, overwrite);
}
int32_t call_unsetenv(char *name) {
return unsetenv(name);
}
// See comment in NFiPosixSupport.execv() for the description of arguments
void call_execv(char *data, int64_t *offsets, int32_t offsetsLen) {
// We reuse the memory allocated for offsets to avoid the need to allocate and reliably free another array
char **strings = (char **) offsets;
for (int32_t i = 0; i < offsetsLen; ++i) {
strings[i] = offsets[i] == -1 ? NULL : data + offsets[i];
}
char *pathname = strings[0];
char **argv = strings + 1;
execv(pathname, argv);
}
int32_t call_system(const char *pathname) {
return system(pathname);
}
int64_t call_mmap(int64_t length, int32_t prot, int32_t flags, int32_t fd, int64_t offset) {
void *result = mmap(NULL, length, prot, flags, fd, offset);
return result == MAP_FAILED ? 0 : (int64_t) result;
}
int32_t call_munmap(int64_t address, int64_t length) {
return munmap((void *) address, length);
}
void call_msync(int64_t address, int64_t offset, int64_t length) {
// TODO: can be generalized to also accept different flags,
// but MS_SYNC and such seem to be defined to different values across systems
msync(((int8_t *) address) + offset, length, MS_SYNC);
}
int32_t call_socket(int32_t family, int32_t type, int32_t protocol) {
return socket(family, type, protocol);
}
// On Java side, socket addresses are stored in a Java byte[] (here represented by a int8_t *).
// Since there are no guarantees about the alignment of this pointer, we cannot simply cast it
// to (struct sockaddr *), instead we do a copy. This shouldn't be a big deal since it is
// just 16/28 bytes (for AF_INET/AF_INET6 respectively).
int32_t call_accept(int32_t sockfd, int8_t *addr, int32_t *addr_len) {
struct sockaddr_storage sa;
socklen_t l = sizeof(sa);
int res = accept(sockfd, (struct sockaddr *) &sa, &l);
if (res >= 0) {
assert(l <= sizeof(sockaddr_storage)); // l is small enough to be representable by int32_t...
*addr_len = (int32_t)l; // ...so this unsigned->signed conversion is well defined
memcpy(addr, &sa, l);
}
return res;
}
int32_t call_bind(int32_t sockfd, int8_t *addr, int32_t addr_len) {
struct sockaddr_storage sa;
memcpy(&sa, addr, addr_len);
return bind(sockfd, (struct sockaddr *) &sa, addr_len);
}
int32_t call_connect(int32_t sockfd, int8_t *addr, int32_t addr_len) {
struct sockaddr_storage sa;
memcpy(&sa, addr, addr_len);
return connect(sockfd, (struct sockaddr *) &sa, addr_len);
}
int32_t call_listen(int32_t sockfd, int32_t backlog) {
return listen(sockfd, backlog);
}
int32_t call_getpeername(int32_t sockfd, int8_t *addr, int32_t *addr_len) {
struct sockaddr_storage sa;
socklen_t l = sizeof(sa);
int res = getpeername(sockfd, (struct sockaddr *) &sa, &l);
if (res != -1) {
assert(l <= sizeof(sockaddr_storage)); // l is small enough to be representable by int32_t...
*addr_len = (int32_t)l; // ...so this unsigned->signed conversion is well defined
memcpy(addr, &sa, l);
}
return res;
}
int32_t call_getsockname(int32_t sockfd, int8_t *addr, int32_t *addr_len) {
struct sockaddr_storage sa;
socklen_t l = sizeof(sa);
int res = getsockname(sockfd, (struct sockaddr *) &sa, &l);
if (res != -1) {
assert(l <= sizeof(sockaddr_storage)); // l is small enough to be representable by int32_t...
*addr_len = (int32_t)l; // ...so this unsigned->signed conversion is well defined
memcpy(addr, &sa, l);
}
return res;
}
//TODO len should be size_t, retval should be ssize_t
int32_t call_send(int32_t sockfd, void *buf, int32_t offset, int32_t len, int32_t flags) {
return send(sockfd, buf + offset, len, flags);
}
int32_t call_sendto(int32_t sockfd, void *buf, int32_t offset, int32_t len, int32_t flags, int8_t *addr, int32_t addr_len) {
struct sockaddr_storage sa;
memcpy(&sa, addr, addr_len);
return sendto(sockfd, buf + offset, len, flags, (struct sockaddr *) &sa, addr_len);
}
int32_t call_recv(int32_t sockfd, void *buf, int32_t offset, int32_t len, int32_t flags) {
return recv(sockfd, buf + offset, len, flags);
}
int32_t call_recvfrom(int32_t sockfd, void *buf, int32_t offset, int32_t len, int32_t flags, int8_t *src_addr, int32_t *addr_len) {
struct sockaddr_storage sa;
socklen_t l = sizeof(sa);
int res = recvfrom(sockfd, buf + offset, len, flags, (struct sockaddr *) &sa, &l);
if (res != -1) {
assert(l <= sizeof(sockaddr_storage)); // l is small enough to be representable by int32_t...
*addr_len = (int32_t)l; // ...so this unsigned->signed conversion is well defined
memcpy(src_addr, &sa, l);
}
return res;
}
int32_t call_shutdown(int32_t sockfd, int32_t how) {
return shutdown(sockfd, how);
}
#define MAX_SOCKOPT_LEN 1024
int32_t call_getsockopt(int32_t sockfd, int32_t level, int32_t optname, void *buf, int32_t *bufLen) {
// We don't know anything about the alignment of the buf pointer, neither we know what alignment
// is expected by getsockopt, since that depends on the actual value of level/optname. Thus we
// need to make a copy to a buffer that is aligned for any data type. We could use malloc for
// this, but most options are just 4 bytes. Or we could use alloca, but I can't find any
// documentation of alignment guarantees, its use is discouraged and there is no way of detecting
// stack overflow, so we'd have to put some arbitrary limit to bufLen anyway - in which case
// a properly aligned, stack-allocated buffer of a fixed size should work fine.
// The limit of 1024 is inspired by the implementation of CPython's sock_getsockopt.
char alignedBuf[MAX_SOCKOPT_LEN] __attribute__ ((aligned));
socklen_t len = *bufLen;
if (len > sizeof(alignedBuf)) {
// If this ever happens, we can increase MAX_SOCKOPT_LEN or use malloc.
errno = ENOMEM;
return -1;
}
int res = getsockopt(sockfd, level, optname, alignedBuf, &len);
if (res == 0) {
*bufLen = len;
memcpy(buf, alignedBuf, len);
}
return res;
}
int32_t call_setsockopt(int32_t sockfd, int32_t level, int32_t optname, void *buf, int32_t bufLen) {
// see comments in call_getsockopt
char alignedBuf[MAX_SOCKOPT_LEN] __attribute__ ((aligned));
if (bufLen > sizeof(alignedBuf)) {
errno = ENOMEM;
return -1;
}
memcpy(alignedBuf, buf, bufLen);
return setsockopt(sockfd, level, optname, alignedBuf, bufLen);
}
int32_t call_inet_addr(const char *src) {
return ntohl(inet_addr(src));
}
int64_t call_inet_aton(const char *src) {
struct in_addr addr;
int r = inet_aton(src, &addr);
if (r != 1) {
return -1;
}
return ntohl(addr.s_addr) & 0xFFFFFFFF;
}
int32_t call_inet_ntoa(int32_t src, char *dst) {
struct in_addr addr;
addr.s_addr = htonl(src);
const char *s = inet_ntoa(addr);
size_t len = strlen(s);
assert(len <= INET_ADDRSTRLEN - 1);
memcpy(dst, s, len);
return len;
}
int32_t call_inet_pton(int32_t family, const char *src, void *dst) {
return inet_pton(family, src, dst);
}
int32_t call_inet_ntop(int32_t family, void *src, char *dst, int32_t dstSize) {
const char *r = inet_ntop(family, src, dst, dstSize);
return r == NULL ? -1 : 0;
}
int32_t call_gethostname(char *buf, int64_t bufLen) {
return gethostname(buf, bufLen);
}
int32_t call_getnameinfo(int8_t *addr, int32_t addr_len, char *hostBuf, int32_t hostBufLen, char *servBuf, int32_t servBufLen, int32_t flags) {
struct sockaddr_storage sa;
memcpy(&sa, addr, addr_len);
return getnameinfo((struct sockaddr *) &sa, addr_len, hostBuf, hostBufLen, servBuf, servBufLen, flags);
}
int32_t call_getaddrinfo(const char *node, const char *service, int32_t family, int32_t sockType, int32_t protocol, int32_t flags, int64_t *ptr) {
struct addrinfo hints;
struct addrinfo *res;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = flags;
hints.ai_family = family;
hints.ai_socktype = sockType;
hints.ai_protocol = protocol;
int ret = getaddrinfo(node, service, &hints, &res);
if (ret == 0) {
*ptr = (int64_t) res;
}
return ret;
}
void call_freeaddrinfo(int64_t ptr) {
freeaddrinfo((struct addrinfo *) ptr);
}
void call_gai_strerror(int32_t error, char *buf, int32_t buflen) {
snprintf(buf, buflen, "%s", gai_strerror(error));
}