You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Demo go code showing how to pass a file descriptor between processesusing Unix Domain Sockets.
Since fd-passing is a fairly arcane part of Unix and perhaps an evenmore arcane part of go, I figure that others might benefit from havingaccess to a known working example. While there are plenty of availableexamples written in C, at the time of writing I could not readily finda working example in go. Thus this is that.
When might you use fd-passing?
There are a number of use-cases, but generally fd-passing is used whenyou want to centrally manage and control access to underlying systemresources in a way that is not readily possible with the controlsoffered by the operating system.
For example you might want to give clients access to sockets createdon privileged ports but only to some clients and only to someports. One way to do this is have a server establish the socket andfd-pass it back to the client if it passes the access-control rules.
Another example might be if you want to give clients access to somefiles in a directory but not others. Such as those under a certainsize or age. The client sends the open request to the server, theserver applies the age/size logic and fd-passes back the opened fileif it's is approved.
Another use-case is to create a server as a container of idle networkconnections. If your main server uses a lot of state per connectionand cannot easily be modified then a small modification to the mainserver might be to fd-pass idle sockets to the container server whichmonitors for activity and then fd-passes active sockets back to theheavy-state server.
To be fair, the number of use-cases are not large and some use-casesmight be implemented just as easily with fuse or similar. But when youdo have a use-case, now you have a guide to get you up and running.
How to use
Runmake
Run./server in one terminal
Run./client in another terminal
Type some lines of text into the./client terminal
Text should show up on the./server terminal
Runs on?
This demo is know to work on Linux, FreeBSD and macOS using go1.11.6and beyond.