]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/process/detail/posix/async_in.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / process / detail / posix / async_in.hpp
1 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
2 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
3 // Copyright (c) 2009 Boris Schaeling
4 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
5 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10 #ifndef BOOST_PROCESS_DETAIL_POSIX_ASYNC_IN_HPP
11 #define BOOST_PROCESS_DETAIL_POSIX_ASYNC_IN_HPP
12
13 #include <boost/process/detail/handler_base.hpp>
14 #include <boost/process/detail/posix/async_handler.hpp>
15 #include <boost/asio/write.hpp>
16 #include <boost/process/async_pipe.hpp>
17 #include <memory>
18 #include <future>
19
20 namespace boost { namespace process { namespace detail { namespace posix {
21
22
23 template<typename Buffer>
24 struct async_in_buffer : ::boost::process::detail::posix::handler_base_ext,
25 ::boost::process::detail::posix::require_io_context
26 {
27 Buffer & buf;
28
29 std::shared_ptr<std::promise<void>> promise;
30 async_in_buffer operator>(std::future<void> & fut)
31 {
32 promise = std::make_shared<std::promise<void>>();
33 fut = promise->get_future(); return std::move(*this);
34 }
35
36 std::shared_ptr<boost::process::async_pipe> pipe;
37
38 async_in_buffer(Buffer & buf) : buf(buf)
39 {
40 }
41 template <typename Executor>
42 inline void on_success(Executor &exec)
43 {
44 auto pipe = this->pipe;
45 if (this->promise)
46 {
47 auto promise = this->promise;
48
49 boost::asio::async_write(*pipe, buf,
50 [pipe, promise](const boost::system::error_code & ec, std::size_t)
51 {
52 if (ec && (ec.value() != EBADF) && (ec.value() != EPERM) && (ec.value() != ENOENT))
53 {
54 std::error_code e(ec.value(), std::system_category());
55 promise->set_exception(std::make_exception_ptr(process_error(e)));
56 }
57 else
58 promise->set_value();
59 });
60 }
61 else
62 boost::asio::async_write(*pipe, buf,
63 [pipe](const boost::system::error_code&ec, std::size_t size){});
64
65 std::move(*pipe).source().close();
66
67 this->pipe = nullptr;
68 }
69
70 template<typename Executor>
71 void on_error(Executor &, const std::error_code &) const
72 {
73 std::move(*pipe).source().close();
74 }
75
76 template<typename Executor>
77 void on_setup(Executor & exec)
78 {
79 pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
80 }
81
82 template <typename Executor>
83 void on_exec_setup(Executor &exec)
84 {
85 if (::dup2(pipe->native_source(), STDIN_FILENO) == -1)
86 exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
87
88 ::close(pipe->native_source());
89 }
90 };
91
92
93 }}}}
94
95 #endif