h1990
h1990

Reputation: 165

Transfer ownership of unix domain sockets from one process to other

I have two processes A and B that use gRPC protocol over Unix domain sockets for remote procedural call. I want to replace process B with updated version of B' seamlessly. One of the requirement to do this is transfer the unix domain socket for gRPC from B to B'. I know some proxies like Envoy and HAproxy transfer network sockets seamlessly without losing connections. Can we do the same thing with Unix domain sockets?

Upvotes: 1

Views: 757

Answers (1)

Marco Bonelli
Marco Bonelli

Reputation: 69346

Yes, this is technically possible. In Linux you can transfer any file descriptor (including one referring to a open Unix domain socket) from one process to another by using a Unix domain socket (see man 7 unix under "Ancillary messages"). So yeah, I would say that what you ask for is perfectly doable.

The real question is how you plan on doing it, as it could get complicated depending on your specific scenario. It's simple enough to do in a low-level language like C:

  1. Create and Unix domain socket on B (or B') and bind it to a path.
  2. On the other process, connect to the Unix domain socket via the known path.
  3. Send any fd you want from B to B' (or vice-versa) using sendmsg with an appropriately built msghdr structure. In this case you would send the fd of your other Unix socket that you wish to "transfer".
  4. After sending (even before receiving) you can close the socket, it will remain open for the receiver.
  5. Receive the fd on the other end with recvmsg.

See Sending file descriptor by Linux socket for a working example in C.

As per other programming languages, I suppose you will have to get your hands dirty digging through the documentation to find out the corresponding functions or syscall wrappers used to do the above.

Upvotes: 2

Related Questions