raz / commit 28c63ff8

commit
28c63ff8b386b1d020a4f3af796ae7fb5efb4807
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
e8ec426b
Sys: sendNow, makeFifo, Info.uid and gid, and chown, in both backends

patch · log

6 files changed, +127 -12

doc/raz-lib.md (history) modified +16 -2

@@ -229,6 +229,8 @@
229229 dev*: U64; (* the file system it is on; with ino, it names the file *)
230230 links*: U64; (* how many names it has: 1 for a file no hard link shares *)
231231 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 *)
232234 end;
233235 Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *)
234236 Dir* = record ... end; (* an open directory; its fields are the target's *)
@@ -289,6 +291,8 @@
289291 proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *)
290292 proc makeDirMode*(path: []U8; mode: INT): INT; (* makeDir, given mode *)
291293 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 *)
292296 proc setTime*(path: []U8; sec: I64; nsec: INT): INT; (* its mtime, and the time it was read, made sec and nsec *)
293297 proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *)
294298 proc umask*(mask: INT): INT; (* the bits taken from every file made; the mask it was *)
@@ -367,6 +371,7 @@
367371 proc listenTcp*(addr: []U8; port: INT): INT;
368372 proc accept*(fd: INT): INT; (* the next connection, waiting for one *)
369373 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 *)
370375 proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *)
371376 proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *)
372377 proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *)
@@ -446,7 +451,14 @@
446451 1EDH (755) for a program, 1A4H (644) for data; `Info.special` holds
447452 the three above them, set-user-id 800H, set-group-id 400H and sticky
448453 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
450462 directory the bits `mode`, less those the umask takes away, as
451463 `openMode` gives a file: 1C0H (700) for one only its owner may enter.
452464 Of the three above them, Linux gives a new directory only sticky; it
@@ -652,7 +664,9 @@
652664 address and port; for a Unix one the user and process that made the
653665 connection, as the system recorded them, so that a server can check
654666 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
656670 `listen`, `connect` and `accept` would, with no name on disk. Each is
657671 closed in a program exec starts. `accept` gives -103 (`errConnAborted`)
658672 for a client that went before it was taken.

lib/C.raz (history) modified +2 -0

@@ -24,6 +24,8 @@
2424 extern proc mkdir*(path: ^U8; mode: U32): I32;
2525 extern proc rmdir*(path: ^U8): I32;
2626 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;
2729 extern proc utimensat*(dir: I32; path, times: ^U8; flags: I32): I32;
2830 extern proc readlink*(path, buf: ^U8; n: UINT): INT;
2931 extern proc lseek*(fd: I32; off: I64; whence: I32): I64;

lib/CAbi.linux-x86_64.raz (history) modified +5 -0

@@ -54,6 +54,10 @@
5454 stModeLen* = 4;
5555 stSizeAt* = 48;
5656 stSizeLen* = 8;
57+ stUidAt* = 28;
58+ stUidLen* = 4;
59+ stGidAt* = 32;
60+ stGidLen* = 4;
5761 stMtimeAt* = 88;
5862 stMtimeLen* = 8;
5963 stMtimeNsAt* = 96;
@@ -100,6 +104,7 @@
100104 soPeercred* = 17;
101105 shutWr* = 1;
102106 msgNosignal* = 16384;
107+ msgDontwait* = 64;
103108 msgCmsgCloexec* = 1073741824;
104109 scmRights* = 1;
105110 sockaddrLen* = 128;

lib/Sys.linux-x86_64.raz (history) modified +50 -5

@@ -55,6 +55,8 @@
5555 dev*: U64; (* the file system it is on; with ino, it names the file *)
5656 links*: U64; (* how many names it has: 1 for a file no hard link shares *)
5757 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 *)
5860 end;
5961
6062 (* What a directory says a name in it is, without asking the file:
@@ -456,6 +458,27 @@
456458 return SYSCALL(83, ADR(a), mode); (* mkdir *)
457459 end;
458460
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+
459482 (* Removes the directory at path, which must be empty. *)
460483 proc removeDir*(path: []U8): INT;
461484 var a: [4096]U8;
@@ -540,6 +563,8 @@
540563 i.seconds := st[11];
541564 i.mode := st[3] & 1FFH;
542565 i.special := st[3] & 0E00H;
566+ i.uid := (st[3] shr 32) & 0FFFFFFFFH; (* st_uid *)
567+ i.gid := st[4] & 0FFFFFFFFH; (* st_gid *)
543568 i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *)
544569 i.file := st[3] & 0F000H = 8000H; (* S_IFREG *)
545570 return i;
@@ -1012,6 +1037,15 @@
10121037 return SYSCALL(44, fd, ADR(s[0]), LEN(s), 4000H, 0, 0); (* sendto, MSG_NOSIGNAL, no address *)
10131038 end;
10141039
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+
10151049 (* The first of reads that has something to read within ms
10161050 milliseconds, or of writes that has room to write, or that has
10171051 reached its end or failed, so that a read or a write won't wait; -1
@@ -2434,9 +2468,10 @@
24342468
24352469 (* The test of what a server needs: a pair of sockets, one end told
24362470 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. *)
24382473 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;
24402475 begin
24412476 a, b, r := socketPair();
24422477 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 @@
24462481 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;
24472482 close(a);
24482483 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);
24492491 _, r := peer(-1);
24502492 if r # errBadFd then exit(2); end;
24512493 l := listenTcp("127.0.0.1", 0);
@@ -2461,9 +2503,12 @@
24612503 close(c);
24622504 close(l);
24632505 _ := 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;
24672512 if open("/tmp/raz-sys-fifo", {writing, nonblocking}) # -6 then exit(2); end; (* ENXIO: no reader, and it didn't wait *)
24682513 a := open("/tmp/raz-sys-fifo", {reading, nonblocking});
24692514 b := open("/tmp/raz-sys-fifo", {writing, nonblocking});

