]>
Commit | Line | Data |
---|---|---|
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_POSIX_PIPE_IN_HPP | |
11 | #define BOOST_PROCESS_POSIX_PIPE_IN_HPP | |
12 | ||
13 | #include <boost/process/pipe.hpp> | |
14 | #include <boost/process/detail/posix/handler.hpp> | |
15 | #include <unistd.h> | |
16 | ||
17 | ||
18 | namespace boost { namespace process { namespace detail { namespace posix { | |
19 | ||
20 | struct pipe_in : handler_base_ext | |
21 | { | |
22 | int source; | |
23 | int sink; //opposite end | |
24 | ||
25 | pipe_in(int sink, int source) : source(source), sink(sink) {} | |
26 | ||
27 | ||
28 | template<typename T> | |
29 | pipe_in(T & p) : source(p.native_source()), sink(p.native_sink()) | |
30 | { | |
31 | p.assign_source(-1); | |
32 | } | |
33 | ||
34 | template<typename Executor> | |
35 | void on_error(Executor &, const std::error_code &) const | |
36 | { | |
37 | ::close(source); | |
38 | } | |
39 | ||
40 | template<typename Executor> | |
41 | void on_success(Executor &) const | |
42 | { | |
43 | ::close(source); | |
44 | } | |
45 | ||
46 | template <class Executor> | |
47 | void on_exec_setup(Executor &e) const | |
48 | { | |
49 | if (::dup2(source, STDIN_FILENO) == -1) | |
50 | e.set_error(::boost::process::detail::get_last_error(), "dup2() failed"); | |
51 | ::close(source); | |
52 | ::close(sink); | |
53 | } | |
54 | ||
55 | }; | |
56 | ||
57 | class async_pipe; | |
58 | ||
59 | struct async_pipe_in : public pipe_in | |
60 | { | |
61 | async_pipe &pipe; | |
62 | ||
63 | template<typename AsyncPipe> | |
64 | async_pipe_in(AsyncPipe & p) : pipe_in(p.native_sink(), p.native_source()), pipe(p) | |
65 | { | |
66 | } | |
67 | ||
68 | template<typename Pipe, typename Executor> | |
69 | static void close(Pipe & pipe, Executor &) | |
70 | { | |
71 | boost::system::error_code ec; | |
72 | std::move(pipe).source().close(ec); | |
73 | } | |
74 | ||
75 | template<typename Executor> | |
76 | void on_error(Executor & exec, const std::error_code &) | |
77 | { | |
78 | close(pipe, exec); | |
79 | } | |
80 | ||
81 | template<typename Executor> | |
82 | void on_success(Executor &exec) | |
83 | { | |
84 | close(pipe, exec); | |
85 | } | |
86 | }; | |
87 | ||
88 | }}}} | |
89 | ||
90 | #endif |