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