raz / commit 8319dbc3

commit
8319dbc37b1769a502bc3893b5cc4faa0449ccc4
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
8b5a222b
Sys: files passed over a socket, memory shared through a file

sendFds and receiveFds (sendmsg/recvmsg, SCM_RIGHTS, at most 16 fds,
the ones past room closed); memFile (memfd_create) and mapFile
(MAP_SHARED), what MIT-SHM's fd form and Wayland take. Both Sys files;
the test maps a file, passes it, and sees a write through both maps.

patch · log

3 files changed, +227 -2

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

@@ -46,6 +46,10 @@
4646 extern proc getsockname*(fd: I32; addr: ^U8; len: ^U32): I32;
4747 extern proc recvfrom*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: ^U32): INT;
4848 extern proc sendto*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: U32): INT;
49+extern proc sendmsg*(fd: I32; msg: ^U8; flags: I32): INT;
50+extern proc recvmsg*(fd: I32; msg: ^U8; flags: I32): INT;
51+extern proc memfd_create*(name: ^U8; flags: U32): I32;
52+extern proc ftruncate*(fd: I32; size: I64): I32;
4953 extern proc inet_pton*(family: I32; src, dst: ^U8): I32;
5054 extern proc clock_gettime*(clock: I32; t: ^U8): I32;
5155 extern proc clock_nanosleep*(clock, flags: I32; t, left: ^U8): I32;

lib/Sys.linux-x86_64.raz (history) modified +109 -1

