Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Linux SOCK_CLOEXEC behaviour

505 views
Skip to first unread message

sp...@potato.field

unread,
Jul 13, 2017, 7:17:31 AM7/13/17
to
This flag when used with the socket() function is supposed to close all open
sockets when an exec() function is called. However it seems to do it when only
fork() is called. Is this correct behaviour? Kernel version is 2.6.32.

--
Spud

Rainer Weikusat

unread,
Jul 13, 2017, 5:20:58 PM7/13/17
to
I didn't check 2.6.32 but in the 3.2.54 I have laying around here, the
effect of SOCK_CLOEXEC is exactly the same as fcntl(fd, F_SETFD,
FD_CLOEXEC) minus the possible race (another thread could invoke exec or
fork + exec before the thread creating the fd could set the CLOEXEC flag
on it).

Jens Thoms Toerring

unread,
Jul 13, 2017, 5:42:02 PM7/13/17
to
sp...@potato.field wrote:
> This flag when used with the socket() function is supposed to close all
> open sockets

Only the socket(s) for which it's set.

> when an exec() function is called. However it seems to do it when only
> fork() is called. Is this correct behaviour?

No, fork(2) is too early - you may call exec() afterwards, but
it shouldn't happen yet. There might be good reasons for keeping
it open in the newly spawned process (e.g. to communicate with
the parent process or if the parent spawned the new process for
minding what's happening on that socket and closes it itself).
As far as my understanding goes it should behave exactly as
O_CLOEXEC does when used with open(2) - and that definitely
closes the file only after an exec() and not on fork(2)

> Kernel version is 2.6.32.

That's a bit ancient now and short after it got introduced
(2.6.27 as far as I read), so perhaps there were stll some
issues back then (but it's a bit hard to believe since you
could set O_CLOEXEC on sockets before that, using fcntl(2),
just not at creation).

How did you come to the conclusion that the socket got already
closed after the fork()?
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

sp...@potato.field

unread,
Jul 14, 2017, 5:00:59 AM7/14/17
to
On 13 Jul 2017 21:41:56 GMT
j...@toerring.de (Jens Thoms Toerring) wrote:
>sp...@potato.field wrote:
>How did you come to the conclusion that the socket got already
>closed after the fork()?

Because in my code, after the fork (no exec) the parent process exits and the
TCP connection is immediately closed despite the child running.

--
Spud

sp...@potato.field

unread,
Jul 14, 2017, 5:24:59 AM7/14/17
to
Ignore my post, its not doing what I thought.

--
Spud


0 new messages