I wanted to write a blog post but got side-tracked. In the meantime, here is something I learned:
Suppose you have a script for which an fd is a unix socket. How do you call getpeername(2) on this socket from the cli to get the remotely bound socket?
On Debian, there is a package called tcputils which has a getpeername binary. It only works on INET sockets though. The netpipes Debian package also used to have a getpeername binary (23 years ago); but now it is in the getsockname binary to prevent conflicting with the previous package. But getsockname(2) is not the call I want, I already know the local socket name. You can then use exec -a getpeername getsockname to get the behaviour you want. But unfortunately netpipes' getpeername will not print the content of a peer name which is an abstract socket, it will print (abstract) instead.
So, back to square one. By now I could already have written a simple C program (many times over) to do what I want and called it a day. But I like weird constraints, so here I am. Is there any interpreter on a default Debian install that could let me do what I want. Tough luck: there is no python. But there is perl, so this is how you'd do it in Perl
#!/usr/bin/env perl
use IO::Socket::UNIX;
my $sock = IO::Socket::UNIX->new_from_fd(0, "r+");
print $sock->peerpath()
And how do you test it works? socat has a convenient bind= option on which you can pass an abstract unix socket name if it start with @. Launching the program with a unix socket as stdin is left as an exercise to the reader.
The blogpost was supposed to be shorter than this post, but we'll see, if it materializes.




