ProcessPosix.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright (C) 2009-2020, 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. char** newenv = static_cast<char**>(malloc((environment.getSize() + 1) * sizeof(char*)));
  88. i = 0;
  89. for(; i < environment.getSize(); ++i)
  90. {
  91. newenv[i] = static_cast<char*>(malloc(environment[i].getLength() + 1));
  92. strcpy(newenv[i], environment[i].cstr());
  93. }
  94. newenv[i] = nullptr;
  95. // Execute file
  96. const int execerror = execvpe(executable.cstr(), cargs, newenv);
  97. if(execerror)
  98. {
  99. printf("execvpe() failed: %s", strerror(errno));
  100. exit(1);
  101. }
  102. }
  103. else
  104. {
  105. close(m_stdoutPipe[1]);
  106. m_stdoutPipe[1] = -1;
  107. close(m_stderrPipe[1]);
  108. m_stderrPipe[1] = -1;
  109. }
  110. return Error::NONE;
  111. }
  112. Error Process::kill(ProcessKillSignal k)
  113. {
  114. if(m_pid < 1)
  115. {
  116. return Error::NONE;
  117. }
  118. int sig;
  119. switch(k)
  120. {
  121. case ProcessKillSignal::NORMAL:
  122. sig = SIGTERM;
  123. break;
  124. case ProcessKillSignal::FORCE:
  125. sig = SIGKILL;
  126. break;
  127. default:
  128. ANKI_ASSERT(0);
  129. sig = 0;
  130. };
  131. const pid_t p = ::kill(m_pid, sig);
  132. if(p != 0)
  133. {
  134. ANKI_UTIL_LOGE("kill() failed: %s", strerror(errno));
  135. return Error::FUNCTION_FAILED;
  136. }
  137. return Error::NONE;
  138. }
  139. Error Process::readFromFd(int fd, StringAuto& text) const
  140. {
  141. fd_set readfds;
  142. FD_ZERO(&readfds);
  143. FD_SET(fd, &readfds);
  144. timeval timeout;
  145. timeout.tv_sec = 0; // Seconds
  146. timeout.tv_usec = 10; // Microseconds
  147. // Limit the iterations in case the process writes to FD constantly
  148. U32 maxIterations = 16;
  149. while(maxIterations--)
  150. {
  151. // Check if there are data
  152. const int sel = select(1 + fd, &readfds, nullptr, nullptr, &timeout);
  153. if(sel == 0)
  154. {
  155. // Timeout expired
  156. break;
  157. }
  158. else if(sel == -1)
  159. {
  160. ANKI_UTIL_LOGE("select() failed: %s", strerror(errno));
  161. return Error::FUNCTION_FAILED;
  162. }
  163. // Read the data
  164. Array<char, 1024> buff;
  165. const ssize_t bytesCount = read(fd, buff.getBegin(), buff.getSize() - 1);
  166. if(bytesCount < 0 && errno != EAGAIN)
  167. {
  168. // NOTE errno == EINTR if a non-fatal signal arrived
  169. ANKI_UTIL_LOGE("read() failed: %s", strerror(errno));
  170. return Error::FUNCTION_FAILED;
  171. }
  172. else if(bytesCount == 0)
  173. {
  174. break;
  175. }
  176. else
  177. {
  178. buff[min<U32>(U32(bytesCount), buff.getSize() - 1)] = '\0';
  179. text.append(&buff[0]);
  180. }
  181. }
  182. return Error::NONE;
  183. }
  184. Error Process::readFromStdout(StringAuto& text)
  185. {
  186. return readFromFd(m_stdoutPipe[0], text);
  187. }
  188. Error Process::readFromStderr(StringAuto& text)
  189. {
  190. return readFromFd(m_stderrPipe[0], text);
  191. }
  192. Error Process::getStatus(ProcessStatus& status)
  193. {
  194. ANKI_CHECK(refresh(WNOHANG));
  195. status = m_status;
  196. return Error::NONE;
  197. }
  198. Error Process::refresh(int waitpidOptions)
  199. {
  200. Error err = Error::NONE;
  201. m_status = ProcessStatus::NOT_RUNNING;
  202. m_exitCode = DEFAULT_EXIT_CODE;
  203. if(m_pid == -1)
  204. {
  205. m_status = ProcessStatus::NOT_RUNNING;
  206. m_exitCode = DEFAULT_EXIT_CODE;
  207. }
  208. else
  209. {
  210. int status;
  211. const pid_t p = waitpid(m_pid, &status, waitpidOptions);
  212. if(p == -1)
  213. {
  214. m_status = ProcessStatus::NOT_RUNNING;
  215. m_exitCode = DEFAULT_EXIT_CODE;
  216. m_pid = -1;
  217. ANKI_UTIL_LOGE("waitpid() failed: %s", strerror(errno));
  218. err = Error::FUNCTION_FAILED;
  219. }
  220. else if(p == 0)
  221. {
  222. m_status = ProcessStatus::RUNNING;
  223. m_exitCode = DEFAULT_EXIT_CODE;
  224. }
  225. else if(WIFEXITED(status))
  226. {
  227. m_status = ProcessStatus::NORMAL_EXIT;
  228. m_exitCode = WEXITSTATUS(status);
  229. m_pid = -1;
  230. }
  231. else if(WIFSIGNALED(status))
  232. {
  233. m_status = ProcessStatus::CRASH_EXIT;
  234. m_exitCode = WTERMSIG(status);
  235. m_pid = -1;
  236. }
  237. else if(WIFSTOPPED(status))
  238. {
  239. // NOTE child may resume later (and become a zombie)
  240. m_status = ProcessStatus::CRASH_EXIT;
  241. m_exitCode = WSTOPSIG(status);
  242. }
  243. else
  244. {
  245. ANKI_ASSERT(0);
  246. }
  247. }
  248. return err;
  249. }
  250. Error Process::wait(Second timeout, ProcessStatus* status, I32* exitCode)
  251. {
  252. ANKI_CHECK(refresh(0));
  253. if(status)
  254. {
  255. *status = m_status;
  256. }
  257. if(exitCode)
  258. {
  259. *exitCode = m_exitCode;
  260. }
  261. return Error::NONE;
  262. }
  263. } // end namespace anki