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.

404 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. #define JUCE_USE_VFORK 1
  24. namespace water {
  25. #ifdef CARLA_OS_WIN
  26. //=====================================================================================================================
  27. class ChildProcess::ActiveProcess
  28. {
  29. public:
  30. ActiveProcess (const String& command, int streamFlags)
  31. : ok (false), readPipe (0), writePipe (0)
  32. {
  33. SECURITY_ATTRIBUTES securityAtts;
  34. carla_zeroStruct(securityAtts);
  35. securityAtts.nLength = sizeof (securityAtts);
  36. securityAtts.bInheritHandle = TRUE;
  37. if (CreatePipe (&readPipe, &writePipe, &securityAtts, 0)
  38. && SetHandleInformation (readPipe, HANDLE_FLAG_INHERIT, 0))
  39. {
  40. STARTUPINFO startupInfo;
  41. carla_zeroStruct(startupInfo);
  42. startupInfo.cb = sizeof (startupInfo);
  43. startupInfo.hStdOutput = (streamFlags & wantStdOut) != 0 ? writePipe : 0;
  44. startupInfo.hStdError = (streamFlags & wantStdErr) != 0 ? writePipe : 0;
  45. startupInfo.dwFlags = STARTF_USESTDHANDLES;
  46. ok = CreateProcess (nullptr, const_cast<LPSTR>(command.toRawUTF8()),
  47. nullptr, nullptr, TRUE, CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT,
  48. nullptr, nullptr, &startupInfo, &processInfo) != FALSE;
  49. }
  50. }
  51. ~ActiveProcess()
  52. {
  53. if (ok)
  54. {
  55. CloseHandle (processInfo.hThread);
  56. CloseHandle (processInfo.hProcess);
  57. }
  58. if (readPipe != 0)
  59. CloseHandle (readPipe);
  60. if (writePipe != 0)
  61. CloseHandle (writePipe);
  62. }
  63. bool isRunning() const noexcept
  64. {
  65. return WaitForSingleObject (processInfo.hProcess, 0) != WAIT_OBJECT_0;
  66. }
  67. int read (void* dest, int numNeeded) const noexcept
  68. {
  69. int total = 0;
  70. while (ok && numNeeded > 0)
  71. {
  72. DWORD available = 0;
  73. if (! PeekNamedPipe ((HANDLE) readPipe, nullptr, 0, nullptr, &available, nullptr))
  74. break;
  75. const int numToDo = jmin ((int) available, numNeeded);
  76. if (available == 0)
  77. {
  78. if (! isRunning())
  79. break;
  80. Sleep(0);
  81. }
  82. else
  83. {
  84. DWORD numRead = 0;
  85. if (! ReadFile ((HANDLE) readPipe, dest, numToDo, &numRead, nullptr))
  86. break;
  87. total += numRead;
  88. dest = addBytesToPointer (dest, numRead);
  89. numNeeded -= numRead;
  90. }
  91. }
  92. return total;
  93. }
  94. bool killProcess() const noexcept
  95. {
  96. return TerminateProcess (processInfo.hProcess, 0) != FALSE;
  97. }
  98. uint32 getExitCode() const noexcept
  99. {
  100. DWORD exitCode = 0;
  101. GetExitCodeProcess (processInfo.hProcess, &exitCode);
  102. return (uint32) exitCode;
  103. }
  104. int getPID() const noexcept
  105. {
  106. return 0;
  107. }
  108. bool ok;
  109. private:
  110. HANDLE readPipe, writePipe;
  111. PROCESS_INFORMATION processInfo;
  112. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ActiveProcess)
  113. };
  114. #else
  115. class ChildProcess::ActiveProcess
  116. {
  117. public:
  118. ActiveProcess (const StringArray& arguments, int streamFlags)
  119. : childPID (0), pipeHandle (0), readHandle (0)
  120. {
  121. String exe (arguments[0].unquoted());
  122. // Looks like you're trying to launch a non-existent exe or a folder (perhaps on OSX
  123. // you're trying to launch the .app folder rather than the actual binary inside it?)
  124. jassert (File::getCurrentWorkingDirectory().getChildFile (exe).existsAsFile()
  125. || ! exe.containsChar (File::separator));
  126. int pipeHandles[2] = { 0 };
  127. if (pipe (pipeHandles) == 0)
  128. {
  129. Array<char*> argv;
  130. for (int i = 0; i < arguments.size(); ++i)
  131. if (arguments[i].isNotEmpty())
  132. argv.add (const_cast<char*> (arguments[i].toRawUTF8()));
  133. argv.add (nullptr);
  134. #if JUCE_USE_VFORK
  135. const pid_t result = vfork();
  136. #else
  137. const pid_t result = fork();
  138. #endif
  139. if (result < 0)
  140. {
  141. close (pipeHandles[0]);
  142. close (pipeHandles[1]);
  143. }
  144. else if (result == 0)
  145. {
  146. #if ! JUCE_USE_VFORK
  147. // we're the child process..
  148. close (pipeHandles[0]); // close the read handle
  149. if ((streamFlags & wantStdOut) != 0)
  150. dup2 (pipeHandles[1], STDOUT_FILENO); // turns the pipe into stdout
  151. else
  152. dup2 (open ("/dev/null", O_WRONLY), STDOUT_FILENO);
  153. if ((streamFlags & wantStdErr) != 0)
  154. dup2 (pipeHandles[1], STDERR_FILENO);
  155. else
  156. dup2 (open ("/dev/null", O_WRONLY), STDERR_FILENO);
  157. close (pipeHandles[1]);
  158. #endif
  159. if (execvp (exe.toRawUTF8(), argv.getRawDataPointer()))
  160. _exit (-1);
  161. }
  162. else
  163. {
  164. // we're the parent process..
  165. childPID = result;
  166. pipeHandle = pipeHandles[0];
  167. close (pipeHandles[1]); // close the write handle
  168. }
  169. // FIXME
  170. (void)streamFlags;
  171. }
  172. }
  173. ~ActiveProcess()
  174. {
  175. if (readHandle != 0)
  176. fclose (readHandle);
  177. if (pipeHandle != 0)
  178. close (pipeHandle);
  179. }
  180. bool isRunning() const noexcept
  181. {
  182. if (childPID != 0)
  183. {
  184. int childState;
  185. const int pid = waitpid (childPID, &childState, WNOHANG);
  186. return pid == 0 || ! (WIFEXITED (childState) || WIFSIGNALED (childState));
  187. }
  188. return false;
  189. }
  190. int read (void* const dest, const int numBytes) noexcept
  191. {
  192. jassert (dest != nullptr);
  193. #ifdef fdopen
  194. #error // the zlib headers define this function as NULL!
  195. #endif
  196. if (readHandle == 0 && childPID != 0)
  197. readHandle = fdopen (pipeHandle, "r");
  198. if (readHandle != 0)
  199. return (int) fread (dest, 1, (size_t) numBytes, readHandle);
  200. return 0;
  201. }
  202. bool killProcess() const noexcept
  203. {
  204. return ::kill (childPID, SIGKILL) == 0;
  205. }
  206. uint32 getExitCode() const noexcept
  207. {
  208. if (childPID != 0)
  209. {
  210. int childState = 0;
  211. const int pid = waitpid (childPID, &childState, WNOHANG);
  212. if (pid >= 0 && WIFEXITED (childState))
  213. return WEXITSTATUS (childState);
  214. }
  215. return 0;
  216. }
  217. int getPID() const noexcept
  218. {
  219. return childPID;
  220. }
  221. int childPID;
  222. private:
  223. int pipeHandle;
  224. FILE* readHandle;
  225. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ActiveProcess)
  226. };
  227. #endif
  228. //=====================================================================================================================
  229. ChildProcess::ChildProcess() {}
  230. ChildProcess::~ChildProcess() {}
  231. bool ChildProcess::isRunning() const
  232. {
  233. return activeProcess != nullptr && activeProcess->isRunning();
  234. }
  235. int ChildProcess::readProcessOutput (void* dest, int numBytes)
  236. {
  237. return activeProcess != nullptr ? activeProcess->read (dest, numBytes) : 0;
  238. }
  239. bool ChildProcess::kill()
  240. {
  241. return activeProcess == nullptr || activeProcess->killProcess();
  242. }
  243. uint32 ChildProcess::getExitCode() const
  244. {
  245. return activeProcess != nullptr ? activeProcess->getExitCode() : 0;
  246. }
  247. bool ChildProcess::waitForProcessToFinish (const int timeoutMs) const
  248. {
  249. const uint32 timeoutTime = Time::getMillisecondCounter() + (uint32) timeoutMs;
  250. do
  251. {
  252. if (! isRunning())
  253. return true;
  254. }
  255. while (timeoutMs < 0 || Time::getMillisecondCounter() < timeoutTime);
  256. return false;
  257. }
  258. String ChildProcess::readAllProcessOutput()
  259. {
  260. MemoryOutputStream result;
  261. for (;;)
  262. {
  263. char buffer [512];
  264. const int num = readProcessOutput (buffer, sizeof (buffer));
  265. if (num <= 0)
  266. break;
  267. result.write (buffer, (size_t) num);
  268. }
  269. return result.toString();
  270. }
  271. uint32 ChildProcess::getPID() const noexcept
  272. {
  273. return activeProcess != nullptr ? activeProcess->getPID() : 0;
  274. }
  275. //=====================================================================================================================
  276. #ifdef CARLA_OS_WIN
  277. bool ChildProcess::start (const String& command, int streamFlags)
  278. {
  279. activeProcess = new ActiveProcess (command, streamFlags);
  280. if (! activeProcess->ok)
  281. activeProcess = nullptr;
  282. return activeProcess != nullptr;
  283. }
  284. bool ChildProcess::start (const StringArray& args, int streamFlags)
  285. {
  286. String escaped;
  287. for (int i = 0; i < args.size(); ++i)
  288. {
  289. String arg (args[i]);
  290. #if 0 // FIXME
  291. // If there are spaces, surround it with quotes. If there are quotes,
  292. // replace them with \" so that CommandLineToArgv will correctly parse them.
  293. if (arg.containsAnyOf ("\" "))
  294. arg = arg.replace ("\"", "\\\"").quoted();
  295. #endif
  296. escaped << arg << ' ';
  297. }
  298. return start (escaped.trim(), streamFlags);
  299. }
  300. #else
  301. bool ChildProcess::start (const String& command, int streamFlags)
  302. {
  303. return start (StringArray::fromTokens (command, true), streamFlags);
  304. }
  305. bool ChildProcess::start (const StringArray& args, int streamFlags)
  306. {
  307. if (args.size() == 0)
  308. return false;
  309. activeProcess = new ActiveProcess (args, streamFlags);
  310. if (activeProcess->childPID == 0)
  311. activeProcess = nullptr;
  312. return activeProcess != nullptr;
  313. }
  314. #endif
  315. }