[Libwebsockets] how to get listen fd?

Peter Pentchev roam at ringlet.net
Sat Jan 9 18:39:04 CET 2016

On Sat, Jan 09, 2016 at 09:44:24PM +0800, Andy Green wrote:
> On January 9, 2016 9:26:51 PM GMT+08:00, Peter Pentchev <roam at ringlet.net> wrote:
> >On Sat, Jan 09, 2016 at 03:46:36PM +0800, Andy Green wrote:
> >> On January 9, 2016 3:37:18 PM GMT+08:00, Ash 20001
> ><ash20001 at hotmail.com> wrote:
> >> >I have looked at the source code in server.c to see when the
> >listening
> >> >fd is created. I would like to gain access to this fd to set a
> >> >different sock option on it (close on exec flag) using fcntl.
> >However I
> >> >can't seem to get this fd from a public API.
> >libwebsocket_get_socket_fd
> >> >only seems to work on wsi and wsi pointer is not provided in most
> >> >callbacks.
> >> >Any ideas?
> >> 
> >> I'd be willing to either expose it or add a context creation flag to
> >perform the operation, but from a quick google you can only attach that
> >attribute to files, at open() time.
> >
> >Hmm, if you mean the O_CLOEXEC flag, it's actually a relatively recent
> >addition; the historical (though non-atomic) way to mark a file for
> >closing upon exec() is, as Ash mentioned, a fcntl(F_GETFD) /
> >fcntl(F_SETFD)
> >sequence, or-ing the file descriptor flags with FD_CLOEXEC in between;
> >see e.g. https://codesearch.debian.net/results/FD_CLOEXEC/page_9 for
> >some
> >examples (although most of the hits are glibc internal definitions).
> I see... but these hits are all on file descriptors... do you know if it
> works the same on sockets, as he's trying to do?

Yes, sockets are file descriptors in the end, so common fd actions do
work on them.  As a matter of fact, the same sequence, but with F_GETFL /
F_SETFL and the O_NONBLOCK flag is the canonical way to put a socket into
non-blocking mode... just as you do it in libwebsockets, it seems :)

> Or Ash if you are already satisfied it works, can you send a diff with
> it just hacked into context create as you tested it?


Peter Pentchev  roam at ringlet.net roam at FreeBSD.org pp at storpool.com
PGP key:        http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint 2EE7 A7A5 17FC 124C F115  C354 651E EFB0 2527 DF13
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20160109/dad1237e/attachment.sig>

More information about the Libwebsockets mailing list