raz / lib / CAbi.linux-x86_64.raz

304 lines · 8.0 KiB · raw · history · last change 89524972

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 the procedures that differ.
5
6 Written by tools/cabi.c, from the system's own headers; not to be
7 edited, but written again. *)
8module CAbi;
9import SYSTEM;
10
11const
12 (* open's flags, fcntl's requests, seek, flock, mmap, access *)
13 oRdonly* = 0;
14 oWronly* = 1;
15 oRdwr* = 2;
16 oCreat* = 64;
17 oExcl* = 128;
18 oTrunc* = 512;
19 oAppend* = 1024;
20 oNonblock* = 2048;
21 oCloexec* = 524288;
22 oNoctty* = 256;
23 fGetfd* = 1;
24 fSetfd* = 2;
25 fGetfl* = 3;
26 fSetfl* = 4;
27 fDupfdCloexec* = 1030;
28 fdCloexec* = 1;
29 seekSet* = 0;
30 seekCur* = 1;
31 seekEnd* = 2;
32 lockEx* = 2;
33 lockSh* = 1;
34 lockNb* = 4;
35 protRead* = 1;
36 protWrite* = 2;
37 mapShared* = 1;
38 mapPrivate* = 2;
39 mapAnonymous* = 32;
40 mfdCloexec* = 1;
41 atFdcwd* = -100;
42 fOk* = 0;
43 rOk* = 4;
44 wOk* = 2;
45 xOk* = 1;
46 (* struct stat, statvfs and dirent *)
47 statLen* = 144;
48 stDevAt* = 0;
49 stDevLen* = 8;
50 stInoAt* = 8;
51 stInoLen* = 8;
52 stNlinkAt* = 16;
53 stNlinkLen* = 8;
54 stModeAt* = 24;
55 stModeLen* = 4;
56 stSizeAt* = 48;
57 stSizeLen* = 8;
58 stUidAt* = 28;
59 stUidLen* = 4;
60 stGidAt* = 32;
61 stGidLen* = 4;
62 stMtimeAt* = 88;
63 stMtimeLen* = 8;
64 stMtimeNsAt* = 96;
65 stMtimeNsLen* = 8;
66 sIfmt* = 61440;
67 sIfdir* = 16384;
68 sIfreg* = 32768;
69 sIflnk* = 40960;
70 statvfsLen* = 112;
71 fFrsizeAt* = 8;
72 fFrsizeLen* = 8;
73 fBlocksAt* = 16;
74 fBlocksLen* = 8;
75 fBavailAt* = 32;
76 fBavailLen* = 8;
77 dNameAt* = 19;
78 dTypeAt* = 18;
79 dtUnknown* = 0;
80 dtDir* = 4;
81 dtReg* = 8;
82 dtLnk* = 10;
83 (* the terminal, poll and the clocks *)
84 tcsanow* = 0;
85 tcsadrain* = 1;
86 tiocgwinsz* = 21523;
87 tiocswinsz* = 21524;
88 tiocsctty* = 21518;
89 ptyHeld* = 0;
90 pollin* = 1;
91 pollout* = 4;
92 clockRealtime* = 0;
93 clockMonotonic* = 1;
94 timerAbstime* = 1;
95 (* sockets, their addresses, and a message with fds *)
96 afUnix* = 1;
97 afInet* = 2;
98 afInet6* = 10;
99 sockStream* = 1;
100 sockDgram* = 2;
101 sockCloexec* = 524288;
102 sockNonblock* = 2048;
103 solSocket* = 1;
104 soReuseaddr* = 2;
105 soError* = 4;
106 soPeercred* = 17;
107 shutWr* = 1;
108 msgNosignal* = 16384;
109 msgDontwait* = 64;
110 msgCmsgCloexec* = 1073741824;
111 scmRights* = 1;
112 sockaddrLen* = 128;
113 saFamilyAt* = 0;
114 saFamilyLen* = 2;
115 saLenAt* = -1;
116 sockaddrUnLen* = 110;
117 sunPathAt* = 2;
118 sunPathLen* = 108;
119 unixAbstract* = 1;
120 sockaddrInLen* = 16;
121 sinPortAt* = 2;
122 sinAddrAt* = 4;
123 sockaddrIn6Len* = 28;
124 sin6PortAt* = 2;
125 sin6AddrAt* = 8;
126 ucPidAt* = 0;
127 ucPidLen* = 4;
128 ucUidAt* = 4;
129 ucUidLen* = 4;
130 msghdrLen* = 56;
131 msgIovAt* = 16;
132 msgIovlenAt* = 24;
133 msgIovlenLen* = 8;
134 msgControlAt* = 32;
135 msgControllenAt* = 40;
136 msgControllenLen* = 8;
137 cmsgLenAt* = 0;
138 cmsgLenLen* = 8;
139 cmsgLevelAt* = 8;
140 cmsgTypeAt* = 12;
141 cmsgDataAt* = 16;
142 cmsgAlign* = 8;
143 (* signals, sigaction and sigprocmask *)
144 sigHup* = 1;
145 sigInt* = 2;
146 sigQuit* = 3;
147 sigFpe* = 8;
148 sigKill* = 9;
149 sigUsr1* = 10;
150 sigSegv* = 11;
151 sigUsr2* = 12;
152 sigPipe* = 13;
153 sigAlrm* = 14;
154 sigTerm* = 15;
155 sigChld* = 17;
156 sigCont* = 18;
157 sigStop* = 19;
158 sigTstp* = 20;
159 sigTtin* = 21;
160 sigTtou* = 22;
161 sigWinch* = 28;
162 sigMax* = 64;
163 sigactionLen* = 152;
164 saHandlerAt* = 0;
165 saMaskAt* = 8;
166 saFlagsAt* = 136;
167 saFlagsLen* = 4;
168 sigsetLen* = 128;
169 saRestart* = 268435456;
170 sigSetmask* = 2;
171 (* limits, time used, and wait *)
172 rlimitCpu* = 0;
173 rlimitFsize* = 1;
174 rlimitData* = 2;
175 rlimitStack* = 3;
176 rlimitCore* = 4;
177 rlimitNproc* = 6;
178 rlimitNofile* = 7;
179 rlimitAs* = 9;
180 rlimInfinity* = -1;
181 rusageSelf* = 0;
182 rusageChildren* = -1;
183 ruUtimeAt* = 0;
184 ruStimeAt* = 16;
185 tvSecAt* = 0;
186 tvSecLen* = 8;
187 tvUsecAt* = 8;
188 tvUsecLen* = 8;
189 wNohang* = 1;
190 wUntraced* = 2;
191 (* errnos: EINTR; and each POSIX names, the C library's number, then Linux's *)
192 eIntr* = 4;
193 errnos*: [152]I32 = [
194 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
195 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12,
196 13, 13, 14, 14, 16, 16, 17, 17, 18, 18, 19, 19,
197 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25,
198 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
199 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37,
200 38, 38, 39, 39, 40, 40, 42, 42, 43, 43, 67, 67,
201 71, 71, 72, 72, 74, 74, 75, 75, 84, 84, 88, 88,
202 89, 89, 90, 90, 91, 91, 92, 92, 93, 93, 95, 95,
203 95, 95, 97, 97, 98, 98, 99, 99, 100, 100, 101, 101,
204 102, 102, 103, 103, 104, 104, 105, 105, 106, 106, 107, 107,
205 110, 110, 111, 111, 113, 113, 114, 114, 115, 115, 116, 116,
206 122, 122, 125, 125, 130, 130, 131, 131];
207
208extern proc __errno_location: ^I32;
209extern proc syscall(n: INT; ...): INT;
210extern proc pipe2(fds: ^I32; flags: I32): I32;
211extern proc accept4(fd: I32; addr: ^U8; len: ^U32; flags: I32): I32;
212extern proc getrandom(buf: ^U8; n: UINT; flags: U32): INT;
213extern proc memfd_create(name: ^U8; flags: U32): I32;
214extern proc clock_nanosleep(clock, flags: I32; t, left: ^U8): I32;
215extern proc sched_getaffinity(pid: I32; size: UINT; mask: ^U8): I32;
216extern proc getsockopt(fd, level, name: I32; value: ^U8; len: ^U32): I32;
217extern proc send(fd: I32; buf: ^U8; n: UINT; flags: I32): INT;
218
219(* errno, this thread's *)
220proc errno*: ^I32;
221begin
222 return __errno_location();
223end;
224
225(* Closes the fds from lo to hi, or every one from lo if hi < 0: by
226 close_range's system call, since musl has no close_range. *)
227proc closeFrom*(lo, hi: INT);
228begin
229 if hi < 0 then hi := 0FFFFFFFFH; end;
230 _ := syscall(436, lo, hi, 0);
231end;
232
233(* A pipe, its two fds put at fds, taking flags, oCloexec and
234 oNonblock, at once: 0, or -1 with errno set, as C's calls are. *)
235proc makePipe*(fds: ^I32; flags: I32): I32;
236begin
237 return pipe2(fds, flags);
238end;
239
240(* The next connection to the socket fd, closed on exec; or -1. *)
241proc accepted*(fd: I32): I32;
242begin
243 return accept4(fd, nil, nil, sockCloexec);
244end;
245
246(* The kernel's random bytes, at most n, put at buf: how many, or -1. *)
247proc random*(buf: ^U8; n: UINT): INT;
248begin
249 return getrandom(buf, n, 0);
250end;
251
252(* A file in memory, of no bytes, with no name, closed on exec: its
253 fd, or -1. *)
254proc memFile*: I32;
255begin
256 return memfd_create(SYSTEM.PTR("raz\0"), mfdCloexec); (* the name /proc shows *)
257end;
258
259(* Waits until t, a struct timespec of the monotonic clock, again
260 after EINTR. *)
261proc sleepUntil*(t: ^U8);
262begin
263 repeat until clock_nanosleep(clockMonotonic, timerAbstime, t, nil) # eIntr;
264end;
265
266(* The number of processors this program may run on, or 0 if the
267 system doesn't say. *)
268proc processors*: INT;
269var mask: [1024]U8; i, b, count: INT;
270begin
271 for i := 0 to LEN(mask) - 1 do mask[i] := 0; end; (* the C library fills in as many bytes as the system has processors' bits *)
272 if sched_getaffinity(0, UINT(LEN(mask)), SYSTEM.ADR(mask[0])) # 0 then return 0; end;
273 count := 0;
274 for i := 0 to LEN(mask) - 1 do
275 for b := 0 to 7 do
276 if (INT(mask[i]) shr b) & 1 = 1 then INC(count); end;
277 end;
278 end;
279 return count;
280end;
281
282(* The process and the user at the other end of the Unix socket fd,
283 as the system recorded them when it connected, and 0; or -1. *)
284proc peer*(fd: I32; var pid, uid: INT): I32;
285var cred: [16]U64; size: U32; p: ^U8; q: ^U32;
286begin
287 p := ^U8(SYSTEM.ADR(cred[0]));
288 size := U32(SIZE([16]U64));
289 if getsockopt(fd, solSocket, soPeercred, p, SYSTEM.ADR(size)) # 0 then return -1; end;
290 q := ^U32(p + ucPidAt);
291 pid := INT(I32(q^));
292 q := ^U32(p + ucUidAt);
293 uid := INT(q^);
294 return 0;
295end;
296
297(* Sends n bytes at buf on the socket fd, without waiting, and without
298 SIGPIPE where the other end is gone: how many were sent, or -1. *)
299proc sendNow*(fd: I32; buf: ^U8; n: UINT): INT;
300begin
301 return send(fd, buf, n, msgNosignal | msgDontwait);
302end;
303
304end CAbi.