bin/113860: sh: shell is still running when using `sh -c'

Oliver Fromme olli at
Tue Jun 19 18:29:43 UTC 2007

Ed Schouten wrote:
 > Some bloated shell from GNU immediately shuts down the shell when using
 > `sh -c'. This means that:
 > sh -c 'sleep 10'
 > only keeps a sleep(1) process running for 10 seconds; the shell is
 > closed immediately.

That's only possible for simple cases, i.e. when a single
external command is used.  In all other cases, the shell
process has to be kept, e.g.:

sh -c 'sleep 10; echo foobar'

In that case the shell process has to stay and wait for the
command to finish.

 > FreeBSD's sh doesn't. This means that all applications that use
 > system(3) or just execve(2) /bin/sh leave shells running around.

Which is not a big deal.  Those shell processes are quite
cheap.  Most of the RSS that you see in top is shared
between processes, so the RAM consumption is negligible.

If you want to avoid the shell process anyway, a simple
workaround is to preped "exec":

sh -c 'exec sleep 10'

In that case the shell will exec() the program directly,
instead of fork() and waiting for it to finish.  Actually
that's exactly what that "bloated shell from GNU" does
automatically (prepending "exec" internally).

Implementing that automatic behaviour in FreeBSD's /bin/sh
is not trivial, because you have to find out first if it
is possible for the given command line or not.

Apart from that I'm not sure if it is desirable to have
such a "feature".  Personally I believe that the shell
should do what I tell it to do.  That is, when I want it
to use "exec", I write "exec".  If I don't want it, I
don't write it.  I would be somewhat surprised if it used
"exec" when I don't instruct it to do so.  It might be
regarded as a POLA violation, but it's probably a matter
of opinion.

Best regards

Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:

$ dd if=/dev/urandom count=1
$ file perl script text executable

More information about the freebsd-bugs mailing list