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

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