From 9a4c531eb1aa68595cd3ab074b31bfa565aaefe2 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 15 Oct 2013 14:53:04 +0300 Subject: [PATCH] Allow setting a pipe read-only or write-only after creation 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 | 16 ++++++++++++++++ source/io/pipe.h | 1 + 2 files changed, 17 insertions(+) diff --git a/source/io/pipe.cpp b/source/io/pipe.cpp index 831076f..0a72a80 100644 --- a/source/io/pipe.cpp +++ b/source/io/pipe.cpp @@ -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); diff --git a/source/io/pipe.h b/source/io/pipe.h index 50da5e5..3f964f4 100644 --- a/source/io/pipe.h +++ b/source/io/pipe.h @@ -22,6 +22,7 @@ private: public: ~Pipe(); + void set_mode(Mode); void set_block(bool); protected: -- 2.45.2