1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-22 19:06:10 +02:00
git/common-main.c
Ævar Arnfjörð Bjarmason 19d75948ef common-main.c: move non-trace2 exit() behavior out of trace2.c
Change the exit() wrapper added in ee4512ed48 (trace2: create new
combined trace facility, 2019-02-22) so that we'll split up the trace2
logging concerns from wanting to wrap the "exit()" function itself for
other purposes.

This makes more sense structurally, as we won't seem to conflate
non-trace2 behavior with the trace2 code. I'd previously added an
explanation for this in 368b584315 (common-main.c: call exit(), don't
return, 2021-12-07), that comment is being adjusted here.

Now the only thing we'll do if we're not using trace2 is to truncate
the "code" argument to the lowest 8 bits.

We only need to do that truncation on non-POSIX systems, but in
ee4512ed48 that "if defined(__MINGW32__)" code added in
47e3de0e79 (MinGW: truncate exit()'s argument to lowest 8 bits,
2009-07-05) was made to run everywhere. It might be good for clarify
to narrow that down by an "ifdef" again, but I'm not certain that in
the interim we haven't had some other non-POSIX systems rely the
behavior. On a POSIX system taking the lowest 8 bits is implicit, see
exit(3)[1] and wait(2)[2]. Let's leave a comment about that instead.

1. https://man7.org/linux/man-pages/man3/exit.3.html
2. https://man7.org/linux/man-pages/man2/wait.2.html

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-06-02 12:51:30 -07:00

77 lines
1.9 KiB
C

#include "cache.h"
#include "exec-cmd.h"
#include "attr.h"
/*
* Many parts of Git have subprograms communicate via pipe, expect the
* upstream of a pipe to die with SIGPIPE when the downstream of a
* pipe does not need to read all that is written. Some third-party
* programs that ignore or block SIGPIPE for their own reason forget
* to restore SIGPIPE handling to the default before spawning Git and
* break this carefully orchestrated machinery.
*
* Restore the way SIGPIPE is handled to default, which is what we
* expect.
*/
static void restore_sigpipe_to_default(void)
{
sigset_t unblock;
sigemptyset(&unblock);
sigaddset(&unblock, SIGPIPE);
sigprocmask(SIG_UNBLOCK, &unblock, NULL);
signal(SIGPIPE, SIG_DFL);
}
int main(int argc, const char **argv)
{
int result;
struct strbuf tmp = STRBUF_INIT;
trace2_initialize_clock();
/*
* Always open file descriptors 0/1/2 to avoid clobbering files
* in die(). It also avoids messing up when the pipes are dup'ed
* onto stdin/stdout/stderr in the child processes we spawn.
*/
sanitize_stdfds();
restore_sigpipe_to_default();
git_resolve_executable_dir(argv[0]);
git_setup_gettext();
initialize_the_repository();
attr_start();
trace2_initialize();
trace2_cmd_start(argv);
trace2_collect_process_info(TRACE2_PROCESS_INFO_STARTUP);
if (!strbuf_getcwd(&tmp))
tmp_original_cwd = strbuf_detach(&tmp, NULL);
result = cmd_main(argc, argv);
/* Not exit(3), but a wrapper calling our common_exit() */
exit(result);
}
/* We wrap exit() to call common_exit() in git-compat-util.h */
int common_exit(const char *file, int line, int code)
{
/*
* For non-POSIX systems: Take the lowest 8 bits of the "code"
* to e.g. turn -1 into 255. On a POSIX system this is
* redundant, see exit(3) and wait(2), but as it doesn't harm
* anything there we don't need to guard this with an "ifdef".
*/
code &= 0xff;
trace2_cmd_exit_fl(file, line, code);
return code;
}