about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc31
-rw-r--r--src/libutil/util.hh9
2 files changed, 39 insertions, 1 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 399233983d62..676404ecfca6 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -365,11 +365,16 @@ AutoCloseFD::AutoCloseFD(int fd)
 
 AutoCloseFD::~AutoCloseFD()
 {
-    if (fd != -1) close(fd);
+    try {
+        close();
+    } catch (Error & e) {
+        printMsg(lvlError, format("error (ignored): %1%") % e.msg());
+    }
 }
 
 void AutoCloseFD::operator =(int fd)
 {
+    if (this->fd != fd) close();
     this->fd = fd;
 }
 
@@ -378,6 +383,30 @@ AutoCloseFD::operator int()
     return fd;
 }
 
+void AutoCloseFD::close()
+{
+    if (fd != -1) {
+        if (::close(fd) == -1)
+            /* This should never happen. */
+            throw SysError("closing file descriptor");
+        fd = -1;
+    }
+}
+
+bool AutoCloseFD::isOpen()
+{
+    return fd != -1;
+}
+
+
+void Pipe::create()
+{
+    int fds[2];
+    if (pipe(fds) != 0) throw SysError("creating pipe");
+    readSide = fds[0];
+    writeSide = fds[1];
+}
+
 
 AutoCloseDir::AutoCloseDir()
 {
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index d9ca1dac2222..dcd0bf766cc7 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -181,6 +181,15 @@ public:
     ~AutoCloseFD();
     void operator =(int fd);
     operator int();
+    void close();
+    bool isOpen();
+};
+
+class Pipe
+{
+public:
+    AutoCloseFD readSide, writeSide;
+    void create();
 };
 
 class AutoCloseDir