]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/process/detail/posix/async_out.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / process / detail / posix / async_out.hpp
CommitLineData
b32b8144
FG
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_OUT_HPP
11#define BOOST_PROCESS_DETAIL_POSIX_ASYNC_OUT_HPP
12
13
14#include <boost/process/detail/posix/handler.hpp>
15#include <boost/asio/posix/stream_descriptor.hpp>
16#include <boost/asio/read.hpp>
17#include <boost/process/async_pipe.hpp>
18#include <istream>
19#include <memory>
20#include <exception>
21#include <future>
22
23namespace boost { namespace process { namespace detail { namespace posix {
24
25
26inline int apply_out_handles(int handle, std::integral_constant<int, 1>, std::integral_constant<int, -1>)
27{
28 return ::dup2(handle, STDOUT_FILENO);
29}
30
31inline int apply_out_handles(int handle, std::integral_constant<int, 2>, std::integral_constant<int, -1>)
32{
33 return ::dup2(handle, STDERR_FILENO);
34}
35
36inline int apply_out_handles(int handle, std::integral_constant<int, 1>, std::integral_constant<int, 2>)
37{
38 if (::dup2(handle, STDOUT_FILENO) == -1)
39 return -1;
40 if (::dup2(handle, STDERR_FILENO) == -1)
41 return -1;
42
43 return 0;
44}
45
46template<int p1, int p2, typename Buffer>
47struct async_out_buffer : ::boost::process::detail::posix::handler_base_ext,
48 ::boost::process::detail::posix::require_io_context
49{
50 Buffer & buf;
51
52 std::shared_ptr<boost::process::async_pipe> pipe;
53
54
55 async_out_buffer(Buffer & buf) : buf(buf)
56 {
57 }
58
59 template <typename Executor>
60 inline void on_success(Executor &exec)
61 {
62 auto pipe = this->pipe;
63 boost::asio::async_read(*pipe, buf,
64 [pipe](const boost::system::error_code&, std::size_t size){});
65
66 this->pipe = nullptr;
67 std::move(*pipe).sink().close();
68 }
69
70 template<typename Executor>
71 void on_error(Executor &, const std::error_code &) const
72 {
73 std::move(*pipe).sink().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
83 template <typename Executor>
84 void on_exec_setup(Executor &exec)
85 {
86 int res = apply_out_handles(pipe->native_sink(),
87 std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
88 if (res == -1)
89 exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
90
91 ::close(pipe->native_sink());
11fdf7f2
TL
92 ::close(pipe->native_source());
93
b32b8144
FG
94 }
95};
96
97
98
99
100template<int p1, int p2, typename Type>
101struct async_out_future : ::boost::process::detail::posix::handler_base_ext,
102 ::boost::process::detail::posix::require_io_context
103{
104 std::shared_ptr<std::promise<Type>> promise = std::make_shared<std::promise<Type>>();
105
106 std::shared_ptr<boost::asio::streambuf> buffer = std::make_shared<boost::asio::streambuf>();
107
108 std::shared_ptr<boost::process::async_pipe> pipe;
109
110 async_out_future(std::future<Type> & fut)
111 {
112 fut = promise->get_future();
113 }
114 template <typename Executor>
115 inline void on_success(Executor &exec)
116 {
117 auto pipe = this->pipe;
118
119 auto buffer = this->buffer;
120 auto promise = this->promise;
121
122 boost::asio::async_read(*pipe, *buffer,
123 [pipe, buffer, promise](const boost::system::error_code& ec, std::size_t size)
124 {
125 if (ec && (ec.value() != ENOENT))
126 {
127 std::error_code e(ec.value(), std::system_category());
128 promise->set_exception(std::make_exception_ptr(process_error(e)));
129 }
130 else
131 {
132 std::istream is (buffer.get());
133 Type arg;
134 arg.resize(buffer->size());
135 is.read(&*arg.begin(), buffer->size());
136 promise->set_value(std::move(arg));
137 }
138 });
139
140 std::move(*pipe).sink().close();
141 this->pipe = nullptr;
142 }
143
144 template<typename Executor>
145 void on_error(Executor &, const std::error_code &) const
146 {
147 std::move(*pipe).sink().close();
148 }
149
150 template<typename Executor>
151 void on_setup(Executor & exec)
152 {
153 pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
154 }
155
156 template <typename Executor>
157 void on_exec_setup(Executor &exec)
158 {
159
160 int res = apply_out_handles(pipe->native_sink(),
161 std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
162 if (res == -1)
163 exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
164
165 ::close(pipe->native_sink());
11fdf7f2 166 ::close(pipe->native_source());
b32b8144
FG
167 }
168
169};
170
171}}}}
172
173#endif