process.cpp 6.1 KB

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