raz / tools / cabi.c

545 lines · 25.0 KiB · raw · history · last change 89524972

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 procedures that differ
5 between systems: errno and closeFrom, and the calls Linux has and
6 another system makes otherwise (makePipe, accepted, random, memFile,
7 sleepUntil, processors, peer and sendNow). Nothing in that file is typed by hand:
8 it is all this program's output, from the system's own headers.
9
10 cc -o build/cabi tools/cabi.c && build/cabi linux-x86_64 > lib/CAbi.linux-x86_64.raz
11
12 make check compiles it with cc, and with musl-gcc where that is
13 installed, and compares what each writes with lib/'s file, so that
14 one file is known to serve glibc and musl alike.
15
16 A field's place is its offset, ...At, and its width, ...Len: the
17 field's own size for one Sys reads; for one it writes, the room up
18 to the next field, so that a C library that keeps an int and a pad
19 where another keeps a size_t gives the same file. Linux's errnos,
20 which Sys gives on every system, are in the table below, and the
21 file pairs each with the C library's own; on Linux,
22 each is checked against the headers before anything is written.
23
24 What a system hasn't, Linux's alone, is given a value that keeps Sys
25 over C compiling there, and that it must not count on: a flag 0, no
26 flag; an option -1, as saLenAt is -1 where a sockaddr has no sa_len.
27 On macOS (darwin-arm64), st_mtim is st_mtimespec, a peer's
28 credentials are struct xucred, which has no pid (0 bytes at 0), and
29 the procedures are its own: no pipe2, accept4 or SOCK_CLOEXEC, so
30 fcntl after; getentropy for getrandom; a shared memory object for
31 memfd_create; nanosleep for clock_nanosleep; sysconf for
32 sched_getaffinity; LOCAL_PEERCRED and LOCAL_PEERPID for SO_PEERCRED;
33 and a socket made nonblocking for a send, whose MSG_DONTWAIT macOS
34 doesn't keep on a Unix socket.
35
36 cc -o build/cabi tools/cabi.c && build/cabi darwin-arm64 > lib/CAbi.darwin-arm64.raz
37
38 Leaves out: systems but Linux and macOS, for the procedures; a
39 port adds them where the #error is. */
40#define _GNU_SOURCE
41#include <dirent.h>
42#include <errno.h>
43#include <fcntl.h>
44#include <netinet/in.h>
45#include <poll.h>
46#include <signal.h>
47#include <stddef.h>
48#include <stdio.h>
49#include <sys/file.h>
50#include <sys/ioctl.h>
51#include <sys/mman.h>
52#include <sys/resource.h>
53#include <sys/socket.h>
54#include <sys/stat.h>
55#include <sys/statvfs.h>
56#include <sys/un.h>
57#include <sys/wait.h>
58#include <termios.h>
59#include <time.h>
60#include <unistd.h>
61
62#ifndef MFD_CLOEXEC
63#define MFD_CLOEXEC 0
64#endif
65#ifndef TIMER_ABSTIME
66#define TIMER_ABSTIME 0
67#endif
68#ifndef SOCK_CLOEXEC
69#define SOCK_CLOEXEC 0
70#endif
71#ifndef SOCK_NONBLOCK
72#define SOCK_NONBLOCK 0
73#endif
74#ifndef MSG_CMSG_CLOEXEC
75#define MSG_CMSG_CLOEXEC 0
76#endif
77#ifndef SO_PEERCRED
78#define SO_PEERCRED -1
79#endif
80#ifdef __APPLE__
81#include <sys/ucred.h>
82#define st_mtim st_mtimespec
83#define CMSG_ALIGN(n) __DARWIN_ALIGN32(n)
84#endif
85
86/* Sys keeps a terminal's modes in 256 bytes, and struct rusage, whose
87 size C libraries differ in, in 512 */
88_Static_assert(sizeof(struct termios) <= 256, "struct termios is larger than Sys.Mode");
89_Static_assert(sizeof(struct rusage) <= 512, "struct rusage is larger than Sys keeps");
90
91#define K(name, value) printf(" %s* = %lld;\n", name, (long long)(value))
92#define AT(name, type, field) K(name "At", offsetof(type, field))
93#define READ(name, type, field) \
94 (AT(name, type, field), K(name "Len", sizeof(((type *)0)->field)))
95#define WRITE(name, type, field, next) \
96 (AT(name, type, field), K(name "Len", offsetof(type, next) - offsetof(type, field)))
97#define LAST(name, type, field) \
98 (AT(name, type, field), K(name "Len", sizeof(type) - offsetof(type, field)))
99#define SAY(s) printf(" (* %s *)\n", s)
100
101/* the errnos POSIX names, with their numbers on Linux */
102static const struct { const char *name; int here, onLinux; } errnos[] = {
103#define E(name, onLinux) {#name, name, onLinux}
104 E(EPERM, 1), E(ENOENT, 2), E(ESRCH, 3), E(EINTR, 4), E(EIO, 5),
105 E(ENXIO, 6), E(E2BIG, 7), E(ENOEXEC, 8), E(EBADF, 9), E(ECHILD, 10),
106 E(EAGAIN, 11), E(ENOMEM, 12), E(EACCES, 13), E(EFAULT, 14),
107 E(EBUSY, 16), E(EEXIST, 17), E(EXDEV, 18), E(ENODEV, 19),
108 E(ENOTDIR, 20), E(EISDIR, 21), E(EINVAL, 22), E(ENFILE, 23),
109 E(EMFILE, 24), E(ENOTTY, 25), E(ETXTBSY, 26), E(EFBIG, 27),
110 E(ENOSPC, 28), E(ESPIPE, 29), E(EROFS, 30), E(EMLINK, 31),
111 E(EPIPE, 32), E(EDOM, 33), E(ERANGE, 34), E(EDEADLK, 35),
112 E(ENAMETOOLONG, 36), E(ENOLCK, 37), E(ENOSYS, 38), E(ENOTEMPTY, 39),
113 E(ELOOP, 40), E(ENOMSG, 42), E(EIDRM, 43), E(ENOLINK, 67),
114 E(EPROTO, 71), E(EMULTIHOP, 72), E(EBADMSG, 74), E(EOVERFLOW, 75),
115 E(EILSEQ, 84), E(ENOTSOCK, 88), E(EDESTADDRREQ, 89), E(EMSGSIZE, 90),
116 E(EPROTOTYPE, 91), E(ENOPROTOOPT, 92), E(EPROTONOSUPPORT, 93),
117 E(ENOTSUP, 95), E(EOPNOTSUPP, 95), E(EAFNOSUPPORT, 97),
118 E(EADDRINUSE, 98), E(EADDRNOTAVAIL, 99), E(ENETDOWN, 100),
119 E(ENETUNREACH, 101), E(ENETRESET, 102), E(ECONNABORTED, 103),
120 E(ECONNRESET, 104), E(ENOBUFS, 105), E(EISCONN, 106),
121 E(ENOTCONN, 107), E(ETIMEDOUT, 110), E(ECONNREFUSED, 111),
122 E(EHOSTUNREACH, 113), E(EALREADY, 114), E(EINPROGRESS, 115),
123 E(ESTALE, 116), E(EDQUOT, 122), E(ECANCELED, 125),
124 E(EOWNERDEAD, 130), E(ENOTRECOVERABLE, 131),
125#undef E
126};
127
128int main(int argc, char **argv) {
129 const char *target = argc > 1 ? argv[1] : "linux-x86_64";
130 int i, n;
131
132#ifdef __linux__
133 for (i = 0; i < (int)(sizeof errnos / sizeof errnos[0]); i++)
134 if (errnos[i].here != errnos[i].onLinux) {
135 fprintf(stderr, "cabi: %s is %d here, not Linux's %d\n",
136 errnos[i].name, errnos[i].here, errnos[i].onLinux);
137 return 1;
138 }
139#endif
140 printf("(* CAbi: what the C library fixes for Sys over C on %s: the\n"
141 " numbers of its flags, requests, signals and limits, where the\n"
142 " fields of its structs lie, an At the offset and a Len the width,\n"
143 " its errnos beside Linux's, and the procedures that differ.\n\n"
144 " Written by tools/cabi.c, from the system's own headers; not to be\n"
145 " edited, but written again. *)\n", target);
146 printf("module CAbi;\nimport SYSTEM;\n\nconst\n");
147
148 SAY("open's flags, fcntl's requests, seek, flock, mmap, access");
149 K("oRdonly", O_RDONLY); K("oWronly", O_WRONLY); K("oRdwr", O_RDWR);
150 K("oCreat", O_CREAT); K("oExcl", O_EXCL); K("oTrunc", O_TRUNC);
151 K("oAppend", O_APPEND); K("oNonblock", O_NONBLOCK);
152 K("oCloexec", O_CLOEXEC); K("oNoctty", O_NOCTTY);
153 K("fGetfd", F_GETFD); K("fSetfd", F_SETFD); K("fGetfl", F_GETFL);
154 K("fSetfl", F_SETFL); K("fDupfdCloexec", F_DUPFD_CLOEXEC);
155 K("fdCloexec", FD_CLOEXEC);
156 K("seekSet", SEEK_SET); K("seekCur", SEEK_CUR); K("seekEnd", SEEK_END);
157 K("lockEx", LOCK_EX); K("lockSh", LOCK_SH); K("lockNb", LOCK_NB);
158 K("protRead", PROT_READ); K("protWrite", PROT_WRITE);
159 K("mapShared", MAP_SHARED); K("mapPrivate", MAP_PRIVATE);
160 K("mapAnonymous", MAP_ANONYMOUS); K("mfdCloexec", MFD_CLOEXEC);
161 K("atFdcwd", AT_FDCWD);
162 K("fOk", F_OK); K("rOk", R_OK); K("wOk", W_OK); K("xOk", X_OK);
163
164 SAY("struct stat, statvfs and dirent");
165 K("statLen", sizeof(struct stat));
166 READ("stDev", struct stat, st_dev); READ("stIno", struct stat, st_ino);
167 READ("stNlink", struct stat, st_nlink); READ("stMode", struct stat, st_mode);
168 READ("stSize", struct stat, st_size);
169 READ("stUid", struct stat, st_uid); READ("stGid", struct stat, st_gid);
170 READ("stMtime", struct stat, st_mtim.tv_sec);
171 READ("stMtimeNs", struct stat, st_mtim.tv_nsec);
172 K("sIfmt", S_IFMT); K("sIfdir", S_IFDIR); K("sIfreg", S_IFREG);
173 K("sIflnk", S_IFLNK);
174 K("statvfsLen", sizeof(struct statvfs));
175 READ("fFrsize", struct statvfs, f_frsize);
176 READ("fBlocks", struct statvfs, f_blocks);
177 READ("fBavail", struct statvfs, f_bavail);
178 AT("dName", struct dirent, d_name); AT("dType", struct dirent, d_type);
179 K("dtUnknown", DT_UNKNOWN); K("dtDir", DT_DIR); K("dtReg", DT_REG);
180 K("dtLnk", DT_LNK);
181
182 SAY("the terminal, poll and the clocks");
183 K("tcsanow", TCSANOW); K("tcsadrain", TCSADRAIN);
184 K("tiocgwinsz", TIOCGWINSZ); K("tiocswinsz", TIOCSWINSZ);
185 K("tiocsctty", TIOCSCTTY);
186#ifdef __APPLE__
187 K("ptyHeld", 1); /* a pseudo-terminal's controlling side works only while its terminal is open */
188#else
189 K("ptyHeld", 0);
190#endif
191 K("pollin", POLLIN); K("pollout", POLLOUT);
192 K("clockRealtime", CLOCK_REALTIME); K("clockMonotonic", CLOCK_MONOTONIC);
193 K("timerAbstime", TIMER_ABSTIME);
194
195 SAY("sockets, their addresses, and a message with fds");
196 K("afUnix", AF_UNIX); K("afInet", AF_INET); K("afInet6", AF_INET6);
197 K("sockStream", SOCK_STREAM); K("sockDgram", SOCK_DGRAM);
198 K("sockCloexec", SOCK_CLOEXEC); K("sockNonblock", SOCK_NONBLOCK);
199 K("solSocket", SOL_SOCKET); K("soReuseaddr", SO_REUSEADDR);
200 K("soError", SO_ERROR); K("soPeercred", SO_PEERCRED); /* Linux's */
201 K("shutWr", SHUT_WR); K("msgNosignal", MSG_NOSIGNAL);
202 K("msgDontwait", MSG_DONTWAIT);
203 K("msgCmsgCloexec", MSG_CMSG_CLOEXEC); K("scmRights", SCM_RIGHTS);
204 K("sockaddrLen", sizeof(struct sockaddr_storage));
205 READ("saFamily", struct sockaddr, sa_family);
206#ifdef SIN6_LEN
207 AT("saLen", struct sockaddr, sa_len); /* the BSDs: the address's length first */
208#else
209 K("saLenAt", -1);
210#endif
211 K("sockaddrUnLen", sizeof(struct sockaddr_un));
212 READ("sunPath", struct sockaddr_un, sun_path);
213#ifdef __linux__
214 K("unixAbstract", 1); /* a Unix socket's name may start with a zero: no file */
215#else
216 K("unixAbstract", 0);
217#endif
218 K("sockaddrInLen", sizeof(struct sockaddr_in));
219 AT("sinPort", struct sockaddr_in, sin_port);
220 AT("sinAddr", struct sockaddr_in, sin_addr);
221 K("sockaddrIn6Len", sizeof(struct sockaddr_in6));
222 AT("sin6Port", struct sockaddr_in6, sin6_port);
223 AT("sin6Addr", struct sockaddr_in6, sin6_addr);
224#ifdef __linux__
225 READ("ucPid", struct ucred, pid); READ("ucUid", struct ucred, uid);
226#else
227 K("ucPidAt", 0); K("ucPidLen", 0);
228 READ("ucUid", struct xucred, cr_uid);
229#endif
230 K("msghdrLen", sizeof(struct msghdr));
231 AT("msgIov", struct msghdr, msg_iov);
232 WRITE("msgIovlen", struct msghdr, msg_iovlen, msg_control);
233 AT("msgControl", struct msghdr, msg_control);
234 WRITE("msgControllen", struct msghdr, msg_controllen, msg_flags);
235 WRITE("cmsgLen", struct cmsghdr, cmsg_len, cmsg_level);
236 AT("cmsgLevel", struct cmsghdr, cmsg_level);
237 AT("cmsgType", struct cmsghdr, cmsg_type);
238 K("cmsgDataAt", CMSG_LEN(0));
239 K("cmsgAlign", CMSG_ALIGN(1));
240
241 SAY("signals, sigaction and sigprocmask");
242 K("sigHup", SIGHUP); K("sigInt", SIGINT); K("sigQuit", SIGQUIT);
243 K("sigFpe", SIGFPE); K("sigKill", SIGKILL); K("sigUsr1", SIGUSR1);
244 K("sigSegv", SIGSEGV); K("sigUsr2", SIGUSR2); K("sigPipe", SIGPIPE);
245 K("sigAlrm", SIGALRM); K("sigTerm", SIGTERM); K("sigChld", SIGCHLD);
246 K("sigCont", SIGCONT); K("sigStop", SIGSTOP); K("sigTstp", SIGTSTP);
247 K("sigTtin", SIGTTIN); K("sigTtou", SIGTTOU); K("sigWinch", SIGWINCH);
248 K("sigMax", NSIG - 1);
249 K("sigactionLen", sizeof(struct sigaction));
250 AT("saHandler", struct sigaction, sa_handler);
251 AT("saMask", struct sigaction, sa_mask);
252 READ("saFlags", struct sigaction, sa_flags);
253 K("sigsetLen", sizeof(sigset_t));
254 K("saRestart", SA_RESTART); K("sigSetmask", SIG_SETMASK);
255
256 SAY("limits, time used, and wait");
257 K("rlimitCpu", RLIMIT_CPU); K("rlimitFsize", RLIMIT_FSIZE);
258 K("rlimitData", RLIMIT_DATA); K("rlimitStack", RLIMIT_STACK);
259 K("rlimitCore", RLIMIT_CORE); K("rlimitNproc", RLIMIT_NPROC);
260 K("rlimitNofile", RLIMIT_NOFILE); K("rlimitAs", RLIMIT_AS);
261 K("rlimInfinity", RLIM_INFINITY);
262 K("rusageSelf", RUSAGE_SELF); K("rusageChildren", RUSAGE_CHILDREN);
263 AT("ruUtime", struct rusage, ru_utime); AT("ruStime", struct rusage, ru_stime);
264 READ("tvSec", struct timeval, tv_sec); READ("tvUsec", struct timeval, tv_usec);
265 K("wNohang", WNOHANG); K("wUntraced", WUNTRACED);
266
267 SAY("errnos: EINTR; and each POSIX names, the C library's number, then Linux's");
268 K("eIntr", EINTR);
269 n = sizeof errnos / sizeof errnos[0];
270 printf(" errnos*: [%d]I32 = [", 2 * n);
271 for (i = 0; i < n; i++)
272 printf("%s%s%d, %d", i ? "," : "", i % 6 ? " " : "\n ", errnos[i].here, errnos[i].onLinux);
273 printf("];\n\n");
274
275#ifdef __linux__
276 printf("extern proc __errno_location: ^I32;\n"
277 "extern proc syscall(n: INT; ...): INT;\n"
278 "extern proc pipe2(fds: ^I32; flags: I32): I32;\n"
279 "extern proc accept4(fd: I32; addr: ^U8; len: ^U32; flags: I32): I32;\n"
280 "extern proc getrandom(buf: ^U8; n: UINT; flags: U32): INT;\n"
281 "extern proc memfd_create(name: ^U8; flags: U32): I32;\n"
282 "extern proc clock_nanosleep(clock, flags: I32; t, left: ^U8): I32;\n"
283 "extern proc sched_getaffinity(pid: I32; size: UINT; mask: ^U8): I32;\n"
284 "extern proc getsockopt(fd, level, name: I32; value: ^U8; len: ^U32): I32;\n"
285 "extern proc send(fd: I32; buf: ^U8; n: UINT; flags: I32): INT;\n"
286 "\n"
287 "(* errno, this thread's *)\n"
288 "proc errno*: ^I32;\n"
289 "begin\n"
290 " return __errno_location();\n"
291 "end;\n"
292 "\n"
293 "(* Closes the fds from lo to hi, or every one from lo if hi < 0: by\n"
294 " close_range's system call, since musl has no close_range. *)\n"
295 "proc closeFrom*(lo, hi: INT);\n"
296 "begin\n"
297 " if hi < 0 then hi := 0FFFFFFFFH; end;\n"
298 " _ := syscall(436, lo, hi, 0);\n"
299 "end;\n"
300 "\n"
301 "(* A pipe, its two fds put at fds, taking flags, oCloexec and\n"
302 " oNonblock, at once: 0, or -1 with errno set, as C's calls are. *)\n"
303 "proc makePipe*(fds: ^I32; flags: I32): I32;\n"
304 "begin\n"
305 " return pipe2(fds, flags);\n"
306 "end;\n"
307 "\n"
308 "(* The next connection to the socket fd, closed on exec; or -1. *)\n"
309 "proc accepted*(fd: I32): I32;\n"
310 "begin\n"
311 " return accept4(fd, nil, nil, sockCloexec);\n"
312 "end;\n"
313 "\n"
314 "(* The kernel's random bytes, at most n, put at buf: how many, or -1. *)\n"
315 "proc random*(buf: ^U8; n: UINT): INT;\n"
316 "begin\n"
317 " return getrandom(buf, n, 0);\n"
318 "end;\n"
319 "\n"
320 "(* A file in memory, of no bytes, with no name, closed on exec: its\n"
321 " fd, or -1. *)\n"
322 "proc memFile*: I32;\n"
323 "begin\n"
324 " return memfd_create(SYSTEM.PTR(\"raz\\0\"), mfdCloexec); (* the name /proc shows *)\n"
325 "end;\n"
326 "\n"
327 "(* Waits until t, a struct timespec of the monotonic clock, again\n"
328 " after EINTR. *)\n"
329 "proc sleepUntil*(t: ^U8);\n"
330 "begin\n"
331 " repeat until clock_nanosleep(clockMonotonic, timerAbstime, t, nil) # eIntr;\n"
332 "end;\n"
333 "\n"
334 "(* The number of processors this program may run on, or 0 if the\n"
335 " system doesn't say. *)\n"
336 "proc processors*: INT;\n"
337 "var mask: [1024]U8; i, b, count: INT;\n"
338 "begin\n"
339 " 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 *)\n"
340 " if sched_getaffinity(0, UINT(LEN(mask)), SYSTEM.ADR(mask[0])) # 0 then return 0; end;\n"
341 " count := 0;\n"
342 " for i := 0 to LEN(mask) - 1 do\n"
343 " for b := 0 to 7 do\n"
344 " if (INT(mask[i]) shr b) & 1 = 1 then INC(count); end;\n"
345 " end;\n"
346 " end;\n"
347 " return count;\n"
348 "end;\n"
349 "\n"
350 "(* The process and the user at the other end of the Unix socket fd,\n"
351 " as the system recorded them when it connected, and 0; or -1. *)\n"
352 "proc peer*(fd: I32; var pid, uid: INT): I32;\n"
353 "var cred: [16]U64; size: U32; p: ^U8; q: ^U32;\n"
354 "begin\n"
355 " p := ^U8(SYSTEM.ADR(cred[0]));\n"
356 " size := U32(SIZE([16]U64));\n"
357 " if getsockopt(fd, solSocket, soPeercred, p, SYSTEM.ADR(size)) # 0 then return -1; end;\n"
358 " q := ^U32(p + ucPidAt);\n"
359 " pid := INT(I32(q^));\n"
360 " q := ^U32(p + ucUidAt);\n"
361 " uid := INT(q^);\n"
362 " return 0;\n"
363 "end;\n"
364 "\n"
365 "(* Sends n bytes at buf on the socket fd, without waiting, and without\n"
366 " SIGPIPE where the other end is gone: how many were sent, or -1. *)\n"
367 "proc sendNow*(fd: I32; buf: ^U8; n: UINT): INT;\n"
368 "begin\n"
369 " return send(fd, buf, n, msgNosignal | msgDontwait);\n"
370 "end;\n"
371 "\n");
372#elif defined(__APPLE__)
373 printf("extern proc __error: ^I32;\n"
374 "extern proc close(fd: I32): I32;\n"
375 "extern proc getdtablesize: I32;\n"
376 "extern proc pipe(fds: ^I32): I32;\n"
377 "extern proc fcntl(fd, cmd: I32; ...): I32;\n"
378 "extern proc accept(fd: I32; addr: ^U8; len: ^U32): I32;\n"
379 "extern proc getentropy(buf: ^U8; n: UINT): I32;\n"
380 "extern proc getpid: I32;\n"
381 "extern proc shm_open(name: ^U8; flags: I32; ...): I32;\n"
382 "extern proc shm_unlink(name: ^U8): I32;\n"
383 "extern proc clock_gettime(clock: I32; t: ^U8): I32;\n"
384 "extern proc nanosleep(t, left: ^U8): I32;\n"
385 "extern proc sysconf(name: I32): INT;\n"
386 "extern proc getsockopt(fd, level, name: I32; value: ^U8; len: ^U32): I32;\n"
387 "extern proc send(fd: I32; buf: ^U8; n: UINT; flags: I32): INT;\n"
388 "\n"
389 "var shmCount: INT; (* the shared memory objects memFile has made *)\n"
390 "\n"
391 "(* errno, this thread's *)\n"
392 "proc errno*: ^I32;\n"
393 "begin\n"
394 " return __error();\n"
395 "end;\n"
396 "\n"
397 "(* Closes the fds from lo to hi, or every one from lo if hi < 0: one\n"
398 " by one, as macOS has no close_range. *)\n"
399 "proc closeFrom*(lo, hi: INT);\n"
400 "var fd: INT;\n"
401 "begin\n"
402 " if hi < 0 then hi := INT(getdtablesize()) - 1; end;\n"
403 " for fd := lo to hi do _ := close(I32(fd)); end;\n"
404 "end;\n"
405 "\n"
406 "(* fd closed on exec, and nonblocking, as flags, oCloexec and\n"
407 " oNonblock, say: macOS has no flags for them where a pipe or a\n"
408 " socket is made, so another thread's exec in between keeps the fd\n"
409 " open, a gap Linux hasn't. *)\n"
410 "proc setFlags(fd, flags: I32);\n"
411 "begin\n"
412 " if flags & oCloexec # 0 then _ := fcntl(fd, fSetfd, fdCloexec); end;\n"
413 " if flags & oNonblock # 0 then _ := fcntl(fd, fSetfl, fcntl(fd, fGetfl) | oNonblock); end;\n"
414 "end;\n"
415 "\n"
416 "(* A pipe, its two fds put at fds, taking flags, oCloexec and\n"
417 " oNonblock: 0, or -1 with errno set, as C's calls are. *)\n"
418 "proc makePipe*(fds: ^I32; flags: I32): I32;\n"
419 "begin\n"
420 " if pipe(fds) # 0 then return -1; end;\n"
421 " setFlags(fds[0], flags);\n"
422 " setFlags(fds[1], flags);\n"
423 " return 0;\n"
424 "end;\n"
425 "\n"
426 "(* The next connection to the socket fd, closed on exec; or -1. *)\n"
427 "proc accepted*(fd: I32): I32;\n"
428 "var r: I32;\n"
429 "begin\n"
430 " r := accept(fd, nil, nil);\n"
431 " if r >= 0 then setFlags(r, oCloexec); end;\n"
432 " return r;\n"
433 "end;\n"
434 "\n"
435 "(* The kernel's random bytes, at most n, put at buf: how many, or -1.\n"
436 " getentropy gives 256 at most at a time. *)\n"
437 "proc random*(buf: ^U8; n: UINT): INT;\n"
438 "begin\n"
439 " if n > 256 then n := 256; end;\n"
440 " if getentropy(buf, n) # 0 then return -1; end;\n"
441 " return INT(n);\n"
442 "end;\n"
443 "\n"
444 "(* v in hexadecimal into name from i on: where its digits end *)\n"
445 "proc hex(var name: [32]U8; i, v: INT): INT;\n"
446 "var n, k, d: INT;\n"
447 "begin\n"
448 " n := v;\n"
449 " repeat INC(i); n := n shr 4; until n = 0;\n"
450 " k := i;\n"
451 " repeat\n"
452 " DEC(k);\n"
453 " d := v & 15;\n"
454 " if d < 10 then name[k] := U8(d) + '0'; else name[k] := U8(d - 10) + 'a'; end;\n"
455 " v := v shr 4;\n"
456 " until v = 0;\n"
457 " return i;\n"
458 "end;\n"
459 "\n"
460 "(* A file in memory, of no bytes, with no name, closed on exec: its\n"
461 " fd, or -1. macOS has no memfd_create: it is a POSIX shared memory\n"
462 " object, made with a name of its own, /raz.PID.N, which is removed\n"
463 " at once. Such an object can be given its size once, and mapped, but\n"
464 " not read or written. *)\n"
465 "proc memFile*: I32;\n"
466 "var name: [32]U8; fd: I32; i: INT;\n"
467 "begin\n"
468 " COPY(name[0..5], \"/raz.\");\n"
469 " i := hex(name, 5, INT(getpid()));\n"
470 " name[i] := '.';\n"
471 " i := hex(name, i + 1, XADD(shmCount, 1));\n"
472 " name[i] := 0;\n"
473 " fd := shm_open(SYSTEM.ADR(name[0]), oRdwr | oCreat | oExcl, 384); (* rw------- *)\n"
474 " if fd < 0 then return -1; end;\n"
475 " _ := shm_unlink(SYSTEM.ADR(name[0]));\n"
476 " setFlags(fd, oCloexec);\n"
477 " return fd;\n"
478 "end;\n"
479 "\n"
480 "(* Waits until t, a struct timespec of the monotonic clock: macOS has\n"
481 " no clock_nanosleep, so nanosleep sleeps what is left, again after\n"
482 " EINTR. *)\n"
483 "proc sleepUntil*(t: ^U8);\n"
484 "var now, left: [2]I64; at: ^I64; d: I64;\n"
485 "begin\n"
486 " at := ^I64(t);\n"
487 " repeat\n"
488 " _ := clock_gettime(clockMonotonic, ^U8(SYSTEM.ADR(now[0])));\n"
489 " d := (at[0] - now[0]) * 1000000000 + at[1] - now[1];\n"
490 " if d <= 0 then return; end;\n"
491 " left := [d div 1000000000, d mod 1000000000];\n"
492 " _ := nanosleep(^U8(SYSTEM.ADR(left[0])), nil);\n"
493 " until false;\n"
494 "end;\n"
495 "\n"
496 "(* The number of processors this program may run on, or 0 if the\n"
497 " system doesn't say: those online, as macOS binds a program to none\n"
498 " of them. *)\n"
499 "proc processors*: INT;\n"
500 "begin\n"
501 " return sysconf(%d); (* _SC_NPROCESSORS_ONLN *)\n"
502 "end;\n"
503 "\n"
504 "(* The process and the user at the other end of the Unix socket fd,\n"
505 " as the system recorded them when it connected, and 0; or -1. The\n"
506 " user is in a struct xucred, LOCAL_PEERCRED's, and the process is\n"
507 " asked for alone, LOCAL_PEERPID. *)\n"
508 "proc peer*(fd: I32; var pid, uid: INT): I32;\n"
509 "var cred: [16]U64; size: U32; p: ^U8; q: ^U32; id: I32;\n"
510 "begin\n"
511 " p := ^U8(SYSTEM.ADR(cred[0]));\n"
512 " size := U32(SIZE([16]U64));\n"
513 " if getsockopt(fd, %d, %d, p, SYSTEM.ADR(size)) # 0 then return -1; end;\n"
514 " q := ^U32(p + ucUidAt);\n"
515 " uid := INT(q^);\n"
516 " size := 4;\n"
517 " if getsockopt(fd, %d, %d, ^U8(SYSTEM.ADR(id)), SYSTEM.ADR(size)) # 0 then return -1; end;\n"
518 " pid := INT(id);\n"
519 " return 0;\n"
520 "end;\n"
521 "\n"
522 "(* Sends n bytes at buf on the socket fd, without waiting, and without\n"
523 " SIGPIPE where the other end is gone: how many were sent, or -1.\n"
524 " macOS's send waits on a Unix socket though MSG_DONTWAIT is given, so\n"
525 " the socket is made nonblocking for it, as it was before after. *)\n"
526 "proc sendNow*(fd: I32; buf: ^U8; n: UINT): INT;\n"
527 "var f: I32; r: INT; e: ^I32; err: I32;\n"
528 "begin\n"
529 " f := fcntl(fd, fGetfl);\n"
530 " if (f >= 0) and (f & oNonblock = 0) then _ := fcntl(fd, fSetfl, f | oNonblock); end;\n"
531 " r := send(fd, buf, n, msgNosignal | msgDontwait);\n"
532 " e := __error();\n"
533 " err := e^;\n"
534 " if (f >= 0) and (f & oNonblock = 0) then _ := fcntl(fd, fSetfl, f); end;\n"
535 " e^ := err;\n"
536 " return r;\n"
537 "end;\n"
538 "\n",
539 _SC_NPROCESSORS_ONLN, SOL_LOCAL, LOCAL_PEERCRED, SOL_LOCAL, LOCAL_PEERPID);
540#else
541#error "a CAbi for this system needs its procedures: add them here"
542#endif
543 printf("end CAbi.\n");
544 return 0;
545}