• Jeff King's avatar
    common-main: call sanitize_stdfds() · 57f5d52a
    Jeff King authored
    This is setup that should be done in every program for
    safety, but we never got around to adding it everywhere (so
    builtins benefited from the call in git.c, but any external
    commands did not). Putting it in the common main() gives us
    this safety everywhere.
    Note that the case in daemon.c is a little funny. We wait
    until we know whether we want to daemonize, and then either:
     - call daemonize(), which will close stdio and reopen it to
       /dev/null under the hood
     - sanitize_stdfds(), to fix up any odd cases
    But that is way too late; the point of sanitizing is to give
    us reliable descriptors on 0/1/2, and we will already have
    executed code, possibly called die(), etc. The sanitizing
    should be the very first thing that happens.
    With this patch, git-daemon will sanitize first, and can
    remove the call in the non-daemonize case. It does mean that
    daemonize() may just end up closing the descriptors we
    opened, but that's not a big deal (it's not wrong to do so,
    nor is it really less optimal than the case where our parent
    process redirected us from /dev/null ahead of time).
    Signed-off-by: default avatarJeff King <peff@peff.net>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
common-main.c 510 Bytes