raz / commit e1dff012

commit
e1dff012947f487dff03e29558e1903db60b44ec
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
6a6bdf5c
CAbi: what the C library fixes, in a module of its own, written by tools/cabi.c

Sys over C keeps its code, over POSIX's names, and takes from CAbi
what a C library fixes for its target: the numbers of flags, requests,
signals and limits, where the fields of struct stat, statvfs, dirent,
sigaction, sockaddr_*, msghdr, cmsghdr, rusage and timeval lie, and
errno and closeFrom, whose names differ. Those structs are bytes here,
read and written at CAbi's offsets (field, setField). A socket address
is a sockaddr_storage, so a family's length byte, where a system has
one, is written too. Sys's signal and limit numbers are CAbi's.

tools/cabi.c writes lib/CAbi.linux-x86_64.raz from the system's own
headers; glibc and musl give the same file. Where C libraries differ
in a struct's size only (struct rusage, termios), cabi checks it fits
the room Sys keeps rather than writing it. Where one keeps an int and
a pad and another a size_t, a field written is given the room up to
the next.

Failures are minus Linux's errno on every target: CAbi pairs each
POSIX errno with Linux's, Sys maps through it, and errorText maps
back. Both Sys files export the errors' names: errNoEntry, errAgain,
... C loses the syscall extern step 1 added; CAbi declares its own.

patch · log

6 files changed, +806 -253

lib/C.raz (history) modified +0 -1

@@ -76,7 +76,6 @@
7676 extern proc dup2*(fd, fd2: I32): I32;
7777 extern proc dup*(fd: I32): I32;
7878 extern proc close_range*(first, last: U32; flags: I32): I32;
79-extern proc syscall*(n: INT; ...): INT; (* a system call by its number, on Linux *)
8079 extern proc execve*(path: ^U8; argv, envp: ^^U8): I32;
8180 extern proc waitpid*(pid: I32; status: ^I32; options: I32): I32;
8281 extern proc kill*(pid, signal: I32): I32;

lib/CAbi.linux-x86_64.raz (history) added +209 -0

