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.

CarlaPipeUtils.cpp 54KB

10 years ago
9 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
7 years ago
10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. /*
  2. * Carla Pipe Utilities
  3. * Copyright (C) 2013-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPipeUtils.hpp"
  18. #include "CarlaProcessUtils.hpp"
  19. #include "CarlaString.hpp"
  20. #include "CarlaMIDI.h"
  21. // needed for atom-util
  22. #ifndef nullptr
  23. # undef NULL
  24. # define NULL nullptr
  25. #endif
  26. #ifdef BUILDING_CARLA
  27. # include "lv2/atom-util.h"
  28. #else
  29. # include "lv2/lv2plug.in/ns/ext/atom/util.h"
  30. #endif
  31. #include <fcntl.h>
  32. #include "water/misc/Time.h"
  33. #include "water/text/String.h"
  34. #ifdef CARLA_OS_WIN
  35. # include <ctime>
  36. #else
  37. # include <cerrno>
  38. # include <signal.h>
  39. # include <sys/wait.h>
  40. # ifdef CARLA_OS_LINUX
  41. # include <sys/prctl.h>
  42. # ifndef F_SETPIPE_SZ
  43. # define F_SETPIPE_SZ 1031
  44. # endif
  45. # endif
  46. #endif
  47. #ifdef CARLA_OS_WIN
  48. # define INVALID_PIPE_VALUE INVALID_HANDLE_VALUE
  49. #else
  50. # define INVALID_PIPE_VALUE -1
  51. #endif
  52. #ifdef CARLA_OS_WIN
  53. // -----------------------------------------------------------------------
  54. // win32 stuff
  55. static inline
  56. bool waitForAsyncObject(const HANDLE object, const HANDLE process = INVALID_HANDLE_VALUE)
  57. {
  58. DWORD dw, dw2;
  59. MSG msg;
  60. // we give it a max
  61. for (int i=20000; --i>=0;)
  62. {
  63. if (process != INVALID_HANDLE_VALUE)
  64. {
  65. switch (::WaitForSingleObject(process, 0))
  66. {
  67. case WAIT_OBJECT_0:
  68. case WAIT_FAILED:
  69. carla_stderr("waitForAsyncObject process has stopped");
  70. return false;
  71. }
  72. }
  73. carla_debug("waitForAsyncObject loop start");
  74. dw = ::MsgWaitForMultipleObjectsEx(1, &object, INFINITE, QS_POSTMESSAGE|QS_TIMER, 0);
  75. carla_debug("waitForAsyncObject initial code is: %u", dw);
  76. if (dw == WAIT_OBJECT_0)
  77. {
  78. carla_debug("waitForAsyncObject WAIT_OBJECT_0");
  79. return true;
  80. }
  81. dw2 = ::GetLastError();
  82. if (dw == WAIT_OBJECT_0 + 1)
  83. {
  84. carla_debug("waitForAsyncObject loop +1");
  85. while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
  86. ::DispatchMessage(&msg);
  87. continue;
  88. }
  89. if (dw2 == 0)
  90. {
  91. carla_debug("waitForAsyncObject loop stop");
  92. return true;
  93. }
  94. carla_stderr2("waitForAsyncObject loop end reached, error was: %u", dw2);
  95. carla_msleep(5);
  96. }
  97. carla_stderr2("waitForAsyncObject reached the end, this should not happen");
  98. return false;
  99. }
  100. static inline
  101. ssize_t ReadFileWin32(const HANDLE pipeh, const HANDLE event, void* const buf, const std::size_t numBytes)
  102. {
  103. DWORD dw, dsize = numBytes;
  104. DWORD available = 0;
  105. if (::PeekNamedPipe(pipeh, nullptr, 0, nullptr, &available, nullptr) == FALSE || available == 0)
  106. return -1;
  107. OVERLAPPED ov;
  108. carla_zeroStruct(ov);
  109. ov.hEvent = event;
  110. if (::ReadFile(pipeh, buf, dsize, nullptr, &ov))
  111. {
  112. if (! ::GetOverlappedResult(pipeh, &ov, &dw, FALSE))
  113. {
  114. carla_stderr("ReadFileWin32 GetOverlappedResult failed, error was: %u", ::GetLastError());
  115. return -1;
  116. }
  117. return static_cast<ssize_t>(dsize);
  118. }
  119. dw = ::GetLastError();
  120. if (dw == ERROR_IO_PENDING)
  121. {
  122. if (! waitForAsyncObject(event))
  123. {
  124. carla_stderr("ReadFileWin32 waitForAsyncObject failed, error was: %u", ::GetLastError());
  125. return -1;
  126. }
  127. if (! ::GetOverlappedResult(pipeh, &ov, &dw, FALSE))
  128. {
  129. carla_stderr("ReadFileWin32 GetOverlappedResult of pending failed, error was: %u", ::GetLastError());
  130. return -1;
  131. }
  132. return static_cast<ssize_t>(dsize);
  133. }
  134. carla_stderr("ReadFileWin32 failed, error was: %u", dw);
  135. return -1;
  136. }
  137. static inline
  138. ssize_t WriteFileWin32(const HANDLE pipeh, const HANDLE event, const void* const buf, const std::size_t numBytes)
  139. {
  140. DWORD dw, dsize = numBytes;
  141. OVERLAPPED ov;
  142. carla_zeroStruct(ov);
  143. ov.hEvent = event;
  144. if (::WriteFile(pipeh, buf, dsize, nullptr, &ov))
  145. {
  146. if (! ::GetOverlappedResult(pipeh, &ov, &dw, FALSE))
  147. {
  148. carla_stderr("WriteFileWin32 GetOverlappedResult failed, error was: %u", ::GetLastError());
  149. return -1;
  150. }
  151. return static_cast<ssize_t>(dsize);
  152. }
  153. dw = ::GetLastError();
  154. if (dw == ERROR_IO_PENDING)
  155. {
  156. if (! waitForAsyncObject(event))
  157. {
  158. carla_stderr("WriteFileWin32 waitForAsyncObject failed, error was: %u", ::GetLastError());
  159. return -1;
  160. }
  161. if (! ::GetOverlappedResult(pipeh, &ov, &dw, FALSE))
  162. {
  163. carla_stderr("WriteFileWin32 GetOverlappedResult of pending failed, error was: %u", ::GetLastError());
  164. return -1;
  165. }
  166. return static_cast<ssize_t>(dsize);
  167. }
  168. if (dw == ERROR_PIPE_NOT_CONNECTED)
  169. {
  170. carla_stdout("WriteFileWin32 failed, client has closed");
  171. return -2;
  172. }
  173. carla_stderr("WriteFileWin32 failed, error was: %u", dw);
  174. return -1;
  175. }
  176. #endif // CARLA_OS_WIN
  177. // -----------------------------------------------------------------------
  178. // startProcess
  179. #ifdef CARLA_OS_WIN
  180. static inline
  181. bool startProcess(const char* const argv[], PROCESS_INFORMATION* const processInfo)
  182. {
  183. CARLA_SAFE_ASSERT_RETURN(processInfo != nullptr, false);
  184. using water::String;
  185. String command;
  186. for (int i=0; argv[i] != nullptr; ++i)
  187. {
  188. String arg(argv[i]);
  189. // If there are spaces, surround it with quotes. If there are quotes,
  190. // replace them with \" so that CommandLineToArgv will correctly parse them.
  191. if (arg.containsAnyOf("\" "))
  192. arg = arg.replace("\"", "\\\"").quoted();
  193. command << arg << ' ';
  194. }
  195. command = command.trim();
  196. STARTUPINFO startupInfo;
  197. carla_zeroStruct(startupInfo);
  198. startupInfo.cb = sizeof(startupInfo);
  199. if (::CreateProcessA(nullptr, const_cast<LPSTR>(command.toRawUTF8()),
  200. nullptr, nullptr, TRUE, CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT,
  201. nullptr, nullptr, &startupInfo, processInfo) != FALSE)
  202. return true;
  203. carla_stderr2("startProcess failed, error was: %u", ::GetLastError());
  204. return false;
  205. }
  206. static inline
  207. bool waitForClientConnect(const HANDLE pipe, const HANDLE event, const HANDLE process, const uint32_t timeOutMilliseconds) noexcept
  208. {
  209. CARLA_SAFE_ASSERT_RETURN(pipe != INVALID_PIPE_VALUE, false);
  210. CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds > 0, false);
  211. DWORD dw;
  212. OVERLAPPED ov;
  213. carla_zeroStruct(ov);
  214. ov.hEvent = event;
  215. const uint32_t timeoutEnd(water::Time::getMillisecondCounter() + timeOutMilliseconds);
  216. for (;;)
  217. {
  218. if (::ConnectNamedPipe(pipe, &ov))
  219. {
  220. if (! ::GetOverlappedResult(pipe, &ov, &dw, FALSE))
  221. {
  222. carla_stderr2("ConnectNamedPipe GetOverlappedResult failed, error was: %u", ::GetLastError());
  223. return false;
  224. }
  225. return true;
  226. }
  227. const DWORD err = ::GetLastError();
  228. switch (err)
  229. {
  230. case ERROR_PIPE_CONNECTED:
  231. return true;
  232. case ERROR_IO_PENDING:
  233. if (! waitForAsyncObject(event, process))
  234. {
  235. carla_stderr2("ConnectNamedPipe waitForAsyncObject failed, error was: %u", ::GetLastError());
  236. return false;
  237. }
  238. if (! ::GetOverlappedResult(pipe, &ov, &dw, FALSE))
  239. {
  240. carla_stderr2("ConnectNamedPipe GetOverlappedResult of pending failed, error was: %u", ::GetLastError());
  241. return false;
  242. }
  243. return true;
  244. case ERROR_PIPE_LISTENING:
  245. if (water::Time::getMillisecondCounter() < timeoutEnd)
  246. {
  247. carla_msleep(5);
  248. continue;
  249. }
  250. carla_stderr2("ConnectNamedPipe listening timed out");
  251. return false;
  252. default:
  253. carla_stderr2("ConnectNamedPipe failed, error was: %u", err);
  254. return false;
  255. }
  256. }
  257. return true;
  258. }
  259. #else
  260. static inline
  261. bool startProcess(const char* const argv[], pid_t& pidinst) noexcept
  262. {
  263. const CarlaScopedEnvVar sev1("LD_LIBRARY_PATH", nullptr);
  264. const CarlaScopedEnvVar sev2("LD_PRELOAD", nullptr);
  265. const pid_t ret = pidinst = vfork();
  266. switch (ret)
  267. {
  268. case 0: { // child process
  269. execvp(argv[0], const_cast<char* const*>(argv));
  270. CarlaString error(std::strerror(errno));
  271. carla_stderr2("exec failed: %s", error.buffer());
  272. _exit(1); // this is not noexcept safe but doesn't matter anyway
  273. } break;
  274. case -1: { // error
  275. CarlaString error(std::strerror(errno));
  276. carla_stderr2("vfork() failed: %s", error.buffer());
  277. } break;
  278. }
  279. return (ret > 0);
  280. }
  281. #endif
  282. // -----------------------------------------------------------------------
  283. // waitForClientFirstMessage
  284. template<typename P>
  285. static inline
  286. bool waitForClientFirstMessage(const P& pipe, void* const ovRecv, void* const process, const uint32_t timeOutMilliseconds) noexcept
  287. {
  288. CARLA_SAFE_ASSERT_RETURN(pipe != INVALID_PIPE_VALUE, false);
  289. CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds > 0, false);
  290. char c;
  291. ssize_t ret;
  292. const uint32_t timeoutEnd(water::Time::getMillisecondCounter() + timeOutMilliseconds);
  293. #ifdef CARLA_OS_WIN
  294. if (! waitForClientConnect(pipe, (HANDLE)ovRecv, (HANDLE)process, timeOutMilliseconds))
  295. return false;
  296. #endif
  297. for (;;)
  298. {
  299. try {
  300. #ifdef CARLA_OS_WIN
  301. ret = ReadFileWin32(pipe, (HANDLE)ovRecv, &c, 1);
  302. #else
  303. ret = ::read(pipe, &c, 1);
  304. #endif
  305. } CARLA_SAFE_EXCEPTION_RETURN("read pipe", false);
  306. switch (ret)
  307. {
  308. case 1:
  309. if (c == '\n')
  310. return true;
  311. carla_stderr("waitForClientFirstMessage() - read has wrong first char '%c'", c);return false;
  312. return false;
  313. case -1: // failed to read
  314. #ifdef CARLA_OS_WIN
  315. if (::GetLastError() == ERROR_NO_DATA)
  316. #else
  317. if (errno == EAGAIN)
  318. #endif
  319. {
  320. if (water::Time::getMillisecondCounter() < timeoutEnd)
  321. {
  322. carla_msleep(5);
  323. continue;
  324. }
  325. carla_stderr("waitForClientFirstMessage() - read timed out");
  326. }
  327. else
  328. {
  329. #ifdef CARLA_OS_WIN
  330. carla_stderr("waitForClientFirstMessage() - read failed");
  331. #else
  332. CarlaString error(std::strerror(errno));
  333. carla_stderr("waitForClientFirstMessage() - read failed: %s", error.buffer());
  334. #endif
  335. }
  336. return false;
  337. default: // ???
  338. carla_stderr("waitForClientFirstMessage() - read returned %i", int(ret));
  339. return false;
  340. }
  341. }
  342. // maybe unused
  343. (void)ovRecv; (void)process;
  344. }
  345. // -----------------------------------------------------------------------
  346. // waitForChildToStop / waitForProcessToStop
  347. #ifdef CARLA_OS_WIN
  348. static inline
  349. bool waitForProcessToStop(const HANDLE process, const uint32_t timeOutMilliseconds, bool sendTerminate) noexcept
  350. {
  351. CARLA_SAFE_ASSERT_RETURN(process != INVALID_HANDLE_VALUE, false);
  352. CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds > 0, false);
  353. const uint32_t timeoutEnd(water::Time::getMillisecondCounter() + timeOutMilliseconds);
  354. for (;;)
  355. {
  356. switch (::WaitForSingleObject(process, 0))
  357. {
  358. case WAIT_OBJECT_0:
  359. case WAIT_FAILED:
  360. return true;
  361. }
  362. if (sendTerminate)
  363. {
  364. sendTerminate = false;
  365. ::TerminateProcess(process, 15);
  366. }
  367. if (water::Time::getMillisecondCounter() >= timeoutEnd)
  368. break;
  369. carla_msleep(5);
  370. }
  371. return false;
  372. }
  373. static inline
  374. void waitForProcessToStopOrKillIt(const HANDLE process, const uint32_t timeOutMilliseconds) noexcept
  375. {
  376. CARLA_SAFE_ASSERT_RETURN(process != INVALID_HANDLE_VALUE,);
  377. CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds > 0,);
  378. if (! waitForProcessToStop(process, timeOutMilliseconds, true))
  379. {
  380. carla_stderr("waitForProcessToStopOrKillIt() - process didn't stop, force termination");
  381. if (::TerminateProcess(process, 9) != FALSE)
  382. {
  383. // wait for process to stop
  384. waitForProcessToStop(process, timeOutMilliseconds, false);
  385. }
  386. }
  387. }
  388. #else
  389. static inline
  390. bool waitForChildToStop(const pid_t pid, const uint32_t timeOutMilliseconds, bool sendTerminate) noexcept
  391. {
  392. CARLA_SAFE_ASSERT_RETURN(pid > 0, false);
  393. CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds > 0, false);
  394. pid_t ret;
  395. const uint32_t timeoutEnd(water::Time::getMillisecondCounter() + timeOutMilliseconds);
  396. for (;;)
  397. {
  398. try {
  399. ret = ::waitpid(pid, nullptr, WNOHANG);
  400. } CARLA_SAFE_EXCEPTION_BREAK("waitpid");
  401. switch (ret)
  402. {
  403. case -1:
  404. if (errno == ECHILD)
  405. {
  406. // success, child doesn't exist
  407. return true;
  408. }
  409. else
  410. {
  411. CarlaString error(std::strerror(errno));
  412. carla_stderr("waitForChildToStop() - waitpid failed: %s", error.buffer());
  413. return false;
  414. }
  415. break;
  416. case 0:
  417. if (sendTerminate)
  418. {
  419. sendTerminate = false;
  420. ::kill(pid, SIGTERM);
  421. }
  422. if (water::Time::getMillisecondCounter() < timeoutEnd)
  423. {
  424. carla_msleep(5);
  425. continue;
  426. }
  427. carla_stderr("waitForChildToStop() - timed out");
  428. break;
  429. default:
  430. if (ret == pid)
  431. {
  432. // success
  433. return true;
  434. }
  435. else
  436. {
  437. carla_stderr("waitForChildToStop() - got wrong pid %i (requested was %i)", int(ret), int(pid));
  438. return false;
  439. }
  440. }
  441. break;
  442. }
  443. return false;
  444. }
  445. static inline
  446. void waitForChildToStopOrKillIt(pid_t& pid, const uint32_t timeOutMilliseconds) noexcept
  447. {
  448. CARLA_SAFE_ASSERT_RETURN(pid > 0,);
  449. CARLA_SAFE_ASSERT_RETURN(timeOutMilliseconds > 0,);
  450. if (! waitForChildToStop(pid, timeOutMilliseconds, true))
  451. {
  452. carla_stderr("waitForChildToStopOrKillIt() - process didn't stop, force killing");
  453. if (::kill(pid, SIGKILL) != -1)
  454. {
  455. // wait for killing to take place
  456. waitForChildToStop(pid, timeOutMilliseconds, false);
  457. }
  458. else
  459. {
  460. CarlaString error(std::strerror(errno));
  461. carla_stderr("waitForChildToStopOrKillIt() - kill failed: %s", error.buffer());
  462. }
  463. }
  464. }
  465. #endif
  466. // -----------------------------------------------------------------------
  467. struct CarlaPipeCommon::PrivateData {
  468. // pipes
  469. #ifdef CARLA_OS_WIN
  470. PROCESS_INFORMATION processInfo;
  471. HANDLE pipeRecv;
  472. HANDLE pipeSend;
  473. HANDLE ovRecv;
  474. HANDLE ovSend;
  475. #else
  476. pid_t pid;
  477. int pipeRecv;
  478. int pipeSend;
  479. #endif
  480. // read functions must only be called in context of idlePipe()
  481. bool isReading;
  482. // the client side is closing down, only waiting for response from server
  483. bool clientClosingDown;
  484. // other side of pipe has closed
  485. bool pipeClosed;
  486. // print error only once
  487. bool lastMessageFailed;
  488. // for debugging
  489. bool isServer;
  490. // common write lock
  491. CarlaMutex writeLock;
  492. // temporary buffers for _readline()
  493. mutable char tmpBuf[0xffff];
  494. mutable CarlaString tmpStr;
  495. PrivateData() noexcept
  496. #ifdef CARLA_OS_WIN
  497. : processInfo(),
  498. #else
  499. : pid(-1),
  500. #endif
  501. pipeRecv(INVALID_PIPE_VALUE),
  502. pipeSend(INVALID_PIPE_VALUE),
  503. isReading(false),
  504. clientClosingDown(false),
  505. pipeClosed(true),
  506. lastMessageFailed(false),
  507. isServer(false),
  508. writeLock(),
  509. tmpBuf(),
  510. tmpStr()
  511. {
  512. #ifdef CARLA_OS_WIN
  513. carla_zeroStruct(processInfo);
  514. processInfo.hProcess = INVALID_HANDLE_VALUE;
  515. processInfo.hThread = INVALID_HANDLE_VALUE;
  516. ovRecv = ::CreateEvent(nullptr, FALSE, FALSE, nullptr);
  517. ovSend = ::CreateEvent(nullptr, FALSE, FALSE, nullptr);
  518. #endif
  519. carla_zeroChars(tmpBuf, 0xffff);
  520. }
  521. CARLA_DECLARE_NON_COPY_STRUCT(PrivateData)
  522. };
  523. // -----------------------------------------------------------------------
  524. CarlaPipeCommon::CarlaPipeCommon() noexcept
  525. : pData(new PrivateData())
  526. {
  527. carla_debug("CarlaPipeCommon::CarlaPipeCommon()");
  528. }
  529. CarlaPipeCommon::~CarlaPipeCommon() /*noexcept*/
  530. {
  531. carla_debug("CarlaPipeCommon::~CarlaPipeCommon()");
  532. delete pData;
  533. }
  534. // -------------------------------------------------------------------
  535. bool CarlaPipeCommon::isPipeRunning() const noexcept
  536. {
  537. return (pData->pipeRecv != INVALID_PIPE_VALUE && pData->pipeSend != INVALID_PIPE_VALUE && ! pData->pipeClosed);
  538. }
  539. void CarlaPipeCommon::idlePipe(const bool onlyOnce) noexcept
  540. {
  541. bool readSucess;
  542. for (;;)
  543. {
  544. readSucess = false;
  545. const char* const msg = _readline(true, 0, readSucess);
  546. if (! readSucess)
  547. break;
  548. if (msg == nullptr)
  549. continue;
  550. pData->isReading = true;
  551. if (std::strcmp(msg, "__carla-quit__") == 0)
  552. {
  553. pData->pipeClosed = true;
  554. }
  555. else if (! pData->clientClosingDown)
  556. {
  557. try {
  558. msgReceived(msg);
  559. } CARLA_SAFE_EXCEPTION("msgReceived");
  560. }
  561. pData->isReading = false;
  562. delete[] msg;
  563. if (onlyOnce || pData->pipeRecv == INVALID_PIPE_VALUE)
  564. break;
  565. }
  566. }
  567. // -------------------------------------------------------------------
  568. void CarlaPipeCommon::lockPipe() const noexcept
  569. {
  570. pData->writeLock.lock();
  571. }
  572. bool CarlaPipeCommon::tryLockPipe() const noexcept
  573. {
  574. return pData->writeLock.tryLock();
  575. }
  576. void CarlaPipeCommon::unlockPipe() const noexcept
  577. {
  578. pData->writeLock.unlock();
  579. }
  580. CarlaMutex& CarlaPipeCommon::getPipeLock() const noexcept
  581. {
  582. return pData->writeLock;
  583. }
  584. // -------------------------------------------------------------------
  585. bool CarlaPipeCommon::readNextLineAsBool(bool& value) const noexcept
  586. {
  587. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  588. if (const char* const msg = _readlineblock(false))
  589. {
  590. value = (std::strcmp(msg, "true") == 0);
  591. return true;
  592. }
  593. return false;
  594. }
  595. bool CarlaPipeCommon::readNextLineAsByte(uint8_t& value) const noexcept
  596. {
  597. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  598. if (const char* const msg = _readlineblock(false))
  599. {
  600. const int asint = std::atoi(msg);
  601. if (asint >= 0 && asint <= 0xFF)
  602. {
  603. value = static_cast<uint8_t>(asint);
  604. return true;
  605. }
  606. }
  607. return false;
  608. }
  609. bool CarlaPipeCommon::readNextLineAsInt(int32_t& value) const noexcept
  610. {
  611. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  612. if (const char* const msg = _readlineblock(false))
  613. {
  614. value = std::atoi(msg);
  615. return true;
  616. }
  617. return false;
  618. }
  619. bool CarlaPipeCommon::readNextLineAsUInt(uint32_t& value) const noexcept
  620. {
  621. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  622. if (const char* const msg = _readlineblock(false))
  623. {
  624. const long aslong = std::atol(msg);
  625. if (aslong >= 0)
  626. {
  627. value = static_cast<uint32_t>(aslong);
  628. return true;
  629. }
  630. }
  631. return false;
  632. }
  633. bool CarlaPipeCommon::readNextLineAsLong(int64_t& value) const noexcept
  634. {
  635. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  636. if (const char* const msg = _readlineblock(false))
  637. {
  638. value = std::atol(msg);
  639. return true;
  640. }
  641. return false;
  642. }
  643. bool CarlaPipeCommon::readNextLineAsULong(uint64_t& value) const noexcept
  644. {
  645. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  646. if (const char* const msg = _readlineblock(false))
  647. {
  648. const int64_t asint64 = std::atol(msg);
  649. if (asint64 >= 0)
  650. {
  651. value = static_cast<uint64_t>(asint64);
  652. return true;
  653. }
  654. }
  655. return false;
  656. }
  657. bool CarlaPipeCommon::readNextLineAsFloat(float& value) const noexcept
  658. {
  659. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  660. if (const char* const msg = _readlineblock(false))
  661. {
  662. {
  663. const CarlaScopedLocale csl;
  664. value = static_cast<float>(std::atof(msg));
  665. }
  666. return true;
  667. }
  668. return false;
  669. }
  670. bool CarlaPipeCommon::readNextLineAsDouble(double& value) const noexcept
  671. {
  672. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  673. if (const char* const msg = _readlineblock(false))
  674. {
  675. {
  676. const CarlaScopedLocale csl;
  677. value = std::atof(msg);
  678. }
  679. return true;
  680. }
  681. return false;
  682. }
  683. bool CarlaPipeCommon::readNextLineAsString(const char*& value, const bool allocateString, uint32_t size) const noexcept
  684. {
  685. CARLA_SAFE_ASSERT_RETURN(pData->isReading, false);
  686. if (size >= 0xffff)
  687. size = 0;
  688. if (const char* const msg = _readlineblock(allocateString, static_cast<uint16_t>(size)))
  689. {
  690. value = msg;
  691. return true;
  692. }
  693. return false;
  694. }
  695. // -------------------------------------------------------------------
  696. // must be locked before calling
  697. bool CarlaPipeCommon::writeMessage(const char* const msg) const noexcept
  698. {
  699. CARLA_SAFE_ASSERT_RETURN(msg != nullptr && msg[0] != '\0', false);
  700. if (pData->pipeClosed)
  701. return false;
  702. const std::size_t size(std::strlen(msg));
  703. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  704. CARLA_SAFE_ASSERT_RETURN(msg[size-1] == '\n', false);
  705. return _writeMsgBuffer(msg, size);
  706. }
  707. bool CarlaPipeCommon::writeMessage(const char* const msg, std::size_t size) const noexcept
  708. {
  709. CARLA_SAFE_ASSERT_RETURN(msg != nullptr && msg[0] != '\0', false);
  710. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  711. CARLA_SAFE_ASSERT_RETURN(msg[size-1] == '\n', false);
  712. if (pData->pipeClosed)
  713. return false;
  714. return _writeMsgBuffer(msg, size);
  715. }
  716. bool CarlaPipeCommon::writeAndFixMessage(const char* const msg) const noexcept
  717. {
  718. CARLA_SAFE_ASSERT_RETURN(msg != nullptr, false);
  719. if (pData->pipeClosed)
  720. return false;
  721. const std::size_t size(std::strlen(msg));
  722. char fixedMsg[size+2];
  723. if (size > 0)
  724. {
  725. std::strcpy(fixedMsg, msg);
  726. for (std::size_t i=0; i<size; ++i)
  727. {
  728. if (fixedMsg[i] == '\n')
  729. fixedMsg[i] = '\r';
  730. }
  731. if (fixedMsg[size-1] == '\r')
  732. {
  733. fixedMsg[size-1] = '\n';
  734. fixedMsg[size ] = '\0';
  735. fixedMsg[size+1] = '\0';
  736. }
  737. else
  738. {
  739. fixedMsg[size ] = '\n';
  740. fixedMsg[size+1] = '\0';
  741. }
  742. }
  743. else
  744. {
  745. fixedMsg[0] = '\n';
  746. fixedMsg[1] = '\0';
  747. }
  748. return _writeMsgBuffer(fixedMsg, size+1);
  749. }
  750. bool CarlaPipeCommon::writeEmptyMessage() const noexcept
  751. {
  752. if (pData->pipeClosed)
  753. return false;
  754. return _writeMsgBuffer("\n", 1);
  755. }
  756. bool CarlaPipeCommon::flushMessages() const noexcept
  757. {
  758. CARLA_SAFE_ASSERT_RETURN(pData->pipeSend != INVALID_PIPE_VALUE, false);
  759. #if defined(CARLA_OS_LINUX) || defined(CARLA_OS_GNU_HURD)
  760. # if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2014
  761. // the only call that seems to do something
  762. return ::syncfs(pData->pipeSend) == 0;
  763. # endif
  764. #elif 0 // defined(CARLA_OS_WIN)
  765. // FIXME causes issues
  766. try {
  767. return (::FlushFileBuffers(pData->pipeSend) != FALSE);
  768. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::writeMsgBuffer", false);
  769. #endif
  770. return true;
  771. }
  772. // -------------------------------------------------------------------
  773. bool CarlaPipeCommon::writeErrorMessage(const char* const error) const noexcept
  774. {
  775. CARLA_SAFE_ASSERT_RETURN(error != nullptr && error[0] != '\0', false);
  776. const CarlaMutexLocker cml(pData->writeLock);
  777. if (! _writeMsgBuffer("error\n", 6))
  778. return false;
  779. if (! writeAndFixMessage(error))
  780. return false;
  781. flushMessages();
  782. return true;
  783. }
  784. bool CarlaPipeCommon::writeControlMessage(const uint32_t index, const float value, const bool withWriteLock) const noexcept
  785. {
  786. if (withWriteLock)
  787. {
  788. const CarlaMutexLocker cml(pData->writeLock);
  789. return writeControlMessage(index, value, false);
  790. }
  791. char tmpBuf[0xff];
  792. tmpBuf[0xfe] = '\0';
  793. if (! _writeMsgBuffer("control\n", 8))
  794. return false;
  795. std::snprintf(tmpBuf, 0xfe, "%i\n", index);
  796. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  797. return false;
  798. {
  799. const CarlaScopedLocale csl;
  800. std::snprintf(tmpBuf, 0xfe, "%.12g\n", static_cast<double>(value));
  801. }
  802. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  803. return false;
  804. flushMessages();
  805. return true;
  806. }
  807. bool CarlaPipeCommon::writeConfigureMessage(const char* const key, const char* const value) const noexcept
  808. {
  809. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0', false);
  810. CARLA_SAFE_ASSERT_RETURN(value != nullptr, false);
  811. const CarlaMutexLocker cml(pData->writeLock);
  812. if (! _writeMsgBuffer("configure\n", 10))
  813. return false;
  814. if (! writeAndFixMessage(key))
  815. return false;
  816. if (! writeAndFixMessage(value))
  817. return false;
  818. flushMessages();
  819. return true;
  820. }
  821. bool CarlaPipeCommon::writeProgramMessage(const uint32_t index) const noexcept
  822. {
  823. char tmpBuf[0xff];
  824. tmpBuf[0xfe] = '\0';
  825. const CarlaMutexLocker cml(pData->writeLock);
  826. if (! _writeMsgBuffer("program\n", 8))
  827. return false;
  828. std::snprintf(tmpBuf, 0xfe, "%i\n", index);
  829. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  830. return false;
  831. flushMessages();
  832. return true;
  833. }
  834. bool CarlaPipeCommon::writeProgramMessage(const uint8_t channel, const uint32_t bank, const uint32_t program) const noexcept
  835. {
  836. char tmpBuf[0xff];
  837. tmpBuf[0xfe] = '\0';
  838. const CarlaMutexLocker cml(pData->writeLock);
  839. if (! _writeMsgBuffer("program\n", 8))
  840. return false;
  841. std::snprintf(tmpBuf, 0xfe, "%i\n", channel);
  842. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  843. return false;
  844. std::snprintf(tmpBuf, 0xfe, "%i\n", bank);
  845. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  846. return false;
  847. std::snprintf(tmpBuf, 0xfe, "%i\n", program);
  848. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  849. return false;
  850. flushMessages();
  851. return true;
  852. }
  853. bool CarlaPipeCommon::writeMidiProgramMessage(const uint32_t bank, const uint32_t program) const noexcept
  854. {
  855. char tmpBuf[0xff];
  856. tmpBuf[0xfe] = '\0';
  857. const CarlaMutexLocker cml(pData->writeLock);
  858. if (! _writeMsgBuffer("midiprogram\n", 12))
  859. return false;
  860. std::snprintf(tmpBuf, 0xfe, "%i\n", bank);
  861. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  862. return false;
  863. std::snprintf(tmpBuf, 0xfe, "%i\n", program);
  864. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  865. return false;
  866. flushMessages();
  867. return true;
  868. }
  869. bool CarlaPipeCommon::writeReloadProgramsMessage(const int32_t index) const noexcept
  870. {
  871. char tmpBuf[0xff];
  872. tmpBuf[0xfe] = '\0';
  873. const CarlaMutexLocker cml(pData->writeLock);
  874. if (! _writeMsgBuffer("reloadprograms\n", 15))
  875. return false;
  876. std::snprintf(tmpBuf, 0xfe, "%i\n", index);
  877. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  878. return false;
  879. flushMessages();
  880. return true;
  881. }
  882. bool CarlaPipeCommon::writeMidiNoteMessage(const bool onOff, const uint8_t channel, const uint8_t note, const uint8_t velocity) const noexcept
  883. {
  884. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  885. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, false);
  886. CARLA_SAFE_ASSERT_RETURN(velocity < MAX_MIDI_VALUE, false);
  887. char tmpBuf[0xff];
  888. tmpBuf[0xfe] = '\0';
  889. const CarlaMutexLocker cml(pData->writeLock);
  890. if (! _writeMsgBuffer("note\n", 5))
  891. return false;
  892. std::snprintf(tmpBuf, 0xfe, "%s\n", bool2str(onOff));
  893. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  894. return false;
  895. std::snprintf(tmpBuf, 0xfe, "%i\n", channel);
  896. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  897. return false;
  898. std::snprintf(tmpBuf, 0xfe, "%i\n", note);
  899. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  900. return false;
  901. std::snprintf(tmpBuf, 0xfe, "%i\n", velocity);
  902. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  903. return false;
  904. flushMessages();
  905. return true;
  906. }
  907. bool CarlaPipeCommon::writeLv2AtomMessage(const uint32_t index, const LV2_Atom* const atom) const noexcept
  908. {
  909. CARLA_SAFE_ASSERT_RETURN(atom != nullptr, false);
  910. char tmpBuf[0xff];
  911. tmpBuf[0xfe] = '\0';
  912. const uint32_t atomTotalSize(lv2_atom_total_size(atom));
  913. CarlaString base64atom(CarlaString::asBase64(atom, atomTotalSize));
  914. const CarlaMutexLocker cml(pData->writeLock);
  915. if (! _writeMsgBuffer("atom\n", 5))
  916. return false;
  917. std::snprintf(tmpBuf, 0xfe, "%i\n", index);
  918. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  919. return false;
  920. std::snprintf(tmpBuf, 0xfe, "%i\n", atomTotalSize);
  921. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  922. return false;
  923. std::snprintf(tmpBuf, 0xfe, "%lu\n", static_cast<long unsigned>(base64atom.length()));
  924. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  925. return false;
  926. if (! writeAndFixMessage(base64atom.buffer()))
  927. return false;
  928. flushMessages();
  929. return true;
  930. }
  931. bool CarlaPipeCommon::writeLv2ParameterMessage(const char* const uri, const float value, const bool withWriteLock) const noexcept
  932. {
  933. if (withWriteLock)
  934. {
  935. const CarlaMutexLocker cml(pData->writeLock);
  936. return writeLv2ParameterMessage(uri, value, false);
  937. }
  938. char tmpBuf[0xff];
  939. tmpBuf[0xfe] = '\0';
  940. if (! _writeMsgBuffer("parameter\n", 10))
  941. return false;
  942. if (! writeAndFixMessage(uri))
  943. return false;
  944. {
  945. const CarlaScopedLocale csl;
  946. std::snprintf(tmpBuf, 0xfe, "%.12g\n", static_cast<double>(value));
  947. }
  948. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  949. return false;
  950. flushMessages();
  951. return true;
  952. }
  953. bool CarlaPipeCommon::writeLv2UridMessage(const uint32_t urid, const char* const uri) const noexcept
  954. {
  955. CARLA_SAFE_ASSERT_RETURN(urid != 0, false);
  956. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', false);
  957. char tmpBuf[0xff];
  958. tmpBuf[0xfe] = '\0';
  959. const CarlaMutexLocker cml(pData->writeLock);
  960. if (! _writeMsgBuffer("urid\n", 5))
  961. return false;
  962. std::snprintf(tmpBuf, 0xfe, "%i\n", urid);
  963. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  964. return false;
  965. std::snprintf(tmpBuf, 0xfe, "%lu\n", static_cast<long unsigned>(std::strlen(uri)));
  966. if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
  967. return false;
  968. if (! writeAndFixMessage(uri))
  969. return false;
  970. flushMessages();
  971. return true;
  972. }
  973. // -------------------------------------------------------------------
  974. // internal
  975. const char* CarlaPipeCommon::_readline(const bool allocReturn, const uint16_t size, bool& readSucess) const noexcept
  976. {
  977. CARLA_SAFE_ASSERT_RETURN(pData->pipeRecv != INVALID_PIPE_VALUE, nullptr);
  978. char c;
  979. char* ptr = pData->tmpBuf;
  980. ssize_t ret = -1;
  981. bool tooBig = false;
  982. pData->tmpStr.clear();
  983. if (size == 0 || size == 1)
  984. {
  985. for (int i=0; i<0xfffe; ++i)
  986. {
  987. try {
  988. #ifdef CARLA_OS_WIN
  989. ret = ReadFileWin32(pData->pipeRecv, pData->ovRecv, &c, 1);
  990. #else
  991. ret = ::read(pData->pipeRecv, &c, 1);
  992. #endif
  993. } CARLA_SAFE_EXCEPTION_BREAK("CarlaPipeCommon::readline() - read");
  994. if (ret != 1)
  995. break;
  996. if (c == '\n')
  997. {
  998. *ptr = '\0';
  999. break;
  1000. }
  1001. if (c == '\r')
  1002. c = '\n';
  1003. *ptr++ = c;
  1004. if (i+1 == 0xfffe)
  1005. {
  1006. i = 0;
  1007. *ptr = '\0';
  1008. tooBig = true;
  1009. pData->tmpStr += pData->tmpBuf;
  1010. ptr = pData->tmpBuf;
  1011. }
  1012. }
  1013. }
  1014. else
  1015. {
  1016. uint16_t remaining = size;
  1017. readSucess = false;
  1018. for (;;)
  1019. {
  1020. try {
  1021. #ifdef CARLA_OS_WIN
  1022. ret = ReadFileWin32(pData->pipeRecv, pData->ovRecv, ptr, remaining);
  1023. #else
  1024. ret = ::read(pData->pipeRecv, ptr, remaining);
  1025. #endif
  1026. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::readline() - read", nullptr);
  1027. if (ret == -1 && errno == EAGAIN)
  1028. continue;
  1029. CARLA_SAFE_ASSERT_INT2_RETURN(ret > 0, ret, remaining, nullptr);
  1030. CARLA_SAFE_ASSERT_INT2_RETURN(ret <= (ssize_t)remaining, ret, remaining, nullptr);
  1031. for (ssize_t i=0; i<ret; ++i)
  1032. {
  1033. if (ptr[i] == '\r')
  1034. ptr[i] = '\n';
  1035. }
  1036. ptr += ret;
  1037. *ptr = '\0';
  1038. remaining = static_cast<uint16_t>(remaining - ret);
  1039. if (remaining != 0)
  1040. continue;
  1041. readSucess = true;
  1042. if (allocReturn)
  1043. {
  1044. pData->tmpStr = pData->tmpBuf;
  1045. return pData->tmpStr.releaseBufferPointer();
  1046. }
  1047. return pData->tmpBuf;
  1048. }
  1049. }
  1050. if (ptr != pData->tmpBuf)
  1051. {
  1052. *ptr = '\0';
  1053. if (! allocReturn && ! tooBig)
  1054. {
  1055. readSucess = true;
  1056. return pData->tmpBuf;
  1057. }
  1058. pData->tmpStr += pData->tmpBuf;
  1059. }
  1060. else if (pData->tmpStr.isEmpty() && ret != 1)
  1061. {
  1062. // some error
  1063. return nullptr;
  1064. }
  1065. readSucess = true;
  1066. if (! allocReturn && ! tooBig)
  1067. return pData->tmpBuf;
  1068. return allocReturn ? pData->tmpStr.releaseBufferPointer() : pData->tmpStr.buffer();
  1069. }
  1070. const char* CarlaPipeCommon::_readlineblock(const bool allocReturn,
  1071. const uint16_t size,
  1072. const uint32_t timeOutMilliseconds) const noexcept
  1073. {
  1074. const uint32_t timeoutEnd = water::Time::getMillisecondCounter() + timeOutMilliseconds;
  1075. bool readSucess;
  1076. for (;;)
  1077. {
  1078. readSucess = false;
  1079. const char* const msg = _readline(allocReturn, size, readSucess);
  1080. if (readSucess)
  1081. return msg;
  1082. if (water::Time::getMillisecondCounter() >= timeoutEnd)
  1083. break;
  1084. carla_msleep(5);
  1085. }
  1086. static const bool testingForValgrind = std::getenv("CARLA_VALGRIND_TEST") != nullptr;
  1087. if (testingForValgrind)
  1088. {
  1089. const uint32_t timeoutEnd2 = water::Time::getMillisecondCounter() + 1000;
  1090. for (;;)
  1091. {
  1092. readSucess = false;
  1093. const char* const msg = _readline(allocReturn, size, readSucess);
  1094. if (readSucess)
  1095. return msg;
  1096. if (water::Time::getMillisecondCounter() >= timeoutEnd2)
  1097. break;
  1098. carla_msleep(100);
  1099. }
  1100. }
  1101. carla_stderr("readlineblock timed out");
  1102. return nullptr;
  1103. }
  1104. bool CarlaPipeCommon::_writeMsgBuffer(const char* const msg, const std::size_t size) const noexcept
  1105. {
  1106. if (pData->pipeClosed)
  1107. return false;
  1108. if (pData->pipeSend == INVALID_PIPE_VALUE)
  1109. {
  1110. carla_stderr2("CarlaPipe write error, isServer:%s, message was:\n%s", bool2str(pData->isServer), msg);
  1111. return false;
  1112. }
  1113. ssize_t ret;
  1114. try {
  1115. #ifdef CARLA_OS_WIN
  1116. ret = WriteFileWin32(pData->pipeSend, pData->ovSend, msg, size);
  1117. #else
  1118. ret = ::write(pData->pipeSend, msg, size);
  1119. #endif
  1120. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::writeMsgBuffer", false);
  1121. #ifdef CARLA_OS_WIN
  1122. if (ret == -2)
  1123. {
  1124. pData->pipeClosed = true;
  1125. return false;
  1126. }
  1127. #endif
  1128. if (ret == static_cast<ssize_t>(size))
  1129. {
  1130. if (pData->lastMessageFailed)
  1131. pData->lastMessageFailed = false;
  1132. return true;
  1133. }
  1134. if (! pData->lastMessageFailed)
  1135. {
  1136. pData->lastMessageFailed = true;
  1137. fprintf(stderr,
  1138. "CarlaPipeCommon::_writeMsgBuffer(..., " P_SIZE ") - failed with " P_SSIZE " (%s), message was:\n%s",
  1139. size, ret, bool2str(pData->isServer), msg);
  1140. }
  1141. return false;
  1142. }
  1143. // -----------------------------------------------------------------------
  1144. CarlaPipeServer::CarlaPipeServer() noexcept
  1145. : CarlaPipeCommon()
  1146. {
  1147. carla_debug("CarlaPipeServer::CarlaPipeServer()");
  1148. pData->isServer = true;
  1149. }
  1150. CarlaPipeServer::~CarlaPipeServer() /*noexcept*/
  1151. {
  1152. carla_debug("CarlaPipeServer::~CarlaPipeServer()");
  1153. stopPipeServer(5*1000);
  1154. }
  1155. uintptr_t CarlaPipeServer::getPID() const noexcept
  1156. {
  1157. #ifndef CARLA_OS_WIN
  1158. return static_cast<uintptr_t>(pData->pid);
  1159. #else
  1160. return 0;
  1161. #endif
  1162. }
  1163. // --------------------------------------------------------------------------------------------------------------------
  1164. bool CarlaPipeServer::startPipeServer(const char* const filename,
  1165. const char* const arg1,
  1166. const char* const arg2,
  1167. const int size) noexcept
  1168. {
  1169. CARLA_SAFE_ASSERT_RETURN(pData->pipeRecv == INVALID_PIPE_VALUE, false);
  1170. CARLA_SAFE_ASSERT_RETURN(pData->pipeSend == INVALID_PIPE_VALUE, false);
  1171. #ifdef CARLA_OS_WIN
  1172. CARLA_SAFE_ASSERT_RETURN(pData->processInfo.hThread == INVALID_HANDLE_VALUE, false);
  1173. CARLA_SAFE_ASSERT_RETURN(pData->processInfo.hProcess == INVALID_HANDLE_VALUE, false);
  1174. #else
  1175. CARLA_SAFE_ASSERT_RETURN(pData->pid == -1, false);
  1176. #endif
  1177. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  1178. CARLA_SAFE_ASSERT_RETURN(arg1 != nullptr, false);
  1179. CARLA_SAFE_ASSERT_RETURN(arg2 != nullptr, false);
  1180. carla_debug("CarlaPipeServer::startPipeServer(\"%s\", \"%s\", \"%s\")", filename, arg1, arg2);
  1181. char pipeRecvServerStr[100+1];
  1182. char pipeSendServerStr[100+1];
  1183. char pipeRecvClientStr[100+1];
  1184. char pipeSendClientStr[100+1];
  1185. pipeRecvServerStr[100] = '\0';
  1186. pipeSendServerStr[100] = '\0';
  1187. pipeRecvClientStr[100] = '\0';
  1188. pipeSendClientStr[100] = '\0';
  1189. const CarlaMutexLocker cml(pData->writeLock);
  1190. //-----------------------------------------------------------------------------------------------------------------
  1191. // create pipes
  1192. #ifdef CARLA_OS_WIN
  1193. HANDLE pipe1, pipe2;
  1194. std::srand(static_cast<uint>(std::time(nullptr)));
  1195. static ulong sCounter = 0;
  1196. ++sCounter;
  1197. const int randint = std::rand();
  1198. std::snprintf(pipeRecvServerStr, 100, "\\\\.\\pipe\\carla-pipe1-%i-%li", randint, sCounter);
  1199. std::snprintf(pipeSendServerStr, 100, "\\\\.\\pipe\\carla-pipe2-%i-%li", randint, sCounter);
  1200. std::snprintf(pipeRecvClientStr, 100, "ignored");
  1201. std::snprintf(pipeSendClientStr, 100, "ignored");
  1202. SECURITY_ATTRIBUTES sa;
  1203. carla_zeroStruct(sa);
  1204. sa.nLength = sizeof(sa);
  1205. sa.bInheritHandle = TRUE;
  1206. pipe1 = ::CreateNamedPipeA(pipeRecvServerStr, PIPE_ACCESS_DUPLEX|FILE_FLAG_FIRST_PIPE_INSTANCE|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE|PIPE_READMODE_BYTE, 1, size, size, 0, &sa);
  1207. if (pipe1 == INVALID_HANDLE_VALUE)
  1208. {
  1209. fail("pipe creation failed");
  1210. return false;
  1211. }
  1212. pipe2 = ::CreateNamedPipeA(pipeSendServerStr, PIPE_ACCESS_DUPLEX|FILE_FLAG_FIRST_PIPE_INSTANCE|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE|PIPE_READMODE_BYTE, 1, size, size, 0, &sa);
  1213. if (pipe2 == INVALID_HANDLE_VALUE)
  1214. {
  1215. try { ::CloseHandle(pipe1); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1)");
  1216. fail("pipe creation failed");
  1217. return false;
  1218. }
  1219. const HANDLE pipeRecvClient = pipe2;
  1220. const HANDLE pipeSendClient = pipe1;
  1221. #else
  1222. int pipe1[2]; // read by server, written by client
  1223. int pipe2[2]; // read by client, written by server
  1224. if (::pipe(pipe1) != 0)
  1225. {
  1226. fail("pipe1 creation failed");
  1227. return false;
  1228. }
  1229. if (::pipe(pipe2) != 0)
  1230. {
  1231. try { ::close(pipe1[0]); } CARLA_SAFE_EXCEPTION("close(pipe1[0])");
  1232. try { ::close(pipe1[1]); } CARLA_SAFE_EXCEPTION("close(pipe1[1])");
  1233. fail("pipe2 creation failed");
  1234. return false;
  1235. }
  1236. /* */ int pipeRecvServer = pipe1[0];
  1237. /* */ int pipeSendServer = pipe2[1];
  1238. const int pipeRecvClient = pipe2[0];
  1239. const int pipeSendClient = pipe1[1];
  1240. std::snprintf(pipeRecvServerStr, 100, "%i", pipeRecvServer);
  1241. std::snprintf(pipeSendServerStr, 100, "%i", pipeSendServer);
  1242. std::snprintf(pipeRecvClientStr, 100, "%i", pipeRecvClient);
  1243. std::snprintf(pipeSendClientStr, 100, "%i", pipeSendClient);
  1244. //-----------------------------------------------------------------------------------------------------------------
  1245. // set size, non-fatal
  1246. # ifdef CARLA_OS_LINUX
  1247. try {
  1248. ::fcntl(pipeRecvClient, F_SETPIPE_SZ, size);
  1249. } CARLA_SAFE_EXCEPTION("Set pipe size");
  1250. try {
  1251. ::fcntl(pipeRecvServer, F_SETPIPE_SZ, size);
  1252. } CARLA_SAFE_EXCEPTION("Set pipe size");
  1253. # endif
  1254. //-----------------------------------------------------------------------------------------------------------------
  1255. // set non-block
  1256. int ret;
  1257. try {
  1258. ret = ::fcntl(pipeRecvClient, F_SETFL, ::fcntl(pipeRecvClient, F_GETFL) | O_NONBLOCK);
  1259. } catch (...) {
  1260. ret = -1;
  1261. fail("failed to set pipe as non-block");
  1262. }
  1263. if (ret == 0)
  1264. {
  1265. try {
  1266. ret = ::fcntl(pipeRecvServer, F_SETFL, ::fcntl(pipeRecvServer, F_GETFL) | O_NONBLOCK);
  1267. } catch (...) {
  1268. ret = -1;
  1269. fail("failed to set pipe as non-block");
  1270. }
  1271. }
  1272. if (ret < 0)
  1273. {
  1274. try { ::close(pipe1[0]); } CARLA_SAFE_EXCEPTION("close(pipe1[0])");
  1275. try { ::close(pipe1[1]); } CARLA_SAFE_EXCEPTION("close(pipe1[1])");
  1276. try { ::close(pipe2[0]); } CARLA_SAFE_EXCEPTION("close(pipe2[0])");
  1277. try { ::close(pipe2[1]); } CARLA_SAFE_EXCEPTION("close(pipe2[1])");
  1278. return false;
  1279. }
  1280. #endif
  1281. //-----------------------------------------------------------------------------------------------------------------
  1282. // set arguments
  1283. const char* argv[8];
  1284. //-----------------------------------------------------------------------------------------------------------------
  1285. // argv[0] => filename
  1286. argv[0] = filename;
  1287. //-----------------------------------------------------------------------------------------------------------------
  1288. // argv[1-2] => args
  1289. argv[1] = arg1;
  1290. argv[2] = arg2;
  1291. //-----------------------------------------------------------------------------------------------------------------
  1292. // argv[3-6] => pipes
  1293. argv[3] = pipeRecvServerStr;
  1294. argv[4] = pipeSendServerStr;
  1295. argv[5] = pipeRecvClientStr;
  1296. argv[6] = pipeSendClientStr;
  1297. //-----------------------------------------------------------------------------------------------------------------
  1298. // argv[7] => null
  1299. argv[7] = nullptr;
  1300. //-----------------------------------------------------------------------------------------------------------------
  1301. // start process
  1302. #ifdef CARLA_OS_WIN
  1303. if (! startProcess(argv, &pData->processInfo))
  1304. {
  1305. carla_zeroStruct(pData->processInfo);
  1306. pData->processInfo.hProcess = INVALID_HANDLE_VALUE;
  1307. pData->processInfo.hThread = INVALID_HANDLE_VALUE;
  1308. try { ::CloseHandle(pipe1); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe1)");
  1309. try { ::CloseHandle(pipe2); } CARLA_SAFE_EXCEPTION("CloseHandle(pipe2)");
  1310. return false;
  1311. }
  1312. // just to make sure
  1313. CARLA_SAFE_ASSERT(pData->processInfo.hThread != INVALID_HANDLE_VALUE);
  1314. CARLA_SAFE_ASSERT(pData->processInfo.hProcess != INVALID_HANDLE_VALUE);
  1315. #else
  1316. if (! startProcess(argv, pData->pid))
  1317. {
  1318. pData->pid = -1;
  1319. try { ::close(pipe1[0]); } CARLA_SAFE_EXCEPTION("close(pipe1[0])");
  1320. try { ::close(pipe1[1]); } CARLA_SAFE_EXCEPTION("close(pipe1[1])");
  1321. try { ::close(pipe2[0]); } CARLA_SAFE_EXCEPTION("close(pipe2[0])");
  1322. try { ::close(pipe2[1]); } CARLA_SAFE_EXCEPTION("close(pipe2[1])");
  1323. fail("startProcess() failed");
  1324. return false;
  1325. }
  1326. //-----------------------------------------------------------------------------------------------------------------
  1327. // close duplicated handles used by the client
  1328. try { ::close(pipeRecvServer); } CARLA_SAFE_EXCEPTION("close(pipeRecvServer)");
  1329. try { ::close(pipeSendServer); } CARLA_SAFE_EXCEPTION("close(pipeSendServer)");
  1330. #endif
  1331. //-----------------------------------------------------------------------------------------------------------------
  1332. // wait for client to say something
  1333. #ifdef CARLA_OS_WIN
  1334. void* const ovRecv = pData->ovRecv;
  1335. void* const process = pData->processInfo.hProcess;
  1336. #else
  1337. void* const ovRecv = nullptr;
  1338. void* const process = nullptr;
  1339. #endif
  1340. if (waitForClientFirstMessage(pipeRecvClient, ovRecv, process, 10*1000 /* 10 secs */))
  1341. {
  1342. pData->pipeRecv = pipeRecvClient;
  1343. pData->pipeSend = pipeSendClient;
  1344. pData->pipeClosed = false;
  1345. carla_stdout("ALL OK!");
  1346. return true;
  1347. }
  1348. //-----------------------------------------------------------------------------------------------------------------
  1349. // failed to set non-block or get first child message, cannot continue
  1350. #ifdef CARLA_OS_WIN
  1351. if (::TerminateProcess(pData->processInfo.hProcess, 9) != FALSE)
  1352. {
  1353. // wait for process to stop
  1354. waitForProcessToStop(pData->processInfo.hProcess, 2*1000, false);
  1355. }
  1356. // clear pData->processInfo
  1357. try { ::CloseHandle(pData->processInfo.hThread); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hThread)");
  1358. try { ::CloseHandle(pData->processInfo.hProcess); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hProcess)");
  1359. carla_zeroStruct(pData->processInfo);
  1360. pData->processInfo.hProcess = INVALID_HANDLE_VALUE;
  1361. pData->processInfo.hThread = INVALID_HANDLE_VALUE;
  1362. #else
  1363. if (::kill(pData->pid, SIGKILL) != -1)
  1364. {
  1365. // wait for killing to take place
  1366. waitForChildToStop(pData->pid, 2*1000, false);
  1367. }
  1368. pData->pid = -1;
  1369. #endif
  1370. //-----------------------------------------------------------------------------------------------------------------
  1371. // close pipes
  1372. #ifdef CARLA_OS_WIN
  1373. try { ::CloseHandle(pipeRecvClient); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeRecvClient)");
  1374. try { ::CloseHandle(pipeSendClient); } CARLA_SAFE_EXCEPTION("CloseHandle(pipeSendClient)");
  1375. #else
  1376. try { ::close (pipeRecvClient); } CARLA_SAFE_EXCEPTION("close(pipeRecvClient)");
  1377. try { ::close (pipeSendClient); } CARLA_SAFE_EXCEPTION("close(pipeSendClient)");
  1378. #endif
  1379. return false;
  1380. // maybe unused
  1381. (void)size; (void)ovRecv; (void)process;
  1382. }
  1383. void CarlaPipeServer::stopPipeServer(const uint32_t timeOutMilliseconds) noexcept
  1384. {
  1385. carla_debug("CarlaPipeServer::stopPipeServer(%i)", timeOutMilliseconds);
  1386. #ifdef CARLA_OS_WIN
  1387. if (pData->processInfo.hThread != INVALID_HANDLE_VALUE || pData->processInfo.hProcess != INVALID_HANDLE_VALUE)
  1388. {
  1389. const CarlaMutexLocker cml(pData->writeLock);
  1390. if (pData->pipeSend != INVALID_PIPE_VALUE && ! pData->pipeClosed)
  1391. {
  1392. if (_writeMsgBuffer("__carla-quit__\n", 15))
  1393. flushMessages();
  1394. }
  1395. waitForProcessToStopOrKillIt(pData->processInfo.hProcess, timeOutMilliseconds);
  1396. try { ::CloseHandle(pData->processInfo.hThread); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hThread)");
  1397. try { ::CloseHandle(pData->processInfo.hProcess); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->processInfo.hProcess)");
  1398. carla_zeroStruct(pData->processInfo);
  1399. pData->processInfo.hProcess = INVALID_HANDLE_VALUE;
  1400. pData->processInfo.hThread = INVALID_HANDLE_VALUE;
  1401. }
  1402. #else
  1403. if (pData->pid != -1)
  1404. {
  1405. const CarlaMutexLocker cml(pData->writeLock);
  1406. if (pData->pipeSend != INVALID_PIPE_VALUE && ! pData->pipeClosed)
  1407. {
  1408. if (_writeMsgBuffer("__carla-quit__\n", 15))
  1409. flushMessages();
  1410. }
  1411. waitForChildToStopOrKillIt(pData->pid, timeOutMilliseconds);
  1412. pData->pid = -1;
  1413. }
  1414. #endif
  1415. closePipeServer();
  1416. }
  1417. void CarlaPipeServer::closePipeServer() noexcept
  1418. {
  1419. carla_debug("CarlaPipeServer::closePipeServer()");
  1420. pData->pipeClosed = true;
  1421. const CarlaMutexLocker cml(pData->writeLock);
  1422. if (pData->pipeRecv != INVALID_PIPE_VALUE)
  1423. {
  1424. #ifdef CARLA_OS_WIN
  1425. DisconnectNamedPipe(pData->pipeRecv);
  1426. try { ::CloseHandle(pData->pipeRecv); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeRecv)");
  1427. #else
  1428. try { ::close (pData->pipeRecv); } CARLA_SAFE_EXCEPTION("close(pData->pipeRecv)");
  1429. #endif
  1430. pData->pipeRecv = INVALID_PIPE_VALUE;
  1431. }
  1432. if (pData->pipeSend != INVALID_PIPE_VALUE)
  1433. {
  1434. #ifdef CARLA_OS_WIN
  1435. DisconnectNamedPipe(pData->pipeSend);
  1436. try { ::CloseHandle(pData->pipeSend); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeSend)");
  1437. #else
  1438. try { ::close (pData->pipeSend); } CARLA_SAFE_EXCEPTION("close(pData->pipeSend)");
  1439. #endif
  1440. pData->pipeSend = INVALID_PIPE_VALUE;
  1441. }
  1442. }
  1443. void CarlaPipeServer::writeShowMessage() const noexcept
  1444. {
  1445. const CarlaMutexLocker cml(pData->writeLock);
  1446. if (! _writeMsgBuffer("show\n", 5))
  1447. return;
  1448. flushMessages();
  1449. }
  1450. void CarlaPipeServer::writeFocusMessage() const noexcept
  1451. {
  1452. const CarlaMutexLocker cml(pData->writeLock);
  1453. if (! _writeMsgBuffer("focus\n", 6))
  1454. return;
  1455. flushMessages();
  1456. }
  1457. void CarlaPipeServer::writeHideMessage() const noexcept
  1458. {
  1459. const CarlaMutexLocker cml(pData->writeLock);
  1460. if (! _writeMsgBuffer("show\n", 5))
  1461. return;
  1462. flushMessages();
  1463. }
  1464. // -----------------------------------------------------------------------
  1465. CarlaPipeClient::CarlaPipeClient() noexcept
  1466. : CarlaPipeCommon()
  1467. {
  1468. carla_debug("CarlaPipeClient::CarlaPipeClient()");
  1469. }
  1470. CarlaPipeClient::~CarlaPipeClient() /*noexcept*/
  1471. {
  1472. carla_debug("CarlaPipeClient::~CarlaPipeClient()");
  1473. closePipeClient();
  1474. }
  1475. bool CarlaPipeClient::initPipeClient(const char* argv[]) noexcept
  1476. {
  1477. CARLA_SAFE_ASSERT_RETURN(pData->pipeRecv == INVALID_PIPE_VALUE, false);
  1478. CARLA_SAFE_ASSERT_RETURN(pData->pipeSend == INVALID_PIPE_VALUE, false);
  1479. carla_debug("CarlaPipeClient::initPipeClient(%p)", argv);
  1480. const CarlaMutexLocker cml(pData->writeLock);
  1481. //----------------------------------------------------------------
  1482. // read arguments
  1483. #ifdef CARLA_OS_WIN
  1484. const char* const pipeRecvServerStr = argv[3];
  1485. const char* const pipeSendServerStr = argv[4];
  1486. HANDLE pipeRecvServer = ::CreateFileA(pipeRecvServerStr, GENERIC_READ, 0x0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  1487. HANDLE pipeSendServer = ::CreateFileA(pipeSendServerStr, GENERIC_WRITE, 0x0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  1488. CARLA_SAFE_ASSERT_RETURN(pipeRecvServer != INVALID_HANDLE_VALUE, false);
  1489. CARLA_SAFE_ASSERT_RETURN(pipeSendServer != INVALID_HANDLE_VALUE, false);
  1490. #else
  1491. const int pipeRecvServer = std::atoi(argv[3]);
  1492. const int pipeSendServer = std::atoi(argv[4]);
  1493. /* */ int pipeRecvClient = std::atoi(argv[5]);
  1494. /* */ int pipeSendClient = std::atoi(argv[6]);
  1495. CARLA_SAFE_ASSERT_RETURN(pipeRecvServer > 0, false);
  1496. CARLA_SAFE_ASSERT_RETURN(pipeSendServer > 0, false);
  1497. CARLA_SAFE_ASSERT_RETURN(pipeRecvClient > 0, false);
  1498. CARLA_SAFE_ASSERT_RETURN(pipeSendClient > 0, false);
  1499. //----------------------------------------------------------------
  1500. // close duplicated handles used by the client
  1501. try { ::close(pipeRecvClient); } CARLA_SAFE_EXCEPTION("close(pipeRecvClient)");
  1502. try { ::close(pipeSendClient); } CARLA_SAFE_EXCEPTION("close(pipeSendClient)");
  1503. //----------------------------------------------------------------
  1504. // kill ourselves if parent dies
  1505. carla_terminateProcessOnParentExit(false);
  1506. #endif
  1507. //----------------------------------------------------------------
  1508. // done
  1509. pData->pipeRecv = pipeRecvServer;
  1510. pData->pipeSend = pipeSendServer;
  1511. pData->pipeClosed = false;
  1512. pData->clientClosingDown = false;
  1513. if (writeMessage("\n", 1))
  1514. flushMessages();
  1515. return true;
  1516. }
  1517. void CarlaPipeClient::closePipeClient() noexcept
  1518. {
  1519. carla_debug("CarlaPipeClient::closePipeClient()");
  1520. pData->pipeClosed = true;
  1521. const CarlaMutexLocker cml(pData->writeLock);
  1522. if (pData->pipeRecv != INVALID_PIPE_VALUE)
  1523. {
  1524. #ifdef CARLA_OS_WIN
  1525. try { ::CloseHandle(pData->pipeRecv); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeRecv)");
  1526. #else
  1527. try { ::close (pData->pipeRecv); } CARLA_SAFE_EXCEPTION("close(pData->pipeRecv)");
  1528. #endif
  1529. pData->pipeRecv = INVALID_PIPE_VALUE;
  1530. }
  1531. if (pData->pipeSend != INVALID_PIPE_VALUE)
  1532. {
  1533. #ifdef CARLA_OS_WIN
  1534. try { ::CloseHandle(pData->pipeSend); } CARLA_SAFE_EXCEPTION("CloseHandle(pData->pipeSend)");
  1535. #else
  1536. try { ::close (pData->pipeSend); } CARLA_SAFE_EXCEPTION("close(pData->pipeSend)");
  1537. #endif
  1538. pData->pipeSend = INVALID_PIPE_VALUE;
  1539. }
  1540. }
  1541. void CarlaPipeClient::writeExitingMessageAndWait() noexcept
  1542. {
  1543. {
  1544. const CarlaMutexLocker cml(pData->writeLock);
  1545. if (_writeMsgBuffer("exiting\n", 8))
  1546. flushMessages();
  1547. }
  1548. // NOTE: no more messages are handled after this point
  1549. pData->clientClosingDown = true;
  1550. for (int i=0; i < 100 && ! pData->pipeClosed; ++i)
  1551. {
  1552. carla_msleep(50);
  1553. idlePipe(true);
  1554. }
  1555. if (! pData->pipeClosed)
  1556. carla_stderr2("writeExitingMessageAndWait pipe is still running!");
  1557. }
  1558. // -----------------------------------------------------------------------
  1559. #undef INVALID_PIPE_VALUE