Repository navigation
Expand file tree
/
Copy pathsys.odin
More file actions
3041 lines (2592 loc) · 82.9 KB
/
Copy pathsys.odin
File metadata and controls
3041 lines (2592 loc) · 82.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
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
#+build linux
#+no-instrumentation
package linux
import "base:intrinsics"
/*
Read data from file into the buffer
Returns the number of bytes successfully read, which may be less than the size
of the buffer even if the termination is successfull.
Available since Linux 1.0.
Before Linux 3.14, this operation is not atomic (i.e. not thread safe).
*/
read :: proc "contextless" (fd: Fd, buf: []u8) -> (int, Errno) {
ret := syscall(SYS_read, fd, raw_data(buf), len(buf))
return errno_unwrap(ret, int)
}
/*
Write the data from a buffer into the file
Returns the number of bytes successfully written, which may be less than the size
of the buffer, even if the termination is successfull
When using direct I/O, error doesn't mean the write has failed. Partial data may
have been written.
If .Eintr is returned, the write operation has failed due to interrupt. You'll probably
need to restart this syscall
Available since Linux 1.0
Before Linux 3.14 this operation is not atomic (i.e. not thread safe)
*/
write :: proc "contextless" (fd: Fd, buf: []u8) -> (int, Errno) {
ret := syscall(SYS_write, fd, raw_data(buf), len(buf))
return errno_unwrap(ret, int)
}
/*
Open file, get the file descriptor
Available since Linux 1.0.
On ARM64 available since Linux 2.6.16.
*/
open :: proc "contextless" (name: cstring, flags: Open_Flags, mode: Mode = {}) -> (Fd, Errno) {
when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
ret := syscall(SYS_openat, AT_FDCWD, transmute(uintptr) name, transmute(u32) flags, transmute(u32) mode)
return errno_unwrap(ret, Fd)
} else {
ret := syscall(SYS_open, transmute(uintptr) name, transmute(u32) flags, transmute(u32) mode)
return errno_unwrap(ret, Fd)
}
}
/*
Close the file.
Available since Linux 1.0.
*/
close :: proc "contextless" (fd: Fd) -> (Errno) {
ret := syscall(SYS_close, fd)
return Errno(-ret)
}
/*
Get file status.
Returns information about the file in struct pointed to by `stat` parameter.
Available since Linux 1.0
For 32-bit systems a different syscall is used that became available since 2.4.
Not available on ARM64.
*/
stat :: proc "contextless" (filename: cstring, stat: ^Stat) -> (Errno) {
when size_of(int) == 8 {
when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
ret := syscall(SYS_fstatat, AT_FDCWD, cast(rawptr) filename, stat, 0)
return Errno(-ret)
} else {
ret := syscall(SYS_stat, cast(rawptr) filename, stat)
return Errno(-ret)
}
} else {
ret := syscall(SYS_stat64, cast(rawptr) filename, stat)
return Errno(-ret)
}
}
/*
Get file status from file descriptor.
Returns information about the file in struct pointed to by `stat` parameter.
Available since Linux 1.0.
For 32-bit systems a different syscall is used that became available since 2.4.
*/
fstat :: proc "contextless" (fd: Fd, stat: ^Stat) -> (Errno) {
when size_of(int) == 8 {
ret := syscall(SYS_fstat, cast(i32) fd, stat)
return Errno(-ret)
} else {
ret := syscall(SYS_fstat64, cast(i32) fd, stat)
return Errno(-ret)
}
}
/*
Get information about the file that's potentially a symbolic link
The information is returned in a struct pointed to by `stat` parameter.
The difference with stat, fstat is that if the file is a symbolic link,
stat and fstat will dereference the link. lstat doesn't dereference symlinks.
Available since Linux 1.0.
For 32-bit systems a different syscall is used that became available since 2.4.
Not available on arm64.
*/
lstat :: proc "contextless" (filename: cstring, stat: ^Stat) -> (Errno) {
when size_of(int) == 8 {
when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
return fstatat(AT_FDCWD, filename, stat, {.SYMLINK_NOFOLLOW})
} else {
ret := syscall(SYS_lstat, cast(rawptr) filename, stat)
return Errno(-ret)
}
} else {
ret := syscall(SYS_lstat64, cast(rawptr) filename, stat)
return Errno(-ret)
}
}
/*
Wait on event on a file descriptor.
Available since Linux 2.2.
*/
poll :: proc "contextless" (fds: []Poll_Fd, timeout: i32) -> (i32, Errno) {
when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
seconds := cast(uint) timeout / 1000
nanoseconds := cast(uint) (timeout % 1000) * 1_000_000
timeout_spec := Time_Spec{seconds, nanoseconds}
ret := syscall(SYS_ppoll, raw_data(fds), len(fds), &timeout_spec, 0, 0)
return errno_unwrap(ret, i32)
} else {
ret := syscall(SYS_poll, raw_data(fds), len(fds), timeout)
return errno_unwrap(ret, i32)
}
}
/*
Seek the file stream to specified offset.
Available since Linux 1.0.
On 32-bit platforms available since Linux 1.2.
*/
lseek :: proc "contextless" (fd: Fd, off: i64, whence: Seek_Whence) -> (i64, Errno) {
when size_of(int) == 8 {
ret := syscall(SYS_lseek, fd, off, whence)
return errno_unwrap(ret, i64)
} else {
result: i64 = ---
lo, hi := compat64_arg_pair(off)
ret := syscall(SYS__llseek, fd, hi, lo, &result, whence)
return result, Errno(-ret)
}
}
/*
Map files into memory.
Available since Linux 1.0.
On 32-bit platforms since Linux 1.0.
*/
mmap :: proc "contextless" (addr: uintptr, size: uint, prot: Mem_Protection, flags: Map_Flags, fd: Fd = Fd(-1), offset: i64 = 0) -> (rawptr, Errno) {
when size_of(int) == 8 {
ret := syscall(SYS_mmap, addr, size, transmute(i32) prot, transmute(i32) flags, fd, offset)
return errno_unwrap(ret, rawptr, uintptr)
} else {
ret := syscall(SYS_mmap2, addr, size, transmute(i32) prot, transmute(i32) flags, fd, cast(uintptr)(offset/4096))
return errno_unwrap(ret, rawptr, uintptr)
}
}
/*
Protect memory region.
Available since Linux 1.0.
*/
mprotect :: proc "contextless" (addr: rawptr, size: uint, prot: Mem_Protection) -> (Errno) {
ret := syscall(SYS_mprotect, addr, size, transmute(i32) prot)
return Errno(-ret)
}
/*
Unmap memory.
Available since Linux 1.0.
*/
munmap :: proc "contextless" (addr: rawptr, size: uint) -> (Errno) {
ret := syscall(SYS_munmap, addr, size)
return Errno(-ret)
}
/*
Extend the data segment size until the address `addr`. Note: you may be
familiar with sbrk(), which is not actually a syscall, so is not
implemented here.
Available since Linux 1.0.
*/
brk :: proc "contextless" (addr: uintptr) -> (Errno) {
ret := syscall(SYS_brk, addr)
return Errno(-ret)
}
/*
Returns from signal handlers on some archs.
*/
rt_sigreturn :: proc "c" () -> ! {
intrinsics.syscall(uintptr(SYS_rt_sigreturn))
unreachable()
}
/*
Alter an action taken by a process.
*/
rt_sigaction :: proc "contextless" (sig: Signal, sigaction: ^Sig_Action($T), old_sigaction: ^Sig_Action($U)) -> Errno {
// NOTE(jason): It appears that the restorer is required for i386 and amd64
when ODIN_ARCH == .i386 || ODIN_ARCH == .amd64 {
sigaction.flags += {.RESTORER}
}
if sigaction != nil && sigaction.restorer == nil && .RESTORER in sigaction.flags {
sigaction.restorer = rt_sigreturn
}
ret := syscall(SYS_rt_sigaction, sig, sigaction, old_sigaction, size_of(Sig_Set))
return Errno(-ret)
}
/*
Examime and alter blocked signals.
Available since Linux 2.2.
*/
rt_sigprocmask :: proc "contextless" (mask_kind: Sig_Mask_Kind, new_set: ^Sig_Set, old_set: ^Sig_Set) -> Errno {
ret := syscall(SYS_rt_sigprocmask, mask_kind, new_set, old_set, size_of(Sig_Set))
return Errno(-ret)
}
/*
Control devices. The ioctl syscall is a bit special because
its argument is usually a pointer to some driver-specific structure.
The request value is device-specific. Consult your LibC implementation's
ioctls.h file to learn more. The returned value of ioctl *may* be an error
code value instead of a memory address depending on the request type.
Available since Linux 1.0.
*/
ioctl :: proc "contextless" (fd: Fd, request: u32, arg: uintptr) -> (uintptr) {
ret := syscall(SYS_ioctl, fd, request, arg)
return uintptr(ret)
}
/*
Read the file at a specified offset.
Note, it is not an error to return less bytes than requested.
Available since Linux 2.2.
*/
pread :: proc "contextless" (fd: Fd, buf: []u8, offset: i64) -> (int, Errno) {
when ODIN_ARCH == .arm32 {
ret := syscall(SYS_pread64, fd, raw_data(buf), len(buf), 0, compat64_arg_pair(offset))
} else {
ret := syscall(SYS_pread64, fd, raw_data(buf), len(buf), compat64_arg_pair(offset))
}
return errno_unwrap(ret, int)
}
/*
Read the file at a specified offset.
Note, it is not an error to return less bytes than requested.
Available since Linux 2.2.
*/
pwrite :: proc "contextless" (fd: Fd, buf: []u8, offset: i64) -> (int, Errno) {
when ODIN_ARCH == .arm32 {
ret := syscall(SYS_pwrite64, fd, raw_data(buf), len(buf), 0, compat64_arg_pair(offset))
} else {
ret := syscall(SYS_pwrite64, fd, raw_data(buf), len(buf), compat64_arg_pair(offset))
}
return errno_unwrap(ret, int)
}
/*
Read the data from file into multiple buffers.
Available since Linux 2.0.
*/
readv :: proc "contextless" (fd: Fd, iov: []IO_Vec) -> (int, Errno) {
ret := syscall(SYS_readv, fd, raw_data(iov), len(iov))
return errno_unwrap(ret, int)
}
/*
Write the data from multiple buffers into a file.
Available since Linux 2.0.
*/
writev :: proc "contextless" (fd: Fd, iov: []IO_Vec) -> (int, Errno) {
ret := syscall(SYS_writev, fd, raw_data(iov), len(iov))
return errno_unwrap(ret, int)
}
/*
Check user permissions for a file.
If Mode is F_OK, checks whether the file exists.
Similarly, X_OK, W_OK, R_OK check if the file is executable, writeable, readable respectively.
Available since Linux 1.0.
For ARM64 available since Linux 2.6.16.
*/
access :: proc "contextless" (name: cstring, mode: Mode = F_OK) -> (Errno) {
when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
ret := syscall(SYS_faccessat, AT_FDCWD, cast(rawptr) name, transmute(u32) mode)
return Errno(-ret)
} else {
ret := syscall(SYS_access, cast(rawptr) name, transmute(u32) mode)
return Errno(-ret)
}
}
/*
Create a pipe
Available since Linux 2.6.27
*/
pipe2 :: proc "contextless" (pipes: ^[2]Fd, flags: Open_Flags) -> (Errno) {
ret := syscall(SYS_pipe2, pipes, transmute(u32) flags)
return Errno(-ret)
}
/*
Yield the processor.
Available since Linux 2.0.
*/
sched_yield :: proc "contextless" () {
syscall(SYS_sched_yield)
}
/*
Remap a virtual memory address.
Available since Linux 2.0.
*/
mremap :: proc "contextless" (old_addr: rawptr, old_size: uint, new_size: uint, flags: MRemap_Flags, new_addr: uintptr = 0) -> (rawptr, Errno) {
if .FIXED in flags {
ret := syscall(SYS_mremap, old_addr, old_size, new_size, transmute(i32) flags, new_addr)
return errno_unwrap(ret, rawptr, rawptr)
} else {
ret := syscall(SYS_mremap, old_addr, old_size, new_size, transmute(i32) flags)
return errno_unwrap(ret, rawptr, rawptr)
}
}
/*
Sync file with memory map.
Available since Linux 2.0.
*/
msync :: proc "contextless" (addr: rawptr, size: uint, flags: MSync_Flags) -> (Errno) {
ret := syscall(SYS_msync, addr, size, transmute(i32) flags)
return Errno(-ret)
}
/*
Determine if pages are resident in memory.
Available since Linux 2.4.
*/
mincore :: proc "contextless" (addr: rawptr, size: uint, vec: []b8) -> (Errno) {
ret := syscall(SYS_mincore, addr, size, raw_data(vec))
return Errno(-ret)
}
/*
Give advice about use of memory.
Available since Linux 2.4.
*/
madvise :: proc "contextless" (addr: rawptr, size: uint, advice: MAdvice) -> (Errno) {
ret := syscall(SYS_madvise, addr, size, advice)
return Errno(-ret)
}
/*
Allocate a SystemV shared memory segment.
Available since Linux 2.0.
*/
shmget :: proc "contextless" (key: Key, size: uint, flags: IPC_Flags) -> (Key, Errno) {
ret := syscall(SYS_shmget, key, size, transmute(i16) flags)
return errno_unwrap(ret, Key)
}
/*
SystemV shared memory segment operations.
Available since Linux 2.0.
*/
shmat :: proc "contextless" (key: Key, addr: rawptr, flags: IPC_Flags) -> (rawptr, Errno) {
ret := syscall(SYS_shmat, key, addr, transmute(i16) flags)
return errno_unwrap(ret, rawptr, uintptr)
}
shmctl_ds :: proc "contextless" (key: Key, cmd: IPC_Cmd, buf: ^Shmid_DS) -> (Errno) {
ret := syscall(SYS_shmctl, key, cmd, buf)
return Errno(-ret)
}
shmctl_info :: proc "contextless" (key: Key, cmd: IPC_Cmd, buf: ^Shm_Info) -> (int, Errno) {
ret := syscall(SYS_shmctl, key, cmd, buf)
return errno_unwrap(ret, int)
}
/*
SystemV shared memory control.
Available since Linux 2.0.
*/
shmctl :: proc {shmctl_ds, shmctl_info}
/*
Allocate a new file descriptor that refers to the same file as the one provided.
Available since Linux 1.0.
*/
dup :: proc "contextless" (fd: Fd) -> (Fd, Errno) {
ret := syscall(SYS_dup, fd)
return errno_unwrap(ret, Fd)
}
/*
Adjust an existing file descriptor to point to the same file as `old`.
Available since Linux 1.0.
On ARM64 available since Linux 2.6.27.
*/
dup2 :: proc "contextless" (old: Fd, new: Fd) -> (Fd, Errno) {
when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
ret := syscall(SYS_dup3, old, new, 0)
return errno_unwrap(ret, Fd)
} else {
ret := syscall(SYS_dup2, old, new)
return errno_unwrap(ret, Fd)
}
}
/*
Wait until the next signal is delivered.
Available since Linux 1.0.
On ARM64 available since Linux 2.6.16.
*/
pause :: proc "contextless" () {
when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
syscall(SYS_ppoll, 0, 0, 0, 0)
} else {
syscall(SYS_pause)
}
}
/*
High-resolution sleep.
Available since Linux 2.0.
*/
nanosleep :: proc "contextless" (req: ^Time_Spec, rem: ^Time_Spec) -> (Errno) {
ret := syscall(SYS_nanosleep, req, rem)
return Errno(-ret)
}
/*
Get the value of an internal timer.
Available since Linux 1.0.
*/
getitimer :: proc "contextless" (which: ITimer_Which, cur: ^ITimer_Val) -> (Errno) {
ret := syscall(SYS_getitimer, cur)
return Errno(-ret)
}
/*
Set an alarm clock for delivery of a signal.
Available since Linux 1.0.
*/
alarm :: proc "contextless" (seconds: u32) -> u32 {
when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
new := ITimer_Val { value = { seconds = cast(int) seconds } }
old := ITimer_Val {}
syscall(SYS_setitimer, ITimer_Which.REAL, &new, &old)
return u32(old.value.seconds) + u32(new.value.microseconds)
} else {
return cast(u32) syscall(SYS_alarm, seconds)
}
}
/*
Set the value of an internal timer.
Available since Linux 1.0.
*/
setitimer :: proc "contextless" (which: ITimer_Which, new: ^ITimer_Val, old: ^ITimer_Val) -> (Errno) {
ret := syscall(SYS_getitimer, new, old)
return Errno(-ret)
}
/*
Returns the thread group ID of the current process.
Note that it doesn't return the pid, despite it's name.
Available since Linux 1.0.
*/
getpid :: proc "contextless" () -> Pid {
return cast(Pid) syscall(SYS_getpid)
}
/*
Transfer the data between file descriptors.
Available since Linux 2.2.
On 32-bit platforms available since Linux 2.6.
*/
sendfile :: proc "contextless" (out_fd: Fd, in_fd: Fd, offset: ^i64, count: uint) -> (int, Errno) {
when size_of(int) == 8 {
ret := syscall(SYS_sendfile, out_fd, in_fd, offset, count)
return errno_unwrap(ret, int)
} else {
ret := syscall(SYS_sendfile64, out_fd, in_fd, offset, count)
return errno_unwrap(ret, int)
}
}
/*
Create a socket file descriptor.
Available since Linux 2.0.
*/
socket :: proc "contextless" (domain: Address_Family, socktype: Socket_Type, sockflags: Socket_FD_Flags, protocol: Protocol) -> (Fd, Errno) {
sock_type_flags: int = cast(int) socktype | transmute(int) sockflags
ret := syscall(SYS_socket, domain, sock_type_flags, protocol)
return errno_unwrap(ret, Fd)
}
/*
Connect the socket to the address.
Available since Linux 2.0.
*/
connect :: proc "contextless" (sock: Fd, addr: ^$T) -> (Errno)
where
T == Sock_Addr_In ||
T == Sock_Addr_In6 ||
T == Sock_Addr_Un ||
T == Sock_Addr_Any
{
ret := syscall(SYS_connect, sock, addr, size_of(T))
return Errno(-ret)
}<
10BC8
/div>
/*
Accept a pending connection or block until new connection appears.
Depends on Sock_FD_Flags of the `sock` parameter.
Available since Linux 2.0.
*/
accept :: proc "contextless" (sock: Fd, addr: ^$T, sockflags: Socket_FD_Flags = {}) -> (Fd, Errno)
where
T == Sock_Addr_In ||
T == Sock_Addr_In6 ||
T == Sock_Addr_Un ||
T == Sock_Addr_Any
{
addr_len: i32 = size_of(T)
ret := syscall(SYS_accept4, sock, addr, &addr_len, transmute(int) sockflags)
return errno_unwrap(ret, Fd)
}
// TODO(flysand): Rewrite recvfrom and sendto to use default parameters
recvfrom :: proc "contextless" (sock: Fd, buf: []u8, flags: Socket_Msg, addr: ^$T) -> (int, Errno)
where
T == Sock_Addr_In ||
T == Sock_Addr_In6 ||
T == Sock_Addr_Un ||
T == Sock_Addr_Any
{
addr_len: i32 = size_of(T)
ret := syscall(SYS_recvfrom, sock, raw_data(buf), len(buf), transmute(i32) flags, addr, &addr_len)
return errno_unwrap(ret, int)
}
@private
recv_noaddr :: proc "contextless" (sock: Fd, buf: []u8, flags: Socket_Msg) -> (int, Errno) {
ret := syscall(SYS_recvfrom, sock, raw_data(buf), len(buf), transmute(i32) flags, cast(rawptr) nil, cast(uintptr) 0)
return errno_unwrap(ret, int)
}
sendto :: proc "contextless" (sock: Fd, buf: []u8, flags: Socket_Msg, addr: ^$T) -> (int, Errno)
where
T == Sock_Addr_In ||
T == Sock_Addr_In6 ||
T == Sock_Addr_Un ||
T == Sock_Addr_Any
{
ret := syscall(SYS_sendto, sock, raw_data(buf), len(buf), transmute(i32) flags, addr, size_of(T))
return errno_unwrap(ret, int)
}
@private
send_noaddr :: proc "contextless" (sock: Fd, buf: []u8, flags: Socket_Msg) -> (int, Errno) {
ret := syscall(SYS_sendto, sock, raw_data(buf), len(buf), transmute(i32) flags, cast(rawptr) nil, cast(uintptr) 0)
return errno_unwrap(ret, int)
}
/*
Receive a message from a socket.
Available since Linux 2.0.
*/
recv :: proc {recvfrom, recv_noaddr}
/*
Send a message through a socket.
Available since Linux 2.0.
*/
send :: proc {sendto, send_noaddr}
/*
Send a message on a socket.
Available since Linux 2.0.
*/
sendmsg :: proc "contextless" (sock: Fd, msghdr: ^Msg_Hdr, flags: Socket_Msg) -> (int, Errno) {
ret := syscall(SYS_sendmsg, sock, msghdr, transmute(i32) flags)
return errno_unwrap(ret, int)
}
/*
Receive a message on a socket.
Available since Linux 2.0.
*/
recvmsg :: proc "contextless" (sock: Fd, msghdr: ^Msg_Hdr, flags: Socket_Msg) -> (int, Errno) {
ret := syscall(SYS_recvmsg, sock, msghdr, transmute(i32) flags)
return errno_unwrap(ret, int)
}
/*
Shutdown a socket.
Available since Linux 2.0.
*/
shutdown :: proc "contextless" (sock: Fd, how: Shutdown_How) -> (Errno) {
ret := syscall(SYS_shutdown, sock, how)
return Errno(-ret)
}
/*
Bind a socket to the given local address.
Available since Linux 2.0.
*/
bind :: proc "contextless" (sock: Fd, addr: ^$T) -> (Errno)
where
T == Sock_Addr_In ||
T == Sock_Addr_In6 ||
T == Sock_Addr_Un ||
T == Sock_Addr_Any
{
ret := syscall(SYS_bind, sock, addr, size_of(T))
return Errno(-ret)
}
/*
Marks the socket as a socket that listen to connections using accept(2).
Available since Linux 2.0.
*/
listen :: proc "contextless" (sock: Fd, queue_len: i32) -> (Errno) {
ret := syscall(SYS_listen, sock, queue_len)
return Errno(-ret)
}
/*
Get socket name (aka it's bound address).
Available since Linux 2.0.
*/
getsockname :: proc "contextless" (sock: Fd, addr: ^Sock_Addr_Any) -> (Errno) {
addr_len := size_of(Sock_Addr_Any)
ret := syscall(SYS_getsockname, sock, addr, &addr_len)
return Errno(-ret)
}
/*
Get the name of the connected peer socket.
Available since Linux 2.0.
*/
getpeername :: proc "contextless" (sock: Fd, addr: ^Sock_Addr_Any) -> (Errno) {
addr_len := size_of(Sock_Addr_Any)
ret := syscall(SYS_getpeername, sock, addr, &addr_len)
return Errno(-ret)
}
/*
Create a pair of connected sockets.
Available since Linux 2.0.
*/
socketpair :: proc "contextless" (domain: Protocol_Family, type: Socket_Type, proto: Protocol, pair: ^[2]Fd) -> (Errno) {
ret := syscall(SYS_socketpair, domain, type, proto, pair)
return Errno(-ret)
}
// TODO(flysand): the parameters are the same, maybe there's a way to make it into a single proc, sacrificing type
// safety slightly
// TODO(flysand): add ability to specify slices
setsockopt_base :: proc "contextless" (sock: Fd, level: int, opt: int, val: $T) -> (Errno)
where
intrinsics.type_is_pointer(T) ||
intrinsics.type_is_multi_pointer(T)
{
val_data := val
val_size := size_of(intrinsics.type_elem_type(T))
ret := syscall(SYS_setsockopt, sock, level, opt, val_data, val_size)
return Errno(-ret)
}
setsockopt_sock :: proc "contextless" (sock: Fd, level: Socket_API_Level_Sock, opt: Socket_Option, val: $T) -> (Errno)
where
intrinsics.type_is_pointer(T) ||
intrinsics.type_is_multi_pointer(T)
{
return setsockopt_base(sock, cast(int) level, cast(int) opt, val)
}
setsockopt_tcp :: proc "contextless" (sock: Fd, level: Socket_API_Level_TCP, opt: Socket_TCP_Option, val: $T) -> (Errno)
where
intrinsics.type_is_pointer(T) ||
intrinsics.type_is_multi_pointer(T)
{
return setsockopt_base(sock, cast(int) level, cast(int) opt, val)
}
setsockopt_udp :: proc "contextless" (sock: Fd, level: Socket_API_Level_UDP, opt: Socket_UDP_Option, val: $T) -> (Errno)
where
intrinsics.type_is_pointer(T) ||
intrinsics.type_is_multi_pointer(T)
{
return setsockopt_base(sock, cast(int) level, cast(int) opt, val)
}
/*
Set socket option for a given socket API level.
Available since Linux 2.0.
*/
setsockopt :: proc {
setsockopt_sock,
setsockopt_tcp,
setsockopt_udp,
setsockopt_base,
}
getsockopt_base :: proc "contextless" (sock: Fd, level: int, opt: Socket_Option, val: $T) -> (int, Errno)
where
intrinsics.type_is_pointer(T) ||
intrinsics.type_is_multi_pointer(T)
{
val_data := val
val_size := size_of(T)
ret := syscall(SYS_getsockopt, sock, level, opt, val_data, cast(rawptr) &val_size)
return val_size, Errno(-ret)
}
getsockopt_sock :: proc "contextless" (sock: Fd, level: Socket_API_Level_Sock, opt: Socket_Option, val: ^$T) -> (int, Errno)
where
intrinsics.type_is_pointer(T) ||
intrinsics.type_is_multi_pointer(T)
{
return getsockopt_base(sock, cast(int) level, cast(int) opt, val)
}
getsockopt_tcp :: proc "contextless" (sock: Fd, level: Socket_API_Level_TCP, opt: Socket_TCP_Option, val: ^$T) -> (int, Errno)
where
intrinsics.type_is_pointer(T) ||
intrinsics.type_is_multi_pointer(T)
{
return getsockopt_base(sock, cast(int) level, cast(int) opt, val)
}
getsockopt_udp :: proc "contextless" (sock: Fd, level: Socket_API_Level_UDP, opt: Socket_UDP_Option, val: ^$T) -> (int, Errno)
where
intrinsics.type_is_pointer(T) ||
intrinsics.type_is_multi_pointer(T)
{
return getsockopt_base(sock, cast(int) level, cast(int) opt, val)
}
/*
Get socket option for a given socket API level.
Available since Linux 2.0.
*/
getsockopt :: proc {
getsockopt_sock,
getsockopt_tcp,
getsockopt_udp,
getsockopt_base,
}
/*
Creates a copy of the running process.
Available since Linux 1.0.
*/
fork :: proc "contextless" () -> (Pid, Errno) {
when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
ret := syscall(SYS_clone, u64(Signal.SIGCHLD), cast(rawptr) nil, cast(rawptr) nil, cast(rawptr) nil, u64(0))
return errno_unwrap(ret, Pid)
} else {
ret := syscall(SYS_fork)
return errno_unwrap(ret, Pid)
}
}
/*
Create a child process and block parent.
Available since Linux 2.2.
*/
vfork :: proc "contextless" () -> Pid {
when ODIN_ARCH != .arm64 && ODIN_ARCH != .riscv64 {
return Pid(syscall(SYS_vfork))
} else {
return Pid(syscall(SYS_clone, Signal.SIGCHLD))
}
}
/*
Replace the current process with another program.
Available since Linux 1.0.
On ARM64 available since Linux 3.19.
*/
execve :: proc "contextless" (name: cstring, argv: [^]cstring, envp: [^]cstring) -> (Errno) {
when ODIN_ARCH != .arm64 && ODIN_ARCH != .riscv64 {
ret := syscall(SYS_execve, cast(rawptr) name, cast(rawptr) argv, cast(rawptr) envp)
return Errno(-ret)
} else {
ret := syscall(SYS_execveat, AT_FDCWD, cast(rawptr) name, cast(rawptr) argv, cast(rawptr) envp, i32(0))
return Errno(-ret)
}
}
/*
Exit the thread with a given exit code.
Available since Linux 1.0.
*/
exit :: proc "contextless" (code: i32) -> ! {
syscall(SYS_exit, code)
unreachable()
}
/*
Wait for the process to change state.
Available since Linux 1.0.
*/
wait4 :: proc "contextless" (pid: Pid, status: ^u32, options: Wait_Options, rusage: ^RUsage) -> (Pid, Errno) {
ret := syscall(SYS_wait4, pid, status, transmute(u32) options, rusage)
return errno_unwrap(ret, Pid)
}
/*
See `wait4` for documentation.
*/
waitpid :: wait4
/*
Send signal to a process.
Available since Linux 1.0.
*/
kill :: proc "contextless" (pid: Pid, signal: Signal) -> (Errno) {
ret := syscall(SYS_kill, pid, signal)
return Errno(-ret)
}
/*
Get system information.
Available since Linux 1.0.
*/
uname :: proc "contextless" (uts_name: ^UTS_Name) -> (Errno) {
ret := syscall(SYS_uname, uts_name)
return Errno(-ret)
}
/*
Get a SystemV semaphore set identifier.
Available since Linux 2.0.
*/
semget :: proc "contextless" (key: Key, n: i32, flags: IPC_Flags) -> (Key, Errno) {
ret := syscall(SYS_semget, key, n, transmute(i16) flags)
return errno_unwrap(ret, Key)
}
/*
SystemV semaphore operations.
Available since Linux 2.0.
*/
semop :: proc "contextless" (key: Key, ops: []Sem_Buf) -> (Errno) {
when ODIN_ARCH != .i386 {
ret := syscall(SYS_semop, key, raw_data(ops), len(ops))
return Errno(-ret)
} else {
// Note(flysand): Max time limit, let's not worry about nanoseconds...
max_timespec := Time_Spec {
time_sec = max(uint),
time_nsec = 0,
}
ret := syscall(SYS_semtimedop_time64, key, raw_data(ops), len(ops), &max_timespec)
return Errno(-ret)
}
}
semctl3 :: proc "contextless" (key: Key, semnum: i32, cmd: IPC_Cmd) -> (int, Errno) {
ret := syscall(SYS_semctl, key, semnum, cmd)
return errno_unwrap(ret, int)
}
semctl4 :: proc "contextless" (key: Key, semnum: i32, cmd: IPC_Cmd, semun: ^Sem_Un) -> (int, Errno) {
ret := syscall(SYS_semctl, key, semnum, cmd, semun)
return errno_unwrap(ret, int)
}
/*
SystemV semaphore control operations.
Available since Linux 2.0.
*/
semctl :: proc {semctl3, semctl4}
/*
SystemV shared memory operations.
Available since Linux 2.0.
*/
shmdt :: proc "contextless" (shmaddr: rawptr) -> (Errno) {
ret := syscall(SYS_shmdt, shmaddr)
return Errno(-ret)
}
/*
Get SystemV message queue identifier.
Available since Linux 2.0.
*/
msgget :: proc "contextless" (key: Key, flags: IPC_Flags) -> (Key, Errno) {
ret := syscall(SYS_msgget, key, transmute(i16) flags)
return errno_unwrap(ret, Key)
}
/*
Send message to a SystemV message queue.
Available since Linux 2.0.
*/
msgsnd :: proc "contextless" (key: Key, buf: rawptr, size: int, flags: IPC_Flags) -> (Errno) {
ret := syscall(SYS_msgsnd, key, buf, size, transmute(i16) flags)
return Errno(-ret)
}
/*
Receive a message from a SystemV message queue.
Available since Linux 2.0.
*/
msgrcv :: proc "contextless" (key: Key, buf: rawptr, size: int, type: uint, flags: IPC_Flags) -> (int, Errno) {
ret := syscall(SYS_msgrcv, key, buf, size, type, transmute(i16) flags)
return errno_unwrap(ret, int)
}
/*
SystemV message control operations.
Available since Linux 2.0.
*/
msgctl :: proc "contextless" (key: Key, cmd: IPC_Cmd, buf: ^Msqid_DS) -> (int, Errno) {
ret := syscall(SYS_msgctl, key, cmd, buf)
return errno_unwrap(ret, int)
}
fcntl_dupfd :: proc "contextless" (fd: Fd, cmd: FCntl_Command_DUPFD, newfd: Fd) -> (Fd, Errno) {
ret := syscall(SYS_fcntl, fd, cmd, newfd)
return errno_unwrap(ret, Fd)
}
fcntl_dupfd_cloexec :: proc "contextless" (fd: Fd, cmd: FCntl_Command_DUPFD_CLOEXEC, newfd: Fd) -> (Fd, Errno) {
ret := syscall(SYS_fcntl, fd, cmd, newfd)
return errno_unwrap(ret, Fd)
}
fcntl_getfd :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GETFD) -> (Fd, Errno) {
ret := syscall(SYS_fcntl, fd, cmd)
return errno_unwrap(ret, Fd)
}
fcntl_setfd :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETFD, newfd: Fd) -> (Errno) {
ret := syscall(SYS_fcntl, fd, cmd, newfd)
return Errno(-ret)
}
fcntl_getfl :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GETFL) -> (Open_Flags, Errno) {
ret := syscall(SYS_fcntl, fd, cmd)
return errno_unwrap(u32(ret), Open_Flags, Open_Flags)
}
fcntl_setfl :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETFL, flags: Open_Flags) -> (Errno) {
ret := syscall(SYS_fcntl, fd, cmd, transmute(u32) flags)
return Errno(-ret)
}
fcntl_setlk :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETLK, lock: ^FLock) -> (Errno) {
when size_of(int) == 4 {
ret := syscall(SYS_fcntl64, fd, cmd, lock)
return Errno(-ret)
} else {
ret := syscall(SYS_fcntl, fd, cmd, lock)
return Errno(-ret)
}
}
fcntl_setlkw :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETLKW, lock: ^FLock) -> (Errno) {
when size_of(int) == 4 {
ret := syscall(SYS_fcntl64, fd, cmd, lock)
return Errno(-ret)
} else {
ret := syscall(SYS_fcntl, fd, cmd, lock)
return Errno(-ret)
}
}
fcntl_getlk :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GETLK, lock: ^FLock) -> (Errno) {
when size_of(int) == 4 {
ret := syscall(SYS_fcntl64, fd, cmd, lock)
return Errno(-ret)
} else {
ret := syscall(SYS_fcntl, fd, cmd, lock)
return Errno(-ret)