@@ -79,6 +79,23 @@
7979 a: Sockaddr;
8080 size: INT;
8181 end;
82+ (* struct msghdr, what sendmsg and recvmsg take: no address, one
83+ buffer, and a control message; the sizes and flags are ints
84+ with room after them, kept zero *)
85+ Msghdr = record
86+ name: ^U8; nameSize: INT;
87+ iov: ^Iovec; iovLen: INT;
88+ control: ^U8; controlSize: INT;
89+ flags: INT;
90+ end;
91+ Iovec = record base: ^U8; size: INT; end;
92+ (* struct cmsghdr, SCM_RIGHTS's: its size, counted to the last fd,
93+ then the fds, as many as sendFds sends *)
94+ Rights = record
95+ size: INT;
96+ level, kind: I32;
97+ fds: [16]I32;
98+ end;
8299 Timespec = record sec, nsec: I64; end;
83100
84101 var
@@ -181,6 +198,30 @@
181198 _ := SYSCALL(11, p, size);
182199 end;
183200
201+(* A file in memory, with no name, of size zeroed bytes: its fd, or
202+ minus an errno. It is closed in a program exec starts. *)
203+proc memFile*(size: INT): INT;
204+var name: [4]U8; fd, r: INT;
205+begin
206+ _ := cstr("raz", name); (* the name /proc shows *)
207+ fd := SYSCALL(319, ADR(name[0]), 1); (* memfd_create, MFD_CLOEXEC *)
208+ if fd < 0 then return fd; end;
209+ r := SYSCALL(77, fd, size); (* ftruncate *)
210+ if r < 0 then close(fd); return r; end;
211+ return fd;
212+end;
213+
214+(* The first size bytes of the file fd, mapped to be read and written,
215+ and shared: what this program writes there, every program that maps
216+ the file sees. nil if it can't be; munmap gives them back. *)
217+proc mapFile*(fd, size: INT): ^U8;
218+var p: INT;
219+begin
220+ p := SYSCALL(9, 0, size, 3, 1, fd, 0); (* mmap: read, write; MAP_SHARED *)
221+ if p < 0 then return nil; end;
222+ return ^U8(p);
223+end;
224+
184225 (* Whether the file descriptor fd is a terminal: whether it has a
185226 terminal's settings to read (ioctl TCGETS). *)
186227 proc isTerminal*(fd: INT): BOOL;
@@ -709,6 +750,49 @@
709750 return SYSCALL(44, fd, bytes(s), LEN(s), 4000H, ADR(peer.a), peer.size); (* sendto, MSG_NOSIGNAL *)
710751 end;
711752
753+(* Sends s, at least one byte, to the Unix socket fd, as send does, and
754+ with it the files fds, at most 16, which the program that receives
755+ them then has open too: the bytes sent, or minus an errno. *)
756+proc sendFds*(fd: INT; s: []U8; fds: []INT): INT;
757+var m: Msghdr; v: Iovec; c: Rights; i: INT;
758+begin
759+ if (LEN(s) = 0) or (LEN(fds) > LEN(c.fds)) then return -22; end;
760+ v := {base: ADR(s[0]), size: LEN(s)};
761+ m := {iov: ADR(v), iovLen: 1};
762+ if LEN(fds) > 0 then
763+ c := {size: 16 + 4 * LEN(fds), level: 1, kind: 1}; (* SOL_SOCKET, SCM_RIGHTS *)
764+ for i := 0 to LEN(fds) - 1 do c.fds[i] := I32(fds[i]); end;
765+ m.control := ^U8(ADR(c));
766+ m.controlSize := (c.size + 7) & ~7; (* CMSG_SPACE: whole quadwords *)
767+ end;
768+ return SYSCALL(46, fd, ADR(m), 4000H); (* sendmsg, MSG_NOSIGNAL *)
769+end;
770+
771+(* Reads into buf from the Unix socket fd, as read does, and puts the
772+ files sent with what it read in fds, as many as fit, closing the
773+ rest: the bytes read, or minus an errno, and the files put in fds.
774+ Each is closed in a program exec starts. *)
775+proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT);
776+var m: Msghdr; v: Iovec; c: Rights; n, k, i: INT;
777+begin
778+ v := {base: bytes(buf), size: LEN(buf)};
779+ m := {iov: ADR(v), iovLen: 1, control: ^U8(ADR(c)), controlSize: SIZE(Rights)};
780+ n := SYSCALL(47, fd, ADR(m), 40000000H); (* recvmsg, MSG_CMSG_CLOEXEC *)
781+ if n < 0 then return n, 0; end;
782+ k := 0;
783+ if (m.controlSize >= 16) and (c.level = 1) and (c.kind = 1) then
784+ for i := 0 to (c.size - 16) div 4 - 1 do
785+ if k < LEN(fds) then
786+ fds[k] := INT(c.fds[i]);
787+ INC(k);
788+ else
789+ close(INT(c.fds[i]));
790+ end;
791+ end;
792+ end;
793+ return n, k;
794+end;
795+
712796 (* The value of the environment variable name; "" if it isn't set. The
713797 environment follows the arguments and their nil. *)
714798 proc env*(name: []U8): []U8;
@@ -1158,7 +1242,7 @@
11581242 directory made, listed and emptied; no terminal; a socket, sent to
11591243 after its end; programs; time and the network. *)
11601244 proc main*;
1161-var free, total: I64; fd, n, err, w, h, l, c, a: INT; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
1245+var free, total: I64; fd, n, err, w, h, l, c, a, m, k: INT; got: [1]INT; buf: [16]U8; p, q: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
11621246 begin
11631247 fd := create("/tmp/raz-sys-test");
11641248 if fd < 0 then exit(2); end;
@@ -1257,6 +1341,30 @@
12571341 close(a);
12581342 if (readyOf([c], 1000) # c) or (read(c, buf) # 0) then exit(2); end; (* its end *)
12591343 if send(c, "x") # -32 then exit(2); end; (* EPIPE, and no SIGPIPE *)
1344+ close(c);
1345+ close(l);
1346+ _ := remove("/tmp/raz-sys-socket");
1347+ (* a file in memory, mapped, and passed with a byte down a socket
1348+ twice, with room for one: what the other side maps is the same *)
1349+ m := memFile(8192);
1350+ p := mapFile(m, 8192);
1351+ if (m < 0) or (p = nil) or (p[8191] # 0) then exit(2); end;
1352+ p[100] := 42;
1353+ l := listen("/tmp/raz-sys-socket");
1354+ c := connect("/tmp/raz-sys-socket");
1355+ a := accept(l);
1356+ 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;
1357+ n, k := receiveFds(a, buf, got);
1358+ if (n # 1) or (k # 1) or (buf[0] # 'f') then exit(2); end;
1359+ q := mapFile(got[0], 8192);
1360+ if (q = nil) or (q[100] # 42) then exit(2); end;
1361+ q[200] := 7;
1362+ if p[200] # 7 then exit(2); end;
1363+ munmap(p, 8192);
1364+ munmap(q, 8192);
1365+ close(got[0]);
1366+ close(m);
1367+ close(a);
12601368 close(c);
12611369 close(l);
12621370 _ := remove("/tmp/raz-sys-socket");

lib/Sys.raz (history) modified +114 -1

@@ -94,6 +94,23 @@
9494 a: Sockaddr;
9595 size: INT;
9696 end;
97+ (* struct msghdr, what sendmsg and recvmsg take: no address, one
98+ buffer, and a control message; the sizes and flags are ints
99+ with room after them, kept zero *)
100+ Msghdr = record
101+ name: ^U8; nameSize: INT;
102+ iov: ^Iovec; iovLen: INT;
103+ control: ^U8; controlSize: INT;
104+ flags: INT;
105+ end;
106+ Iovec = record base: ^U8; size: INT; end;
107+ (* struct cmsghdr, SCM_RIGHTS's: its size, counted to the last fd,
108+ then the fds, as many as sendFds sends *)
109+ Rights = record
110+ size: INT;
111+ level, kind: I32;
112+ fds: [16]I32;
113+ end;
97114 Timespec = record sec, nsec: I64; end;
98115
99116 var
@@ -214,6 +231,33 @@
214231 _ := C.munmap(p, UINT(size));
215232 end;
216233
234+(* A file in memory, with no name, of size zeroed bytes: its fd, or
235+ minus an errno. It is closed in a program exec starts. *)
236+proc memFile*(size: INT): INT;
237+var name: [4]U8; fd: I32; err: INT;
238+begin
239+ _ := cstr("raz", name); (* the name /proc shows *)
240+ fd := C.memfd_create(ADR(name[0]), 1); (* MFD_CLOEXEC *)
241+ if fd < 0 then return failed(); end;
242+ if C.ftruncate(fd, I64(size)) # 0 then
243+ err := failed(); (* before close can change errno *)
244+ _ := C.close(fd);
245+ return err;
246+ end;
247+ return INT(fd);
248+end;
249+
250+(* The first size bytes of the file fd, mapped to be read and written,
251+ and shared: what this program writes there, every program that maps
252+ the file sees. nil if it can't be; munmap gives them back. *)
253+proc mapFile*(fd, size: INT): ^U8;
254+var p: ^U8;
255+begin
256+ p := C.mmap(nil, UINT(size), 3, 1, I32(fd), 0); (* read, write; MAP_SHARED *)
257+ if INT(p) = -1 then return nil; end;
258+ return p;
259+end;
260+
217261 (* Renames old to new, replacing what new names: 0, or minus an errno. *)
218262 proc rename*(old, new: []U8): INT;
219263 var a, b: [4096]U8;
@@ -666,6 +710,51 @@
666710 return r;
667711 end;
668712
713+(* Sends s, at least one byte, to the Unix socket fd, as send does, and
714+ with it the files fds, at most 16, which the program that receives
715+ them then has open too: the bytes sent, or minus an errno. *)
716+proc sendFds*(fd: INT; s: []U8; fds: []INT): INT;
717+var m: Msghdr; v: Iovec; c: Rights; i, r: INT;
718+begin
719+ if (LEN(s) = 0) or (LEN(fds) > LEN(c.fds)) then return -22; end;
720+ v := {base: ADR(s[0]), size: LEN(s)};
721+ m := {iov: ADR(v), iovLen: 1};
722+ if LEN(fds) > 0 then
723+ c := {size: 16 + 4 * LEN(fds), level: 1, kind: 1}; (* SOL_SOCKET, SCM_RIGHTS *)
724+ for i := 0 to LEN(fds) - 1 do c.fds[i] := I32(fds[i]); end;
725+ m.control := ^U8(ADR(c));
726+ m.controlSize := (c.size + 7) & ~7; (* CMSG_SPACE: whole quadwords *)
727+ end;
728+ r := C.sendmsg(I32(fd), ^U8(ADR(m)), 4000H); (* MSG_NOSIGNAL *)
729+ if r < 0 then return failed(); end;
730+ return r;
731+end;
732+
733+(* Reads into buf from the Unix socket fd, as read does, and puts the
734+ files sent with what it read in fds, as many as fit, closing the
735+ rest: the bytes read, or minus an errno, and the files put in fds.
736+ Each is closed in a program exec starts. *)
737+proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT);
738+var m: Msghdr; v: Iovec; c: Rights; n, k, i: INT;
739+begin
740+ v := {base: bytes(buf), size: LEN(buf)};
741+ m := {iov: ADR(v), iovLen: 1, control: ^U8(ADR(c)), controlSize: SIZE(Rights)};
742+ n := C.recvmsg(I32(fd), ^U8(ADR(m)), 40000000H); (* MSG_CMSG_CLOEXEC *)
743+ if n < 0 then return failed(), 0; end;
744+ k := 0;
745+ if (m.controlSize >= 16) and (c.level = 1) and (c.kind = 1) then
746+ for i := 0 to (c.size - 16) div 4 - 1 do
747+ if k < LEN(fds) then
748+ fds[k] := INT(c.fds[i]);
749+ INC(k);
750+ else
751+ _ := C.close(c.fds[i]);
752+ end;
753+ end;
754+ end;
755+ return n, k;
756+end;
757+
669758 (* The value of the environment variable name; "" if it isn't set. *)
670759 proc env*(name: []U8): []U8;
671760 var a: [4096]U8; s: ^U8; n: INT;
@@ -1128,7 +1217,7 @@
11281217 and closed; a directory made, listed and emptied; no terminal; a
11291218 socket, sent to after its end; programs; time and the network. *)
11301219 proc main*;
1131-var free, total: I64; fd, n, err, w, h, l, c, a: INT; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
1220+var free, total: I64; fd, n, err, w, h, l, c, a, m, k: INT; got: [1]INT; buf: [16]U8; p, q: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
11321221 begin
11331222 fd := create("/tmp/raz-sys-test");
11341223 if fd < 0 then exit(2); end;
@@ -1227,6 +1316,30 @@
12271316 close(a);
12281317 if (readyOf([c], 1000) # c) or (read(c, buf) # 0) then exit(2); end; (* its end *)
12291318 if send(c, "x") # -32 then exit(2); end; (* EPIPE, and no SIGPIPE *)
1319+ close(c);
1320+ close(l);
1321+ _ := remove("/tmp/raz-sys-socket");
1322+ (* a file in memory, mapped, and passed with a byte down a socket
1323+ twice, with room for one: what the other side maps is the same *)
1324+ m := memFile(8192);
1325+ p := mapFile(m, 8192);
1326+ if (m < 0) or (p = nil) or (p[8191] # 0) then exit(2); end;
1327+ p[100] := 42;
1328+ l := listen("/tmp/raz-sys-socket");
1329+ c := connect("/tmp/raz-sys-socket");
1330+ a := accept(l);
1331+ 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;
1332+ n, k := receiveFds(a, buf, got);
1333+ if (n # 1) or (k # 1) or (buf[0] # 'f') then exit(2); end;
1334+ q := mapFile(got[0], 8192);
1335+ if (q = nil) or (q[100] # 42) then exit(2); end;
1336+ q[200] := 7;
1337+ if p[200] # 7 then exit(2); end;
1338+ munmap(p, 8192);
1339+ munmap(q, 8192);
1340+ close(got[0]);
1341+ close(m);
1342+ close(a);
12301343 close(c);
12311344 close(l);
12321345 _ := remove("/tmp/raz-sys-socket");