From 062cad2d07081f9a60adbb90fc1184a435a1a93f Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 20 Mar 2010 17:20:29 +0100 Subject: [PATCH] pipe: Set errno upon failure. --- ChangeLog | 7 +++++++ lib/pipe.c | 7 ++++++- lib/pipe.h | 3 ++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index e21684c57..94e90f0f1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,11 @@ 2010-03-20 Bruno Haible + + pipe: Set errno upon failure. + * lib/pipe.h: Specify that when -1 is returned, errno is set. + * lib/pipe.c (create_pipe): Set errno when returning -1. Use the right + errno value in error message. + +2010-03-20 Bruno Haible Jim Meyering lchown: Avoid "unused variable" warning. diff --git a/lib/pipe.c b/lib/pipe.c index 698652508..1c81d1452 100644 --- a/lib/pipe.c +++ b/lib/pipe.c @@ -128,6 +128,7 @@ create_pipe (const char *progname, int nulloutfd; int stdinfd; int stdoutfd; + int saved_errno; /* FIXME: Need to free memory allocated by prepare_spawn. */ prog_argv = prepare_spawn (prog_argv); @@ -203,6 +204,8 @@ create_pipe (const char *progname, (const char **) environ); } } + if (child == -1) + saved_errno = errno; if (stdinfd >= 0) close (stdinfd); if (stdoutfd >= 0) @@ -225,12 +228,13 @@ create_pipe (const char *progname, if (child == -1) { if (exit_on_error || !null_stderr) - error (exit_on_error ? EXIT_FAILURE : 0, errno, + error (exit_on_error ? EXIT_FAILURE : 0, saved_errno, _("%s subprocess failed"), progname); if (pipe_stdout) close (ifd[0]); if (pipe_stdin) close (ofd[1]); + errno = saved_errno; return -1; } @@ -350,6 +354,7 @@ create_pipe (const char *progname, close (ofd[0]); close (ofd[1]); } + errno = err; return -1; } posix_spawn_file_actions_destroy (&actions); diff --git a/lib/pipe.h b/lib/pipe.h index 2f23903ab..7517892ba 100644 --- a/lib/pipe.h +++ b/lib/pipe.h @@ -36,7 +36,8 @@ extern "C" { one or two file descriptors for communication with the subprocess. If the subprocess creation fails: if exit_on_error is true, the main process exits with an error message; otherwise, an error message is given - if null_stderr is false, then -1 is returned and fd[] remain uninitialized. + if null_stderr is false, then -1 is returned, with errno set, and fd[] + remain uninitialized. After finishing communication, the caller should call wait_subprocess() to get rid of the subprocess in the process table. -- 2.11.0