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.
| @@ -712,7 +712,9 @@ | ||
| 712 | 712 | or failed, counts as ready, since a read of it won't wait. It looks at |
| 713 | 713 | the first 64 of `fds`, and `connect` and `listen` take a path of at |
| 714 | 714 | 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. | |
| 716 | 718 | |
| 717 | 719 | `poll` is `readyFor` without its limits. Each `Poll` names a |
| 718 | 720 | descriptor `fd` and whether to wait to `read` from it, to `write` to |
| @@ -830,30 +830,36 @@ | ||
| 830 | 830 | return fd; |
| 831 | 831 | end; |
| 832 | 832 | |
| 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; | |
| 835 | 838 | begin |
| 836 | - if LEN(path) >= LEN(a^.path) then return false; end; | |
| 839 | + if LEN(path) >= LEN(a^.path) then return -36; end; | |
| 837 | 840 | a^.family := 1; (* AF_UNIX *) |
| 838 | 841 | 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); | |
| 840 | 844 | end; |
| 841 | 845 | |
| 842 | 846 | (* A stream socket connected to the Unix socket at path. *) |
| 843 | 847 | proc connect*(path: []U8): INT; |
| 844 | -var a: SockaddrUn; | |
| 848 | +var a: SockaddrUn; size: INT; | |
| 845 | 849 | 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 *) | |
| 848 | 853 | end; |
| 849 | 854 | |
| 850 | 855 | (* A stream socket made at path, which must not be there yet, taking |
| 851 | 856 | connections. *) |
| 852 | 857 | proc listen*(path: []U8): INT; |
| 853 | -var a: SockaddrUn; | |
| 858 | +var a: SockaddrUn; size: INT; | |
| 854 | 859 | 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); | |
| 857 | 863 | end; |
| 858 | 864 | |
| 859 | 865 | (* The value of the hexadecimal digit c, or -1 if it isn't one. *) |
| @@ -2591,9 +2597,10 @@ | ||
| 2591 | 2597 | |
| 2592 | 2598 | (* poll over 100 socket pairs with a byte sent on three, so that all |
| 2593 | 2599 | 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. *) | |
| 2595 | 2602 | 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; | |
| 2597 | 2604 | begin |
| 2598 | 2605 | for i := 0 to 99 do |
| 2599 | 2606 | x, y, r := socketPair(); |
| @@ -2620,6 +2627,16 @@ | ||
| 2620 | 2627 | close(ends[i]); |
| 2621 | 2628 | if i # 5 then close(a[i].fd); end; |
| 2622 | 2629 | 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); | |
| 2623 | 2640 | end; |
| 2624 | 2641 | |
| 2625 | 2642 | (* The module's test: a file written, read back and closed; a |
| @@ -921,32 +921,38 @@ | ||
| 921 | 921 | return INT(field(p, CAbi.saFamilyAt, CAbi.saFamilyLen)); |
| 922 | 922 | end; |
| 923 | 923 | |
| 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; | |
| 926 | 929 | var p: ^U8; |
| 927 | 930 | begin |
| 928 | - if LEN(path) >= CAbi.sunPathLen then return false; end; | |
| 931 | + if LEN(path) >= CAbi.sunPathLen then return -36; end; | |
| 929 | 932 | p := ^U8(a); |
| 930 | 933 | setFamily(p, CAbi.afUnix, CAbi.sockaddrUnLen); |
| 931 | 934 | 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; | |
| 933 | 937 | end; |
| 934 | 938 | |
| 935 | 939 | (* A stream socket connected to the Unix socket at path. *) |
| 936 | 940 | proc connect*(path: []U8): INT; |
| 937 | -var a: Sockaddr; | |
| 941 | +var a: Sockaddr; size: INT; | |
| 938 | 942 | 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); | |
| 941 | 946 | end; |
| 942 | 947 | |
| 943 | 948 | (* A stream socket made at path, which must not be there yet, taking |
| 944 | 949 | connections. *) |
| 945 | 950 | proc listen*(path: []U8): INT; |
| 946 | -var a: Sockaddr; | |
| 951 | +var a: Sockaddr; size: INT; | |
| 947 | 952 | 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); | |
| 950 | 956 | end; |
| 951 | 957 | |
| 952 | 958 | (* addr, an IPv4 or IPv6 address as numbers, and port, in a: the size |
| @@ -2545,9 +2551,10 @@ | ||
| 2545 | 2551 | |
| 2546 | 2552 | (* poll over 100 socket pairs with a byte sent on three, so that all |
| 2547 | 2553 | 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. *) | |
| 2549 | 2556 | 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; | |
| 2551 | 2558 | begin |
| 2552 | 2559 | for i := 0 to 99 do |
| 2553 | 2560 | x, y, r := socketPair(); |
| @@ -2574,6 +2581,16 @@ | ||
| 2574 | 2581 | close(ends[i]); |
| 2575 | 2582 | if i # 5 then close(a[i].fd); end; |
| 2576 | 2583 | 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); | |
| 2577 | 2594 | end; |
| 2578 | 2595 | |
| 2579 | 2596 | (* The module's test, as Linux's Sys has it: a file written, read back |