Skip to content

net/local: Name the FIFOs of a socket pair with a counter. - #22469

Open
casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:local-pair-name-counter-up
Open

casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:local-pair-name-counter-up

Conversation

@casaroli

@casaroli casaroli commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

socketpair() gives both ends an internal name, and the FIFOs of the pair get their names from it. The name was "socketpair" and a hexadecimal number that was different in each build and each run. The name shows in F_GETPATH and FIOC_FILEPATH (local:[name]), in the file list of /proc/<pid>/group/fd and in the FIFO names in CONFIG_NET_LOCAL_VFS_PATH, so these outputs were long and changed from run to run.

Now the name is "socketpair" and a decimal number from a counter (socketpair0, socketpair1, ...). The counter changes under the lock of the local sockets and keeps the name unique, as before. The names are short and the same in each run for the same order of socketpair() calls.

The second commit stops local_findconn() (the lookup by name of bind(), connect() and sendto() of a datagram socket) from finding the ends of socket pairs. A pair has no name for the program (getsockname() gives an unnamed address), but connect() of a datagram socket to the internal name succeeded, a sendto() found an end of the pair, and bind() of that name failed with EADDRINUSE. Now connect() and sendto() to the name fail with ENOENT, as for any name with no socket, and bind() of the name succeeds. local_peerconn() matches a pair end only with a pair end.

Impact

The internal name of a socket pair changes (the output of F_GETPATH, /proc/<pid>/group/fd and the FIFO names), and a lookup by name no longer finds a socket pair. No API, ABI or Kconfig change. The same in all build modes and on all architectures.

Testing

New cases in cmocka_net_local_dgram (apache/nuttx-apps#4175, need CONFIG_FS_IOCTL_FILEPATH): test_local_dgram_pair_path: F_GETPATH of both ends of a pair gives local:[socketpairN] with a decimal N, the same for both ends, and another N for the next pair. test_local_dgram_pair_noname: connect() and sendto() of a datagram socket to that name fail with ENOENT, bind() of the name succeeds, and the pair still works. On qemu-armv8a:nsh (flat), Arm GNU 13.2, QEMU TCG:

master this PR
test_local_dgram_pair_path FAILED (the number is not decimal) PASSED
test_local_dgram_pair_noname FAILED (the name check; with commit 1 only: the connect() returns 0) PASSED
the other cases of cmocka_net_local_dgram 91 PASSED 91 PASSED

Builds with -Werror in the CI image, with local stream and datagram sockets: qemu-armv8a:nsh (flat), qemu-armv8a:knsh (kernel, with the apps), rv-virt:netnsh64 (flat), rv-virt:knetnsh64 (kernel, with the apps).

socketpair() gives both ends an internal name, and the FIFOs of the pair
get their names from it.  The name was "socketpair" and a hexadecimal
number that was different in each build and each run.  The name shows
in F_GETPATH and FIOC_FILEPATH ("local:[name]"), in the file list of
/proc/<pid>/group/fd and in the FIFO names in CONFIG_NET_LOCAL_VFS_PATH.
So these outputs were long and changed from run to run.

Now the name is "socketpair" and a decimal number from a counter: the
first pair is socketpair0, the next socketpair1.  The counter keeps the
name unique, as before, and changes under the lock of the local
sockets.  The names are short and the same in each run for the same
order of socketpair() calls.

The behavior is the same in all build modes.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
local_findconn() finds a local socket by its name for bind(), connect()
and sendto() of a datagram socket.  It also compared the internal name
of a socket pair.  So connect() of a datagram socket to the name
"socketpairN" succeeded, a sendto() to it found an end of the pair, and
bind() of that name failed with EADDRINUSE.  A socket pair has no name
for the program: getsockname() gives an unnamed address.

Now local_findconn() skips the ends of socket pairs: a pair end is found
only through its pair.  connect() and sendto() to the name fail with
ENOENT, as for any name with no socket, and bind() of the name succeeds.
local_peerconn() matches a pair end only with a pair end, so a socket
bound to that name is never taken for the peer of a pair.

The behavior is the same in all build modes.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@github-actions github-actions Bot added Area: Networking Effects networking subsystem Size: S The size of the change in this PR is small labels Nov 27, 2026
@github-actions

Copy link
Copy Markdown

Comment thread net/local/local_sockif.c

static int local_socketpair(FAR struct socket *psocks[2])
{
static uint32_t g_next_pair_id;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to atomic_t

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Networking Effects networking subsystem Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants