basic_pipe.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #ifndef BOOST_PROCESS_POSIX_PIPE_HPP
  10. #define BOOST_PROCESS_POSIX_PIPE_HPP
  11. #include <boost/filesystem.hpp>
  12. #include <boost/process/detail/posix/compare_handles.hpp>
  13. #include <system_error>
  14. #include <array>
  15. #include <unistd.h>
  16. #include <fcntl.h>
  17. #include <memory>
  18. namespace boost { namespace process { namespace detail { namespace posix {
  19. template<class CharT, class Traits = std::char_traits<CharT>>
  20. class basic_pipe
  21. {
  22. int _source = -1;
  23. int _sink = -1;
  24. public:
  25. explicit basic_pipe(int source, int sink) : _source(source), _sink(sink) {}
  26. explicit basic_pipe(int source, int sink, const std::string&) : _source(source), _sink(sink) {}
  27. typedef CharT char_type ;
  28. typedef Traits traits_type;
  29. typedef typename Traits::int_type int_type ;
  30. typedef typename Traits::pos_type pos_type ;
  31. typedef typename Traits::off_type off_type ;
  32. typedef int native_handle_type;
  33. basic_pipe()
  34. {
  35. int fds[2];
  36. if (::pipe(fds) == -1)
  37. boost::process::detail::throw_last_error("pipe(2) failed");
  38. _source = fds[0];
  39. _sink = fds[1];
  40. }
  41. inline basic_pipe(const basic_pipe& rhs);
  42. explicit inline basic_pipe(const std::string& name);
  43. basic_pipe(basic_pipe&& lhs) : _source(lhs._source), _sink(lhs._sink)
  44. {
  45. lhs._source = -1;
  46. lhs._sink = -1;
  47. }
  48. inline basic_pipe& operator=(const basic_pipe& );
  49. basic_pipe& operator=(basic_pipe&& lhs)
  50. {
  51. _source = lhs._source;
  52. _sink = lhs._sink ;
  53. lhs._source = -1;
  54. lhs._sink = -1;
  55. return *this;
  56. }
  57. ~basic_pipe()
  58. {
  59. if (_sink != -1)
  60. ::close(_sink);
  61. if (_source != -1)
  62. ::close(_source);
  63. }
  64. native_handle_type native_source() const {return _source;}
  65. native_handle_type native_sink () const {return _sink;}
  66. void assign_source(native_handle_type h) { _source = h;}
  67. void assign_sink (native_handle_type h) { _sink = h;}
  68. int_type write(const char_type * data, int_type count)
  69. {
  70. int_type write_len;
  71. while ((write_len = ::write(_sink, data, count * sizeof(char_type))) == -1)
  72. {
  73. //Try again if interrupted
  74. auto err = errno;
  75. if (err != EINTR)
  76. ::boost::process::detail::throw_last_error();
  77. }
  78. return write_len;
  79. }
  80. int_type read(char_type * data, int_type count)
  81. {
  82. int_type read_len;
  83. while ((read_len = ::read(_source, data, count * sizeof(char_type))) == -1)
  84. {
  85. //Try again if interrupted
  86. auto err = errno;
  87. if (err != EINTR)
  88. ::boost::process::detail::throw_last_error();
  89. }
  90. return read_len;
  91. }
  92. bool is_open() const
  93. {
  94. return (_source != -1) ||
  95. (_sink != -1);
  96. }
  97. void close()
  98. {
  99. if (_source != -1)
  100. ::close(_source);
  101. if (_sink != -1)
  102. ::close(_sink);
  103. _source = -1;
  104. _sink = -1;
  105. }
  106. };
  107. template<class CharT, class Traits>
  108. basic_pipe<CharT, Traits>::basic_pipe(const basic_pipe & rhs)
  109. {
  110. if (rhs._source != -1)
  111. {
  112. _source = ::dup(rhs._source);
  113. if (_source == -1)
  114. ::boost::process::detail::throw_last_error("dup() failed");
  115. }
  116. if (rhs._sink != -1)
  117. {
  118. _sink = ::dup(rhs._sink);
  119. if (_sink == -1)
  120. ::boost::process::detail::throw_last_error("dup() failed");
  121. }
  122. }
  123. template<class CharT, class Traits>
  124. basic_pipe<CharT, Traits> &basic_pipe<CharT, Traits>::operator=(const basic_pipe & rhs)
  125. {
  126. if (rhs._source != -1)
  127. {
  128. _source = ::dup(rhs._source);
  129. if (_source == -1)
  130. ::boost::process::detail::throw_last_error("dup() failed");
  131. }
  132. if (rhs._sink != -1)
  133. {
  134. _sink = ::dup(rhs._sink);
  135. if (_sink == -1)
  136. ::boost::process::detail::throw_last_error("dup() failed");
  137. }
  138. return *this;
  139. }
  140. template<class CharT, class Traits>
  141. basic_pipe<CharT, Traits>::basic_pipe(const std::string & name)
  142. {
  143. auto fifo = mkfifo(name.c_str(), 0666 );
  144. if (fifo != 0)
  145. boost::process::detail::throw_last_error("mkfifo() failed");
  146. int read_fd = open(name.c_str(), O_RDWR);
  147. if (read_fd == -1)
  148. boost::process::detail::throw_last_error();
  149. int write_fd = dup(read_fd);
  150. if (write_fd == -1)
  151. boost::process::detail::throw_last_error();
  152. _sink = write_fd;
  153. _source = read_fd;
  154. ::unlink(name.c_str());
  155. }
  156. template<class Char, class Traits>
  157. inline bool operator==(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
  158. {
  159. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  160. compare_handles(lhs.native_sink(), rhs.native_sink());
  161. }
  162. template<class Char, class Traits>
  163. inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
  164. {
  165. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  166. !compare_handles(lhs.native_sink(), rhs.native_sink());
  167. }
  168. }}}}
  169. #endif