Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Demo code showing how to pass a file descriptor between processes using Unix Domain Sockets

License

NotificationsYou must be signed in to change notification settings

markdingo/go-fdpass-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  1. Runmake

  2. Run./server in one terminal

  3. Run./client in another terminal

  4. Type some lines of text into the./client terminal

  5. 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.

--

Search terms: go, golang, fd-passing, CMSG, Control Message, SCM_RIGHTS, recvmsg, sendmsg, Unix.

Copyright and License

go-fdpass-demo is Copyright ©️ 2020 Mark Delany. This software is licensed under the BSD 2-Clause "Simplified" License.

About

Demo code showing how to pass a file descriptor between processes using Unix Domain Sockets

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp