Re: [PATCH v2] fix start_command() bug when stdin is closed
From: Johannes Sixt <hidden>
Date: 2016-06-15 22:45:13
Paolo Bonzini schrieb:
There is a problem in the use of dup2+close in start_command() when one or more of file descriptors 0/1/2 are closed.
"Karl Chen pointed out a problem..." (just to give due credit).
int start_command(struct child_process *cmd)
{
int need_in, need_out, need_err;
int fdin[2], fdout[2], fderr[2];
/*
+ * Make sure that all file descriptors <= 2 are open, otherwise we
+ * mess them up when dup'ing pipes onto stdin/stdout/stderr. Since
+ * we are at it, open a file descriptor on /dev/null to use it later.
+ */
+ if (devnull_fd == -1)
+ {
+ devnull_fd = open("/dev/null", O_RDWR);
+ while (devnull_fd >= 0 && devnull_fd <= 2)
+ devnull_fd = dup(devnull_fd);
+ if (devnull_fd == -1)
+ die("opening /dev/null failed (%s)", strerror(errno));
+ }Except for the insane GNU style indentation ;-) this makes a lot of sense. Acked-by: Johannes Sixt <redacted> The changes to the MINGW32 section are good (they pass the test suite). Thanks for taking care of that. -- Hannes