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 52KB

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