mirror of
https://github.com/git/git.git
synced 2026-03-01 18:24:00 +01:00
run-command: add close_fd_above_stderr option
Add a new option to struct child_process that closes file descriptors 3 and above in the child after forking but before exec. This prevents long-running child processes from inheriting pipe endpoints or other descriptors from the parent environment. The upper bound for the fd scan comes from sysconf(_SC_OPEN_MAX), capped at 4096 to avoid excessive iteration when the limit is set very high. Signed-off-by: Paul Tarjan <github@paulisageek.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
5020fb7b90
commit
cd26f3d67d
@@ -832,6 +832,17 @@ fail_pipe:
|
||||
child_close(cmd->out);
|
||||
}
|
||||
|
||||
if (cmd->close_fd_above_stderr) {
|
||||
long max_fd = sysconf(_SC_OPEN_MAX);
|
||||
int fd;
|
||||
if (max_fd < 0 || max_fd > 4096)
|
||||
max_fd = 4096;
|
||||
for (fd = 3; fd < max_fd; fd++) {
|
||||
if (fd != child_notifier)
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
if (cmd->dir && chdir(cmd->dir))
|
||||
child_die(CHILD_ERR_CHDIR);
|
||||
|
||||
|
||||
@@ -141,6 +141,15 @@ struct child_process {
|
||||
unsigned stdout_to_stderr:1;
|
||||
unsigned clean_on_exit:1;
|
||||
unsigned wait_after_clean:1;
|
||||
|
||||
/**
|
||||
* Close file descriptors 3 and above in the child after forking
|
||||
* but before exec. This prevents the long-running child from
|
||||
* inheriting pipe endpoints or other descriptors from the parent
|
||||
* environment (e.g., the test harness).
|
||||
*/
|
||||
unsigned close_fd_above_stderr:1;
|
||||
|
||||
void (*clean_on_exit_handler)(struct child_process *process);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user