system(3) && open file descriptors

Charles Swiger cswiger at mac.com
Wed Apr 30 17:49:52 UTC 2014


On Apr 29, 2014, at 9:41 PM, Matthias Apitz <guru at unixarea.de> wrote:
> El día Tuesday, April 29, 2014 a las 02:41:22PM -0700, Charles Swiger escribió:
>> Hi--
>> 
>> On Apr 29, 2014, at 11:43 AM, Matthias Apitz <guru at unixarea.de> wrote:
>>> It seems that the proc started by the C library call system(3), as
>>> /bin/sh -c string
>>> owns the same set of open file descriptors as its calling proc. Is this
>>> somewhere documented as a feature? 'man system" does not say anything
>>> about, while 'man fork' does.
>> 
>> At least my version of system(3) says that it invokes fork(2) and checks
>> the exit status of the shell via waitpid(2).  That plus listing fork(2)
>> in SEE ALSO section seems to be enough of a pointer to the detailed
>> behavior....
> 
> Hi,
> Ofc it must use fork(2), but it *could* as well close all fd before
> execv(2). IMHO it should do this for all fd > 2, at least the man page
> should mention the fact that it does not.

Ah, I see your point.

Yes, I suspect that folks who intend to pass FDs to children would be more
disciplined about using FD_CLOEXEC and/or doing their own FD cleanup while
calling fork/exec directly.

Folks who call system() probably aren't expecting their FDs to be passed,
but I'm not sure it would be safe to change the current behavior by
closing FDs for them when it did not do so before.

So improving the manpage strikes me as a fine idea....

Regards,
-- 
-Chuck



More information about the freebsd-questions mailing list