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_POSIX_ASYNC_PIPE_HPP_
7 #define BOOST_PROCESS_DETAIL_POSIX_ASYNC_PIPE_HPP_
8 
9 
10 #include <boost/process/detail/posix/basic_pipe.hpp>
11 #include <boost/asio/posix/stream_descriptor.hpp>
12 #include <boost/asio/post.hpp>
13 #include <system_error>
14 #include <string>
15 #include <utility>
16 
17 namespace boost { namespace process { namespace detail { namespace posix {
18 
19 class async_pipe
20 {
21     ::boost::asio::posix::stream_descriptor _source;
22     ::boost::asio::posix::stream_descriptor _sink  ;
23 public:
24     typedef int native_handle_type;
25     typedef ::boost::asio::posix::stream_descriptor handle_type;
26     typedef typename handle_type::executor_type executor_type;
27 
get_executor()28     executor_type get_executor()
29     {
30         return _source.get_executor();
31     }
32 
async_pipe(boost::asio::io_context & ios)33     inline async_pipe(boost::asio::io_context & ios) : async_pipe(ios, ios) {}
34 
async_pipe(boost::asio::io_context & ios_source,boost::asio::io_context & ios_sink)35     inline async_pipe(boost::asio::io_context & ios_source,
36                       boost::asio::io_context & ios_sink) : _source(ios_source), _sink(ios_sink)
37     {
38         int fds[2];
39         if (::pipe(fds) == -1)
40             boost::process::detail::throw_last_error("pipe(2) failed");
41 
42         _source.assign(fds[0]);
43         _sink  .assign(fds[1]);
44     };
async_pipe(boost::asio::io_context & ios,const std::string & name)45     inline async_pipe(boost::asio::io_context & ios, const std::string & name)
46         : async_pipe(ios, ios, name) {}
47 
48     inline async_pipe(boost::asio::io_context & ios_source,
49                       boost::asio::io_context & io_sink, const std::string & name);
50     inline async_pipe(const async_pipe& lhs);
async_pipe(async_pipe && lhs)51     async_pipe(async_pipe&& lhs)  : _source(std::move(lhs._source)), _sink(std::move(lhs._sink))
52     {
53         lhs._source = ::boost::asio::posix::stream_descriptor{lhs._source.get_executor()};
54         lhs._sink   = ::boost::asio::posix::stream_descriptor{lhs._sink.  get_executor()};
55     }
56 
57     template<class CharT, class Traits = std::char_traits<CharT>>
async_pipe(::boost::asio::io_context & ios_source,::boost::asio::io_context & ios_sink,const basic_pipe<CharT,Traits> & p)58     explicit async_pipe(::boost::asio::io_context & ios_source,
59                         ::boost::asio::io_context & ios_sink,
60                          const basic_pipe<CharT, Traits> & p)
61             : _source(ios_source, p.native_source()), _sink(ios_sink, p.native_sink())
62     {
63     }
64 
65     template<class CharT, class Traits = std::char_traits<CharT>>
async_pipe(boost::asio::io_context & ios,const basic_pipe<CharT,Traits> & p)66     explicit async_pipe(boost::asio::io_context & ios, const basic_pipe<CharT, Traits> & p)
67             : async_pipe(ios, ios, p)
68     {
69     }
70 
71     template<class CharT, class Traits = std::char_traits<CharT>>
72     inline async_pipe& operator=(const basic_pipe<CharT, Traits>& p);
73     inline async_pipe& operator=(const async_pipe& rhs);
74 
75     inline async_pipe& operator=(async_pipe&& lhs);
76 
~async_pipe()77     ~async_pipe()
78     {
79         boost::system::error_code ec;
80         close(ec);
81     }
82 
83     template<class CharT, class Traits = std::char_traits<CharT>>
84     inline explicit operator basic_pipe<CharT, Traits>() const;
85 
cancel()86     void cancel()
87     {
88         if (_sink.is_open())
89             _sink.cancel();
90         if (_source.is_open())
91             _source.cancel();
92     }
93 
close()94     void close()
95     {
96         if (_sink.is_open())
97             _sink.close();
98         if (_source.is_open())
99             _source.close();
100     }
close(boost::system::error_code & ec)101     void close(boost::system::error_code & ec)
102     {
103         if (_sink.is_open())
104             _sink.close(ec);
105         if (_source.is_open())
106             _source.close(ec);
107     }
108 
109 
is_open() const110     bool is_open() const
111     {
112         return  _sink.is_open() || _source.is_open();
113     }
async_close()114     void async_close()
115     {
116         if (_sink.is_open())
117             boost::asio::post(_sink.get_executor(),   [this]{_sink.close();});
118         if (_source.is_open())
119             boost::asio::post(_source.get_executor(), [this]{_source.close();});
120     }
121 
122     template<typename MutableBufferSequence>
read_some(const MutableBufferSequence & buffers)123     std::size_t read_some(const MutableBufferSequence & buffers)
124     {
125         return _source.read_some(buffers);
126     }
127     template<typename MutableBufferSequence>
write_some(const MutableBufferSequence & buffers)128     std::size_t write_some(const MutableBufferSequence & buffers)
129     {
130         return _sink.write_some(buffers);
131     }
132 
133     template<typename MutableBufferSequence>
read_some(const MutableBufferSequence & buffers,boost::system::error_code & ec)134     std::size_t read_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
135     {
136         return _source.read_some(buffers, ec);
137     }
138     template<typename MutableBufferSequence>
write_some(const MutableBufferSequence & buffers,boost::system::error_code & ec)139     std::size_t write_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
140     {
141         return _sink.write_some(buffers, ec);
142     }
143 
144 
native_source() const145     native_handle_type native_source() const {return const_cast<boost::asio::posix::stream_descriptor&>(_source).native_handle();}
native_sink() const146     native_handle_type native_sink  () const {return const_cast<boost::asio::posix::stream_descriptor&>(_sink  ).native_handle();}
147 
148     template<typename MutableBufferSequence,
149              typename ReadHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,void (boost::system::error_code,std::size_t))150     BOOST_ASIO_INITFN_RESULT_TYPE(
151           ReadHandler, void(boost::system::error_code, std::size_t))
152       async_read_some(
153         const MutableBufferSequence & buffers,
154               ReadHandler &&handler)
155     {
156         return _source.async_read_some(buffers, std::forward<ReadHandler>(handler));
157     }
158 
159     template<typename ConstBufferSequence,
160              typename WriteHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,void (boost::system::error_code,std::size_t))161     BOOST_ASIO_INITFN_RESULT_TYPE(
162               WriteHandler, void(boost::system::error_code, std::size_t))
163       async_write_some(
164         const ConstBufferSequence & buffers,
165         WriteHandler&& handler)
166     {
167         return _sink.async_write_some(buffers, std::forward<WriteHandler>(handler));
168     }
169 
170 
sink() const171     const handle_type & sink  () const & {return _sink;}
source() const172     const handle_type & source() const & {return _source;}
173 
sink()174     handle_type && sink()  &&  { return std::move(_sink); }
source()175     handle_type && source()&&  { return std::move(_source); }
176 
source(::boost::asio::io_context & ios)177     handle_type source(::boost::asio::io_context& ios) &&
178     {
179         ::boost::asio::posix::stream_descriptor stolen(ios, _source.release());
180         return stolen;
181     }
sink(::boost::asio::io_context & ios)182     handle_type sink  (::boost::asio::io_context& ios) &&
183     {
184         ::boost::asio::posix::stream_descriptor stolen(ios, _sink.release());
185         return stolen;
186     }
187 
source(::boost::asio::io_context & ios) const188     handle_type source(::boost::asio::io_context& ios) const &
189     {
190         auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
191         return ::boost::asio::posix::stream_descriptor(ios, ::dup(source_in));
192     }
sink(::boost::asio::io_context & ios) const193     handle_type sink  (::boost::asio::io_context& ios) const &
194     {
195         auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
196         return ::boost::asio::posix::stream_descriptor(ios, ::dup(sink_in));
197     }
198 };
199 
200 
async_pipe(boost::asio::io_context & ios_source,boost::asio::io_context & ios_sink,const std::string & name)201 async_pipe::async_pipe(boost::asio::io_context & ios_source,
202                        boost::asio::io_context & ios_sink,
203                        const std::string & name) : _source(ios_source), _sink(ios_sink)
204 {
205     auto fifo = mkfifo(name.c_str(), 0666 );
206 
207     if (fifo != 0)
208         boost::process::detail::throw_last_error("mkfifo() failed");
209 
210 
211     int  read_fd = open(name.c_str(), O_RDWR);
212 
213     if (read_fd == -1)
214         boost::process::detail::throw_last_error();
215 
216     int write_fd = dup(read_fd);
217 
218     if (write_fd == -1)
219         boost::process::detail::throw_last_error();
220 
221     _source.assign(read_fd);
222     _sink  .assign(write_fd);
223 }
224 
async_pipe(const async_pipe & p)225 async_pipe::async_pipe(const async_pipe & p) :
226         _source(const_cast<async_pipe&>(p)._source.get_executor()),
227         _sink(  const_cast<async_pipe&>(p)._sink.get_executor())
228 {
229 
230     //cannot get the handle from a const object.
231     auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
232     auto sink_in   = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
233     if (source_in == -1)
234         _source.assign(-1);
235     else
236     {
237         _source.assign(::dup(source_in));
238         if (_source.native_handle()== -1)
239             ::boost::process::detail::throw_last_error("dup()");
240     }
241 
242     if (sink_in   == -1)
243         _sink.assign(-1);
244     else
245     {
246         _sink.assign(::dup(sink_in));
247         if (_sink.native_handle() == -1)
248             ::boost::process::detail::throw_last_error("dup()");
249     }
250 }
251 
operator =(const async_pipe & p)252 async_pipe& async_pipe::operator=(const async_pipe & p)
253 {
254     int source;
255     int sink;
256 
257     //cannot get the handle from a const object.
258     auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(p._source).native_handle();
259     auto sink_in   = const_cast<::boost::asio::posix::stream_descriptor &>(p._sink).native_handle();
260     if (source_in == -1)
261         source = -1;
262     else
263     {
264         source = ::dup(source_in);
265         if (source == -1)
266             ::boost::process::detail::throw_last_error("dup()");
267     }
268 
269     if (sink_in   == -1)
270         sink = -1;
271     else
272     {
273         sink  = ::dup(sink_in);
274         if (sink == -1)
275             ::boost::process::detail::throw_last_error("dup()");
276     }
277     _source.assign(source);
278     _sink.  assign(sink);
279 
280     return *this;
281 }
282 
operator =(async_pipe && lhs)283 async_pipe& async_pipe::operator=(async_pipe && lhs)
284 {
285     std::swap(_source, lhs._source);
286     std::swap(_sink, lhs._sink);
287     return *this;
288 }
289 
290 template<class CharT, class Traits>
operator basic_pipe<CharT,Traits>() const291 async_pipe::operator basic_pipe<CharT, Traits>() const
292 {
293     int source;
294     int sink;
295 
296     //cannot get the handle from a const object.
297     auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
298     auto sink_in   = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
299 
300 
301     if (source_in == -1)
302         source = -1;
303     else
304     {
305         source = ::dup(source_in);
306         if (source == -1)
307             ::boost::process::detail::throw_last_error("dup()");
308     }
309 
310     if (sink_in   == -1)
311         sink = -1;
312     else
313     {
314         sink = ::dup(sink_in);
315         if (sink == -1)
316             ::boost::process::detail::throw_last_error("dup()");
317     }
318 
319     return basic_pipe<CharT, Traits>{source, sink};
320 }
321 
322 
operator ==(const async_pipe & lhs,const async_pipe & rhs)323 inline bool operator==(const async_pipe & lhs, const async_pipe & rhs)
324 {
325     return compare_handles(lhs.native_source(), rhs.native_source()) &&
326            compare_handles(lhs.native_sink(),   rhs.native_sink());
327 }
328 
operator !=(const async_pipe & lhs,const async_pipe & rhs)329 inline bool operator!=(const async_pipe & lhs, const async_pipe & rhs)
330 {
331     return !compare_handles(lhs.native_source(), rhs.native_source()) ||
332            !compare_handles(lhs.native_sink(),   rhs.native_sink());
333 }
334 
335 template<class Char, class Traits>
operator ==(const async_pipe & lhs,const basic_pipe<Char,Traits> & rhs)336 inline bool operator==(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
337 {
338     return compare_handles(lhs.native_source(), rhs.native_source()) &&
339            compare_handles(lhs.native_sink(),   rhs.native_sink());
340 }
341 
342 template<class Char, class Traits>
operator !=(const async_pipe & lhs,const basic_pipe<Char,Traits> & rhs)343 inline bool operator!=(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
344 {
345     return !compare_handles(lhs.native_source(), rhs.native_source()) ||
346            !compare_handles(lhs.native_sink(),   rhs.native_sink());
347 }
348 
349 template<class Char, class Traits>
operator ==(const basic_pipe<Char,Traits> & lhs,const async_pipe & rhs)350 inline bool operator==(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
351 {
352     return compare_handles(lhs.native_source(), rhs.native_source()) &&
353            compare_handles(lhs.native_sink(),   rhs.native_sink());
354 }
355 
356 template<class Char, class Traits>
operator !=(const basic_pipe<Char,Traits> & lhs,const async_pipe & rhs)357 inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
358 {
359     return !compare_handles(lhs.native_source(), rhs.native_source()) ||
360            !compare_handles(lhs.native_sink(),   rhs.native_sink());
361 }
362 
363 }}}}
364 
365 #endif /* INCLUDE_BOOST_PIPE_DETAIL_WINDOWS_ASYNC_PIPE_HPP_ */
366