Can you transfer/recreate a unix domain socket file across filesystems?

As I understand it, a unix domain socket is a file (with its own FS inode) that internally points to a socket inode (namespaced internally by the kernel). This is evident by inspecting the file descriptors of the binding process, returning a value such as socket:[345678].

Typically, unix domain sockets can be mv/rm'd, etc. within the same filesystem, as their own file inodes do not change in such instances (as mv is just a hard link + unlink, conceptually).

However, if I wanted to move a socket file to a new filesystem, such that a new filesystem inode would have to be allocated to the socket file, I'd need to somehow register that new socket file with the underlying socket descriptor in the kernel.

Is there a way to do this, transparently and without affecting the running, bound server process?

Particularly interested in doing this in C, but conceptually understanding this is also helpful. It's probably worth mentioning that, I assume, mknod() will be involved, and that's OK in this case. I just have no idea how to go about invoking it correctly.

Upvotes: 4

Views: 1123

Answers (1)

At least under Linux, no.

Unix domain socket endpoints are identified internally via the unique tuple consisting of their st_dev value and st_ino value, as described in this libfuse ticket.

Even under a FUSE filesystem, the kernel enforces that st_dev always accurately reflects that of the underlying filesystem entry. Thus, unless the kernel were to be patched to allow faking of st_dev, or to edit the socket device/inode numbers, this is not possible - even with something like FUSE.

Further, starting in Linux kernel 5.3, SYS_pidfd_getfd could theoretically be used to clone a file descriptor from the target process to the current process (assuming you have PTRACE caps set), but you're stuck there as you cannot re-bind() the socket to a new location after the original bind was already created.

This is, quite simply, not possible.

Upvotes: 1

Related Questions