aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-08-07 19:31:25 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-08-07 19:31:25 +0000
commit9013518c2e8c391f625dadb6d6852837b7dd8214 (patch)
tree20fb9932a6681ee55573a95a39b970e79f935f4f
parentd9873928cd11868700433072e7abd9872dbae5db (diff)
downloadpaludis-9013518c2e8c391f625dadb6d6852837b7dd8214.tar.gz
paludis-9013518c2e8c391f625dadb6d6852837b7dd8214.tar.xz
Fix PStream FD usage
-rw-r--r--paludis/util/pipe.cc17
-rw-r--r--paludis/util/pipe.hh13
-rw-r--r--paludis/util/pstream.cc24
3 files changed, 42 insertions, 12 deletions
diff --git a/paludis/util/pipe.cc b/paludis/util/pipe.cc
index 803733e..f109994 100644
--- a/paludis/util/pipe.cc
+++ b/paludis/util/pipe.cc
@@ -20,19 +20,32 @@
#include "pipe.hh"
#include <paludis/util/exception.hh>
#include <paludis/util/stringify.hh>
+#include <paludis/util/log.hh>
#include <unistd.h>
+#include <cstring>
+#include <errno.h>
using namespace paludis;
Pipe::Pipe()
{
+ Context context("When creating pipe FDs:");
+
if (-1 == pipe(_fds))
throw InternalError(PALUDIS_HERE, "pipe(2) failed");
+
+ Log::get_instance()->message(ll_debug, lc_context) << "Pipe FDs are '" << read_fd() << "', '"
+ << write_fd() << "'";
}
Pipe::~Pipe()
{
- close(_fds[0]);
- close(_fds[1]);
+ Context context("When destroying pipe FDs '" + stringify(read_fd()) + "', '" + stringify(write_fd()) + "'");
+ if (-1 != _fds[0])
+ if (-1 == close(_fds[0]))
+ Log::get_instance()->message(ll_warning, lc_context) << "close(" << _fds[0] << ") -> " << strerror(errno);
+ if (-1 != _fds[1])
+ if (-1 == close(_fds[1]))
+ Log::get_instance()->message(ll_warning, lc_context) << "close(" << _fds[1] << ") -> " << strerror(errno);
}
diff --git a/paludis/util/pipe.hh b/paludis/util/pipe.hh
index e7068e5..996b80e 100644
--- a/paludis/util/pipe.hh
+++ b/paludis/util/pipe.hh
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
+ * Copyright (c) 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -65,7 +65,18 @@ namespace paludis
return _fds[1];
}
+ void clear_read_fd()
+ {
+ _fds[0] = -1;
+ }
+
+ void clear_write_fd()
+ {
+ _fds[1] = -1;
+ }
+
///\}
+
};
}
diff --git a/paludis/util/pstream.cc b/paludis/util/pstream.cc
index 4cd339c..9450146 100644
--- a/paludis/util/pstream.cc
+++ b/paludis/util/pstream.cc
@@ -60,10 +60,9 @@ PStreamInBuf::underflow()
if (n == 0)
return EOF;
else if (n < 0)
- throw PStreamError("read returned error " + stringify(strerror(errno)));
+ throw PStreamError("read returned error " + stringify(strerror(errno)) + ", fd is " + stringify(fd));
- setg(buffer + putback_size - num_putback, buffer + putback_size,
- buffer + putback_size + n);
+ setg(buffer + putback_size - num_putback, buffer + putback_size, buffer + putback_size + n);
return *gptr();
}
@@ -99,7 +98,7 @@ PStreamInBuf::PStreamInBuf(const Command & cmd) :
+ " -- " + c;
cmd.echo_to_stderr();
- Log::get_instance()->message(ll_debug, lc_no_context, "execl /bin/sh -c " + c + " " + extras);
+ Log::get_instance()->message(ll_debug, lc_context, "execl /bin/sh -c " + c + " " + extras);
child = fork();
@@ -164,6 +163,7 @@ PStreamInBuf::PStreamInBuf(const Command & cmd) :
else
{
close(stdout_pipe.write_fd());
+ stdout_pipe.clear_write_fd();
fd = stdout_pipe.read_fd();
}
@@ -172,24 +172,30 @@ PStreamInBuf::PStreamInBuf(const Command & cmd) :
PStreamInBuf::~PStreamInBuf()
{
+ Context context("When destroying PStream process with fd '" + stringify(fd) + "':");
+
if (0 != fd)
{
int fdn(fd), x;
- waitpid(child, &x, 0);
- Log::get_instance()->message(ll_debug, lc_no_context, "close " + stringify(fdn) + " -> " + stringify(x));
+ if (-1 == waitpid(child, &x, 0))
+ throw PStreamError("waitpid returned error " + stringify(strerror(errno)) + ", fd is " + stringify(fd));
+ Log::get_instance()->message(ll_debug, lc_context) << "waitpid " << fdn << " for destructor -> " <<
+ (WIFSIGNALED(x) ? "signal " + stringify(WTERMSIG(x) + 128) : "exit status " + stringify(WEXITSTATUS(x)));
}
}
int
PStreamInBuf::exit_status()
{
+ Context context("When requesting exit status for PStream process with fd '" + stringify(fd) + "':");
if (0 != fd)
{
int fdn(fd);
- waitpid(child, &_exit_status, 0);
+ if (-1 == waitpid(child, &_exit_status, 0))
+ throw PStreamError("waitpid returned error " + stringify(strerror(errno)) + ", fd is " + stringify(fd));
fd = 0;
- Log::get_instance()->message(ll_debug, lc_no_context, "close " + stringify(fdn) +
- " for exit status -> " + stringify(_exit_status));
+ Log::get_instance()->message(ll_debug, lc_context) << "waitpid " << fdn << " for exit_status() -> " <<
+ (WIFSIGNALED(_exit_status) ? "signal " + stringify(WTERMSIG(_exit_status) + 128) : "exit status " + stringify(WEXITSTATUS(_exit_status)));
}
return WIFSIGNALED(_exit_status) ? WTERMSIG(_exit_status) + 128 : WEXITSTATUS(_exit_status);
}