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
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)
10 #ifndef BOOST_PROCESS_POSIX_PIPE_HPP
11 #define BOOST_PROCESS_POSIX_PIPE_HPP
14 #include <boost/filesystem.hpp>
15 #include <boost/process/detail/posix/compare_handles.hpp>
16 #include <system_error>
22 namespace boost { namespace process { namespace detail { namespace posix {
25 template<class CharT, class Traits = std::char_traits<CharT>>
31 explicit basic_pipe(int source, int sink) : _source(source), _sink(sink) {}
32 explicit basic_pipe(int source, int sink, const std::string&) : _source(source), _sink(sink) {}
33 typedef CharT char_type ;
34 typedef Traits traits_type;
35 typedef typename Traits::int_type int_type ;
36 typedef typename Traits::pos_type pos_type ;
37 typedef typename Traits::off_type off_type ;
38 typedef int native_handle_type;
43 if (::pipe(fds) == -1)
44 boost::process::detail::throw_last_error("pipe(2) failed");
49 inline basic_pipe(const basic_pipe& rhs);
50 explicit inline basic_pipe(const std::string& name);
51 basic_pipe(basic_pipe&& lhs) : _source(lhs._source), _sink(lhs._sink)
56 inline basic_pipe& operator=(const basic_pipe& );
57 basic_pipe& operator=(basic_pipe&& lhs)
59 _source = lhs._source;
74 native_handle_type native_source() const {return _source;}
75 native_handle_type native_sink () const {return _sink;}
77 void assign_source(native_handle_type h) { _source = h;}
78 void assign_sink (native_handle_type h) { _sink = h;}
83 int_type write(const char_type * data, int_type count)
86 while ((write_len = ::write(_sink, data, count * sizeof(char_type))) == -1)
88 //Try again if interrupted
91 ::boost::process::detail::throw_last_error();
95 int_type read(char_type * data, int_type count)
98 while ((read_len = ::read(_source, data, count * sizeof(char_type))) == -1)
100 //Try again if interrupted
103 ::boost::process::detail::throw_last_error();
110 return (_source != -1) ||
125 template<class CharT, class Traits>
126 basic_pipe<CharT, Traits>::basic_pipe(const basic_pipe & rhs)
128 if (rhs._source != -1)
130 _source = ::dup(rhs._source);
132 ::boost::process::detail::throw_last_error("dup() failed");
136 _sink = ::dup(rhs._sink);
138 ::boost::process::detail::throw_last_error("dup() failed");
143 template<class CharT, class Traits>
144 basic_pipe<CharT, Traits> &basic_pipe<CharT, Traits>::operator=(const basic_pipe & rhs)
146 if (rhs._source != -1)
148 _source = ::dup(rhs._source);
150 ::boost::process::detail::throw_last_error("dup() failed");
154 _sink = ::dup(rhs._sink);
156 ::boost::process::detail::throw_last_error("dup() failed");
163 template<class CharT, class Traits>
164 basic_pipe<CharT, Traits>::basic_pipe(const std::string & name)
166 auto fifo = mkfifo(name.c_str(), 0666 );
169 boost::process::detail::throw_last_error("mkfifo() failed");
172 int read_fd = open(name.c_str(), O_RDWR);
175 boost::process::detail::throw_last_error();
177 int write_fd = dup(read_fd);
180 boost::process::detail::throw_last_error();
184 ::unlink(name.c_str());
187 template<class Char, class Traits>
188 inline bool operator==(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
190 return compare_handles(lhs.native_source(), rhs.native_source()) &&
191 compare_handles(lhs.native_sink(), rhs.native_sink());
194 template<class Char, class Traits>
195 inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
197 return !compare_handles(lhs.native_source(), rhs.native_source()) ||
198 !compare_handles(lhs.native_sink(), rhs.native_sink());