Creating IP/TCP/*Conn from fd and socket activated services

1,237 views
Skip to first unread message

Niklas Schnelle

unread,
Jan 25, 2013, 2:38:55 PM1/25/13
to golan...@googlegroups.com
Hi fellow Gophers,

when reading http://0pointer.de/blog/projects/inetd.html I began to wonder how hard it would be to
support socket activation in a Go server. I couldn't find anything in the net package on how to create a XXConn from an
fd though. Is this supported? It would be kind of cool to have systemd listen on port 80 and just passing connected sockets to a Go server
especially since this also solves the problem of listening on privileged ports.

Greetings Nik

minux

unread,
Jan 25, 2013, 6:57:03 PM1/25/13
to Niklas Schnelle, golan...@googlegroups.com
first convert the fd to *os.File via os.NewFile,
and then convert the *os.File to a net.Conn via net.FileConn.

Albert Strasheim

unread,
Jan 26, 2013, 2:25:33 AM1/26/13
to golan...@googlegroups.com, Niklas Schnelle
Hello
I contributed the FileConn patch long ago to be able to do exactly this.

Enjoy!

Regards

Albert

Albert Strasheim

unread,
Jan 26, 2013, 2:29:32 AM1/26/13
to golan...@googlegroups.com, Niklas Schnelle
Hello again


On Saturday, January 26, 2013 1:57:03 AM UTC+2, minux wrote:
On Sat, Jan 26, 2013 at 3:38 AM, Niklas Schnelle <niklas....@gmail.com> wrote:
when reading http://0pointer.de/blog/projects/inetd.html I began to wonder how hard it would be to
support socket activation in a Go server. I couldn't find anything in the net package on how to create a XXConn from an
fd though. Is this supported? It would be kind of cool to have systemd listen on port 80 and just passing connected sockets to a Go server
especially since this also solves the problem of listening on privileged ports.
first convert the fd to *os.File via os.NewFile,
and then convert the *os.File to a net.Conn via net.FileConn.

You might also find this snippet useful:


Regards

Albert

al...@polvi.net

unread,
Jun 24, 2013, 2:29:32 PM6/24/13
to golan...@googlegroups.com, Niklas Schnelle
Here is a complete example using that snippet.


-Alex

Dave Cheney

unread,
Jun 24, 2013, 8:07:11 PM6/24/13
to al...@polvi.net, golang-nuts, Niklas Schnelle
Very nice. You don't need to play with O_CLOEXEC, net.FileListener
will ensure the fd is in the correct state.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Naitik Shah

unread,
Jun 24, 2013, 8:35:27 PM6/24/13
to Dave Cheney, al...@polvi.net, golang-nuts, Niklas Schnelle
I've also built a library that makes it possible to optionally use
systemd socket activation:

https://github.com/daaku/go.grace/blob/master/grace.go#L144

(Though as I re-read the code I realized the TCPListener assumption is
unnecessary and it should just be any FileListener).


-Naitik
--
-Naitik

Albert Strasheim

unread,
Jun 25, 2013, 1:49:33 AM6/25/13
to al...@polvi.net, golan...@googlegroups.com
Hello
mattharden made a good point which made me realise that the original
snippet isn't quite doing what sd_listen_fds is doing.

Looking at the code again:

http://cgit.freedesktop.org/systemd/systemd/plain/src/libsystemd-daemon/sd-daemon.c

sd_listen_fds is checking the CLOEXEC flag and if it's set, doesn't
set it again. However, all the file descriptors are still "returned"
to the caller.

I mistakenly made the snippet skip file descriptors without CLOEXEC
set. Not enough coffee that day.

Aside: it might be nice to write a sd_notify in Go too. Check out the
WATCHDOG=1 section here:

http://www.freedesktop.org/software/systemd/man/sd_notify.html

Regards

Albert
Reply all
Reply to author
Forward
0 new messages