-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
library_syscall.js
1500 lines (1449 loc) · 52 KB
/
library_syscall.js
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
/**
* @license
* Copyright 2015 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
var SyscallsLibrary = {
$SYSCALLS__deps: [
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
'$PATH',
'$FS',
#endif
#if SYSCALL_DEBUG
'$ERRNO_MESSAGES'
#endif
],
$SYSCALLS: {
mappings: {},
#if SYSCALLS_REQUIRE_FILESYSTEM
// global constants
DEFAULT_POLLMASK: {{{ cDefine('POLLIN') }}} | {{{ cDefine('POLLOUT') }}},
// global state
umask: 0x1FF, // S_IRWXU | S_IRWXG | S_IRWXO
// shared utilities
calculateAt: function(dirfd, path, allowEmpty) {
if (path[0] === '/') {
return path;
}
// relative path
var dir;
if (dirfd === {{{ cDefine('AT_FDCWD') }}}) {
dir = FS.cwd();
} else {
var dirstream = FS.getStream(dirfd);
if (!dirstream) throw new FS.ErrnoError({{{ cDefine('EBADF') }}});
dir = dirstream.path;
}
if (path.length == 0) {
if (!allowEmpty) {
throw new FS.ErrnoError({{{ cDefine('ENOENT') }}});;
}
return dir;
}
return PATH.join2(dir, path);
},
doStat: function(func, path, buf) {
try {
var stat = func(path);
} catch (e) {
if (e && e.node && PATH.normalize(path) !== PATH.normalize(FS.getPath(e.node))) {
// an error occurred while trying to look up the path; we should just report ENOTDIR
return -{{{ cDefine('ENOTDIR') }}};
}
throw e;
}
{{{ makeSetValue('buf', C_STRUCTS.stat.st_dev, 'stat.dev', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.__st_dev_padding, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.__st_ino_truncated, 'stat.ino', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mode, 'stat.mode', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_nlink, 'stat.nlink', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_uid, 'stat.uid', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_gid, 'stat.gid', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_rdev, 'stat.rdev', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.__st_rdev_padding, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_size, 'stat.size', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_blksize, '4096', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_blocks, 'stat.blocks', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_sec, '(stat.atime.getTime() / 1000)|0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_nsec, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_sec, '(stat.mtime.getTime() / 1000)|0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_nsec, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, '(stat.ctime.getTime() / 1000)|0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_nsec, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'i64') }}};
return 0;
},
doMsync: function(addr, stream, len, flags, offset) {
var buffer = HEAPU8.slice(addr, addr + len);
FS.msync(stream, buffer, offset, len, flags);
},
doMkdir: function(path, mode) {
// remove a trailing slash, if one - /a/b/ has basename of '', but
// we want to create b in the context of this function
path = PATH.normalize(path);
if (path[path.length-1] === '/') path = path.substr(0, path.length-1);
FS.mkdir(path, mode, 0);
return 0;
},
doMknod: function(path, mode, dev) {
// we don't want this in the JS API as it uses mknod to create all nodes.
switch (mode & {{{ cDefine('S_IFMT') }}}) {
case {{{ cDefine('S_IFREG') }}}:
case {{{ cDefine('S_IFCHR') }}}:
case {{{ cDefine('S_IFBLK') }}}:
case {{{ cDefine('S_IFIFO') }}}:
case {{{ cDefine('S_IFSOCK') }}}:
break;
default: return -{{{ cDefine('EINVAL') }}};
}
FS.mknod(path, mode, dev);
return 0;
},
doReadlink: function(path, buf, bufsize) {
if (bufsize <= 0) return -{{{ cDefine('EINVAL') }}};
var ret = FS.readlink(path);
var len = Math.min(bufsize, lengthBytesUTF8(ret));
var endChar = HEAP8[buf+len];
stringToUTF8(ret, buf, bufsize+1);
// readlink is one of the rare functions that write out a C string, but does never append a null to the output buffer(!)
// stringToUTF8() always appends a null byte, so restore the character under the null byte after the write.
HEAP8[buf+len] = endChar;
return len;
},
doAccess: function(path, amode) {
if (amode & ~{{{ cDefine('S_IRWXO') }}}) {
// need a valid mode
return -{{{ cDefine('EINVAL') }}};
}
var node;
var lookup = FS.lookupPath(path, { follow: true });
node = lookup.node;
if (!node) {
return -{{{ cDefine('ENOENT') }}};
}
var perms = '';
if (amode & {{{ cDefine('R_OK') }}}) perms += 'r';
if (amode & {{{ cDefine('W_OK') }}}) perms += 'w';
if (amode & {{{ cDefine('X_OK') }}}) perms += 'x';
if (perms /* otherwise, they've just passed F_OK */ && FS.nodePermissions(node, perms)) {
return -{{{ cDefine('EACCES') }}};
}
return 0;
},
doDup: function(path, flags, suggestFD) {
var suggest = FS.getStream(suggestFD);
if (suggest) FS.close(suggest);
return FS.open(path, flags, 0, suggestFD, suggestFD).fd;
},
doReadv: function(stream, iov, iovcnt, offset) {
var ret = 0;
for (var i = 0; i < iovcnt; i++) {
var ptr = {{{ makeGetValue('iov', 'i*8', 'i32') }}};
var len = {{{ makeGetValue('iov', 'i*8 + 4', 'i32') }}};
var curr = FS.read(stream, {{{ heapAndOffset('HEAP8', 'ptr') }}}, len, offset);
if (curr < 0) return -1;
ret += curr;
if (curr < len) break; // nothing more to read
}
return ret;
},
doWritev: function(stream, iov, iovcnt, offset) {
var ret = 0;
for (var i = 0; i < iovcnt; i++) {
var ptr = {{{ makeGetValue('iov', 'i*8', 'i32') }}};
var len = {{{ makeGetValue('iov', 'i*8 + 4', 'i32') }}};
var curr = FS.write(stream, {{{ heapAndOffset('HEAP8', 'ptr') }}}, len, offset);
if (curr < 0) return -1;
ret += curr;
}
return ret;
},
#else
// MEMFS filesystem disabled lite handling of stdout and stderr:
buffers: [null, [], []], // 1 => stdout, 2 => stderr
printChar: function(stream, curr) {
var buffer = SYSCALLS.buffers[stream];
#if ASSERTIONS
assert(buffer);
#endif
if (curr === 0 || curr === {{{ charCode('\n') }}}) {
(stream === 1 ? out : err)(UTF8ArrayToString(buffer, 0));
buffer.length = 0;
} else {
buffer.push(curr);
}
},
#endif // SYSCALLS_REQUIRE_FILESYSTEM
// arguments handling
varargs: undefined,
get: function() {
#if ASSERTIONS
assert(SYSCALLS.varargs != undefined);
#endif
SYSCALLS.varargs += 4;
var ret = {{{ makeGetValue('SYSCALLS.varargs', '-4', 'i32') }}};
#if SYSCALL_DEBUG
err(' (raw: "' + ret + '")');
#endif
return ret;
},
getStr: function(ptr) {
var ret = UTF8ToString(ptr);
#if SYSCALL_DEBUG
err(' (str: "' + ret + '")');
#endif
return ret;
},
#if SYSCALLS_REQUIRE_FILESYSTEM
getStreamFromFD: function(fd) {
var stream = FS.getStream(fd);
if (!stream) throw new FS.ErrnoError({{{ cDefine('EBADF') }}});
#if SYSCALL_DEBUG
err(' (stream: "' + stream.path + '")');
#endif
return stream;
},
#endif // SYSCALLS_REQUIRE_FILESYSTEM
get64: function(low, high) {
#if ASSERTIONS
if (low >= 0) assert(high === 0);
else assert(high === -1);
#endif
#if SYSCALL_DEBUG
err(' (i64: "' + low + '")');
#endif
return low;
}
},
$syscallMmap2__deps: ['$SYSCALLS', '$zeroMemory', '$mmapAlloc',
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
'$FS',
#endif
],
$syscallMmap2: function(addr, len, prot, flags, fd, off) {
off <<= 12; // undo pgoffset
var ptr;
var allocated = false;
// addr argument must be page aligned if MAP_FIXED flag is set.
if ((flags & {{{ cDefine('MAP_FIXED') }}}) !== 0 && (addr % {{{ WASM_PAGE_SIZE }}}) !== 0) {
return -{{{ cDefine('EINVAL') }}};
}
// MAP_ANONYMOUS (aka MAP_ANON) isn't actually defined by POSIX spec,
// but it is widely used way to allocate memory pages on Linux, BSD and Mac.
// In this case fd argument is ignored.
if ((flags & {{{ cDefine('MAP_ANONYMOUS') }}}) !== 0) {
ptr = mmapAlloc(len);
if (!ptr) return -{{{ cDefine('ENOMEM') }}};
allocated = true;
} else {
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
var info = FS.getStream(fd);
if (!info) return -{{{ cDefine('EBADF') }}};
var res = FS.mmap(info, addr, len, off, prot, flags);
ptr = res.ptr;
allocated = res.allocated;
#else // no filesystem support; report lack of support
return -{{{ cDefine('ENOSYS') }}};
#endif
}
#if CAN_ADDRESS_2GB
ptr >>>= 0;
#endif
SYSCALLS.mappings[ptr] = { malloc: ptr, len: len, allocated: allocated, fd: fd, prot: prot, flags: flags, offset: off };
return ptr;
},
$syscallMunmap__deps: ['$SYSCALLS',
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
'$FS',
#endif
],
$syscallMunmap: function(addr, len) {
#if CAN_ADDRESS_2GB
addr >>>= 0;
#endif
// TODO: support unmmap'ing parts of allocations
var info = SYSCALLS.mappings[addr];
if (len === 0 || !info) {
return -{{{ cDefine('EINVAL') }}};
}
if (len === info.len) {
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
var stream = FS.getStream(info.fd);
if (stream) {
if (info.prot & {{{ cDefine('PROT_WRITE') }}}) {
SYSCALLS.doMsync(addr, stream, len, info.flags, info.offset);
}
FS.munmap(stream);
}
#else
#if ASSERTIONS
// Without FS support, only anonymous mappings are supported.
assert(SYSCALLS.mappings[addr].flags & {{{ cDefine('MAP_ANONYMOUS') }}});
#endif
#endif
SYSCALLS.mappings[addr] = null;
if (info.allocated) {
_free(info.malloc);
}
}
return 0;
},
__sys_open: function(path, flags, varargs) {
var pathname = SYSCALLS.getStr(path);
var mode = varargs ? SYSCALLS.get() : 0;
var stream = FS.open(pathname, flags, mode);
return stream.fd;
},
__sys_link__nothrow: true,
__sys_link__proxy: false,
__sys_link: function(oldpath, newpath) {
return -{{{ cDefine('EMLINK') }}}; // no hardlinks for us
},
__sys_unlink: function(path) {
path = SYSCALLS.getStr(path);
FS.unlink(path);
return 0;
},
__sys_chdir: function(path) {
path = SYSCALLS.getStr(path);
FS.chdir(path);
return 0;
},
__sys_mknod: function(path, mode, dev) {
path = SYSCALLS.getStr(path);
return SYSCALLS.doMknod(path, mode, dev);
},
__sys_chmod: function(path, mode) {
path = SYSCALLS.getStr(path);
FS.chmod(path, mode);
return 0;
},
__sys_getpid__nothrow: true,
__sys_getpid__proxy: false,
__sys_getpid: function() {
return {{{ PROCINFO.pid }}};
},
__sys_pause__nothrow: true,
__sys_pause__proxy: false,
__sys_pause: function() {
return -{{{ cDefine('EINTR') }}}; // we can't pause
},
__sys_access: function(path, amode) {
path = SYSCALLS.getStr(path);
return SYSCALLS.doAccess(path, amode);
},
__sys_nice__nothrow: true,
__sys_nice__proxy: false,
__sys_nice: function(inc) {
return -{{{ cDefine('EPERM') }}}; // no meaning to nice for our single-process environment
},
__sys_sync__proxy: false,
__sys_sync__nothrow: true,
__sys_sync: function() {
return 0;
},
__sys_rename: function(old_path, new_path) {
old_path = SYSCALLS.getStr(old_path);
new_path = SYSCALLS.getStr(new_path);
FS.rename(old_path, new_path);
return 0;
},
__sys_mkdir: function(path, mode) {
path = SYSCALLS.getStr(path);
return SYSCALLS.doMkdir(path, mode);
},
__sys_rmdir: function(path) {
path = SYSCALLS.getStr(path);
FS.rmdir(path);
return 0;
},
__sys_dup: function(fd) {
var old = SYSCALLS.getStreamFromFD(fd);
return FS.open(old.path, old.flags, 0).fd;
},
__sys_pipe__deps: ['$PIPEFS'],
__sys_pipe: function(fdPtr) {
if (fdPtr == 0) {
throw new FS.ErrnoError({{{ cDefine('EFAULT') }}});
}
var res = PIPEFS.createPipe();
{{{ makeSetValue('fdPtr', 0, 'res.readable_fd', 'i32') }}};
{{{ makeSetValue('fdPtr', 4, 'res.writable_fd', 'i32') }}};
return 0;
},
__sys_ioctl: function(fd, op, varargs) {
#if SYSCALLS_REQUIRE_FILESYSTEM == 0
#if SYSCALL_DEBUG
err('no-op in ioctl syscall due to SYSCALLS_REQUIRE_FILESYSTEM=0');
#endif
return 0;
#else
var stream = SYSCALLS.getStreamFromFD(fd);
switch (op) {
case {{{ cDefine('TCGETA') }}}:
case {{{ cDefine('TCGETS') }}}: {
if (!stream.tty) return -{{{ cDefine('ENOTTY') }}};
#if SYSCALL_DEBUG
err('warning: not filling tio struct');
#endif
return 0;
}
case {{{ cDefine('TCSETA') }}}:
case {{{ cDefine('TCSETAW') }}}:
case {{{ cDefine('TCSETAF') }}}:
case {{{ cDefine('TCSETS') }}}:
case {{{ cDefine('TCSETSW') }}}:
case {{{ cDefine('TCSETSF') }}}: {
if (!stream.tty) return -{{{ cDefine('ENOTTY') }}};
return 0; // no-op, not actually adjusting terminal settings
}
case {{{ cDefine('TIOCGPGRP') }}}: {
if (!stream.tty) return -{{{ cDefine('ENOTTY') }}};
var argp = SYSCALLS.get();
{{{ makeSetValue('argp', 0, 0, 'i32') }}};
return 0;
}
case {{{ cDefine('TIOCSPGRP') }}}: {
if (!stream.tty) return -{{{ cDefine('ENOTTY') }}};
return -{{{ cDefine('EINVAL') }}}; // not supported
}
case {{{ cDefine('FIONREAD') }}}: {
var argp = SYSCALLS.get();
return FS.ioctl(stream, op, argp);
}
case {{{ cDefine('TIOCGWINSZ') }}}: {
// TODO: in theory we should write to the winsize struct that gets
// passed in, but for now musl doesn't read anything on it
if (!stream.tty) return -{{{ cDefine('ENOTTY') }}};
return 0;
}
case {{{ cDefine('TIOCSWINSZ') }}}: {
// TODO: technically, this ioctl call should change the window size.
// but, since emscripten doesn't have any concept of a terminal window
// yet, we'll just silently throw it away as we do TIOCGWINSZ
if (!stream.tty) return -{{{ cDefine('ENOTTY') }}};
return 0;
}
default: abort('bad ioctl syscall ' + op);
}
#endif // SYSCALLS_REQUIRE_FILESYSTEM
},
__sys_setpgid__nothrow: true,
__sys_setpgid__proxy: false,
__sys_setpgid: function(pid, pgid) {
if (pid && pid !== {{{ PROCINFO.pid }}}) return -{{{ cDefine('ESRCH') }}};
if (pgid && pgid !== {{{ PROCINFO.pgid }}}) return -{{{ cDefine('EPERM') }}};
return 0;
},
__sys_umask: function(mask) {
var old = SYSCALLS.umask;
SYSCALLS.umask = mask;
return old;
},
__sys_dup2: function(oldfd, suggestFD) {
var old = SYSCALLS.getStreamFromFD(oldfd);
if (old.fd === suggestFD) return suggestFD;
return SYSCALLS.doDup(old.path, old.flags, suggestFD);
},
__sys_getppid__nothrow: true,
__sys_getppid__proxy: false,
__sys_getppid: function() {
return {{{ PROCINFO.ppid }}};
},
__sys_getpgrp__nothrow: true,
__sys_getpgrp__proxy: false,
__sys_getpgrp: function() {
return {{{ PROCINFO.pgid }}};
},
__sys_setsid__nothrow: true,
__sys_setsid__proxy: false,
__sys_setsid: function() {
return 0; // no-op
},
__sys_setrlimit__nothrow: true,
__sys_setrlimit__proxy: false,
__sys_setrlimit: function(varargs) {
return 0; // no-op
},
__sys_getrusage__deps: ['$zeroMemory'],
__sys_getrusage: function(who, usage) {
zeroMemory(usage, {{{ C_STRUCTS.rusage.__size__ }}});
{{{ makeSetValue('usage', C_STRUCTS.rusage.ru_utime.tv_sec, '1', 'i32') }}}; // fake some values
{{{ makeSetValue('usage', C_STRUCTS.rusage.ru_utime.tv_usec, '2', 'i32') }}};
{{{ makeSetValue('usage', C_STRUCTS.rusage.ru_stime.tv_sec, '3', 'i32') }}};
{{{ makeSetValue('usage', C_STRUCTS.rusage.ru_stime.tv_usec, '4', 'i32') }}};
return 0;
},
__sys_symlink: function(target, linkpath) {
target = SYSCALLS.getStr(target);
linkpath = SYSCALLS.getStr(linkpath);
FS.symlink(target, linkpath);
return 0;
},
__sys_readlink: function(path, buf, bufsize) {
path = SYSCALLS.getStr(path);
return SYSCALLS.doReadlink(path, buf, bufsize);
},
__sys_munmap__deps: ['$syscallMunmap'],
__sys_munmap: function(addr, len) {
return syscallMunmap(addr, len);
},
__sys_fchmod: function(fd, mode) {
FS.fchmod(fd, mode);
return 0;
},
__sys_getpriority__nothrow: true,
__sys_getpriority__proxy: false,
__sys_getpriority: function() {
return 0;
},
__sys_setpriority__nothrow: true,
__sys_setpriority__proxy: false,
__sys_setpriority: function() {
return -{{{ cDefine('EPERM') }}};
},
#if PROXY_POSIX_SOCKETS == 0
$getSocketFromFD__deps: ['$SOCKFS', '$FS'],
$getSocketFromFD: function(fd) {
var socket = SOCKFS.getSocket(fd);
if (!socket) throw new FS.ErrnoError({{{ cDefine('EBADF') }}});
#if SYSCALL_DEBUG
err(' (socket: "' + socket.path + '")');
#endif
return socket;
},
/** @param {boolean=} allowNull */
$getSocketAddress__deps: ['$readSockaddr'],
$getSocketAddress: function(addrp, addrlen, allowNull) {
if (allowNull && addrp === 0) return null;
var info = readSockaddr(addrp, addrlen);
if (info.errno) throw new FS.ErrnoError(info.errno);
info.addr = DNS.lookup_addr(info.addr) || info.addr;
#if SYSCALL_DEBUG
err(' (socketaddress: "' + [info.addr, info.port] + '")');
#endif
return info;
},
__sys_socket__deps: ['$SOCKFS'],
__sys_socket: function(domain, type, protocol) {
var sock = SOCKFS.createSocket(domain, type, protocol);
#if ASSERTIONS
assert(sock.stream.fd < 64); // XXX ? select() assumes socket fd values are in 0..63
#endif
return sock.stream.fd;
},
__sys_getsockname__deps: ['$getSocketFromFD', '$writeSockaddr', '$DNS'],
__sys_getsockname: function(fd, addr, addrlen) {
err("__sys_getsockname " + fd);
var sock = getSocketFromFD(fd);
// TODO: sock.saddr should never be undefined, see TODO in websocket_sock_ops.getname
var errno = writeSockaddr(addr, sock.family, DNS.lookup_name(sock.saddr || '0.0.0.0'), sock.sport, addrlen);
#if ASSERTIONS
assert(!errno);
#endif
return 0;
},
__sys_setsockopt: function(fd) {
return -{{{ cDefine('ENOPROTOOPT') }}}; // The option is unknown at the level indicated.
},
__sys_getpeername__deps: ['$getSocketFromFD', '$writeSockaddr', '$DNS'],
__sys_getpeername: function(fd, addr, addrlen) {
var sock = getSocketFromFD(fd);
if (!sock.daddr) {
return -{{{ cDefine('ENOTCONN') }}}; // The socket is not connected.
}
var errno = writeSockaddr(addr, sock.family, DNS.lookup_name(sock.daddr), sock.dport, addrlen);
#if ASSERTIONS
assert(!errno);
#endif
return 0;
},
__sys_connect__deps: ['$getSocketFromFD', '$getSocketAddress'],
__sys_connect: function(fd, addr, addrlen) {
var sock = getSocketFromFD(fd);
var info = getSocketAddress(addr, addrlen);
sock.sock_ops.connect(sock, info.addr, info.port);
return 0;
},
__sys_shutdown__deps: ['$getSocketFromFD'],
__sys_shutdown: function(fd, how) {
getSocketFromFD(fd);
return -{{{ cDefine('ENOSYS') }}}; // unsupported feature
},
__sys_accept4__deps: ['$getSocketFromFD', '$writeSockaddr', '$DNS'],
__sys_accept4: function(fd, addr, addrlen, flags) {
var sock = getSocketFromFD(fd);
var newsock = sock.sock_ops.accept(sock);
if (addr) {
var errno = writeSockaddr(addr, newsock.family, DNS.lookup_name(newsock.daddr), newsock.dport, addrlen);
#if ASSERTIONS
assert(!errno);
#endif
}
return newsock.stream.fd;
},
__sys_bind__deps: ['$getSocketFromFD', '$getSocketAddress'],
__sys_bind: function(fd, addr, addrlen) {
var sock = getSocketFromFD(fd);
var info = getSocketAddress(addr, addrlen);
sock.sock_ops.bind(sock, info.addr, info.port);
return 0;
},
__sys_listen__deps: ['$getSocketFromFD'],
__sys_listen: function(fd, backlog) {
var sock = getSocketFromFD(fd);
sock.sock_ops.listen(sock, backlog);
return 0;
},
__sys_recvfrom__deps: ['$getSocketFromFD', '$writeSockaddr', '$DNS'],
__sys_recvfrom: function(fd, buf, len, flags, addr, addrlen) {
var sock = getSocketFromFD(fd);
var msg = sock.sock_ops.recvmsg(sock, len);
if (!msg) return 0; // socket is closed
if (addr) {
var errno = writeSockaddr(addr, sock.family, DNS.lookup_name(msg.addr), msg.port, addrlen);
#if ASSERTIONS
assert(!errno);
#endif
}
HEAPU8.set(msg.buffer, buf);
return msg.buffer.byteLength;
},
__sys_sendto__deps: ['$getSocketFromFD', '$getSocketAddress'],
__sys_sendto: function(fd, message, length, flags, addr, addr_len) {
var sock = getSocketFromFD(fd);
var dest = getSocketAddress(addr, addr_len, true);
if (!dest) {
// send, no address provided
return FS.write(sock.stream, {{{ heapAndOffset('HEAP8', 'message') }}}, length);
} else {
// sendto an address
return sock.sock_ops.sendmsg(sock, {{{ heapAndOffset('HEAP8', 'message') }}}, length, dest.addr, dest.port);
}
},
__sys_getsockopt__deps: ['$getSocketFromFD'],
__sys_getsockopt: function(fd, level, optname, optval, optlen) {
var sock = getSocketFromFD(fd);
// Minimal getsockopt aimed at resolving https://github.com/emscripten-core/emscripten/issues/2211
// so only supports SOL_SOCKET with SO_ERROR.
if (level === {{{ cDefine('SOL_SOCKET') }}}) {
if (optname === {{{ cDefine('SO_ERROR') }}}) {
{{{ makeSetValue('optval', 0, 'sock.error', 'i32') }}};
{{{ makeSetValue('optlen', 0, 4, 'i32') }}};
sock.error = null; // Clear the error (The SO_ERROR option obtains and then clears this field).
return 0;
}
}
return -{{{ cDefine('ENOPROTOOPT') }}}; // The option is unknown at the level indicated.
},
__sys_sendmsg__deps: ['$getSocketFromFD', '$readSockaddr', '$DNS'],
__sys_sendmsg: function(fd, message, flags) {
var sock = getSocketFromFD(fd);
var iov = {{{ makeGetValue('message', C_STRUCTS.msghdr.msg_iov, '*') }}};
var num = {{{ makeGetValue('message', C_STRUCTS.msghdr.msg_iovlen, 'i32') }}};
// read the address and port to send to
var addr, port;
var name = {{{ makeGetValue('message', C_STRUCTS.msghdr.msg_name, '*') }}};
var namelen = {{{ makeGetValue('message', C_STRUCTS.msghdr.msg_namelen, 'i32') }}};
if (name) {
var info = readSockaddr(name, namelen);
if (info.errno) return -info.errno;
port = info.port;
addr = DNS.lookup_addr(info.addr) || info.addr;
}
// concatenate scatter-gather arrays into one message buffer
var total = 0;
for (var i = 0; i < num; i++) {
total += {{{ makeGetValue('iov', '(' + C_STRUCTS.iovec.__size__ + ' * i) + ' + C_STRUCTS.iovec.iov_len, 'i32') }}};
}
var view = new Uint8Array(total);
var offset = 0;
for (var i = 0; i < num; i++) {
var iovbase = {{{ makeGetValue('iov', '(' + C_STRUCTS.iovec.__size__ + ' * i) + ' + C_STRUCTS.iovec.iov_base, 'i8*') }}};
var iovlen = {{{ makeGetValue('iov', '(' + C_STRUCTS.iovec.__size__ + ' * i) + ' + C_STRUCTS.iovec.iov_len, 'i32') }}};
for (var j = 0; j < iovlen; j++) {
view[offset++] = {{{ makeGetValue('iovbase', 'j', 'i8') }}};
}
}
// write the buffer
return sock.sock_ops.sendmsg(sock, view, 0, total, addr, port);
},
__sys_recvmsg__deps: ['$getSocketFromFD', '$writeSockaddr', '$DNS'],
__sys_recvmsg: function(fd, message, flags) {
var sock = getSocketFromFD(fd);
var iov = {{{ makeGetValue('message', C_STRUCTS.msghdr.msg_iov, 'i8*') }}};
var num = {{{ makeGetValue('message', C_STRUCTS.msghdr.msg_iovlen, 'i32') }}};
// get the total amount of data we can read across all arrays
var total = 0;
for (var i = 0; i < num; i++) {
total += {{{ makeGetValue('iov', '(' + C_STRUCTS.iovec.__size__ + ' * i) + ' + C_STRUCTS.iovec.iov_len, 'i32') }}};
}
// try to read total data
var msg = sock.sock_ops.recvmsg(sock, total);
if (!msg) return 0; // socket is closed
// TODO honor flags:
// MSG_OOB
// Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific.
// MSG_PEEK
// Peeks at the incoming message.
// MSG_WAITALL
// Requests that the function block until the full amount of data requested can be returned. The function may return a smaller amount of data if a signal is caught, if the connection is terminated, if MSG_PEEK was specified, or if an error is pending for the socket.
// write the source address out
var name = {{{ makeGetValue('message', C_STRUCTS.msghdr.msg_name, '*') }}};
if (name) {
var errno = writeSockaddr(name, sock.family, DNS.lookup_name(msg.addr), msg.port);
#if ASSERTIONS
assert(!errno);
#endif
}
// write the buffer out to the scatter-gather arrays
var bytesRead = 0;
var bytesRemaining = msg.buffer.byteLength;
for (var i = 0; bytesRemaining > 0 && i < num; i++) {
var iovbase = {{{ makeGetValue('iov', '(' + C_STRUCTS.iovec.__size__ + ' * i) + ' + C_STRUCTS.iovec.iov_base, 'i8*') }}};
var iovlen = {{{ makeGetValue('iov', '(' + C_STRUCTS.iovec.__size__ + ' * i) + ' + C_STRUCTS.iovec.iov_len, 'i32') }}};
if (!iovlen) {
continue;
}
var length = Math.min(iovlen, bytesRemaining);
var buf = msg.buffer.subarray(bytesRead, bytesRead + length);
HEAPU8.set(buf, iovbase + bytesRead);
bytesRead += length;
bytesRemaining -= length;
}
// TODO set msghdr.msg_flags
// MSG_EOR
// End of record was received (if supported by the protocol).
// MSG_OOB
// Out-of-band data was received.
// MSG_TRUNC
// Normal data was truncated.
// MSG_CTRUNC
return bytesRead;
},
#endif // ~PROXY_POSIX_SOCKETS==0
__sys_setdomainname__nothrow: true,
__sys_setdomainname__proxy: false,
__sys_setdomainname: function(name, size) {
return -{{{ cDefine('EPERM') }}};
},
#if MINIMAL_RUNTIME
__sys_uname__deps: ['$writeAsciiToMemory'],
#endif
__sys_uname__proxy: false,
__sys_uname: function(buf) {
if (!buf) return -{{{ cDefine('EFAULT') }}}
var layout = {{{ JSON.stringify(C_STRUCTS.utsname) }}};
var copyString = function(element, value) {
var offset = layout[element];
writeAsciiToMemory(value, buf + offset);
};
copyString('sysname', 'Emscripten');
copyString('nodename', 'emscripten');
copyString('release', '1.0');
copyString('version', '#1');
#if MEMORY64 == 1
copyString('machine', 'wasm64');
#else
copyString('machine', 'wasm32');
#endif
return 0;
},
__sys_mprotect: function(addr, len, size) {
return 0; // let's not and say we did
},
__sys_getpgid__nothrow: true,
__sys_getpgid__proxy: false,
__sys_getpgid: function(pid) {
if (pid && pid !== {{{ PROCINFO.pid }}}) return -{{{ cDefine('ESRCH') }}};
return {{{ PROCINFO.pgid }}};
},
__sys_fchdir: function(fd) {
var stream = SYSCALLS.getStreamFromFD(fd);
FS.chdir(stream.path);
return 0;
},
__sys__newselect: function(nfds, readfds, writefds, exceptfds, timeout) {
// readfds are supported,
// writefds checks socket open status
// exceptfds not supported
// timeout is always 0 - fully async
#if ASSERTIONS
assert(nfds <= 64, 'nfds must be less than or equal to 64'); // fd sets have 64 bits // TODO: this could be 1024 based on current musl headers
assert(!exceptfds, 'exceptfds not supported');
#endif
var total = 0;
var srcReadLow = (readfds ? {{{ makeGetValue('readfds', 0, 'i32') }}} : 0),
srcReadHigh = (readfds ? {{{ makeGetValue('readfds', 4, 'i32') }}} : 0);
var srcWriteLow = (writefds ? {{{ makeGetValue('writefds', 0, 'i32') }}} : 0),
srcWriteHigh = (writefds ? {{{ makeGetValue('writefds', 4, 'i32') }}} : 0);
var srcExceptLow = (exceptfds ? {{{ makeGetValue('exceptfds', 0, 'i32') }}} : 0),
srcExceptHigh = (exceptfds ? {{{ makeGetValue('exceptfds', 4, 'i32') }}} : 0);
var dstReadLow = 0,
dstReadHigh = 0;
var dstWriteLow = 0,
dstWriteHigh = 0;
var dstExceptLow = 0,
dstExceptHigh = 0;
var allLow = (readfds ? {{{ makeGetValue('readfds', 0, 'i32') }}} : 0) |
(writefds ? {{{ makeGetValue('writefds', 0, 'i32') }}} : 0) |
(exceptfds ? {{{ makeGetValue('exceptfds', 0, 'i32') }}} : 0);
var allHigh = (readfds ? {{{ makeGetValue('readfds', 4, 'i32') }}} : 0) |
(writefds ? {{{ makeGetValue('writefds', 4, 'i32') }}} : 0) |
(exceptfds ? {{{ makeGetValue('exceptfds', 4, 'i32') }}} : 0);
var check = function(fd, low, high, val) {
return (fd < 32 ? (low & val) : (high & val));
};
for (var fd = 0; fd < nfds; fd++) {
var mask = 1 << (fd % 32);
if (!(check(fd, allLow, allHigh, mask))) {
continue; // index isn't in the set
}
var stream = FS.getStream(fd);
if (!stream) throw new FS.ErrnoError({{{ cDefine('EBADF') }}});
var flags = SYSCALLS.DEFAULT_POLLMASK;
if (stream.stream_ops.poll) {
flags = stream.stream_ops.poll(stream);
}
if ((flags & {{{ cDefine('POLLIN') }}}) && check(fd, srcReadLow, srcReadHigh, mask)) {
fd < 32 ? (dstReadLow = dstReadLow | mask) : (dstReadHigh = dstReadHigh | mask);
total++;
}
if ((flags & {{{ cDefine('POLLOUT') }}}) && check(fd, srcWriteLow, srcWriteHigh, mask)) {
fd < 32 ? (dstWriteLow = dstWriteLow | mask) : (dstWriteHigh = dstWriteHigh | mask);
total++;
}
if ((flags & {{{ cDefine('POLLPRI') }}}) && check(fd, srcExceptLow, srcExceptHigh, mask)) {
fd < 32 ? (dstExceptLow = dstExceptLow | mask) : (dstExceptHigh = dstExceptHigh | mask);
total++;
}
}
if (readfds) {
{{{ makeSetValue('readfds', '0', 'dstReadLow', 'i32') }}};
{{{ makeSetValue('readfds', '4', 'dstReadHigh', 'i32') }}};
}
if (writefds) {
{{{ makeSetValue('writefds', '0', 'dstWriteLow', 'i32') }}};
{{{ makeSetValue('writefds', '4', 'dstWriteHigh', 'i32') }}};
}
if (exceptfds) {
{{{ makeSetValue('exceptfds', '0', 'dstExceptLow', 'i32') }}};
{{{ makeSetValue('exceptfds', '4', 'dstExceptHigh', 'i32') }}};
}
return total;
},
__sys_msync: function(addr, len, flags) {
#if CAN_ADDRESS_2GB
addr >>>= 0;
#endif
var info = SYSCALLS.mappings[addr];
if (!info) return 0;
SYSCALLS.doMsync(addr, FS.getStream(info.fd), len, info.flags, 0);
return 0;
},
__sys_getsid__nothrow: true,
__sys_getsid__proxy: false,
__sys_getsid: function(pid) {
if (pid && pid !== {{{ PROCINFO.pid }}}) return -{{{ cDefine('ESRCH') }}};
return {{{ PROCINFO.sid }}};
},
__sys_fdatasync: function(fd) {
var stream = SYSCALLS.getStreamFromFD(fd);
return 0; // we can't do anything synchronously; the in-memory FS is already synced to
},
__sys_mlock__sig: 'iii',
__sys_mlock: function(addr, len) {
return 0;
},
__sys_munlock__sig: 'iii',
__sys_munlock: function(addr, len) {
return 0;
},
__sys_mlockall__sig: 'ii',
__sys_mlockall: function(flags) {
return 0;
},
__sys_munlockall__sig: 'i',
__sys_munlockall: function() {
return 0;
},
__sys_mremap: function(old_addr, old_size, new_size, flags) {
return -{{{ cDefine('ENOMEM') }}}; // never succeed
},
__sys_poll: function(fds, nfds, timeout) {
var nonzero = 0;
for (var i = 0; i < nfds; i++) {
var pollfd = fds + {{{ C_STRUCTS.pollfd.__size__ }}} * i;
var fd = {{{ makeGetValue('pollfd', C_STRUCTS.pollfd.fd, 'i32') }}};
var events = {{{ makeGetValue('pollfd', C_STRUCTS.pollfd.events, 'i16') }}};
var mask = {{{ cDefine('POLLNVAL') }}};
var stream = FS.getStream(fd);
if (stream) {
mask = SYSCALLS.DEFAULT_POLLMASK;
if (stream.stream_ops.poll) {
mask = stream.stream_ops.poll(stream);
}
}
mask &= events | {{{ cDefine('POLLERR') }}} | {{{ cDefine('POLLHUP') }}};
if (mask) nonzero++;
{{{ makeSetValue('pollfd', C_STRUCTS.pollfd.revents, 'mask', 'i16') }}};
}
return nonzero;
},
__sys_rt_sigqueueinfo: function(tgid, pid, uinfo) {
return 0;
},
__sys_getcwd: function(buf, size) {
if (size === 0) return -{{{ cDefine('EINVAL') }}};
var cwd = FS.cwd();
var cwdLengthInBytes = lengthBytesUTF8(cwd);
if (size < cwdLengthInBytes + 1) return -{{{ cDefine('ERANGE') }}};
stringToUTF8(cwd, buf, size);
return buf;
},
__sys_ugetrlimit: function(resource, rlim) {
{{{ makeSetValue('rlim', C_STRUCTS.rlimit.rlim_cur, '-1', 'i32') }}}; // RLIM_INFINITY
{{{ makeSetValue('rlim', C_STRUCTS.rlimit.rlim_cur + 4, '-1', 'i32') }}}; // RLIM_INFINITY
{{{ makeSetValue('rlim', C_STRUCTS.rlimit.rlim_max, '-1', 'i32') }}}; // RLIM_INFINITY
{{{ makeSetValue('rlim', C_STRUCTS.rlimit.rlim_max + 4, '-1', 'i32') }}}; // RLIM_INFINITY
return 0; // just report no limits
},
__sys_mmap2__deps: ['$syscallMmap2'],
__sys_mmap2: function(addr, len, prot, flags, fd, off) {
return syscallMmap2(addr, len, prot, flags, fd, off);
},
__sys_truncate64: function(path, zero, low, high) {
path = SYSCALLS.getStr(path);
var length = SYSCALLS.get64(low, high);
FS.truncate(path, length);
return 0;
},
__sys_ftruncate64: function(fd, zero, low, high) {
var length = SYSCALLS.get64(low, high);
FS.ftruncate(fd, length);
return 0;
},
__sys_stat64: function(path, buf) {
path = SYSCALLS.getStr(path);
return SYSCALLS.doStat(FS.stat, path, buf);
},
__sys_lstat64: function(path, buf) {
path = SYSCALLS.getStr(path);
return SYSCALLS.doStat(FS.lstat, path, buf);
},
__sys_fstat64: function(fd, buf) {
var stream = SYSCALLS.getStreamFromFD(fd);
return SYSCALLS.doStat(FS.stat, stream.path, buf);
},
__sys_lchown32: function(path, owner, group) {
path = SYSCALLS.getStr(path);
FS.chown(path, owner, group); // XXX we ignore the 'l' aspect, and do the same as chown
return 0;
},
__sys_getuid32__sig: 'i',
__sys_getuid32__nothrow: true,
__sys_getuid32__proxy: false,
__sys_getuid32: '__sys_getegid32',
__sys_getgid32__sig: 'i',
__sys_getgid32__nothrow: true,
__sys_getgid32__proxy: false,
__sys_getgid32: '__sys_getegid32',
__sys_geteuid32__sig: 'i',
__sys_geteuid32__nothrow: true,
__sys_geteuid32__proxy: false,
__sys_geteuid32: '__sys_getegid32',
__sys_getegid32__nothrow: true,
__sys_getegid32__proxy: false,
__sys_getegid32: function() {
return 0;
},
__sys_fchown32: function(fd, owner, group) {
FS.fchown(fd, owner, group);
return 0;
},
__sys_chown32: function(path, owner, group) {
path = SYSCALLS.getStr(path);