ProcessPosix.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Util/Process.h>
  6. #include <AnKi/Util/Functions.h>
  7. #include <sys/wait.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. namespace anki
  12. {
  13. Process::~Process()
  14. {
  15. ProcessStatus s;
  16. const Error err = getStatus(s);
  17. if(err)
  18. {
  19. ANKI_UTIL_LOGE("Failed to get the status. Expect cleanup errors");
  20. return;
  21. }
  22. if(s == ProcessStatus::RUNNING)
  23. {
  24. ANKI_UTIL_LOGE("Destroying while child process is running");
  25. return;
  26. }
  27. destroyPipes();
  28. }
  29. void Process::destroyPipes()
  30. {
  31. for(U32 i = 0; i < 2; ++i)
  32. {
  33. close(m_stdoutPipe[i]);
  34. close(m_stderrPipe[i]);
  35. m_stdoutPipe[i] = m_stderrPipe[i] = -1;
  36. }
  37. }
  38. Error Process::createPipes()
  39. {
  40. destroyPipes();
  41. if(pipe(m_stdoutPipe.getBegin()) || pipe(m_stderrPipe.getBegin()))
  42. {
  43. ANKI_UTIL_LOGE("pipe() failed: %s", strerror(errno));
  44. return Error::FUNCTION_FAILED;
  45. }
  46. fcntl(m_stdoutPipe[0], F_SETFL, O_NONBLOCK);
  47. fcntl(m_stderrPipe[0], F_SETFL, O_NONBLOCK);
  48. return Error::NONE;
  49. }
  50. Error Process::start(CString executable, ConstWeakArray<CString> arguments, ConstWeakArray<CString> environment)
  51. {
  52. ANKI_CHECK(refresh(0));
  53. if(m_status == ProcessStatus::RUNNING)
  54. {
  55. ANKI_UTIL_LOGE("Process already running");
  56. return Error::USER_DATA;
  57. }
  58. // Create the pipes
  59. ANKI_CHECK(createPipes());
  60. // Fork
  61. m_pid = fork();
  62. if(m_pid < 0)
  63. {
  64. ANKI_UTIL_LOGE("fork() failed: %s", strerror(errno));
  65. }
  66. if(m_pid == 0)
  67. {
  68. // Child
  69. dup2(m_stdoutPipe[1], 1);
  70. close(m_stdoutPipe[0]);
  71. m_stdoutPipe[0] = -1;
  72. dup2(m_stderrPipe[1], 2);
  73. close(m_stderrPipe[0]);
  74. m_stderrPipe[0] = -1;
  75. // Set the args
  76. char** cargs = static_cast<char**>(malloc((arguments.getSize() + 2) * sizeof(char*)));
  77. cargs[0] = static_cast<char*>(malloc(executable.getLength() + 1));
  78. strcpy(cargs[0], executable.cstr());
  79. U32 i = 0;
  80. for(; i < arguments.getSize(); ++i)
  81. {
  82. cargs[i + 1] = static_cast<char*>(malloc(arguments[i].getLength() + 1));
  83. strcpy(cargs[i + 1], arguments[i].cstr());
  84. }
  85. cargs[i + 1] = nullptr;
  86. // Set the env
  87. for(U32 i = 0; i < environment.getSize(); i += 2)
  88. {
  89. setenv(environment[i].cstr(), environment[i + 1].cstr(), 1);
  90. }
  91. // Execute file
  92. const int execerror = execvp(executable.cstr(), cargs);
  93. if(execerror)
  94. {
  95. printf("execvp() failed: %s", strerror(errno));
  96. exit(1);
  97. }
  98. }
  99. else
  100. {
  101. close(m_stdoutPipe[1]);
  102. m_stdoutPipe[1] = -1;
  103. close(m_stderrPipe[1]);
  104. m_stderrPipe[1] = -1;
  105. }
  106. return Error::NONE;
  107. }
  108. Error Process::kill(ProcessKillSignal k)
  109. {
  110. if(m_pid < 1)
  111. {
  112. return Error::NONE;
  113. }
  114. int sig;
  115. switch(k)
  116. {
  117. case ProcessKillSignal::NORMAL:
  118. sig = SIGTERM;
  119. break;
  120. case ProcessKillSignal::FORCE:
  121. sig = SIGKILL;
  122. break;
  123. default:
  124. ANKI_ASSERT(0);
  125. sig = 0;
  126. };
  127. const pid_t p = ::kill(m_pid, sig);
  128. if(p != 0)
  129. {
  130. ANKI_UTIL_LOGE("kill() failed: %s", strerror(errno));
  131. return Error::FUNCTION_FAILED;
  132. }
  133. return Error::NONE;
  134. }
  135. Error Process::readFromFd(int fd, StringAuto& text) const
  136. {
  137. fd_set readfds;
  138. FD_ZERO(&readfds);
  139. FD_SET(fd, &readfds);
  140. timeval timeout;
  141. timeout.tv_sec = 0; // Seconds
  142. timeout.tv_usec = 10; // Microseconds
  143. // Limit the iterations in case the process writes to FD constantly
  144. U32 maxIterations = 16;
  145. while(maxIterations--)
  146. {
  147. // Check if there are data
  148. const int sel = select(1 + fd, &readfds, nullptr, nullptr, &timeout);
  149. if(sel == 0)
  150. {
  151. // Timeout expired
  152. break;
  153. }
  154. else if(sel == -1)
  155. {
  156. ANKI_UTIL_LOGE("select() failed: %s", strerror(errno));
  157. return Error::FUNCTION_FAILED;
  158. }
  159. // Read the data
  160. Array<char, 1024> buff;
  161. const ssize_t bytesCount = read(fd, buff.getBegin(), buff.getSize() - 1);
  162. if(bytesCount < 0 && errno != EAGAIN)
  163. {
  164. // NOTE errno == EINTR if a non-fatal signal arrived
  165. ANKI_UTIL_LOGE("read() failed: %s", strerror(errno));
  166. return Error::FUNCTION_FAILED;
  167. }
  168. else if(bytesCount == 0)
  169. {
  170. break;
  171. }
  172. else
  173. {
  174. buff[min<U32>(U32(bytesCount), buff.getSize() - 1)] = '\0';
  175. text.append(&buff[0]);
  176. }
  177. }
  178. return Error::NONE;
  179. }
  180. Error Process::readFromStdout(StringAuto& text)
  181. {
  182. return readFromFd(m_stdoutPipe[0], text);
  183. }
  184. Error Process::readFromStderr(StringAuto& text)
  185. {
  186. return readFromFd(m_stderrPipe[0], text);
  187. }
  188. Error Process::getStatus(ProcessStatus& status)
  189. {
  190. ANKI_CHECK(refresh(WNOHANG));
  191. status = m_status;
  192. return Error::NONE;
  193. }
  194. Error Process::refresh(int waitpidOptions)
  195. {
  196. Error err = Error::NONE;
  197. m_status = ProcessStatus::NOT_RUNNING;
  198. m_exitCode = DEFAULT_EXIT_CODE;
  199. if(m_pid == -1)
  200. {
  201. m_status = ProcessStatus::NOT_RUNNING;
  202. m_exitCode = DEFAULT_EXIT_CODE;
  203. }
  204. else
  205. {
  206. int status;
  207. const pid_t p = waitpid(m_pid, &status, waitpidOptions);
  208. if(p == -1)
  209. {
  210. m_status = ProcessStatus::NOT_RUNNING;
  211. m_exitCode = DEFAULT_EXIT_CODE;
  212. m_pid = -1;
  213. ANKI_UTIL_LOGE("waitpid() failed: %s", strerror(errno));
  214. err = Error::FUNCTION_FAILED;
  215. }
  216. else if(p == 0)
  217. {
  218. m_status = ProcessStatus::RUNNING;
  219. m_exitCode = DEFAULT_EXIT_CODE;
  220. }
  221. else if(WIFEXITED(status))
  222. {
  223. m_status = ProcessStatus::NORMAL_EXIT;
  224. m_exitCode = WEXITSTATUS(status);
  225. m_pid = -1;
  226. }
  227. else if(WIFSIGNALED(status))
  228. {
  229. m_status = ProcessStatus::CRASH_EXIT;
  230. m_exitCode = WTERMSIG(status);
  231. m_pid = -1;
  232. }
  233. else if(WIFSTOPPED(status))
  234. {
  235. // NOTE child may resume later (and become a zombie)
  236. m_status = ProcessStatus::CRASH_EXIT;
  237. m_exitCode = WSTOPSIG(status);
  238. }
  239. else
  240. {
  241. ANKI_ASSERT(0);
  242. }
  243. }
  244. return err;
  245. }
  246. Error Process::wait(Second timeout, ProcessStatus* status, I32* exitCode)
  247. {
  248. ANKI_CHECK(refresh(0));
  249. if(status)
  250. {
  251. *status = m_status;
  252. }
  253. if(exitCode)
  254. {
  255. *exitCode = m_exitCode;
  256. }
  257. return Error::NONE;
  258. }
  259. } // end namespace anki