open-process: Fix dup(2) and execvp(2) error handling.
authorMark H Weaver <mhw@netris.org>
Sat, 11 May 2019 07:47:41 +0000 (03:47 -0400)
committerMark H Weaver <mhw@netris.org>
Tue, 18 Jun 2019 07:07:27 +0000 (03:07 -0400)
commit521f1ab4709217407496004019c00005d2a82f78
tree435da047351958406de971bfab41feb306c88056
parent3ec7afb2c6906811dcc6f02420ff0f4efc844824
open-process: Fix dup(2) and execvp(2) error handling.

Previously, in the case where OUT is 0, or ERR is 0 or 1,
e.g. when (current-error-port) points to STDOUT, the code in
'start_child' to relocate OUT/ERR out of the way to another file
descriptor had multiple bugs:

(1) It neglected to close the original file descriptor.

(2) It checked 'errno' without first checking the return value of
    dup(2).  This doesn't work because dup(2) leaves 'errno' unchanged
    if there's no error.

(3) In case 'errno' contained EINTR, the retry code failed because
    OUT (or ERR) was overwritten by the result of the previous failed
    dup(2) call.

This commit fixes these problems, as well as another problem with
'execvp' error reporting.

* libguile/posix.c (renumber_file_descriptor): New static helper
function.
(start_child): Use 'renumber_file_descriptor'.  If 'execvp' fails, write
the error message to file descriptor 2.  Previously, we wrote the error
message to ERR, which was the old file descriptor before being relocated
to 2.
libguile/posix.c