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.

340 lines
8.8KB

  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. closeProcessInfo();
  47. }
  48. void closeProcessInfo() noexcept
  49. {
  50. if (ok)
  51. {
  52. ok = false;
  53. CloseHandle (processInfo.hThread);
  54. CloseHandle (processInfo.hProcess);
  55. }
  56. }
  57. bool isRunning() const noexcept
  58. {
  59. return WaitForSingleObject (processInfo.hProcess, 0) != WAIT_OBJECT_0;
  60. }
  61. bool checkRunningAndUnsetPID() noexcept
  62. {
  63. if (isRunning())
  64. return true;
  65. ok = false;
  66. CloseHandle (processInfo.hThread);
  67. CloseHandle (processInfo.hProcess);
  68. return false;
  69. }
  70. bool killProcess() const noexcept
  71. {
  72. return TerminateProcess (processInfo.hProcess, 0) != FALSE;
  73. }
  74. bool terminateProcess() const noexcept
  75. {
  76. return TerminateProcess (processInfo.hProcess, 0) != FALSE;
  77. }
  78. uint32 getExitCodeAndClearPID() noexcept
  79. {
  80. DWORD exitCode = 0;
  81. GetExitCodeProcess (processInfo.hProcess, &exitCode);
  82. closeProcessInfo();
  83. return (uint32) exitCode;
  84. }
  85. int getPID() const noexcept
  86. {
  87. return 0;
  88. }
  89. bool ok;
  90. private:
  91. PROCESS_INFORMATION processInfo;
  92. CARLA_DECLARE_NON_COPY_CLASS (ActiveProcess)
  93. };
  94. #else
  95. class ChildProcess::ActiveProcess
  96. {
  97. public:
  98. ActiveProcess (const StringArray& arguments)
  99. : childPID (0)
  100. {
  101. String exe (arguments[0].unquoted());
  102. // Looks like you're trying to launch a non-existent exe or a folder (perhaps on OSX
  103. // you're trying to launch the .app folder rather than the actual binary inside it?)
  104. wassert (File::getCurrentWorkingDirectory().getChildFile (exe).existsAsFile()
  105. || ! exe.containsChar (File::separator));
  106. Array<char*> argv;
  107. for (int i = 0; i < arguments.size(); ++i)
  108. if (arguments[i].isNotEmpty())
  109. argv.add (const_cast<char*> (arguments[i].toRawUTF8()));
  110. argv.add (nullptr);
  111. const pid_t result = vfork();
  112. if (result < 0)
  113. {
  114. // error
  115. }
  116. else if (result == 0)
  117. {
  118. // child process
  119. carla_terminateProcessOnParentExit(true);
  120. if (execvp (exe.toRawUTF8(), argv.getRawDataPointer()))
  121. _exit (-1);
  122. }
  123. else
  124. {
  125. // we're the parent process..
  126. childPID = result;
  127. }
  128. }
  129. ~ActiveProcess()
  130. {
  131. CARLA_SAFE_ASSERT_INT(childPID == 0, childPID);
  132. }
  133. bool isRunning() const noexcept
  134. {
  135. if (childPID != 0)
  136. {
  137. int childState = 0;
  138. const int pid = waitpid (childPID, &childState, WNOHANG|WUNTRACED);
  139. return pid == 0 || ! (WIFEXITED (childState) || WIFSIGNALED (childState) || WIFSTOPPED (childState));
  140. }
  141. return false;
  142. }
  143. bool checkRunningAndUnsetPID() noexcept
  144. {
  145. if (childPID != 0)
  146. {
  147. int childState = 0;
  148. const int pid = waitpid (childPID, &childState, WNOHANG|WUNTRACED);
  149. if (pid == 0)
  150. return true;
  151. if ( ! (WIFEXITED (childState) || WIFSIGNALED (childState) || WIFSTOPPED (childState)))
  152. return true;
  153. childPID = 0;
  154. return false;
  155. }
  156. return false;
  157. }
  158. bool killProcess() noexcept
  159. {
  160. if (::kill (childPID, SIGKILL) == 0)
  161. {
  162. childPID = 0;
  163. return true;
  164. }
  165. return false;
  166. }
  167. bool terminateProcess() const noexcept
  168. {
  169. return ::kill (childPID, SIGTERM) == 0;
  170. }
  171. uint32 getExitCodeAndClearPID() noexcept
  172. {
  173. if (childPID != 0)
  174. {
  175. int childState = 0;
  176. const int pid = waitpid (childPID, &childState, WNOHANG);
  177. childPID = 0;
  178. if (pid >= 0 && WIFEXITED (childState))
  179. return WEXITSTATUS (childState);
  180. }
  181. return 0;
  182. }
  183. int getPID() const noexcept
  184. {
  185. return childPID;
  186. }
  187. int childPID;
  188. private:
  189. CARLA_DECLARE_NON_COPY_CLASS (ActiveProcess)
  190. };
  191. #endif
  192. //=====================================================================================================================
  193. ChildProcess::ChildProcess() {}
  194. ChildProcess::~ChildProcess() {}
  195. bool ChildProcess::isRunning() const
  196. {
  197. return activeProcess != nullptr && activeProcess->isRunning();
  198. }
  199. bool ChildProcess::kill()
  200. {
  201. return activeProcess == nullptr || activeProcess->killProcess();
  202. }
  203. bool ChildProcess::terminate()
  204. {
  205. return activeProcess == nullptr || activeProcess->terminateProcess();
  206. }
  207. uint32 ChildProcess::getExitCodeAndClearPID()
  208. {
  209. return activeProcess != nullptr ? activeProcess->getExitCodeAndClearPID() : 0;
  210. }
  211. bool ChildProcess::waitForProcessToFinish (const int timeoutMs)
  212. {
  213. const uint32 timeoutTime = Time::getMillisecondCounter() + (uint32) timeoutMs;
  214. do
  215. {
  216. if (activeProcess == nullptr)
  217. return true;
  218. if (! activeProcess->checkRunningAndUnsetPID())
  219. return true;
  220. carla_msleep(5);
  221. }
  222. while (timeoutMs < 0 || Time::getMillisecondCounter() < timeoutTime);
  223. return false;
  224. }
  225. uint32 ChildProcess::getPID() const noexcept
  226. {
  227. return activeProcess != nullptr ? activeProcess->getPID() : 0;
  228. }
  229. //=====================================================================================================================
  230. #ifdef CARLA_OS_WIN
  231. bool ChildProcess::start (const String& command)
  232. {
  233. activeProcess = new ActiveProcess (command);
  234. if (! activeProcess->ok)
  235. activeProcess = nullptr;
  236. return activeProcess != nullptr;
  237. }
  238. bool ChildProcess::start (const StringArray& args)
  239. {
  240. String escaped;
  241. for (int i = 0, size = args.size(); i < size; ++i)
  242. {
  243. String arg (args[i]);
  244. // If there are spaces, surround it with quotes. If there are quotes,
  245. // replace them with \" so that CommandLineToArgv will correctly parse them.
  246. if (arg.containsAnyOf ("\" "))
  247. arg = arg.replace ("\"", "\\\"").quoted();
  248. escaped << arg;
  249. if (i+1 < size)
  250. escaped << ' ';
  251. }
  252. return start (escaped.trim());
  253. }
  254. #else
  255. bool ChildProcess::start (const String& command)
  256. {
  257. return start (StringArray::fromTokens (command, true));
  258. }
  259. bool ChildProcess::start (const StringArray& args)
  260. {
  261. if (args.size() == 0)
  262. return false;
  263. activeProcess = new ActiveProcess (args);
  264. if (activeProcess->childPID == 0)
  265. activeProcess = nullptr;
  266. return activeProcess != nullptr;
  267. }
  268. #endif
  269. }