[Libwebsockets] how to get listen fd?

Andy Green andy at warmcat.com
Sat Jan 9 20:08:36 CET 2016



On January 10, 2016 1:39:04 AM GMT+08:00, Peter Pentchev <roam at ringlet.net> wrote:
>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

This is an uncommon fd action...

>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 :)

If I get some code, which api we're discussing will become unambiguous and I'll integrate it.

-Andy

>> 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?
>
>G'luck,
>Peter




More information about the Libwebsockets mailing list