lib/Sys.raz (history) modified +52 -5

@@ -72,6 +72,8 @@
7272 dev*: U64; (* the file system it is on; with ino, it names the file *)
7373 links*: U64; (* how many names it has: 1 for a file no hard link shares *)
7474 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 *)
7577 end;
7678
7779 (* What a directory says a name in it is, without asking the file:
@@ -546,6 +548,27 @@
546548 return 0;
547549 end;
548550
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+
549572 (* Sets when the contents of what path names last changed, and when it
550573 was last read, to sec seconds and nsec nanoseconds since 1970, as a
551574 program copying a file keeps its time: Info.seconds gives sec back,
@@ -626,6 +649,8 @@
626649 i.seconds := field(p, CAbi.stMtimeAt, CAbi.stMtimeLen);
627650 i.mode := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 1FFH;
628651 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));
629654 i.dir := fileType(st) = CAbi.sIfdir;
630655 i.file := fileType(st) = CAbi.sIfreg;
631656 return i;
@@ -1059,6 +1084,17 @@
10591084 return r;
10601085 end;
10611086
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+
10621098 (* The first of reads that has something to read within ms
10631099 milliseconds, or of writes that has room to write, or that has
10641100 reached its end or failed, so that a read or a write won't wait; -1
@@ -2383,9 +2419,10 @@
23832419
23842420 (* The test of what a server needs: a pair of sockets, one end told
23852421 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. *)
23872424 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;
23892426 begin
23902427 a, b, r := socketPair();
23912428 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 @@
23952432 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;
23962433 close(a);
23972434 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);
23982442 _, r := peer(-1);
23992443 if r # errBadFd then exit(2); end;
24002444 l := listenTcp("127.0.0.1", 0);
@@ -2410,9 +2454,12 @@
24102454 close(c);
24112455 close(l);
24122456 _ := 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;
24162463 if open("/tmp/raz-sys-fifo", {writing, nonblocking}) # -6 then exit(2); end; (* ENXIO: no reader, and it didn't wait *)
24172464 a := open("/tmp/raz-sys-fifo", {reading, nonblocking});
24182465 b := open("/tmp/raz-sys-fifo", {writing, nonblocking});

tools/cabi.c (history) modified +2 -0

@@ -126,6 +126,7 @@
126126 READ("stDev", struct stat, st_dev); READ("stIno", struct stat, st_ino);
127127 READ("stNlink", struct stat, st_nlink); READ("stMode", struct stat, st_mode);
128128 READ("stSize", struct stat, st_size);
129+ READ("stUid", struct stat, st_uid); READ("stGid", struct stat, st_gid);
129130 READ("stMtime", struct stat, st_mtim.tv_sec);
130131 READ("stMtimeNs", struct stat, st_mtim.tv_nsec);
131132 K("sIfmt", S_IFMT); K("sIfdir", S_IFDIR); K("sIfreg", S_IFREG);
@@ -153,6 +154,7 @@
153154 K("solSocket", SOL_SOCKET); K("soReuseaddr", SO_REUSEADDR);
154155 K("soError", SO_ERROR); K("soPeercred", SO_PEERCRED); /* Linux's */
155156 K("shutWr", SHUT_WR); K("msgNosignal", MSG_NOSIGNAL);
157+ K("msgDontwait", MSG_DONTWAIT);
156158 K("msgCmsgCloexec", MSG_CMSG_CLOEXEC); K("scmRights", SCM_RIGHTS);
157159 K("sockaddrLen", sizeof(struct sockaddr_storage));
158160 READ("saFamily", struct sockaddr, sa_family);