Sys: mapRead and protect; open, openMode, openRead, create and openDir close on exec mapRead maps a file open only to read, protect sets the access of pages. The files open gives are now closed in a program exec starts, as pipe's are, and keepOnExec(fd, true) keeps one; spawn is unchanged since it closes all but its fds and more. No caller in lib, src, tools, examples or test relied on it. In both backends; tests in Sys.main, ExecOpened and ExecOpenedC.
| @@ -16,7 +16,7 @@ | ||
| 16 | 16 | | Module | What | |
| 17 | 17 | |---|---| |
| 18 | 18 | | `Rt` | ending the program, `exit`, `exitStatus` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | |
| 19 | -| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `lockShared`, `close`, `closeChecked`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `makeDirMode`, `chmod`, `setTime`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `findEnv`, `envAt`, `setEnv`, `workDir`, `setWorkDir`; programs, `spawn`, `exec` and `execve`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; errors, by name, and what one means, `errorText`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others, which takes what each C library fixes from `CAbi` | | |
| 19 | +| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `lockShared`, `close`, `closeChecked`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `mapRead`, `protect`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `makeDirMode`, `chmod`, `setTime`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `findEnv`, `envAt`, `setEnv`, `workDir`, `setWorkDir`; programs, `spawn`, `exec` and `execve`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; errors, by name, and what one means, `errorText`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others, which takes what each C library fixes from `CAbi` | | |
| 20 | 20 | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | |
| 21 | 21 | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | |
| 22 | 22 | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | |
| @@ -256,7 +256,7 @@ | ||
| 256 | 256 | |
| 257 | 257 | proc write*(fd: INT; s: []U8): INT; (* the bytes written *) |
| 258 | 258 | proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) |
| 259 | -proc open*(path: []U8; how: Accesses): INT; (* its fd *) | |
| 259 | +proc open*(path: []U8; how: Accesses): INT; (* its fd, closed on exec *) | |
| 260 | 260 | proc openMode*(path: []U8; how: Accesses; mode: INT): INT; (* open, a file it makes given mode *) |
| 261 | 261 | proc openRead*(path: []U8): INT; (* open(path, {reading}) *) |
| 262 | 262 | proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *) |
| @@ -272,8 +272,10 @@ | ||
| 272 | 272 | proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *) |
| 273 | 273 | proc dupTo*(fd, into: INT): INT; (* into made the same file as fd, closing what it was *) |
| 274 | 274 | proc dupAbove*(fd, least: INT): INT; (* a new fd for the same file, the lowest free from least on, closed on exec *) |
| 275 | -proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd left open, or not, in the program exec runs *) | |
| 275 | +proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd left open, or not (as at first for a file made by open or pipe), in the program exec runs *) | |
| 276 | 276 | proc isTerminal*(fd: INT): BOOL; |
| 277 | +proc mapRead*(fd, size: INT): ^U8; (* the first size bytes of fd, which may be open only to read, mapped read only; or nil *) | |
| 278 | +proc protect*(p: ^U8; size: INT; read, write: BOOL): INT; (* the pages at p readable and writable as asked; neither, and touching them stops the program *) | |
| 277 | 279 | proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) |
| 278 | 280 | proc munmap*(p: ^U8; size: INT); |
| 279 | 281 | proc yield*; (* the processor given to another thread *) |
| @@ -501,8 +503,10 @@ | ||
| 501 | 503 | shell's `exec cmd` does, found and started as `spawn` would start it, |
| 502 | 504 | `p.tty`, `p.group`, `p.foreground`, `p.fds` and `p.dir` included, |
| 503 | 505 | except that it keeps this program's other open files: each stays open |
| 504 | -in the program unless `keepOnExec(fd, false)` was said of it, as it is | |
| 505 | -of `pipe`'s, and those of `p.more` are kept. It returns only if the | |
| 506 | +in the program unless it is closed on exec, as the files `open`, | |
| 507 | +`pipe`, a socket or `memFile` gives are, unless `keepOnExec(fd, true)` | |
| 508 | +was said of them; `dup` and `dupTo` give files that stay open; and | |
| 509 | +those of `p.more` are kept. It returns only if the | |
| 506 | 510 | program can't run: -2 (ENOENT) or -13 (EACCES) if there is none to |
| 507 | 511 | run, with nothing done; or, if the program is found but won't start, |
| 508 | 512 | minus the errno, with what `p` asks of the terminal, the group, the |
| @@ -533,11 +537,16 @@ | ||
| 533 | 537 | `access` says whether this user, by its real ids, may read, write or |
| 534 | 538 | execute a file, as `test -r`, `-w` and `-x` ask, which `Info.mode` |
| 535 | 539 | alone can't say; an empty `how` asks only whether it is there. |
| 536 | -`open` refuses `executing`, with -22 (EINVAL). `nonblocking` opens a file without waiting: a fifo opened to write | |
| 537 | -waits for a reader and a fifo opened to read waits for a writer, but | |
| 538 | -with `nonblocking` the open returns at once (-6, ENXIO, for a write | |
| 539 | -with no reader), and a read or write that would wait is -11 later, as | |
| 540 | -`setBlocking(fd, false)` makes it. `umask` sets the bits | |
| 540 | +`open` refuses `executing`, with -22 (EINVAL). `nonblocking` opens a | |
| 541 | +file without waiting: a fifo opened to write waits for a reader and a | |
| 542 | +fifo opened to read waits for a writer, but with `nonblocking` the open | |
| 543 | +returns at once (-6, ENXIO, for a write with no reader), and a read or | |
| 544 | +write that would wait is -11 later, as `setBlocking(fd, false)` makes | |
| 545 | +it. A file `open`, `openMode`, `openRead` or `create` gives is closed in | |
| 546 | +a program `exec` or `execve` starts, as `pipe`'s are, so that a fifo or | |
| 547 | +a log one program holds doesn't reach every program it runs; | |
| 548 | +`keepOnExec(fd, true)` keeps one open, and `spawn` is unchanged, since | |
| 549 | +it passes its `fds` and `more` as before. `umask` sets the bits | |
| 541 | 550 | taken away from the mode of every file this program, and those it |
| 542 | 551 | starts, make, and says what they were. `envAt` gives the environment |
| 543 | 552 | one variable at a time, from 0, for a shell to list it and to pass it |
| @@ -653,6 +662,15 @@ | ||
| 653 | 662 | what one program writes there another sees at once: so a program hands |
| 654 | 663 | an X server or a Wayland compositor its pixels, by `sendFds` of a |
| 655 | 664 | `memFile` both map, rather than by copying them down a socket. |
| 665 | +`mapFile` needs a file open to read and write; `mapRead` maps one that | |
| 666 | +is open only to read, a dictionary that isn't the user's to write, for | |
| 667 | +reading only: a write there stops the program. `protect` changes what | |
| 668 | +may be done with pages `mmap` gave, from a page boundary: read, write, | |
| 669 | +both, or neither, so that touching them stops the program, as a guard | |
| 670 | +page after a buffer does, or a reserve mapped in full and opened as it | |
| 671 | +fills. It takes the address as the system does, so with a C library it | |
| 672 | +is that library's whether an address not on a page boundary is -22 | |
| 673 | +(EINVAL) or rounded down. | |
| 656 | 674 | |
| 657 | 675 | `readyOf` waits on several file descriptors at once, as a program does |
| 658 | 676 | that listens to a server and a terminal both; a descriptor at its end, |
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | extern proc close*(fd: I32): I32; |
| 11 | 11 | extern proc mmap*(addr: ^U8; n: UINT; prot, flags, fd: I32; off: I64): ^U8; |
| 12 | 12 | extern proc munmap*(addr: ^U8; n: UINT): I32; |
| 13 | +extern proc mprotect*(addr: ^U8; n: UINT; prot: I32): I32; | |
| 13 | 14 | extern proc isatty*(fd: I32): I32; |
| 14 | 15 | extern proc exit*(status: I32); |
| 15 | 16 | extern proc _exit*(status: I32); |
| @@ -193,11 +193,11 @@ | ||
| 193 | 193 | return true; |
| 194 | 194 | end; |
| 195 | 195 | |
| 196 | -(* open's flags for how; Linux's numbers *) | |
| 196 | +(* open's flags for how, with O_CLOEXEC; Linux's numbers *) | |
| 197 | 197 | proc flags(how: Accesses): I32; |
| 198 | 198 | var f: I32; |
| 199 | 199 | begin |
| 200 | - f := 0; (* O_RDONLY *) | |
| 200 | + f := 80000H; (* O_CLOEXEC, with O_RDONLY *) | |
| 201 | 201 | if writing in how then |
| 202 | 202 | f := 1; (* O_WRONLY *) |
| 203 | 203 | if reading in how then f := 2; end; (* O_RDWR *) |
| @@ -210,9 +210,9 @@ | ||
| 210 | 210 | return f; |
| 211 | 211 | end; |
| 212 | 212 | |
| 213 | -(* The file at path, opened as how asks: its file descriptor, or minus | |
| 214 | - an errno. A file it creates has the permission bits mode, less | |
| 215 | - those the umask takes away. *) | |
| 213 | +(* The file at path, opened as how asks: its file descriptor, closed in | |
| 214 | + a program exec starts, or minus an errno. A file it creates has the | |
| 215 | + permission bits mode, less those the umask takes away. *) | |
| 216 | 216 | proc openMode*(path: []U8; how: Accesses; mode: INT): INT; |
| 217 | 217 | var buf: [4096]U8; |
| 218 | 218 | begin |
| @@ -228,13 +228,15 @@ | ||
| 228 | 228 | return openMode(path, how, 1A4H); (* 644 *) |
| 229 | 229 | end; |
| 230 | 230 | |
| 231 | -(* The file at path, opened for reading. *) | |
| 231 | +(* The file at path, opened for reading, closed in a program exec | |
| 232 | + starts. *) | |
| 232 | 233 | proc openRead*(path: []U8): INT; |
| 233 | 234 | begin |
| 234 | 235 | return open(path, {reading}); |
| 235 | 236 | end; |
| 236 | 237 | |
| 237 | -(* The file at path, made empty or new, opened for writing. *) | |
| 238 | +(* The file at path, made empty or new, opened for writing, closed in a | |
| 239 | + program exec starts. *) | |
| 238 | 240 | proc create*(path: []U8): INT; |
| 239 | 241 | begin |
| 240 | 242 | return open(path, {writing, creating, truncating}); |
| @@ -337,7 +339,8 @@ | ||
| 337 | 339 | end; |
| 338 | 340 | |
| 339 | 341 | (* Whether fd stays open in the program exec runs in this one's place: |
| 340 | - yes, or no, closed then, as pipe's fds are at first. 0, or minus an | |
| 342 | + yes, or no, closed then, as the fds open, pipe and socket give are at | |
| 343 | + first. 0, or minus an | |
| 341 | 344 | errno. *) |
| 342 | 345 | proc keepOnExec*(fd: INT; yes: BOOL): INT; |
| 343 | 346 | var f: INT; |
| @@ -388,6 +391,29 @@ | ||
| 388 | 391 | return ^U8(p); |
| 389 | 392 | end; |
| 390 | 393 | |
| 394 | +(* The first size bytes of the file fd, which may be open only to | |
| 395 | + read, mapped to be read only: writing there stops the program. nil | |
| 396 | + if it can't be; munmap gives them back. *) | |
| 397 | +proc mapRead*(fd, size: INT): ^U8; | |
| 398 | +var p: INT; | |
| 399 | +begin | |
| 400 | + p := SYSCALL(9, 0, size, 1, 2, fd, 0); (* mmap: read; MAP_PRIVATE *) | |
| 401 | + if p < 0 then return nil; end; | |
| 402 | + return ^U8(p); | |
| 403 | +end; | |
| 404 | + | |
| 405 | +(* Lets the pages from p, size bytes from a page boundary, be read and | |
| 406 | + written as read and write say; neither, and touching them stops the | |
| 407 | + program. 0, or minus an errno. *) | |
| 408 | +proc protect*(p: ^U8; size: INT; read, write: BOOL): INT; | |
| 409 | +var prot: INT; | |
| 410 | +begin | |
| 411 | + prot := 0; (* PROT_NONE *) | |
| 412 | + if read then prot := 1; end; | |
| 413 | + if write then prot := prot | 2; end; | |
| 414 | + return SYSCALL(10, p, size, prot); (* mprotect *) | |
| 415 | +end; | |
| 416 | + | |
| 391 | 417 | (* Whether the file descriptor fd is a terminal: whether it has a |
| 392 | 418 | terminal's settings to read (ioctl TCGETS). *) |
| 393 | 419 | proc isTerminal*(fd: INT): BOOL; |
| @@ -593,7 +619,7 @@ | ||
| 593 | 619 | d.pos := 0; |
| 594 | 620 | d.len := 0; |
| 595 | 621 | if not cstr(path, a) then return -36; end; |
| 596 | - d.fd := SYSCALL(2, ADR(a), 10000H); (* open, O_RDONLY, O_DIRECTORY *) | |
| 622 | + d.fd := SYSCALL(2, ADR(a), 90000H); (* open, O_RDONLY, O_DIRECTORY, O_CLOEXEC *) | |
| 597 | 623 | if d.fd < 0 then return d.fd; end; |
| 598 | 624 | return 0; |
| 599 | 625 | end; |
| @@ -1770,7 +1796,11 @@ | ||
| 1770 | 1796 | if p.foreground then _ := foreground(0, SYSCALL(111)); end; (* getpgrp *) |
| 1771 | 1797 | end; |
| 1772 | 1798 | for i := 0 to 2 do |
| 1773 | - if p.fds[i] # 0 then _ := SYSCALL(33, p.fds[i], i); end; (* dup2 *) | |
| 1799 | + if p.fds[i] = i then | |
| 1800 | + _ := keepOnExec(i, true); (* dup2 of a file onto itself would leave it closed on exec *) | |
| 1801 | + elsif p.fds[i] # 0 then | |
| 1802 | + _ := SYSCALL(33, p.fds[i], i); (* dup2 *) | |
| 1803 | + end; | |
| 1774 | 1804 | end; |
| 1775 | 1805 | if LEN(p.dir) > 0 then return SYSCALL(80, ADR(dir[0])); end; (* chdir *) |
| 1776 | 1806 | return 0; |
| @@ -1836,7 +1866,8 @@ | ||
| 1836 | 1866 | |
| 1837 | 1867 | (* Runs the program p describes in this one's place, as spawn would |
| 1838 | 1868 | start it, but keeping this program's other fds as they are, open |
| 1839 | - unless keepOnExec said no; the fds of p.more are kept. It returns | |
| 1869 | + unless closed on exec (as open and pipe give) and not kept by | |
| 1870 | + keepOnExec; the fds of p.more are kept. It returns | |
| 1840 | 1871 | only if it can't: -2 (ENOENT) or -13 (EACCES) if there is no |
| 1841 | 1872 | program to run, before anything is done; or, once the program's |
| 1842 | 1873 | terminal, group, fds and working directory are as p asks, which |
| @@ -2398,6 +2429,33 @@ | ||
| 2398 | 2429 | _ := remove("/tmp/raz-sys-fifo"); |
| 2399 | 2430 | end; |
| 2400 | 2431 | |
| 2432 | +(* The test of memory and files mapped: a file open only to read, mapped | |
| 2433 | + to be read, and not to be written; and pages that change what may be | |
| 2434 | + done with them. *) | |
| 2435 | +proc testMaps; | |
| 2436 | +var fd: INT; p, q: ^U8; | |
| 2437 | +begin | |
| 2438 | + fd := create("/tmp/raz-sys-map"); | |
| 2439 | + if (fd < 0) or (write(fd, "hello") # 5) then exit(2); end; | |
| 2440 | + close(fd); | |
| 2441 | + fd := openRead("/tmp/raz-sys-map"); | |
| 2442 | + if (fd < 0) or (mapFile(fd, 5) # nil) then exit(2); end; (* shared and writable: not for a file open to read *) | |
| 2443 | + p := mapRead(fd, 5); | |
| 2444 | + if (p = nil) or (p[0..5] # "hello") then exit(2); end; | |
| 2445 | + munmap(p, 5); | |
| 2446 | + close(fd); | |
| 2447 | + _ := remove("/tmp/raz-sys-map"); | |
| 2448 | + p := mmap(8192); | |
| 2449 | + if p = nil then exit(2); end; | |
| 2450 | + p[0] := 1; | |
| 2451 | + q := p + 4096; | |
| 2452 | + if (protect(q, 4096, true, false) # 0) or (q[0] # 0) then exit(2); end; | |
| 2453 | + if protect(q, 4096, true, true) # 0 then exit(2); end; | |
| 2454 | + q[0] := 2; | |
| 2455 | + 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 *) | |
| 2456 | + munmap(p, 8192); | |
| 2457 | +end; | |
| 2458 | + | |
| 2401 | 2459 | (* The module's test: a file written, read back and closed; a |
| 2402 | 2460 | directory made, listed and emptied; no terminal; a socket, sent to |
| 2403 | 2461 | after its end; programs; time and the network. *) |
| @@ -2626,6 +2684,7 @@ | ||
| 2626 | 2684 | testNet; |
| 2627 | 2685 | testProcess; |
| 2628 | 2686 | testServer; |
| 2687 | + testMaps; | |
| 2629 | 2688 | end; |
| 2630 | 2689 | |
| 2631 | 2690 | end Sys. |
| @@ -234,11 +234,11 @@ | ||
| 234 | 234 | return true; |
| 235 | 235 | end; |
| 236 | 236 | |
| 237 | -(* open's flags for how *) | |
| 237 | +(* open's flags for how, with O_CLOEXEC *) | |
| 238 | 238 | proc flags(how: Accesses): I32; |
| 239 | 239 | var f: I32; |
| 240 | 240 | begin |
| 241 | - f := CAbi.oRdonly; | |
| 241 | + f := CAbi.oRdonly | CAbi.oCloexec; | |
| 242 | 242 | if writing in how then |
| 243 | 243 | f := CAbi.oWronly; |
| 244 | 244 | if reading in how then f := CAbi.oRdwr; end; |
| @@ -251,9 +251,9 @@ | ||
| 251 | 251 | return f; |
| 252 | 252 | end; |
| 253 | 253 | |
| 254 | -(* The file at path, opened as how asks: its file descriptor, or minus | |
| 255 | - an errno. A file it creates has the permission bits mode, less | |
| 256 | - those the umask takes away. *) | |
| 254 | +(* The file at path, opened as how asks: its file descriptor, closed in | |
| 255 | + a program exec starts, or minus an errno. A file it creates has the | |
| 256 | + permission bits mode, less those the umask takes away. *) | |
| 257 | 257 | proc openMode*(path: []U8; how: Accesses; mode: INT): INT; |
| 258 | 258 | var buf: [4096]U8; r: INT; |
| 259 | 259 | begin |
| @@ -271,13 +271,15 @@ | ||
| 271 | 271 | return openMode(path, how, 1A4H); (* 644 *) |
| 272 | 272 | end; |
| 273 | 273 | |
| 274 | -(* The file at path, opened for reading. *) | |
| 274 | +(* The file at path, opened for reading, closed in a program exec | |
| 275 | + starts. *) | |
| 275 | 276 | proc openRead*(path: []U8): INT; |
| 276 | 277 | begin |
| 277 | 278 | return open(path, {reading}); |
| 278 | 279 | end; |
| 279 | 280 | |
| 280 | -(* The file at path, made empty or new, opened for writing. *) | |
| 281 | +(* The file at path, made empty or new, opened for writing, closed in a | |
| 282 | + program exec starts. *) | |
| 281 | 283 | proc create*(path: []U8): INT; |
| 282 | 284 | begin |
| 283 | 285 | return open(path, {writing, creating, truncating}); |
| @@ -408,7 +410,8 @@ | ||
| 408 | 410 | end; |
| 409 | 411 | |
| 410 | 412 | (* Whether fd stays open in the program exec runs in this one's place: |
| 411 | - yes, or no, closed then, as pipe's fds are at first. 0, or minus an | |
| 413 | + yes, or no, closed then, as the fds open, pipe and socket give are at | |
| 414 | + first. 0, or minus an | |
| 412 | 415 | errno. *) |
| 413 | 416 | proc keepOnExec*(fd: INT; yes: BOOL): INT; |
| 414 | 417 | var f: I32; |
| @@ -463,6 +466,30 @@ | ||
| 463 | 466 | return p; |
| 464 | 467 | end; |
| 465 | 468 | |
| 469 | +(* The first size bytes of the file fd, which may be open only to | |
| 470 | + read, mapped to be read only: writing there stops the program. nil | |
| 471 | + if it can't be; munmap gives them back. *) | |
| 472 | +proc mapRead*(fd, size: INT): ^U8; | |
| 473 | +var p: ^U8; | |
| 474 | +begin | |
| 475 | + p := C.mmap(nil, UINT(size), CAbi.protRead, CAbi.mapPrivate, I32(fd), 0); | |
| 476 | + if INT(p) = -1 then return nil; end; | |
| 477 | + return p; | |
| 478 | +end; | |
| 479 | + | |
| 480 | +(* Lets the pages from p, size bytes from a page boundary, be read and | |
| 481 | + written as read and write say; neither, and touching them stops the | |
| 482 | + program. 0, or minus an errno. *) | |
| 483 | +proc protect*(p: ^U8; size: INT; read, write: BOOL): INT; | |
| 484 | +var prot: I32; | |
| 485 | +begin | |
| 486 | + prot := 0; (* PROT_NONE *) | |
| 487 | + if read then prot := CAbi.protRead; end; | |
| 488 | + if write then prot := prot | CAbi.protWrite; end; | |
| 489 | + if C.mprotect(p, UINT(size), prot) # 0 then return failed(); end; | |
| 490 | + return 0; | |
| 491 | +end; | |
| 492 | + | |
| 466 | 493 | (* Renames old to new, replacing what new names: 0, or minus an errno. *) |
| 467 | 494 | proc rename*(old, new: []U8): INT; |
| 468 | 495 | var a, b: [4096]U8; |
| @@ -1659,7 +1686,11 @@ | ||
| 1659 | 1686 | if p.foreground then _ := C.tcsetpgrp(0, C.getpgrp()); end; |
| 1660 | 1687 | end; |
| 1661 | 1688 | for i := 0 to 2 do |
| 1662 | - if p.fds[i] # 0 then _ := C.dup2(I32(p.fds[i]), I32(i)); end; | |
| 1689 | + if p.fds[i] = i then | |
| 1690 | + _ := keepOnExec(i, true); (* dup2 of a file onto itself would leave it closed on exec *) | |
| 1691 | + elsif p.fds[i] # 0 then | |
| 1692 | + _ := C.dup2(I32(p.fds[i]), I32(i)); | |
| 1693 | + end; | |
| 1663 | 1694 | end; |
| 1664 | 1695 | if (LEN(p.dir) > 0) and (C.chdir(ADR(dir[0])) # 0) then return failed(); end; |
| 1665 | 1696 | return 0; |
| @@ -1738,7 +1769,8 @@ | ||
| 1738 | 1769 | |
| 1739 | 1770 | (* Runs the program p describes in this one's place, as spawn would |
| 1740 | 1771 | start it, but keeping this program's other fds as they are, open |
| 1741 | - unless keepOnExec said no; the fds of p.more are kept. It returns | |
| 1772 | + unless closed on exec (as open and pipe give) and not kept by | |
| 1773 | + keepOnExec; the fds of p.more are kept. It returns | |
| 1742 | 1774 | only if it can't: -2 (ENOENT) or -13 (EACCES) if there is no |
| 1743 | 1775 | program to run, before anything is done; or, once the program's |
| 1744 | 1776 | terminal, group, fds and working directory are as p asks, which |
| @@ -2343,6 +2375,33 @@ | ||
| 2343 | 2375 | _ := remove("/tmp/raz-sys-fifo"); |
| 2344 | 2376 | end; |
| 2345 | 2377 | |
| 2378 | +(* The test of memory and files mapped: a file open only to read, mapped | |
| 2379 | + to be read, and not to be written; and pages that change what may be | |
| 2380 | + done with them. *) | |
| 2381 | +proc testMaps; | |
| 2382 | +var fd: INT; p, q: ^U8; | |
| 2383 | +begin | |
| 2384 | + fd := create("/tmp/raz-sys-map"); | |
| 2385 | + if (fd < 0) or (write(fd, "hello") # 5) then exit(2); end; | |
| 2386 | + close(fd); | |
| 2387 | + fd := openRead("/tmp/raz-sys-map"); | |
| 2388 | + if (fd < 0) or (mapFile(fd, 5) # nil) then exit(2); end; (* shared and writable: not for a file open to read *) | |
| 2389 | + p := mapRead(fd, 5); | |
| 2390 | + if (p = nil) or (p[0..5] # "hello") then exit(2); end; | |
| 2391 | + munmap(p, 5); | |
| 2392 | + close(fd); | |
| 2393 | + _ := remove("/tmp/raz-sys-map"); | |
| 2394 | + p := mmap(8192); | |
| 2395 | + if p = nil then exit(2); end; | |
| 2396 | + p[0] := 1; | |
| 2397 | + q := p + 4096; | |
| 2398 | + if (protect(q, 4096, true, false) # 0) or (q[0] # 0) then exit(2); end; | |
| 2399 | + if protect(q, 4096, true, true) # 0 then exit(2); end; | |
| 2400 | + q[0] := 2; | |
| 2401 | + 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 *) | |
| 2402 | + munmap(p, 8192); | |
| 2403 | +end; | |
| 2404 | + | |
| 2346 | 2405 | (* The module's test, as Linux's Sys has it: a file written, read back |
| 2347 | 2406 | and closed; a directory made, listed and emptied; no terminal; a |
| 2348 | 2407 | socket, sent to after its end; programs; time and the network. *) |
| @@ -2570,6 +2629,7 @@ | ||
| 2570 | 2629 | testNet; |
| 2571 | 2630 | testProcess; |
| 2572 | 2631 | testServer; |
| 2632 | + testMaps; | |
| 2573 | 2633 | end; |
| 2574 | 2634 | |
| 2575 | 2635 | end Sys. |
| @@ -0,0 +1,2 @@ | ||
| 1 | +closed 3 | |
| 2 | +open 4 |
| @@ -0,0 +1,17 @@ | ||
| 1 | +(* Over the C library: a file open gives is closed in the program exec | |
| 2 | + runs, unless keepOnExec says to keep it: of two opened files, sh sees | |
| 3 | + the second. *) | |
| 4 | +module ExecOpenedC; | |
| 5 | +import Sys; | |
| 6 | + | |
| 7 | +proc main*: INT; | |
| 8 | +var a, b: INT; | |
| 9 | +begin | |
| 10 | + a := Sys.openRead("/dev/null"); | |
| 11 | + b := Sys.create("/dev/null"); | |
| 12 | + if (a # 3) or (b # 4) or (Sys.keepOnExec(b, true) # 0) then return 1; end; | |
| 13 | + return Sys.exec({path: "sh", | |
| 14 | + args: ["sh", "-c", "for n in 3 4; do if [ -e /proc/self/fd/$n ]; then echo open $n; else echo closed $n; fi; done"]}); | |
| 15 | +end; | |
| 16 | + | |
| 17 | +end ExecOpenedC. |
| @@ -0,0 +1,2 @@ | ||
| 1 | +closed 3 | |
| 2 | +open 4 |
| @@ -0,0 +1,16 @@ | ||
| 1 | +(* A file open gives is closed in the program exec runs, unless | |
| 2 | + keepOnExec says to keep it: of two opened files, sh sees the second. *) | |
| 3 | +module ExecOpened; | |
| 4 | +import Sys; | |
| 5 | + | |
| 6 | +proc main*: INT; | |
| 7 | +var a, b: INT; | |
| 8 | +begin | |
| 9 | + a := Sys.openRead("/dev/null"); | |
| 10 | + b := Sys.create("/dev/null"); | |
| 11 | + if (a # 3) or (b # 4) or (Sys.keepOnExec(b, true) # 0) then return 1; end; | |
| 12 | + return Sys.exec({path: "sh", | |
| 13 | + args: ["sh", "-c", "for n in 3 4; do if [ -e /proc/self/fd/$n ]; then echo open $n; else echo closed $n; fi; done"]}); | |
| 14 | +end; | |
| 15 | + | |
| 16 | +end ExecOpened. |