@@ -0,0 +1,209 @@
1+(* CAbi: what the C library fixes for Sys over C on linux-x86_64: the
2+ numbers of its flags, requests, signals and limits, where the
3+ fields of its structs lie, an At the offset and a Len the width,
4+ its errnos beside Linux's, and errno and closeFrom.
5+
6+ Written by tools/cabi.c, from the system's own headers; not to be
7+ edited, but written again. *)
8+module CAbi;
9+
10+const
11+ (* open's flags, fcntl's requests, seek, flock, mmap, access *)
12+ oRdonly* = 0;
13+ oWronly* = 1;
14+ oRdwr* = 2;
15+ oCreat* = 64;
16+ oExcl* = 128;
17+ oTrunc* = 512;
18+ oAppend* = 1024;
19+ oNonblock* = 2048;
20+ oCloexec* = 524288;
21+ oNoctty* = 256;
22+ fGetfd* = 1;
23+ fSetfd* = 2;
24+ fGetfl* = 3;
25+ fSetfl* = 4;
26+ fDupfdCloexec* = 1030;
27+ fdCloexec* = 1;
28+ seekSet* = 0;
29+ seekCur* = 1;
30+ seekEnd* = 2;
31+ lockEx* = 2;
32+ lockNb* = 4;
33+ protRead* = 1;
34+ protWrite* = 2;
35+ mapShared* = 1;
36+ mapPrivate* = 2;
37+ mapAnonymous* = 32;
38+ mfdCloexec* = 1;
39+ fOk* = 0;
40+ rOk* = 4;
41+ wOk* = 2;
42+ xOk* = 1;
43+ (* struct stat, statvfs and dirent *)
44+ statLen* = 144;
45+ stDevAt* = 0;
46+ stDevLen* = 8;
47+ stInoAt* = 8;
48+ stInoLen* = 8;
49+ stNlinkAt* = 16;
50+ stNlinkLen* = 8;
51+ stModeAt* = 24;
52+ stModeLen* = 4;
53+ stSizeAt* = 48;
54+ stSizeLen* = 8;
55+ stMtimeAt* = 88;
56+ stMtimeLen* = 8;
57+ stMtimeNsAt* = 96;
58+ stMtimeNsLen* = 8;
59+ sIfmt* = 61440;
60+ sIfdir* = 16384;
61+ sIfreg* = 32768;
62+ sIflnk* = 40960;
63+ statvfsLen* = 112;
64+ fFrsizeAt* = 8;
65+ fFrsizeLen* = 8;
66+ fBlocksAt* = 16;
67+ fBlocksLen* = 8;
68+ fBavailAt* = 32;
69+ fBavailLen* = 8;
70+ dNameAt* = 19;
71+ dTypeAt* = 18;
72+ dtUnknown* = 0;
73+ dtDir* = 4;
74+ dtReg* = 8;
75+ dtLnk* = 10;
76+ (* the terminal, poll and the clocks *)
77+ tcsanow* = 0;
78+ tcsadrain* = 1;
79+ tiocgwinsz* = 21523;
80+ tiocswinsz* = 21524;
81+ tiocsctty* = 21518;
82+ pollin* = 1;
83+ pollout* = 4;
84+ clockRealtime* = 0;
85+ clockMonotonic* = 1;
86+ timerAbstime* = 1;
87+ (* sockets, their addresses, and a message with fds *)
88+ afUnix* = 1;
89+ afInet* = 2;
90+ afInet6* = 10;
91+ sockStream* = 1;
92+ sockDgram* = 2;
93+ sockCloexec* = 524288;
94+ sockNonblock* = 2048;
95+ solSocket* = 1;
96+ soReuseaddr* = 2;
97+ soError* = 4;
98+ msgNosignal* = 16384;
99+ msgCmsgCloexec* = 1073741824;
100+ scmRights* = 1;
101+ sockaddrLen* = 128;
102+ saFamilyAt* = 0;
103+ saFamilyLen* = 2;
104+ saLenAt* = -1;
105+ sockaddrUnLen* = 110;
106+ sunPathAt* = 2;
107+ sunPathLen* = 108;
108+ sockaddrInLen* = 16;
109+ sinPortAt* = 2;
110+ sinAddrAt* = 4;
111+ sockaddrIn6Len* = 28;
112+ sin6PortAt* = 2;
113+ sin6AddrAt* = 8;
114+ msghdrLen* = 56;
115+ msgIovAt* = 16;
116+ msgIovlenAt* = 24;
117+ msgIovlenLen* = 8;
118+ msgControlAt* = 32;
119+ msgControllenAt* = 40;
120+ msgControllenLen* = 8;
121+ cmsgLenAt* = 0;
122+ cmsgLenLen* = 8;
123+ cmsgLevelAt* = 8;
124+ cmsgTypeAt* = 12;
125+ cmsgDataAt* = 16;
126+ cmsgAlign* = 8;
127+ (* signals, sigaction and sigprocmask *)
128+ sigHup* = 1;
129+ sigInt* = 2;
130+ sigQuit* = 3;
131+ sigFpe* = 8;
132+ sigKill* = 9;
133+ sigUsr1* = 10;
134+ sigSegv* = 11;
135+ sigUsr2* = 12;
136+ sigPipe* = 13;
137+ sigAlrm* = 14;
138+ sigTerm* = 15;
139+ sigChld* = 17;
140+ sigCont* = 18;
141+ sigStop* = 19;
142+ sigTstp* = 20;
143+ sigTtin* = 21;
144+ sigTtou* = 22;
145+ sigWinch* = 28;
146+ sigMax* = 64;
147+ sigactionLen* = 152;
148+ saHandlerAt* = 0;
149+ saMaskAt* = 8;
150+ saFlagsAt* = 136;
151+ saFlagsLen* = 4;
152+ sigsetLen* = 128;
153+ saRestart* = 268435456;
154+ sigSetmask* = 2;
155+ (* limits, time used, and wait *)
156+ rlimitCpu* = 0;
157+ rlimitFsize* = 1;
158+ rlimitData* = 2;
159+ rlimitStack* = 3;
160+ rlimitCore* = 4;
161+ rlimitNproc* = 6;
162+ rlimitNofile* = 7;
163+ rlimitAs* = 9;
164+ rlimInfinity* = -1;
165+ rusageSelf* = 0;
166+ rusageChildren* = -1;
167+ ruUtimeAt* = 0;
168+ ruStimeAt* = 16;
169+ tvSecAt* = 0;
170+ tvSecLen* = 8;
171+ tvUsecAt* = 8;
172+ tvUsecLen* = 8;
173+ wNohang* = 1;
174+ wUntraced* = 2;
175+ (* errnos: EINTR; and each POSIX names, the C library's number, then Linux's *)
176+ eIntr* = 4;
177+ errnos*: [152]I32 = [
178+ 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
179+ 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12,
180+ 13, 13, 14, 14, 16, 16, 17, 17, 18, 18, 19, 19,
181+ 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25,
182+ 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
183+ 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37,
184+ 38, 38, 39, 39, 40, 40, 42, 42, 43, 43, 67, 67,
185+ 71, 71, 72, 72, 74, 74, 75, 75, 84, 84, 88, 88,
186+ 89, 89, 90, 90, 91, 91, 92, 92, 93, 93, 95, 95,
187+ 95, 95, 97, 97, 98, 98, 99, 99, 100, 100, 101, 101,
188+ 102, 102, 103, 103, 104, 104, 105, 105, 106, 106, 107, 107,
189+ 110, 110, 111, 111, 113, 113, 114, 114, 115, 115, 116, 116,
190+ 122, 122, 125, 125, 130, 130, 131, 131];
191+
192+extern proc __errno_location: ^I32;
193+extern proc syscall(n: INT; ...): INT;
194+
195+(* errno, this thread's *)
196+proc errno*: ^I32;
197+begin
198+ return __errno_location();
199+end;
200+
201+(* Closes the fds from lo to hi, or every one from lo if hi < 0: by
202+ close_range's system call, since musl has no close_range. *)
203+proc closeFrom*(lo, hi: INT);
204+begin
205+ if hi < 0 then hi := 0FFFFFFFFH; end;
206+ _ := syscall(436, lo, hi, 0);
207+end;
208+
209+end CAbi.

lib/Sys.linux-x86_64.raz (history) modified +10 -0

@@ -23,6 +23,16 @@
2323 limitProcs* = 6; (* processes of its user *)
2424 limitFiles* = 7; (* one more than its highest fd *)
2525 limitMemory* = 9; (* bytes of its address space *)
26+ (* errors, minus Linux's errnos, as on every target *)
27+ errPerm* = -1; errNoEntry* = -2; errNoProcess* = -3; errIntr* = -4;
28+ errIo* = -5; errBadFd* = -9; errChild* = -10; errAgain* = -11;
29+ errNoMemory* = -12; errDenied* = -13; errExists* = -17;
30+ errNotDir* = -20; errIsDir* = -21; errInvalid* = -22;
31+ errNotTerminal* = -25; errNoSpace* = -28; errPipe* = -32;
32+ errNameTooLong* = -36; errNotEmpty* = -39; errLoop* = -40;
33+ errAddrInUse* = -98; errNetUnreach* = -101; errConnReset* = -104;
34+ errTimedOut* = -110; errRefused* = -111; errHostUnreach* = -113;
35+ errInProgress* = -115;
2636
2737 type
2838 (* What open is asked for: reading, writing or both, and what else

lib/Sys.raz (history) modified +350 -250

@@ -2,19 +2,20 @@
22 file of their own, with the same exports. A program that imports it
33 uses C.
44
5- It keeps to POSIX's names, and raw mode is cfmakeraw's. A terminal's
6- modes, Mode, are its struct termios whole, of whatever layout, and
7- only the two struct timeval that begin struct rusage are read. What
8- is glibc's, on x86-64: where a name lies in struct dirent; struct
9- stat; open's flags; the numbers of the requests TIOCGWINSZ,
10- TIOCSWINSZ and TIOCSCTTY, and of F_DUPFD_CLOEXEC; close_range's
11- system call;
12- SOCK_CLOEXEC, AF_INET6 and SO_REUSEADDR; struct sockaddr_un and sockaddr_in, which on the BSDs
13- start with a byte of length; the numbers of the signals and of the
14- limits, and RLIM_INFINITY, all ones. A C library that differs needs
15- a Sys of its own. *)
5+ It keeps to POSIX's names, and raw mode is cfmakeraw's. What the C
6+ library fixes for its target, the numbers of flags, requests,
7+ signals, limits and errnos, where a field lies in a struct, and the
8+ two procedures whose names differ, it takes from CAbi, one file per
9+ target, which tools/cabi.c writes; so a struct the C library fills
10+ in or reads is bytes here, its fields read and written at CAbi's
11+ offsets by field and setField. A failure is minus Linux's errno, on
12+ every target: linux maps the C library's, and errorText maps back.
13+ A terminal's modes, Mode, are its struct termios whole, which cabi
14+ checks fits in 256 bytes. Still Linux's: memfd_create, getrandom,
15+ pipe2 and accept4, which glibc and musl have, and what a wait
16+ status holds, which every Unix encodes alike. *)
1617 module Sys;
17-import C;
18+import C, CAbi;
1819
1920 const
2021 stdin* = 0; (* standard input's file descriptor *)
@@ -23,26 +24,32 @@
2324 (* the bytes Out keeps before it writes them; a target with little
2425 memory keeps fewer *)
2526 outSize* = 4096;
26- (* signals, by their numbers on Linux; another target has its own *)
27- sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13;
28- sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19;
29- sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28;
30- sigUsr1* = 10; sigUsr2* = 12; sigAlrm* = 14;
31- (* the limits limit and setLimit take, by their numbers on Linux;
32- another target has its own *)
33- limitCpu* = 0; (* seconds of the processor *)
34- limitFileSize* = 1; (* bytes in a file it writes *)
35- limitData* = 2; (* bytes of its data *)
36- limitStack* = 3; (* bytes of its stack *)
37- limitCore* = 4; (* bytes of a core dump *)
38- limitProcs* = 6; (* processes of its user *)
39- limitFiles* = 7; (* one more than its highest fd *)
40- limitMemory* = 9; (* bytes of its address space *)
41- DirentName = 19; (* the offset of d_name in glibc's struct dirent *)
42- DirentType = 18; (* and of d_type *)
43- TIOCGWINSZ = 5413H; (* glibc's, as the two below *)
44- TIOCSWINSZ = 5414H;
45- TIOCSCTTY = 540EH;
27+ (* signals, by the target's numbers *)
28+ sigHup* = CAbi.sigHup; sigInt* = CAbi.sigInt; sigQuit* = CAbi.sigQuit;
29+ sigKill* = CAbi.sigKill; sigPipe* = CAbi.sigPipe; sigTerm* = CAbi.sigTerm;
30+ sigChld* = CAbi.sigChld; sigCont* = CAbi.sigCont; sigStop* = CAbi.sigStop;
31+ sigTstp* = CAbi.sigTstp; sigTtin* = CAbi.sigTtin; sigTtou* = CAbi.sigTtou;
32+ sigWinch* = CAbi.sigWinch; sigUsr1* = CAbi.sigUsr1; sigUsr2* = CAbi.sigUsr2;
33+ sigAlrm* = CAbi.sigAlrm;
34+ (* the limits limit and setLimit take, by the target's numbers *)
35+ limitCpu* = CAbi.rlimitCpu; (* seconds of the processor *)
36+ limitFileSize* = CAbi.rlimitFsize; (* bytes in a file it writes *)
37+ limitData* = CAbi.rlimitData; (* bytes of its data *)
38+ limitStack* = CAbi.rlimitStack; (* bytes of its stack *)
39+ limitCore* = CAbi.rlimitCore; (* bytes of a core dump *)
40+ limitProcs* = CAbi.rlimitNproc; (* processes of its user *)
41+ limitFiles* = CAbi.rlimitNofile; (* one more than its highest fd *)
42+ limitMemory* = CAbi.rlimitAs; (* bytes of its address space *)
43+ (* errors, minus Linux's errnos, as on every target *)
44+ errPerm* = -1; errNoEntry* = -2; errNoProcess* = -3; errIntr* = -4;
45+ errIo* = -5; errBadFd* = -9; errChild* = -10; errAgain* = -11;
46+ errNoMemory* = -12; errDenied* = -13; errExists* = -17;
47+ errNotDir* = -20; errIsDir* = -21; errInvalid* = -22;
48+ errNotTerminal* = -25; errNoSpace* = -28; errPipe* = -32;
49+ errNameTooLong* = -36; errNotEmpty* = -39; errLoop* = -40;
50+ errAddrInUse* = -98; errNetUnreach* = -101; errConnReset* = -104;
51+ errTimedOut* = -110; errRefused* = -111; errHostUnreach* = -113;
52+ errInProgress* = -115;
4653
4754 type
4855 (* What open is asked for: reading, writing or both, and what else
@@ -92,14 +99,12 @@
9299 (* How a program that wait waited for changed. *)
93100 Status* = (running, exited, killed, stopped);
94101
95- (* glibc's struct sigaction; a handler of 0 is SIG_DFL, 1 SIG_IGN *)
102+ (* a signal's handler; nil is SIG_DFL, 1 SIG_IGN *)
96103 Handler = proc(sig: I32);
97- Sigaction = record
98- handler: Handler;
99- mask: [16]U64;
100- flags: I32;
101- restorer: ^U8;
102- end;
104+ (* the C library's struct sigaction and sigset_t, as bytes, kept
105+ aligned for the pointers in them *)
106+ Sigaction = record b: [(CAbi.sigactionLen + 7) div 8]U64; end;
107+ Sigset = [(CAbi.sigsetLen + 7) div 8]U64;
103108
104109 (* A terminal's modes, as getMode reads them and setMode sets them:
105110 a struct termios, of the C library's size, 256 bytes at most. *)
@@ -107,50 +112,24 @@
107112 t: [256]U8;
108113 end;
109114
110- (* struct rusage: what is read of it, the time used in the program
111- and in the system, each a struct timeval, its microseconds at
112- most an int and little-endian, then room for the rest *)
113- Timeval = record sec: I64; usec: I32; pad: I32; end;
114- Rusage = record user, system: Timeval; rest: [32]I64; end;
115-
116115 Winsize = record rows, cols, xpixel, ypixel: U16; end;
117116 Pollfd = record fd: I32; events, revents: I16; end;
118117
119- (* glibc's struct sockaddr_un: AF_UNIX, then the path and its zero *)
120- SockaddrUn = record
121- family: U16;
122- path: [108]U8;
123- end;
124- (* struct sockaddr_in or sockaddr_in6: AF_INET and the IPv4 address
125- in data[0..4], or AF_INET6 and the IPv6 one in data[4..20] *)
126- Sockaddr = record
127- family: U16;
128- port: [2]U8;
129- data: [24]U8;
130- end;
118+ (* a socket's address, struct sockaddr_storage, which holds any:
119+ a struct sockaddr_un, sockaddr_in or sockaddr_in6 *)
120+ Sockaddr = record b: [(CAbi.sockaddrLen + 7) div 8]U64; end;
131121 (* Whom a datagram came from, as receive says and reply takes: the
132122 address recvfrom fills in, and its size. *)
133123 Peer* = record
134124 a: Sockaddr;
135125 size: INT;
136126 end;
137- (* struct msghdr, what sendmsg and recvmsg take: no address, one
138- buffer, and a control message; the sizes and flags are ints
139- with room after them, kept zero *)
140- Msghdr = record
141- name: ^U8; nameSize: INT;
142- iov: ^Iovec; iovLen: INT;
143- control: ^U8; controlSize: INT;
144- flags: INT;
145- end;
127+ (* struct msghdr, what sendmsg and recvmsg take, as bytes: no
128+ address, one buffer, and a control message, a struct cmsghdr
129+ and the fds sent, at most 16 *)
130+ Msghdr = record b: [(CAbi.msghdrLen + 7) div 8]U64; end;
146131 Iovec = record base: ^U8; size: INT; end;
147- (* struct cmsghdr, SCM_RIGHTS's: its size, counted to the last fd,
148- then the fds, as many as sendFds sends *)
149- Rights = record
150- size: INT;
151- level, kind: I32;
152- fds: [16]I32;
153- end;
132+ Control = record b: [(CAbi.cmsgDataAt + 4 * 16 + 7) div 8]U64; end;
154133 Timespec = record sec, nsec: I64; end;
155134
156135 var
@@ -159,17 +138,50 @@
159138 saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *)
160139 host: [256]U8; (* what hostName gives *)
161140 sigRead, sigWrite: I32; (* the pipe the handler of caught signals writes their numbers to, once open *)
162- noted: [65]INT; (* 1 at sig when a signal handle took has come, until came asks *)
141+ noted: [CAbi.sigMax + 1]INT; (* 1 at sig when a signal handle took has come, until came asks *)
163142 broke: INT; (* 1 when one has come since a call that ready or readyFor makes again began *)
164143
165-(* minus errno, as a system call returns it *)
144+(* err, minus an errno of the C library's, as minus Linux's errno of
145+ the same name; err itself if it has none *)
146+proc linux(err: INT): INT;
147+var i: INT;
148+begin
149+ for i := 0 to LEN(CAbi.errnos) - 2 by 2 do
150+ if -INT(CAbi.errnos[i]) = err then return -INT(CAbi.errnos[i + 1]); end;
151+ end;
152+ return err;
153+end;
154+
155+(* minus errno, Linux's, as a system call returns it *)
166156 proc failed: INT;
167157 var e: ^I32;
168158 begin
169- e := C.__errno_location();
170- return -INT(e^);
159+ e := CAbi.errno();
160+ return linux(-INT(e^));
171161 end;
172162
163+(* The integer of len bytes at p + at, a field of a C struct *)
164+proc field(p: ^U8; at, len: INT): I64;
165+var q8: ^I64; q4: ^U32; q2: ^U16;
166+begin
167+ if len = 8 then q8 := ^I64(p + at); return q8^;
168+ elsif len = 4 then q4 := ^U32(p + at); return I64(q4^);
169+ elsif len = 2 then q2 := ^U16(p + at); return I64(q2^);
170+ end;
171+ return I64(p[at]);
172+end;
173+
174+(* Writes v to the field of len bytes at p + at, its low bytes *)
175+proc setField(p: ^U8; at, len: INT; v: I64);
176+var q8: ^I64; q4: ^U32; q2: ^U16;
177+begin
178+ if len = 8 then q8 := ^I64(p + at); q8^ := v;
179+ elsif len = 4 then q4 := ^U32(p + at); q4^ := U32(v & 0FFFFFFFFH);
180+ elsif len = 2 then q2 := ^U16(p + at); q2^ := U16(v & 0FFFFH);
181+ else p[at] := U8(v & 0FFH);
182+ end;
183+end;
184+
173185 (* Whether a call that failed is to be made again: when it failed with
174186 EINTR because a signal catch took broke into it, since on Linux those
175187 break into nothing; not when one handle took did, which the program
@@ -210,19 +222,19 @@
210222 return true;
211223 end;
212224
213-(* open's flags for how; glibc's numbers *)
225+(* open's flags for how *)
214226 proc flags(how: Accesses): I32;
215227 var f: I32;
216228 begin
217- f := 0; (* O_RDONLY *)
229+ f := CAbi.oRdonly;
218230 if writing in how then
219- f := 1; (* O_WRONLY *)
220- if reading in how then f := 2; end; (* O_RDWR *)
231+ f := CAbi.oWronly;
232+ if reading in how then f := CAbi.oRdwr; end;
221233 end;
222- if creating in how then f := f | 40H; end;
223- if exclusive in how then f := f | 80H; end;
224- if truncating in how then f := f | 200H; end;
225- if appending in how then f := f | 400H; end;
234+ if creating in how then f := f | CAbi.oCreat; end;
235+ if exclusive in how then f := f | CAbi.oExcl; end;
236+ if truncating in how then f := f | CAbi.oTrunc; end;
237+ if appending in how then f := f | CAbi.oAppend; end;
226238 return f;
227239 end;
228240
@@ -263,8 +275,8 @@
263275 proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64;
264276 var r: I64;
265277 begin
266- if fromEnd then r := C.lseek(I32(fd), pos, 2); (* SEEK_END *)
267- else r := C.lseek(I32(fd), pos, 0); end; (* SEEK_SET *)
278+ if fromEnd then r := C.lseek(I32(fd), pos, CAbi.seekEnd);
279+ else r := C.lseek(I32(fd), pos, CAbi.seekSet); end;
268280 if r < 0 then return failed(); end;
269281 return r;
270282 end;
@@ -274,7 +286,7 @@
274286 proc position*(fd: INT): I64;
275287 var r: I64;
276288 begin
277- r := C.lseek(I32(fd), 0, 1); (* SEEK_CUR *)
289+ r := C.lseek(I32(fd), 0, CAbi.seekCur);
278290 if r < 0 then return failed(); end;
279291 return r;
280292 end;
@@ -302,7 +314,7 @@
302314 syncfs gives one that isn't. *)
303315 proc syncAll*(fd: INT): INT;
304316 begin
305- if C.fcntl(I32(fd), 1, 0) < 0 then return failed(); end; (* F_GETFD *)
317+ if C.fcntl(I32(fd), CAbi.fGetfd, 0) < 0 then return failed(); end;
306318 C.sync;
307319 return 0;
308320 end;
@@ -314,8 +326,8 @@
314326 proc lock*(fd: INT; wait: BOOL): INT;
315327 var how: I32;
316328 begin
317- how := 2; (* LOCK_EX *)
318- if not wait then how := how | 4; end; (* LOCK_NB *)
329+ how := CAbi.lockEx;
330+ if not wait then how := how | CAbi.lockNb; end;
319331 if C.flock(I32(fd), how) # 0 then return failed(); end;
320332 return 0;
321333 end;
@@ -353,7 +365,7 @@
353365 proc dupAbove*(fd, least: INT): INT;
354366 var r: I32;
355367 begin
356- r := C.fcntl(I32(fd), 1030, I32(least)); (* F_DUPFD_CLOEXEC *)
368+ r := C.fcntl(I32(fd), CAbi.fDupfdCloexec, I32(least));
357369 if r < 0 then return failed(); end;
358370 return INT(r);
359371 end;
@@ -364,10 +376,10 @@
364376 proc keepOnExec*(fd: INT; yes: BOOL): INT;
365377 var f: I32;
366378 begin
367- f := C.fcntl(I32(fd), 1, 0); (* F_GETFD *)
379+ f := C.fcntl(I32(fd), CAbi.fGetfd, 0);
368380 if f < 0 then return failed(); end;
369- if yes then f := f & ~1; else f := f | 1; end; (* FD_CLOEXEC *)
370- if C.fcntl(I32(fd), 2, f) < 0 then return failed(); end; (* F_SETFD *)
381+ if yes then f := f & ~I32(CAbi.fdCloexec); else f := f | CAbi.fdCloexec; end;
382+ if C.fcntl(I32(fd), CAbi.fSetfd, f) < 0 then return failed(); end;
371383 return 0;
372384 end;
373385
@@ -376,7 +388,7 @@
376388 proc mmap*(size: INT): ^U8;
377389 var p: ^U8;
378390 begin
379- p := C.mmap(nil, UINT(size), 3, 22H, -1, 0); (* read, write; private, anonymous *)
391+ p := C.mmap(nil, UINT(size), CAbi.protRead | CAbi.protWrite, CAbi.mapPrivate | CAbi.mapAnonymous, -1, 0);
380392 if INT(p) = -1 then return nil; end;
381393 return p;
382394 end;
@@ -393,7 +405,7 @@
393405 var name: [4]U8; fd: I32; err: INT;
394406 begin
395407 _ := cstr("raz", name); (* the name /proc shows *)
396- fd := C.memfd_create(ADR(name[0]), 1); (* MFD_CLOEXEC *)
408+ fd := C.memfd_create(ADR(name[0]), CAbi.mfdCloexec);
397409 if fd < 0 then return failed(); end;
398410 if C.ftruncate(fd, I64(size)) # 0 then
399411 err := failed(); (* before close can change errno *)
@@ -409,7 +421,7 @@
409421 proc mapFile*(fd, size: INT): ^U8;
410422 var p: ^U8;
411423 begin
412- p := C.mmap(nil, UINT(size), 3, 1, I32(fd), 0); (* read, write; MAP_SHARED *)
424+ p := C.mmap(nil, UINT(size), CAbi.protRead | CAbi.protWrite, CAbi.mapShared, I32(fd), 0);
413425 if INT(p) = -1 then return nil; end;
414426 return p;
415427 end;
@@ -466,10 +478,10 @@
466478 var a: [4096]U8; m: I32;
467479 begin
468480 if not cstr(path, a) then return -36; end;
469- m := 0; (* F_OK *)
470- if reading in how then m := m | 4; end; (* R_OK *)
471- if writing in how then m := m | 2; end; (* W_OK *)
472- if executing in how then m := m | 1; end; (* X_OK *)
481+ m := CAbi.fOk;
482+ if reading in how then m := m | CAbi.rOk; end;
483+ if writing in how then m := m | CAbi.wOk; end;
484+ if executing in how then m := m | CAbi.xOk; end;
473485 if C.access(ADR(a[0]), m) # 0 then return failed(); end;
474486 return 0;
475487 end;
@@ -502,18 +514,27 @@
502514 return linked[0..n], 0;
503515 end;
504516
517+type Stat = [(CAbi.statLen + 7) div 8]U64; (* a struct stat, as bytes *)
518+
519+(* The type of file a struct stat says, S_IFDIR, S_IFREG or another *)
520+proc fileType(st: Stat): INT;
521+begin
522+ return INT(field(^U8(ADR(st)), CAbi.stModeAt, CAbi.stModeLen)) & CAbi.sIfmt;
523+end;
524+
505525 (* An Info from a struct stat, all but symlink. *)
506-proc fromStat(st: [18]INT): Info;
507-var i: Info;
526+proc fromStat(st: Stat): Info;
527+var i: Info; p: ^U8;
508528 begin
509- i.size := st[6]; (* st_size *)
510- i.dev := U64(st[0]); (* st_dev *)
511- i.ino := U64(st[1]); (* st_ino *)
512- i.links := U64(st[2]); (* st_nlink *)
513- i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *)
514- i.mode := st[3] & 1FFH;
515- i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *)
516- i.file := st[3] & 0F000H = 8000H; (* S_IFREG *)
529+ p := ^U8(ADR(st));
530+ i.size := field(p, CAbi.stSizeAt, CAbi.stSizeLen);
531+ i.dev := U64(field(p, CAbi.stDevAt, CAbi.stDevLen));
532+ i.ino := U64(field(p, CAbi.stInoAt, CAbi.stInoLen));
533+ i.links := U64(field(p, CAbi.stNlinkAt, CAbi.stNlinkLen));
534+ i.mtime := field(p, CAbi.stMtimeAt, CAbi.stMtimeLen) * 1000000000 + field(p, CAbi.stMtimeNsAt, CAbi.stMtimeNsLen);
535+ i.mode := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 1FFH;
536+ i.dir := fileType(st) = CAbi.sIfdir;
537+ i.file := fileType(st) = CAbi.sIfreg;
517538 return i;
518539 end;
519540
@@ -521,11 +542,11 @@
521542 file, following a symbolic link, and whether path is one; and 0, or
522543 minus an errno. *)
523544 proc info*(path: []U8): (Info, INT);
524-var a: [4096]U8; st: [18]INT; i: Info; isLink: BOOL; (* glibc's struct stat: 144 bytes *)
545+var a: [4096]U8; st: Stat; i: Info; isLink: BOOL;
525546 begin
526547 if not cstr(path, a) then return i, -36; end;
527548 if C.lstat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end;
528- isLink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *)
549+ isLink := fileType(st) = CAbi.sIflnk;
529550 if C.stat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end;
530551 i := fromStat(st);
531552 i.symlink := isLink;
@@ -536,18 +557,18 @@
536557 what it names, so that symlink is true, dir and file false, and the
537558 size the length of what it names. *)
538559 proc linkInfo*(path: []U8): (Info, INT);
539-var a: [4096]U8; st: [18]INT; i: Info;
560+var a: [4096]U8; st: Stat; i: Info;
540561 begin
541562 if not cstr(path, a) then return i, -36; end;
542563 if C.lstat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end;
543564 i := fromStat(st);
544- i.symlink := st[3] & 0F000H = 0A000H; (* S_IFLNK *)
565+ i.symlink := fileType(st) = CAbi.sIflnk;
545566 return i, 0;
546567 end;
547568
548569 (* info of the file open on fd, which is never a link. *)
549570 proc infoOf*(fd: INT): (Info, INT);
550-var st: [18]INT; i: Info;
571+var st: Stat; i: Info;
551572 begin
552573 if C.fstat(I32(fd), ^U8(ADR(st))) # 0 then return i, failed(); end;
553574 return fromStat(st), 0;
@@ -556,22 +577,23 @@
556577 (* The bytes free to an ordinary user on path's file system, and the
557578 bytes in all. *)
558579 proc space*(path: []U8): (I64, I64, INT);
559-var a: [4096]U8; st: [14]INT; (* glibc's struct statvfs: 112 bytes *)
580+var a: [4096]U8; st: [(CAbi.statvfsLen + 7) div 8]U64; p: ^U8; unit: I64;
560581 begin
561582 if not cstr(path, a) then return 0, 0, -36; end;
562- if C.statvfs(ADR(a[0]), ^U8(ADR(st))) # 0 then return 0, 0, failed(); end;
563- return st[4] * st[1], st[2] * st[1], 0; (* f_bavail, f_blocks, in f_frsize *)
583+ p := ^U8(ADR(st));
584+ if C.statvfs(ADR(a[0]), p) # 0 then return 0, 0, failed(); end;
585+ unit := field(p, CAbi.fFrsizeAt, CAbi.fFrsizeLen);
586+ return field(p, CAbi.fBavailAt, CAbi.fBavailLen) * unit, field(p, CAbi.fBlocksAt, CAbi.fBlocksLen) * unit, 0;
564587 end;
565588
566-(* The Kind of a d_type, as getdents64 and readdir give it: DT_REG is
567- 8, DT_DIR 4, DT_LNK 10, DT_UNKNOWN 0, and the rest (pipes, devices,
568- sockets) other things. *)
589+(* The Kind of a d_type, as readdir gives it: DT_REG, DT_DIR, DT_LNK,
590+ DT_UNKNOWN, and the rest (pipes, devices, sockets) other things. *)
569591 proc kindOf(t: U8): Kind;
570592 begin
571- if t = 8 then return isFile;
572- elsif t = 4 then return isDir;
573- elsif t = 10 then return isLink;
574- elsif t = 0 then return unknown;
593+ if t = CAbi.dtReg then return isFile;
594+ elsif t = CAbi.dtDir then return isDir;
595+ elsif t = CAbi.dtLnk then return isLink;
596+ elsif t = CAbi.dtUnknown then return unknown;
575597 end;
576598 return isOther;
577599 end;
@@ -595,8 +617,8 @@
595617 while true do
596618 e := C.readdir(d^.d);
597619 if e = nil then return "", false; end;
598- d^.kind := kindOf(e[DirentType]);
599- e := e + DirentName;
620+ d^.kind := kindOf(e[CAbi.dTypeAt]);
621+ e := e + CAbi.dNameAt;
600622 n := 0;
601623 while e[n] # 0 do INC(n); end;
602624 if (e[0..n] # ".") and (e[0..n] # "..") then return e[0..n], true; end;
@@ -627,13 +649,13 @@
627649 if C.tcgetattr(I32(fd), ADR(saved[0])) # 0 then return false; end;
628650 t := saved;
629651 C.cfmakeraw(ADR(t[0]));
630- return C.tcsetattr(I32(fd), 0, ADR(t[0])) = 0; (* TCSANOW *)
652+ return C.tcsetattr(I32(fd), CAbi.tcsanow, ADR(t[0])) = 0;
631653 end;
632654
633655 (* The terminal on fd as rawMode found it. *)
634656 proc restoreMode*(fd: INT);
635657 begin
636- _ := C.tcsetattr(I32(fd), 0, ADR(saved[0]));
658+ _ := C.tcsetattr(I32(fd), CAbi.tcsanow, ADR(saved[0]));
637659 end;
638660
639661 (* The modes of the terminal on fd, as a shell keeps them for each job;
@@ -649,7 +671,7 @@
649671 is sent: 0, or minus an errno. *)
650672 proc setMode*(fd: INT; m: Mode): INT;
651673 begin
652- if C.tcsetattr(I32(fd), 1, ADR(m.t[0])) # 0 then return failed(); end; (* TCSADRAIN *)
674+ if C.tcsetattr(I32(fd), CAbi.tcsadrain, ADR(m.t[0])) # 0 then return failed(); end;
653675 return 0;
654676 end;
655677
@@ -658,7 +680,7 @@
658680 proc termSize*(fd: INT): (INT, INT);
659681 var w: Winsize;
660682 begin
661- if (C.ioctl(I32(fd), TIOCGWINSZ, ADR(w)) # 0) or (w.cols = 0) or (w.rows = 0) then
683+ if (C.ioctl(I32(fd), CAbi.tiocgwinsz, ADR(w)) # 0) or (w.cols = 0) or (w.rows = 0) then
662684 return 80, 24;
663685 end;
664686 return INT(w.cols), INT(w.rows);
@@ -669,7 +691,7 @@
669691 proc ready*(fd, ms: INT): BOOL;
670692 var p: Pollfd; r: I32;
671693 begin
672- p := {fd: I32(fd), events: 1}; (* POLLIN *)
694+ p := {fd: I32(fd), events: CAbi.pollin};
673695 _ := XCHG(ADR(broke), 0);
674696 repeat r := C.poll(^U8(ADR(p)), 1, I32(ms)); until (r >= 0) or not again();
675697 return r > 0;
@@ -687,13 +709,13 @@
687709 something takes. *)
688710 proc clock*: I64;
689711 begin
690- return time(1); (* CLOCK_MONOTONIC *)
712+ return time(CAbi.clockMonotonic);
691713 end;
692714
693715 (* The time in nanoseconds since 1970-01-01, UTC. *)
694716 proc now*: I64;
695717 begin
696- return time(0); (* CLOCK_REALTIME *)
718+ return time(CAbi.clockRealtime);
697719 end;
698720
699721 (* Waits ms milliseconds. *)
@@ -702,7 +724,8 @@
702724 begin
703725 at := clock() + I64(ms) * 1000000;
704726 t := {sec: at div 1000000000, nsec: at mod 1000000000};
705- repeat until C.clock_nanosleep(1, 1, ^U8(ADR(t)), nil) # 4; (* until t, again after EINTR *)
727+ (* until t, again after EINTR, which it returns, the C library's *)
728+ repeat until C.clock_nanosleep(CAbi.clockMonotonic, CAbi.timerAbstime, ^U8(ADR(t)), nil) # CAbi.eIntr;
706729 end;
707730
708731 (* Fills b with random bytes, from the kernel. *)
@@ -723,13 +746,13 @@
723746 proc socket(family, kind: INT; a: ^U8; size: INT; serving: BOOL): INT;
724747 var fd, r, one: I32; err: INT;
725748 begin
726- fd := C.socket(I32(family), I32(kind) | 80000H, 0); (* SOCK_CLOEXEC *)
749+ fd := C.socket(I32(family), I32(kind) | CAbi.sockCloexec, 0);
727750 if fd < 0 then return failed(); end;
728751 if serving then
729752 one := 1; (* so that a server can start again at once *)
730- _ := C.setsockopt(fd, 1, 2, ^U8(ADR(one)), 4); (* SOL_SOCKET, SO_REUSEADDR *)
753+ _ := C.setsockopt(fd, CAbi.solSocket, CAbi.soReuseaddr, ^U8(ADR(one)), 4);
731754 r := C.bind(fd, a, U32(size));
732- if (r = 0) and (kind = 1) then (* a stream socket takes connections; *)
755+ if (r = 0) and (kind = CAbi.sockStream) then (* a stream socket takes connections; *)
733756 r := C.listen(fd, 4096); (* one of datagrams only receives *)
734757 end;
735758 else
@@ -743,57 +766,85 @@
743766 return INT(fd);
744767 end;
745768
769+(* Gives the address at p the family, and, on a system whose addresses
770+ start with their length, size *)
771+proc setFamily(p: ^U8; family, size: INT);
772+begin
773+ setField(p, CAbi.saFamilyAt, CAbi.saFamilyLen, family);
774+ if CAbi.saLenAt >= 0 then p[CAbi.saLenAt] := U8(size); end;
775+end;
776+
777+(* The family of the address at p *)
778+proc familyOf(p: ^U8): INT;
779+begin
780+ return INT(field(p, CAbi.saFamilyAt, CAbi.saFamilyLen));
781+end;
782+
746783 (* path as a Unix socket's address in a, or false if it is too long. *)
747-proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL;
784+proc unixAddr(path: []U8; a: ^Sockaddr): BOOL;
785+var p: ^U8;
748786 begin
749- if LEN(path) >= LEN(a^.path) then return false; end;
750- a^.family := 1; (* AF_UNIX *)
751- COPY(a^.path, path);
787+ if LEN(path) >= CAbi.sunPathLen then return false; end;
788+ p := ^U8(a);
789+ setFamily(p, CAbi.afUnix, CAbi.sockaddrUnLen);
790+ COPY(p[CAbi.sunPathAt..CAbi.sunPathAt + LEN(path)], path);
752791 return true;
753792 end;
754793
755794 (* A stream socket connected to the Unix socket at path. *)
756795 proc connect*(path: []U8): INT;
757-var a: SockaddrUn;
796+var a: Sockaddr;
758797 begin
759798 if not unixAddr(path, ADR(a)) then return -36; end; (* ENAMETOOLONG *)
760- return socket(1, 1, ^U8(ADR(a)), SIZE(SockaddrUn), false); (* SOCK_STREAM *)
799+ return socket(CAbi.afUnix, CAbi.sockStream, ^U8(ADR(a)), CAbi.sockaddrUnLen, false);
761800 end;
762801
763802 (* A stream socket made at path, which must not be there yet, taking
764803 connections. *)
765804 proc listen*(path: []U8): INT;
766-var a: SockaddrUn;
805+var a: Sockaddr;
767806 begin
768807 if not unixAddr(path, ADR(a)) then return -36; end;
769- return socket(1, 1, ^U8(ADR(a)), SIZE(SockaddrUn), true);
808+ return socket(CAbi.afUnix, CAbi.sockStream, ^U8(ADR(a)), CAbi.sockaddrUnLen, true);
770809 end;
771810
772811 (* addr, an IPv4 or IPv6 address as numbers, and port, in a: the size
773812 of its struct sockaddr_in or sockaddr_in6, or 0 if either is
774813 wrong. *)
775814 proc inetAddr(addr: []U8; port: INT; a: ^Sockaddr): INT;
776-var s: [64]U8; b: [16]U8;
815+var s: [64]U8; b: [16]U8; p: ^U8; at, size: INT;
777816 begin
778817 if (port < 0) or (port > 65535) or not cstr(addr, s) then return 0; end;
779- a^.port[0] := U8(port shr 8); (* in the network's order, the high byte first *)
780- a^.port[1] := U8(port & 255);
818+ p := ^U8(a);
781819 (* into b first, since musl's inet_pton leaves what it read of an
782- address it refuses, and the IPv6 one's first four bytes are its
783- flow, to be zero *)
784- if C.inet_pton(2, ADR(s[0]), ADR(b[0])) = 1 then (* AF_INET *)
785- a^.family := 2;
786- COPY(a^.data[0..4], b[0..4]);
787- return 16;
820+ address it refuses, and the rest of the address is to be zero *)
821+ if C.inet_pton(CAbi.afInet, ADR(s[0]), ADR(b[0])) = 1 then
822+ size := CAbi.sockaddrInLen;
823+ setFamily(p, CAbi.afInet, size);
824+ COPY(p[CAbi.sinAddrAt..CAbi.sinAddrAt + 4], b[0..4]);
825+ at := CAbi.sinPortAt;
826+ elsif C.inet_pton(CAbi.afInet6, ADR(s[0]), ADR(b[0])) = 1 then
827+ size := CAbi.sockaddrIn6Len;
828+ setFamily(p, CAbi.afInet6, size);
829+ COPY(p[CAbi.sin6AddrAt..CAbi.sin6AddrAt + 16], b);
830+ at := CAbi.sin6PortAt;
831+ else
832+ return 0;
788833 end;
789- if C.inet_pton(10, ADR(s[0]), ADR(b[0])) = 1 then (* AF_INET6 *)
790- a^.family := 10;
791- COPY(a^.data[4..20], b);
792- return 28;
793- end;
794- return 0;
834+ p[at] := U8(port shr 8); (* in the network's order, the high byte first *)
835+ p[at + 1] := U8(port & 255);
836+ return size;
795837 end;
796838
839+(* The port in the IPv4 or IPv6 address at p *)
840+proc portOf(p: ^U8): INT;
841+var at: INT;
842+begin
843+ at := CAbi.sinPortAt;
844+ if familyOf(p) = CAbi.afInet6 then at := CAbi.sin6PortAt; end;
845+ return (INT(p[at]) shl 8) | INT(p[at + 1]);
846+end;
847+
797848 (* A socket of kind at addr and port, as socket makes it; -22 (EINVAL)
798849 if they are wrong. *)
799850 proc inet(addr: []U8; port, kind: INT; serving: BOOL): INT;
@@ -801,34 +852,34 @@
801852 begin
802853 size := inetAddr(addr, port, ADR(a));
803854 if size = 0 then return -22; end;
804- return socket(INT(a.family), kind, ^U8(ADR(a)), size, serving);
855+ return socket(familyOf(^U8(ADR(a))), kind, ^U8(ADR(a)), size, serving);
805856 end;
806857
807858 (* A stream socket connected to port at addr, an IPv4 or IPv6 address
808859 as numbers. *)
809860 proc connectTcp*(addr: []U8; port: INT): INT;
810861 begin
811- return inet(addr, port, 1, false); (* SOCK_STREAM *)
862+ return inet(addr, port, CAbi.sockStream, false);
812863 end;
813864
814865 (* A socket of datagrams to and from port at addr. *)
815866 proc connectUdp*(addr: []U8; port: INT): INT;
816867 begin
817- return inet(addr, port, 2, false); (* SOCK_DGRAM *)
868+ return inet(addr, port, CAbi.sockDgram, false);
818869 end;
819870
820871 (* A stream socket taking connections on port at addr; "0.0.0.0" or
821872 "::" is every address the machine has. *)
822873 proc listenTcp*(addr: []U8; port: INT): INT;
823874 begin
824- return inet(addr, port, 1, true);
875+ return inet(addr, port, CAbi.sockStream, true);
825876 end;
826877
827878 (* A socket of datagrams on port at addr, from anyone, for receive and
828879 reply. *)
829880 proc listenUdp*(addr: []U8; port: INT): INT;
830881 begin
831- return inet(addr, port, 2, true);
882+ return inet(addr, port, CAbi.sockDgram, true);
832883 end;
833884
834885 (* The next connection to the socket fd, waiting for one: a socket of
@@ -836,7 +887,7 @@
836887 proc accept*(fd: INT): INT;
837888 var r: I32;
838889 begin
839- r := C.accept4(I32(fd), nil, nil, 80000H); (* SOCK_CLOEXEC *)
890+ r := C.accept4(I32(fd), nil, nil, CAbi.sockCloexec);
840891 if r < 0 then return failed(); end;
841892 return INT(r);
842893 end;
@@ -848,7 +899,7 @@
848899 var r: INT;
849900 begin
850901 if LEN(s) = 0 then return 0; end;
851- r := C.send(I32(fd), ADR(s[0]), UINT(LEN(s)), 4000H); (* MSG_NOSIGNAL, glibc's *)
902+ r := C.send(I32(fd), ADR(s[0]), UINT(LEN(s)), CAbi.msgNosignal);
852903 if r < 0 then return failed(); end;
853904 return r;
854905 end;
@@ -864,13 +915,13 @@
864915 n := 0;
865916 for i := 0 to LEN(reads) - 1 do
866917 if n < LEN(p) then
867- p[n] := {fd: I32(reads[i]), events: 1}; (* POLLIN *)
918+ p[n] := {fd: I32(reads[i]), events: CAbi.pollin};
868919 INC(n);
869920 end;
870921 end;
871922 for i := 0 to LEN(writes) - 1 do
872923 if n < LEN(p) then
873- p[n] := {fd: I32(writes[i]), events: 4}; (* POLLOUT *)
924+ p[n] := {fd: I32(writes[i]), events: CAbi.pollout};
874925 INC(n);
875926 end;
876927 end;
@@ -897,10 +948,10 @@
897948 proc setBlocking*(fd: INT; yes: BOOL): INT;
898949 var flags: I32;
899950 begin
900- flags := C.fcntl(I32(fd), 3, 0); (* F_GETFL *)
951+ flags := C.fcntl(I32(fd), CAbi.fGetfl, 0);
901952 if flags < 0 then return failed(); end;
902- if yes then flags := flags & ~I32(800H); else flags := flags | 800H; end; (* O_NONBLOCK *)
903- if C.fcntl(I32(fd), 4, flags) < 0 then return failed(); end; (* F_SETFL *)
953+ if yes then flags := flags & ~I32(CAbi.oNonblock); else flags := flags | CAbi.oNonblock; end;
954+ if C.fcntl(I32(fd), CAbi.fSetfl, flags) < 0 then return failed(); end;
904955 return 0;
905956 end;
906957
@@ -921,7 +972,7 @@
921972 begin
922973 size := inetAddr(addr, port, ADR(a));
923974 if size = 0 then return -22; end;
924- fd := C.socket(I32(a.family), 1 | 80800H, 0); (* SOCK_STREAM, SOCK_CLOEXEC, SOCK_NONBLOCK *)
975+ fd := C.socket(I32(familyOf(^U8(ADR(a)))), CAbi.sockStream | CAbi.sockCloexec | CAbi.sockNonblock, 0);
925976 if fd < 0 then return failed(); end;
926977 r := 0;
927978 if C.connect(fd, ^U8(ADR(a)), U32(size)) # 0 then r := failed(); end;
@@ -929,8 +980,8 @@
929980 r := -110;
930981 if readyFor([], [INT(fd)], ms) = INT(fd) then
931982 errSize := 4;
932- if C.getsockopt(fd, 1, 4, ^U8(ADR(err)), ADR(errSize)) = 0 then (* SOL_SOCKET, SO_ERROR *)
933- r := -INT(err);
983+ if C.getsockopt(fd, CAbi.solSocket, CAbi.soError, ^U8(ADR(err)), ADR(errSize)) = 0 then
984+ r := linux(-INT(err));
934985 else
935986 r := failed();
936987 end;
@@ -948,7 +999,7 @@
948999 begin
9491000 size := U32(SIZE(Sockaddr));
9501001 if C.getsockname(I32(fd), ^U8(ADR(a)), ADR(size)) # 0 then return failed(); end;
951- return (INT(a.port[0]) shl 8) | INT(a.port[1]);
1002+ return portOf(^U8(ADR(a)));
9521003 end;
9531004
9541005 (* The next datagram to the socket fd, waiting for one: its first
@@ -969,7 +1020,7 @@
9691020 proc reply*(fd: INT; peer: Peer; s: []U8): INT;
9701021 var r: INT;
9711022 begin
972- r := C.sendto(I32(fd), bytes(s), UINT(LEN(s)), 4000H, ^U8(ADR(peer.a)), U32(peer.size)); (* MSG_NOSIGNAL *)
1023+ r := C.sendto(I32(fd), bytes(s), UINT(LEN(s)), CAbi.msgNosignal, ^U8(ADR(peer.a)), U32(peer.size));
9731024 if r < 0 then return failed(); end;
9741025 return r;
9751026 end;
@@ -978,18 +1029,27 @@
9781029 with it the files fds, at most 16, which the program that receives
9791030 them then has open too: the bytes sent, or minus an errno. *)
9801031 proc sendFds*(fd: INT; s: []U8; fds: []INT): INT;
981-var m: Msghdr; v: Iovec; c: Rights; i, r: INT;
1032+var m: Msghdr; v: Iovec; c: Control; mp, cp: ^U8; sent: ^I32; i, r, size: INT;
9821033 begin
983- if (LEN(s) = 0) or (LEN(fds) > LEN(c.fds)) then return -22; end;
1034+ if (LEN(s) = 0) or (LEN(fds) > 16) then return -22; end;
9841035 v := {base: ADR(s[0]), size: LEN(s)};
985- m := {iov: ADR(v), iovLen: 1};
1036+ mp := ^U8(ADR(m));
1037+ setField(mp, CAbi.msgIovAt, SIZE(^U8), INT(ADR(v)));
1038+ setField(mp, CAbi.msgIovlenAt, CAbi.msgIovlenLen, 1);
9861039 if LEN(fds) > 0 then
987- c := {size: 16 + 4 * LEN(fds), level: 1, kind: 1}; (* SOL_SOCKET, SCM_RIGHTS *)
988- for i := 0 to LEN(fds) - 1 do c.fds[i] := I32(fds[i]); end;
989- m.control := ^U8(ADR(c));
990- m.controlSize := (c.size + 7) & ~7; (* CMSG_SPACE: whole quadwords *)
1040+ (* a struct cmsghdr, its level and type ints, then the fds *)
1041+ cp := ^U8(ADR(c));
1042+ size := CAbi.cmsgDataAt + 4 * LEN(fds);
1043+ setField(cp, CAbi.cmsgLenAt, CAbi.cmsgLenLen, size);
1044+ setField(cp, CAbi.cmsgLevelAt, 4, CAbi.solSocket);
1045+ setField(cp, CAbi.cmsgTypeAt, 4, CAbi.scmRights);
1046+ sent := ^I32(cp + CAbi.cmsgDataAt);
1047+ for i := 0 to LEN(fds) - 1 do sent[i] := I32(fds[i]); end;
1048+ setField(mp, CAbi.msgControlAt, SIZE(^U8), INT(cp));
1049+ (* CMSG_SPACE: rounded up to the C library's alignment *)
1050+ setField(mp, CAbi.msgControllenAt, CAbi.msgControllenLen, (size + CAbi.cmsgAlign - 1) & ~(CAbi.cmsgAlign - 1));
9911051 end;
992- r := C.sendmsg(I32(fd), ^U8(ADR(m)), 4000H); (* MSG_NOSIGNAL *)
1052+ r := C.sendmsg(I32(fd), mp, CAbi.msgNosignal);
9931053 if r < 0 then return failed(); end;
9941054 return r;
9951055 end;
@@ -999,20 +1059,28 @@
9991059 rest: the bytes read, or minus an errno, and the files put in fds.
10001060 Each is closed in a program exec starts. *)
10011061 proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT);
1002-var m: Msghdr; v: Iovec; c: Rights; n, k, i: INT;
1062+var m: Msghdr; v: Iovec; c: Control; mp, cp: ^U8; got: ^I32; n, k, i, size: INT;
10031063 begin
10041064 v := {base: bytes(buf), size: LEN(buf)};
1005- m := {iov: ADR(v), iovLen: 1, control: ^U8(ADR(c)), controlSize: SIZE(Rights)};
1006- n := C.recvmsg(I32(fd), ^U8(ADR(m)), 40000000H); (* MSG_CMSG_CLOEXEC *)
1065+ mp := ^U8(ADR(m));
1066+ cp := ^U8(ADR(c));
1067+ setField(mp, CAbi.msgIovAt, SIZE(^U8), INT(ADR(v)));
1068+ setField(mp, CAbi.msgIovlenAt, CAbi.msgIovlenLen, 1);
1069+ setField(mp, CAbi.msgControlAt, SIZE(^U8), INT(cp));
1070+ setField(mp, CAbi.msgControllenAt, CAbi.msgControllenLen, SIZE(Control));
1071+ n := C.recvmsg(I32(fd), mp, CAbi.msgCmsgCloexec);
10071072 if n < 0 then return failed(), 0; end;
10081073 k := 0;
1009- if (m.controlSize >= 16) and (c.level = 1) and (c.kind = 1) then
1010- for i := 0 to (c.size - 16) div 4 - 1 do
1074+ if (field(mp, CAbi.msgControllenAt, CAbi.msgControllenLen) >= CAbi.cmsgDataAt)
1075+ and (field(cp, CAbi.cmsgLevelAt, 4) = CAbi.solSocket) and (field(cp, CAbi.cmsgTypeAt, 4) = CAbi.scmRights) then
1076+ size := INT(field(cp, CAbi.cmsgLenAt, CAbi.cmsgLenLen));
1077+ got := ^I32(cp + CAbi.cmsgDataAt);
1078+ for i := 0 to (size - CAbi.cmsgDataAt) div 4 - 1 do
10111079 if k < LEN(fds) then
1012- fds[k] := INT(c.fds[i]);
1080+ fds[k] := INT(got[i]);
10131081 INC(k);
10141082 else
1015- _ := C.close(c.fds[i]);
1083+ _ := C.close(got[i]);
10161084 end;
10171085 end;
10181086 end;
@@ -1022,10 +1090,13 @@
10221090 (* What errno err, or minus it, means, as the C library's strerror
10231091 says it. *)
10241092 proc errorText*(err: INT): []U8;
1025-var s: ^U8; n: INT;
1093+var s: ^U8; n, i: INT;
10261094 begin
10271095 if err < 0 then err := -err; end;
10281096 if (err < 0) or (err > 7FFFFFFFH) then return "unknown error"; end; (* the smallest INT, or past an int *)
1097+ for i := 0 to LEN(CAbi.errnos) - 2 by 2 do (* Linux's errno as the C library's *)
1098+ if INT(CAbi.errnos[i + 1]) = err then err := INT(CAbi.errnos[i]); break; end;
1099+ end;
10291100 s := C.strerror(I32(err));
10301101 if s = nil then return "unknown error"; end;
10311102 n := 0;
@@ -1087,7 +1158,7 @@
10871158 proc openPty*: INT;
10881159 var fd: I32; err: INT;
10891160 begin
1090- fd := C.posix_openpt(102H); (* O_RDWR, O_NOCTTY: glibc's *)
1161+ fd := C.posix_openpt(CAbi.oRdwr | CAbi.oNoctty);
10911162 if fd < 0 then return failed(); end;
10921163 if (C.grantpt(fd) # 0) or (C.unlockpt(fd) # 0) then
10931164 err := failed();
@@ -1103,7 +1174,7 @@
11031174 var w: Winsize;
11041175 begin
11051176 w := {rows: U16(rows), cols: U16(cols)};
1106- if C.ioctl(I32(fd), TIOCSWINSZ, ADR(w)) # 0 then return failed(); end;
1177+ if C.ioctl(I32(fd), CAbi.tiocswinsz, ADR(w)) # 0 then return failed(); end;
11071178 return 0;
11081179 end;
11091180
@@ -1187,7 +1258,7 @@
11871258 proc pipe*: (INT, INT);
11881259 var fds: [2]I32;
11891260 begin
1190- if C.pipe2(ADR(fds[0]), 80000H) # 0 then return failed(), -1; end; (* O_CLOEXEC, glibc's *)
1261+ if C.pipe2(ADR(fds[0]), CAbi.oCloexec) # 0 then return failed(), -1; end;
11911262 return INT(fds[0]), INT(fds[1]);
11921263 end;
11931264
@@ -1222,35 +1293,47 @@
12221293 return host[0..n], 0;
12231294 end;
12241295
1296+(* The struct timeval at p, in ns *)
1297+proc ns(p: ^U8): I64;
1298+begin
1299+ return field(p, CAbi.tvSecAt, CAbi.tvSecLen) * 1000000000 + field(p, CAbi.tvUsecAt, CAbi.tvUsecLen) * 1000;
1300+end;
1301+
12251302 (* The time on the processor, in ns, that this program has used, or,
12261303 if children, the programs it started and waited for: in the program
12271304 itself, and in the system for it. *)
12281305 proc cpuTime*(children: BOOL): (I64, I64);
1229-var ru: Rusage;
1306+var ru: [64]U64; p: ^U8; (* struct rusage, which cabi checks fits *)
12301307 begin
1231- if children then _ := C.getrusage(-1, ^U8(ADR(ru))); (* RUSAGE_CHILDREN *)
1232- else _ := C.getrusage(0, ^U8(ADR(ru))); (* RUSAGE_SELF *)
1308+ p := ^U8(ADR(ru));
1309+ if children then _ := C.getrusage(CAbi.rusageChildren, p);
1310+ else _ := C.getrusage(CAbi.rusageSelf, p);
12331311 end;
1234- return ru.user.sec * 1000000000 + I64(ru.user.usec) * 1000,
1235- ru.system.sec * 1000000000 + I64(ru.system.usec) * 1000;
1312+ return ns(p + CAbi.ruUtimeAt), ns(p + CAbi.ruStimeAt);
12361313 end;
12371314
12381315 (* The limit which, one of the limit constants: the soft one, which
12391316 this program may raise up to the hard one; -1 for none; and 0, or
12401317 minus an errno. *)
12411318 proc limit*(which: INT): (I64, I64, INT);
1242-var r: [2]I64; (* struct rlimit; RLIM_INFINITY is -1 *)
1319+var r: [2]I64; i: INT; (* struct rlimit *)
12431320 begin
12441321 if C.getrlimit(I32(which), ^U8(ADR(r))) # 0 then return 0, 0, failed(); end;
1322+ for i := 0 to 1 do
1323+ if r[i] = CAbi.rlimInfinity then r[i] := -1; end;
1324+ end;
12451325 return r[0], r[1], 0;
12461326 end;
12471327
12481328 (* Sets the limit which to soft and hard, -1 for none: 0, or minus an
12491329 errno. *)
12501330 proc setLimit*(which: INT; soft, hard: I64): INT;
1251-var r: [2]I64;
1331+var r: [2]I64; i: INT;
12521332 begin
12531333 r := [soft, hard];
1334+ for i := 0 to 1 do
1335+ if r[i] = -1 then r[i] := CAbi.rlimInfinity; end;
1336+ end;
12541337 if C.setrlimit(I32(which), ^U8(ADR(r))) # 0 then return failed(); end;
12551338 return 0;
12561339 end;
@@ -1390,8 +1473,7 @@
13901473 (* Closes every fd from 3 on but those of keep, which stay open in the
13911474 program exec starts. It runs in spawn's child, after the fork, so it
13921475 takes no memory: it closes the runs between the fds kept, the lowest
1393- first, each with one close_range: through syscall, since musl has no
1394- close_range of its own. *)
1476+ first, each with one closeFrom. *)
13951477 proc closeAllBut(keep: []INT);
13961478 var lo, next, i: INT;
13971479 begin
@@ -1402,10 +1484,10 @@
14021484 if (keep[i] >= lo) and ((next < 0) or (keep[i] < next)) then next := keep[i]; end;
14031485 end;
14041486 if next < 0 then
1405- _ := C.syscall(436, lo, 0FFFFFFFFH, 0); (* close_range, the rest *)
1487+ CAbi.closeFrom(lo, -1); (* the rest *)
14061488 else
1407- if next > lo then _ := C.syscall(436, lo, next - 1, 0); end;
1408- _ := C.fcntl(I32(next), 2, 0); (* F_SETFD: kept on exec *)
1489+ if next > lo then CAbi.closeFrom(lo, next - 1); end;
1490+ _ := C.fcntl(I32(next), CAbi.fSetfd, 0); (* kept on exec *)
14091491 lo := next + 1;
14101492 end;
14111493 until next < 0;
@@ -1421,9 +1503,9 @@
14211503 begin
14221504 if p.tty # 0 then
14231505 _ := C.setsid();
1424- fd := C.open(ADR(name[0]), 2); (* O_RDWR *)
1506+ fd := C.open(ADR(name[0]), CAbi.oRdwr);
14251507 if fd < 0 then return failed(); end;
1426- if C.ioctl(fd, TIOCSCTTY, 0) # 0 then return failed(); end;
1508+ if C.ioctl(fd, CAbi.tiocsctty, 0) # 0 then return failed(); end;
14271509 for i := 0 to 2 do _ := C.dup2(fd, I32(i)); end;
14281510 else
14291511 if p.group = -1 then _ := C.setpgid(0, 0);
@@ -1446,7 +1528,7 @@
14461528 proc spawn*(p: Program): INT;
14471529 var
14481530 a, dir, name: [4096]U8; argv: ^^U8; none: ^U8; size, err, i, g: INT; search: []U8;
1449- all, old, empty: [16]U64; dfl: Sigaction; pid: I32; (* sigset_t: 128 bytes *)
1531+ all, old, empty: Sigset; dfl: Sigaction; pid: I32;
14501532 begin
14511533 if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *)
14521534 if p.tty # 0 then
@@ -1463,13 +1545,13 @@
14631545 search := env("PATH"); (* read here, before the fork *)
14641546 if LEN(search) = 0 then search := defaultPath; end;
14651547 for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end;
1466- _ := C.sigprocmask(2, ^U8(ADR(all)), ^U8(ADR(old))); (* SIG_SETMASK *)
1548+ _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(all)), ^U8(ADR(old)));
14671549 pid := C.fork();
14681550 if pid = 0 then
14691551 if takeOn(p, name, dir) # 0 then C._exit(127); end;
14701552 closeAllBut(p.more);
1471- for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end;
1472- _ := C.sigprocmask(2, ^U8(ADR(empty)), nil);
1553+ for i := 1 to CAbi.sigMax do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end;
1554+ _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(empty)), nil);
14731555 run(p.path, search, a, argv, argv + (LEN(p.args) + 1));
14741556 C._exit(127);
14751557 end;
@@ -1481,7 +1563,7 @@
14811563 if p.group # 0 then _ := C.setpgid(pid, I32(g)); end;
14821564 if p.foreground then _ := C.tcsetpgrp(0, I32(g)); end;
14831565 end;
1484- _ := C.sigprocmask(2, ^U8(ADR(old)), nil);
1566+ _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(old)), nil);
14851567 munmap(^U8(argv), size);
14861568 if pid < 0 then return err; end;
14871569 return INT(pid);
@@ -1498,7 +1580,7 @@
14981580 proc exec*(p: Program): INT;
14991581 var
15001582 a, dir, name: [4096]U8; argv: ^^U8; none: ^U8; size, r, i: INT; search: []U8;
1501- all, old, empty: [16]U64; dfl: Sigaction; acts: [65]Sigaction;
1583+ all, old, empty: Sigset; dfl: Sigaction; acts: [CAbi.sigMax + 1]Sigaction;
15021584 begin
15031585 if not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *)
15041586 if p.tty # 0 then
@@ -1524,14 +1606,14 @@
15241606 (* every signal as it is at first and none blocked, as spawn
15251607 gives them, each kept to give back if execve fails *)
15261608 for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end;
1527- _ := C.sigprocmask(2, ^U8(ADR(all)), ^U8(ADR(old))); (* SIG_SETMASK *)
1528- for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), ^U8(ADR(acts[i]))); end;
1529- _ := C.sigprocmask(2, ^U8(ADR(empty)), nil);
1609+ _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(all)), ^U8(ADR(old)));
1610+ for i := 1 to CAbi.sigMax do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), ^U8(ADR(acts[i]))); end;
1611+ _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(empty)), nil);
15301612 _ := C.execve(ADR(a[0]), argv, argv + (LEN(p.args) + 1));
15311613 r := failed();
1532- _ := C.sigprocmask(2, ^U8(ADR(all)), nil);
1533- for i := 1 to 64 do _ := C.sigaction(I32(i), ^U8(ADR(acts[i])), nil); end;
1534- _ := C.sigprocmask(2, ^U8(ADR(old)), nil);
1614+ _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(all)), nil);
1615+ for i := 1 to CAbi.sigMax do _ := C.sigaction(I32(i), ^U8(ADR(acts[i])), nil); end;
1616+ _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(old)), nil);
15351617 end;
15361618 munmap(^U8(argv), size);
15371619 return r;
@@ -1560,12 +1642,12 @@
15601642 its exit status; killed or stopped, with the signal's number. Without
15611643 block it doesn't wait: 0 and running if none has changed. Minus an
15621644 errno, and running, if there is none to wait for. The status is read
1563- as glibc's macros read it. *)
1645+ as C's macros read it, which every Unix encodes alike. *)
15641646 proc wait*(pid: INT; block: BOOL): (INT, Status, INT);
15651647 var status, options, r: I32;
15661648 begin
1567- options := 3; (* WNOHANG, WUNTRACED *)
1568- if block then options := 2; end;
1649+ options := CAbi.wNohang | CAbi.wUntraced;
1650+ if block then options := CAbi.wUntraced; end;
15691651 r := C.waitpid(I32(pid), ADR(status), options);
15701652 if r < 0 then return failed(), running, 0; end;
15711653 if r = 0 then return 0, running, 0; end;
@@ -1587,13 +1669,32 @@
15871669 proc onSignal(sig: I32);
15881670 var e: ^I32; saved: I32; b: [1]U8;
15891671 begin
1590- e := C.__errno_location();
1672+ e := CAbi.errno();
15911673 saved := e^; (* as the code it stopped left it *)
15921674 b[0] := U8(sig);
15931675 _ := C.write(sigWrite, ADR(b[0]), 1);
15941676 e^ := saved;
15951677 end;
15961678
1679+(* A struct sigaction with the handler h, nil for SIG_DFL, and flags,
1680+ and no signal blocked while it runs *)
1681+proc action(h: Handler; flags: INT): Sigaction;
1682+var a: Sigaction; p: ^Handler;
1683+begin
1684+ p := ^Handler(^U8(ADR(a)) + CAbi.saHandlerAt);
1685+ p^ := h;
1686+ setField(^U8(ADR(a)), CAbi.saFlagsAt, CAbi.saFlagsLen, flags);
1687+ return a;
1688+end;
1689+
1690+(* Whether sig is a signal a program may take: not SIGKILL or SIGSTOP,
1691+ which can't be, nor SIGFPE or SIGSEGV, Rt's *)
1692+proc takable(sig: INT): BOOL;
1693+begin
1694+ return (sig >= 1) and (sig <= CAbi.sigMax) and (sig # sigKill) and (sig # sigStop)
1695+ and (sig # CAbi.sigFpe) and (sig # CAbi.sigSegv);
1696+end;
1697+
15971698 (* Catches the signals sigs: they don't do what they would, but give
15981699 their numbers on an fd, which a later catch adds to and gives again;
15991700 or minus an errno. *)
@@ -1601,17 +1702,14 @@
16011702 var fds: [2]I32; act: Sigaction; i: INT;
16021703 begin
16031704 for i := 0 to LEN(sigs) - 1 do
1604- if (sigs[i] < 1) or (sigs[i] > 64) or (sigs[i] = sigKill) or (sigs[i] = sigStop)
1605- or (sigs[i] = 8) or (sigs[i] = 11) then (* SIGFPE and SIGSEGV, Rt's *)
1606- return -22; (* EINVAL *)
1607- end;
1705+ if not takable(sigs[i]) then return -22; end; (* EINVAL *)
16081706 end;
16091707 if sigWrite = 0 then
1610- if C.pipe2(ADR(fds[0]), 80800H) # 0 then return failed(); end; (* O_NONBLOCK, O_CLOEXEC *)
1708+ if C.pipe2(ADR(fds[0]), CAbi.oNonblock | CAbi.oCloexec) # 0 then return failed(); end;
16111709 sigRead := fds[0];
16121710 sigWrite := fds[1];
16131711 end;
1614- act := {handler: onSignal, flags: 10000000H}; (* SA_RESTART *)
1712+ act := action(onSignal, CAbi.saRestart);
16151713 for i := 0 to LEN(sigs) - 1 do
16161714 if C.sigaction(I32(sigs[i]), ^U8(ADR(act)), nil) # 0 then return failed(); end;
16171715 end;
@@ -1631,7 +1729,7 @@
16311729 proc ignore*(sig: INT; yes: BOOL): INT;
16321730 var act: Sigaction;
16331731 begin
1634- if yes then act.handler := Handler(1); end; (* SIG_IGN *)
1732+ if yes then act := action(Handler(1), 0); end; (* SIG_IGN *)
16351733 if C.sigaction(I32(sig), ^U8(ADR(act)), nil) # 0 then return failed(); end;
16361734 return 0;
16371735 end;
@@ -1659,11 +1757,8 @@
16591757 proc handle*(sig: INT): INT;
16601758 var act: Sigaction;
16611759 begin
1662- if (sig < 1) or (sig > 64) or (sig = sigKill) or (sig = sigStop)
1663- or (sig = 8) or (sig = 11) then (* SIGFPE and SIGSEGV, Rt's *)
1664- return -22; (* EINVAL *)
1665- end;
1666- act := {handler: onNoted}; (* no SA_RESTART *)
1760+ if not takable(sig) then return -22; end; (* EINVAL *)
1761+ act := action(onNoted, 0); (* no SA_RESTART *)
16671762 if C.sigaction(I32(sig), ^U8(ADR(act)), nil) # 0 then return failed(); end;
16681763 return 0;
16691764 end;
@@ -1672,7 +1767,7 @@
16721767 it was asked. *)
16731768 proc came*(sig: INT): BOOL;
16741769 begin
1675- if (sig < 1) or (sig > 64) then return false; end;
1770+ if (sig < 1) or (sig > CAbi.sigMax) then return false; end;
16761771 return XCHG(ADR(noted[sig]), 0) = 1;
16771772 end;
16781773
@@ -1779,7 +1874,7 @@
17791874 if (r # child) or (st # exited) or (c # 127) then exit(2); end;
17801875
17811876 sigs := catch([sigChld, sigHup]);
1782- if (sigs < 0) or (catch([sigWinch]) # sigs) or (catch([11]) # -22) or (catch([sigKill]) # -22) then exit(2); end;
1877+ if (sigs < 0) or (catch([sigWinch]) # sigs) or (catch([CAbi.sigSegv]) # -22) or (catch([sigKill]) # -22) then exit(2); end;
17831878 if (caught(sigs) # 0) or (ignore(sigInt, true) # 0) then exit(2); end;
17841879 child := spawn({path: "/bin/sh", args: ["sh", "-c", "kill -INT $$; exit 0"]});
17851880 if not ready(sigs, 5000) or (caught(sigs) # sigChld) then exit(2); end;
@@ -1913,7 +2008,7 @@
19132008 a datagram to a port where no one listens, refused; servers on
19142009 ports the system chooses, of streams and of datagrams. *)
19152010 proc testNet;
1916-var t: I64; l, c, a, u, port, n: INT; x, y: [16]U8; s, s6: Sockaddr; peer: Peer;
2011+var t: I64; l, c, a, u, port, n, i: INT; x, y: [16]U8; s, s6: Sockaddr; p, q: ^U8; peer: Peer;
19172012 begin
19182013 t := clock();
19192014 sleep(20);
@@ -1922,16 +2017,21 @@
19222017 random(x);
19232018 random(y);
19242019 if x[0..16] = y[0..16] then exit(2); end;
1925- if (inetAddr("10.0.0.255", 80, ADR(s)) # 16) or (s.port[0] # 0) or (s.port[1] # 80)
1926- or (s.data[0] # 10) or (s.data[2] # 0) or (s.data[3] # 255) then
2020+ p := ^U8(ADR(s));
2021+ q := ^U8(ADR(s6)) + CAbi.sin6AddrAt;
2022+ if (inetAddr("10.0.0.255", 80, ADR(s)) # CAbi.sockaddrInLen) or (portOf(p) # 80)
2023+ or (p[CAbi.sinAddrAt] # 10) or (p[CAbi.sinAddrAt + 2] # 0) or (p[CAbi.sinAddrAt + 3] # 255) then
19272024 exit(2);
19282025 end;
1929- if (inetAddr("1:aBcd::ff", 258, ADR(s6)) # 28) or (s6.port[0] # 1) or (s6.port[1] # 2)
1930- or (s6.data[0] # 0) or (s6.data[5] # 1) or (s6.data[6] # 0ABH) or (s6.data[7] # 0CDH)
1931- or (s6.data[18] # 0) or (s6.data[19] # 0FFH) then
2026+ if (inetAddr("1:aBcd::ff", 258, ADR(s6)) # CAbi.sockaddrIn6Len) or (portOf(^U8(ADR(s6))) # 258)
2027+ or (q[1] # 1) or (q[2] # 0ABH) or (q[3] # 0CDH) or (q[14] # 0) or (q[15] # 0FFH) then
19322028 exit(2);
19332029 end;
1934- if (inetAddr("::", 0, ADR(s6)) # 28) or (inetAddr("1:2:3:4:5:6:7:8", 0, ADR(s6)) # 28) then exit(2); end;
2030+ q := ^U8(ADR(s6));
2031+ for i := CAbi.sin6PortAt + 2 to CAbi.sin6AddrAt - 1 do (* the flow, zero *)
2032+ if q[i] # 0 then exit(2); end;
2033+ end;
2034+ if (inetAddr("::", 0, ADR(s6)) # CAbi.sockaddrIn6Len) or (inetAddr("1:2:3:4:5:6:7:8", 0, ADR(s6)) # CAbi.sockaddrIn6Len) then exit(2); end;
19352035 if (connectTcp("1.2.3", 80) # -22) or (connectTcp("1.2.3.256", 80) # -22) or (connectTcp("1::2::3", 80) # -22)
19362036 or (connectTcp("1:2:3:4:5:6:7:8:9", 80) # -22) or (connectTcp("1:", 80) # -22)
19372037 or (connectTcp("12345::", 80) # -22) or (connectTcp("::1", 65536) # -22) then

test/err/Clash.err (history) modified +2 -2

@@ -1,5 +1,5 @@
1-lib/Sys.raz:17:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz
2- 17 | import C;
1+lib/Sys.raz:18:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz
2+ 18 | import C, CAbi;
33 | ^
44 help: a program has one module of each name; a symbolic link to one of these files chooses it
55 raz: 1 error

tools/cabi.c (history) added +235 -0

@@ -0,0 +1,235 @@
1+/* cabi: writes lib/CAbi.OS-ARCH.raz, what the C library it is compiled
2+ against fixes for Sys over C (lib/Sys.raz): the numbers of flags,
3+ requests, signals, limits and errnos, where the fields Sys reads and
4+ writes lie in the C library's structs, and the two procedures whose
5+ names differ between systems. Nothing in that file is typed by hand:
6+ it is all this program's output, from the system's own headers.
7+
8+ cc -o build/cabi tools/cabi.c && build/cabi linux-x86_64 > lib/CAbi.linux-x86_64.raz
9+
10+ make check compiles it with cc, and with musl-gcc where that is
11+ installed, and compares what each writes with lib/'s file, so that
12+ one file is known to serve glibc and musl alike.
13+
14+ A field's place is its offset, ...At, and its width, ...Len: the
15+ field's own size for one Sys reads; for one it writes, the room up
16+ to the next field, so that a C library that keeps an int and a pad
17+ where another keeps a size_t gives the same file. Linux's errnos,
18+ which Sys gives on every system, are in the table below, and the
19+ file pairs each with the C library's own; on Linux,
20+ each is checked against the headers before anything is written.
21+
22+ Leaves out: systems but Linux, for errno and closeFrom; a port adds
23+ them where the #error is. */
24+#define _GNU_SOURCE
25+#include <dirent.h>
26+#include <errno.h>
27+#include <fcntl.h>
28+#include <netinet/in.h>
29+#include <poll.h>
30+#include <signal.h>
31+#include <stddef.h>
32+#include <stdio.h>
33+#include <sys/file.h>
34+#include <sys/ioctl.h>
35+#include <sys/mman.h>
36+#include <sys/resource.h>
37+#include <sys/socket.h>
38+#include <sys/stat.h>
39+#include <sys/statvfs.h>
40+#include <sys/un.h>
41+#include <sys/wait.h>
42+#include <termios.h>
43+#include <time.h>
44+#include <unistd.h>
45+
46+/* Sys keeps a terminal's modes in 256 bytes, and struct rusage, whose
47+ size C libraries differ in, in 512 */
48+_Static_assert(sizeof(struct termios) <= 256, "struct termios is larger than Sys.Mode");
49+_Static_assert(sizeof(struct rusage) <= 512, "struct rusage is larger than Sys keeps");
50+
51+#define K(name, value) printf(" %s* = %lld;\n", name, (long long)(value))
52+#define AT(name, type, field) K(name "At", offsetof(type, field))
53+#define READ(name, type, field) \
54+ (AT(name, type, field), K(name "Len", sizeof(((type *)0)->field)))
55+#define WRITE(name, type, field, next) \
56+ (AT(name, type, field), K(name "Len", offsetof(type, next) - offsetof(type, field)))
57+#define LAST(name, type, field) \
58+ (AT(name, type, field), K(name "Len", sizeof(type) - offsetof(type, field)))
59+#define SAY(s) printf(" (* %s *)\n", s)
60+
61+/* the errnos POSIX names, with their numbers on Linux */
62+static const struct { const char *name; int here, onLinux; } errnos[] = {
63+#define E(name, onLinux) {#name, name, onLinux}
64+ E(EPERM, 1), E(ENOENT, 2), E(ESRCH, 3), E(EINTR, 4), E(EIO, 5),
65+ E(ENXIO, 6), E(E2BIG, 7), E(ENOEXEC, 8), E(EBADF, 9), E(ECHILD, 10),
66+ E(EAGAIN, 11), E(ENOMEM, 12), E(EACCES, 13), E(EFAULT, 14),
67+ E(EBUSY, 16), E(EEXIST, 17), E(EXDEV, 18), E(ENODEV, 19),
68+ E(ENOTDIR, 20), E(EISDIR, 21), E(EINVAL, 22), E(ENFILE, 23),
69+ E(EMFILE, 24), E(ENOTTY, 25), E(ETXTBSY, 26), E(EFBIG, 27),
70+ E(ENOSPC, 28), E(ESPIPE, 29), E(EROFS, 30), E(EMLINK, 31),
71+ E(EPIPE, 32), E(EDOM, 33), E(ERANGE, 34), E(EDEADLK, 35),
72+ E(ENAMETOOLONG, 36), E(ENOLCK, 37), E(ENOSYS, 38), E(ENOTEMPTY, 39),
73+ E(ELOOP, 40), E(ENOMSG, 42), E(EIDRM, 43), E(ENOLINK, 67),
74+ E(EPROTO, 71), E(EMULTIHOP, 72), E(EBADMSG, 74), E(EOVERFLOW, 75),
75+ E(EILSEQ, 84), E(ENOTSOCK, 88), E(EDESTADDRREQ, 89), E(EMSGSIZE, 90),
76+ E(EPROTOTYPE, 91), E(ENOPROTOOPT, 92), E(EPROTONOSUPPORT, 93),
77+ E(ENOTSUP, 95), E(EOPNOTSUPP, 95), E(EAFNOSUPPORT, 97),
78+ E(EADDRINUSE, 98), E(EADDRNOTAVAIL, 99), E(ENETDOWN, 100),
79+ E(ENETUNREACH, 101), E(ENETRESET, 102), E(ECONNABORTED, 103),
80+ E(ECONNRESET, 104), E(ENOBUFS, 105), E(EISCONN, 106),
81+ E(ENOTCONN, 107), E(ETIMEDOUT, 110), E(ECONNREFUSED, 111),
82+ E(EHOSTUNREACH, 113), E(EALREADY, 114), E(EINPROGRESS, 115),
83+ E(ESTALE, 116), E(EDQUOT, 122), E(ECANCELED, 125),
84+ E(EOWNERDEAD, 130), E(ENOTRECOVERABLE, 131),
85+#undef E
86+};
87+
88+int main(int argc, char **argv) {
89+ const char *target = argc > 1 ? argv[1] : "linux-x86_64";
90+ int i, n;
91+
92+#ifdef __linux__
93+ for (i = 0; i < (int)(sizeof errnos / sizeof errnos[0]); i++)
94+ if (errnos[i].here != errnos[i].onLinux) {
95+ fprintf(stderr, "cabi: %s is %d here, not Linux's %d\n",
96+ errnos[i].name, errnos[i].here, errnos[i].onLinux);
97+ return 1;
98+ }
99+#endif
100+ printf("(* CAbi: what the C library fixes for Sys over C on %s: the\n"
101+ " numbers of its flags, requests, signals and limits, where the\n"
102+ " fields of its structs lie, an At the offset and a Len the width,\n"
103+ " its errnos beside Linux's, and errno and closeFrom.\n\n"
104+ " Written by tools/cabi.c, from the system's own headers; not to be\n"
105+ " edited, but written again. *)\n", target);
106+ printf("module CAbi;\n\nconst\n");
107+
108+ SAY("open's flags, fcntl's requests, seek, flock, mmap, access");
109+ K("oRdonly", O_RDONLY); K("oWronly", O_WRONLY); K("oRdwr", O_RDWR);
110+ K("oCreat", O_CREAT); K("oExcl", O_EXCL); K("oTrunc", O_TRUNC);
111+ K("oAppend", O_APPEND); K("oNonblock", O_NONBLOCK);
112+ K("oCloexec", O_CLOEXEC); K("oNoctty", O_NOCTTY);
113+ K("fGetfd", F_GETFD); K("fSetfd", F_SETFD); K("fGetfl", F_GETFL);
114+ K("fSetfl", F_SETFL); K("fDupfdCloexec", F_DUPFD_CLOEXEC);
115+ K("fdCloexec", FD_CLOEXEC);
116+ K("seekSet", SEEK_SET); K("seekCur", SEEK_CUR); K("seekEnd", SEEK_END);
117+ K("lockEx", LOCK_EX); K("lockNb", LOCK_NB);
118+ K("protRead", PROT_READ); K("protWrite", PROT_WRITE);
119+ K("mapShared", MAP_SHARED); K("mapPrivate", MAP_PRIVATE);
120+ K("mapAnonymous", MAP_ANONYMOUS); K("mfdCloexec", MFD_CLOEXEC);
121+ K("fOk", F_OK); K("rOk", R_OK); K("wOk", W_OK); K("xOk", X_OK);
122+
123+ SAY("struct stat, statvfs and dirent");
124+ K("statLen", sizeof(struct stat));
125+ READ("stDev", struct stat, st_dev); READ("stIno", struct stat, st_ino);
126+ READ("stNlink", struct stat, st_nlink); READ("stMode", struct stat, st_mode);
127+ READ("stSize", struct stat, st_size);
128+ READ("stMtime", struct stat, st_mtim.tv_sec);
129+ READ("stMtimeNs", struct stat, st_mtim.tv_nsec);
130+ K("sIfmt", S_IFMT); K("sIfdir", S_IFDIR); K("sIfreg", S_IFREG);
131+ K("sIflnk", S_IFLNK);
132+ K("statvfsLen", sizeof(struct statvfs));
133+ READ("fFrsize", struct statvfs, f_frsize);
134+ READ("fBlocks", struct statvfs, f_blocks);
135+ READ("fBavail", struct statvfs, f_bavail);
136+ AT("dName", struct dirent, d_name); AT("dType", struct dirent, d_type);
137+ K("dtUnknown", DT_UNKNOWN); K("dtDir", DT_DIR); K("dtReg", DT_REG);
138+ K("dtLnk", DT_LNK);
139+
140+ SAY("the terminal, poll and the clocks");
141+ K("tcsanow", TCSANOW); K("tcsadrain", TCSADRAIN);
142+ K("tiocgwinsz", TIOCGWINSZ); K("tiocswinsz", TIOCSWINSZ);
143+ K("tiocsctty", TIOCSCTTY);
144+ K("pollin", POLLIN); K("pollout", POLLOUT);
145+ K("clockRealtime", CLOCK_REALTIME); K("clockMonotonic", CLOCK_MONOTONIC);
146+ K("timerAbstime", TIMER_ABSTIME);
147+
148+ SAY("sockets, their addresses, and a message with fds");
149+ K("afUnix", AF_UNIX); K("afInet", AF_INET); K("afInet6", AF_INET6);
150+ K("sockStream", SOCK_STREAM); K("sockDgram", SOCK_DGRAM);
151+ K("sockCloexec", SOCK_CLOEXEC); K("sockNonblock", SOCK_NONBLOCK);
152+ K("solSocket", SOL_SOCKET); K("soReuseaddr", SO_REUSEADDR);
153+ K("soError", SO_ERROR); K("msgNosignal", MSG_NOSIGNAL);
154+ K("msgCmsgCloexec", MSG_CMSG_CLOEXEC); K("scmRights", SCM_RIGHTS);
155+ K("sockaddrLen", sizeof(struct sockaddr_storage));
156+ READ("saFamily", struct sockaddr, sa_family);
157+#ifdef SIN6_LEN
158+ AT("saLen", struct sockaddr, sa_len); /* the BSDs: the address's length first */
159+#else
160+ K("saLenAt", -1);
161+#endif
162+ K("sockaddrUnLen", sizeof(struct sockaddr_un));
163+ READ("sunPath", struct sockaddr_un, sun_path);
164+ K("sockaddrInLen", sizeof(struct sockaddr_in));
165+ AT("sinPort", struct sockaddr_in, sin_port);
166+ AT("sinAddr", struct sockaddr_in, sin_addr);
167+ K("sockaddrIn6Len", sizeof(struct sockaddr_in6));
168+ AT("sin6Port", struct sockaddr_in6, sin6_port);
169+ AT("sin6Addr", struct sockaddr_in6, sin6_addr);
170+ K("msghdrLen", sizeof(struct msghdr));
171+ AT("msgIov", struct msghdr, msg_iov);
172+ WRITE("msgIovlen", struct msghdr, msg_iovlen, msg_control);
173+ AT("msgControl", struct msghdr, msg_control);
174+ WRITE("msgControllen", struct msghdr, msg_controllen, msg_flags);
175+ WRITE("cmsgLen", struct cmsghdr, cmsg_len, cmsg_level);
176+ AT("cmsgLevel", struct cmsghdr, cmsg_level);
177+ AT("cmsgType", struct cmsghdr, cmsg_type);
178+ K("cmsgDataAt", CMSG_LEN(0));
179+ K("cmsgAlign", CMSG_ALIGN(1));
180+
181+ SAY("signals, sigaction and sigprocmask");
182+ K("sigHup", SIGHUP); K("sigInt", SIGINT); K("sigQuit", SIGQUIT);
183+ K("sigFpe", SIGFPE); K("sigKill", SIGKILL); K("sigUsr1", SIGUSR1);
184+ K("sigSegv", SIGSEGV); K("sigUsr2", SIGUSR2); K("sigPipe", SIGPIPE);
185+ K("sigAlrm", SIGALRM); K("sigTerm", SIGTERM); K("sigChld", SIGCHLD);
186+ K("sigCont", SIGCONT); K("sigStop", SIGSTOP); K("sigTstp", SIGTSTP);
187+ K("sigTtin", SIGTTIN); K("sigTtou", SIGTTOU); K("sigWinch", SIGWINCH);
188+ K("sigMax", NSIG - 1);
189+ K("sigactionLen", sizeof(struct sigaction));
190+ AT("saHandler", struct sigaction, sa_handler);
191+ AT("saMask", struct sigaction, sa_mask);
192+ READ("saFlags", struct sigaction, sa_flags);
193+ K("sigsetLen", sizeof(sigset_t));
194+ K("saRestart", SA_RESTART); K("sigSetmask", SIG_SETMASK);
195+
196+ SAY("limits, time used, and wait");
197+ K("rlimitCpu", RLIMIT_CPU); K("rlimitFsize", RLIMIT_FSIZE);
198+ K("rlimitData", RLIMIT_DATA); K("rlimitStack", RLIMIT_STACK);
199+ K("rlimitCore", RLIMIT_CORE); K("rlimitNproc", RLIMIT_NPROC);
200+ K("rlimitNofile", RLIMIT_NOFILE); K("rlimitAs", RLIMIT_AS);
201+ K("rlimInfinity", RLIM_INFINITY);
202+ K("rusageSelf", RUSAGE_SELF); K("rusageChildren", RUSAGE_CHILDREN);
203+ AT("ruUtime", struct rusage, ru_utime); AT("ruStime", struct rusage, ru_stime);
204+ READ("tvSec", struct timeval, tv_sec); READ("tvUsec", struct timeval, tv_usec);
205+ K("wNohang", WNOHANG); K("wUntraced", WUNTRACED);
206+
207+ SAY("errnos: EINTR; and each POSIX names, the C library's number, then Linux's");
208+ K("eIntr", EINTR);
209+ n = sizeof errnos / sizeof errnos[0];
210+ printf(" errnos*: [%d]I32 = [", 2 * n);
211+ for (i = 0; i < n; i++)
212+ printf("%s%s%d, %d", i ? "," : "", i % 6 ? " " : "\n ", errnos[i].here, errnos[i].onLinux);
213+ printf("];\n\n");
214+
215+#ifdef __linux__
216+ printf("extern proc __errno_location: ^I32;\n"
217+ "extern proc syscall(n: INT; ...): INT;\n\n"
218+ "(* errno, this thread's *)\n"
219+ "proc errno*: ^I32;\n"
220+ "begin\n"
221+ " return __errno_location();\n"
222+ "end;\n\n"
223+ "(* Closes the fds from lo to hi, or every one from lo if hi < 0: by\n"
224+ " close_range's system call, since musl has no close_range. *)\n"
225+ "proc closeFrom*(lo, hi: INT);\n"
226+ "begin\n"
227+ " if hi < 0 then hi := 0FFFFFFFFH; end;\n"
228+ " _ := syscall(436, lo, hi, 0);\n"
229+ "end;\n\n");
230+#else
231+#error "a CAbi for this system needs its errno and closeFrom: add them here"
232+#endif
233+ printf("end CAbi.\n");
234+ return 0;
235+}