Sys: sendNow, makeFifo, Info.uid and gid, and chown, in both backends
| @@ -229,6 +229,8 @@ | ||
| 229 | 229 | dev*: U64; (* the file system it is on; with ino, it names the file *) |
| 230 | 230 | links*: U64; (* how many names it has: 1 for a file no hard link shares *) |
| 231 | 231 | special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) |
| 232 | + uid*: INT; (* its owner's user number *) | |
| 233 | + gid*: INT; (* its group's number *) | |
| 232 | 234 | end; |
| 233 | 235 | Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *) |
| 234 | 236 | Dir* = record ... end; (* an open directory; its fields are the target's *) |
| @@ -289,6 +291,8 @@ | ||
| 289 | 291 | proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *) |
| 290 | 292 | proc makeDirMode*(path: []U8; mode: INT): INT; (* makeDir, given mode *) |
| 291 | 293 | proc chmod*(path: []U8; mode: INT): INT; |
| 294 | +proc chown*(path: []U8; uid, gid: INT): INT; (* its owner and group; -1 leaves one as it is *) | |
| 295 | +proc makeFifo*(path: []U8; mode: INT): INT; (* a named pipe, given mode *) | |
| 292 | 296 | proc setTime*(path: []U8; sec: I64; nsec: INT): INT; (* its mtime, and the time it was read, made sec and nsec *) |
| 293 | 297 | proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *) |
| 294 | 298 | proc umask*(mask: INT): INT; (* the bits taken from every file made; the mask it was *) |
| @@ -367,6 +371,7 @@ | ||
| 367 | 371 | proc listenTcp*(addr: []U8; port: INT): INT; |
| 368 | 372 | proc accept*(fd: INT): INT; (* the next connection, waiting for one *) |
| 369 | 373 | proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *) |
| 374 | +proc sendNow*(fd: INT; s: []U8): INT; (* send, but -11 (EAGAIN) where it would wait *) | |
| 370 | 375 | proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *) |
| 371 | 376 | proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *) |
| 372 | 377 | proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *) |
| @@ -446,7 +451,14 @@ | ||
| 446 | 451 | 1EDH (755) for a program, 1A4H (644) for data; `Info.special` holds |
| 447 | 452 | the three above them, set-user-id 800H, set-group-id 400H and sticky |
| 448 | 453 | 200H, which `chmod` sets too, with `mode | special`, as a program that |
| 449 | -copies a file keeps them. `makeDirMode` is `makeDir` that gives the | |
| 454 | +copies a file keeps them. `Info.uid` and `Info.gid` are the owner's | |
| 455 | +and the group's numbers; `chown` sets them, with -1 for one to leave | |
| 456 | +as it is, so that a program that saves a file by writing another and | |
| 457 | +renaming it over the first can give it the first's owner (only root may | |
| 458 | +give a file to another user). `makeFifo` makes a named pipe, given the | |
| 459 | +bits `mode` as `makeDirMode` is; `Info` of one has `file` and `dir` | |
| 460 | +false, and `open` with `nonblocking` doesn't wait for its other end. | |
| 461 | +`makeDirMode` is `makeDir` that gives the | |
| 450 | 462 | directory the bits `mode`, less those the umask takes away, as |
| 451 | 463 | `openMode` gives a file: 1C0H (700) for one only its owner may enter. |
| 452 | 464 | Of the three above them, Linux gives a new directory only sticky; it |
| @@ -652,7 +664,9 @@ | ||
| 652 | 664 | address and port; for a Unix one the user and process that made the |
| 653 | 665 | connection, as the system recorded them, so that a server can check |
| 654 | 666 | that its client is the same user instead of trusting a directory's |
| 655 | -mode. `socketPair` makes two connected stream sockets at once, as a | |
| 667 | +mode. `sendNow` is `send` that never waits: where the socket has no | |
| 668 | +room it gives -11 (`errAgain`), without making reads on it | |
| 669 | +non-blocking too. `socketPair` makes two connected stream sockets at once, as a | |
| 656 | 670 | `listen`, `connect` and `accept` would, with no name on disk. Each is |
| 657 | 671 | closed in a program exec starts. `accept` gives -103 (`errConnAborted`) |
| 658 | 672 | for a client that went before it was taken. |
| @@ -24,6 +24,8 @@ | ||
| 24 | 24 | extern proc mkdir*(path: ^U8; mode: U32): I32; |
| 25 | 25 | extern proc rmdir*(path: ^U8): I32; |
| 26 | 26 | extern proc chmod*(path: ^U8; mode: U32): I32; |
| 27 | +extern proc chown*(path: ^U8; uid, gid: U32): I32; | |
| 28 | +extern proc mkfifo*(path: ^U8; mode: U32): I32; | |
| 27 | 29 | extern proc utimensat*(dir: I32; path, times: ^U8; flags: I32): I32; |
| 28 | 30 | extern proc readlink*(path, buf: ^U8; n: UINT): INT; |
| 29 | 31 | extern proc lseek*(fd: I32; off: I64; whence: I32): I64; |
| @@ -54,6 +54,10 @@ | ||
| 54 | 54 | stModeLen* = 4; |
| 55 | 55 | stSizeAt* = 48; |
| 56 | 56 | stSizeLen* = 8; |
| 57 | + stUidAt* = 28; | |
| 58 | + stUidLen* = 4; | |
| 59 | + stGidAt* = 32; | |
| 60 | + stGidLen* = 4; | |
| 57 | 61 | stMtimeAt* = 88; |
| 58 | 62 | stMtimeLen* = 8; |
| 59 | 63 | stMtimeNsAt* = 96; |
| @@ -100,6 +104,7 @@ | ||
| 100 | 104 | soPeercred* = 17; |
| 101 | 105 | shutWr* = 1; |
| 102 | 106 | msgNosignal* = 16384; |
| 107 | + msgDontwait* = 64; | |
| 103 | 108 | msgCmsgCloexec* = 1073741824; |
| 104 | 109 | scmRights* = 1; |
| 105 | 110 | sockaddrLen* = 128; |
| @@ -55,6 +55,8 @@ | ||
| 55 | 55 | dev*: U64; (* the file system it is on; with ino, it names the file *) |
| 56 | 56 | links*: U64; (* how many names it has: 1 for a file no hard link shares *) |
| 57 | 57 | special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) |
| 58 | + uid*: INT; (* its owner's user number *) | |
| 59 | + gid*: INT; (* its group's number *) | |
| 58 | 60 | end; |
| 59 | 61 | |
| 60 | 62 | (* What a directory says a name in it is, without asking the file: |
| @@ -456,6 +458,27 @@ | ||
| 456 | 458 | return SYSCALL(83, ADR(a), mode); (* mkdir *) |
| 457 | 459 | end; |
| 458 | 460 | |
| 461 | +(* Makes a fifo (a named pipe) at path with the permission bits mode, | |
| 462 | + less those the umask takes away. 0, or minus an errno. *) | |
| 463 | +proc makeFifo*(path: []U8; mode: INT): INT; | |
| 464 | +var a: [4096]U8; | |
| 465 | +begin | |
| 466 | + if not cstr(path, a) then return -36; end; | |
| 467 | + return SYSCALL(133, ADR(a), 1000H | (mode & 1FFH), 0); (* mknod, S_IFIFO *) | |
| 468 | +end; | |
| 469 | + | |
| 470 | + | |
| 471 | +(* Sets the owner and group of what path names, following a symbolic | |
| 472 | + link: a user or group number, or -1 to leave it as it is. Only | |
| 473 | + root may give a file away. 0, or minus an errno. *) | |
| 474 | +proc chown*(path: []U8; uid, gid: INT): INT; | |
| 475 | +var a: [4096]U8; | |
| 476 | +begin | |
| 477 | + if not cstr(path, a) then return -36; end; | |
| 478 | + return SYSCALL(92, ADR(a), uid, gid); (* chown *) | |
| 479 | +end; | |
| 480 | + | |
| 481 | + | |
| 459 | 482 | (* Removes the directory at path, which must be empty. *) |
| 460 | 483 | proc removeDir*(path: []U8): INT; |
| 461 | 484 | var a: [4096]U8; |
| @@ -540,6 +563,8 @@ | ||
| 540 | 563 | i.seconds := st[11]; |
| 541 | 564 | i.mode := st[3] & 1FFH; |
| 542 | 565 | i.special := st[3] & 0E00H; |
| 566 | + i.uid := (st[3] shr 32) & 0FFFFFFFFH; (* st_uid *) | |
| 567 | + i.gid := st[4] & 0FFFFFFFFH; (* st_gid *) | |
| 543 | 568 | i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) |
| 544 | 569 | i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) |
| 545 | 570 | return i; |
| @@ -1012,6 +1037,15 @@ | ||
| 1012 | 1037 | return SYSCALL(44, fd, ADR(s[0]), LEN(s), 4000H, 0, 0); (* sendto, MSG_NOSIGNAL, no address *) |
| 1013 | 1038 | end; |
| 1014 | 1039 | |
| 1040 | +(* send, but never waits: returns -11 (EAGAIN) where send would wait for | |
| 1041 | + room, and doesn't make reads on fd non-blocking too. *) | |
| 1042 | +proc sendNow*(fd: INT; s: []U8): INT; | |
| 1043 | +begin | |
| 1044 | + if LEN(s) = 0 then return 0; end; | |
| 1045 | + return SYSCALL(44, fd, ADR(s[0]), LEN(s), 4040H, 0, 0); (* sendto, MSG_NOSIGNAL | MSG_DONTWAIT, no address *) | |
| 1046 | +end; | |
| 1047 | + | |
| 1048 | + | |
| 1015 | 1049 | (* The first of reads that has something to read within ms |
| 1016 | 1050 | milliseconds, or of writes that has room to write, or that has |
| 1017 | 1051 | reached its end or failed, so that a read or a write won't wait; -1 |
| @@ -2434,9 +2468,10 @@ | ||
| 2434 | 2468 | |
| 2435 | 2469 | (* The test of what a server needs: a pair of sockets, one end told |
| 2436 | 2470 | it has no more to read, who is at the other end of a Unix socket and |
| 2437 | - of a TCP one, and a fifo opened without waiting. *) | |
| 2471 | + of a TCP one, a send that doesn't wait, and a fifo opened without | |
| 2472 | + waiting. *) | |
| 2438 | 2473 | proc testServer; |
| 2439 | -var a, b, l, c, s, port, child, r, how: INT; buf: [8]U8; w: Remote; st: Status; | |
| 2474 | +var a, b, l, c, s, port, n, r: INT; buf: [8]U8; full: [4096]U8; w: Remote; j: Info; | |
| 2440 | 2475 | begin |
| 2441 | 2476 | a, b, r := socketPair(); |
| 2442 | 2477 | if (r # 0) or (send(a, "hi") # 2) or (read(b, buf) # 2) or (buf[0..2] # "hi") then exit(2); end; |
| @@ -2446,6 +2481,13 @@ | ||
| 2446 | 2481 | 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; |
| 2447 | 2482 | close(a); |
| 2448 | 2483 | close(b); |
| 2484 | + a, b, r := socketPair(); (* sendNow gives -11 once the socket is full, *) | |
| 2485 | + if r # 0 then exit(2); end; | |
| 2486 | + n := 0; | |
| 2487 | + while sendNow(a, full) = LEN(full) do INC(n); end; | |
| 2488 | + if (n = 0) or (sendNow(a, full) # errAgain) then exit(2); end; (* where send would wait *) | |
| 2489 | + close(a); | |
| 2490 | + close(b); | |
| 2449 | 2491 | _, r := peer(-1); |
| 2450 | 2492 | if r # errBadFd then exit(2); end; |
| 2451 | 2493 | l := listenTcp("127.0.0.1", 0); |
| @@ -2461,9 +2503,12 @@ | ||
| 2461 | 2503 | close(c); |
| 2462 | 2504 | close(l); |
| 2463 | 2505 | _ := remove("/tmp/raz-sys-fifo"); |
| 2464 | - child := spawn({path: "mkfifo", args: ["mkfifo", "/tmp/raz-sys-fifo"]}); | |
| 2465 | - r, st, how := waitExit(child); | |
| 2466 | - if (st # exited) or (how # 0) then exit(2); end; | |
| 2506 | + if makeFifo("/tmp/raz-sys-fifo", 1A4H) # 0 then exit(2); end; | |
| 2507 | + j, r := info("/tmp/raz-sys-fifo"); | |
| 2508 | + if (r # 0) or j.file or j.dir then exit(2); end; (* a fifo is neither *) | |
| 2509 | + if j.uid # uid() then exit(2); end; (* made by this user *) | |
| 2510 | + if (chown("/tmp/raz-sys-fifo", j.uid, j.gid) # 0) or (chown("/tmp/raz-sys-fifo", -1, -1) # 0) then exit(2); end; | |
| 2511 | + if chown("/no/such/file", -1, -1) # -2 then exit(2); end; | |
| 2467 | 2512 | if open("/tmp/raz-sys-fifo", {writing, nonblocking}) # -6 then exit(2); end; (* ENXIO: no reader, and it didn't wait *) |
| 2468 | 2513 | a := open("/tmp/raz-sys-fifo", {reading, nonblocking}); |
| 2469 | 2514 | b := open("/tmp/raz-sys-fifo", {writing, nonblocking}); |
| @@ -72,6 +72,8 @@ | ||
| 72 | 72 | dev*: U64; (* the file system it is on; with ino, it names the file *) |
| 73 | 73 | links*: U64; (* how many names it has: 1 for a file no hard link shares *) |
| 74 | 74 | special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) |
| 75 | + uid*: INT; (* its owner's user number *) | |
| 76 | + gid*: INT; (* its group's number *) | |
| 75 | 77 | end; |
| 76 | 78 | |
| 77 | 79 | (* What a directory says a name in it is, without asking the file: |
| @@ -546,6 +548,27 @@ | ||
| 546 | 548 | return 0; |
| 547 | 549 | end; |
| 548 | 550 | |
| 551 | +(* Makes a fifo (a named pipe) at path with the permission bits mode, | |
| 552 | + less those the umask takes away. 0, or minus an errno. *) | |
| 553 | +proc makeFifo*(path: []U8; mode: INT): INT; | |
| 554 | +var a: [4096]U8; | |
| 555 | +begin | |
| 556 | + if not cstr(path, a) then return -36; end; | |
| 557 | + if C.mkfifo(ADR(a[0]), U32(mode & 1FFH)) # 0 then return failed(); end; | |
| 558 | + return 0; | |
| 559 | +end; | |
| 560 | + | |
| 561 | +(* Sets the owner and group of what path names, following a symbolic | |
| 562 | + link: a user or group number, or -1 to leave it as it is. Only | |
| 563 | + root may give a file away. 0, or minus an errno. *) | |
| 564 | +proc chown*(path: []U8; uid, gid: INT): INT; | |
| 565 | +var a: [4096]U8; | |
| 566 | +begin | |
| 567 | + if not cstr(path, a) then return -36; end; | |
| 568 | + if C.chown(ADR(a[0]), U32(uid & 0FFFFFFFFH), U32(gid & 0FFFFFFFFH)) # 0 then return failed(); end; | |
| 569 | + return 0; | |
| 570 | +end; | |
| 571 | + | |
| 549 | 572 | (* Sets when the contents of what path names last changed, and when it |
| 550 | 573 | was last read, to sec seconds and nsec nanoseconds since 1970, as a |
| 551 | 574 | program copying a file keeps its time: Info.seconds gives sec back, |
| @@ -626,6 +649,8 @@ | ||
| 626 | 649 | i.seconds := field(p, CAbi.stMtimeAt, CAbi.stMtimeLen); |
| 627 | 650 | i.mode := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 1FFH; |
| 628 | 651 | i.special := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 0E00H; |
| 652 | + i.uid := INT(field(p, CAbi.stUidAt, CAbi.stUidLen)); | |
| 653 | + i.gid := INT(field(p, CAbi.stGidAt, CAbi.stGidLen)); | |
| 629 | 654 | i.dir := fileType(st) = CAbi.sIfdir; |
| 630 | 655 | i.file := fileType(st) = CAbi.sIfreg; |
| 631 | 656 | return i; |
| @@ -1059,6 +1084,17 @@ | ||
| 1059 | 1084 | return r; |
| 1060 | 1085 | end; |
| 1061 | 1086 | |
| 1087 | +(* send, but never waits: returns -11 (EAGAIN) where send would wait for | |
| 1088 | + room, and doesn't make reads on fd non-blocking too. *) | |
| 1089 | +proc sendNow*(fd: INT; s: []U8): INT; | |
| 1090 | +var r: INT; | |
| 1091 | +begin | |
| 1092 | + if LEN(s) = 0 then return 0; end; | |
| 1093 | + r := C.send(I32(fd), ADR(s[0]), UINT(LEN(s)), CAbi.msgNosignal | CAbi.msgDontwait); | |
| 1094 | + if r < 0 then return failed(); end; | |
| 1095 | + return r; | |
| 1096 | +end; | |
| 1097 | + | |
| 1062 | 1098 | (* The first of reads that has something to read within ms |
| 1063 | 1099 | milliseconds, or of writes that has room to write, or that has |
| 1064 | 1100 | reached its end or failed, so that a read or a write won't wait; -1 |
| @@ -2383,9 +2419,10 @@ | ||
| 2383 | 2419 | |
| 2384 | 2420 | (* The test of what a server needs: a pair of sockets, one end told |
| 2385 | 2421 | it has no more to read, who is at the other end of a Unix socket and |
| 2386 | - of a TCP one, and a fifo opened without waiting. *) | |
| 2422 | + of a TCP one, a send that doesn't wait, and a fifo opened without | |
| 2423 | + waiting. *) | |
| 2387 | 2424 | proc testServer; |
| 2388 | -var a, b, l, c, s, port, child, r, how: INT; buf: [8]U8; w: Remote; st: Status; | |
| 2425 | +var a, b, l, c, s, port, n, r: INT; buf: [8]U8; full: [4096]U8; w: Remote; j: Info; | |
| 2389 | 2426 | begin |
| 2390 | 2427 | a, b, r := socketPair(); |
| 2391 | 2428 | if (r # 0) or (send(a, "hi") # 2) or (read(b, buf) # 2) or (buf[0..2] # "hi") then exit(2); end; |
| @@ -2395,6 +2432,13 @@ | ||
| 2395 | 2432 | 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; |
| 2396 | 2433 | close(a); |
| 2397 | 2434 | close(b); |
| 2435 | + a, b, r := socketPair(); (* sendNow gives -11 once the socket is full, *) | |
| 2436 | + if r # 0 then exit(2); end; | |
| 2437 | + n := 0; | |
| 2438 | + while sendNow(a, full) = LEN(full) do INC(n); end; | |
| 2439 | + if (n = 0) or (sendNow(a, full) # errAgain) then exit(2); end; (* where send would wait *) | |
| 2440 | + close(a); | |
| 2441 | + close(b); | |
| 2398 | 2442 | _, r := peer(-1); |
| 2399 | 2443 | if r # errBadFd then exit(2); end; |
| 2400 | 2444 | l := listenTcp("127.0.0.1", 0); |
| @@ -2410,9 +2454,12 @@ | ||
| 2410 | 2454 | close(c); |
| 2411 | 2455 | close(l); |
| 2412 | 2456 | _ := remove("/tmp/raz-sys-fifo"); |
| 2413 | - child := spawn({path: "mkfifo", args: ["mkfifo", "/tmp/raz-sys-fifo"]}); | |
| 2414 | - r, st, how := waitExit(child); | |
| 2415 | - if (st # exited) or (how # 0) then exit(2); end; | |
| 2457 | + if makeFifo("/tmp/raz-sys-fifo", 1A4H) # 0 then exit(2); end; | |
| 2458 | + j, r := info("/tmp/raz-sys-fifo"); | |
| 2459 | + if (r # 0) or j.file or j.dir then exit(2); end; (* a fifo is neither *) | |
| 2460 | + if j.uid # uid() then exit(2); end; (* made by this user *) | |
| 2461 | + if (chown("/tmp/raz-sys-fifo", j.uid, j.gid) # 0) or (chown("/tmp/raz-sys-fifo", -1, -1) # 0) then exit(2); end; | |
| 2462 | + if chown("/no/such/file", -1, -1) # -2 then exit(2); end; | |
| 2416 | 2463 | if open("/tmp/raz-sys-fifo", {writing, nonblocking}) # -6 then exit(2); end; (* ENXIO: no reader, and it didn't wait *) |
| 2417 | 2464 | a := open("/tmp/raz-sys-fifo", {reading, nonblocking}); |
| 2418 | 2465 | b := open("/tmp/raz-sys-fifo", {writing, nonblocking}); |
| @@ -126,6 +126,7 @@ | ||
| 126 | 126 | READ("stDev", struct stat, st_dev); READ("stIno", struct stat, st_ino); |
| 127 | 127 | READ("stNlink", struct stat, st_nlink); READ("stMode", struct stat, st_mode); |
| 128 | 128 | READ("stSize", struct stat, st_size); |
| 129 | + READ("stUid", struct stat, st_uid); READ("stGid", struct stat, st_gid); | |
| 129 | 130 | READ("stMtime", struct stat, st_mtim.tv_sec); |
| 130 | 131 | READ("stMtimeNs", struct stat, st_mtim.tv_nsec); |
| 131 | 132 | K("sIfmt", S_IFMT); K("sIfdir", S_IFDIR); K("sIfreg", S_IFREG); |
| @@ -153,6 +154,7 @@ | ||
| 153 | 154 | K("solSocket", SOL_SOCKET); K("soReuseaddr", SO_REUSEADDR); |
| 154 | 155 | K("soError", SO_ERROR); K("soPeercred", SO_PEERCRED); /* Linux's */ |
| 155 | 156 | K("shutWr", SHUT_WR); K("msgNosignal", MSG_NOSIGNAL); |
| 157 | + K("msgDontwait", MSG_DONTWAIT); | |
| 156 | 158 | K("msgCmsgCloexec", MSG_CMSG_CLOEXEC); K("scmRights", SCM_RIGHTS); |
| 157 | 159 | K("sockaddrLen", sizeof(struct sockaddr_storage)); |
| 158 | 160 | READ("saFamily", struct sockaddr, sa_family); |