basic_pipe.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. auto write_len = ::write(_sink, data, count * sizeof(char_type));
  71. if (write_len == -1)
  72. ::boost::process::detail::throw_last_error();
  73. return write_len;
  74. }
  75. int_type read(char_type * data, int_type count)
  76. {
  77. auto read_len = ::read(_source, data, count * sizeof(char_type));
  78. if (read_len == -1)
  79. ::boost::process::detail::throw_last_error();
  80. return read_len;
  81. }
  82. bool is_open() const
  83. {
  84. return (_source != -1) ||
  85. (_sink != -1);
  86. }
  87. void close()
  88. {
  89. if (_source != -1)
  90. ::close(_source);
  91. if (_sink != -1)
  92. ::close(_sink);
  93. _source = -1;
  94. _sink = -1;
  95. }
  96. };
  97. template<class CharT, class Traits>
  98. basic_pipe<CharT, Traits>::basic_pipe(const basic_pipe & rhs)
  99. {
  100. if (rhs._source != -1)
  101. {
  102. _source = ::dup(rhs._source);
  103. if (_source == -1)
  104. ::boost::process::detail::throw_last_error("dup() failed");
  105. }
  106. if (rhs._sink != -1)
  107. {
  108. _sink = ::dup(rhs._sink);
  109. if (_sink == -1)
  110. ::boost::process::detail::throw_last_error("dup() failed");
  111. }
  112. }
  113. template<class CharT, class Traits>
  114. basic_pipe<CharT, Traits> &basic_pipe<CharT, Traits>::operator=(const basic_pipe & rhs)
  115. {
  116. if (rhs._source != -1)
  117. {
  118. _source = ::dup(rhs._source);
  119. if (_source == -1)
  120. ::boost::process::detail::throw_last_error("dup() failed");
  121. }
  122. if (rhs._sink != -1)
  123. {
  124. _sink = ::dup(rhs._sink);
  125. if (_sink == -1)
  126. ::boost::process::detail::throw_last_error("dup() failed");
  127. }
  128. return *this;
  129. }
  130. template<class CharT, class Traits>
  131. basic_pipe<CharT, Traits>::basic_pipe(const std::string & name)
  132. {
  133. auto fifo = mkfifo(name.c_str(), 0666 );
  134. if (fifo != 0)
  135. boost::process::detail::throw_last_error("mkfifo() failed");
  136. int read_fd = open(name.c_str(), O_RDWR);
  137. if (read_fd == -1)
  138. boost::process::detail::throw_last_error();
  139. int write_fd = dup(read_fd);
  140. if (write_fd == -1)
  141. boost::process::detail::throw_last_error();
  142. _sink = write_fd;
  143. _source = read_fd;
  144. ::unlink(name.c_str());
  145. }
  146. template<class Char, class Traits>
  147. inline bool operator==(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
  148. {
  149. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  150. compare_handles(lhs.native_sink(), rhs.native_sink());
  151. }
  152. template<class Char, class Traits>
  153. inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
  154. {
  155. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  156. !compare_handles(lhs.native_sink(), rhs.native_sink());
  157. }
  158. }}}}
  159. #endif