1 // Copyright (c) 2016 Klemens D. Morgenstern
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef BOOST_PROCESS_DETAIL_WINDOWS_PIPE_HPP
7 #define BOOST_PROCESS_DETAIL_WINDOWS_PIPE_HPP
8 
9 #include <boost/winapi/basic_types.hpp>
10 #include <boost/winapi/error_codes.hpp>
11 #include <boost/winapi/pipes.hpp>
12 #include <boost/winapi/handles.hpp>
13 #include <boost/winapi/file_management.hpp>
14 #include <boost/winapi/get_last_error.hpp>
15 #include <boost/winapi/access_rights.hpp>
16 #include <boost/winapi/process.hpp>
17 #include <boost/process/detail/windows/compare_handles.hpp>
18 #include <system_error>
19 #include <string>
20 
21 
22 namespace boost { namespace process { namespace detail { namespace windows {
23 
24 template<class CharT, class Traits = std::char_traits<CharT>>
25 class basic_pipe
26 {
27     ::boost::winapi::HANDLE_ _source = ::boost::winapi::INVALID_HANDLE_VALUE_;
28     ::boost::winapi::HANDLE_ _sink   = ::boost::winapi::INVALID_HANDLE_VALUE_;
29 public:
30     typedef CharT                      char_type  ;
31     typedef          Traits            traits_type;
32     typedef typename Traits::int_type  int_type   ;
33     typedef typename Traits::pos_type  pos_type   ;
34     typedef typename Traits::off_type  off_type   ;
35     typedef ::boost::winapi::HANDLE_ native_handle_type;
36 
basic_pipe(::boost::winapi::HANDLE_ source,::boost::winapi::HANDLE_ sink)37     explicit basic_pipe(::boost::winapi::HANDLE_ source, ::boost::winapi::HANDLE_ sink)
38             : _source(source), _sink(sink) {}
39     inline explicit basic_pipe(const std::string & name);
40     inline basic_pipe(const basic_pipe& p);
basic_pipe(basic_pipe && lhs)41     basic_pipe(basic_pipe&& lhs)  : _source(lhs._source), _sink(lhs._sink)
42     {
43         lhs._source = ::boost::winapi::INVALID_HANDLE_VALUE_;
44         lhs._sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
45     }
46     inline basic_pipe& operator=(const basic_pipe& p);
47     inline basic_pipe& operator=(basic_pipe&& lhs);
~basic_pipe()48     ~basic_pipe()
49     {
50         if (_sink   != ::boost::winapi::INVALID_HANDLE_VALUE_)
51             ::boost::winapi::CloseHandle(_sink);
52         if (_source != ::boost::winapi::INVALID_HANDLE_VALUE_)
53             ::boost::winapi::CloseHandle(_source);
54     }
native_source() const55     native_handle_type native_source() const {return _source;}
native_sink() const56     native_handle_type native_sink  () const {return _sink;}
57 
assign_source(native_handle_type h)58     void assign_source(native_handle_type h) { _source = h;}
assign_sink(native_handle_type h)59     void assign_sink  (native_handle_type h) { _sink = h;}
60 
basic_pipe()61     basic_pipe()
62     {
63         if (!::boost::winapi::CreatePipe(&_source, &_sink, nullptr, 0))
64             throw_last_error("CreatePipe() failed");
65 
66     }
67 
write(const char_type * data,int_type count)68     int_type write(const char_type * data, int_type count)
69     {
70         ::boost::winapi::DWORD_ write_len;
71         if (!::boost::winapi::WriteFile(
72                 _sink, data, count * sizeof(char_type), &write_len, nullptr
73                 ))
74         {
75             auto ec = ::boost::process::detail::get_last_error();
76             if ((ec.value() == ::boost::winapi::ERROR_BROKEN_PIPE_) ||
77                 (ec.value() == ::boost::winapi::ERROR_NO_DATA_))
78                 return 0;
79             else
80                 throw process_error(ec, "WriteFile failed");
81         }
82         return static_cast<int_type>(write_len);
83     }
read(char_type * data,int_type count)84     int_type read(char_type * data, int_type count)
85     {
86         ::boost::winapi::DWORD_ read_len;
87         if (!::boost::winapi::ReadFile(
88                 _source, data, count * sizeof(char_type), &read_len, nullptr
89                 ))
90         {
91             auto ec = ::boost::process::detail::get_last_error();
92             if ((ec.value() == ::boost::winapi::ERROR_BROKEN_PIPE_) ||
93                 (ec.value() == ::boost::winapi::ERROR_NO_DATA_))
94                 return 0;
95             else
96                 throw process_error(ec, "ReadFile failed");
97         }
98         return static_cast<int_type>(read_len);
99     }
100 
is_open() const101     bool is_open() const
102     {
103         return (_source != ::boost::winapi::INVALID_HANDLE_VALUE_) ||
104                (_sink   != ::boost::winapi::INVALID_HANDLE_VALUE_);
105     }
106 
close()107     void close()
108     {
109         ::boost::winapi::CloseHandle(_source);
110         ::boost::winapi::CloseHandle(_sink);
111         _source = ::boost::winapi::INVALID_HANDLE_VALUE_;
112         _sink   = ::boost::winapi::INVALID_HANDLE_VALUE_;
113     }
114 };
115 
116 template<class Char, class Traits>
basic_pipe(const basic_pipe & p)117 basic_pipe<Char, Traits>::basic_pipe(const basic_pipe & p)
118 {
119     auto proc = ::boost::winapi::GetCurrentProcess();
120 
121     if (p._source == ::boost::winapi::INVALID_HANDLE_VALUE_)
122         _source = ::boost::winapi::INVALID_HANDLE_VALUE_;
123     else if (!::boost::winapi::DuplicateHandle(
124             proc, p._source, proc, &_source, 0,
125             static_cast<::boost::winapi::BOOL_>(true),
126              ::boost::winapi::DUPLICATE_SAME_ACCESS_))
127         throw_last_error("Duplicate Pipe Failed");
128 
129     if (p._sink == ::boost::winapi::INVALID_HANDLE_VALUE_)
130         _sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
131     else if (!::boost::winapi::DuplicateHandle(
132             proc, p._sink, proc, &_sink, 0,
133             static_cast<::boost::winapi::BOOL_>(true),
134              ::boost::winapi::DUPLICATE_SAME_ACCESS_))
135         throw_last_error("Duplicate Pipe Failed");
136 
137 }
138 
139 template<class Char, class Traits>
basic_pipe(const std::string & name)140 basic_pipe<Char, Traits>::basic_pipe(const std::string & name)
141 {
142     static constexpr int OPEN_EXISTING_         = 3; //temporary.
143     static constexpr int FILE_FLAG_OVERLAPPED_  = 0x40000000; //temporary
144     //static constexpr int FILE_ATTRIBUTE_NORMAL_ = 0x00000080; //temporary
145 
146 #if BOOST_NO_ANSI_APIS
147     std::wstring name_ = boost::process::detail::convert(name);
148 #else
149     auto &name_ = name;
150 #endif
151     ::boost::winapi::HANDLE_ source = ::boost::winapi::create_named_pipe(
152             name_.c_str(),
153             ::boost::winapi::PIPE_ACCESS_INBOUND_
154             | FILE_FLAG_OVERLAPPED_, //write flag
155             0, ::boost::winapi::PIPE_UNLIMITED_INSTANCES_, 8192, 8192, 0, nullptr);
156 
157     if (source == boost::winapi::INVALID_HANDLE_VALUE_)
158         ::boost::process::detail::throw_last_error("create_named_pipe() failed");
159 
160     ::boost::winapi::HANDLE_ sink = boost::winapi::create_file(
161             name.c_str(),
162             ::boost::winapi::GENERIC_WRITE_, 0, nullptr,
163             OPEN_EXISTING_,
164             FILE_FLAG_OVERLAPPED_, //to allow read
165             nullptr);
166 
167     if (sink == ::boost::winapi::INVALID_HANDLE_VALUE_)
168         ::boost::process::detail::throw_last_error("create_file() failed");
169 
170     _source = source;
171     _sink   = sink;
172 }
173 
174 template<class Char, class Traits>
operator =(const basic_pipe & p)175 basic_pipe<Char, Traits>& basic_pipe<Char, Traits>::operator=(const basic_pipe & p)
176 {
177     auto proc = ::boost::winapi::GetCurrentProcess();
178 
179     if (p._source == ::boost::winapi::INVALID_HANDLE_VALUE_)
180         _source = ::boost::winapi::INVALID_HANDLE_VALUE_;
181     else if (!::boost::winapi::DuplicateHandle(
182             proc, p._source, proc, &_source, 0,
183             static_cast<::boost::winapi::BOOL_>(true),
184              ::boost::winapi::DUPLICATE_SAME_ACCESS_))
185         throw_last_error("Duplicate Pipe Failed");
186 
187     if (p._sink == ::boost::winapi::INVALID_HANDLE_VALUE_)
188         _sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
189     else if (!::boost::winapi::DuplicateHandle(
190             proc, p._sink, proc, &_sink, 0,
191             static_cast<::boost::winapi::BOOL_>(true),
192              ::boost::winapi::DUPLICATE_SAME_ACCESS_))
193         throw_last_error("Duplicate Pipe Failed");
194 
195     return *this;
196 }
197 
198 template<class Char, class Traits>
operator =(basic_pipe && lhs)199 basic_pipe<Char, Traits>& basic_pipe<Char, Traits>::operator=(basic_pipe && lhs)
200 {
201     if (_source != ::boost::winapi::INVALID_HANDLE_VALUE_)
202         ::boost::winapi::CloseHandle(_source);
203 
204     if (_sink != ::boost::winapi::INVALID_HANDLE_VALUE_)
205         ::boost::winapi::CloseHandle(_sink);
206 
207     _source = lhs._source;
208     _sink   = lhs._sink;
209     lhs._source = ::boost::winapi::INVALID_HANDLE_VALUE_;
210     lhs._sink   = ::boost::winapi::INVALID_HANDLE_VALUE_;
211     return *this;
212 }
213 
214 template<class Char, class Traits>
operator ==(const basic_pipe<Char,Traits> & lhs,const basic_pipe<Char,Traits> & rhs)215 inline bool operator==(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
216 {
217     return compare_handles(lhs.native_source(), rhs.native_source()) &&
218            compare_handles(lhs.native_sink(),   rhs.native_sink());
219 }
220 
221 template<class Char, class Traits>
operator !=(const basic_pipe<Char,Traits> & lhs,const basic_pipe<Char,Traits> & rhs)222 inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
223 {
224     return !compare_handles(lhs.native_source(), rhs.native_source()) ||
225            !compare_handles(lhs.native_sink(),   rhs.native_sink());
226 }
227 
228 }}}}
229 
230 #endif
231