exec-stream-impl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. Copyright (C) 2004 Artem Khodush
  3. Redistribution and use in source and binary forms, with or without modification,
  4. are permitted provided that the following conditions are met:
  5. 1. Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. 2. Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. 3. The name of the author may not be used to endorse or promote products
  11. derived from this software without specific prior written permission.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  13. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  18. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  19. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  20. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  21. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. using namespace helpers;
  24. // exec_stream_t::impl_t
  25. struct exec_stream_t::impl_t {
  26. impl_t();
  27. HANDLE m_child_process;
  28. HANDLE m_in_pipe;
  29. HANDLE m_out_pipe;
  30. HANDLE m_err_pipe;
  31. thread_buffer_t m_in_thread;
  32. thread_buffer_t m_out_thread;
  33. thread_buffer_t m_err_thread;
  34. exec_stream_buffer_t m_in_buffer;
  35. exec_stream_buffer_t m_out_buffer;
  36. exec_stream_buffer_t m_err_buffer;
  37. exec_ostream_t m_in;
  38. exec_istream_t m_out;
  39. exec_istream_t m_err;
  40. DWORD m_child_timeout;
  41. int m_exit_code;
  42. };
  43. exec_stream_t::impl_t::impl_t()
  44. : m_in_buffer( exec_stream_t::s_in, m_in_thread ), m_out_buffer( exec_stream_t::s_out, m_out_thread ), m_err_buffer( exec_stream_t::s_err, m_err_thread ),
  45. m_in( m_in_buffer ), m_out( m_out_buffer ), m_err( m_err_buffer )
  46. {
  47. m_out.tie( &m_in );
  48. m_err.tie( &m_in );
  49. m_child_process=0;
  50. m_in_pipe=0;
  51. m_out_pipe=0;
  52. m_err_pipe=0;
  53. m_child_timeout=500;
  54. m_exit_code=0;
  55. }
  56. void exec_stream_t::set_buffer_limit( int stream_kind, std::size_t size )
  57. {
  58. if( stream_kind&s_in ) {
  59. m_impl->m_in_thread.set_buffer_limit( size );
  60. }
  61. if( stream_kind&s_out ) {
  62. m_impl->m_out_thread.set_buffer_limit( size );
  63. }
  64. if( stream_kind&s_err ) {
  65. m_impl->m_err_thread.set_buffer_limit( size );
  66. }
  67. }
  68. void exec_stream_t::set_wait_timeout( int stream_kind, exec_stream_t::timeout_t milliseconds )
  69. {
  70. if( stream_kind&s_in ) {
  71. m_impl->m_in_thread.set_wait_timeout( milliseconds );
  72. }
  73. if( stream_kind&s_out ) {
  74. m_impl->m_out_thread.set_wait_timeout( milliseconds );
  75. }
  76. if( stream_kind&s_err ) {
  77. m_impl->m_err_thread.set_wait_timeout( milliseconds );
  78. }
  79. if( stream_kind&s_child ) {
  80. m_impl->m_child_timeout=milliseconds;
  81. m_impl->m_in_thread.set_thread_termination_timeout( milliseconds );
  82. m_impl->m_out_thread.set_thread_termination_timeout( milliseconds );
  83. m_impl->m_err_thread.set_thread_termination_timeout( milliseconds );
  84. }
  85. }
  86. void exec_stream_t::set_binary_mode( int stream_kind )
  87. {
  88. if( stream_kind&s_in ) {
  89. m_impl->m_in_thread.set_binary_mode();
  90. }
  91. if( stream_kind&s_out ) {
  92. m_impl->m_out_thread.set_binary_mode();
  93. }
  94. if( stream_kind&s_err ) {
  95. m_impl->m_err_thread.set_binary_mode();
  96. }
  97. }
  98. void exec_stream_t::set_text_mode( int stream_kind )
  99. {
  100. if( stream_kind&s_in ) {
  101. m_impl->m_in_thread.set_text_mode();
  102. }
  103. if( stream_kind&s_out ) {
  104. m_impl->m_out_thread.set_text_mode();
  105. }
  106. if( stream_kind&s_err ) {
  107. m_impl->m_err_thread.set_text_mode();
  108. }
  109. }
  110. void exec_stream_t::start( std::string const & program, std::string const & arguments )
  111. {
  112. if( !close() ) {
  113. throw exec_stream_t::error_t( "exec_stream_t::start: previous child process has not yet terminated" );
  114. }
  115. pipe_t in;
  116. pipe_t out;
  117. pipe_t err;
  118. set_stdhandle_t set_in( STD_INPUT_HANDLE, in.r() );
  119. set_stdhandle_t set_out( STD_OUTPUT_HANDLE, out.w() );
  120. set_stdhandle_t set_err( STD_ERROR_HANDLE, err.w() );
  121. HANDLE cp=GetCurrentProcess();
  122. if( !DuplicateHandle( cp, in.w(), cp, &m_impl->m_in_pipe, 0, FALSE, DUPLICATE_SAME_ACCESS ) ) {
  123. throw os_error_t( "exec_stream_t::start: unable to duplicate in handle" );
  124. }
  125. in.close_w();
  126. if( !DuplicateHandle( cp, out.r(), cp, &m_impl->m_out_pipe, 0, FALSE, DUPLICATE_SAME_ACCESS ) ) {
  127. throw os_error_t( "exec_stream_t::start: unable to duplicate out handle" );
  128. }
  129. out.close_r();
  130. if( !DuplicateHandle( cp, err.r(), cp, &m_impl->m_err_pipe, 0, FALSE, DUPLICATE_SAME_ACCESS ) ) {
  131. throw os_error_t( "exec_stream_t::start: unable to duplicate err handle" );
  132. }
  133. err.close_r();
  134. std::string command;
  135. command.reserve( program.size()+arguments.size()+3 );
  136. if( program.find_first_of( " \t" )!=std::string::npos ) {
  137. command+='"';
  138. command+=program;
  139. command+='"';
  140. }else
  141. command=program;
  142. if( arguments.size()!=0 ) {
  143. command+=' ';
  144. command+=arguments;
  145. }
  146. STARTUPINFO si;
  147. ZeroMemory( &si, sizeof( si ) );
  148. si.cb=sizeof( si );
  149. PROCESS_INFORMATION pi;
  150. ZeroMemory( &pi, sizeof( pi ) );
  151. if( !CreateProcess( 0, const_cast< char * >( command.c_str() ), 0, 0, TRUE, 0, 0, 0, &si, &pi ) ) {
  152. throw os_error_t( "exec_stream_t::start: CreateProcess failed.\n command line was: "+command );
  153. }
  154. m_impl->m_child_process=pi.hProcess;
  155. m_impl->m_in_buffer.clear();
  156. m_impl->m_out_buffer.clear();
  157. m_impl->m_err_buffer.clear();
  158. m_impl->m_in.clear();
  159. m_impl->m_out.clear();
  160. m_impl->m_err.clear();
  161. m_impl->m_out_thread.set_read_buffer_size( STREAM_BUFFER_SIZE );
  162. m_impl->m_out_thread.start_reader_thread( m_impl->m_out_pipe );
  163. m_impl->m_err_thread.set_read_buffer_size( STREAM_BUFFER_SIZE );
  164. m_impl->m_err_thread.start_reader_thread( m_impl->m_err_pipe );
  165. m_impl->m_in_thread.start_writer_thread( m_impl->m_in_pipe );
  166. }
  167. void exec_stream_t::start( std::string const & program, exec_stream_t::next_arg_t & next_arg )
  168. {
  169. std::string arguments;
  170. while( std::string const * arg=next_arg.next() ) {
  171. if( arg->find_first_of( " \t\"" )!=std::string::npos ) {
  172. arguments+=" \"";
  173. std::string::size_type cur=0;
  174. while( cur<arg->size() ) {
  175. std::string::size_type next=arg->find( '"', cur );
  176. if( next==std::string::npos ) {
  177. next=arg->size();
  178. arguments.append( *arg, cur, next-cur );
  179. cur=next;
  180. }else {
  181. arguments.append( *arg, cur, next-cur );
  182. arguments+="\\\"";
  183. cur=next+1;
  184. }
  185. }
  186. arguments+="\"";
  187. }else {
  188. arguments+=" "+*arg;
  189. }
  190. }
  191. start( program, arguments );
  192. }
  193. bool exec_stream_t::close_in()
  194. {
  195. if( m_impl->m_in_pipe!=0 ) {
  196. m_impl->m_in.flush();
  197. // stop writer thread before closing the handle it writes to,
  198. // the thread will attempt to write anything it can and close child's stdin
  199. // before thread_termination_timeout elapses
  200. if( m_impl->m_in_thread.stop_thread() ) {
  201. m_impl->m_in_pipe=0;
  202. return true;
  203. }else {
  204. return false;
  205. }
  206. }else {
  207. return true;
  208. }
  209. }
  210. bool exec_stream_t::close()
  211. {
  212. if( !close_in() ) {
  213. // need to close child's stdin no matter what, because otherwise "usual" child will run forever
  214. // And before closing child's stdin the writer thread should be stopped no matter what,
  215. // because it may be blocked on Write to m_in_pipe, and in that case closing m_in_pipe may block.
  216. if( !m_impl->m_in_thread.abort_thread() ) {
  217. throw exec_stream_t::error_t( "exec_stream_t::close: waiting till in_thread stops exceeded timeout" );
  218. }
  219. // when thread is terminated abnormally, it may left child's stdin open
  220. // try to close it here
  221. CloseHandle( m_impl->m_in_pipe );
  222. m_impl->m_in_pipe=0;
  223. }
  224. if( !m_impl->m_out_thread.stop_thread() ) {
  225. if( !m_impl->m_out_thread.abort_thread() ) {
  226. throw exec_stream_t::error_t( "exec_stream_t::close: waiting till out_thread stops exceeded timeout" );
  227. }
  228. }
  229. if( !m_impl->m_err_thread.stop_thread() ) {
  230. if( !m_impl->m_err_thread.abort_thread() ) {
  231. throw exec_stream_t::error_t( "exec_stream_t::close: waiting till err_thread stops exceeded timeout" );
  232. }
  233. }
  234. if( m_impl->m_out_pipe!=0 ) {
  235. if( !CloseHandle( m_impl->m_out_pipe ) ) {
  236. throw os_error_t( "exec_stream_t::close: unable to close out_pipe handle" );
  237. }
  238. m_impl->m_out_pipe=0;
  239. }
  240. if( m_impl->m_err_pipe!=0 ) {
  241. if( !CloseHandle( m_impl->m_err_pipe ) ) {
  242. throw os_error_t( "exec_stream_t::close: unable to close err_pipe handle" );
  243. }
  244. m_impl->m_err_pipe=0;
  245. }
  246. if( m_impl->m_child_process!=0 ) {
  247. wait_result_t wait_result=wait( m_impl->m_child_process, m_impl->m_child_timeout );
  248. if( !wait_result.ok() & !wait_result.timed_out() ) {
  249. throw os_error_t( std::string( "exec_stream_t::close: wait for child process failed. " )+wait_result.error_message() );
  250. }
  251. if( wait_result.ok() ) {
  252. DWORD exit_code;
  253. if( !GetExitCodeProcess( m_impl->m_child_process, &exit_code ) ) {
  254. throw os_error_t( "exec_stream_t::close: unable to get process exit code" );
  255. }
  256. m_impl->m_exit_code=exit_code;
  257. if( !CloseHandle( m_impl->m_child_process ) ) {
  258. throw os_error_t( "exec_stream_t::close: unable to close child process handle" );
  259. }
  260. m_impl->m_child_process=0;
  261. }
  262. }
  263. return m_impl->m_child_process==0;
  264. }
  265. void exec_stream_t::kill()
  266. {
  267. if( m_impl->m_child_process!=0 ) {
  268. if( !TerminateProcess( m_impl->m_child_process, 0 ) ) {
  269. throw os_error_t( "exec_stream_t::kill: unable to terminate child process" );
  270. }
  271. m_impl->m_exit_code=0;
  272. if( !CloseHandle( m_impl->m_child_process ) ) {
  273. throw os_error_t( "exec_stream_t::close: unable to close child process handle" );
  274. }
  275. m_impl->m_child_process=0;
  276. }
  277. }
  278. int exec_stream_t::exit_code()
  279. {
  280. if( m_impl->m_child_process!=0 ) {
  281. throw exec_stream_t::error_t( "exec_stream_t:exit_code: child process still running" );
  282. }
  283. return m_impl->m_exit_code;
  284. }