From 28c63ff8b386b1d020a4f3af796ae7fb5efb4807 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:20:55 +0100 Subject: [PATCH] Sys: sendNow, makeFifo, Info.uid and gid, and chown, in both backends --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index b84f2aa..1d55ba1 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -229,6 +229,8 @@ dev*: U64; (* the file system it is on; with ino, it names the file *) links*: U64; (* how many names it has: 1 for a file no hard link shares *) special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) + uid*: INT; (* its owner's user number *) + gid*: INT; (* its group's number *) end; Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *) Dir* = record ... end; (* an open directory; its fields are the target's *) @@ -289,6 +291,8 @@ proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *) proc makeDirMode*(path: []U8; mode: INT): INT; (* makeDir, given mode *) proc chmod*(path: []U8; mode: INT): INT; +proc chown*(path: []U8; uid, gid: INT): INT; (* its owner and group; -1 leaves one as it is *) +proc makeFifo*(path: []U8; mode: INT): INT; (* a named pipe, given mode *) proc setTime*(path: []U8; sec: I64; nsec: INT): INT; (* its mtime, and the time it was read, made sec and nsec *) proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *) proc umask*(mask: INT): INT; (* the bits taken from every file made; the mask it was *) @@ -367,6 +371,7 @@ proc listenTcp*(addr: []U8; port: INT): INT; proc accept*(fd: INT): INT; (* the next connection, waiting for one *) proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *) +proc sendNow*(fd: INT; s: []U8): INT; (* send, but -11 (EAGAIN) where it would wait *) proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *) proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *) proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *) @@ -446,7 +451,14 @@ 1EDH (755) for a program, 1A4H (644) for data; `Info.special` holds the three above them, set-user-id 800H, set-group-id 400H and sticky 200H, which `chmod` sets too, with `mode | special`, as a program that -copies a file keeps them. `makeDirMode` is `makeDir` that gives the +copies a file keeps them. `Info.uid` and `Info.gid` are the owner's +and the group's numbers; `chown` sets them, with -1 for one to leave +as it is, so that a program that saves a file by writing another and +renaming it over the first can give it the first's owner (only root may +give a file to another user). `makeFifo` makes a named pipe, given the +bits `mode` as `makeDirMode` is; `Info` of one has `file` and `dir` +false, and `open` with `nonblocking` doesn't wait for its other end. +`makeDirMode` is `makeDir` that gives the directory the bits `mode`, less those the umask takes away, as `openMode` gives a file: 1C0H (700) for one only its owner may enter. Of the three above them, Linux gives a new directory only sticky; it @@ -652,7 +664,9 @@ address and port; for a Unix one the user and process that made the connection, as the system recorded them, so that a server can check that its client is the same user instead of trusting a directory's -mode. `socketPair` makes two connected stream sockets at once, as a +mode. `sendNow` is `send` that never waits: where the socket has no +room it gives -11 (`errAgain`), without making reads on it +non-blocking too. `socketPair` makes two connected stream sockets at once, as a `listen`, `connect` and `accept` would, with no name on disk. Each is closed in a program exec starts. `accept` gives -103 (`errConnAborted`) for a client that went before it was taken. diff --git a/lib/C.raz b/lib/C.raz index 26f1d79..f3330e7 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -24,6 +24,8 @@ extern proc mkdir*(path: ^U8; mode: U32): I32; extern proc rmdir*(path: ^U8): I32; extern proc chmod*(path: ^U8; mode: U32): I32; +extern proc chown*(path: ^U8; uid, gid: U32): I32; +extern proc mkfifo*(path: ^U8; mode: U32): I32; extern proc utimensat*(dir: I32; path, times: ^U8; flags: I32): I32; extern proc readlink*(path, buf: ^U8; n: UINT): INT; extern proc lseek*(fd: I32; off: I64; whence: I32): I64; diff --git a/lib/CAbi.linux-x86_64.raz b/lib/CAbi.linux-x86_64.raz index 177f7c8..afe78a7 100644 --- a/lib/CAbi.linux-x86_64.raz +++ b/lib/CAbi.linux-x86_64.raz @@ -54,6 +54,10 @@ stModeLen* = 4; stSizeAt* = 48; stSizeLen* = 8; + stUidAt* = 28; + stUidLen* = 4; + stGidAt* = 32; + stGidLen* = 4; stMtimeAt* = 88; stMtimeLen* = 8; stMtimeNsAt* = 96; @@ -100,6 +104,7 @@ soPeercred* = 17; shutWr* = 1; msgNosignal* = 16384; + msgDontwait* = 64; msgCmsgCloexec* = 1073741824; scmRights* = 1; sockaddrLen* = 128; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 15664d6..396cdc8 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -55,6 +55,8 @@ dev*: U64; (* the file system it is on; with ino, it names the file *) links*: U64; (* how many names it has: 1 for a file no hard link shares *) special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) + uid*: INT; (* its owner's user number *) + gid*: INT; (* its group's number *) end; (* What a directory says a name in it is, without asking the file: @@ -456,6 +458,27 @@ return SYSCALL(83, ADR(a), mode); (* mkdir *) end; +(* Makes a fifo (a named pipe) at path with the permission bits mode, + less those the umask takes away. 0, or minus an errno. *) +proc makeFifo*(path: []U8; mode: INT): INT; +var a: [4096]U8; +begin + if not cstr(path, a) then return -36; end; + return SYSCALL(133, ADR(a), 1000H | (mode & 1FFH), 0); (* mknod, S_IFIFO *) +end; + + +(* Sets the owner and group of what path names, following a symbolic + link: a user or group number, or -1 to leave it as it is. Only + root may give a file away. 0, or minus an errno. *) +proc chown*(path: []U8; uid, gid: INT): INT; +var a: [4096]U8; +begin + if not cstr(path, a) then return -36; end; + return SYSCALL(92, ADR(a), uid, gid); (* chown *) +end; + + (* Removes the directory at path, which must be empty. *) proc removeDir*(path: []U8): INT; var a: [4096]U8; @@ -540,6 +563,8 @@ i.seconds := st[11]; i.mode := st[3] & 1FFH; i.special := st[3] & 0E00H; + i.uid := (st[3] shr 32) & 0FFFFFFFFH; (* st_uid *) + i.gid := st[4] & 0FFFFFFFFH; (* st_gid *) i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) return i; @@ -1012,6 +1037,15 @@ return SYSCALL(44, fd, ADR(s[0]), LEN(s), 4000H, 0, 0); (* sendto, MSG_NOSIGNAL, no address *) end; +(* send, but never waits: returns -11 (EAGAIN) where send would wait for + room, and doesn't make reads on fd non-blocking too. *) +proc sendNow*(fd: INT; s: []U8): INT; +begin + if LEN(s) = 0 then return 0; end; + return SYSCALL(44, fd, ADR(s[0]), LEN(s), 4040H, 0, 0); (* sendto, MSG_NOSIGNAL | MSG_DONTWAIT, no address *) +end; + + (* The first of reads that has something to read within ms milliseconds, or of writes that has room to write, or that has reached its end or failed, so that a read or a write won't wait; -1 @@ -2434,9 +2468,10 @@ (* The test of what a server needs: a pair of sockets, one end told it has no more to read, who is at the other end of a Unix socket and - of a TCP one, and a fifo opened without waiting. *) + of a TCP one, a send that doesn't wait, and a fifo opened without + waiting. *) proc testServer; -var a, b, l, c, s, port, child, r, how: INT; buf: [8]U8; w: Remote; st: Status; +var a, b, l, c, s, port, n, r: INT; buf: [8]U8; full: [4096]U8; w: Remote; j: Info; begin a, b, r := socketPair(); 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 @@ 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; close(a); close(b); + a, b, r := socketPair(); (* sendNow gives -11 once the socket is full, *) + if r # 0 then exit(2); end; + n := 0; + while sendNow(a, full) = LEN(full) do INC(n); end; + if (n = 0) or (sendNow(a, full) # errAgain) then exit(2); end; (* where send would wait *) + close(a); + close(b); _, r := peer(-1); if r # errBadFd then exit(2); end; l := listenTcp("127.0.0.1", 0); @@ -2461,9 +2503,12 @@ close(c); close(l); _ := remove("/tmp/raz-sys-fifo"); - child := spawn({path: "mkfifo", args: ["mkfifo", "/tmp/raz-sys-fifo"]}); - r, st, how := waitExit(child); - if (st # exited) or (how # 0) then exit(2); end; + if makeFifo("/tmp/raz-sys-fifo", 1A4H) # 0 then exit(2); end; + j, r := info("/tmp/raz-sys-fifo"); + if (r # 0) or j.file or j.dir then exit(2); end; (* a fifo is neither *) + if j.uid # uid() then exit(2); end; (* made by this user *) + if (chown("/tmp/raz-sys-fifo", j.uid, j.gid) # 0) or (chown("/tmp/raz-sys-fifo", -1, -1) # 0) then exit(2); end; + if chown("/no/such/file", -1, -1) # -2 then exit(2); end; if open("/tmp/raz-sys-fifo", {writing, nonblocking}) # -6 then exit(2); end; (* ENXIO: no reader, and it didn't wait *) a := open("/tmp/raz-sys-fifo", {reading, nonblocking}); b := open("/tmp/raz-sys-fifo", {writing, nonblocking}); diff --git a/lib/Sys.raz b/lib/Sys.raz index ce1e3ae..cde9a1d 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -72,6 +72,8 @@ dev*: U64; (* the file system it is on; with ino, it names the file *) links*: U64; (* how many names it has: 1 for a file no hard link shares *) special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) + uid*: INT; (* its owner's user number *) + gid*: INT; (* its group's number *) end; (* What a directory says a name in it is, without asking the file: @@ -546,6 +548,27 @@ return 0; end; +(* Makes a fifo (a named pipe) at path with the permission bits mode, + less those the umask takes away. 0, or minus an errno. *) +proc makeFifo*(path: []U8; mode: INT): INT; +var a: [4096]U8; +begin + if not cstr(path, a) then return -36; end; + if C.mkfifo(ADR(a[0]), U32(mode & 1FFH)) # 0 then return failed(); end; + return 0; +end; + +(* Sets the owner and group of what path names, following a symbolic + link: a user or group number, or -1 to leave it as it is. Only + root may give a file away. 0, or minus an errno. *) +proc chown*(path: []U8; uid, gid: INT): INT; +var a: [4096]U8; +begin + if not cstr(path, a) then return -36; end; + if C.chown(ADR(a[0]), U32(uid & 0FFFFFFFFH), U32(gid & 0FFFFFFFFH)) # 0 then return failed(); end; + return 0; +end; + (* Sets when the contents of what path names last changed, and when it was last read, to sec seconds and nsec nanoseconds since 1970, as a program copying a file keeps its time: Info.seconds gives sec back, @@ -626,6 +649,8 @@ i.seconds := field(p, CAbi.stMtimeAt, CAbi.stMtimeLen); i.mode := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 1FFH; i.special := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 0E00H; + i.uid := INT(field(p, CAbi.stUidAt, CAbi.stUidLen)); + i.gid := INT(field(p, CAbi.stGidAt, CAbi.stGidLen)); i.dir := fileType(st) = CAbi.sIfdir; i.file := fileType(st) = CAbi.sIfreg; return i; @@ -1059,6 +1084,17 @@ return r; end; +(* send, but never waits: returns -11 (EAGAIN) where send would wait for + room, and doesn't make reads on fd non-blocking too. *) +proc sendNow*(fd: INT; s: []U8): INT; +var r: INT; +begin + if LEN(s) = 0 then return 0; end; + r := C.send(I32(fd), ADR(s[0]), UINT(LEN(s)), CAbi.msgNosignal | CAbi.msgDontwait); + if r < 0 then return failed(); end; + return r; +end; + (* The first of reads that has something to read within ms milliseconds, or of writes that has room to write, or that has reached its end or failed, so that a read or a write won't wait; -1 @@ -2383,9 +2419,10 @@ (* The test of what a server needs: a pair of sockets, one end told it has no more to read, who is at the other end of a Unix socket and - of a TCP one, and a fifo opened without waiting. *) + of a TCP one, a send that doesn't wait, and a fifo opened without + waiting. *) proc testServer; -var a, b, l, c, s, port, child, r, how: INT; buf: [8]U8; w: Remote; st: Status; +var a, b, l, c, s, port, n, r: INT; buf: [8]U8; full: [4096]U8; w: Remote; j: Info; begin a, b, r := socketPair(); 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 @@ 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; close(a); close(b); + a, b, r := socketPair(); (* sendNow gives -11 once the socket is full, *) + if r # 0 then exit(2); end; + n := 0; + while sendNow(a, full) = LEN(full) do INC(n); end; + if (n = 0) or (sendNow(a, full) # errAgain) then exit(2); end; (* where send would wait *) + close(a); + close(b); _, r := peer(-1); if r # errBadFd then exit(2); end; l := listenTcp("127.0.0.1", 0); @@ -2410,9 +2454,12 @@ close(c); close(l); _ := remove("/tmp/raz-sys-fifo"); - child := spawn({path: "mkfifo", args: ["mkfifo", "/tmp/raz-sys-fifo"]}); - r, st, how := waitExit(child); - if (st # exited) or (how # 0) then exit(2); end; + if makeFifo("/tmp/raz-sys-fifo", 1A4H) # 0 then exit(2); end; + j, r := info("/tmp/raz-sys-fifo"); + if (r # 0) or j.file or j.dir then exit(2); end; (* a fifo is neither *) + if j.uid # uid() then exit(2); end; (* made by this user *) + if (chown("/tmp/raz-sys-fifo", j.uid, j.gid) # 0) or (chown("/tmp/raz-sys-fifo", -1, -1) # 0) then exit(2); end; + if chown("/no/such/file", -1, -1) # -2 then exit(2); end; if open("/tmp/raz-sys-fifo", {writing, nonblocking}) # -6 then exit(2); end; (* ENXIO: no reader, and it didn't wait *) a := open("/tmp/raz-sys-fifo", {reading, nonblocking}); b := open("/tmp/raz-sys-fifo", {writing, nonblocking}); diff --git a/tools/cabi.c b/tools/cabi.c index a3591ac..3a21f91 100644 --- a/tools/cabi.c +++ b/tools/cabi.c @@ -126,6 +126,7 @@ READ("stDev", struct stat, st_dev); READ("stIno", struct stat, st_ino); READ("stNlink", struct stat, st_nlink); READ("stMode", struct stat, st_mode); READ("stSize", struct stat, st_size); + READ("stUid", struct stat, st_uid); READ("stGid", struct stat, st_gid); READ("stMtime", struct stat, st_mtim.tv_sec); READ("stMtimeNs", struct stat, st_mtim.tv_nsec); K("sIfmt", S_IFMT); K("sIfdir", S_IFDIR); K("sIfreg", S_IFREG); @@ -153,6 +154,7 @@ K("solSocket", SOL_SOCKET); K("soReuseaddr", SO_REUSEADDR); K("soError", SO_ERROR); K("soPeercred", SO_PEERCRED); /* Linux's */ K("shutWr", SHUT_WR); K("msgNosignal", MSG_NOSIGNAL); + K("msgDontwait", MSG_DONTWAIT); K("msgCmsgCloexec", MSG_CMSG_CLOEXEC); K("scmRights", SCM_RIGHTS); K("sockaddrLen", sizeof(struct sockaddr_storage)); READ("saFamily", struct sockaddr, sa_family);