raz / commit 2c293e79

commit
2c293e797c3185235e02d7dc5d2f05131310be8b
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
c7550b66
Sys.connect and listen: a path that starts with a zero byte is an abstract name

Its address is 2 + LEN(path) long, with no zero after it.

patch · log

3 files changed, +61 -25

doc/raz-lib.md (history) modified +3 -1

@@ -712,7 +712,9 @@
712712 or failed, counts as ready, since a read of it won't wait. It looks at
713713 the first 64 of `fds`, and `connect` and `listen` take a path of at
714714 most 107 bytes, as `struct sockaddr_un` holds; a longer one is -36
715-(ENAMETOOLONG).
715+(ENAMETOOLONG). A path that starts with a zero byte is one of Linux's
716+abstract names (D-Bus's `unix:abstract=`), which has no file: the
717+address is as long as the name, with no zero after it.
716718
717719 `poll` is `readyFor` without its limits. Each `Poll` names a
718720 descriptor `fd` and whether to wait to `read` from it, to `write` to

lib/Sys.linux-x86_64.raz (history) modified +29 -12

@@ -830,30 +830,36 @@
830830 return fd;
831831 end;
832832
833-(* path as a Unix socket's address in a, or false if it is too long. *)
834-proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL;
833+(* path as a Unix socket's address in a, and the address's length, or
834+ -36 (ENAMETOOLONG) if it is too long. A path that starts with a zero
835+ byte is one of Linux's abstract names, which has no file and no
836+ trailing zero: its length ends with the name's. *)
837+proc unixAddr(path: []U8; a: ^SockaddrUn): INT;
835838 begin
836- if LEN(path) >= LEN(a^.path) then return false; end;
839+ if LEN(path) >= LEN(a^.path) then return -36; end;
837840 a^.family := 1; (* AF_UNIX *)
838841 COPY(a^.path, path);
839- return true;
842+ if (LEN(path) > 0) and (path[0] = 0) then return 2 + LEN(path); end;
843+ return SIZE(SockaddrUn);
840844 end;
841845
842846 (* A stream socket connected to the Unix socket at path. *)
843847 proc connect*(path: []U8): INT;
844-var a: SockaddrUn;
848+var a: SockaddrUn; size: INT;
845849 begin
846- if not unixAddr(path, ADR(a)) then return -36; end; (* ENAMETOOLONG *)
847- return socket(1, 1, ^U8(ADR(a)), SIZE(SockaddrUn), false); (* SOCK_STREAM *)
850+ size := unixAddr(path, ADR(a));
851+ if size < 0 then return size; end;
852+ return socket(1, 1, ^U8(ADR(a)), size, false); (* SOCK_STREAM *)
848853 end;
849854
850855 (* A stream socket made at path, which must not be there yet, taking
851856 connections. *)
852857 proc listen*(path: []U8): INT;
853-var a: SockaddrUn;
858+var a: SockaddrUn; size: INT;
854859 begin
855- if not unixAddr(path, ADR(a)) then return -36; end;
856- return socket(1, 1, ^U8(ADR(a)), SIZE(SockaddrUn), true);
860+ size := unixAddr(path, ADR(a));
861+ if size < 0 then return size; end;
862+ return socket(1, 1, ^U8(ADR(a)), size, true);
857863 end;
858864
859865 (* The value of the hexadecimal digit c, or -1 if it isn't one. *)
@@ -2591,9 +2597,10 @@
25912597
25922598 (* poll over 100 socket pairs with a byte sent on three, so that all
25932599 three are reported in one call, at their places; a timeout, 0; a
2594- closed descriptor, ready, and -9 from a read. *)
2600+ closed descriptor, ready, and -9 from a read; and an abstract Unix
2601+ socket, whose name starts with a zero byte. *)
25952602 proc testPoll;
2596-var a: [100]Poll; ends: [100]INT; ps: []Poll; buf: [4]U8; x, y, r, i: INT;
2603+var a: [100]Poll; ends: [100]INT; ps: []Poll; nm: [18]U8; buf: [4]U8; x, y, r, i, l, c, s: INT;
25972604 begin
25982605 for i := 0 to 99 do
25992606 x, y, r := socketPair();
@@ -2620,6 +2627,16 @@
26202627 close(ends[i]);
26212628 if i # 5 then close(a[i].fd); end;
26222629 end;
2630+ nm[0] := 0; (* an abstract name: a zero byte, then 17 *)
2631+ COPY(nm[1..18], "raz-abstract-test");
2632+ l := listen(nm);
2633+ c := connect(nm);
2634+ if (l < 0) or (c < 0) or (readyOf([c, l], 1000) # l) then exit(2); end;
2635+ s := accept(l);
2636+ if (s < 0) or (send(s, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end;
2637+ close(s);
2638+ close(c);
2639+ close(l);
26232640 end;
26242641
26252642 (* The module's test: a file written, read back and closed; a

lib/Sys.raz (history) modified +29 -12

@@ -921,32 +921,38 @@
921921 return INT(field(p, CAbi.saFamilyAt, CAbi.saFamilyLen));
922922 end;
923923
924-(* path as a Unix socket's address in a, or false if it is too long. *)
925-proc unixAddr(path: []U8; a: ^Sockaddr): BOOL;
924+(* path as a Unix socket's address in a, and the address's length, or
925+ -36 (ENAMETOOLONG) if it is too long. A path that starts with a zero
926+ byte is one of Linux's abstract names, which has no file and no
927+ trailing zero: its length ends with the name's. *)
928+proc unixAddr(path: []U8; a: ^Sockaddr): INT;
926929 var p: ^U8;
927930 begin
928- if LEN(path) >= CAbi.sunPathLen then return false; end;
931+ if LEN(path) >= CAbi.sunPathLen then return -36; end;
929932 p := ^U8(a);
930933 setFamily(p, CAbi.afUnix, CAbi.sockaddrUnLen);
931934 COPY(p[CAbi.sunPathAt..CAbi.sunPathAt + LEN(path)], path);
932- return true;
935+ if (LEN(path) > 0) and (path[0] = 0) then return CAbi.sunPathAt + LEN(path); end;
936+ return CAbi.sockaddrUnLen;
933937 end;
934938
935939 (* A stream socket connected to the Unix socket at path. *)
936940 proc connect*(path: []U8): INT;
937-var a: Sockaddr;
941+var a: Sockaddr; size: INT;
938942 begin
939- if not unixAddr(path, ADR(a)) then return -36; end; (* ENAMETOOLONG *)
940- return socket(CAbi.afUnix, CAbi.sockStream, ^U8(ADR(a)), CAbi.sockaddrUnLen, false);
943+ size := unixAddr(path, ADR(a));
944+ if size < 0 then return size; end;
945+ return socket(CAbi.afUnix, CAbi.sockStream, ^U8(ADR(a)), size, false);
941946 end;
942947
943948 (* A stream socket made at path, which must not be there yet, taking
944949 connections. *)
945950 proc listen*(path: []U8): INT;
946-var a: Sockaddr;
951+var a: Sockaddr; size: INT;
947952 begin
948- if not unixAddr(path, ADR(a)) then return -36; end;
949- return socket(CAbi.afUnix, CAbi.sockStream, ^U8(ADR(a)), CAbi.sockaddrUnLen, true);
953+ size := unixAddr(path, ADR(a));
954+ if size < 0 then return size; end;
955+ return socket(CAbi.afUnix, CAbi.sockStream, ^U8(ADR(a)), size, true);
950956 end;
951957
952958 (* addr, an IPv4 or IPv6 address as numbers, and port, in a: the size
@@ -2545,9 +2551,10 @@
25452551
25462552 (* poll over 100 socket pairs with a byte sent on three, so that all
25472553 three are reported in one call, at their places; a timeout, 0; a
2548- closed descriptor, ready, and -9 from a read. *)
2554+ closed descriptor, ready, and -9 from a read; and an abstract Unix
2555+ socket, whose name starts with a zero byte. *)
25492556 proc testPoll;
2550-var a: [100]Poll; ends: [100]INT; ps: []Poll; buf: [4]U8; x, y, r, i: INT;
2557+var a: [100]Poll; ends: [100]INT; ps: []Poll; nm: [18]U8; buf: [4]U8; x, y, r, i, l, c, s: INT;
25512558 begin
25522559 for i := 0 to 99 do
25532560 x, y, r := socketPair();
@@ -2574,6 +2581,16 @@
25742581 close(ends[i]);
25752582 if i # 5 then close(a[i].fd); end;
25762583 end;
2584+ nm[0] := 0; (* an abstract name: a zero byte, then 17 *)
2585+ COPY(nm[1..18], "raz-abstract-test");
2586+ l := listen(nm);
2587+ c := connect(nm);
2588+ if (l < 0) or (c < 0) or (readyOf([c, l], 1000) # l) then exit(2); end;
2589+ s := accept(l);
2590+ if (s < 0) or (send(s, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end;
2591+ close(s);
2592+ close(c);
2593+ close(l);
25772594 end;
25782595
25792596 (* The module's test, as Linux's Sys has it: a file written, read back