Allow setting a pipe read-only or write-only after creation
authorMikko Rasa <tdb@tdb.fi>
Tue, 15 Oct 2013 11:53:04 +0000 (14:53 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 15 Oct 2013 11:53:04 +0000 (14:53 +0300)
This is useful for handing the pipe to a child process and then closing
the unused end so an end-of-file event can be properly received.

source/io/pipe.cpp
source/io/pipe.h

index 831076f0086b2ffd756ec9e86a3b1b1364e9b1cc..0a72a80068da4d9ac686fdb8cd3216801e316725 100644 (file)
@@ -24,6 +24,22 @@ Pipe::~Pipe()
        sys_close(write_handle);
 }
 
+void Pipe::set_mode(Mode m)
+{
+       m = m&M_RDWR;
+       if(!m)
+               throw invalid_argument("Pipe::set_mode");
+
+       check_access(m);
+
+       Mode close = mode&M_RDWR&~m;
+       if(close&M_READ)
+               sys_close(read_handle);
+       if(close&M_WRITE)
+               sys_close(write_handle);
+       mode = (mode&~M_RDWR)|m;
+}
+
 void Pipe::set_block(bool b)
 {
        mode = (mode&~M_NONBLOCK);
index 50da5e53e1d6b49a9c1d599a319ae6fb5502c83f..3f964f4536facbb16413d5c05efc373cf4216722 100644 (file)
@@ -22,6 +22,7 @@ private:
 public:
        ~Pipe();
 
+       void set_mode(Mode);
        void set_block(bool);
 
 protected: