Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChildProcess.cpp 8.6KB

7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2020 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "ChildProcess.h"
  21. #include "../files/File.h"
  22. #include "../misc/Time.h"
  23. #ifndef CARLA_OS_WIN
  24. # include <signal.h>
  25. # include <sys/wait.h>
  26. #endif
  27. #include "CarlaProcessUtils.hpp"
  28. namespace water {
  29. #ifdef CARLA_OS_WIN
  30. //=====================================================================================================================
  31. class ChildProcess::ActiveProcess
  32. {
  33. public:
  34. ActiveProcess (const String& command)
  35. : ok (false)
  36. {
  37. STARTUPINFO startupInfo;
  38. carla_zeroStruct(startupInfo);
  39. startupInfo.cb = sizeof (startupInfo);
  40. ok = CreateProcess (nullptr, const_cast<LPSTR>(command.toRawUTF8()),
  41. nullptr, nullptr, TRUE, CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT,
  42. nullptr, nullptr, &startupInfo, &processInfo) != FALSE;
  43. }
  44. ~ActiveProcess()
  45. {
  46. if (ok)
  47. {
  48. CloseHandle (processInfo.hThread);
  49. CloseHandle (processInfo.hProcess);
  50. }
  51. }
  52. bool isRunning() const noexcept
  53. {
  54. return WaitForSingleObject (processInfo.hProcess, 0) != WAIT_OBJECT_0;
  55. }
  56. bool checkRunningAndUnsetPID() noexcept
  57. {
  58. if (isRunning())
  59. return true;
  60. ok = false;
  61. CloseHandle (processInfo.hThread);
  62. CloseHandle (processInfo.hProcess);
  63. return false;
  64. }
  65. bool killProcess() const noexcept
  66. {
  67. return TerminateProcess (processInfo.hProcess, 0) != FALSE;
  68. }
  69. bool terminateProcess() const noexcept
  70. {
  71. return TerminateProcess (processInfo.hProcess, 0) != FALSE;
  72. }
  73. uint32 getExitCode() const noexcept
  74. {
  75. DWORD exitCode = 0;
  76. GetExitCodeProcess (processInfo.hProcess, &exitCode);
  77. return (uint32) exitCode;
  78. }
  79. int getPID() const noexcept
  80. {
  81. return 0;
  82. }
  83. bool ok;
  84. private:
  85. PROCESS_INFORMATION processInfo;
  86. CARLA_DECLARE_NON_COPY_CLASS (ActiveProcess)
  87. };
  88. #else
  89. class ChildProcess::ActiveProcess
  90. {
  91. public:
  92. ActiveProcess (const StringArray& arguments)
  93. : childPID (0)
  94. {
  95. String exe (arguments[0].unquoted());
  96. // Looks like you're trying to launch a non-existent exe or a folder (perhaps on OSX
  97. // you're trying to launch the .app folder rather than the actual binary inside it?)
  98. wassert (File::getCurrentWorkingDirectory().getChildFile (exe).existsAsFile()
  99. || ! exe.containsChar (File::separator));
  100. Array<char*> argv;
  101. for (int i = 0; i < arguments.size(); ++i)
  102. if (arguments[i].isNotEmpty())
  103. argv.add (const_cast<char*> (arguments[i].toRawUTF8()));
  104. argv.add (nullptr);
  105. const pid_t result = vfork();
  106. if (result < 0)
  107. {
  108. // error
  109. }
  110. else if (result == 0)
  111. {
  112. // child process
  113. carla_terminateProcessOnParentExit(true);
  114. if (execvp (exe.toRawUTF8(), argv.getRawDataPointer()))
  115. _exit (-1);
  116. }
  117. else
  118. {
  119. // we're the parent process..
  120. childPID = result;
  121. }
  122. }
  123. ~ActiveProcess()
  124. {
  125. CARLA_SAFE_ASSERT_INT(childPID == 0, childPID);
  126. }
  127. bool isRunning() const noexcept
  128. {
  129. if (childPID != 0)
  130. {
  131. int childState = 0;
  132. const int pid = waitpid (childPID, &childState, WNOHANG|WUNTRACED);
  133. return pid == 0 || ! (WIFEXITED (childState) || WIFSIGNALED (childState) || WIFSTOPPED (childState));
  134. }
  135. return false;
  136. }
  137. bool checkRunningAndUnsetPID() noexcept
  138. {
  139. if (childPID != 0)
  140. {
  141. int childState = 0;
  142. const int pid = waitpid (childPID, &childState, WNOHANG|WUNTRACED);
  143. if (pid == 0)
  144. return true;
  145. if ( ! (WIFEXITED (childState) || WIFSIGNALED (childState) || WIFSTOPPED (childState)))
  146. return true;
  147. childPID = 0;
  148. return false;
  149. }
  150. return false;
  151. }
  152. bool killProcess() noexcept
  153. {
  154. if (::kill (childPID, SIGKILL) == 0)
  155. {
  156. childPID = 0;
  157. return true;
  158. }
  159. return false;
  160. }
  161. bool terminateProcess() const noexcept
  162. {
  163. return ::kill (childPID, SIGTERM) == 0;
  164. }
  165. uint32 getExitCode() const noexcept
  166. {
  167. if (childPID != 0)
  168. {
  169. int childState = 0;
  170. const int pid = waitpid (childPID, &childState, WNOHANG);
  171. if (pid >= 0 && WIFEXITED (childState))
  172. return WEXITSTATUS (childState);
  173. }
  174. return 0;
  175. }
  176. int getPID() const noexcept
  177. {
  178. return childPID;
  179. }
  180. int childPID;
  181. private:
  182. CARLA_DECLARE_NON_COPY_CLASS (ActiveProcess)
  183. };
  184. #endif
  185. //=====================================================================================================================
  186. ChildProcess::ChildProcess() {}
  187. ChildProcess::~ChildProcess() {}
  188. bool ChildProcess::isRunning() const
  189. {
  190. return activeProcess != nullptr && activeProcess->isRunning();
  191. }
  192. bool ChildProcess::kill()
  193. {
  194. return activeProcess == nullptr || activeProcess->killProcess();
  195. }
  196. bool ChildProcess::terminate()
  197. {
  198. return activeProcess == nullptr || activeProcess->terminateProcess();
  199. }
  200. uint32 ChildProcess::getExitCode() const
  201. {
  202. return activeProcess != nullptr ? activeProcess->getExitCode() : 0;
  203. }
  204. bool ChildProcess::waitForProcessToFinish (const int timeoutMs)
  205. {
  206. const uint32 timeoutTime = Time::getMillisecondCounter() + (uint32) timeoutMs;
  207. do
  208. {
  209. if (activeProcess == nullptr)
  210. return true;
  211. if (! activeProcess->checkRunningAndUnsetPID())
  212. return true;
  213. carla_msleep(5);
  214. }
  215. while (timeoutMs < 0 || Time::getMillisecondCounter() < timeoutTime);
  216. return false;
  217. }
  218. uint32 ChildProcess::getPID() const noexcept
  219. {
  220. return activeProcess != nullptr ? activeProcess->getPID() : 0;
  221. }
  222. //=====================================================================================================================
  223. #ifdef CARLA_OS_WIN
  224. bool ChildProcess::start (const String& command)
  225. {
  226. activeProcess = new ActiveProcess (command);
  227. if (! activeProcess->ok)
  228. activeProcess = nullptr;
  229. return activeProcess != nullptr;
  230. }
  231. bool ChildProcess::start (const StringArray& args)
  232. {
  233. String escaped;
  234. for (int i = 0, size = args.size(); i < size; ++i)
  235. {
  236. String arg (args[i]);
  237. // If there are spaces, surround it with quotes. If there are quotes,
  238. // replace them with \" so that CommandLineToArgv will correctly parse them.
  239. if (arg.containsAnyOf ("\" "))
  240. arg = arg.replace ("\"", "\\\"").quoted();
  241. escaped << arg;
  242. if (i+1 < size)
  243. escaped << ' ';
  244. }
  245. return start (escaped.trim());
  246. }
  247. #else
  248. bool ChildProcess::start (const String& command)
  249. {
  250. return start (StringArray::fromTokens (command, true));
  251. }
  252. bool ChildProcess::start (const StringArray& args)
  253. {
  254. if (args.size() == 0)
  255. return false;
  256. activeProcess = new ActiveProcess (args);
  257. if (activeProcess->childPID == 0)
  258. activeProcess = nullptr;
  259. return activeProcess != nullptr;
  260. }
  261. #endif
  262. }