| 1 | (* System calls on Linux for x86-64. Other targets supply their own |
| 2 | Sys, with the same exports. *) |
| 3 | module Sys; |
| 4 | import SYSTEM; |
| 5 | |
| 6 | const |
| 7 | stdin* = 0; (* standard input's file descriptor *) |
| 8 | stdout* = 1; (* standard output's *) |
| 9 | stderr* = 2; (* standard error's *) |
| 10 | (* the bytes Out keeps before it writes them; a target with little |
| 11 | memory keeps fewer *) |
| 12 | outSize* = 4096; |
| 13 | (* signals, by their numbers on Linux *) |
| 14 | sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; |
| 15 | sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; |
| 16 | sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; |
| 17 | sigUsr1* = 10; sigUsr2* = 12; sigAlrm* = 14; |
| 18 | (* the limits limit and setLimit take, by their numbers on Linux *) |
| 19 | limitCpu* = 0; (* seconds of the processor *) |
| 20 | limitFileSize* = 1; (* bytes in a file it writes *) |
| 21 | limitData* = 2; (* bytes of its data *) |
| 22 | limitStack* = 3; (* bytes of its stack *) |
| 23 | limitCore* = 4; (* bytes of a core dump *) |
| 24 | limitProcs* = 6; (* processes of its user *) |
| 25 | limitFiles* = 7; (* one more than its highest fd *) |
| 26 | limitMemory* = 9; (* bytes of its address space *) |
| 27 | (* errors, minus Linux's errnos, as on every target *) |
| 28 | errPerm* = -1; errNoEntry* = -2; errNoProcess* = -3; errIntr* = -4; |
| 29 | errIo* = -5; errBadFd* = -9; errChild* = -10; errAgain* = -11; |
| 30 | errNoMemory* = -12; errDenied* = -13; errExists* = -17; |
| 31 | errNotDir* = -20; errIsDir* = -21; errInvalid* = -22; |
| 32 | errNotTerminal* = -25; errNoSpace* = -28; errReadOnly* = -30; errPipe* = -32; |
| 33 | errNameTooLong* = -36; errNotEmpty* = -39; errLoop* = -40; |
| 34 | errAddrInUse* = -98; errNetUnreach* = -101; errConnAborted* = -103; errConnReset* = -104; |
| 35 | errTimedOut* = -110; errRefused* = -111; errHostUnreach* = -113; |
| 36 | errInProgress* = -115; |
| 37 | |
| 38 | type |
| 39 | (* One descriptor poll waits on: whether to wait to read from it, to |
| 40 | write to it, or both, and, when poll returns, whether it may be. |
| 41 | One that has reached its end, has failed or isn't open is ready |
| 42 | too, so that a read or a write finds out. *) |
| 43 | Poll* = record |
| 44 | fd*: INT; (* the file descriptor; a negative one is never ready *) |
| 45 | read*: BOOL; (* wait until it has something to read *) |
| 46 | write*: BOOL; (* wait until it has room to write *) |
| 47 | ready*: BOOL; (* set by poll: a read or a write on it won't wait *) |
| 48 | end; |
| 49 | |
| 50 | (* What open is asked for: reading, writing or both, and what else |
| 51 | it does to the file; nonblocking, that open doesn't wait (for a |
| 52 | fifo's other end) and reads and writes on the file don't either. *) |
| 53 | Access* = (reading, writing, creating, exclusive, truncating, |
| 54 | appending, executing, nonblocking); |
| 55 | Accesses* = set of Access; (* what open is given *) |
| 56 | |
| 57 | (* What info says of a file. *) |
| 58 | Info* = record |
| 59 | size*: I64; (* in bytes *) |
| 60 | mtime*: I64; (* its contents' last change, in ns since 1970 *) |
| 61 | seconds*: I64; (* mtime in whole seconds, rounded down: exact past 2262 and before 1677, where mtime wraps *) |
| 62 | mode*: INT; (* its permission bits, rwxrwxrwx *) |
| 63 | dir*: BOOL; (* a directory *) |
| 64 | file*: BOOL; (* not a directory, device, pipe or socket *) |
| 65 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 66 | ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) |
| 67 | dev*: U64; (* the file system it is on; with ino, it names the file *) |
| 68 | links*: U64; (* how many names it has: 1 for a file no hard link shares *) |
| 69 | special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) |
| 70 | uid*: INT; (* its owner's user number *) |
| 71 | gid*: INT; (* its group's number *) |
| 72 | end; |
| 73 | |
| 74 | (* What a directory says a name in it is, without asking the file: |
| 75 | a regular file, a directory, a symbolic link, or something else |
| 76 | (a device, pipe or socket); unknown where the file system |
| 77 | doesn't say, and linkInfo must be asked. *) |
| 78 | Kind* = (unknown, isFile, isDir, isLink, isOther); |
| 79 | |
| 80 | (* An open directory: getdents64's records, read a block at a time. *) |
| 81 | Dir* = record |
| 82 | fd, pos, len: INT; |
| 83 | kind: Kind; (* of the name nextEntry gave last *) |
| 84 | buf: [8192]U8; |
| 85 | end; |
| 86 | |
| 87 | (* A program for spawn to start. *) |
| 88 | Program* = record |
| 89 | path*: []const U8; |
| 90 | args*: [][]const U8; (* the first is its name *) |
| 91 | env*: [][]const U8; (* NAME=value, each added to this environment *) |
| 92 | onlyEnv*: BOOL; (* env is its whole environment *) |
| 93 | dir*: []const U8; (* its working directory; "" for this program's *) |
| 94 | fds*: [3]INT; (* stdin, stdout, stderr; 0 for this program's *) |
| 95 | group*: INT; (* its group: 0 for this program's, -1 a new one *) |
| 96 | foreground*: BOOL; (* its group gets the terminal on standard input *) |
| 97 | tty*: INT; (* a pseudo-terminal to start a session on, or 0 *) |
| 98 | more*: []INT; (* fds above 2 passed on to it at the same numbers *) |
| 99 | keepIgnored*: BOOL; (* signals this program ignores stay ignored in it *) |
| 100 | end; |
| 101 | (* How a program that wait waited for changed. *) |
| 102 | Status* = (running, exited, killed, stopped); |
| 103 | |
| 104 | (* struct termios as the kernel's TCGETS fills it: 36 bytes *) |
| 105 | Termios = record |
| 106 | iflag, oflag, cflag, lflag: U32; |
| 107 | line: U8; |
| 108 | cc: [19]U8; |
| 109 | end; |
| 110 | (* A terminal's modes, as getMode reads them and setMode sets them. *) |
| 111 | Mode* = record |
| 112 | t: Termios; |
| 113 | end; |
| 114 | |
| 115 | Winsize = record rows, cols, xpixel, ypixel: U16; end; |
| 116 | Pollfd = record fd: I32; events, revents: I16; end; |
| 117 | |
| 118 | (* struct sockaddr_un: AF_UNIX, then the path and its zero *) |
| 119 | SockaddrUn = record |
| 120 | family: U16; |
| 121 | path: [108]U8; |
| 122 | end; |
| 123 | (* struct sockaddr_in or sockaddr_in6: AF_INET and the IPv4 address |
| 124 | in data[0..4], or AF_INET6 and the IPv6 one in data[4..20] *) |
| 125 | Sockaddr = record |
| 126 | family: U16; |
| 127 | port: [2]U8; |
| 128 | data: [24]U8; |
| 129 | end; |
| 130 | (* Whom a datagram came from, as receive says and reply takes: the |
| 131 | address recvfrom fills in, and its size. *) |
| 132 | Peer* = record |
| 133 | a: Sockaddr; |
| 134 | size: INT; |
| 135 | end; |
| 136 | (* Who is at the other end of a connected socket, as peer says. *) |
| 137 | Remote* = record |
| 138 | unix*: BOOL; (* a Unix socket: uid and pid say who; no addr or port *) |
| 139 | ipv6*: BOOL; (* addr is an IPv6 address, 16 bytes; else an IPv4 one, 4 bytes then zeros *) |
| 140 | addr*: [16]U8; (* an IP peer's address *) |
| 141 | port*: INT; (* an IP peer's port *) |
| 142 | uid*, pid*: INT; (* a Unix peer's user and process, as the system recorded them when it connected *) |
| 143 | end; |
| 144 | (* struct msghdr, what sendmsg and recvmsg take: no address, one |
| 145 | buffer, and a control message; the sizes and flags are ints |
| 146 | with room after them, kept zero *) |
| 147 | Msghdr = record |
| 148 | name: ^U8; nameSize: INT; |
| 149 | iov: ^Iovec; iovLen: INT; |
| 150 | control: ^U8; controlSize: INT; |
| 151 | flags: INT; |
| 152 | end; |
| 153 | Iovec = record base: ^U8; size: INT; end; |
| 154 | (* struct cmsghdr, SCM_RIGHTS's: its size, counted to the last fd, |
| 155 | then the fds, as many as sendFds sends *) |
| 156 | Rights = record |
| 157 | size: INT; |
| 158 | level, kind: I32; |
| 159 | fds: [16]I32; |
| 160 | end; |
| 161 | Timespec = record sec, nsec: I64; end; |
| 162 | (* struct sigaction, as rt_sigaction takes it *) |
| 163 | Action = record |
| 164 | handler: proc(sig: INT); |
| 165 | flags: U64; |
| 166 | restorer: proc; |
| 167 | mask: U64; |
| 168 | end; |
| 169 | |
| 170 | var |
| 171 | cwd: [4096]U8; (* what workDir gives *) |
| 172 | linked: [4096]U8; (* what readLink gives *) |
| 173 | saved: Termios; (* the terminal as rawMode found it *) |
| 174 | caughtSigs: U64; (* the signals catch has blocked, bit n - 1 for signal n *) |
| 175 | sigFd: INT; (* the signalfd that gives them, once caughtSigs isn't empty *) |
| 176 | noted: [65]INT; (* 1 at sig when a signal handle took has come, until came asks *) |
| 177 | uts: [390]U8; (* struct utsname, which hostName gives the nodename of *) |
| 178 | (* the environment setEnv made, nil while it is the one the program |
| 179 | started with; its variables, and the pointers it has room for *) |
| 180 | envp: ^^U8; |
| 181 | envLen, envCap: INT; |
| 182 | envText: ^U8; (* where setEnv puts its next NAME=value *) |
| 183 | envLeft: INT; (* and the bytes left there *) |
| 184 | |
| 185 | (* Writes s to the file descriptor fd; the number of bytes written, or |
| 186 | minus an errno. *) |
| 187 | proc write*(fd: INT; s: []const U8): INT; |
| 188 | begin |
| 189 | if LEN(s) = 0 then return 0; end; |
| 190 | return SYSTEM.SYSCALL(1, fd, SYSTEM.PTR(s), LEN(s)); |
| 191 | end; |
| 192 | |
| 193 | (* Reads into buf from fd; the number of bytes read, 0 at the end, or |
| 194 | minus an errno. *) |
| 195 | proc read*(fd: INT; buf: []U8): INT; |
| 196 | begin |
| 197 | if LEN(buf) = 0 then return 0; end; |
| 198 | return SYSTEM.SYSCALL(0, fd, SYSTEM.ADR(buf[0]), LEN(buf)); |
| 199 | end; |
| 200 | |
| 201 | (* path, a C string in buf, or false if it doesn't fit. *) |
| 202 | proc cstr(path: []const U8; buf: []U8): BOOL; |
| 203 | begin |
| 204 | if LEN(path) >= LEN(buf) then return false; end; |
| 205 | COPY(buf, path); |
| 206 | buf[LEN(path)] := 0; |
| 207 | return true; |
| 208 | end; |
| 209 | |
| 210 | (* open's flags for how, with O_CLOEXEC; Linux's numbers *) |
| 211 | proc flags(how: Accesses): I32; |
| 212 | var f: I32; |
| 213 | begin |
| 214 | f := 80000H; (* O_CLOEXEC, with O_RDONLY *) |
| 215 | if writing in how then |
| 216 | f := 1; (* O_WRONLY *) |
| 217 | if reading in how then f := 2; end; (* O_RDWR *) |
| 218 | end; |
| 219 | if creating in how then f := f | 40H; end; |
| 220 | if exclusive in how then f := f | 80H; end; |
| 221 | if truncating in how then f := f | 200H; end; |
| 222 | if appending in how then f := f | 400H; end; |
| 223 | if nonblocking in how then f := f | 800H; end; (* O_NONBLOCK *) |
| 224 | return f; |
| 225 | end; |
| 226 | |
| 227 | (* The file at path, opened as how asks: its file descriptor, closed in |
| 228 | a program exec starts, or minus an errno. A file it creates has the |
| 229 | permission bits mode, less those the umask takes away. *) |
| 230 | proc openMode*(path: []const U8; how: Accesses; mode: INT): INT; |
| 231 | var buf: [4096]U8; |
| 232 | begin |
| 233 | if executing in how then return -22; end; (* EINVAL: access asks it, open can't *) |
| 234 | if not cstr(path, buf) then return -36; end; (* ENAMETOOLONG *) |
| 235 | return SYSTEM.SYSCALL(2, SYSTEM.ADR(buf), flags(how), mode); |
| 236 | end; |
| 237 | |
| 238 | (* openMode with 644: a file it creates is readable by all, writable |
| 239 | by its owner. *) |
| 240 | proc open*(path: []const U8; how: Accesses): INT; |
| 241 | begin |
| 242 | return openMode(path, how, 1A4H); (* 644 *) |
| 243 | end; |
| 244 | |
| 245 | (* The file at path, opened for reading, closed in a program exec |
| 246 | starts. *) |
| 247 | proc openRead*(path: []const U8): INT; |
| 248 | begin |
| 249 | return open(path, {reading}); |
| 250 | end; |
| 251 | |
| 252 | (* The file at path, made empty or new, opened for writing, closed in a |
| 253 | program exec starts. *) |
| 254 | proc create*(path: []const U8): INT; |
| 255 | begin |
| 256 | return open(path, {writing, creating, truncating}); |
| 257 | end; |
| 258 | |
| 259 | (* Moves fd to pos from the start of its file, or from the end if |
| 260 | fromEnd: the new position, or minus an errno. *) |
| 261 | proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; |
| 262 | begin |
| 263 | if fromEnd then return SYSTEM.SYSCALL(8, fd, pos, 2); end; (* lseek, SEEK_END *) |
| 264 | return SYSTEM.SYSCALL(8, fd, pos, 0); (* SEEK_SET *) |
| 265 | end; |
| 266 | |
| 267 | (* Where in its file fd is, without moving: the next read or write's |
| 268 | position, or minus an errno. *) |
| 269 | proc position*(fd: INT): I64; |
| 270 | begin |
| 271 | return SYSTEM.SYSCALL(8, fd, 0, 1); (* lseek, SEEK_CUR *) |
| 272 | end; |
| 273 | |
| 274 | (* The file fd makes size bytes long, cut or filled with zeros; its |
| 275 | position stays. 0, or minus an errno. *) |
| 276 | proc truncate*(fd: INT; size: I64): INT; |
| 277 | begin |
| 278 | return SYSTEM.SYSCALL(77, fd, size); (* ftruncate *) |
| 279 | end; |
| 280 | |
| 281 | (* Returns once what was written to fd is on the disk: 0, or minus an |
| 282 | errno. *) |
| 283 | proc sync*(fd: INT): INT; |
| 284 | begin |
| 285 | return SYSTEM.SYSCALL(74, fd); (* fsync *) |
| 286 | end; |
| 287 | |
| 288 | (* Returns once everything written to the file system that holds fd |
| 289 | is on the disk, as a sync of each file would, but at once: 0, or |
| 290 | minus an errno. *) |
| 291 | proc syncAll*(fd: INT): INT; |
| 292 | begin |
| 293 | return SYSTEM.SYSCALL(306, fd); (* syncfs *) |
| 294 | end; |
| 295 | |
| 296 | (* Takes the lock on the open file fd, which only one program holds at |
| 297 | a time, and which goes when fd is closed or the program ends, |
| 298 | however it ends. If another program holds it, waits for it if wait, |
| 299 | and otherwise returns -11 (EAGAIN). 0, or minus an errno. *) |
| 300 | proc lock*(fd: INT; wait: BOOL): INT; |
| 301 | begin |
| 302 | if wait then return SYSTEM.SYSCALL(73, fd, 2); end; (* flock, LOCK_EX *) |
| 303 | return SYSTEM.SYSCALL(73, fd, 2 | 4); (* LOCK_EX | LOCK_NB *) |
| 304 | end; |
| 305 | |
| 306 | (* Takes the lock on the open file fd as lock does, but shared: any |
| 307 | number of programs may hold it so at once, as readers do, while none |
| 308 | holds it as lock takes it. 0, or minus an errno. *) |
| 309 | proc lockShared*(fd: INT; wait: BOOL): INT; |
| 310 | begin |
| 311 | if wait then return SYSTEM.SYSCALL(73, fd, 1); end; (* flock, LOCK_SH *) |
| 312 | return SYSTEM.SYSCALL(73, fd, 1 | 4); (* LOCK_SH | LOCK_NB *) |
| 313 | end; |
| 314 | |
| 315 | (* Closes the file descriptor fd. *) |
| 316 | proc close*(fd: INT); |
| 317 | begin |
| 318 | _ := SYSTEM.SYSCALL(3, fd); |
| 319 | end; |
| 320 | |
| 321 | (* Closes the file descriptor fd, as close does, and says whether that |
| 322 | failed: 0, or minus an errno. A file system may report only at the |
| 323 | close that what was written to the file never reached it, -28 |
| 324 | (ENOSPC) or -122 (EDQUOT) say, as NFS does; a program that must know |
| 325 | its writes landed asks so. The fd is closed either way, so it is never |
| 326 | closed again, after -4 (EINTR) too. *) |
| 327 | proc closeChecked*(fd: INT): INT; |
| 328 | begin |
| 329 | return SYSTEM.SYSCALL(3, fd); |
| 330 | end; |
| 331 | |
| 332 | (* A new file descriptor for the file fd is open on, the lowest free: |
| 333 | it, or minus an errno. *) |
| 334 | proc dup*(fd: INT): INT; |
| 335 | begin |
| 336 | return SYSTEM.SYSCALL(32, fd); (* dup *) |
| 337 | end; |
| 338 | |
| 339 | (* Makes the file descriptor into stand for the file fd is open on, |
| 340 | closing what into was first: into, or minus an errno. *) |
| 341 | proc dupTo*(fd, into: INT): INT; |
| 342 | begin |
| 343 | return SYSTEM.SYSCALL(33, fd, into); (* dup2 *) |
| 344 | end; |
| 345 | |
| 346 | (* A new file descriptor for the file fd is open on, the lowest free |
| 347 | from least on, and closed on exec: it, or minus an errno. A shell |
| 348 | keeps its own at 10 and above, out of the way of those a script |
| 349 | names. *) |
| 350 | proc dupAbove*(fd, least: INT): INT; |
| 351 | begin |
| 352 | return SYSTEM.SYSCALL(72, fd, 1030, least); (* fcntl: F_DUPFD_CLOEXEC *) |
| 353 | end; |
| 354 | |
| 355 | (* Whether fd stays open in the program exec runs in this one's place: |
| 356 | yes, or no, closed then, as the fds open, pipe and socket give are at |
| 357 | first. 0, or minus an |
| 358 | errno. *) |
| 359 | proc keepOnExec*(fd: INT; yes: BOOL): INT; |
| 360 | var f: INT; |
| 361 | begin |
| 362 | f := SYSTEM.SYSCALL(72, fd, 1); (* fcntl: F_GETFD *) |
| 363 | if f < 0 then return f; end; |
| 364 | if yes then f := f & ~1; else f := f | 1; end; (* FD_CLOEXEC *) |
| 365 | return SYSTEM.SYSCALL(72, fd, 2, f); (* F_SETFD *) |
| 366 | end; |
| 367 | |
| 368 | (* size bytes of fresh, zeroed memory from the system, a multiple of |
| 369 | pages, or nil. *) |
| 370 | proc mmap*(size: INT): ^U8; |
| 371 | var p: INT; |
| 372 | begin |
| 373 | p := SYSTEM.SYSCALL(9, 0, size, 3, 22H, -1, 0); (* read, write; private, anonymous *) |
| 374 | if p < 0 then return nil; end; |
| 375 | return ^U8(p); |
| 376 | end; |
| 377 | |
| 378 | (* Gives back to the system the size bytes at p that mmap gave. *) |
| 379 | proc munmap*(p: ^U8; size: INT); |
| 380 | begin |
| 381 | _ := SYSTEM.SYSCALL(11, p, size); |
| 382 | end; |
| 383 | |
| 384 | (* A file in memory, with no name, of size zeroed bytes: its fd, or |
| 385 | minus an errno. It is closed in a program exec starts. *) |
| 386 | proc memFile*(size: INT): INT; |
| 387 | var name: [4]U8; fd, r: INT; |
| 388 | begin |
| 389 | _ := cstr("raz", name); (* the name /proc shows *) |
| 390 | fd := SYSTEM.SYSCALL(319, SYSTEM.ADR(name[0]), 1); (* memfd_create, MFD_CLOEXEC *) |
| 391 | if fd < 0 then return fd; end; |
| 392 | r := SYSTEM.SYSCALL(77, fd, size); (* ftruncate *) |
| 393 | if r < 0 then close(fd); return r; end; |
| 394 | return fd; |
| 395 | end; |
| 396 | |
| 397 | (* The first size bytes of the file fd, mapped to be read and written, |
| 398 | and shared: what this program writes there, every program that maps |
| 399 | the file sees. nil if it can't be; munmap gives them back. *) |
| 400 | proc mapFile*(fd, size: INT): ^U8; |
| 401 | var p: INT; |
| 402 | begin |
| 403 | p := SYSTEM.SYSCALL(9, 0, size, 3, 1, fd, 0); (* mmap: read, write; MAP_SHARED *) |
| 404 | if p < 0 then return nil; end; |
| 405 | return ^U8(p); |
| 406 | end; |
| 407 | |
| 408 | (* The first size bytes of the file fd, which may be open only to |
| 409 | read, mapped to be read only: writing there stops the program. nil |
| 410 | if it can't be; munmap gives them back. *) |
| 411 | proc mapRead*(fd, size: INT): ^U8; |
| 412 | var p: INT; |
| 413 | begin |
| 414 | p := SYSTEM.SYSCALL(9, 0, size, 1, 2, fd, 0); (* mmap: read; MAP_PRIVATE *) |
| 415 | if p < 0 then return nil; end; |
| 416 | return ^U8(p); |
| 417 | end; |
| 418 | |
| 419 | (* Lets the pages from p, size bytes from a page boundary, be read and |
| 420 | written as read and write say; neither, and touching them stops the |
| 421 | program. 0, or minus an errno. *) |
| 422 | proc protect*(p: ^U8; size: INT; read, write: BOOL): INT; |
| 423 | var prot: INT; |
| 424 | begin |
| 425 | prot := 0; (* PROT_NONE *) |
| 426 | if read then prot := 1; end; |
| 427 | if write then prot := prot | 2; end; |
| 428 | return SYSTEM.SYSCALL(10, p, size, prot); (* mprotect *) |
| 429 | end; |
| 430 | |
| 431 | (* Whether the file descriptor fd is a terminal: whether it has a |
| 432 | terminal's settings to read (ioctl TCGETS). *) |
| 433 | proc isTerminal*(fd: INT): BOOL; |
| 434 | var t: [8]INT; (* struct termios: 60 bytes *) |
| 435 | begin |
| 436 | return SYSTEM.SYSCALL(16, fd, 5401H, SYSTEM.ADR(t)) = 0; |
| 437 | end; |
| 438 | |
| 439 | (* Renames old to new, replacing what new names: 0, or minus an errno. *) |
| 440 | proc rename*(old, new: []const U8): INT; |
| 441 | var a, b: [4096]U8; |
| 442 | begin |
| 443 | if not cstr(old, a) or not cstr(new, b) then return -36; end; |
| 444 | return SYSTEM.SYSCALL(82, SYSTEM.ADR(a), SYSTEM.ADR(b)); |
| 445 | end; |
| 446 | |
| 447 | (* Removes the file at path; not a directory. *) |
| 448 | proc remove*(path: []const U8): INT; |
| 449 | var a: [4096]U8; |
| 450 | begin |
| 451 | if not cstr(path, a) then return -36; end; |
| 452 | return SYSTEM.SYSCALL(87, SYSTEM.ADR(a)); (* unlink *) |
| 453 | end; |
| 454 | |
| 455 | (* Makes a directory at path, readable by all, writable by its owner. *) |
| 456 | proc makeDir*(path: []const U8): INT; |
| 457 | var a: [4096]U8; |
| 458 | begin |
| 459 | if not cstr(path, a) then return -36; end; |
| 460 | return SYSTEM.SYSCALL(83, SYSTEM.ADR(a), 1EDH); (* mkdir, mode 755 *) |
| 461 | end; |
| 462 | |
| 463 | (* Makes a directory at path with the permission bits mode, less those |
| 464 | the umask takes away: 1C0H (700) for one only its owner may enter. Of |
| 465 | the bits above them Linux keeps only sticky, 200H. *) |
| 466 | proc makeDirMode*(path: []const U8; mode: INT): INT; |
| 467 | var a: [4096]U8; |
| 468 | begin |
| 469 | if not cstr(path, a) then return -36; end; |
| 470 | return SYSTEM.SYSCALL(83, SYSTEM.ADR(a), mode); (* mkdir *) |
| 471 | end; |
| 472 | |
| 473 | (* Makes a fifo (a named pipe) at path with the permission bits mode, |
| 474 | less those the umask takes away. 0, or minus an errno. *) |
| 475 | proc makeFifo*(path: []const U8; mode: INT): INT; |
| 476 | var a: [4096]U8; |
| 477 | begin |
| 478 | if not cstr(path, a) then return -36; end; |
| 479 | return SYSTEM.SYSCALL(133, SYSTEM.ADR(a), 1000H | (mode & 1FFH), 0); (* mknod, S_IFIFO *) |
| 480 | end; |
| 481 | |
| 482 | |
| 483 | (* Sets the owner and group of what path names, following a symbolic |
| 484 | link: a user or group number, or -1 to leave it as it is. Only |
| 485 | root may give a file away. 0, or minus an errno. *) |
| 486 | proc chown*(path: []const U8; uid, gid: INT): INT; |
| 487 | var a: [4096]U8; |
| 488 | begin |
| 489 | if not cstr(path, a) then return -36; end; |
| 490 | return SYSTEM.SYSCALL(92, SYSTEM.ADR(a), uid, gid); (* chown *) |
| 491 | end; |
| 492 | |
| 493 | |
| 494 | (* Removes the directory at path, which must be empty. *) |
| 495 | proc removeDir*(path: []const U8): INT; |
| 496 | var a: [4096]U8; |
| 497 | begin |
| 498 | if not cstr(path, a) then return -36; end; |
| 499 | return SYSTEM.SYSCALL(84, SYSTEM.ADR(a)); (* rmdir *) |
| 500 | end; |
| 501 | |
| 502 | (* Sets the permission bits of what path names. *) |
| 503 | proc chmod*(path: []const U8; mode: INT): INT; |
| 504 | var a: [4096]U8; |
| 505 | begin |
| 506 | if not cstr(path, a) then return -36; end; |
| 507 | return SYSTEM.SYSCALL(90, SYSTEM.ADR(a), mode); |
| 508 | end; |
| 509 | |
| 510 | (* Sets when the contents of what path names last changed, and when it |
| 511 | was last read, to sec seconds and nsec nanoseconds since 1970, as a |
| 512 | program copying a file keeps its time: Info.seconds gives sec back, |
| 513 | whatever the time. *) |
| 514 | proc setTime*(path: []const U8; sec: I64; nsec: INT): INT; |
| 515 | var a: [4096]U8; t: [2]Timespec; |
| 516 | begin |
| 517 | if not cstr(path, a) then return -36; end; |
| 518 | if (nsec < 0) or (nsec >= 1000000000) then return -22; end; (* EINVAL *) |
| 519 | t[0].sec := sec; |
| 520 | t[0].nsec := nsec; |
| 521 | t[1] := t[0]; |
| 522 | return SYSTEM.SYSCALL(280, -100, SYSTEM.ADR(a), SYSTEM.ADR(t), 0); (* utimensat, from the working directory *) |
| 523 | end; |
| 524 | |
| 525 | (* 0 if this user, by its real ids, may do with path what how asks: |
| 526 | reading, writing and executing; minus an errno, -13 (EACCES) if not. |
| 527 | An empty how asks only whether path is there. *) |
| 528 | proc access*(path: []const U8; how: Accesses): INT; |
| 529 | var a: [4096]U8; m: INT; |
| 530 | begin |
| 531 | if not cstr(path, a) then return -36; end; |
| 532 | m := 0; (* F_OK *) |
| 533 | if reading in how then m := m | 4; end; (* R_OK *) |
| 534 | if writing in how then m := m | 2; end; (* W_OK *) |
| 535 | if executing in how then m := m | 1; end; (* X_OK *) |
| 536 | return SYSTEM.SYSCALL(21, SYSTEM.ADR(a), m); (* access *) |
| 537 | end; |
| 538 | |
| 539 | (* Sets the bits taken from every file this program, and the programs |
| 540 | it starts, make: the mask it was. *) |
| 541 | proc umask*(mask: INT): INT; |
| 542 | begin |
| 543 | return SYSTEM.SYSCALL(95, mask & 1FFH); (* umask *) |
| 544 | end; |
| 545 | |
| 546 | (* Makes path a symbolic link to target. *) |
| 547 | proc symlink*(target, path: []const U8): INT; |
| 548 | var a, b: [4096]U8; |
| 549 | begin |
| 550 | if not cstr(target, a) or not cstr(path, b) then return -36; end; |
| 551 | return SYSTEM.SYSCALL(88, SYSTEM.ADR(a), SYSTEM.ADR(b)); |
| 552 | end; |
| 553 | |
| 554 | (* What the symbolic link at path names, good until the next call; and |
| 555 | 0, or minus an errno. *) |
| 556 | proc readLink*(path: []const U8): ([]const U8, INT); |
| 557 | var a: [4096]U8; n: INT; |
| 558 | begin |
| 559 | if not cstr(path, a) then return "", -36; end; |
| 560 | n := SYSTEM.SYSCALL(89, SYSTEM.ADR(a), SYSTEM.ADR(linked), LEN(linked)); (* readlink *) |
| 561 | if n < 0 then return "", n; end; |
| 562 | if n = LEN(linked) then return "", -36; end; (* it may be cut short *) |
| 563 | return linked[0..n], 0; |
| 564 | end; |
| 565 | |
| 566 | (* An Info from a struct stat, all but symlink. *) |
| 567 | proc fromStat(st: [18]INT): Info; |
| 568 | var i: Info; |
| 569 | begin |
| 570 | i.size := st[6]; (* st_size *) |
| 571 | i.dev := U64(st[0]); (* st_dev *) |
| 572 | i.ino := U64(st[1]); (* st_ino *) |
| 573 | i.links := U64(st[2]); (* st_nlink *) |
| 574 | i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) |
| 575 | i.seconds := st[11]; |
| 576 | i.mode := st[3] & 1FFH; |
| 577 | i.special := st[3] & 0E00H; |
| 578 | i.uid := (st[3] shr 32) & 0FFFFFFFFH; (* st_uid *) |
| 579 | i.gid := st[4] & 0FFFFFFFFH; (* st_gid *) |
| 580 | i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) |
| 581 | i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) |
| 582 | return i; |
| 583 | end; |
| 584 | |
| 585 | (* The size of what path names, whether it is a directory or a regular |
| 586 | file, following a symbolic link, and whether path is one; and 0, or |
| 587 | minus an errno. *) |
| 588 | proc info*(path: []const U8): (Info, INT); |
| 589 | var a: [4096]U8; st: [18]INT; i: Info; r: INT; isLink: BOOL; (* struct stat: 144 bytes *) |
| 590 | begin |
| 591 | if not cstr(path, a) then return i, -36; end; |
| 592 | r := SYSTEM.SYSCALL(6, SYSTEM.ADR(a), SYSTEM.ADR(st)); (* lstat *) |
| 593 | if r # 0 then return i, r; end; |
| 594 | isLink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) |
| 595 | r := SYSTEM.SYSCALL(4, SYSTEM.ADR(a), SYSTEM.ADR(st)); (* stat *) |
| 596 | if r # 0 then return i, r; end; |
| 597 | i := fromStat(st); |
| 598 | i.symlink := isLink; |
| 599 | return i, 0; |
| 600 | end; |
| 601 | |
| 602 | (* info of path itself: if it is a symbolic link, of the link, not of |
| 603 | what it names, so that symlink is true, dir and file false, and the |
| 604 | size the length of what it names. *) |
| 605 | proc linkInfo*(path: []const U8): (Info, INT); |
| 606 | var a: [4096]U8; st: [18]INT; i: Info; r: INT; |
| 607 | begin |
| 608 | if not cstr(path, a) then return i, -36; end; |
| 609 | r := SYSTEM.SYSCALL(6, SYSTEM.ADR(a), SYSTEM.ADR(st)); (* lstat *) |
| 610 | if r # 0 then return i, r; end; |
| 611 | i := fromStat(st); |
| 612 | i.symlink := st[3] & 0F000H = 0A000H; (* S_IFLNK *) |
| 613 | return i, 0; |
| 614 | end; |
| 615 | |
| 616 | (* info of the file open on fd, which is never a link. *) |
| 617 | proc infoOf*(fd: INT): (Info, INT); |
| 618 | var st: [18]INT; i: Info; r: INT; |
| 619 | begin |
| 620 | r := SYSTEM.SYSCALL(5, fd, SYSTEM.ADR(st)); (* fstat *) |
| 621 | if r # 0 then return i, r; end; |
| 622 | return fromStat(st), 0; |
| 623 | end; |
| 624 | |
| 625 | (* The bytes free to an ordinary user on path's file system, and the |
| 626 | bytes in all. *) |
| 627 | proc space*(path: []const U8): (I64, I64, INT); |
| 628 | var a: [4096]U8; st: [15]INT; r, unit: INT; (* struct statfs: 120 bytes *) |
| 629 | begin |
| 630 | if not cstr(path, a) then return 0, 0, -36; end; |
| 631 | r := SYSTEM.SYSCALL(137, SYSTEM.ADR(a), SYSTEM.ADR(st)); (* statfs *) |
| 632 | if r # 0 then return 0, 0, r; end; |
| 633 | unit := st[9]; (* f_frsize, the unit blocks are counted in *) |
| 634 | if unit = 0 then unit := st[1]; end; (* f_bsize *) |
| 635 | return st[4] * unit, st[2] * unit, 0; (* f_bavail, f_blocks *) |
| 636 | end; |
| 637 | |
| 638 | (* The Kind of a d_type, as getdents64 and readdir give it: DT_REG is |
| 639 | 8, DT_DIR 4, DT_LNK 10, DT_UNKNOWN 0, and the rest (pipes, devices, |
| 640 | sockets) other things. *) |
| 641 | proc kindOf(t: U8): Kind; |
| 642 | begin |
| 643 | if t = 8 then return isFile; |
| 644 | elsif t = 4 then return isDir; |
| 645 | elsif t = 10 then return isLink; |
| 646 | elsif t = 0 then return unknown; |
| 647 | end; |
| 648 | return isOther; |
| 649 | end; |
| 650 | |
| 651 | (* Opens the directory at path, for nextEntry. *) |
| 652 | proc openDir*(var d: Dir; path: []const U8): INT; |
| 653 | var a: [4096]U8; |
| 654 | begin |
| 655 | d.kind := unknown; |
| 656 | d.pos := 0; |
| 657 | d.len := 0; |
| 658 | if not cstr(path, a) then return -36; end; |
| 659 | d.fd := SYSTEM.SYSCALL(2, SYSTEM.ADR(a), 90000H); (* open, O_RDONLY, O_DIRECTORY, O_CLOEXEC *) |
| 660 | if d.fd < 0 then return d.fd; end; |
| 661 | return 0; |
| 662 | end; |
| 663 | |
| 664 | (* The next name in the directory, good until the next call, but . and |
| 665 | .., in no order; false at the end. *) |
| 666 | proc nextEntry*(var d: Dir): ([]const U8, BOOL); |
| 667 | var n, at: INT; |
| 668 | begin |
| 669 | while true do |
| 670 | if d.pos >= d.len then |
| 671 | d.len := SYSTEM.SYSCALL(217, d.fd, SYSTEM.ADR(d.buf), LEN(d.buf)); (* getdents64 *) |
| 672 | d.pos := 0; |
| 673 | if d.len <= 0 then return "", false; end; |
| 674 | end; |
| 675 | (* a record: d_ino and d_off, 8 bytes each; d_reclen, 2; d_type, |
| 676 | 1; then d_name, ending in a zero *) |
| 677 | at := d.pos + 19; |
| 678 | n := 0; |
| 679 | while d.buf[at + n] # 0 do INC(n); end; |
| 680 | d.kind := kindOf(d.buf[d.pos + 18]); |
| 681 | INC(d.pos, INT(d.buf[d.pos + 16]) + INT(d.buf[d.pos + 17]) * 256); |
| 682 | if (d.buf[at..at + n] # ".") and (d.buf[at..at + n] # "..") then |
| 683 | return d.buf[at..at + n], true; |
| 684 | end; |
| 685 | end; |
| 686 | return "", false; |
| 687 | end; |
| 688 | |
| 689 | (* What the name nextEntry gave last is, as the directory says, so that |
| 690 | a walk needn't ask each file with linkInfo; unknown where the file |
| 691 | system doesn't say. A link is isLink, whatever it names. *) |
| 692 | proc entryKind*(const d: Dir): Kind; |
| 693 | begin |
| 694 | return d.kind; |
| 695 | end; |
| 696 | |
| 697 | (* Closes the directory d that openDir opened. *) |
| 698 | proc closeDir*(var d: Dir); |
| 699 | begin |
| 700 | _ := SYSTEM.SYSCALL(3, d.fd); |
| 701 | end; |
| 702 | |
| 703 | (* Puts the terminal on fd into raw mode: no echo, no lines, no signals, |
| 704 | no flow control, no CR to NL, no NL to CR NL; false if fd isn't a |
| 705 | terminal. *) |
| 706 | proc rawMode*(fd: INT): BOOL; |
| 707 | var t: Termios; |
| 708 | begin |
| 709 | if SYSTEM.SYSCALL(16, fd, 5401H, SYSTEM.ADR(saved)) # 0 then return false; end; (* TCGETS *) |
| 710 | t := saved; |
| 711 | t.iflag := t.iflag & ~U32(5EBH); (* IGNBRK BRKINT PARMRK ISTRIP INLCR IGNCR ICRNL IXON *) |
| 712 | t.oflag := t.oflag & ~U32(1); (* OPOST *) |
| 713 | t.cflag := t.cflag & ~U32(130H) | 30H; (* CS8, no PARENB *) |
| 714 | t.lflag := t.lflag & ~U32(804BH); (* ISIG ICANON ECHO ECHONL IEXTEN *) |
| 715 | t.cc[5] := 0; (* VTIME: no time limit *) |
| 716 | t.cc[6] := 1; (* VMIN: a byte at least *) |
| 717 | return SYSTEM.SYSCALL(16, fd, 5402H, SYSTEM.ADR(t)) = 0; (* TCSETS *) |
| 718 | end; |
| 719 | |
| 720 | (* The terminal on fd as rawMode found it. *) |
| 721 | proc restoreMode*(fd: INT); |
| 722 | begin |
| 723 | _ := SYSTEM.SYSCALL(16, fd, 5402H, SYSTEM.ADR(saved)); |
| 724 | end; |
| 725 | |
| 726 | (* The modes of the terminal on fd, as a shell keeps them for each job; |
| 727 | and 0, or minus an errno, -25 (ENOTTY) if it isn't a terminal. *) |
| 728 | proc getMode*(fd: INT): (Mode, INT); |
| 729 | var m: Mode; r: INT; |
| 730 | begin |
| 731 | r := SYSTEM.SYSCALL(16, fd, 5401H, SYSTEM.ADR(m.t)); (* TCGETS *) |
| 732 | return m, r; |
| 733 | end; |
| 734 | |
| 735 | (* Gives the terminal on fd the modes m, once what was written to it |
| 736 | is sent: 0, or minus an errno. *) |
| 737 | proc setMode*(fd: INT; m: Mode): INT; |
| 738 | begin |
| 739 | return SYSTEM.SYSCALL(16, fd, 5403H, SYSTEM.ADR(m.t)); (* TCSETSW *) |
| 740 | end; |
| 741 | |
| 742 | (* The columns and rows of the terminal on fd; 80 by 24 if it doesn't |
| 743 | say. *) |
| 744 | proc termSize*(fd: INT): (INT, INT); |
| 745 | var w: Winsize; |
| 746 | begin |
| 747 | if (SYSTEM.SYSCALL(16, fd, 5413H, SYSTEM.ADR(w)) # 0) or (w.cols = 0) or (w.rows = 0) then (* TIOCGWINSZ *) |
| 748 | return 80, 24; |
| 749 | end; |
| 750 | return INT(w.cols), INT(w.rows); |
| 751 | end; |
| 752 | |
| 753 | (* Whether fd has something to read within ms milliseconds; ms < 0 |
| 754 | waits as long as it takes. *) |
| 755 | proc ready*(fd, ms: INT): BOOL; |
| 756 | var p: Pollfd; |
| 757 | begin |
| 758 | p := {fd: I32(fd), events: 1}; (* POLLIN *) |
| 759 | return SYSTEM.SYSCALL(7, SYSTEM.ADR(p), 1, ms) > 0; (* poll *) |
| 760 | end; |
| 761 | |
| 762 | (* The clock that only goes forward, in nanoseconds: for how long |
| 763 | something takes. *) |
| 764 | proc clock*: I64; |
| 765 | var t: Timespec; |
| 766 | begin |
| 767 | _ := SYSTEM.SYSCALL(228, 1, SYSTEM.ADR(t)); (* clock_gettime: CLOCK_MONOTONIC *) |
| 768 | return t.sec * 1000000000 + t.nsec; |
| 769 | end; |
| 770 | |
| 771 | (* The time in nanoseconds since 1970-01-01, UTC. *) |
| 772 | proc now*: I64; |
| 773 | var t: Timespec; |
| 774 | begin |
| 775 | _ := SYSTEM.SYSCALL(228, 0, SYSTEM.ADR(t)); (* clock_gettime: CLOCK_REALTIME *) |
| 776 | return t.sec * 1000000000 + t.nsec; |
| 777 | end; |
| 778 | |
| 779 | (* Waits ms milliseconds. *) |
| 780 | proc sleep*(ms: INT); |
| 781 | var t: Timespec; at: I64; |
| 782 | begin |
| 783 | at := clock() + I64(ms) * 1000000; |
| 784 | t := {sec: at div 1000000000, nsec: at mod 1000000000}; |
| 785 | repeat until SYSTEM.SYSCALL(230, 1, 1, SYSTEM.ADR(t), 0) # -4; (* clock_nanosleep until t, again after EINTR *) |
| 786 | end; |
| 787 | |
| 788 | (* Ends the program: random bytes can't be faked. *) |
| 789 | proc noRandom; |
| 790 | begin |
| 791 | _ := write(stderr, "Sys.random: the kernel gave no random bytes\n"); |
| 792 | _ := SYSTEM.SYSCALL(231, 2); (* exit *) |
| 793 | end; |
| 794 | |
| 795 | (* Fills b with random bytes, from the kernel. Random bytes can't be |
| 796 | faked, so if the kernel can't give them (no getrandom, or a filter |
| 797 | refusing it) it says so on standard error and ends the program by |
| 798 | exit with status 2. *) |
| 799 | proc random*(b: []U8); |
| 800 | var n, r: INT; |
| 801 | begin |
| 802 | n := 0; |
| 803 | while n < LEN(b) do (* a signal may cut it short *) |
| 804 | r := SYSTEM.SYSCALL(318, SYSTEM.ADR(b[n]), LEN(b) - n, 0); (* getrandom *) |
| 805 | if r > 0 then INC(n, r); |
| 806 | elsif r # errIntr then noRandom(); |
| 807 | end; |
| 808 | end; |
| 809 | end; |
| 810 | |
| 811 | (* A socket of family and kind, connected to the address at a, of size |
| 812 | bytes, or, if serving, bound to it and taking connections: its file |
| 813 | descriptor, or minus an errno. It is closed in a program exec |
| 814 | starts. *) |
| 815 | proc socket(family, kind: INT; a: ^U8; size: INT; serving: BOOL): INT; |
| 816 | var fd, r, one: INT; |
| 817 | begin |
| 818 | fd := SYSTEM.SYSCALL(41, family, kind | 80000H, 0); (* socket, SOCK_CLOEXEC *) |
| 819 | if fd < 0 then return fd; end; |
| 820 | if serving then |
| 821 | one := 1; (* so that a server can start again at once *) |
| 822 | _ := SYSTEM.SYSCALL(54, fd, 1, 2, SYSTEM.ADR(one), 4); (* setsockopt: SOL_SOCKET, SO_REUSEADDR *) |
| 823 | r := SYSTEM.SYSCALL(49, fd, a, size); (* bind *) |
| 824 | if (r = 0) and (kind = 1) then (* a stream socket takes connections; *) |
| 825 | r := SYSTEM.SYSCALL(50, fd, 4096); (* one of datagrams only receives *) |
| 826 | end; |
| 827 | else |
| 828 | r := SYSTEM.SYSCALL(42, fd, a, size); (* connect *) |
| 829 | end; |
| 830 | if r < 0 then close(fd); return r; end; |
| 831 | return fd; |
| 832 | end; |
| 833 | |
| 834 | (* path as a Unix socket's address in a, and the address's length, or |
| 835 | -36 (ENAMETOOLONG) if it is too long. A path that starts with a zero |
| 836 | byte is one of Linux's abstract names, which has no file and no |
| 837 | trailing zero: its length ends with the name's. *) |
| 838 | proc unixAddr(path: []const U8; a: ^SockaddrUn): INT; |
| 839 | begin |
| 840 | if LEN(path) >= LEN(a^.path) then return -36; end; |
| 841 | a^.family := 1; (* AF_UNIX *) |
| 842 | COPY(a^.path, path); |
| 843 | if (LEN(path) > 0) and (path[0] = 0) then return 2 + LEN(path); end; |
| 844 | return SIZE(SockaddrUn); |
| 845 | end; |
| 846 | |
| 847 | (* A stream socket connected to the Unix socket at path. *) |
| 848 | proc connect*(path: []const U8): INT; |
| 849 | var a: SockaddrUn; size: INT; |
| 850 | begin |
| 851 | size := unixAddr(path, SYSTEM.ADR(a)); |
| 852 | if size < 0 then return size; end; |
| 853 | return socket(1, 1, ^U8(SYSTEM.ADR(a)), size, false); (* SOCK_STREAM *) |
| 854 | end; |
| 855 | |
| 856 | (* A stream socket made at path, which must not be there yet, taking |
| 857 | connections. *) |
| 858 | proc listen*(path: []const U8): INT; |
| 859 | var a: SockaddrUn; size: INT; |
| 860 | begin |
| 861 | size := unixAddr(path, SYSTEM.ADR(a)); |
| 862 | if size < 0 then return size; end; |
| 863 | return socket(1, 1, ^U8(SYSTEM.ADR(a)), size, true); |
| 864 | end; |
| 865 | |
| 866 | (* The value of the hexadecimal digit c, or -1 if it isn't one. *) |
| 867 | proc hexDigit(c: U8): INT; |
| 868 | begin |
| 869 | if (c >= '0') and (c <= '9') then return INT(c - '0'); end; |
| 870 | if (c >= 'a') and (c <= 'f') then return INT(c - 'a') + 10; end; |
| 871 | if (c >= 'A') and (c <= 'F') then return INT(c - 'A') + 10; end; |
| 872 | return -1; |
| 873 | end; |
| 874 | |
| 875 | (* The IPv4 address s, four numbers and dots, in out's 4 bytes, or false |
| 876 | if it isn't one. *) |
| 877 | proc ipv4(s: []const U8; out: []U8): BOOL; |
| 878 | var b: [4]U8; n, i, v, d: INT; |
| 879 | begin |
| 880 | i := 0; |
| 881 | for n := 0 to 3 do |
| 882 | if n > 0 then |
| 883 | if (i = LEN(s)) or (s[i] # '.') then return false; end; |
| 884 | INC(i); |
| 885 | end; |
| 886 | v := 0; |
| 887 | d := 0; |
| 888 | while (i < LEN(s)) and (d < 3) and (s[i] >= '0') and (s[i] <= '9') do |
| 889 | v := v * 10 + INT(s[i] - '0'); |
| 890 | INC(i); |
| 891 | INC(d); |
| 892 | end; |
| 893 | if (d = 0) or (v > 255) then return false; end; |
| 894 | b[n] := U8(v); |
| 895 | end; |
| 896 | if i # LEN(s) then return false; end; |
| 897 | for i := 0 to 3 do out[i] := b[i]; end; |
| 898 | return true; |
| 899 | end; |
| 900 | |
| 901 | (* The IPv6 address s, groups of hexadecimal digits and colons, one :: |
| 902 | standing for groups of zeros, in out's 16 bytes, or false if it isn't |
| 903 | one. *) |
| 904 | proc ipv6(s: []const U8; out: []U8): BOOL; |
| 905 | var g: [8]INT; n, gap, i, v, d, k: INT; |
| 906 | begin |
| 907 | n := 0; |
| 908 | gap := -1; (* the group the :: is before *) |
| 909 | i := 0; |
| 910 | if (LEN(s) >= 2) and (s[0..2] = "::") then gap := 0; i := 2; end; |
| 911 | while i < LEN(s) do |
| 912 | if n = 8 then return false; end; |
| 913 | v := 0; |
| 914 | d := 0; |
| 915 | while (i < LEN(s)) and (d < 5) and (hexDigit(s[i]) >= 0) do |
| 916 | v := v * 16 + hexDigit(s[i]); |
| 917 | INC(i); |
| 918 | INC(d); |
| 919 | end; |
| 920 | if (d = 0) or (d > 4) then return false; end; |
| 921 | g[n] := v; |
| 922 | INC(n); |
| 923 | if i < LEN(s) then |
| 924 | if s[i] # ':' then return false; end; |
| 925 | INC(i); |
| 926 | if i = LEN(s) then return false; end; |
| 927 | if s[i] = ':' then |
| 928 | if gap >= 0 then return false; end; |
| 929 | gap := n; |
| 930 | INC(i); |
| 931 | end; |
| 932 | end; |
| 933 | end; |
| 934 | if ((gap < 0) and (n < 8)) or ((gap >= 0) and (n = 8)) then return false; end; |
| 935 | for i := 0 to 15 do out[i] := 0; end; |
| 936 | for i := 0 to n - 1 do |
| 937 | k := i; |
| 938 | if (gap >= 0) and (i >= gap) then k := i + 8 - n; end; (* after the zeros *) |
| 939 | out[2 * k] := U8(g[i] shr 8); |
| 940 | out[2 * k + 1] := U8(g[i] & 255); |
| 941 | end; |
| 942 | return true; |
| 943 | end; |
| 944 | |
| 945 | (* addr, an IPv4 or IPv6 address as numbers, and port, in a: the size |
| 946 | of its struct sockaddr_in or sockaddr_in6, or 0 if either is |
| 947 | wrong. *) |
| 948 | proc inetAddr(addr: []const U8; port: INT; a: ^Sockaddr): INT; |
| 949 | begin |
| 950 | if (port < 0) or (port > 65535) then return 0; end; |
| 951 | a^.port[0] := U8(port shr 8); (* in the network's order, the high byte first *) |
| 952 | a^.port[1] := U8(port & 255); |
| 953 | if ipv4(addr, a^.data[0..4]) then a^.family := 2; return 16; end; (* AF_INET *) |
| 954 | if ipv6(addr, a^.data[4..20]) then a^.family := 10; return 28; end; (* AF_INET6 *) |
| 955 | return 0; |
| 956 | end; |
| 957 | |
| 958 | (* A socket of kind at addr and port, as socket makes it; -22 (EINVAL) |
| 959 | if they are wrong. *) |
| 960 | proc inet(addr: []const U8; port, kind: INT; serving: BOOL): INT; |
| 961 | var a: Sockaddr; size: INT; |
| 962 | begin |
| 963 | size := inetAddr(addr, port, SYSTEM.ADR(a)); |
| 964 | if size = 0 then return -22; end; |
| 965 | return socket(INT(a.family), kind, ^U8(SYSTEM.ADR(a)), size, serving); |
| 966 | end; |
| 967 | |
| 968 | (* A stream socket connected to port at addr, an IPv4 or IPv6 address |
| 969 | as numbers. *) |
| 970 | proc connectTcp*(addr: []const U8; port: INT): INT; |
| 971 | begin |
| 972 | return inet(addr, port, 1, false); (* SOCK_STREAM *) |
| 973 | end; |
| 974 | |
| 975 | (* A socket of datagrams to and from port at addr. *) |
| 976 | proc connectUdp*(addr: []const U8; port: INT): INT; |
| 977 | begin |
| 978 | return inet(addr, port, 2, false); (* SOCK_DGRAM *) |
| 979 | end; |
| 980 | |
| 981 | (* A stream socket taking connections on port at addr; "0.0.0.0" or |
| 982 | "::" is every address the machine has. *) |
| 983 | proc listenTcp*(addr: []const U8; port: INT): INT; |
| 984 | begin |
| 985 | return inet(addr, port, 1, true); |
| 986 | end; |
| 987 | |
| 988 | (* A socket of datagrams on port at addr, from anyone, for receive and |
| 989 | reply. *) |
| 990 | proc listenUdp*(addr: []const U8; port: INT): INT; |
| 991 | begin |
| 992 | return inet(addr, port, 2, true); |
| 993 | end; |
| 994 | |
| 995 | (* The next connection to the socket fd, waiting for one: a socket of |
| 996 | its own, closed in a program exec starts; or minus an errno. *) |
| 997 | proc accept*(fd: INT): INT; |
| 998 | begin |
| 999 | return SYSTEM.SYSCALL(288, fd, 0, 0, 80000H); (* accept4, SOCK_CLOEXEC *) |
| 1000 | end; |
| 1001 | |
| 1002 | (* Ends this side's writing to the socket fd, so that the other end |
| 1003 | reads an end of input and can still answer on it. 0, or minus an |
| 1004 | errno. *) |
| 1005 | proc closeWrite*(fd: INT): INT; |
| 1006 | begin |
| 1007 | return SYSTEM.SYSCALL(48, fd, 1); (* shutdown, SHUT_WR *) |
| 1008 | end; |
| 1009 | |
| 1010 | (* Who is at the other end of the connected socket fd, and 0; or minus |
| 1011 | an errno. *) |
| 1012 | proc peer*(fd: INT): (Remote, INT); |
| 1013 | var r: Remote; a: Sockaddr; size, credSize: I32; cred: [3]I32; e: INT; |
| 1014 | begin |
| 1015 | r := {unix: false, ipv6: false}; |
| 1016 | size := I32(SIZE(Sockaddr)); |
| 1017 | e := SYSTEM.SYSCALL(52, fd, SYSTEM.ADR(a), SYSTEM.ADR(size)); (* getpeername *) |
| 1018 | if e < 0 then return r, e; end; |
| 1019 | if a.family = 1 then (* AF_UNIX: ask who made the connection *) |
| 1020 | credSize := I32(4 * LEN(cred)); |
| 1021 | e := SYSTEM.SYSCALL(55, fd, 1, 17, SYSTEM.ADR(cred), SYSTEM.ADR(credSize)); (* getsockopt: SOL_SOCKET, SO_PEERCRED *) |
| 1022 | if e < 0 then return r, e; end; |
| 1023 | r.unix := true; |
| 1024 | r.pid := INT(cred[0]); |
| 1025 | r.uid := INT(cred[1]); |
| 1026 | else |
| 1027 | r.port := (INT(a.port[0]) shl 8) | INT(a.port[1]); |
| 1028 | if a.family = 10 then (* AF_INET6 *) |
| 1029 | r.ipv6 := true; |
| 1030 | COPY(r.addr[0..16], a.data[4..20]); |
| 1031 | else |
| 1032 | COPY(r.addr[0..4], a.data[0..4]); |
| 1033 | end; |
| 1034 | end; |
| 1035 | return r, 0; |
| 1036 | end; |
| 1037 | |
| 1038 | (* Two connected stream sockets, as a Unix socket and the one that |
| 1039 | connects to it make: their fds and 0, closed in a program exec |
| 1040 | starts; or -1, -1 and minus an errno. *) |
| 1041 | proc socketPair*: (INT, INT, INT); |
| 1042 | var fds: [2]I32; r: INT; |
| 1043 | begin |
| 1044 | r := SYSTEM.SYSCALL(53, 1, 1 | 80000H, 0, SYSTEM.ADR(fds)); (* socketpair: AF_UNIX, SOCK_STREAM, SOCK_CLOEXEC *) |
| 1045 | if r < 0 then return -1, -1, r; end; |
| 1046 | return INT(fds[0]), INT(fds[1]), 0; |
| 1047 | end; |
| 1048 | |
| 1049 | (* Writes s to the socket fd, as write does, but returns -32 (EPIPE) |
| 1050 | where write would stop the program with SIGPIPE: when the other end |
| 1051 | has closed it. *) |
| 1052 | proc send*(fd: INT; s: []const U8): INT; |
| 1053 | begin |
| 1054 | if LEN(s) = 0 then return 0; end; |
| 1055 | return SYSTEM.SYSCALL(44, fd, SYSTEM.PTR(s), LEN(s), 4000H, 0, 0); (* sendto, MSG_NOSIGNAL, no address *) |
| 1056 | end; |
| 1057 | |
| 1058 | (* send, but never waits: returns -11 (EAGAIN) where send would wait for |
| 1059 | room, and doesn't make reads on fd non-blocking too. *) |
| 1060 | proc sendNow*(fd: INT; s: []const U8): INT; |
| 1061 | begin |
| 1062 | if LEN(s) = 0 then return 0; end; |
| 1063 | return SYSTEM.SYSCALL(44, fd, SYSTEM.PTR(s), LEN(s), 4040H, 0, 0); (* sendto, MSG_NOSIGNAL | MSG_DONTWAIT, no address *) |
| 1064 | end; |
| 1065 | |
| 1066 | |
| 1067 | (* Waits up to ms milliseconds (ms < 0: as long as it takes) until one |
| 1068 | of fds may be read or written as each one's read and write say, and |
| 1069 | sets each one's ready. The number ready, 0 on a timeout, or minus an |
| 1070 | errno. It takes as many as it is given: the system's list is built |
| 1071 | on the stack for 64 or fewer, else in memory from mmap, given back |
| 1072 | before it returns (-12 if there is none). *) |
| 1073 | proc poll*(var fds: []Poll; ms: INT): INT; |
| 1074 | var small: [64]Pollfd; list: []Pollfd; big: ^Pollfd; n, i, r: INT; events: I16; |
| 1075 | begin |
| 1076 | n := LEN(fds); |
| 1077 | big := nil; |
| 1078 | if n <= LEN(small) then |
| 1079 | list := small[0..n]; |
| 1080 | else |
| 1081 | big := ^Pollfd(mmap(n * SIZE(Pollfd))); |
| 1082 | if big = nil then return -12; end; (* ENOMEM *) |
| 1083 | list := big[0..n]; |
| 1084 | end; |
| 1085 | for i := 0 to n - 1 do |
| 1086 | events := 0; |
| 1087 | if fds[i].read then events := 1; end; (* POLLIN *) |
| 1088 | if fds[i].write then events := events | 4; end; (* POLLOUT *) |
| 1089 | list[i] := {fd: I32(fds[i].fd), events: events}; |
| 1090 | end; |
| 1091 | r := SYSTEM.SYSCALL(7, SYSTEM.PTR(list), n, ms); (* poll *) |
| 1092 | for i := 0 to n - 1 do |
| 1093 | fds[i].ready := (r > 0) and (list[i].revents # 0); |
| 1094 | end; |
| 1095 | if big # nil then munmap(^U8(big), n * SIZE(Pollfd)); end; |
| 1096 | return r; |
| 1097 | end; |
| 1098 | |
| 1099 | (* The first of reads that has something to read within ms |
| 1100 | milliseconds, or of writes that has room to write, or that has |
| 1101 | reached its end or failed, so that a read or a write won't wait; -1 |
| 1102 | if none has. ms < 0 waits as long as it takes. At most 64 fds in |
| 1103 | all. *) |
| 1104 | proc readyFor*(reads, writes: []const INT; ms: INT): INT; |
| 1105 | var p: [64]Poll; ps: []Poll; n, i: INT; |
| 1106 | begin |
| 1107 | n := 0; |
| 1108 | for i := 0 to LEN(reads) - 1 do |
| 1109 | if n < LEN(p) then |
| 1110 | p[n] := {fd: reads[i], read: true}; |
| 1111 | INC(n); |
| 1112 | end; |
| 1113 | end; |
| 1114 | for i := 0 to LEN(writes) - 1 do |
| 1115 | if n < LEN(p) then |
| 1116 | p[n] := {fd: writes[i], write: true}; |
| 1117 | INC(n); |
| 1118 | end; |
| 1119 | end; |
| 1120 | ps := p[0..n]; |
| 1121 | if poll(ps, ms) <= 0 then return -1; end; |
| 1122 | for i := 0 to n - 1 do |
| 1123 | if p[i].ready then return p[i].fd; end; |
| 1124 | end; |
| 1125 | return -1; |
| 1126 | end; |
| 1127 | |
| 1128 | (* The first of fds that has something to read within ms milliseconds, |
| 1129 | or that has reached its end or failed, so that a read won't wait; -1 |
| 1130 | if none has. ms < 0 waits as long as it takes. At most 64 fds. *) |
| 1131 | proc readyOf*(fds: []const INT; ms: INT): INT; |
| 1132 | begin |
| 1133 | return readyFor(fds, [], ms); |
| 1134 | end; |
| 1135 | |
| 1136 | (* Reads and writes on fd made to wait, as they do at first; or, if not |
| 1137 | yes, to return -11 (EAGAIN) at once where they would wait. 0, or |
| 1138 | minus an errno. *) |
| 1139 | proc setBlocking*(fd: INT; yes: BOOL): INT; |
| 1140 | var flags: INT; |
| 1141 | begin |
| 1142 | flags := SYSTEM.SYSCALL(72, fd, 3, 0); (* fcntl F_GETFL *) |
| 1143 | if flags < 0 then return flags; end; |
| 1144 | if yes then flags := flags & ~INT(800H); else flags := flags | 800H; end; (* O_NONBLOCK *) |
| 1145 | return SYSTEM.SYSCALL(72, fd, 4, flags); (* F_SETFL *) |
| 1146 | end; |
| 1147 | |
| 1148 | (* Whether reads and writes on fd wait, as they do at first: 1 if they |
| 1149 | do, 0 if they return -11 (EAGAIN) at once, or minus an errno. *) |
| 1150 | proc blocking*(fd: INT): INT; |
| 1151 | var flags: INT; |
| 1152 | begin |
| 1153 | flags := SYSTEM.SYSCALL(72, fd, 3, 0); (* fcntl F_GETFL *) |
| 1154 | if flags < 0 then return flags; end; |
| 1155 | if flags & 800H # 0 then return 0; end; (* O_NONBLOCK *) |
| 1156 | return 1; |
| 1157 | end; |
| 1158 | |
| 1159 | (* A stream socket connected to port at addr, as connectTcp makes it, |
| 1160 | or -110 (ETIMEDOUT) if it isn't connected within ms milliseconds. |
| 1161 | It starts to connect without waiting, then waits for the socket to |
| 1162 | be writable, which it is once connected or refused, and asks which. *) |
| 1163 | proc connectTcpWithin*(addr: []const U8; port, ms: INT): INT; |
| 1164 | var a: Sockaddr; size, fd, r: INT; err, errSize: I32; |
| 1165 | begin |
| 1166 | size := inetAddr(addr, port, SYSTEM.ADR(a)); |
| 1167 | if size = 0 then return -22; end; |
| 1168 | fd := SYSTEM.SYSCALL(41, INT(a.family), 1 | 80800H, 0); (* socket: SOCK_STREAM, SOCK_CLOEXEC, SOCK_NONBLOCK *) |
| 1169 | if fd < 0 then return fd; end; |
| 1170 | r := SYSTEM.SYSCALL(42, fd, SYSTEM.ADR(a), size); (* connect *) |
| 1171 | if r = -115 then (* EINPROGRESS *) |
| 1172 | r := -110; |
| 1173 | if readyFor([], [fd], ms) = fd then |
| 1174 | errSize := 4; |
| 1175 | r := SYSTEM.SYSCALL(55, fd, 1, 4, SYSTEM.ADR(err), SYSTEM.ADR(errSize)); (* getsockopt: SOL_SOCKET, SO_ERROR *) |
| 1176 | if r = 0 then r := -INT(err); end; |
| 1177 | end; |
| 1178 | end; |
| 1179 | if r = 0 then r := setBlocking(fd, true); end; |
| 1180 | if r < 0 then close(fd); return r; end; |
| 1181 | return fd; |
| 1182 | end; |
| 1183 | |
| 1184 | (* The port the socket fd is bound to, or minus an errno: for a socket |
| 1185 | made on port 0, the one the system chose. *) |
| 1186 | proc localPort*(fd: INT): INT; |
| 1187 | var a: Sockaddr; size: I32; r: INT; |
| 1188 | begin |
| 1189 | size := I32(SIZE(Sockaddr)); |
| 1190 | r := SYSTEM.SYSCALL(51, fd, SYSTEM.ADR(a), SYSTEM.ADR(size)); (* getsockname *) |
| 1191 | if r < 0 then return r; end; |
| 1192 | return (INT(a.port[0]) shl 8) | INT(a.port[1]); |
| 1193 | end; |
| 1194 | |
| 1195 | (* The next datagram to the socket fd, waiting for one: its first |
| 1196 | LEN(buf) bytes in buf, the rest lost, and who sent it. The number is |
| 1197 | the bytes in buf, or minus an errno. *) |
| 1198 | proc receive*(fd: INT; buf: []U8): (INT, Peer); |
| 1199 | var p: Peer; size: I32; n: INT; |
| 1200 | begin |
| 1201 | size := I32(SIZE(Sockaddr)); |
| 1202 | n := SYSTEM.SYSCALL(45, fd, SYSTEM.PTR(buf), LEN(buf), 0, SYSTEM.ADR(p.a), SYSTEM.ADR(size)); (* recvfrom *) |
| 1203 | p.size := INT(size); |
| 1204 | return n, p; |
| 1205 | end; |
| 1206 | |
| 1207 | (* Sends s, as one datagram, from the socket fd to peer, as receive |
| 1208 | gave it: the bytes sent, or minus an errno. *) |
| 1209 | proc reply*(fd: INT; peer: Peer; s: []const U8): INT; |
| 1210 | begin |
| 1211 | return SYSTEM.SYSCALL(44, fd, SYSTEM.PTR(s), LEN(s), 4000H, SYSTEM.ADR(peer.a), peer.size); (* sendto, MSG_NOSIGNAL *) |
| 1212 | end; |
| 1213 | |
| 1214 | (* Sends s, at least one byte, to the Unix socket fd, as send does, and |
| 1215 | with it the files fds, at most 16, which the program that receives |
| 1216 | them then has open too: the bytes sent, or minus an errno. *) |
| 1217 | proc sendFds*(fd: INT; s: []const U8; fds: []const INT): INT; |
| 1218 | var m: Msghdr; v: Iovec; c: Rights; i: INT; |
| 1219 | begin |
| 1220 | if (LEN(s) = 0) or (LEN(fds) > LEN(c.fds)) then return -22; end; |
| 1221 | v := {base: SYSTEM.PTR(s), size: LEN(s)}; |
| 1222 | m := {iov: SYSTEM.ADR(v), iovLen: 1}; |
| 1223 | if LEN(fds) > 0 then |
| 1224 | c := {size: 16 + 4 * LEN(fds), level: 1, kind: 1}; (* SOL_SOCKET, SCM_RIGHTS *) |
| 1225 | for i := 0 to LEN(fds) - 1 do c.fds[i] := I32(fds[i]); end; |
| 1226 | m.control := ^U8(SYSTEM.ADR(c)); |
| 1227 | m.controlSize := (c.size + 7) & ~7; (* CMSG_SPACE: whole quadwords *) |
| 1228 | end; |
| 1229 | return SYSTEM.SYSCALL(46, fd, SYSTEM.ADR(m), 4000H); (* sendmsg, MSG_NOSIGNAL *) |
| 1230 | end; |
| 1231 | |
| 1232 | (* Reads into buf from the Unix socket fd, as read does, and puts the |
| 1233 | files sent with what it read in fds, as many as fit, closing the |
| 1234 | rest: the bytes read, or minus an errno, and the files put in fds. |
| 1235 | Each is closed in a program exec starts. *) |
| 1236 | proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); |
| 1237 | var m: Msghdr; v: Iovec; c: Rights; n, k, i: INT; |
| 1238 | begin |
| 1239 | v := {base: SYSTEM.PTR(buf), size: LEN(buf)}; |
| 1240 | m := {iov: SYSTEM.ADR(v), iovLen: 1, control: ^U8(SYSTEM.ADR(c)), controlSize: SIZE(Rights)}; |
| 1241 | n := SYSTEM.SYSCALL(47, fd, SYSTEM.ADR(m), 40000000H); (* recvmsg, MSG_CMSG_CLOEXEC *) |
| 1242 | if n < 0 then return n, 0; end; |
| 1243 | k := 0; |
| 1244 | if (m.controlSize >= 16) and (c.level = 1) and (c.kind = 1) then |
| 1245 | for i := 0 to (c.size - 16) div 4 - 1 do |
| 1246 | if k < LEN(fds) then |
| 1247 | fds[k] := INT(c.fds[i]); |
| 1248 | INC(k); |
| 1249 | else |
| 1250 | close(INT(c.fds[i])); |
| 1251 | end; |
| 1252 | end; |
| 1253 | end; |
| 1254 | return n, k; |
| 1255 | end; |
| 1256 | |
| 1257 | const |
| 1258 | (* what each errno means, as glibc's strerror says it; 41 and 58 |
| 1259 | are none *) |
| 1260 | errorTexts: [134][]const U8 = [ |
| 1261 | "Success", (* 0 *) |
| 1262 | "Operation not permitted", (* 1, EPERM *) |
| 1263 | "No such file or directory", (* 2, ENOENT *) |
| 1264 | "No such process", (* 3, ESRCH *) |
| 1265 | "Interrupted system call", (* 4, EINTR *) |
| 1266 | "Input/output error", (* 5, EIO *) |
| 1267 | "No such device or address", (* 6, ENXIO *) |
| 1268 | "Argument list too long", (* 7, E2BIG *) |
| 1269 | "Exec format error", (* 8, ENOEXEC *) |
| 1270 | "Bad file descriptor", (* 9, EBADF *) |
| 1271 | "No child processes", (* 10, ECHILD *) |
| 1272 | "Resource temporarily unavailable", (* 11, EAGAIN *) |
| 1273 | "Cannot allocate memory", (* 12, ENOMEM *) |
| 1274 | "Permission denied", (* 13, EACCES *) |
| 1275 | "Bad address", (* 14, EFAULT *) |
| 1276 | "Block device required", (* 15, ENOTBLK *) |
| 1277 | "Device or resource busy", (* 16, EBUSY *) |
| 1278 | "File exists", (* 17, EEXIST *) |
| 1279 | "Invalid cross-device link", (* 18, EXDEV *) |
| 1280 | "No such device", (* 19, ENODEV *) |
| 1281 | "Not a directory", (* 20, ENOTDIR *) |
| 1282 | "Is a directory", (* 21, EISDIR *) |
| 1283 | "Invalid argument", (* 22, EINVAL *) |
| 1284 | "Too many open files in system", (* 23, ENFILE *) |
| 1285 | "Too many open files", (* 24, EMFILE *) |
| 1286 | "Inappropriate ioctl for device", (* 25, ENOTTY *) |
| 1287 | "Text file busy", (* 26, ETXTBSY *) |
| 1288 | "File too large", (* 27, EFBIG *) |
| 1289 | "No space left on device", (* 28, ENOSPC *) |
| 1290 | "Illegal seek", (* 29, ESPIPE *) |
| 1291 | "Read-only file system", (* 30, EROFS *) |
| 1292 | "Too many links", (* 31, EMLINK *) |
| 1293 | "Broken pipe", (* 32, EPIPE *) |
| 1294 | "Numerical argument out of domain", (* 33, EDOM *) |
| 1295 | "Numerical result out of range", (* 34, ERANGE *) |
| 1296 | "Resource deadlock avoided", (* 35, EDEADLOCK *) |
| 1297 | "File name too long", (* 36, ENAMETOOLONG *) |
| 1298 | "No locks available", (* 37, ENOLCK *) |
| 1299 | "Function not implemented", (* 38, ENOSYS *) |
| 1300 | "Directory not empty", (* 39, ENOTEMPTY *) |
| 1301 | "Too many levels of symbolic links", (* 40, ELOOP *) |
| 1302 | "unknown error", (* 41 *) |
| 1303 | "No message of desired type", (* 42, ENOMSG *) |
| 1304 | "Identifier removed", (* 43, EIDRM *) |
| 1305 | "Channel number out of range", (* 44, ECHRNG *) |
| 1306 | "Level 2 not synchronized", (* 45, EL2NSYNC *) |
| 1307 | "Level 3 halted", (* 46, EL3HLT *) |
| 1308 | "Level 3 reset", (* 47, EL3RST *) |
| 1309 | "Link number out of range", (* 48, ELNRNG *) |
| 1310 | "Protocol driver not attached", (* 49, EUNATCH *) |
| 1311 | "No CSI structure available", (* 50, ENOCSI *) |
| 1312 | "Level 2 halted", (* 51, EL2HLT *) |
| 1313 | "Invalid exchange", (* 52, EBADE *) |
| 1314 | "Invalid request descriptor", (* 53, EBADR *) |
| 1315 | "Exchange full", (* 54, EXFULL *) |
| 1316 | "No anode", (* 55, ENOANO *) |
| 1317 | "Invalid request code", (* 56, EBADRQC *) |
| 1318 | "Invalid slot", (* 57, EBADSLT *) |
| 1319 | "unknown error", (* 58 *) |
| 1320 | "Bad font file format", (* 59, EBFONT *) |
| 1321 | "Device not a stream", (* 60, ENOSTR *) |
| 1322 | "No data available", (* 61, ENODATA *) |
| 1323 | "Timer expired", (* 62, ETIME *) |
| 1324 | "Out of streams resources", (* 63, ENOSR *) |
| 1325 | "Machine is not on the network", (* 64, ENONET *) |
| 1326 | "Package not installed", (* 65, ENOPKG *) |
| 1327 | "Object is remote", (* 66, EREMOTE *) |
| 1328 | "Link has been severed", (* 67, ENOLINK *) |
| 1329 | "Advertise error", (* 68, EADV *) |
| 1330 | "Srmount error", (* 69, ESRMNT *) |
| 1331 | "Communication error on send", (* 70, ECOMM *) |
| 1332 | "Protocol error", (* 71, EPROTO *) |
| 1333 | "Multihop attempted", (* 72, EMULTIHOP *) |
| 1334 | "RFS specific error", (* 73, EDOTDOT *) |
| 1335 | "Bad message", (* 74, EBADMSG *) |
| 1336 | "Value too large for defined data type", (* 75, EOVERFLOW *) |
| 1337 | "Name not unique on network", (* 76, ENOTUNIQ *) |
| 1338 | "File descriptor in bad state", (* 77, EBADFD *) |
| 1339 | "Remote address changed", (* 78, EREMCHG *) |
| 1340 | "Can not access a needed shared library", (* 79, ELIBACC *) |
| 1341 | "Accessing a corrupted shared library", (* 80, ELIBBAD *) |
| 1342 | ".lib section in a.out corrupted", (* 81, ELIBSCN *) |
| 1343 | "Attempting to link in too many shared libraries", (* 82, ELIBMAX *) |
| 1344 | "Cannot exec a shared library directly", (* 83, ELIBEXEC *) |
| 1345 | "Invalid or incomplete multibyte or wide character", (* 84, EILSEQ *) |
| 1346 | "Interrupted system call should be restarted", (* 85, ERESTART *) |
| 1347 | "Streams pipe error", (* 86, ESTRPIPE *) |
| 1348 | "Too many users", (* 87, EUSERS *) |
| 1349 | "Socket operation on non-socket", (* 88, ENOTSOCK *) |
| 1350 | "Destination address required", (* 89, EDESTADDRREQ *) |
| 1351 | "Message too long", (* 90, EMSGSIZE *) |
| 1352 | "Protocol wrong type for socket", (* 91, EPROTOTYPE *) |
| 1353 | "Protocol not available", (* 92, ENOPROTOOPT *) |
| 1354 | "Protocol not supported", (* 93, EPROTONOSUPPORT *) |
| 1355 | "Socket type not supported", (* 94, ESOCKTNOSUPPORT *) |
| 1356 | "Operation not supported", (* 95, ENOTSUP *) |
| 1357 | "Protocol family not supported", (* 96, EPFNOSUPPORT *) |
| 1358 | "Address family not supported by protocol", (* 97, EAFNOSUPPORT *) |
| 1359 | "Address already in use", (* 98, EADDRINUSE *) |
| 1360 | "Cannot assign requested address", (* 99, EADDRNOTAVAIL *) |
| 1361 | "Network is down", (* 100, ENETDOWN *) |
| 1362 | "Network is unreachable", (* 101, ENETUNREACH *) |
| 1363 | "Network dropped connection on reset", (* 102, ENETRESET *) |
| 1364 | "Software caused connection abort", (* 103, ECONNABORTED *) |
| 1365 | "Connection reset by peer", (* 104, ECONNRESET *) |
| 1366 | "No buffer space available", (* 105, ENOBUFS *) |
| 1367 | "Transport endpoint is already connected", (* 106, EISCONN *) |
| 1368 | "Transport endpoint is not connected", (* 107, ENOTCONN *) |
| 1369 | "Cannot send after transport endpoint shutdown", (* 108, ESHUTDOWN *) |
| 1370 | "Too many references: cannot splice", (* 109, ETOOMANYREFS *) |
| 1371 | "Connection timed out", (* 110, ETIMEDOUT *) |
| 1372 | "Connection refused", (* 111, ECONNREFUSED *) |
| 1373 | "Host is down", (* 112, EHOSTDOWN *) |
| 1374 | "No route to host", (* 113, EHOSTUNREACH *) |
| 1375 | "Operation already in progress", (* 114, EALREADY *) |
| 1376 | "Operation now in progress", (* 115, EINPROGRESS *) |
| 1377 | "Stale file handle", (* 116, ESTALE *) |
| 1378 | "Structure needs cleaning", (* 117, EUCLEAN *) |
| 1379 | "Not a XENIX named type file", (* 118, ENOTNAM *) |
| 1380 | "No XENIX semaphores available", (* 119, ENAVAIL *) |
| 1381 | "Is a named type file", (* 120, EISNAM *) |
| 1382 | "Remote I/O error", (* 121, EREMOTEIO *) |
| 1383 | "Disk quota exceeded", (* 122, EDQUOT *) |
| 1384 | "No medium found", (* 123, ENOMEDIUM *) |
| 1385 | "Wrong medium type", (* 124, EMEDIUMTYPE *) |
| 1386 | "Operation canceled", (* 125, ECANCELED *) |
| 1387 | "Required key not available", (* 126, ENOKEY *) |
| 1388 | "Key has expired", (* 127, EKEYEXPIRED *) |
| 1389 | "Key has been revoked", (* 128, EKEYREVOKED *) |
| 1390 | "Key was rejected by service", (* 129, EKEYREJECTED *) |
| 1391 | "Owner died", (* 130, EOWNERDEAD *) |
| 1392 | "State not recoverable", (* 131, ENOTRECOVERABLE *) |
| 1393 | "Operation not possible due to RF-kill", (* 132, ERFKILL *) |
| 1394 | "Memory page has hardware error" (* 133, EHWPOISON *)]; |
| 1395 | |
| 1396 | (* What errno err, or minus it, means, as C's strerror says it; |
| 1397 | "unknown error" for a number that is no errno. *) |
| 1398 | proc errorText*(err: INT): []const U8; |
| 1399 | begin |
| 1400 | if (err < 0) and (err > -LEN(errorTexts)) then err := -err; end; |
| 1401 | if (err < 0) or (err >= LEN(errorTexts)) then return "unknown error"; end; |
| 1402 | return errorTexts[err]; |
| 1403 | end; |
| 1404 | |
| 1405 | (* The environment, its NAME=values as C strings and a nil: the one the |
| 1406 | program started with, after the arguments and their nil, until |
| 1407 | setEnv makes one of its own. *) |
| 1408 | proc environment: ^^U8; |
| 1409 | begin |
| 1410 | if envp # nil then return envp; end; |
| 1411 | return SYSTEM.ARGV() + (ARGC() + 1); |
| 1412 | end; |
| 1413 | |
| 1414 | (* Whether the NAME=value s, a C string, is name's. *) |
| 1415 | proc named(s: ^U8; name: []const U8): BOOL; |
| 1416 | var n: INT; |
| 1417 | begin |
| 1418 | n := 0; |
| 1419 | while (n < LEN(name)) and (s[n] = name[n]) do INC(n); end; |
| 1420 | return (n = LEN(name)) and (s[n] = '='); |
| 1421 | end; |
| 1422 | |
| 1423 | (* The value of the environment variable name and true, or "" and |
| 1424 | false if it isn't set: "" and true if it is set to nothing. Every |
| 1425 | program calls env, from Rt, so this reads the environment itself |
| 1426 | rather than through environment and named, which would add their |
| 1427 | code to every executable. *) |
| 1428 | proc findEnv*(name: []const U8): ([]const U8, BOOL); |
| 1429 | var e: ^^U8; s: ^U8; i, n, m: INT; |
| 1430 | begin |
| 1431 | e := envp; |
| 1432 | if e = nil then e := SYSTEM.ARGV() + (ARGC() + 1); end; |
| 1433 | i := 0; |
| 1434 | while e[i] # nil do |
| 1435 | s := e[i]; |
| 1436 | n := 0; |
| 1437 | while (n < LEN(name)) and (s[n] = name[n]) do INC(n); end; |
| 1438 | if (n = LEN(name)) and (s[n] = '=') then |
| 1439 | m := n + 1; |
| 1440 | while s[m] # 0 do INC(m); end; |
| 1441 | return s[n + 1..m], true; |
| 1442 | end; |
| 1443 | INC(i); |
| 1444 | end; |
| 1445 | return "", false; |
| 1446 | end; |
| 1447 | |
| 1448 | (* The value of the environment variable name; "" if it isn't set. *) |
| 1449 | proc env*(name: []const U8): []const U8; |
| 1450 | var v: []const U8; |
| 1451 | begin |
| 1452 | v, _ := findEnv(name); |
| 1453 | return v; |
| 1454 | end; |
| 1455 | |
| 1456 | (* The environment's ith NAME=value, from 0, and true; "" and false |
| 1457 | past the last. *) |
| 1458 | proc envAt*(i: INT): ([]const U8, BOOL); |
| 1459 | var e: ^^U8; s: ^U8; j, n: INT; |
| 1460 | begin |
| 1461 | if i < 0 then return "", false; end; |
| 1462 | e := environment(); |
| 1463 | for j := 0 to i do |
| 1464 | if e[j] = nil then return "", false; end; |
| 1465 | end; |
| 1466 | s := e[i]; |
| 1467 | n := 0; |
| 1468 | while s[n] # 0 do INC(n); end; |
| 1469 | return s[0..n], true; |
| 1470 | end; |
| 1471 | |
| 1472 | (* The working directory, good until the next call; and 0, or minus an |
| 1473 | errno. *) |
| 1474 | proc workDir*: ([]const U8, INT); |
| 1475 | var r: INT; |
| 1476 | begin |
| 1477 | r := SYSTEM.SYSCALL(79, SYSTEM.ADR(cwd), LEN(cwd)); (* getcwd: its length with its zero *) |
| 1478 | if r < 0 then return "", r; end; |
| 1479 | return cwd[0..r - 1], 0; |
| 1480 | end; |
| 1481 | |
| 1482 | (* Makes path the working directory. *) |
| 1483 | proc setWorkDir*(path: []const U8): INT; |
| 1484 | var a: [4096]U8; |
| 1485 | begin |
| 1486 | if not cstr(path, a) then return -36; end; |
| 1487 | return SYSTEM.SYSCALL(80, SYSTEM.ADR(a)); (* chdir *) |
| 1488 | end; |
| 1489 | |
| 1490 | (* A pseudo-terminal: the file descriptor of its controlling side, or |
| 1491 | minus an errno. Its other side, the terminal its programs run on, is |
| 1492 | unlocked, for spawn to open. *) |
| 1493 | proc openPty*: INT; |
| 1494 | var a: [4096]U8; fd, unlock: INT; |
| 1495 | begin |
| 1496 | _ := cstr("/dev/ptmx", a); |
| 1497 | fd := SYSTEM.SYSCALL(2, SYSTEM.ADR(a), 80102H); (* open: O_RDWR, O_NOCTTY, O_CLOEXEC *) |
| 1498 | if fd < 0 then return fd; end; |
| 1499 | unlock := 0; |
| 1500 | if SYSTEM.SYSCALL(16, fd, 40045431H, SYSTEM.ADR(unlock)) # 0 then (* TIOCSPTLCK *) |
| 1501 | close(fd); |
| 1502 | return -5; (* EIO *) |
| 1503 | end; |
| 1504 | return fd; |
| 1505 | end; |
| 1506 | |
| 1507 | (* Tells the terminal of the pseudo-terminal fd that it has cols |
| 1508 | columns and rows rows: 0, or minus an errno. *) |
| 1509 | proc setTermSize*(fd, cols, rows: INT): INT; |
| 1510 | var w: Winsize; |
| 1511 | begin |
| 1512 | w := {rows: U16(rows), cols: U16(cols)}; |
| 1513 | return SYSTEM.SYSCALL(16, fd, 5414H, SYSTEM.ADR(w)); (* TIOCSWINSZ *) |
| 1514 | end; |
| 1515 | |
| 1516 | (* Copies s and a zero to at: the byte after them. *) |
| 1517 | proc put(s: []const U8; at: ^U8): ^U8; |
| 1518 | begin |
| 1519 | COPY(at[0..LEN(s)], s); |
| 1520 | at[LEN(s)] := 0; |
| 1521 | return at + (LEN(s) + 1); |
| 1522 | end; |
| 1523 | |
| 1524 | (* Whether the NAME=value s, a C string, names a variable that one of |
| 1525 | env gives. *) |
| 1526 | proc replaced(s: ^U8; env: [][]const U8): BOOL; |
| 1527 | var i, n: INT; |
| 1528 | begin |
| 1529 | for i := 0 to LEN(env) - 1 do |
| 1530 | n := 0; |
| 1531 | while (n < LEN(env[i])) and (s[n] = env[i][n]) and (s[n] # '=') do INC(n); end; |
| 1532 | if (n < LEN(env[i])) and (env[i][n] = '=') and (s[n] = '=') then return true; end; |
| 1533 | end; |
| 1534 | return false; |
| 1535 | end; |
| 1536 | |
| 1537 | (* What execve takes, in fresh memory of the size returned: args as C |
| 1538 | strings and a nil, then the environment old with each NAME=value of |
| 1539 | env in place of its variable or added, and a nil; or nil. The |
| 1540 | environment follows the arguments. *) |
| 1541 | proc argsAndEnv(args, env: [][]const U8; old: ^^U8): (^^U8, INT); |
| 1542 | var p: ^^U8; s: ^U8; size, n, i, k: INT; |
| 1543 | begin |
| 1544 | n := 0; |
| 1545 | while old[n] # nil do INC(n); end; |
| 1546 | size := (LEN(args) + n + LEN(env) + 2) * SIZE(^U8); |
| 1547 | for i := 0 to LEN(args) - 1 do INC(size, LEN(args[i]) + 1); end; |
| 1548 | for i := 0 to LEN(env) - 1 do INC(size, LEN(env[i]) + 1); end; |
| 1549 | p := ^^U8(mmap(size)); |
| 1550 | if p = nil then return nil, 0; end; |
| 1551 | s := ^U8(p + (LEN(args) + n + LEN(env) + 2)); |
| 1552 | k := 0; |
| 1553 | for i := 0 to LEN(args) - 1 do p[k] := s; s := put(args[i], s); INC(k); end; |
| 1554 | p[k] := nil; |
| 1555 | INC(k); |
| 1556 | for i := 0 to n - 1 do |
| 1557 | if not replaced(old[i], env) then p[k] := old[i]; INC(k); end; |
| 1558 | end; |
| 1559 | for i := 0 to LEN(env) - 1 do p[k] := s; s := put(env[i], s); INC(k); end; |
| 1560 | p[k] := nil; |
| 1561 | return p, size; |
| 1562 | end; |
| 1563 | |
| 1564 | (* Sets the environment variable name to value, for env and for the |
| 1565 | programs this one starts; 0, or minus an errno: -22 if name is empty |
| 1566 | or holds an = or a 0, or value a 0, and -12 if there is no memory. |
| 1567 | As C's setenv does, it makes the environment the program's own the |
| 1568 | first time it adds a variable: an array of pointers that doubles |
| 1569 | when it is full, the old one given back. A variable set again keeps |
| 1570 | its place. A NAME=value is never given back, so that what env gave |
| 1571 | stays good for the rest of the run; they are taken in turn from |
| 1572 | blocks of 64 KB. It isn't safe while another thread reads the |
| 1573 | environment or starts a program, as C's setenv isn't. *) |
| 1574 | proc setEnv*(name, value: []const U8): INT; |
| 1575 | var e, p: ^^U8; s: ^U8; size, cap, n, i: INT; |
| 1576 | begin |
| 1577 | if LEN(name) = 0 then return -22; end; (* EINVAL *) |
| 1578 | for i := 0 to LEN(name) - 1 do |
| 1579 | if (name[i] = '=') or (name[i] = 0) then return -22; end; |
| 1580 | end; |
| 1581 | for i := 0 to LEN(value) - 1 do |
| 1582 | if value[i] = 0 then return -22; end; |
| 1583 | end; |
| 1584 | (* NAME=value, written where the next one goes, and taken only once |
| 1585 | the array has room for it *) |
| 1586 | size := LEN(name) + LEN(value) + 2; |
| 1587 | if size > envLeft then |
| 1588 | n := 10000H; |
| 1589 | if size > n then n := size; end; |
| 1590 | s := mmap(n); |
| 1591 | if s = nil then return -12; end; (* ENOMEM *) |
| 1592 | envText := s; |
| 1593 | envLeft := n; |
| 1594 | end; |
| 1595 | s := envText; |
| 1596 | COPY(s[0..LEN(name)], name); |
| 1597 | s[LEN(name)] := '='; |
| 1598 | COPY(s[LEN(name) + 1..size - 1], value); |
| 1599 | s[size - 1] := 0; |
| 1600 | e := environment(); |
| 1601 | if envp = nil then |
| 1602 | envLen := 0; |
| 1603 | while e[envLen] # nil do INC(envLen); end; |
| 1604 | end; |
| 1605 | i := 0; |
| 1606 | while (i < envLen) and not named(e[i], name) do INC(i); end; |
| 1607 | if i = envLen then |
| 1608 | (* a new variable: the array grows if it is the first, or full *) |
| 1609 | if (envp = nil) or (envLen + 1 = envCap) then |
| 1610 | cap := 2 * (envLen + 1); |
| 1611 | if cap < 32 then cap := 32; end; |
| 1612 | p := ^^U8(mmap(cap * SIZE(^U8))); |
| 1613 | if p = nil then return -12; end; |
| 1614 | if envLen > 0 then COPY(p[0..envLen], e[0..envLen]); end; |
| 1615 | if envp # nil then munmap(^U8(envp), envCap * SIZE(^U8)); end; |
| 1616 | envp := p; |
| 1617 | envCap := cap; |
| 1618 | e := p; |
| 1619 | end; |
| 1620 | INC(envLen); |
| 1621 | e[envLen] := nil; |
| 1622 | end; |
| 1623 | e[i] := s; |
| 1624 | envText := envText + size; |
| 1625 | envLeft := envLeft - size; |
| 1626 | return 0; |
| 1627 | end; |
| 1628 | |
| 1629 | (* A pipe: the fd to read and the fd to write, both closed in a program |
| 1630 | exec starts; or minus an errno and -1. *) |
| 1631 | proc pipe*: (INT, INT); |
| 1632 | var fds: [2]I32; r: INT; |
| 1633 | begin |
| 1634 | r := SYSTEM.SYSCALL(293, SYSTEM.ADR(fds), 80000H); (* pipe2, O_CLOEXEC *) |
| 1635 | if r < 0 then return r, -1; end; |
| 1636 | return INT(fds[0]), INT(fds[1]); |
| 1637 | end; |
| 1638 | |
| 1639 | (* This program's process id. *) |
| 1640 | proc pid*: INT; |
| 1641 | begin |
| 1642 | return SYSTEM.SYSCALL(39); (* getpid *) |
| 1643 | end; |
| 1644 | |
| 1645 | (* The user this program runs as: its real id. *) |
| 1646 | proc uid*: INT; |
| 1647 | begin |
| 1648 | return SYSTEM.SYSCALL(102); (* getuid *) |
| 1649 | end; |
| 1650 | |
| 1651 | (* The user whose rights this program has, its effective id: 0 is |
| 1652 | root. *) |
| 1653 | proc euid*: INT; |
| 1654 | begin |
| 1655 | return SYSTEM.SYSCALL(107); (* geteuid *) |
| 1656 | end; |
| 1657 | |
| 1658 | (* The name of this machine, good until the next call; and 0, or minus |
| 1659 | an errno. *) |
| 1660 | proc hostName*: ([]const U8, INT); |
| 1661 | var r, n: INT; |
| 1662 | begin |
| 1663 | r := SYSTEM.SYSCALL(63, SYSTEM.ADR(uts)); (* uname *) |
| 1664 | if r < 0 then return "", r; end; |
| 1665 | n := 0; |
| 1666 | while (n < 64) and (uts[65 + n] # 0) do INC(n); end; (* the nodename, at 65 *) |
| 1667 | return uts[65..65 + n], 0; |
| 1668 | end; |
| 1669 | |
| 1670 | (* The number of processors this program may run on, at least 1. *) |
| 1671 | proc processors*: INT; |
| 1672 | var mask: [1024]U8; n, i, b, count: INT; |
| 1673 | begin |
| 1674 | n := SYSTEM.SYSCALL(204, 0, LEN(mask), SYSTEM.ADR(mask)); (* sched_getaffinity: this thread, the bytes it filled in *) |
| 1675 | count := 0; |
| 1676 | for i := 0 to n - 1 do |
| 1677 | for b := 0 to 7 do |
| 1678 | if (INT(mask[i]) shr b) & 1 = 1 then INC(count); end; |
| 1679 | end; |
| 1680 | end; |
| 1681 | if count < 1 then return 1; end; (* n < 0: it failed *) |
| 1682 | return count; |
| 1683 | end; |
| 1684 | |
| 1685 | (* The time on the processor, in ns, that this program has used, or, |
| 1686 | if children, the programs it started and waited for: in the program |
| 1687 | itself, and in the system for it. *) |
| 1688 | proc cpuTime*(children: BOOL): (I64, I64); |
| 1689 | var ru: [18]I64; (* struct rusage: two struct timeval first *) |
| 1690 | begin |
| 1691 | if children then _ := SYSTEM.SYSCALL(98, -1, SYSTEM.ADR(ru)); (* getrusage: RUSAGE_CHILDREN *) |
| 1692 | else _ := SYSTEM.SYSCALL(98, 0, SYSTEM.ADR(ru)); (* RUSAGE_SELF *) |
| 1693 | end; |
| 1694 | return ru[0] * 1000000000 + ru[1] * 1000, ru[2] * 1000000000 + ru[3] * 1000; |
| 1695 | end; |
| 1696 | |
| 1697 | (* The limit which, one of the limit constants: the soft one, which |
| 1698 | this program may raise up to the hard one; -1 for none; and 0, or |
| 1699 | minus an errno. *) |
| 1700 | proc limit*(which: INT): (I64, I64, INT); |
| 1701 | var r: [2]I64; err: INT; |
| 1702 | begin |
| 1703 | err := SYSTEM.SYSCALL(97, which, SYSTEM.ADR(r)); (* getrlimit; RLIM_INFINITY is -1 *) |
| 1704 | return r[0], r[1], err; |
| 1705 | end; |
| 1706 | |
| 1707 | (* Sets the limit which to soft and hard, -1 for none: 0, or minus an |
| 1708 | errno. *) |
| 1709 | proc setLimit*(which: INT; soft, hard: I64): INT; |
| 1710 | var r: [2]I64; |
| 1711 | begin |
| 1712 | r := [soft, hard]; |
| 1713 | return SYSTEM.SYSCALL(160, which, SYSTEM.ADR(r)); (* setrlimit *) |
| 1714 | end; |
| 1715 | |
| 1716 | (* Moves the program pid, or this one if pid is 0, to the process group |
| 1717 | group, or to a new one it leads if group is 0. *) |
| 1718 | proc setGroup*(pid, group: INT): INT; |
| 1719 | begin |
| 1720 | return SYSTEM.SYSCALL(109, pid, group); (* setpgid *) |
| 1721 | end; |
| 1722 | |
| 1723 | (* The process group this program is in. *) |
| 1724 | proc group*: INT; |
| 1725 | begin |
| 1726 | return SYSTEM.SYSCALL(111); (* getpgrp *) |
| 1727 | end; |
| 1728 | |
| 1729 | (* Gives the keys of the terminal on fd, and its signals, to the process |
| 1730 | group group. *) |
| 1731 | proc foreground*(fd, group: INT): INT; |
| 1732 | var g: I32; |
| 1733 | begin |
| 1734 | g := I32(group); |
| 1735 | return SYSTEM.SYSCALL(16, fd, 5410H, SYSTEM.ADR(g)); (* TIOCSPGRP *) |
| 1736 | end; |
| 1737 | |
| 1738 | (* The process group the terminal on fd gives its keys to, or minus an |
| 1739 | errno. *) |
| 1740 | proc foregroundOf*(fd: INT): INT; |
| 1741 | var g: I32; r: INT; |
| 1742 | begin |
| 1743 | r := SYSTEM.SYSCALL(16, fd, 540FH, SYSTEM.ADR(g)); (* TIOCGPGRP *) |
| 1744 | if r < 0 then return r; end; |
| 1745 | return INT(g); |
| 1746 | end; |
| 1747 | |
| 1748 | (* Where spawn looks for a program named without a /, if PATH is unset |
| 1749 | or empty: glibc's execvp looks in these. *) |
| 1750 | const defaultPath = "/bin:/usr/bin"; |
| 1751 | |
| 1752 | (* The ith directory of search, a list of them with : between, as PATH |
| 1753 | is: dir, "." if it is empty, and where the next starts; -1 past the |
| 1754 | last. *) |
| 1755 | proc pathDir(search: []const U8; i: INT): ([]const U8, INT); |
| 1756 | var j: INT; |
| 1757 | begin |
| 1758 | if i > LEN(search) then return "", -1; end; |
| 1759 | j := i; |
| 1760 | while (j < LEN(search)) and (search[j] # ':') do INC(j); end; |
| 1761 | if j = i then return ".", j + 1; end; |
| 1762 | return search[i..j], j + 1; |
| 1763 | end; |
| 1764 | |
| 1765 | (* dir, a /, and name, a C string in buf; false if it doesn't fit. *) |
| 1766 | proc joinPath(dir, name: []const U8; buf: []U8): BOOL; |
| 1767 | var n: INT; |
| 1768 | begin |
| 1769 | n := LEN(dir) + 1 + LEN(name); |
| 1770 | if n >= LEN(buf) then return false; end; |
| 1771 | COPY(buf, dir); |
| 1772 | buf[LEN(dir)] := '/'; |
| 1773 | COPY(buf[LEN(dir) + 1..n], name); |
| 1774 | buf[n] := 0; |
| 1775 | return true; |
| 1776 | end; |
| 1777 | |
| 1778 | (* Whether name holds a /, and so is a path, not a name to look for. *) |
| 1779 | proc isPath(name: []const U8): BOOL; |
| 1780 | var i: INT; |
| 1781 | begin |
| 1782 | for i := 0 to LEN(name) - 1 do |
| 1783 | if name[i] = '/' then return true; end; |
| 1784 | end; |
| 1785 | return false; |
| 1786 | end; |
| 1787 | |
| 1788 | (* Runs the program at name, as execve does; or, for a name without a |
| 1789 | /, the first program of that name in the directories of search, as |
| 1790 | execvp does: each in turn, an empty one being the working directory. |
| 1791 | It returns only if none could be run. It runs in spawn's child, after |
| 1792 | the fork, so it takes no memory: each path is made in buf. *) |
| 1793 | proc run(name, search: []const U8; buf: []U8; argv, envp: ^^U8); |
| 1794 | var dir: []const U8; i: INT; |
| 1795 | begin |
| 1796 | if isPath(name) or (LEN(name) = 0) then |
| 1797 | if cstr(name, buf) then _ := SYSTEM.SYSCALL(59, SYSTEM.ADR(buf[0]), argv, envp); end; (* execve *) |
| 1798 | return; |
| 1799 | end; |
| 1800 | dir, i := pathDir(search, 0); |
| 1801 | while i >= 0 do |
| 1802 | if joinPath(dir, name, buf) then _ := SYSTEM.SYSCALL(59, SYSTEM.ADR(buf[0]), argv, envp); end; (* execve *) |
| 1803 | dir, i := pathDir(search, i); |
| 1804 | end; |
| 1805 | end; |
| 1806 | |
| 1807 | (* The program exec runs for name, as a C string in buf: name itself |
| 1808 | if it has a /, or else the first file of that name in the |
| 1809 | directories of search that is no directory and that this user may |
| 1810 | run. 0; or -2 (ENOENT) if there is none, or -13 (EACCES) if one is |
| 1811 | there and can't be run. *) |
| 1812 | proc find(name, search: []const U8; buf: []U8): INT; |
| 1813 | var dir: []const U8; i, err, r: INT; f: Info; |
| 1814 | begin |
| 1815 | if isPath(name) then |
| 1816 | if not cstr(name, buf) then return -36; end; |
| 1817 | return 0; |
| 1818 | end; |
| 1819 | r := -2; (* ENOENT *) |
| 1820 | dir, i := pathDir(search, 0); |
| 1821 | while (LEN(name) > 0) and (i >= 0) do |
| 1822 | if joinPath(dir, name, buf) then |
| 1823 | f, err := info(buf[0..LEN(dir) + 1 + LEN(name)]); |
| 1824 | if (err = 0) and not f.dir then |
| 1825 | if SYSTEM.SYSCALL(21, SYSTEM.ADR(buf[0]), 1) = 0 then return 0; end; (* access, X_OK *) |
| 1826 | r := -13; (* EACCES *) |
| 1827 | end; |
| 1828 | end; |
| 1829 | dir, i := pathDir(search, i); |
| 1830 | end; |
| 1831 | return r; |
| 1832 | end; |
| 1833 | |
| 1834 | (* The name of the terminal of the pseudo-terminal fd, as a C string |
| 1835 | in name: /dev/pts/ and the number TIOCGPTN gives. 0, or -25 |
| 1836 | (ENOTTY). *) |
| 1837 | proc ptyName(fd: INT; name: []U8): INT; |
| 1838 | var n, i, size: INT; |
| 1839 | begin |
| 1840 | n := 0; |
| 1841 | if SYSTEM.SYSCALL(16, fd, 80045430H, SYSTEM.ADR(n)) # 0 then return -25; end; (* TIOCGPTN; ENOTTY *) |
| 1842 | i := put("/dev/pts/", SYSTEM.ADR(name[0])) - SYSTEM.ADR(name[0]) - 1; (* at its zero *) |
| 1843 | size := n; |
| 1844 | repeat INC(i); size := size div 10; until size = 0; |
| 1845 | name[i] := 0; |
| 1846 | repeat DEC(i); name[i] := U8(48 + n mod 10); n := n div 10; until n = 0; |
| 1847 | return 0; |
| 1848 | end; |
| 1849 | |
| 1850 | (* Closes every fd from 3 on but those of keep, which stay open in the |
| 1851 | program exec starts. It runs in spawn's child, after the fork, so it |
| 1852 | takes no memory: it closes the runs between the fds kept, the lowest |
| 1853 | first, each with one close_range. *) |
| 1854 | proc closeAllBut(keep: []const INT); |
| 1855 | var lo, next, i: INT; |
| 1856 | begin |
| 1857 | lo := 3; |
| 1858 | repeat |
| 1859 | next := -1; |
| 1860 | for i := 0 to LEN(keep) - 1 do |
| 1861 | if (keep[i] >= lo) and ((next < 0) or (keep[i] < next)) then next := keep[i]; end; |
| 1862 | end; |
| 1863 | if next < 0 then |
| 1864 | _ := SYSTEM.SYSCALL(436, lo, 0FFFFFFFFH, 0); (* close_range: the rest *) |
| 1865 | else |
| 1866 | if next > lo then _ := SYSTEM.SYSCALL(436, lo, next - 1, 0); end; |
| 1867 | _ := SYSTEM.SYSCALL(72, next, 2, 0); (* fcntl: F_SETFD, kept on exec *) |
| 1868 | lo := next + 1; |
| 1869 | end; |
| 1870 | until next < 0; |
| 1871 | end; |
| 1872 | |
| 1873 | (* What p asks of the program before it runs, done to this one: a |
| 1874 | session on the terminal name, if p.tty, or else its group, its |
| 1875 | terminal's foreground; then its standard input, output and error, |
| 1876 | each p.fds names over the terminal's; then its working directory, |
| 1877 | the C string dir. 0, or minus an errno. *) |
| 1878 | proc takeOn(p: Program; name, dir: []const U8): INT; |
| 1879 | var fd, i, r: INT; |
| 1880 | begin |
| 1881 | if p.tty # 0 then |
| 1882 | _ := SYSTEM.SYSCALL(112); (* setsid *) |
| 1883 | fd := SYSTEM.SYSCALL(2, SYSTEM.PTR(name), 2); (* open, O_RDWR *) |
| 1884 | if fd < 0 then return fd; end; |
| 1885 | r := SYSTEM.SYSCALL(16, fd, 540EH, 0); (* TIOCSCTTY *) |
| 1886 | if r # 0 then return r; end; |
| 1887 | for i := 0 to 2 do _ := SYSTEM.SYSCALL(33, fd, i); end; (* dup2 *) |
| 1888 | else |
| 1889 | if p.group = -1 then _ := SYSTEM.SYSCALL(109, 0, 0); (* setpgid *) |
| 1890 | elsif p.group # 0 then _ := SYSTEM.SYSCALL(109, 0, p.group); |
| 1891 | end; |
| 1892 | if p.foreground then _ := foreground(0, SYSTEM.SYSCALL(111)); end; (* getpgrp *) |
| 1893 | end; |
| 1894 | for i := 0 to 2 do |
| 1895 | if p.fds[i] = i then |
| 1896 | _ := keepOnExec(i, true); (* dup2 of a file onto itself would leave it closed on exec *) |
| 1897 | elsif p.fds[i] # 0 then |
| 1898 | _ := SYSTEM.SYSCALL(33, p.fds[i], i); (* dup2 *) |
| 1899 | end; |
| 1900 | end; |
| 1901 | if LEN(p.dir) > 0 then return SYSTEM.SYSCALL(80, SYSTEM.PTR(dir)); end; (* chdir *) |
| 1902 | return 0; |
| 1903 | end; |
| 1904 | |
| 1905 | (* Whether the signal sig does nothing now, as ignore(sig, true) or the |
| 1906 | program that started this one left it: its handler is SIG_IGN, 1. *) |
| 1907 | proc ignored*(sig: INT): BOOL; |
| 1908 | var act: [4]U64; (* struct sigaction *) |
| 1909 | begin |
| 1910 | _ := SYSTEM.SYSCALL(13, sig, 0, SYSTEM.ADR(act), 8); (* rt_sigaction *) |
| 1911 | return act[0] = 1; |
| 1912 | end; |
| 1913 | |
| 1914 | (* Starts the program p describes, found as run says: its process id, |
| 1915 | or minus an errno: -2 (ENOENT) or -13 (EACCES) as exec says, before |
| 1916 | the fork, if there is no program to run. A program found that |
| 1917 | execve then refuses ends with status 127. Every signal is blocked |
| 1918 | from the fork until the program has its own: its group, its |
| 1919 | terminal's foreground, and signals as they are at first. *) |
| 1920 | proc spawn*(p: Program): INT; |
| 1921 | var |
| 1922 | a, dir: [4096]U8; name: [32]U8; dfl: [4]U64; (* struct sigaction: SIG_DFL *) |
| 1923 | argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, i, g, r: INT; search: []const U8; |
| 1924 | begin |
| 1925 | if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) |
| 1926 | if (p.tty # 0) and (ptyName(p.tty, name) # 0) then return -25; end; (* ENOTTY *) |
| 1927 | none := nil; |
| 1928 | if p.onlyEnv then |
| 1929 | argv, size := argsAndEnv(p.args, p.env, SYSTEM.ADR(none)); |
| 1930 | else |
| 1931 | argv, size := argsAndEnv(p.args, p.env, environment()); |
| 1932 | end; |
| 1933 | if argv = nil then return -12; end; (* ENOMEM *) |
| 1934 | search := env("PATH"); (* read here, before the fork *) |
| 1935 | if LEN(search) = 0 then search := defaultPath; end; |
| 1936 | r := find(p.path, search, a); |
| 1937 | if r < 0 then munmap(^U8(argv), size); return r; end; |
| 1938 | all := 0FFFFFFFFFFFFFFFFH; |
| 1939 | _ := SYSTEM.SYSCALL(14, 2, SYSTEM.ADR(all), SYSTEM.ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *) |
| 1940 | pid := SYSTEM.SYSCALL(57); (* fork *) |
| 1941 | if pid = 0 then |
| 1942 | if takeOn(p, name, dir) # 0 then _ := SYSTEM.SYSCALL(231, 127); end; (* exit *) |
| 1943 | closeAllBut(p.more); |
| 1944 | for i := 1 to 64 do |
| 1945 | if not (p.keepIgnored and ignored(i)) then _ := SYSTEM.SYSCALL(13, i, SYSTEM.ADR(dfl), 0, 8); end; (* rt_sigaction *) |
| 1946 | end; |
| 1947 | _ := SYSTEM.SYSCALL(14, 2, SYSTEM.ADR(empty), 0, 8); (* no signal blocked *) |
| 1948 | run(p.path, search, a, argv, argv + (LEN(p.args) + 1)); |
| 1949 | _ := SYSTEM.SYSCALL(231, 127); (* exit *) |
| 1950 | end; |
| 1951 | if (pid > 0) and (p.tty = 0) then |
| 1952 | (* as the program does, for whichever of the two runs first *) |
| 1953 | g := p.group; |
| 1954 | if g = -1 then g := pid; elsif g = 0 then g := SYSTEM.SYSCALL(111); end; |
| 1955 | if p.group # 0 then _ := SYSTEM.SYSCALL(109, pid, g); end; |
| 1956 | if p.foreground then _ := foreground(0, g); end; |
| 1957 | end; |
| 1958 | _ := SYSTEM.SYSCALL(14, 2, SYSTEM.ADR(old), 0, 8); |
| 1959 | munmap(^U8(argv), size); |
| 1960 | return pid; |
| 1961 | end; |
| 1962 | |
| 1963 | (* Runs the program p describes in this one's place, as spawn would |
| 1964 | start it, but keeping this program's other fds as they are, open |
| 1965 | unless closed on exec (as open and pipe give) and not kept by |
| 1966 | keepOnExec; the fds of p.more are kept. It returns |
| 1967 | only if it can't: -2 (ENOENT) or -13 (EACCES) if there is no |
| 1968 | program to run, before anything is done; or, once the program's |
| 1969 | terminal, group, fds and working directory are as p asks, which |
| 1970 | stay so, minus the errno that stopped it, with this program's |
| 1971 | signals given back as they were. *) |
| 1972 | proc exec*(p: Program): INT; |
| 1973 | var |
| 1974 | a, dir: [4096]U8; name: [32]U8; dfl: [4]U64; acts: [65][4]U64; (* struct sigaction *) |
| 1975 | argv: ^^U8; none: ^U8; all, old, empty: U64; size, r, i: INT; search: []const U8; |
| 1976 | begin |
| 1977 | if not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) |
| 1978 | if (p.tty # 0) and (ptyName(p.tty, name) # 0) then return -25; end; (* ENOTTY *) |
| 1979 | search := env("PATH"); |
| 1980 | if LEN(search) = 0 then search := defaultPath; end; |
| 1981 | r := find(p.path, search, a); |
| 1982 | if r < 0 then return r; end; |
| 1983 | none := nil; |
| 1984 | if p.onlyEnv then |
| 1985 | argv, size := argsAndEnv(p.args, p.env, SYSTEM.ADR(none)); |
| 1986 | else |
| 1987 | argv, size := argsAndEnv(p.args, p.env, environment()); |
| 1988 | end; |
| 1989 | if argv = nil then return -12; end; (* ENOMEM *) |
| 1990 | r := takeOn(p, name, dir); |
| 1991 | if r = 0 then |
| 1992 | for i := 0 to LEN(p.more) - 1 do |
| 1993 | if p.more[i] > 2 then _ := keepOnExec(p.more[i], true); end; |
| 1994 | end; |
| 1995 | (* every signal as it is at first and none blocked, as spawn |
| 1996 | gives them, each kept to give back if execve fails *) |
| 1997 | all := 0FFFFFFFFFFFFFFFFH; |
| 1998 | _ := SYSTEM.SYSCALL(14, 2, SYSTEM.ADR(all), SYSTEM.ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *) |
| 1999 | for i := 1 to 64 do |
| 2000 | _ := SYSTEM.SYSCALL(13, i, 0, SYSTEM.ADR(acts[i]), 8); (* rt_sigaction: as it is *) |
| 2001 | if not (p.keepIgnored and (acts[i][0] = 1)) then _ := SYSTEM.SYSCALL(13, i, SYSTEM.ADR(dfl), 0, 8); end; |
| 2002 | end; |
| 2003 | _ := SYSTEM.SYSCALL(14, 2, SYSTEM.ADR(empty), 0, 8); |
| 2004 | r := SYSTEM.SYSCALL(59, SYSTEM.ADR(a), argv, argv + (LEN(p.args) + 1)); (* execve *) |
| 2005 | _ := SYSTEM.SYSCALL(14, 2, SYSTEM.ADR(all), 0, 8); |
| 2006 | for i := 1 to 64 do _ := SYSTEM.SYSCALL(13, i, SYSTEM.ADR(acts[i]), 0, 8); end; |
| 2007 | _ := SYSTEM.SYSCALL(14, 2, SYSTEM.ADR(old), 0, 8); |
| 2008 | end; |
| 2009 | munmap(^U8(argv), size); |
| 2010 | return r; |
| 2011 | end; |
| 2012 | |
| 2013 | (* Runs the program at path in this one's place, with args, the first |
| 2014 | its name, and env, NAME=value, its whole environment, as the |
| 2015 | kernel's execve does and a shell wants: no search, and every fd and |
| 2016 | signal as it is, but that a handled signal does what it does at |
| 2017 | first. It returns only if it can't: minus an errno. *) |
| 2018 | proc execve*(path: []const U8; args, env: [][]const U8): INT; |
| 2019 | var a: [4096]U8; argv: ^^U8; none: ^U8; size, r: INT; |
| 2020 | begin |
| 2021 | if not cstr(path, a) then return -36; end; (* ENAMETOOLONG *) |
| 2022 | none := nil; |
| 2023 | argv, size := argsAndEnv(args, env, SYSTEM.ADR(none)); |
| 2024 | if argv = nil then return -12; end; (* ENOMEM *) |
| 2025 | r := SYSTEM.SYSCALL(59, SYSTEM.ADR(a), argv, argv + (LEN(args) + 1)); (* execve *) |
| 2026 | munmap(^U8(argv), size); |
| 2027 | return r; |
| 2028 | end; |
| 2029 | |
| 2030 | (* wait4 with options, its status read as C's macros read it. *) |
| 2031 | proc waitFor(pid, options: INT): (INT, Status, INT); |
| 2032 | var status: I32; r: INT; |
| 2033 | begin |
| 2034 | r := SYSTEM.SYSCALL(61, pid, SYSTEM.ADR(status), options, 0); (* wait4 *) |
| 2035 | if r <= 0 then return r, running, 0; end; |
| 2036 | if status & 7FH = 0 then return r, exited, INT(status) shr 8 & 0FFH; end; |
| 2037 | if status & 0FFH = 7FH then return r, stopped, INT(status) shr 8 & 0FFH; end; |
| 2038 | return r, killed, INT(status) & 7FH; |
| 2039 | end; |
| 2040 | |
| 2041 | (* Waits for the program pid, any of this program's if pid is -1, or |
| 2042 | any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with |
| 2043 | its exit status; killed or stopped, with the signal's number. Without |
| 2044 | block it doesn't wait: 0 and running if none has changed. Minus an |
| 2045 | errno, and running, if there is none to wait for. *) |
| 2046 | proc wait*(pid: INT; block: BOOL): (INT, Status, INT); |
| 2047 | var options: INT; |
| 2048 | begin |
| 2049 | options := 3; (* WNOHANG, WUNTRACED *) |
| 2050 | if block then options := 2; end; |
| 2051 | return waitFor(pid, options); |
| 2052 | end; |
| 2053 | |
| 2054 | (* Waits for the program pid as wait does, but only for one to end: |
| 2055 | a stopped program is not waited for, so the status is exited or |
| 2056 | killed, or running if there is none to wait for. *) |
| 2057 | proc waitExit*(pid: INT): (INT, Status, INT); |
| 2058 | begin |
| 2059 | return waitFor(pid, 0); |
| 2060 | end; |
| 2061 | |
| 2062 | (* Sends the program pid, or the group -pid, the signal: 0, or minus an |
| 2063 | errno. *) |
| 2064 | proc kill*(pid, signal: INT): INT; |
| 2065 | begin |
| 2066 | return SYSTEM.SYSCALL(62, pid, signal); |
| 2067 | end; |
| 2068 | |
| 2069 | (* Catches the signals sigs: blocks them, so that they don't do what |
| 2070 | they would, and gives them on an fd, which a later catch adds to and |
| 2071 | gives again; or minus an errno. Only the thread that catches them |
| 2072 | blocks them, and threads it starts later. ignore gives a signal up. *) |
| 2073 | proc catch*(sigs: []const INT): INT; |
| 2074 | var m: U64; i, fd: INT; |
| 2075 | begin |
| 2076 | m := caughtSigs; |
| 2077 | for i := 0 to LEN(sigs) - 1 do |
| 2078 | if (sigs[i] < 1) or (sigs[i] > 64) or (sigs[i] = sigKill) or (sigs[i] = sigStop) |
| 2079 | or (sigs[i] = 8) or (sigs[i] = 11) then (* SIGFPE and SIGSEGV, Rt's *) |
| 2080 | return -22; (* EINVAL *) |
| 2081 | end; |
| 2082 | m := m | U64(1) shl (sigs[i] - 1); |
| 2083 | end; |
| 2084 | fd := -1; |
| 2085 | if caughtSigs # 0 then fd := sigFd; end; |
| 2086 | _ := SYSTEM.SYSCALL(14, 0, SYSTEM.ADR(m), 0, 8); (* rt_sigprocmask: SIG_BLOCK *) |
| 2087 | fd := SYSTEM.SYSCALL(289, fd, SYSTEM.ADR(m), 8, 80800H); (* signalfd4: SFD_NONBLOCK, SFD_CLOEXEC *) |
| 2088 | if fd < 0 then return fd; end; |
| 2089 | caughtSigs := m; |
| 2090 | sigFd := fd; |
| 2091 | return fd; |
| 2092 | end; |
| 2093 | |
| 2094 | (* The next signal catch's fd has, or 0 if none is waiting. *) |
| 2095 | proc caught*(fd: INT): INT; |
| 2096 | var info: [128]U8; (* struct signalfd_siginfo: the number first, little-endian, below 65 *) |
| 2097 | begin |
| 2098 | if read(fd, info) # 128 then return 0; end; |
| 2099 | return INT(info[0]); |
| 2100 | end; |
| 2101 | |
| 2102 | (* Makes the signal sig do nothing, or, if not yes, what it does at |
| 2103 | first: 0, or minus an errno. A signal catch took is given up: it is |
| 2104 | unblocked in this thread, and catch's fd no longer gives it. *) |
| 2105 | proc ignore*(sig: INT; yes: BOOL): INT; |
| 2106 | var act: [4]U64; bit, m: U64; (* struct sigaction: its handler, SIG_IGN or SIG_DFL, first *) |
| 2107 | begin |
| 2108 | if yes then act[0] := 1; end; |
| 2109 | if (sig >= 1) and (sig <= 64) then |
| 2110 | bit := U64(1) shl (sig - 1); |
| 2111 | if caughtSigs & bit # 0 then |
| 2112 | caughtSigs := caughtSigs & ~bit; |
| 2113 | m := caughtSigs; |
| 2114 | _ := SYSTEM.SYSCALL(289, sigFd, SYSTEM.ADR(m), 8, 80800H); (* signalfd4: the signals still caught *) |
| 2115 | _ := SYSTEM.SYSCALL(14, 1, SYSTEM.ADR(bit), 0, 8); (* rt_sigprocmask: SIG_UNBLOCK *) |
| 2116 | end; |
| 2117 | end; |
| 2118 | return SYSTEM.SYSCALL(13, sig, SYSTEM.ADR(act), 0, 8); (* rt_sigaction *) |
| 2119 | end; |
| 2120 | |
| 2121 | (* Returns from a signal handler, as the kernel asks: rt_sigreturn, |
| 2122 | which sa_restorer names. *) |
| 2123 | proc sigReturn; |
| 2124 | code |
| 2125 | mov eax, 15; |
| 2126 | syscall; |
| 2127 | end sigReturn; |
| 2128 | |
| 2129 | (* The handler of the signals handle takes: it notes that sig came, and |
| 2130 | does nothing else, since it runs between any two instructions of the |
| 2131 | program, perhaps while Out or Heap holds its lock. *) |
| 2132 | proc onNoted(sig: INT); |
| 2133 | begin |
| 2134 | _ := XCHG(noted[sig], 1); |
| 2135 | end; |
| 2136 | |
| 2137 | (* Takes the signal sig away from what it would do: it is noted, for |
| 2138 | came to see, and a system call it comes during fails with -4 (EINTR) |
| 2139 | rather than going on, so that a program waiting for another, or for |
| 2140 | a key, sees Ctrl-C at once. 0, or minus an errno. ignore(sig, false) |
| 2141 | gives it back what it does at first. *) |
| 2142 | proc handle*(sig: INT): INT; |
| 2143 | var act: Action; |
| 2144 | begin |
| 2145 | if (sig < 1) or (sig > 64) or (sig = sigKill) or (sig = sigStop) |
| 2146 | or (sig = 8) or (sig = 11) then (* SIGFPE and SIGSEGV, Rt's *) |
| 2147 | return -22; (* EINVAL *) |
| 2148 | end; |
| 2149 | act := {handler: onNoted, flags: 4000000H, restorer: sigReturn}; (* SA_RESTORER, and no SA_RESTART *) |
| 2150 | return SYSTEM.SYSCALL(13, sig, SYSTEM.ADR(act), 0, 8); (* rt_sigaction *) |
| 2151 | end; |
| 2152 | |
| 2153 | (* Whether the signal sig, which handle took, came since the last time |
| 2154 | it was asked. *) |
| 2155 | proc came*(sig: INT): BOOL; |
| 2156 | begin |
| 2157 | if (sig < 1) or (sig > 64) then return false; end; |
| 2158 | return XCHG(noted[sig], 0) = 1; |
| 2159 | end; |
| 2160 | |
| 2161 | (* A copy of this program, which goes on from here: 0 in the copy, its |
| 2162 | process id in this one, or minus an errno. Only the thread that |
| 2163 | calls it is copied, so a program with threads mustn't; and what Out |
| 2164 | keeps is copied too, to be written twice, so the program flushes it |
| 2165 | first. *) |
| 2166 | proc fork*: INT; |
| 2167 | begin |
| 2168 | return SYSTEM.SYSCALL(57); (* fork *) |
| 2169 | end; |
| 2170 | |
| 2171 | (* A new session and process group, led by this program, with no |
| 2172 | controlling terminal: its process id, or minus an errno (-1, EPERM, |
| 2173 | if it leads a process group already, as a program just forked |
| 2174 | doesn't). *) |
| 2175 | proc setsid*: INT; |
| 2176 | begin |
| 2177 | return SYSTEM.SYSCALL(112); (* setsid *) |
| 2178 | end; |
| 2179 | |
| 2180 | (* Gives the processor to another thread. *) |
| 2181 | proc yield*; |
| 2182 | begin |
| 2183 | _ := SYSTEM.SYSCALL(24); |
| 2184 | end; |
| 2185 | |
| 2186 | (* Ends the program, at once, with status. *) |
| 2187 | proc exit*(status: INT); |
| 2188 | begin |
| 2189 | _ := SYSTEM.SYSCALL(231, status); |
| 2190 | end; |
| 2191 | |
| 2192 | (* The test of programs: one on a pseudo-terminal of 100 by 30 prints |
| 2193 | its arguments, a variable put in place and its size, and leads the |
| 2194 | terminal's foreground until it ends with status 3; one in a |
| 2195 | directory and an environment of its own writes to a pipe; one in a |
| 2196 | group of its own is stopped and killed through it; one that isn't |
| 2197 | there is no child, and one found that execve refuses ends with 127; |
| 2198 | ignored signals stay so only with keepIgnored; fds replace a |
| 2199 | terminal's files. Then signals: caught as a program ends and as |
| 2200 | one is sent; and a program starts with them neither caught nor |
| 2201 | ignored. *) |
| 2202 | proc testPrograms; |
| 2203 | var pty, child, n, m, r, w, sigs, c: INT; st: Status; buf: [64]U8; |
| 2204 | begin |
| 2205 | pty := openPty(); |
| 2206 | if (pty < 0) or (setTermSize(pty, 100, 30) # 0) then exit(2); end; |
| 2207 | child := spawn({path: "/bin/sh", args: ["sh", "-c", "echo $0 $1 $HOME; stty size; read x; exit 3", "a", "b"], |
| 2208 | env: ["HOME=/h"], tty: pty}); |
| 2209 | if child <= 0 then exit(2); end; |
| 2210 | n := 0; |
| 2211 | while n < 16 do |
| 2212 | if not ready(pty, 5000) then exit(2); end; |
| 2213 | m := read(pty, buf[n..LEN(buf)]); |
| 2214 | if m <= 0 then exit(2); end; |
| 2215 | INC(n, m); |
| 2216 | end; |
| 2217 | if (buf[0..n] # "a b /h\r\n30 100\r\n") or (foregroundOf(pty) # child) then exit(2); end; |
| 2218 | if write(pty, "\n") # 1 then exit(2); end; |
| 2219 | repeat |
| 2220 | if not ready(pty, 5000) then exit(2); end; |
| 2221 | until read(pty, buf) <= 0; (* -5, EIO, once the terminal has no program *) |
| 2222 | close(pty); |
| 2223 | r, st, c := wait(child, true); |
| 2224 | if (r # child) or (st # exited) or (c # 3) then exit(2); end; |
| 2225 | |
| 2226 | r, w := pipe(); |
| 2227 | if w < 0 then exit(2); end; |
| 2228 | child := spawn({path: "/bin/sh", args: ["sh", "-c", "echo $X $HOME; pwd"], env: ["X=x"], onlyEnv: true, |
| 2229 | dir: "/tmp", fds: [0, w, 0]}); |
| 2230 | close(w); |
| 2231 | n := 0; |
| 2232 | repeat |
| 2233 | m := read(r, buf[n..LEN(buf)]); |
| 2234 | if m > 0 then INC(n, m); end; |
| 2235 | until m <= 0; |
| 2236 | close(r); |
| 2237 | if buf[0..n] # "x\n/tmp\n" then exit(2); end; |
| 2238 | r, st, c := wait(child, true); |
| 2239 | if (r # child) or (st # exited) or (c # 0) then exit(2); end; |
| 2240 | |
| 2241 | (* a pipe filled without waiting, then emptied: room to write again *) |
| 2242 | r, w := pipe(); |
| 2243 | if (w < 0) or (setBlocking(w, false) # 0) or (setBlocking(r, false) # 0) then exit(2); end; |
| 2244 | repeat n := write(w, buf); until n < 0; |
| 2245 | if (n # -11) or (readyFor([], [w], 0) # -1) or (readyFor([r], [w], 0) # r) then exit(2); end; |
| 2246 | repeat n := read(r, buf); until n < 0; |
| 2247 | if (n # -11) or (readyFor([r], [w], 0) # w) or (setBlocking(w, true) # 0) then exit(2); end; |
| 2248 | close(r); |
| 2249 | close(w); |
| 2250 | |
| 2251 | (* blocking: a fresh pipe waits, a non-blocking one doesn't, a closed fd is -9 *) |
| 2252 | r, w := pipe(); |
| 2253 | if (blocking(r) # 1) or (setBlocking(r, false) # 0) or (blocking(r) # 0) then exit(2); end; |
| 2254 | if (setBlocking(r, true) # 0) or (blocking(r) # 1) then exit(2); end; |
| 2255 | close(r); |
| 2256 | close(w); |
| 2257 | if blocking(r) # -9 then exit(2); end; |
| 2258 | |
| 2259 | (* setsid: a forked child leads a session of its own, once; a group leader can't *) |
| 2260 | child := fork(); |
| 2261 | if child = 0 then |
| 2262 | if (setsid() # pid()) or (setsid() # -1) then exit(2); end; |
| 2263 | exit(0); |
| 2264 | end; |
| 2265 | r, st, c := wait(child, true); |
| 2266 | if (r # child) or (st # exited) or (c # 0) then exit(2); end; |
| 2267 | child := fork(); |
| 2268 | if child = 0 then |
| 2269 | if (setGroup(0, 0) # 0) or (setsid() # -1) then exit(2); end; |
| 2270 | exit(0); |
| 2271 | end; |
| 2272 | r, st, c := wait(child, true); |
| 2273 | if (r # child) or (st # exited) or (c # 0) then exit(2); end; |
| 2274 | |
| 2275 | child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1}); |
| 2276 | r, st, c := wait(child, false); |
| 2277 | if (r # 0) or (st # running) or (kill(-child, sigStop) # 0) then exit(2); end; |
| 2278 | r, st, c := wait(child, true); |
| 2279 | if (r # child) or (st # stopped) or (c # sigStop) or (kill(-child, sigKill) # 0) then exit(2); end; |
| 2280 | r, st, c := wait(-1, true); |
| 2281 | if (r # child) or (st # killed) or (c # sigKill) then exit(2); end; |
| 2282 | r, st, c := wait(-1, true); |
| 2283 | if r # -10 then exit(2); end; (* ECHILD: none left *) |
| 2284 | child := spawn({path: "/no/such/program", args: ["x"]}); |
| 2285 | r, st, c := wait(child, true); |
| 2286 | if (r # child) or (st # exited) or (c # 127) then exit(2); end; |
| 2287 | (* a name without a / is looked for along PATH *) |
| 2288 | child := spawn({path: "sh", args: ["sh", "-c", "exit 4"]}); |
| 2289 | r, st, c := wait(child, true); |
| 2290 | if (r # child) or (st # exited) or (c # 4) then exit(2); end; |
| 2291 | (* one not on PATH is no child at all *) |
| 2292 | if spawn({path: "no-such-program-on-path", args: ["x"]}) # -2 then exit(2); end; |
| 2293 | r, st, c := wait(-1, false); |
| 2294 | if r # -10 then exit(2); end; (* ECHILD: none was made *) |
| 2295 | (* an ignored signal stays so only with keepIgnored *) |
| 2296 | if ignore(sigInt, true) # 0 then exit(2); end; |
| 2297 | child := spawn({path: "sh", args: ["sh", "-c", "kill -INT $$; exit 6"], keepIgnored: true}); |
| 2298 | r, st, c := waitExit(child); |
| 2299 | if (r # child) or (st # exited) or (c # 6) then exit(2); end; |
| 2300 | child := spawn({path: "sh", args: ["sh", "-c", "kill -INT $$; exit 6"]}); |
| 2301 | r, st, c := waitExit(child); |
| 2302 | if (r # child) or (st # killed) or (c # sigInt) then exit(2); end; |
| 2303 | if ignore(sigInt, false) # 0 then exit(2); end; |
| 2304 | (* fds replace the terminal's files, with tty *) |
| 2305 | pty := openPty(); |
| 2306 | r, w := pipe(); |
| 2307 | child := spawn({path: "sh", args: ["sh", "-c", "echo out; echo err >&2"], tty: pty, fds: [0, w, 0]}); |
| 2308 | close(w); |
| 2309 | n := 0; |
| 2310 | repeat |
| 2311 | m := read(r, buf[n..LEN(buf)]); |
| 2312 | if m > 0 then INC(n, m); end; |
| 2313 | until m <= 0; |
| 2314 | close(r); |
| 2315 | r, st, c := waitExit(child); |
| 2316 | close(pty); |
| 2317 | if (buf[0..n] # "out\n") or (r # child) or (st # exited) or (c # 0) then exit(2); end; |
| 2318 | |
| 2319 | sigs := catch([sigChld, sigHup]); |
| 2320 | if (sigs < 0) or (catch([sigWinch]) # sigs) or (catch([11]) # -22) or (catch([sigKill]) # -22) then exit(2); end; |
| 2321 | if (caught(sigs) # 0) or (ignore(sigInt, true) # 0) then exit(2); end; |
| 2322 | child := spawn({path: "/bin/sh", args: ["sh", "-c", "kill -INT $$; exit 0"]}); |
| 2323 | if not ready(sigs, 5000) or (caught(sigs) # sigChld) then exit(2); end; |
| 2324 | r, st, c := wait(child, true); |
| 2325 | if (st # killed) or (c # sigInt) then exit(2); end; |
| 2326 | child := spawn({path: "/bin/sh", args: ["sh", "-c", "kill -HUP $$; exit 0"]}); |
| 2327 | r, st, c := wait(child, true); |
| 2328 | if (st # killed) or (c # sigHup) then exit(2); end; |
| 2329 | while caught(sigs) # 0 do end; |
| 2330 | if (kill(pid(), sigHup) # 0) or not ready(sigs, 1000) or (caught(sigs) # sigHup) then exit(2); end; |
| 2331 | if ignore(sigInt, false) # 0 then exit(2); end; |
| 2332 | end; |
| 2333 | |
| 2334 | (* The test of what a shell needs: fds copied, moved and kept on |
| 2335 | exec; a program given an fd at its own number, and one not; access, |
| 2336 | the umask and the environment by number; a terminal's modes; time |
| 2337 | used; limits; who and where; and exec, with nothing to run, and |
| 2338 | with a program that can't be run, after which the signals caught |
| 2339 | are caught still. *) |
| 2340 | proc testShell; |
| 2341 | var |
| 2342 | r, w, fd, n, k, child, c, err, pty, sigs, mask, parent: INT; t: I64; st: Status; buf: [64]U8; |
| 2343 | m, raw: Mode; soft, hard, s2, h2, user, system: I64; e: []const U8; ok, path: BOOL; |
| 2344 | begin |
| 2345 | r, w := pipe(); |
| 2346 | if w < 0 then exit(2); end; |
| 2347 | fd := dup(w); |
| 2348 | if (fd < 0) or (write(fd, "ab") # 2) or (read(r, buf) # 2) then exit(2); end; |
| 2349 | close(fd); |
| 2350 | if (dupTo(w, 9) # 9) or (write(9, "c") # 1) or (read(r, buf) # 1) or (buf[0] # 'c') then exit(2); end; |
| 2351 | if (keepOnExec(9, false) # 0) or (keepOnExec(9, true) # 0) or (keepOnExec(-1, true) # -9) then exit(2); end; |
| 2352 | child := spawn({path: "/bin/sh", args: ["sh", "-c", "echo more >&9"], more: [9]}); |
| 2353 | close(9); |
| 2354 | close(w); |
| 2355 | n := 0; |
| 2356 | repeat |
| 2357 | k := read(r, buf[n..LEN(buf)]); |
| 2358 | if k > 0 then INC(n, k); end; |
| 2359 | until k <= 0; |
| 2360 | close(r); |
| 2361 | if buf[0..n] # "more\n" then exit(2); end; |
| 2362 | k, st, c := wait(child, true); |
| 2363 | if (st # exited) or (c # 0) then exit(2); end; |
| 2364 | r, w := pipe(); |
| 2365 | if (w < 0) or (dupTo(w, 9) # 9) then exit(2); end; |
| 2366 | child := spawn({path: "/bin/sh", args: ["sh", "-c", "{ echo none >&9; } 2>/dev/null"]}); (* 9 is closed in it *) |
| 2367 | close(9); |
| 2368 | close(w); |
| 2369 | if read(r, buf) # 0 then exit(2); end; |
| 2370 | close(r); |
| 2371 | k, st, c := wait(child, true); |
| 2372 | if (st # exited) or (c = 0) then exit(2); end; |
| 2373 | |
| 2374 | if (access("/bin/sh", {executing}) # 0) or (access("/", {reading}) # 0) |
| 2375 | or (access("/no/such/file", {}) # -2) or (open("/bin/sh", {executing}) # -22) then exit(2); end; |
| 2376 | mask := umask(3FH); (* 077 *) |
| 2377 | if umask(mask) # 3FH then exit(2); end; |
| 2378 | path := false; |
| 2379 | n := 0; |
| 2380 | repeat |
| 2381 | e, ok := envAt(n); |
| 2382 | if ok and (LEN(e) > 5) and (e[0..5] = "PATH=") then path := true; end; |
| 2383 | INC(n); |
| 2384 | until not ok; |
| 2385 | e, ok := envAt(-1); |
| 2386 | if not path or ok or (LEN(e) # 0) then exit(2); end; |
| 2387 | |
| 2388 | (* a terminal's modes, kept and given back: a new one has lines *) |
| 2389 | pty := openPty(); |
| 2390 | m, err := getMode(pty); |
| 2391 | if (err # 0) or (m.t.lflag & 2 = 0) or not rawMode(pty) then exit(2); end; (* ICANON *) |
| 2392 | raw, err := getMode(pty); |
| 2393 | if (err # 0) or (raw.t.lflag & 2 # 0) or (setMode(pty, m) # 0) then exit(2); end; |
| 2394 | raw, err := getMode(pty); |
| 2395 | if (err # 0) or (raw.t.lflag & 2 = 0) then exit(2); end; |
| 2396 | m, err := getMode(-1); |
| 2397 | if err # -9 then exit(2); end; (* EBADF *) |
| 2398 | r, w := pipe(); |
| 2399 | m, err := getMode(r); |
| 2400 | if err # -25 then exit(2); end; (* ENOTTY *) |
| 2401 | close(r); |
| 2402 | close(w); |
| 2403 | close(pty); |
| 2404 | |
| 2405 | user, system := cpuTime(false); |
| 2406 | if (user < 0) or (system < 0) or (user + system = 0) then exit(2); end; |
| 2407 | user, system := cpuTime(true); |
| 2408 | if (user < 0) or (system < 0) then exit(2); end; |
| 2409 | soft, hard, err := limit(limitFiles); |
| 2410 | if (err # 0) or (soft < 16) or (setLimit(limitFiles, soft - 1, hard) # 0) then exit(2); end; |
| 2411 | s2, h2, err := limit(limitFiles); |
| 2412 | if (err # 0) or (s2 # soft - 1) or (h2 # hard) or (setLimit(limitFiles, soft, hard) # 0) then exit(2); end; |
| 2413 | soft, hard, err := limit(10000); |
| 2414 | if err # -22 then exit(2); end; (* EINVAL *) |
| 2415 | e, err := hostName(); |
| 2416 | if (err # 0) or (LEN(e) = 0) or (uid() < 0) or (euid() < 0) then exit(2); end; |
| 2417 | |
| 2418 | (* a copy of an fd out of the way; this program's group; a copy of |
| 2419 | this program, waited for by the group; a signal noted, and a wait |
| 2420 | it breaks into *) |
| 2421 | r, w := pipe(); |
| 2422 | fd := dupAbove(w, 10); |
| 2423 | if (fd < 10) or (write(fd, "d") # 1) or (read(r, buf) # 1) or (dupAbove(-1, 10) # -9) then exit(2); end; |
| 2424 | close(fd); |
| 2425 | if group() <= 0 then exit(2); end; |
| 2426 | child := fork(); |
| 2427 | if child = 0 then exit(7); end; |
| 2428 | k, st, c := wait(-group(), true); |
| 2429 | if (k # child) or (st # exited) or (c # 7) then exit(2); end; |
| 2430 | if (handle(sigUsr2) # 0) or came(sigUsr2) or (handle(sigKill) # -22) then exit(2); end; |
| 2431 | if (kill(pid(), sigUsr2) # 0) or not came(sigUsr2) or came(sigUsr2) then exit(2); end; |
| 2432 | parent := pid(); |
| 2433 | child := fork(); |
| 2434 | if child = 0 then sleep(100); _ := kill(parent, sigUsr2); exit(0); end; |
| 2435 | t := clock(); |
| 2436 | if ready(r, 5000) or not came(sigUsr2) or (clock() - t > 4000000000) then exit(2); end; |
| 2437 | k, st, c := wait(child, true); |
| 2438 | if (k # child) or (ignore(sigUsr2, false) # 0) then exit(2); end; |
| 2439 | close(r); |
| 2440 | close(w); |
| 2441 | |
| 2442 | sigs := catch([sigUsr1]); |
| 2443 | while caught(sigs) # 0 do end; (* the sigChld of the programs above *) |
| 2444 | if (sigs < 0) or (exec({path: "no-such-program-on-path", args: ["x"]}) # -2) then exit(2); end; |
| 2445 | if exec({path: "/etc/passwd", args: ["x"]}) # -13 then exit(2); end; (* not executable *) |
| 2446 | if (kill(pid(), sigUsr1) # 0) or not ready(sigs, 1000) or (caught(sigs) # sigUsr1) then exit(2); end; |
| 2447 | end; |
| 2448 | |
| 2449 | (* The test of time, random bytes and the network: a sleep the clock |
| 2450 | sees; addresses read and refused; a TCP connection on the loopback; |
| 2451 | a datagram to a port where no one listens, refused; servers on |
| 2452 | ports the system chooses, of streams and of datagrams. *) |
| 2453 | proc testNet; |
| 2454 | var t: I64; l, c, a, u, port, n: INT; x, y: [16]U8; s, s6: Sockaddr; peer: Peer; |
| 2455 | begin |
| 2456 | t := clock(); |
| 2457 | sleep(20); |
| 2458 | t := clock() - t; |
| 2459 | if (t < 20000000) or (t > 2000000000) or (now() < 1700000000 * 1000000000) then exit(2); end; |
| 2460 | random(x); |
| 2461 | random(y); |
| 2462 | if x[0..16] = y[0..16] then exit(2); end; |
| 2463 | if (inetAddr("10.0.0.255", 80, SYSTEM.ADR(s)) # 16) or (s.port[0] # 0) or (s.port[1] # 80) |
| 2464 | or (s.data[0] # 10) or (s.data[2] # 0) or (s.data[3] # 255) then |
| 2465 | exit(2); |
| 2466 | end; |
| 2467 | if (inetAddr("1:aBcd::ff", 258, SYSTEM.ADR(s6)) # 28) or (s6.port[0] # 1) or (s6.port[1] # 2) |
| 2468 | or (s6.data[0] # 0) or (s6.data[5] # 1) or (s6.data[6] # 0ABH) or (s6.data[7] # 0CDH) |
| 2469 | or (s6.data[18] # 0) or (s6.data[19] # 0FFH) then |
| 2470 | exit(2); |
| 2471 | end; |
| 2472 | if (inetAddr("::", 0, SYSTEM.ADR(s6)) # 28) or (inetAddr("1:2:3:4:5:6:7:8", 0, SYSTEM.ADR(s6)) # 28) then exit(2); end; |
| 2473 | if (connectTcp("1.2.3", 80) # -22) or (connectTcp("1.2.3.256", 80) # -22) or (connectTcp("1::2::3", 80) # -22) |
| 2474 | or (connectTcp("1:2:3:4:5:6:7:8:9", 80) # -22) or (connectTcp("1:", 80) # -22) |
| 2475 | or (connectTcp("12345::", 80) # -22) or (connectTcp("::1", 65536) # -22) then |
| 2476 | exit(2); |
| 2477 | end; |
| 2478 | l := listenTcp("127.0.0.1", 47391); |
| 2479 | c := connectTcp("127.0.0.1", 47391); |
| 2480 | if (l < 0) or (c < 0) then exit(2); end; |
| 2481 | a := accept(l); |
| 2482 | if (a < 0) or (send(c, "hi") # 2) or (read(a, x) # 2) or (x[0..2] # "hi") then exit(2); end; |
| 2483 | close(a); |
| 2484 | close(c); |
| 2485 | close(l); |
| 2486 | u := connectUdp("127.0.0.1", 47391); |
| 2487 | if (u < 0) or (send(u, "x") # 1) or not ready(u, 1000) or (read(u, x) # -111) then exit(2); end; (* ECONNREFUSED *) |
| 2488 | close(u); |
| 2489 | (* a server on a port the system chooses, and a connection to it |
| 2490 | within a time; then none, where no one listens *) |
| 2491 | l := listenTcp("127.0.0.1", 0); |
| 2492 | port := localPort(l); |
| 2493 | if (l < 0) or (port <= 0) then exit(2); end; |
| 2494 | c := connectTcpWithin("127.0.0.1", port, 1000); |
| 2495 | a := accept(l); |
| 2496 | if (c < 0) or (a < 0) or (send(c, "hi") # 2) or (read(a, x) # 2) then exit(2); end; |
| 2497 | close(a); |
| 2498 | close(c); |
| 2499 | close(l); |
| 2500 | if connectTcpWithin("127.0.0.1", port, 1000) # -111 then exit(2); end; |
| 2501 | (* a datagram to a server, and its answer *) |
| 2502 | l := listenUdp("127.0.0.1", 0); |
| 2503 | port := localPort(l); |
| 2504 | u := connectUdp("127.0.0.1", port); |
| 2505 | if (l < 0) or (port <= 0) or (u < 0) or (send(u, "ping") # 4) then exit(2); end; |
| 2506 | n, peer := receive(l, x[0..2]); |
| 2507 | if (n # 2) or (x[0..2] # "pi") or (reply(l, peer, "pong") # 4) then exit(2); end; |
| 2508 | if (read(u, x) # 4) or (x[0..4] # "pong") then exit(2); end; |
| 2509 | close(u); |
| 2510 | close(l); |
| 2511 | end; |
| 2512 | |
| 2513 | (* Runs a copy of this program that catches sigUsr1 and sigUsr2, gives |
| 2514 | up sigUsr1 by ignore(sigUsr1, yes) and sends itself send. The copy |
| 2515 | exits 0 if sigUsr1 then does nothing, or sigUsr2 still comes on |
| 2516 | catch's fd, and 1 if not; this says how it ended. *) |
| 2517 | proc giveUpChild(yes: BOOL; send: INT): (Status, INT); |
| 2518 | var child, c, fd: INT; st: Status; |
| 2519 | begin |
| 2520 | child := fork(); |
| 2521 | if child = 0 then |
| 2522 | fd := catch([sigUsr1, sigUsr2]); |
| 2523 | if fd < 0 then exit(3); end; |
| 2524 | while caught(fd) # 0 do end; (* what the parent had left there *) |
| 2525 | if (ignore(sigUsr1, yes) # 0) or (kill(pid(), send) # 0) then exit(3); end; |
| 2526 | if send = sigUsr1 then |
| 2527 | if ready(fd, 100) then exit(1); end; (* it ought to have ended, or be ignored *) |
| 2528 | exit(0); |
| 2529 | end; |
| 2530 | if not ready(fd, 1000) or (caught(fd) # sigUsr2) then exit(1); end; |
| 2531 | exit(0); |
| 2532 | end; |
| 2533 | _, st, c := wait(child, true); |
| 2534 | return st, c; |
| 2535 | end; |
| 2536 | |
| 2537 | (* The test of giving a caught signal up: with the default back it |
| 2538 | ends the program; ignored it does nothing; and the signals still |
| 2539 | caught still come on catch's fd. *) |
| 2540 | proc testGiveUp; |
| 2541 | var st: Status; c: INT; |
| 2542 | begin |
| 2543 | st, c := giveUpChild(false, sigUsr1); |
| 2544 | if (st # killed) or (c # sigUsr1) then exit(2); end; |
| 2545 | st, c := giveUpChild(true, sigUsr1); |
| 2546 | if (st # exited) or (c # 0) then exit(2); end; |
| 2547 | st, c := giveUpChild(false, sigUsr2); |
| 2548 | if (st # exited) or (c # 0) then exit(2); end; |
| 2549 | end; |
| 2550 | |
| 2551 | (* The test of what a program asks of itself: a signal ignored or not, |
| 2552 | an environment variable unset or empty, and the processors. *) |
| 2553 | proc testProcess; |
| 2554 | var v: []const U8; found: BOOL; |
| 2555 | begin |
| 2556 | if (ignore(sigUsr1, true) # 0) or not ignored(sigUsr1) then exit(2); end; |
| 2557 | if (ignore(sigUsr1, false) # 0) or ignored(sigUsr1) then exit(2); end; |
| 2558 | v, found := findEnv("RAZ_NO_SUCH_VARIABLE"); |
| 2559 | if found or (LEN(v) # 0) then exit(2); end; |
| 2560 | if setEnv("RAZ_SYS_TEST_EMPTY", "") # 0 then exit(2); end; |
| 2561 | v, found := findEnv("RAZ_SYS_TEST_EMPTY"); |
| 2562 | if not found or (LEN(v) # 0) then exit(2); end; |
| 2563 | v, found := findEnv("PATH"); |
| 2564 | if not found or (LEN(v) = 0) then exit(2); end; |
| 2565 | if processors() < 1 then exit(2); end; |
| 2566 | end; |
| 2567 | |
| 2568 | (* The test of what a server needs: a pair of sockets, one end told |
| 2569 | it has no more to read, who is at the other end of a Unix socket and |
| 2570 | of a TCP one, a send that doesn't wait, and a fifo opened without |
| 2571 | waiting. *) |
| 2572 | proc testServer; |
| 2573 | var a, b, l, c, s, port, n, r: INT; buf: [8]U8; full: [4096]U8; w: Remote; j: Info; |
| 2574 | begin |
| 2575 | a, b, r := socketPair(); |
| 2576 | if (r # 0) or (send(a, "hi") # 2) or (read(b, buf) # 2) or (buf[0..2] # "hi") then exit(2); end; |
| 2577 | if (closeWrite(a) # 0) or (read(b, buf) # 0) then exit(2); end; (* b reads an end, *) |
| 2578 | if (send(b, "yo") # 2) or (read(a, buf) # 2) or (buf[0..2] # "yo") then exit(2); end; (* and can still answer *) |
| 2579 | w, r := peer(a); |
| 2580 | if (r # 0) or not w.unix or (w.pid # pid()) or (w.uid # uid()) or (w.port # 0) or (w.addr[0] # 0) then exit(2); end; |
| 2581 | close(a); |
| 2582 | close(b); |
| 2583 | a, b, r := socketPair(); (* sendNow gives -11 once the socket is full, *) |
| 2584 | if r # 0 then exit(2); end; |
| 2585 | n := 0; |
| 2586 | while sendNow(a, full) = LEN(full) do INC(n); end; |
| 2587 | if (n = 0) or (sendNow(a, full) # errAgain) then exit(2); end; (* where send would wait *) |
| 2588 | close(a); |
| 2589 | close(b); |
| 2590 | _, r := peer(-1); |
| 2591 | if r # errBadFd then exit(2); end; |
| 2592 | l := listenTcp("127.0.0.1", 0); |
| 2593 | port := localPort(l); |
| 2594 | c := connectTcp("127.0.0.1", port); |
| 2595 | s := accept(l); |
| 2596 | if (l < 0) or (c < 0) or (s < 0) then exit(2); end; |
| 2597 | w, r := peer(c); |
| 2598 | if (r # 0) or w.unix or w.ipv6 or (w.port # port) or (w.addr[0..4] # "\x7f\x00\x00\x01") then exit(2); end; |
| 2599 | w, r := peer(s); |
| 2600 | if (r # 0) or (w.port = port) or (w.addr[3] # 1) or (w.addr[4] # 0) then exit(2); end; |
| 2601 | close(s); |
| 2602 | close(c); |
| 2603 | close(l); |
| 2604 | _ := remove("/tmp/raz-sys-fifo"); |
| 2605 | if makeFifo("/tmp/raz-sys-fifo", 1A4H) # 0 then exit(2); end; |
| 2606 | j, r := info("/tmp/raz-sys-fifo"); |
| 2607 | if (r # 0) or j.file or j.dir then exit(2); end; (* a fifo is neither *) |
| 2608 | if j.uid # uid() then exit(2); end; (* made by this user *) |
| 2609 | if (chown("/tmp/raz-sys-fifo", j.uid, j.gid) # 0) or (chown("/tmp/raz-sys-fifo", -1, -1) # 0) then exit(2); end; |
| 2610 | if chown("/no/such/file", -1, -1) # -2 then exit(2); end; |
| 2611 | if open("/tmp/raz-sys-fifo", {writing, nonblocking}) # -6 then exit(2); end; (* ENXIO: no reader, and it didn't wait *) |
| 2612 | a := open("/tmp/raz-sys-fifo", {reading, nonblocking}); |
| 2613 | b := open("/tmp/raz-sys-fifo", {writing, nonblocking}); |
| 2614 | if (a < 0) or (b < 0) or (read(a, buf) # errAgain) then exit(2); end; (* nothing to read, and it didn't wait *) |
| 2615 | close(a); |
| 2616 | close(b); |
| 2617 | _ := remove("/tmp/raz-sys-fifo"); |
| 2618 | end; |
| 2619 | |
| 2620 | (* The test of memory and files mapped: a file open only to read, mapped |
| 2621 | to be read, and not to be written; and pages that change what may be |
| 2622 | done with them. *) |
| 2623 | proc testMaps; |
| 2624 | var fd: INT; p, q: ^U8; |
| 2625 | begin |
| 2626 | fd := create("/tmp/raz-sys-map"); |
| 2627 | if (fd < 0) or (write(fd, "hello") # 5) then exit(2); end; |
| 2628 | close(fd); |
| 2629 | fd := openRead("/tmp/raz-sys-map"); |
| 2630 | if (fd < 0) or (mapFile(fd, 5) # nil) then exit(2); end; (* shared and writable: not for a file open to read *) |
| 2631 | p := mapRead(fd, 5); |
| 2632 | if (p = nil) or (p[0..5] # "hello") then exit(2); end; |
| 2633 | munmap(p, 5); |
| 2634 | close(fd); |
| 2635 | _ := remove("/tmp/raz-sys-map"); |
| 2636 | p := mmap(8192); |
| 2637 | if p = nil then exit(2); end; |
| 2638 | p[0] := 1; |
| 2639 | q := p + 4096; |
| 2640 | if (protect(q, 4096, true, false) # 0) or (q[0] # 0) then exit(2); end; |
| 2641 | if protect(q, 4096, true, true) # 0 then exit(2); end; |
| 2642 | q[0] := 2; |
| 2643 | if (protect(p, 4096, false, false) # 0) or (protect(p, 4096, true, true) # 0) or (p[0] # 1) then exit(2); end; (* its bytes stay *) |
| 2644 | munmap(p, 8192); |
| 2645 | end; |
| 2646 | |
| 2647 | (* poll over 100 socket pairs with a byte sent on three, so that all |
| 2648 | three are reported in one call, at their places; a timeout, 0; a |
| 2649 | closed descriptor, ready, and -9 from a read; and an abstract Unix |
| 2650 | socket, whose name starts with a zero byte. *) |
| 2651 | proc testPoll; |
| 2652 | var a: [100]Poll; ends: [100]INT; ps: []Poll; nm: [18]U8; buf: [4]U8; x, y, r, i, l, c, s: INT; |
| 2653 | begin |
| 2654 | for i := 0 to 99 do |
| 2655 | x, y, r := socketPair(); |
| 2656 | if r # 0 then exit(2); end; |
| 2657 | a[i] := {fd: x, read: true}; |
| 2658 | ends[i] := y; |
| 2659 | end; |
| 2660 | ps := a[0..100]; |
| 2661 | if poll(ps, 0) # 0 then exit(2); end; (* a timeout *) |
| 2662 | for i := 3 to 77 by 37 do (* 3, 40, 77 *) |
| 2663 | if send(ends[i], "x") # 1 then exit(2); end; |
| 2664 | end; |
| 2665 | if poll(ps, 1000) # 3 then exit(2); end; |
| 2666 | for i := 0 to 99 do |
| 2667 | if a[i].ready # ((i = 3) or (i = 40) or (i = 77)) then exit(2); end; |
| 2668 | end; |
| 2669 | for i := 3 to 77 by 37 do |
| 2670 | if read(a[i].fd, buf) # 1 then exit(2); end; |
| 2671 | end; |
| 2672 | if poll(ps, 0) # 0 then exit(2); end; |
| 2673 | close(a[5].fd); (* not open: ready, and a read says why *) |
| 2674 | if (poll(ps, 0) # 1) or not a[5].ready or (read(a[5].fd, buf) # -9) then exit(2); end; |
| 2675 | for i := 0 to 99 do |
| 2676 | close(ends[i]); |
| 2677 | if i # 5 then close(a[i].fd); end; |
| 2678 | end; |
| 2679 | nm[0] := 0; (* an abstract name: a zero byte, then 17 *) |
| 2680 | COPY(nm[1..18], "raz-abstract-test"); |
| 2681 | l := listen(nm); |
| 2682 | c := connect(nm); |
| 2683 | if (l < 0) or (c < 0) or (readyOf([c, l], 1000) # l) then exit(2); end; |
| 2684 | s := accept(l); |
| 2685 | if (s < 0) or (send(s, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end; |
| 2686 | close(s); |
| 2687 | close(c); |
| 2688 | close(l); |
| 2689 | end; |
| 2690 | |
| 2691 | (* The module's test: a file written, read back and closed; a |
| 2692 | directory made, listed and emptied; no terminal; a socket, sent to |
| 2693 | after its end; programs; time and the network. *) |
| 2694 | proc main*; |
| 2695 | var free, total: I64; fd, n, err, w, h, l, c, a, m, k, o: INT; got: [1]INT; buf: [16]U8; p, q: ^U8; i, j: Info; d: Dir; name: []const U8; ok: BOOL; |
| 2696 | begin |
| 2697 | testPoll(); |
| 2698 | fd := create("/tmp/raz-sys-test"); |
| 2699 | if fd < 0 then exit(2); end; |
| 2700 | if write(fd, "hello") # 5 then exit(2); end; |
| 2701 | close(fd); |
| 2702 | fd := openRead("/tmp/raz-sys-test"); |
| 2703 | if fd < 0 then exit(2); end; |
| 2704 | n := read(fd, buf); |
| 2705 | if (n # 5) or (buf[0..5] # "hello") or (read(fd, buf) # 0) then exit(2); end; |
| 2706 | close(fd); |
| 2707 | if openRead("/no/such/file") >= 0 then exit(2); end; |
| 2708 | if (errorText(-2) # "No such file or directory") or (errorText(13) # "Permission denied") |
| 2709 | or (errorText(41) # "unknown error") or (errorText(134) # "unknown error") then exit(2); end; |
| 2710 | if isTerminal(-1) then exit(2); end; |
| 2711 | p := mmap(100); |
| 2712 | if (p = nil) or (p[99] # 0) then exit(2); end; |
| 2713 | munmap(p, 100); |
| 2714 | (* files and directories *) |
| 2715 | _ := remove("/tmp/raz-sys-dir/f"); |
| 2716 | _ := remove("/tmp/raz-sys-dir/g"); |
| 2717 | _ := makeDir("/tmp/raz-sys-dir"); |
| 2718 | fd := create("/tmp/raz-sys-dir/f"); |
| 2719 | if (fd < 0) or (write(fd, "abc") # 3) then exit(2); end; |
| 2720 | close(fd); |
| 2721 | if rename("/tmp/raz-sys-dir/f", "/tmp/raz-sys-dir/g") # 0 then exit(2); end; |
| 2722 | i, err := info("/tmp/raz-sys-dir/g"); |
| 2723 | if (err # 0) or (i.size # 3) or i.dir or not i.file or i.symlink then exit(2); end; |
| 2724 | i, err := info("/tmp/raz-sys-dir"); |
| 2725 | if (err # 0) or not i.dir or i.file then exit(2); end; |
| 2726 | i, err := info("/dev/null"); |
| 2727 | if (err # 0) or i.dir or i.file then exit(2); end; |
| 2728 | free, total, err := space("/tmp"); |
| 2729 | if (err # 0) or (total <= 0) or (free < 0) or (free > total) then exit(2); end; |
| 2730 | free, total, err := space("/no/such/dir"); |
| 2731 | if err # -2 then exit(2); end; |
| 2732 | _ := remove("/tmp/raz-sys-link"); |
| 2733 | if symlink("/tmp/raz-sys-dir", "/tmp/raz-sys-link") # 0 then exit(2); end; |
| 2734 | i, err := info("/tmp/raz-sys-link"); |
| 2735 | if (err # 0) or not i.dir or not i.symlink then exit(2); end; |
| 2736 | i, err := linkInfo("/tmp/raz-sys-link"); |
| 2737 | if (err # 0) or i.dir or i.file or not i.symlink or (i.size # 16) then exit(2); end; |
| 2738 | i, err := linkInfo("/tmp/raz-sys-dir"); |
| 2739 | if (err # 0) or not i.dir or i.symlink or (i.ino = 0) then exit(2); end; |
| 2740 | name, err := readLink("/tmp/raz-sys-link"); |
| 2741 | if (err # 0) or (name # "/tmp/raz-sys-dir") then exit(2); end; |
| 2742 | name, err := readLink("/tmp/raz-sys-dir"); |
| 2743 | if err # -22 then exit(2); end; (* EINVAL: not a link *) |
| 2744 | _ := remove("/tmp/raz-sys-link"); |
| 2745 | if symlink("/no/such/file", "/tmp/raz-sys-link") # 0 then exit(2); end; (* dangling *) |
| 2746 | i, err := linkInfo("/tmp/raz-sys-link"); |
| 2747 | if (err # 0) or not i.symlink or (i.size # 13) then exit(2); end; |
| 2748 | _ := remove("/tmp/raz-sys-link"); |
| 2749 | (* open as asked, seek, the mode and the time *) |
| 2750 | _ := remove("/tmp/raz-sys-dir/h"); |
| 2751 | fd := open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}); |
| 2752 | if (fd < 0) or (write(fd, "abc") # 3) then exit(2); end; |
| 2753 | close(fd); |
| 2754 | if open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}) # -17 then exit(2); end; |
| 2755 | fd := open("/tmp/raz-sys-dir/h", {writing, appending}); |
| 2756 | if (fd < 0) or (seek(fd, 0, false) # 0) or (write(fd, "de") # 2) then exit(2); end; |
| 2757 | close(fd); |
| 2758 | fd := open("/tmp/raz-sys-dir/h", {reading, writing}); |
| 2759 | if (seek(fd, -2, true) # 3) or (read(fd, buf) # 2) or (buf[0..2] # "de") then exit(2); end; |
| 2760 | if (seek(fd, 1, false) # 1) or (write(fd, "B") # 1) or (seek(fd, 0, false) # 0) then exit(2); end; |
| 2761 | if (read(fd, buf) # 5) or (buf[0..5] # "aBcde") then exit(2); end; |
| 2762 | (* where it is, its size open, cut short, on the disk *) |
| 2763 | if (position(fd) # 5) or (seek(fd, 2, false) # 2) or (position(fd) # 2) then exit(2); end; |
| 2764 | if (truncate(fd, 4) # 0) or (position(fd) # 2) or (sync(fd) # 0) then exit(2); end; |
| 2765 | i, err := infoOf(fd); |
| 2766 | if (err # 0) or (i.size # 4) or not i.file or i.symlink then exit(2); end; |
| 2767 | m := INT(i.ino); |
| 2768 | i, err := info("/tmp/raz-sys-dir/h"); |
| 2769 | if (err # 0) or (INT(i.ino) # m) or (syncAll(fd) # 0) then exit(2); end; |
| 2770 | if syncAll(-1) # -9 then exit(2); end; (* EBADF *) |
| 2771 | (* the lock: one open file holds it, another can't take it until |
| 2772 | the first is closed *) |
| 2773 | k := openRead("/tmp/raz-sys-dir/h"); |
| 2774 | if (lock(fd, false) # 0) or (lock(k, false) # -11) then exit(2); end; |
| 2775 | close(fd); |
| 2776 | if lock(k, true) # 0 then exit(2); end; |
| 2777 | close(k); |
| 2778 | (* the shared lock: two open files hold it at once, lock is refused |
| 2779 | until both have gone, and a shared one while lock holds it *) |
| 2780 | fd := openRead("/tmp/raz-sys-dir/h"); |
| 2781 | k := openRead("/tmp/raz-sys-dir/h"); |
| 2782 | if (lockShared(fd, false) # 0) or (lockShared(k, true) # 0) then exit(2); end; |
| 2783 | o := openRead("/tmp/raz-sys-dir/h"); |
| 2784 | if lock(o, false) # -11 then exit(2); end; |
| 2785 | close(fd); |
| 2786 | close(k); |
| 2787 | if lock(o, false) # 0 then exit(2); end; |
| 2788 | fd := openRead("/tmp/raz-sys-dir/h"); |
| 2789 | if lockShared(fd, false) # -11 then exit(2); end; |
| 2790 | close(fd); |
| 2791 | close(o); |
| 2792 | if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) |
| 2793 | k := openRead("/tmp/raz-sys-dir/h"); |
| 2794 | if (closeChecked(k) # 0) or (closeChecked(k) # -9) then exit(2); end; |
| 2795 | i, err := infoOf(fd); |
| 2796 | if err # -9 then exit(2); end; |
| 2797 | i, err := info("/tmp/raz-sys-dir/h"); |
| 2798 | if (err # 0) or (i.mode & 1C0H # 180H) or (i.mtime < 1700000000 * 1000000000) then exit(2); end; |
| 2799 | if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; |
| 2800 | i, err := info("/tmp/raz-sys-dir/h"); |
| 2801 | if i.mode # 1EDH then exit(2); end; |
| 2802 | (* setTime, after 1970 and before it, to the nanosecond *) |
| 2803 | if setTime("/tmp/raz-sys-dir/h", 1234567890, 123456789) # 0 then exit(2); end; |
| 2804 | i, err := info("/tmp/raz-sys-dir/h"); |
| 2805 | if (i.mtime # 1234567890123456789) or (i.seconds # 1234567890) then exit(2); end; |
| 2806 | if setTime("/tmp/raz-sys-dir/h", -2, 500000000) # 0 then exit(2); end; |
| 2807 | i, err := info("/tmp/raz-sys-dir/h"); |
| 2808 | if (i.mtime # -1500000000) or (i.seconds # -2) then exit(2); end; |
| 2809 | if setTime("/tmp/raz-sys-dir/h", 10413792000, 0) # 0 then exit(2); end; (* 2300, past mtime's reach *) |
| 2810 | i, err := info("/tmp/raz-sys-dir/h"); |
| 2811 | if i.seconds # 10413792000 then exit(2); end; |
| 2812 | if setTime("/tmp/raz-sys-dir/h", 0, 1000000000) # -22 then exit(2); end; |
| 2813 | if setTime("/no/such/file", 0, 0) # -2 then exit(2); end; |
| 2814 | (* the special bits: set-user-id by chmod, and sticky by makeDirMode *) |
| 2815 | if chmod("/tmp/raz-sys-dir/h", 9EDH) # 0 then exit(2); end; |
| 2816 | i, err := info("/tmp/raz-sys-dir/h"); |
| 2817 | if (i.mode # 1EDH) or (i.special # 800H) then exit(2); end; |
| 2818 | if makeDirMode("/tmp/raz-sys-dir/m", 3C0H) # 0 then exit(2); end; |
| 2819 | i, err := info("/tmp/raz-sys-dir/m"); |
| 2820 | if (err # 0) or not i.dir or (i.mode # 1C0H) or (i.special # 200H) then exit(2); end; |
| 2821 | if removeDir("/tmp/raz-sys-dir/m") # 0 then exit(2); end; |
| 2822 | _ := remove("/tmp/raz-sys-dir/h"); |
| 2823 | fd := openMode("/tmp/raz-sys-dir/h", {writing, creating}, 1C0H); (* rwx------ *) |
| 2824 | close(fd); |
| 2825 | i, err := info("/tmp/raz-sys-dir/h"); |
| 2826 | if (fd < 0) or (err # 0) or (i.mode # 1C0H) then exit(2); end; |
| 2827 | _ := remove("/tmp/raz-sys-dir/h"); |
| 2828 | i, err := info("/no/such/file"); |
| 2829 | if err >= 0 then exit(2); end; |
| 2830 | if openDir(d, "/tmp/raz-sys-dir") # 0 then exit(2); end; |
| 2831 | name, ok := nextEntry(d); |
| 2832 | if not ok or (name # "g") then exit(2); end; |
| 2833 | if (entryKind(d) # isFile) and (entryKind(d) # unknown) then exit(2); end; |
| 2834 | i, err := info("/tmp/raz-sys-dir/g"); |
| 2835 | j, err := info("/tmp/raz-sys-dir"); |
| 2836 | if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) or (i.links # 1) then exit(2); end; |
| 2837 | name, ok := nextEntry(d); |
| 2838 | if ok then exit(2); end; |
| 2839 | closeDir(d); |
| 2840 | if (remove("/tmp/raz-sys-dir/g") # 0) or (remove("/tmp/raz-sys-dir/g") >= 0) then exit(2); end; |
| 2841 | if openDir(d, "/no/such/dir") >= 0 then exit(2); end; |
| 2842 | (* the terminal: none here, fd -1 *) |
| 2843 | if rawMode(-1) or ready(-1, 0) then exit(2); end; |
| 2844 | w, h := termSize(-1); |
| 2845 | if (w # 80) or (h # 24) then exit(2); end; |
| 2846 | (* the environment *) |
| 2847 | if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end; |
| 2848 | (* the environment set: refused, added, set again, grown past its first array *) |
| 2849 | if (setEnv("", "x") # -22) or (setEnv("A=B", "x") # -22) or (setEnv("A", "\x00") # -22) then exit(2); end; |
| 2850 | if (setEnv("RAZ_SYS_TEST", "one") # 0) or (env("RAZ_SYS_TEST") # "one") then exit(2); end; |
| 2851 | name := env("RAZ_SYS_TEST"); |
| 2852 | if (setEnv("RAZ_SYS_TEST", "") # 0) or (env("RAZ_SYS_TEST") # "") or (name # "one") then exit(2); end; |
| 2853 | buf := "RAZ_SYS_TEST_.."; |
| 2854 | for k := 0 to 99 do |
| 2855 | buf[13] := U8('0' + k div 10); |
| 2856 | buf[14] := U8('0' + k mod 10); |
| 2857 | if setEnv(buf[0..15], buf[13..15]) # 0 then exit(2); end; |
| 2858 | end; |
| 2859 | if (env("RAZ_SYS_TEST_42") # "42") or (env("RAZ_SYS_TEST") # "") or (LEN(env("PATH")) = 0) then exit(2); end; |
| 2860 | n := 0; |
| 2861 | k := 0; |
| 2862 | repeat |
| 2863 | name, ok := envAt(k); |
| 2864 | if (LEN(name) > 12) and (name[0..12] = "RAZ_SYS_TEST") then INC(n); end; |
| 2865 | INC(k); |
| 2866 | until not ok; |
| 2867 | if n # 101 then exit(2); end; |
| 2868 | name, err := workDir(); |
| 2869 | if (err # 0) or (LEN(name) = 0) or (name[0] # '/') then exit(2); end; |
| 2870 | if (setWorkDir("/tmp/raz-sys-dir") # 0) or (removeDir("/tmp/raz-sys-dir") # 0) then exit(2); end; |
| 2871 | if (setWorkDir("/") # 0) or (removeDir("/tmp/raz-sys-dir") # -2) then exit(2); end; |
| 2872 | (* a socket: nothing at a path, then a listener that a connection |
| 2873 | makes ready, and a byte from it to the connection *) |
| 2874 | if connect("/tmp/raz-no-such-socket") # -2 then exit(2); end; (* ENOENT *) |
| 2875 | _ := remove("/tmp/raz-sys-socket"); |
| 2876 | l := listen("/tmp/raz-sys-socket"); |
| 2877 | if (l < 0) or (listen("/tmp/raz-sys-socket") # -98) then exit(2); end; (* EADDRINUSE: there already *) |
| 2878 | c := connect("/tmp/raz-sys-socket"); |
| 2879 | if c < 0 then exit(2); end; |
| 2880 | if readyOf([c], 0) # -1 then exit(2); end; |
| 2881 | if readyOf([c, l], 1000) # l then exit(2); end; |
| 2882 | a := accept(l); |
| 2883 | if (a < 0) or (send(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end; |
| 2884 | if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end; |
| 2885 | close(a); |
| 2886 | if (readyOf([c], 1000) # c) or (read(c, buf) # 0) then exit(2); end; (* its end *) |
| 2887 | if send(c, "x") # -32 then exit(2); end; (* EPIPE, and no SIGPIPE *) |
| 2888 | close(c); |
| 2889 | close(l); |
| 2890 | _ := remove("/tmp/raz-sys-socket"); |
| 2891 | (* a file in memory, mapped, and passed with a byte down a socket |
| 2892 | twice, with room for one: what the other side maps is the same *) |
| 2893 | m := memFile(8192); |
| 2894 | p := mapFile(m, 8192); |
| 2895 | if (m < 0) or (p = nil) or (p[8191] # 0) then exit(2); end; |
| 2896 | p[100] := 42; |
| 2897 | l := listen("/tmp/raz-sys-socket"); |
| 2898 | c := connect("/tmp/raz-sys-socket"); |
| 2899 | a := accept(l); |
| 2900 | if (l < 0) or (c < 0) or (a < 0) or (sendFds(c, "", [m]) # -22) or (sendFds(c, "f", [m, m]) # 1) then exit(2); end; |
| 2901 | n, k := receiveFds(a, buf, got); |
| 2902 | if (n # 1) or (k # 1) or (buf[0] # 'f') then exit(2); end; |
| 2903 | q := mapFile(got[0], 8192); |
| 2904 | if (q = nil) or (q[100] # 42) then exit(2); end; |
| 2905 | q[200] := 7; |
| 2906 | if p[200] # 7 then exit(2); end; |
| 2907 | munmap(p, 8192); |
| 2908 | munmap(q, 8192); |
| 2909 | close(got[0]); |
| 2910 | close(m); |
| 2911 | close(a); |
| 2912 | close(c); |
| 2913 | close(l); |
| 2914 | _ := remove("/tmp/raz-sys-socket"); |
| 2915 | testPrograms; |
| 2916 | testShell; |
| 2917 | testNet; |
| 2918 | testProcess; |
| 2919 | testGiveUp; |
| 2920 | testServer; |
| 2921 | testMaps; |
| 2922 | end; |
| 2923 | |
| 2924 | end Sys. |