process.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include "process.h"
  2. #include "procutil.h"
  3. #include "../../../std/async/native/async.h"
  4. struct bbProcess::Rep{
  5. struct FinishedEvent : public bbAsync::Event{
  6. Rep *rep;
  7. bbFunction<void()> func;
  8. void dispatch(){
  9. func();
  10. rep->release();
  11. }
  12. };
  13. struct StdoutEvent : public bbAsync::Event{
  14. Rep *rep;
  15. bbFunction<void()> func;
  16. void dispatch(){
  17. bool release=rep->stdoutAvail==0;
  18. func();
  19. if( release ) rep->release();
  20. }
  21. };
  22. std::atomic_int refs;
  23. bbAsync::Semaphore stdoutSema;
  24. char stdoutBuf[4096];
  25. char *stdoutGet;
  26. int stdoutAvail=0;
  27. bool terminated=false;
  28. int exit;
  29. FinishedEvent finishedEvent;
  30. StdoutEvent stdoutEvent;
  31. #if _WIN32
  32. HANDLE proc;
  33. HANDLE in;
  34. HANDLE out;
  35. HANDLE err;
  36. HANDLE breakEvent;
  37. Rep( HANDLE proc,HANDLE in,HANDLE out,HANDLE err,HANDLE breakEvent ):proc( proc ),in( in ),out( out ),err( err ),breakEvent( breakEvent ),exit( -1 ),refs( 1 ){
  38. }
  39. void close(){
  40. CloseHandle( in );
  41. CloseHandle( out );
  42. CloseHandle( err );
  43. }
  44. #else
  45. int proc;
  46. int in;
  47. int out;
  48. int err;
  49. Rep( int proc,int in,int out,int err ):proc( proc ),in( in ),out( out ),err( err ),exit( -1 ),refs( 1 ){
  50. }
  51. void close(){
  52. ::close( in );
  53. ::close( out );
  54. ::close( err );
  55. }
  56. #endif
  57. void retain(){
  58. ++refs;
  59. }
  60. void release(){
  61. if( --refs ) return;
  62. close();
  63. delete this;
  64. }
  65. };
  66. bbProcess::bbProcess():_rep( nullptr ){
  67. }
  68. bbProcess::~bbProcess(){
  69. if( _rep ) _rep->release();
  70. }
  71. bbBool bbProcess::start( bbString cmd ){
  72. if( _rep ) return false;
  73. #if _WIN32
  74. HANDLE in[2],out[2],err[2];
  75. SECURITY_ATTRIBUTES sa={sizeof(sa),0,1};
  76. CreatePipe( &in[0],&in[1],&sa,0 );
  77. CreatePipe( &out[0],&out[1],&sa,0 );
  78. CreatePipe( &err[0],&err[1],&sa,0 );
  79. HANDLE breakEvent=CreateEvent( &sa,0,0,"MX2_BREAK_EVENT" );
  80. STARTUPINFOA si={sizeof(si)};
  81. si.dwFlags=STARTF_USESTDHANDLES;
  82. si.hStdInput=in[0];
  83. si.hStdOutput=out[1];
  84. si.hStdError=err[1];
  85. PROCESS_INFORMATION pi={0};
  86. DWORD flags=CREATE_NEW_PROCESS_GROUP|CREATE_NO_WINDOW;
  87. int res=CreateProcessA( 0,(LPSTR)cmd.c_str(),0,0,TRUE,flags,0,0,&si,&pi );
  88. CloseHandle( in[0] );
  89. CloseHandle( out[1] );
  90. CloseHandle( err[1] );
  91. if( !res ){
  92. CloseHandle( in[1] );
  93. CloseHandle( out[0] );
  94. CloseHandle( err[0] );
  95. return false;
  96. }
  97. CloseHandle( pi.hThread );
  98. Rep *rep=new Rep( pi.hProcess,in[1],out[0],err[0],breakEvent );
  99. #else
  100. int in[2],out[2],err[2];
  101. pipe( in );
  102. pipe( out );
  103. pipe( err );
  104. char **argv=bbProcUtil::makeargv( bbCString( cmd ) );
  105. bool failed=false;
  106. int proc=vfork();
  107. if( !proc ){
  108. #if __linux
  109. setsid();
  110. #else
  111. setpgid(0,0);
  112. #endif
  113. dup2( in[0],0 );
  114. dup2( out[1],1 );
  115. dup2( err[1],2 );
  116. execvp( argv[0],argv );
  117. failed=true;
  118. _exit( 127 );
  119. }
  120. if( failed ) proc=-1;
  121. close( in[0] );
  122. close( out[1] );
  123. close( err[1] );
  124. if( proc==-1 ){
  125. close( in[1] );
  126. close( out[0] );
  127. close( err[0] );
  128. return false;
  129. }
  130. Rep *rep=new Rep( proc,in[1],out[0],err[0] );
  131. #endif
  132. //Create finished thread
  133. rep->retain();
  134. rep->finishedEvent.rep=rep;
  135. rep->finishedEvent.func=finished;
  136. std::thread( [=](){
  137. #if _WIN32
  138. WaitForSingleObject( rep->proc,INFINITE );
  139. GetExitCodeProcess( rep->proc,(DWORD*)&rep->exit );
  140. CloseHandle( rep->breakEvent );
  141. CloseHandle( rep->proc );
  142. #else
  143. int status;
  144. waitpid( rep->proc,&status,0 );
  145. if( WIFEXITED( status ) ){
  146. rep->exit=WEXITSTATUS( status );
  147. }else{
  148. rep->exit=-1;
  149. }
  150. #endif
  151. rep->finishedEvent.post();
  152. } ).detach();
  153. // Create stdoutReady thread
  154. //
  155. rep->retain();
  156. rep->stdoutEvent.rep=rep;
  157. rep->stdoutEvent.func=stdoutReady;
  158. std::thread( [=](){
  159. for(;;){
  160. #if _WIN32
  161. DWORD n=0;
  162. if( !ReadFile( rep->out,rep->stdoutBuf,4096,&n,0 ) ) break;
  163. if( n<=0 ) break;
  164. #else
  165. int n=read( rep->out,rep->stdoutBuf,4096 );
  166. if( n<=0 ) break;
  167. #endif
  168. rep->stdoutGet=rep->stdoutBuf;
  169. rep->stdoutAvail=n;
  170. rep->stdoutEvent.post();
  171. rep->stdoutSema.wait();
  172. if( rep->stdoutAvail ) break;
  173. }
  174. rep->stdoutAvail=0;
  175. rep->stdoutEvent.post();
  176. } ).detach();
  177. _rep=rep;
  178. return true;
  179. }
  180. int bbProcess::exitCode(){
  181. if( !_rep ) return -1;
  182. return _rep->exit;
  183. }
  184. bbInt bbProcess::stdoutAvail(){
  185. if( !_rep ) return 0;
  186. return _rep->stdoutAvail;
  187. }
  188. bbString bbProcess::readStdout(){
  189. if( !_rep || !_rep->stdoutAvail ) return "";
  190. bbString str=bbString::fromCString( _rep->stdoutGet,_rep->stdoutAvail );
  191. _rep->stdoutAvail=0;
  192. _rep->stdoutSema.signal();
  193. return str;
  194. }
  195. bbInt bbProcess::readStdout( void *buf,int count ){
  196. if( !_rep || count<=0 || !_rep->stdoutAvail ) return 0;
  197. if( count>_rep->stdoutAvail ) count=_rep->stdoutAvail;
  198. memcpy( buf,_rep->stdoutGet,count );
  199. _rep->stdoutGet+=count;
  200. _rep->stdoutAvail-=count;
  201. if( !_rep->stdoutAvail ) _rep->stdoutSema.signal();
  202. return count;
  203. }
  204. int bbProcess::writeStdin( bbString str ){
  205. if( !_rep ) return 0;
  206. #if _WIN32
  207. int n=WriteFile( _rep->in,str.c_str(),str.length(),0,0 );
  208. #else
  209. int n=write( _rep->in,str.c_str(),str.length() );
  210. #endif
  211. return n>=0 ? n : 0;
  212. }
  213. int bbProcess::writeStdin( void *buf,int count ){
  214. if( !_rep ) return 0;
  215. #if _WIN32
  216. int n=WriteFile( _rep->in,buf,count,0,0 );
  217. #else
  218. int n=write( _rep->in,buf,count );
  219. #endif
  220. return n>=0 ? n : 0;
  221. }
  222. void bbProcess::sendBreak(){
  223. if( !_rep ) return;
  224. #if _WIN32
  225. SetEvent( _rep->breakEvent );
  226. #else
  227. killpg( _rep->proc,SIGTSTP );
  228. #endif
  229. }
  230. void bbProcess::terminate(){
  231. if( !_rep ) return;
  232. #if _WIN32
  233. bbProcUtil::TerminateProcessGroup( _rep->proc,-1 );
  234. CancelIoEx( _rep->out,0 );
  235. #else
  236. killpg( _rep->proc,SIGTERM );
  237. #endif
  238. }
  239. void bbProcess::gcMark(){
  240. bbGCMark( finished );
  241. bbGCMark( stdoutReady );
  242. if( !_rep ) return;
  243. bbGCMark( _rep->finishedEvent.func );
  244. bbGCMark( _rep->stdoutEvent.func );
  245. }