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.

409 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #include "juce_ChildProcess.h"
  24. #include "../files/juce_File.h"
  25. #include "../streams/juce_MemoryOutputStream.h"
  26. #include "../time/juce_Time.h"
  27. #define JUCE_USE_VFORK 1
  28. namespace water {
  29. #ifdef CARLA_OS_WIN
  30. //=====================================================================================================================
  31. class ChildProcess::ActiveProcess
  32. {
  33. public:
  34. ActiveProcess (const String& command, int streamFlags)
  35. : ok (false), readPipe (0), writePipe (0)
  36. {
  37. SECURITY_ATTRIBUTES securityAtts;
  38. carla_zeroStruct(securityAtts);
  39. securityAtts.nLength = sizeof (securityAtts);
  40. securityAtts.bInheritHandle = TRUE;
  41. if (CreatePipe (&readPipe, &writePipe, &securityAtts, 0)
  42. && SetHandleInformation (readPipe, HANDLE_FLAG_INHERIT, 0))
  43. {
  44. STARTUPINFO startupInfo;
  45. carla_zeroStruct(startupInfo);
  46. startupInfo.cb = sizeof (startupInfo);
  47. startupInfo.hStdOutput = (streamFlags & wantStdOut) != 0 ? writePipe : 0;
  48. startupInfo.hStdError = (streamFlags & wantStdErr) != 0 ? writePipe : 0;
  49. startupInfo.dwFlags = STARTF_USESTDHANDLES;
  50. ok = CreateProcess (nullptr, const_cast<LPSTR>(command.toRawUTF8()),
  51. nullptr, nullptr, TRUE, CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT,
  52. nullptr, nullptr, &startupInfo, &processInfo) != FALSE;
  53. }
  54. }
  55. ~ActiveProcess()
  56. {
  57. if (ok)
  58. {
  59. CloseHandle (processInfo.hThread);
  60. CloseHandle (processInfo.hProcess);
  61. }
  62. if (readPipe != 0)
  63. CloseHandle (readPipe);
  64. if (writePipe != 0)
  65. CloseHandle (writePipe);
  66. }
  67. bool isRunning() const noexcept
  68. {
  69. return WaitForSingleObject (processInfo.hProcess, 0) != WAIT_OBJECT_0;
  70. }
  71. int read (void* dest, int numNeeded) const noexcept
  72. {
  73. int total = 0;
  74. while (ok && numNeeded > 0)
  75. {
  76. DWORD available = 0;
  77. if (! PeekNamedPipe ((HANDLE) readPipe, nullptr, 0, nullptr, &available, nullptr))
  78. break;
  79. const int numToDo = jmin ((int) available, numNeeded);
  80. if (available == 0)
  81. {
  82. if (! isRunning())
  83. break;
  84. Sleep(0);
  85. }
  86. else
  87. {
  88. DWORD numRead = 0;
  89. if (! ReadFile ((HANDLE) readPipe, dest, numToDo, &numRead, nullptr))
  90. break;
  91. total += numRead;
  92. dest = addBytesToPointer (dest, numRead);
  93. numNeeded -= numRead;
  94. }
  95. }
  96. return total;
  97. }
  98. bool killProcess() const noexcept
  99. {
  100. return TerminateProcess (processInfo.hProcess, 0) != FALSE;
  101. }
  102. uint32 getExitCode() const noexcept
  103. {
  104. DWORD exitCode = 0;
  105. GetExitCodeProcess (processInfo.hProcess, &exitCode);
  106. return (uint32) exitCode;
  107. }
  108. int getPID() const noexcept
  109. {
  110. return 0;
  111. }
  112. bool ok;
  113. private:
  114. HANDLE readPipe, writePipe;
  115. PROCESS_INFORMATION processInfo;
  116. JUCE_DECLARE_NON_COPYABLE (ActiveProcess)
  117. };
  118. #else
  119. class ChildProcess::ActiveProcess
  120. {
  121. public:
  122. ActiveProcess (const StringArray& arguments, int streamFlags)
  123. : childPID (0), pipeHandle (0), readHandle (0)
  124. {
  125. String exe (arguments[0].unquoted());
  126. // Looks like you're trying to launch a non-existent exe or a folder (perhaps on OSX
  127. // you're trying to launch the .app folder rather than the actual binary inside it?)
  128. jassert (File::getCurrentWorkingDirectory().getChildFile (exe).existsAsFile()
  129. || ! exe.containsChar (File::separator));
  130. int pipeHandles[2] = { 0 };
  131. if (pipe (pipeHandles) == 0)
  132. {
  133. Array<char*> argv;
  134. for (int i = 0; i < arguments.size(); ++i)
  135. if (arguments[i].isNotEmpty())
  136. argv.add (const_cast<char*> (arguments[i].toRawUTF8()));
  137. argv.add (nullptr);
  138. #if JUCE_USE_VFORK
  139. const pid_t result = vfork();
  140. #else
  141. const pid_t result = fork();
  142. #endif
  143. if (result < 0)
  144. {
  145. close (pipeHandles[0]);
  146. close (pipeHandles[1]);
  147. }
  148. else if (result == 0)
  149. {
  150. #if ! JUCE_USE_VFORK
  151. // we're the child process..
  152. close (pipeHandles[0]); // close the read handle
  153. if ((streamFlags & wantStdOut) != 0)
  154. dup2 (pipeHandles[1], STDOUT_FILENO); // turns the pipe into stdout
  155. else
  156. dup2 (open ("/dev/null", O_WRONLY), STDOUT_FILENO);
  157. if ((streamFlags & wantStdErr) != 0)
  158. dup2 (pipeHandles[1], STDERR_FILENO);
  159. else
  160. dup2 (open ("/dev/null", O_WRONLY), STDERR_FILENO);
  161. close (pipeHandles[1]);
  162. #endif
  163. if (execvp (exe.toRawUTF8(), argv.getRawDataPointer()))
  164. _exit (-1);
  165. }
  166. else
  167. {
  168. // we're the parent process..
  169. childPID = result;
  170. pipeHandle = pipeHandles[0];
  171. close (pipeHandles[1]); // close the write handle
  172. }
  173. // FIXME
  174. (void)streamFlags;
  175. }
  176. }
  177. ~ActiveProcess()
  178. {
  179. if (readHandle != 0)
  180. fclose (readHandle);
  181. if (pipeHandle != 0)
  182. close (pipeHandle);
  183. }
  184. bool isRunning() const noexcept
  185. {
  186. if (childPID != 0)
  187. {
  188. int childState;
  189. const int pid = waitpid (childPID, &childState, WNOHANG);
  190. return pid == 0 || ! (WIFEXITED (childState) || WIFSIGNALED (childState));
  191. }
  192. return false;
  193. }
  194. int read (void* const dest, const int numBytes) noexcept
  195. {
  196. jassert (dest != nullptr);
  197. #ifdef fdopen
  198. #error // the zlib headers define this function as NULL!
  199. #endif
  200. if (readHandle == 0 && childPID != 0)
  201. readHandle = fdopen (pipeHandle, "r");
  202. if (readHandle != 0)
  203. return (int) fread (dest, 1, (size_t) numBytes, readHandle);
  204. return 0;
  205. }
  206. bool killProcess() const noexcept
  207. {
  208. return ::kill (childPID, SIGKILL) == 0;
  209. }
  210. uint32 getExitCode() const noexcept
  211. {
  212. if (childPID != 0)
  213. {
  214. int childState = 0;
  215. const int pid = waitpid (childPID, &childState, WNOHANG);
  216. if (pid >= 0 && WIFEXITED (childState))
  217. return WEXITSTATUS (childState);
  218. }
  219. return 0;
  220. }
  221. int getPID() const noexcept
  222. {
  223. return childPID;
  224. }
  225. int childPID;
  226. private:
  227. int pipeHandle;
  228. FILE* readHandle;
  229. JUCE_DECLARE_NON_COPYABLE (ActiveProcess)
  230. };
  231. #endif
  232. //=====================================================================================================================
  233. ChildProcess::ChildProcess() {}
  234. ChildProcess::~ChildProcess() {}
  235. bool ChildProcess::isRunning() const
  236. {
  237. return activeProcess != nullptr && activeProcess->isRunning();
  238. }
  239. int ChildProcess::readProcessOutput (void* dest, int numBytes)
  240. {
  241. return activeProcess != nullptr ? activeProcess->read (dest, numBytes) : 0;
  242. }
  243. bool ChildProcess::kill()
  244. {
  245. return activeProcess == nullptr || activeProcess->killProcess();
  246. }
  247. uint32 ChildProcess::getExitCode() const
  248. {
  249. return activeProcess != nullptr ? activeProcess->getExitCode() : 0;
  250. }
  251. bool ChildProcess::waitForProcessToFinish (const int timeoutMs) const
  252. {
  253. const uint32 timeoutTime = Time::getMillisecondCounter() + (uint32) timeoutMs;
  254. do
  255. {
  256. if (! isRunning())
  257. return true;
  258. }
  259. while (timeoutMs < 0 || Time::getMillisecondCounter() < timeoutTime);
  260. return false;
  261. }
  262. String ChildProcess::readAllProcessOutput()
  263. {
  264. MemoryOutputStream result;
  265. for (;;)
  266. {
  267. char buffer [512];
  268. const int num = readProcessOutput (buffer, sizeof (buffer));
  269. if (num <= 0)
  270. break;
  271. result.write (buffer, (size_t) num);
  272. }
  273. return result.toString();
  274. }
  275. uint32 ChildProcess::getPID() const noexcept
  276. {
  277. return activeProcess != nullptr ? activeProcess->getPID() : 0;
  278. }
  279. //=====================================================================================================================
  280. #ifdef CARLA_OS_WIN
  281. bool ChildProcess::start (const String& command, int streamFlags)
  282. {
  283. activeProcess = new ActiveProcess (command, streamFlags);
  284. if (! activeProcess->ok)
  285. activeProcess = nullptr;
  286. return activeProcess != nullptr;
  287. }
  288. bool ChildProcess::start (const StringArray& args, int streamFlags)
  289. {
  290. String escaped;
  291. for (int i = 0; i < args.size(); ++i)
  292. {
  293. String arg (args[i]);
  294. #if 0 // FIXME
  295. // If there are spaces, surround it with quotes. If there are quotes,
  296. // replace them with \" so that CommandLineToArgv will correctly parse them.
  297. if (arg.containsAnyOf ("\" "))
  298. arg = arg.replace ("\"", "\\\"").quoted();
  299. #endif
  300. escaped << arg << ' ';
  301. }
  302. return start (escaped.trim(), streamFlags);
  303. }
  304. #else
  305. bool ChildProcess::start (const String& command, int streamFlags)
  306. {
  307. return start (StringArray::fromTokens (command, true), streamFlags);
  308. }
  309. bool ChildProcess::start (const StringArray& args, int streamFlags)
  310. {
  311. if (args.size() == 0)
  312. return false;
  313. activeProcess = new ActiveProcess (args, streamFlags);
  314. if (activeProcess->childPID == 0)
  315. activeProcess = nullptr;
  316. return activeProcess != nullptr;
  317. }
  318. #endif
  319. }