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.

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