| | @@ -2,19 +2,20 @@ |
| 2 | 2 | file of their own, with the same exports. A program that imports it |
| 3 | 3 | uses C. |
| 4 | 4 | |
| 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. *) |
| 16 | 17 | module Sys; |
| 17 | | -import C; |
| 18 | +import C, CAbi; |
| 18 | 19 | |
| 19 | 20 | const |
| 20 | 21 | stdin* = 0; (* standard input's file descriptor *) |
| | @@ -23,26 +24,32 @@ |
| 23 | 24 | (* the bytes Out keeps before it writes them; a target with little |
| 24 | 25 | memory keeps fewer *) |
| 25 | 26 | 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; |
| 46 | 53 | |
| 47 | 54 | type |
| 48 | 55 | (* What open is asked for: reading, writing or both, and what else |
| | @@ -92,14 +99,12 @@ |
| 92 | 99 | (* How a program that wait waited for changed. *) |
| 93 | 100 | Status* = (running, exited, killed, stopped); |
| 94 | 101 | |
| 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 *) |
| 96 | 103 | 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; |
| 103 | 108 | |
| 104 | 109 | (* A terminal's modes, as getMode reads them and setMode sets them: |
| 105 | 110 | a struct termios, of the C library's size, 256 bytes at most. *) |
| | @@ -107,50 +112,24 @@ |
| 107 | 112 | t: [256]U8; |
| 108 | 113 | end; |
| 109 | 114 | |
| 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 | | - |
| 116 | 115 | Winsize = record rows, cols, xpixel, ypixel: U16; end; |
| 117 | 116 | Pollfd = record fd: I32; events, revents: I16; end; |
| 118 | 117 | |
| 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; |
| 131 | 121 | (* Whom a datagram came from, as receive says and reply takes: the |
| 132 | 122 | address recvfrom fills in, and its size. *) |
| 133 | 123 | Peer* = record |
| 134 | 124 | a: Sockaddr; |
| 135 | 125 | size: INT; |
| 136 | 126 | 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; |
| 146 | 131 | 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; |
| 154 | 133 | Timespec = record sec, nsec: I64; end; |
| 155 | 134 | |
| 156 | 135 | var |
| | @@ -159,17 +138,50 @@ |
| 159 | 138 | saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *) |
| 160 | 139 | host: [256]U8; (* what hostName gives *) |
| 161 | 140 | 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 *) |
| 163 | 142 | broke: INT; (* 1 when one has come since a call that ready or readyFor makes again began *) |
| 164 | 143 | |
| 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 *) |
| 166 | 156 | proc failed: INT; |
| 167 | 157 | var e: ^I32; |
| 168 | 158 | begin |
| 169 | | - e := C.__errno_location(); |
| 170 | | - return -INT(e^); |
| 159 | + e := CAbi.errno(); |
| 160 | + return linux(-INT(e^)); |
| 171 | 161 | end; |
| 172 | 162 | |
| 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 | + |
| 173 | 185 | (* Whether a call that failed is to be made again: when it failed with |
| 174 | 186 | EINTR because a signal catch took broke into it, since on Linux those |
| 175 | 187 | break into nothing; not when one handle took did, which the program |
| | @@ -210,19 +222,19 @@ |
| 210 | 222 | return true; |
| 211 | 223 | end; |
| 212 | 224 | |
| 213 | | -(* open's flags for how; glibc's numbers *) |
| 225 | +(* open's flags for how *) |
| 214 | 226 | proc flags(how: Accesses): I32; |
| 215 | 227 | var f: I32; |
| 216 | 228 | begin |
| 217 | | - f := 0; (* O_RDONLY *) |
| 229 | + f := CAbi.oRdonly; |
| 218 | 230 | 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; |
| 221 | 233 | 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; |
| 226 | 238 | return f; |
| 227 | 239 | end; |
| 228 | 240 | |
| | @@ -263,8 +275,8 @@ |
| 263 | 275 | proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; |
| 264 | 276 | var r: I64; |
| 265 | 277 | 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; |
| 268 | 280 | if r < 0 then return failed(); end; |
| 269 | 281 | return r; |
| 270 | 282 | end; |
| | @@ -274,7 +286,7 @@ |
| 274 | 286 | proc position*(fd: INT): I64; |
| 275 | 287 | var r: I64; |
| 276 | 288 | begin |
| 277 | | - r := C.lseek(I32(fd), 0, 1); (* SEEK_CUR *) |
| 289 | + r := C.lseek(I32(fd), 0, CAbi.seekCur); |
| 278 | 290 | if r < 0 then return failed(); end; |
| 279 | 291 | return r; |
| 280 | 292 | end; |
| | @@ -302,7 +314,7 @@ |
| 302 | 314 | syncfs gives one that isn't. *) |
| 303 | 315 | proc syncAll*(fd: INT): INT; |
| 304 | 316 | 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; |
| 306 | 318 | C.sync; |
| 307 | 319 | return 0; |
| 308 | 320 | end; |
| | @@ -314,8 +326,8 @@ |
| 314 | 326 | proc lock*(fd: INT; wait: BOOL): INT; |
| 315 | 327 | var how: I32; |
| 316 | 328 | 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; |
| 319 | 331 | if C.flock(I32(fd), how) # 0 then return failed(); end; |
| 320 | 332 | return 0; |
| 321 | 333 | end; |
| | @@ -353,7 +365,7 @@ |
| 353 | 365 | proc dupAbove*(fd, least: INT): INT; |
| 354 | 366 | var r: I32; |
| 355 | 367 | begin |
| 356 | | - r := C.fcntl(I32(fd), 1030, I32(least)); (* F_DUPFD_CLOEXEC *) |
| 368 | + r := C.fcntl(I32(fd), CAbi.fDupfdCloexec, I32(least)); |
| 357 | 369 | if r < 0 then return failed(); end; |
| 358 | 370 | return INT(r); |
| 359 | 371 | end; |
| | @@ -364,10 +376,10 @@ |
| 364 | 376 | proc keepOnExec*(fd: INT; yes: BOOL): INT; |
| 365 | 377 | var f: I32; |
| 366 | 378 | begin |
| 367 | | - f := C.fcntl(I32(fd), 1, 0); (* F_GETFD *) |
| 379 | + f := C.fcntl(I32(fd), CAbi.fGetfd, 0); |
| 368 | 380 | 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; |
| 371 | 383 | return 0; |
| 372 | 384 | end; |
| 373 | 385 | |
| | @@ -376,7 +388,7 @@ |
| 376 | 388 | proc mmap*(size: INT): ^U8; |
| 377 | 389 | var p: ^U8; |
| 378 | 390 | 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); |
| 380 | 392 | if INT(p) = -1 then return nil; end; |
| 381 | 393 | return p; |
| 382 | 394 | end; |
| | @@ -393,7 +405,7 @@ |
| 393 | 405 | var name: [4]U8; fd: I32; err: INT; |
| 394 | 406 | begin |
| 395 | 407 | _ := 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); |
| 397 | 409 | if fd < 0 then return failed(); end; |
| 398 | 410 | if C.ftruncate(fd, I64(size)) # 0 then |
| 399 | 411 | err := failed(); (* before close can change errno *) |
| | @@ -409,7 +421,7 @@ |
| 409 | 421 | proc mapFile*(fd, size: INT): ^U8; |
| 410 | 422 | var p: ^U8; |
| 411 | 423 | 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); |
| 413 | 425 | if INT(p) = -1 then return nil; end; |
| 414 | 426 | return p; |
| 415 | 427 | end; |
| | @@ -466,10 +478,10 @@ |
| 466 | 478 | var a: [4096]U8; m: I32; |
| 467 | 479 | begin |
| 468 | 480 | 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; |
| 473 | 485 | if C.access(ADR(a[0]), m) # 0 then return failed(); end; |
| 474 | 486 | return 0; |
| 475 | 487 | end; |
| | @@ -502,18 +514,27 @@ |
| 502 | 514 | return linked[0..n], 0; |
| 503 | 515 | end; |
| 504 | 516 | |
| 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 | + |
| 505 | 525 | (* 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; |
| 508 | 528 | 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; |
| 517 | 538 | return i; |
| 518 | 539 | end; |
| 519 | 540 | |
| | @@ -521,11 +542,11 @@ |
| 521 | 542 | file, following a symbolic link, and whether path is one; and 0, or |
| 522 | 543 | minus an errno. *) |
| 523 | 544 | 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; |
| 525 | 546 | begin |
| 526 | 547 | if not cstr(path, a) then return i, -36; end; |
| 527 | 548 | 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; |
| 529 | 550 | if C.stat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; |
| 530 | 551 | i := fromStat(st); |
| 531 | 552 | i.symlink := isLink; |
| | @@ -536,18 +557,18 @@ |
| 536 | 557 | what it names, so that symlink is true, dir and file false, and the |
| 537 | 558 | size the length of what it names. *) |
| 538 | 559 | 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; |
| 540 | 561 | begin |
| 541 | 562 | if not cstr(path, a) then return i, -36; end; |
| 542 | 563 | if C.lstat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; |
| 543 | 564 | i := fromStat(st); |
| 544 | | - i.symlink := st[3] & 0F000H = 0A000H; (* S_IFLNK *) |
| 565 | + i.symlink := fileType(st) = CAbi.sIflnk; |
| 545 | 566 | return i, 0; |
| 546 | 567 | end; |
| 547 | 568 | |
| 548 | 569 | (* info of the file open on fd, which is never a link. *) |
| 549 | 570 | proc infoOf*(fd: INT): (Info, INT); |
| 550 | | -var st: [18]INT; i: Info; |
| 571 | +var st: Stat; i: Info; |
| 551 | 572 | begin |
| 552 | 573 | if C.fstat(I32(fd), ^U8(ADR(st))) # 0 then return i, failed(); end; |
| 553 | 574 | return fromStat(st), 0; |
| | @@ -556,22 +577,23 @@ |
| 556 | 577 | (* The bytes free to an ordinary user on path's file system, and the |
| 557 | 578 | bytes in all. *) |
| 558 | 579 | 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; |
| 560 | 581 | begin |
| 561 | 582 | 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; |
| 564 | 587 | end; |
| 565 | 588 | |
| 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. *) |
| 569 | 591 | proc kindOf(t: U8): Kind; |
| 570 | 592 | 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; |
| 575 | 597 | end; |
| 576 | 598 | return isOther; |
| 577 | 599 | end; |
| | @@ -595,8 +617,8 @@ |
| 595 | 617 | while true do |
| 596 | 618 | e := C.readdir(d^.d); |
| 597 | 619 | 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; |
| 600 | 622 | n := 0; |
| 601 | 623 | while e[n] # 0 do INC(n); end; |
| 602 | 624 | if (e[0..n] # ".") and (e[0..n] # "..") then return e[0..n], true; end; |
| | @@ -627,13 +649,13 @@ |
| 627 | 649 | if C.tcgetattr(I32(fd), ADR(saved[0])) # 0 then return false; end; |
| 628 | 650 | t := saved; |
| 629 | 651 | 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; |
| 631 | 653 | end; |
| 632 | 654 | |
| 633 | 655 | (* The terminal on fd as rawMode found it. *) |
| 634 | 656 | proc restoreMode*(fd: INT); |
| 635 | 657 | begin |
| 636 | | - _ := C.tcsetattr(I32(fd), 0, ADR(saved[0])); |
| 658 | + _ := C.tcsetattr(I32(fd), CAbi.tcsanow, ADR(saved[0])); |
| 637 | 659 | end; |
| 638 | 660 | |
| 639 | 661 | (* The modes of the terminal on fd, as a shell keeps them for each job; |
| | @@ -649,7 +671,7 @@ |
| 649 | 671 | is sent: 0, or minus an errno. *) |
| 650 | 672 | proc setMode*(fd: INT; m: Mode): INT; |
| 651 | 673 | 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; |
| 653 | 675 | return 0; |
| 654 | 676 | end; |
| 655 | 677 | |
| | @@ -658,7 +680,7 @@ |
| 658 | 680 | proc termSize*(fd: INT): (INT, INT); |
| 659 | 681 | var w: Winsize; |
| 660 | 682 | 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 |
| 662 | 684 | return 80, 24; |
| 663 | 685 | end; |
| 664 | 686 | return INT(w.cols), INT(w.rows); |
| | @@ -669,7 +691,7 @@ |
| 669 | 691 | proc ready*(fd, ms: INT): BOOL; |
| 670 | 692 | var p: Pollfd; r: I32; |
| 671 | 693 | begin |
| 672 | | - p := {fd: I32(fd), events: 1}; (* POLLIN *) |
| 694 | + p := {fd: I32(fd), events: CAbi.pollin}; |
| 673 | 695 | _ := XCHG(ADR(broke), 0); |
| 674 | 696 | repeat r := C.poll(^U8(ADR(p)), 1, I32(ms)); until (r >= 0) or not again(); |
| 675 | 697 | return r > 0; |
| | @@ -687,13 +709,13 @@ |
| 687 | 709 | something takes. *) |
| 688 | 710 | proc clock*: I64; |
| 689 | 711 | begin |
| 690 | | - return time(1); (* CLOCK_MONOTONIC *) |
| 712 | + return time(CAbi.clockMonotonic); |
| 691 | 713 | end; |
| 692 | 714 | |
| 693 | 715 | (* The time in nanoseconds since 1970-01-01, UTC. *) |
| 694 | 716 | proc now*: I64; |
| 695 | 717 | begin |
| 696 | | - return time(0); (* CLOCK_REALTIME *) |
| 718 | + return time(CAbi.clockRealtime); |
| 697 | 719 | end; |
| 698 | 720 | |
| 699 | 721 | (* Waits ms milliseconds. *) |
| | @@ -702,7 +724,8 @@ |
| 702 | 724 | begin |
| 703 | 725 | at := clock() + I64(ms) * 1000000; |
| 704 | 726 | 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; |
| 706 | 729 | end; |
| 707 | 730 | |
| 708 | 731 | (* Fills b with random bytes, from the kernel. *) |
| | @@ -723,13 +746,13 @@ |
| 723 | 746 | proc socket(family, kind: INT; a: ^U8; size: INT; serving: BOOL): INT; |
| 724 | 747 | var fd, r, one: I32; err: INT; |
| 725 | 748 | begin |
| 726 | | - fd := C.socket(I32(family), I32(kind) | 80000H, 0); (* SOCK_CLOEXEC *) |
| 749 | + fd := C.socket(I32(family), I32(kind) | CAbi.sockCloexec, 0); |
| 727 | 750 | if fd < 0 then return failed(); end; |
| 728 | 751 | if serving then |
| 729 | 752 | 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); |
| 731 | 754 | 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; *) |
| 733 | 756 | r := C.listen(fd, 4096); (* one of datagrams only receives *) |
| 734 | 757 | end; |
| 735 | 758 | else |
| | @@ -743,57 +766,85 @@ |
| 743 | 766 | return INT(fd); |
| 744 | 767 | end; |
| 745 | 768 | |
| 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 | + |
| 746 | 783 | (* 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; |
| 748 | 786 | 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); |
| 752 | 791 | return true; |
| 753 | 792 | end; |
| 754 | 793 | |
| 755 | 794 | (* A stream socket connected to the Unix socket at path. *) |
| 756 | 795 | proc connect*(path: []U8): INT; |
| 757 | | -var a: SockaddrUn; |
| 796 | +var a: Sockaddr; |
| 758 | 797 | begin |
| 759 | 798 | 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); |
| 761 | 800 | end; |
| 762 | 801 | |
| 763 | 802 | (* A stream socket made at path, which must not be there yet, taking |
| 764 | 803 | connections. *) |
| 765 | 804 | proc listen*(path: []U8): INT; |
| 766 | | -var a: SockaddrUn; |
| 805 | +var a: Sockaddr; |
| 767 | 806 | begin |
| 768 | 807 | 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); |
| 770 | 809 | end; |
| 771 | 810 | |
| 772 | 811 | (* addr, an IPv4 or IPv6 address as numbers, and port, in a: the size |
| 773 | 812 | of its struct sockaddr_in or sockaddr_in6, or 0 if either is |
| 774 | 813 | wrong. *) |
| 775 | 814 | 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; |
| 777 | 816 | begin |
| 778 | 817 | 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); |
| 781 | 819 | (* 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; |
| 788 | 833 | 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; |
| 795 | 837 | end; |
| 796 | 838 | |
| 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 | + |
| 797 | 848 | (* A socket of kind at addr and port, as socket makes it; -22 (EINVAL) |
| 798 | 849 | if they are wrong. *) |
| 799 | 850 | proc inet(addr: []U8; port, kind: INT; serving: BOOL): INT; |
| | @@ -801,34 +852,34 @@ |
| 801 | 852 | begin |
| 802 | 853 | size := inetAddr(addr, port, ADR(a)); |
| 803 | 854 | 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); |
| 805 | 856 | end; |
| 806 | 857 | |
| 807 | 858 | (* A stream socket connected to port at addr, an IPv4 or IPv6 address |
| 808 | 859 | as numbers. *) |
| 809 | 860 | proc connectTcp*(addr: []U8; port: INT): INT; |
| 810 | 861 | begin |
| 811 | | - return inet(addr, port, 1, false); (* SOCK_STREAM *) |
| 862 | + return inet(addr, port, CAbi.sockStream, false); |
| 812 | 863 | end; |
| 813 | 864 | |
| 814 | 865 | (* A socket of datagrams to and from port at addr. *) |
| 815 | 866 | proc connectUdp*(addr: []U8; port: INT): INT; |
| 816 | 867 | begin |
| 817 | | - return inet(addr, port, 2, false); (* SOCK_DGRAM *) |
| 868 | + return inet(addr, port, CAbi.sockDgram, false); |
| 818 | 869 | end; |
| 819 | 870 | |
| 820 | 871 | (* A stream socket taking connections on port at addr; "0.0.0.0" or |
| 821 | 872 | "::" is every address the machine has. *) |
| 822 | 873 | proc listenTcp*(addr: []U8; port: INT): INT; |
| 823 | 874 | begin |
| 824 | | - return inet(addr, port, 1, true); |
| 875 | + return inet(addr, port, CAbi.sockStream, true); |
| 825 | 876 | end; |
| 826 | 877 | |
| 827 | 878 | (* A socket of datagrams on port at addr, from anyone, for receive and |
| 828 | 879 | reply. *) |
| 829 | 880 | proc listenUdp*(addr: []U8; port: INT): INT; |
| 830 | 881 | begin |
| 831 | | - return inet(addr, port, 2, true); |
| 882 | + return inet(addr, port, CAbi.sockDgram, true); |
| 832 | 883 | end; |
| 833 | 884 | |
| 834 | 885 | (* The next connection to the socket fd, waiting for one: a socket of |
| | @@ -836,7 +887,7 @@ |
| 836 | 887 | proc accept*(fd: INT): INT; |
| 837 | 888 | var r: I32; |
| 838 | 889 | begin |
| 839 | | - r := C.accept4(I32(fd), nil, nil, 80000H); (* SOCK_CLOEXEC *) |
| 890 | + r := C.accept4(I32(fd), nil, nil, CAbi.sockCloexec); |
| 840 | 891 | if r < 0 then return failed(); end; |
| 841 | 892 | return INT(r); |
| 842 | 893 | end; |
| | @@ -848,7 +899,7 @@ |
| 848 | 899 | var r: INT; |
| 849 | 900 | begin |
| 850 | 901 | 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); |
| 852 | 903 | if r < 0 then return failed(); end; |
| 853 | 904 | return r; |
| 854 | 905 | end; |
| | @@ -864,13 +915,13 @@ |
| 864 | 915 | n := 0; |
| 865 | 916 | for i := 0 to LEN(reads) - 1 do |
| 866 | 917 | 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}; |
| 868 | 919 | INC(n); |
| 869 | 920 | end; |
| 870 | 921 | end; |
| 871 | 922 | for i := 0 to LEN(writes) - 1 do |
| 872 | 923 | 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}; |
| 874 | 925 | INC(n); |
| 875 | 926 | end; |
| 876 | 927 | end; |
| | @@ -897,10 +948,10 @@ |
| 897 | 948 | proc setBlocking*(fd: INT; yes: BOOL): INT; |
| 898 | 949 | var flags: I32; |
| 899 | 950 | begin |
| 900 | | - flags := C.fcntl(I32(fd), 3, 0); (* F_GETFL *) |
| 951 | + flags := C.fcntl(I32(fd), CAbi.fGetfl, 0); |
| 901 | 952 | 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; |
| 904 | 955 | return 0; |
| 905 | 956 | end; |
| 906 | 957 | |
| | @@ -921,7 +972,7 @@ |
| 921 | 972 | begin |
| 922 | 973 | size := inetAddr(addr, port, ADR(a)); |
| 923 | 974 | 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); |
| 925 | 976 | if fd < 0 then return failed(); end; |
| 926 | 977 | r := 0; |
| 927 | 978 | if C.connect(fd, ^U8(ADR(a)), U32(size)) # 0 then r := failed(); end; |
| | @@ -929,8 +980,8 @@ |
| 929 | 980 | r := -110; |
| 930 | 981 | if readyFor([], [INT(fd)], ms) = INT(fd) then |
| 931 | 982 | 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)); |
| 934 | 985 | else |
| 935 | 986 | r := failed(); |
| 936 | 987 | end; |
| | @@ -948,7 +999,7 @@ |
| 948 | 999 | begin |
| 949 | 1000 | size := U32(SIZE(Sockaddr)); |
| 950 | 1001 | 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))); |
| 952 | 1003 | end; |
| 953 | 1004 | |
| 954 | 1005 | (* The next datagram to the socket fd, waiting for one: its first |
| | @@ -969,7 +1020,7 @@ |
| 969 | 1020 | proc reply*(fd: INT; peer: Peer; s: []U8): INT; |
| 970 | 1021 | var r: INT; |
| 971 | 1022 | 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)); |
| 973 | 1024 | if r < 0 then return failed(); end; |
| 974 | 1025 | return r; |
| 975 | 1026 | end; |
| | @@ -978,18 +1029,27 @@ |
| 978 | 1029 | with it the files fds, at most 16, which the program that receives |
| 979 | 1030 | them then has open too: the bytes sent, or minus an errno. *) |
| 980 | 1031 | 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; |
| 982 | 1033 | 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; |
| 984 | 1035 | 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); |
| 986 | 1039 | 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)); |
| 991 | 1051 | end; |
| 992 | | - r := C.sendmsg(I32(fd), ^U8(ADR(m)), 4000H); (* MSG_NOSIGNAL *) |
| 1052 | + r := C.sendmsg(I32(fd), mp, CAbi.msgNosignal); |
| 993 | 1053 | if r < 0 then return failed(); end; |
| 994 | 1054 | return r; |
| 995 | 1055 | end; |
| | @@ -999,20 +1059,28 @@ |
| 999 | 1059 | rest: the bytes read, or minus an errno, and the files put in fds. |
| 1000 | 1060 | Each is closed in a program exec starts. *) |
| 1001 | 1061 | 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; |
| 1003 | 1063 | begin |
| 1004 | 1064 | 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); |
| 1007 | 1072 | if n < 0 then return failed(), 0; end; |
| 1008 | 1073 | 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 |
| 1011 | 1079 | if k < LEN(fds) then |
| 1012 | | - fds[k] := INT(c.fds[i]); |
| 1080 | + fds[k] := INT(got[i]); |
| 1013 | 1081 | INC(k); |
| 1014 | 1082 | else |
| 1015 | | - _ := C.close(c.fds[i]); |
| 1083 | + _ := C.close(got[i]); |
| 1016 | 1084 | end; |
| 1017 | 1085 | end; |
| 1018 | 1086 | end; |
| | @@ -1022,10 +1090,13 @@ |
| 1022 | 1090 | (* What errno err, or minus it, means, as the C library's strerror |
| 1023 | 1091 | says it. *) |
| 1024 | 1092 | proc errorText*(err: INT): []U8; |
| 1025 | | -var s: ^U8; n: INT; |
| 1093 | +var s: ^U8; n, i: INT; |
| 1026 | 1094 | begin |
| 1027 | 1095 | if err < 0 then err := -err; end; |
| 1028 | 1096 | 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; |
| 1029 | 1100 | s := C.strerror(I32(err)); |
| 1030 | 1101 | if s = nil then return "unknown error"; end; |
| 1031 | 1102 | n := 0; |
| | @@ -1087,7 +1158,7 @@ |
| 1087 | 1158 | proc openPty*: INT; |
| 1088 | 1159 | var fd: I32; err: INT; |
| 1089 | 1160 | begin |
| 1090 | | - fd := C.posix_openpt(102H); (* O_RDWR, O_NOCTTY: glibc's *) |
| 1161 | + fd := C.posix_openpt(CAbi.oRdwr | CAbi.oNoctty); |
| 1091 | 1162 | if fd < 0 then return failed(); end; |
| 1092 | 1163 | if (C.grantpt(fd) # 0) or (C.unlockpt(fd) # 0) then |
| 1093 | 1164 | err := failed(); |
| | @@ -1103,7 +1174,7 @@ |
| 1103 | 1174 | var w: Winsize; |
| 1104 | 1175 | begin |
| 1105 | 1176 | 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; |
| 1107 | 1178 | return 0; |
| 1108 | 1179 | end; |
| 1109 | 1180 | |
| | @@ -1187,7 +1258,7 @@ |
| 1187 | 1258 | proc pipe*: (INT, INT); |
| 1188 | 1259 | var fds: [2]I32; |
| 1189 | 1260 | 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; |
| 1191 | 1262 | return INT(fds[0]), INT(fds[1]); |
| 1192 | 1263 | end; |
| 1193 | 1264 | |
| | @@ -1222,35 +1293,47 @@ |
| 1222 | 1293 | return host[0..n], 0; |
| 1223 | 1294 | end; |
| 1224 | 1295 | |
| 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 | + |
| 1225 | 1302 | (* The time on the processor, in ns, that this program has used, or, |
| 1226 | 1303 | if children, the programs it started and waited for: in the program |
| 1227 | 1304 | itself, and in the system for it. *) |
| 1228 | 1305 | proc cpuTime*(children: BOOL): (I64, I64); |
| 1229 | | -var ru: Rusage; |
| 1306 | +var ru: [64]U64; p: ^U8; (* struct rusage, which cabi checks fits *) |
| 1230 | 1307 | 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); |
| 1233 | 1311 | 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); |
| 1236 | 1313 | end; |
| 1237 | 1314 | |
| 1238 | 1315 | (* The limit which, one of the limit constants: the soft one, which |
| 1239 | 1316 | this program may raise up to the hard one; -1 for none; and 0, or |
| 1240 | 1317 | minus an errno. *) |
| 1241 | 1318 | 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 *) |
| 1243 | 1320 | begin |
| 1244 | 1321 | 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; |
| 1245 | 1325 | return r[0], r[1], 0; |
| 1246 | 1326 | end; |
| 1247 | 1327 | |
| 1248 | 1328 | (* Sets the limit which to soft and hard, -1 for none: 0, or minus an |
| 1249 | 1329 | errno. *) |
| 1250 | 1330 | proc setLimit*(which: INT; soft, hard: I64): INT; |
| 1251 | | -var r: [2]I64; |
| 1331 | +var r: [2]I64; i: INT; |
| 1252 | 1332 | begin |
| 1253 | 1333 | r := [soft, hard]; |
| 1334 | + for i := 0 to 1 do |
| 1335 | + if r[i] = -1 then r[i] := CAbi.rlimInfinity; end; |
| 1336 | + end; |
| 1254 | 1337 | if C.setrlimit(I32(which), ^U8(ADR(r))) # 0 then return failed(); end; |
| 1255 | 1338 | return 0; |
| 1256 | 1339 | end; |
| | @@ -1390,8 +1473,7 @@ |
| 1390 | 1473 | (* Closes every fd from 3 on but those of keep, which stay open in the |
| 1391 | 1474 | program exec starts. It runs in spawn's child, after the fork, so it |
| 1392 | 1475 | 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. *) |
| 1395 | 1477 | proc closeAllBut(keep: []INT); |
| 1396 | 1478 | var lo, next, i: INT; |
| 1397 | 1479 | begin |
| | @@ -1402,10 +1484,10 @@ |
| 1402 | 1484 | if (keep[i] >= lo) and ((next < 0) or (keep[i] < next)) then next := keep[i]; end; |
| 1403 | 1485 | end; |
| 1404 | 1486 | if next < 0 then |
| 1405 | | - _ := C.syscall(436, lo, 0FFFFFFFFH, 0); (* close_range, the rest *) |
| 1487 | + CAbi.closeFrom(lo, -1); (* the rest *) |
| 1406 | 1488 | 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 *) |
| 1409 | 1491 | lo := next + 1; |
| 1410 | 1492 | end; |
| 1411 | 1493 | until next < 0; |
| | @@ -1421,9 +1503,9 @@ |
| 1421 | 1503 | begin |
| 1422 | 1504 | if p.tty # 0 then |
| 1423 | 1505 | _ := C.setsid(); |
| 1424 | | - fd := C.open(ADR(name[0]), 2); (* O_RDWR *) |
| 1506 | + fd := C.open(ADR(name[0]), CAbi.oRdwr); |
| 1425 | 1507 | 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; |
| 1427 | 1509 | for i := 0 to 2 do _ := C.dup2(fd, I32(i)); end; |
| 1428 | 1510 | else |
| 1429 | 1511 | if p.group = -1 then _ := C.setpgid(0, 0); |
| | @@ -1446,7 +1528,7 @@ |
| 1446 | 1528 | proc spawn*(p: Program): INT; |
| 1447 | 1529 | var |
| 1448 | 1530 | 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; |
| 1450 | 1532 | begin |
| 1451 | 1533 | if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) |
| 1452 | 1534 | if p.tty # 0 then |
| | @@ -1463,13 +1545,13 @@ |
| 1463 | 1545 | search := env("PATH"); (* read here, before the fork *) |
| 1464 | 1546 | if LEN(search) = 0 then search := defaultPath; end; |
| 1465 | 1547 | 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))); |
| 1467 | 1549 | pid := C.fork(); |
| 1468 | 1550 | if pid = 0 then |
| 1469 | 1551 | if takeOn(p, name, dir) # 0 then C._exit(127); end; |
| 1470 | 1552 | 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); |
| 1473 | 1555 | run(p.path, search, a, argv, argv + (LEN(p.args) + 1)); |
| 1474 | 1556 | C._exit(127); |
| 1475 | 1557 | end; |
| | @@ -1481,7 +1563,7 @@ |
| 1481 | 1563 | if p.group # 0 then _ := C.setpgid(pid, I32(g)); end; |
| 1482 | 1564 | if p.foreground then _ := C.tcsetpgrp(0, I32(g)); end; |
| 1483 | 1565 | end; |
| 1484 | | - _ := C.sigprocmask(2, ^U8(ADR(old)), nil); |
| 1566 | + _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(old)), nil); |
| 1485 | 1567 | munmap(^U8(argv), size); |
| 1486 | 1568 | if pid < 0 then return err; end; |
| 1487 | 1569 | return INT(pid); |
| | @@ -1498,7 +1580,7 @@ |
| 1498 | 1580 | proc exec*(p: Program): INT; |
| 1499 | 1581 | var |
| 1500 | 1582 | 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; |
| 1502 | 1584 | begin |
| 1503 | 1585 | if not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) |
| 1504 | 1586 | if p.tty # 0 then |
| | @@ -1524,14 +1606,14 @@ |
| 1524 | 1606 | (* every signal as it is at first and none blocked, as spawn |
| 1525 | 1607 | gives them, each kept to give back if execve fails *) |
| 1526 | 1608 | 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); |
| 1530 | 1612 | _ := C.execve(ADR(a[0]), argv, argv + (LEN(p.args) + 1)); |
| 1531 | 1613 | 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); |
| 1535 | 1617 | end; |
| 1536 | 1618 | munmap(^U8(argv), size); |
| 1537 | 1619 | return r; |
| | @@ -1560,12 +1642,12 @@ |
| 1560 | 1642 | its exit status; killed or stopped, with the signal's number. Without |
| 1561 | 1643 | block it doesn't wait: 0 and running if none has changed. Minus an |
| 1562 | 1644 | 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. *) |
| 1564 | 1646 | proc wait*(pid: INT; block: BOOL): (INT, Status, INT); |
| 1565 | 1647 | var status, options, r: I32; |
| 1566 | 1648 | 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; |
| 1569 | 1651 | r := C.waitpid(I32(pid), ADR(status), options); |
| 1570 | 1652 | if r < 0 then return failed(), running, 0; end; |
| 1571 | 1653 | if r = 0 then return 0, running, 0; end; |
| | @@ -1587,13 +1669,32 @@ |
| 1587 | 1669 | proc onSignal(sig: I32); |
| 1588 | 1670 | var e: ^I32; saved: I32; b: [1]U8; |
| 1589 | 1671 | begin |
| 1590 | | - e := C.__errno_location(); |
| 1672 | + e := CAbi.errno(); |
| 1591 | 1673 | saved := e^; (* as the code it stopped left it *) |
| 1592 | 1674 | b[0] := U8(sig); |
| 1593 | 1675 | _ := C.write(sigWrite, ADR(b[0]), 1); |
| 1594 | 1676 | e^ := saved; |
| 1595 | 1677 | end; |
| 1596 | 1678 | |
| 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 | + |
| 1597 | 1698 | (* Catches the signals sigs: they don't do what they would, but give |
| 1598 | 1699 | their numbers on an fd, which a later catch adds to and gives again; |
| 1599 | 1700 | or minus an errno. *) |
| | @@ -1601,17 +1702,14 @@ |
| 1601 | 1702 | var fds: [2]I32; act: Sigaction; i: INT; |
| 1602 | 1703 | begin |
| 1603 | 1704 | 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 *) |
| 1608 | 1706 | end; |
| 1609 | 1707 | 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; |
| 1611 | 1709 | sigRead := fds[0]; |
| 1612 | 1710 | sigWrite := fds[1]; |
| 1613 | 1711 | end; |
| 1614 | | - act := {handler: onSignal, flags: 10000000H}; (* SA_RESTART *) |
| 1712 | + act := action(onSignal, CAbi.saRestart); |
| 1615 | 1713 | for i := 0 to LEN(sigs) - 1 do |
| 1616 | 1714 | if C.sigaction(I32(sigs[i]), ^U8(ADR(act)), nil) # 0 then return failed(); end; |
| 1617 | 1715 | end; |
| | @@ -1631,7 +1729,7 @@ |
| 1631 | 1729 | proc ignore*(sig: INT; yes: BOOL): INT; |
| 1632 | 1730 | var act: Sigaction; |
| 1633 | 1731 | begin |
| 1634 | | - if yes then act.handler := Handler(1); end; (* SIG_IGN *) |
| 1732 | + if yes then act := action(Handler(1), 0); end; (* SIG_IGN *) |
| 1635 | 1733 | if C.sigaction(I32(sig), ^U8(ADR(act)), nil) # 0 then return failed(); end; |
| 1636 | 1734 | return 0; |
| 1637 | 1735 | end; |
| | @@ -1659,11 +1757,8 @@ |
| 1659 | 1757 | proc handle*(sig: INT): INT; |
| 1660 | 1758 | var act: Sigaction; |
| 1661 | 1759 | 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 *) |
| 1667 | 1762 | if C.sigaction(I32(sig), ^U8(ADR(act)), nil) # 0 then return failed(); end; |
| 1668 | 1763 | return 0; |
| 1669 | 1764 | end; |
| | @@ -1672,7 +1767,7 @@ |
| 1672 | 1767 | it was asked. *) |
| 1673 | 1768 | proc came*(sig: INT): BOOL; |
| 1674 | 1769 | begin |
| 1675 | | - if (sig < 1) or (sig > 64) then return false; end; |
| 1770 | + if (sig < 1) or (sig > CAbi.sigMax) then return false; end; |
| 1676 | 1771 | return XCHG(ADR(noted[sig]), 0) = 1; |
| 1677 | 1772 | end; |
| 1678 | 1773 | |
| | @@ -1779,7 +1874,7 @@ |
| 1779 | 1874 | if (r # child) or (st # exited) or (c # 127) then exit(2); end; |
| 1780 | 1875 | |
| 1781 | 1876 | 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; |
| 1783 | 1878 | if (caught(sigs) # 0) or (ignore(sigInt, true) # 0) then exit(2); end; |
| 1784 | 1879 | child := spawn({path: "/bin/sh", args: ["sh", "-c", "kill -INT $$; exit 0"]}); |
| 1785 | 1880 | if not ready(sigs, 5000) or (caught(sigs) # sigChld) then exit(2); end; |
| | @@ -1913,7 +2008,7 @@ |
| 1913 | 2008 | a datagram to a port where no one listens, refused; servers on |
| 1914 | 2009 | ports the system chooses, of streams and of datagrams. *) |
| 1915 | 2010 | 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; |
| 1917 | 2012 | begin |
| 1918 | 2013 | t := clock(); |
| 1919 | 2014 | sleep(20); |
| | @@ -1922,16 +2017,21 @@ |
| 1922 | 2017 | random(x); |
| 1923 | 2018 | random(y); |
| 1924 | 2019 | 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 |
| 1927 | 2024 | exit(2); |
| 1928 | 2025 | 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 |
| 1932 | 2028 | exit(2); |
| 1933 | 2029 | 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; |
| 1935 | 2035 | if (connectTcp("1.2.3", 80) # -22) or (connectTcp("1.2.3.256", 80) # -22) or (connectTcp("1::2::3", 80) # -22) |
| 1936 | 2036 | or (connectTcp("1:2:3:4:5:6:7:8:9", 80) # -22) or (connectTcp("1:", 80) # -22) |
| 1937 | 2037 | or (connectTcp("12345::", 80) # -22) or (connectTcp("::1", 65536) # -22) then |