system(3) && open file descriptors
freebsd-questions-local at be-well.ilk.org
Wed Apr 30 19:06:37 UTC 2014
Charles Swiger <cswiger at mac.com> writes:
> 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ó:
>>> 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
>> 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.
My reading of the POSIX spec leads me to say that it isn't allowed
to. This is probably an oversight on the part of the standards folks,
because there's little useful application for the child process to use
open file descriptors beyond stdin/out/error.
> 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.
One would think.
> 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.
I definitely wouldn't be comfortable with it if it violates the standard
(I'm not sufficiently confident in my reading of the relevant language
to flatly declare that it does). I *can* dream up scenarios that would
use such an effect, but I've never seen any in the wild; people do the
fork() and execl() by hand.
> So improving the manpage strikes me as a fine idea....
More information about the freebsd-questions