18#include "CarlaPipeUtils.hpp"
19#include "CarlaProcessUtils.hpp"
20#include "CarlaString.hpp"
21#include "CarlaTimeUtils.hpp"
46# include <sys/prctl.h>
48# define F_SETPIPE_SZ 1031
54# define INVALID_PIPE_VALUE INVALID_HANDLE_VALUE
56# define INVALID_PIPE_VALUE -1
70 for (
int i=20000; --
i>=0;)
72 if (
process != INVALID_HANDLE_VALUE)
78 carla_stderr(
"waitForAsyncObject process has stopped");
83 carla_debug(
"waitForAsyncObject loop start");
84 dw = ::MsgWaitForMultipleObjectsEx(1, &
object,
INFINITE, QS_POSTMESSAGE|QS_TIMER, 0);
85 carla_debug(
"waitForAsyncObject initial code is: %u", dw);
89 carla_debug(
"waitForAsyncObject WAIT_OBJECT_0");
93 dw2 = ::GetLastError();
97 carla_debug(
"waitForAsyncObject loop +1");
99 while (::PeekMessage(&
msg,
nullptr, 0, 0, PM_REMOVE))
100 ::DispatchMessage(&
msg);
107 carla_debug(
"waitForAsyncObject loop stop");
111 carla_stderr2(
"waitForAsyncObject loop end reached, error was: %u", dw2);
115 carla_stderr2(
"waitForAsyncObject reached the end, this should not happen");
120ssize_t ReadFileWin32(
const HANDLE pipeh,
const HANDLE event,
void*
const buf,
const DWORD numBytes)
122 DWORD dw, dsize = numBytes;
125 if (::PeekNamedPipe(pipeh,
nullptr, 0,
nullptr, &available,
nullptr) ==
FALSE || available == 0)
129 carla_zeroStruct(ov);
132 if (::ReadFile(pipeh, buf, dsize,
nullptr, &ov))
134 if (! ::GetOverlappedResult(pipeh, &ov, &dw,
FALSE))
136 carla_stderr(
"ReadFileWin32 GetOverlappedResult failed, error was: %u", ::GetLastError());
140 return static_cast<ssize_t
>(dsize);
143 dw = ::GetLastError();
145 if (dw == ERROR_IO_PENDING)
147 if (! waitForAsyncObject(event))
149 carla_stderr(
"ReadFileWin32 waitForAsyncObject failed, error was: %u", ::GetLastError());
153 if (! ::GetOverlappedResult(pipeh, &ov, &dw,
FALSE))
155 carla_stderr(
"ReadFileWin32 GetOverlappedResult of pending failed, error was: %u", ::GetLastError());
159 return static_cast<ssize_t
>(dsize);
162 carla_stderr(
"ReadFileWin32 failed, error was: %u", dw);
167ssize_t WriteFileWin32(
const HANDLE pipeh,
const HANDLE event,
const void*
const buf,
const DWORD numBytes)
169 DWORD dw, dsize = numBytes;
172 carla_zeroStruct(ov);
175 if (::WriteFile(pipeh, buf, dsize,
nullptr, &ov))
177 if (! ::GetOverlappedResult(pipeh, &ov, &dw,
FALSE))
179 carla_stderr(
"WriteFileWin32 GetOverlappedResult failed, error was: %u", ::GetLastError());
183 return static_cast<ssize_t
>(dsize);
186 dw = ::GetLastError();
188 if (dw == ERROR_IO_PENDING)
190 if (! waitForAsyncObject(event))
192 carla_stderr(
"WriteFileWin32 waitForAsyncObject failed, error was: %u", ::GetLastError());
196 if (! ::GetOverlappedResult(pipeh, &ov, &dw,
FALSE))
198 carla_stderr(
"WriteFileWin32 GetOverlappedResult of pending failed, error was: %u", ::GetLastError());
202 return static_cast<ssize_t
>(dsize);
205 if (dw == ERROR_PIPE_NOT_CONNECTED)
207 carla_stdout(
"WriteFileWin32 failed, client has closed");
211 carla_stderr(
"WriteFileWin32 failed, error was: %u", dw);
221bool startProcess(
const char*
const argv[], PROCESS_INFORMATION*
const processInfo)
229 for (
int i=0;
argv[
i] !=
nullptr; ++
i)
235 if (
arg.containsAnyOf(
"\" "))
236 arg =
arg.replace(
"\"",
"\\\"").quoted();
238 command <<
arg <<
' ';
241 command = command.
trim();
243 STARTUPINFOA startupInfo;
244 carla_zeroStruct(startupInfo);
245 startupInfo.cb =
sizeof(startupInfo);
247 if (::CreateProcessA(
nullptr,
const_cast<LPSTR>(command.
toRawUTF8()),
248 nullptr,
nullptr,
TRUE, CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT,
249 nullptr,
nullptr, &startupInfo, processInfo) !=
FALSE)
252 carla_stderr2(
"startProcess failed, error was: %u", ::GetLastError());
265 carla_zeroStruct(ov);
268 const uint32_t timeoutEnd = carla_gettime_ms() + timeOutMilliseconds;
272 if (::ConnectNamedPipe(pipe, &ov))
274 if (! ::GetOverlappedResult(pipe, &ov, &dw,
FALSE))
276 carla_stderr2(
"ConnectNamedPipe GetOverlappedResult failed, error was: %u", ::GetLastError());
283 const DWORD err = ::GetLastError();
287 case ERROR_PIPE_CONNECTED:
290 case ERROR_IO_PENDING:
291 if (! waitForAsyncObject(event,
process))
293 carla_stderr2(
"ConnectNamedPipe waitForAsyncObject failed, error was: %u", ::GetLastError());
297 if (! ::GetOverlappedResult(pipe, &ov, &dw,
FALSE))
299 carla_stderr2(
"ConnectNamedPipe GetOverlappedResult of pending failed, error was: %u", ::GetLastError());
305 case ERROR_PIPE_LISTENING:
306 if (carla_gettime_ms() < timeoutEnd)
311 carla_stderr2(
"ConnectNamedPipe listening timed out");
315 carla_stderr2(
"ConnectNamedPipe failed, error was: %u", err);
326 const CarlaScopedEnvVar sev1(
"LD_LIBRARY_PATH",
nullptr);
327 const CarlaScopedEnvVar sev2(
"LD_PRELOAD",
nullptr);
330 #pragma clang diagnostic push
331 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
333 const pid_t ret = pidinst = vfork();
335 #pragma clang diagnostic pop
343 CarlaString
error(std::strerror(errno));
344 carla_stderr2(
"exec failed: %s",
error.buffer());
350 CarlaString
error(std::strerror(errno));
351 carla_stderr2(
"vfork() failed: %s",
error.buffer());
371 const uint32_t timeoutEnd = carla_gettime_ms() + timeOutMilliseconds;
382 ret = ReadFileWin32(pipe, (
HANDLE)ovRecv, &
c, 1);
394 carla_stderr(
"waitForClientFirstMessage() - read has wrong first char '%c'",
c);
return false;
399 if (::GetLastError() == ERROR_NO_DATA)
404 if (carla_gettime_ms() < timeoutEnd)
409 carla_stderr(
"waitForClientFirstMessage() - read timed out");
414 carla_stderr(
"waitForClientFirstMessage() - read failed");
416 CarlaString
error(std::strerror(errno));
417 carla_stderr(
"waitForClientFirstMessage() - read failed: %s",
error.buffer());
423 carla_stderr(
"waitForClientFirstMessage() - read returned %i",
int(ret));
437bool waitForProcessToStop(
const HANDLE process,
const uint32_t timeOutMilliseconds,
bool sendTerminate)
noexcept
442 const uint32_t timeoutEnd = carla_gettime_ms() + timeOutMilliseconds;
455 sendTerminate =
false;
456 ::TerminateProcess(
process, 15);
459 if (carla_gettime_ms() >= timeoutEnd)
474 if (! waitForProcessToStop(
process, timeOutMilliseconds,
true))
476 carla_stderr(
"waitForProcessToStopOrKillIt() - process didn't stop, force termination");
481 waitForProcessToStop(
process, timeOutMilliseconds,
false);
493 const uint32_t timeoutEnd = carla_gettime_ms() + timeOutMilliseconds;
498 ret = ::waitpid(pid,
nullptr, WNOHANG);
511 CarlaString
error(std::strerror(errno));
512 carla_stderr(
"waitForChildToStop() - waitpid failed: %s",
error.buffer());
520 sendTerminate =
false;
521 ::kill(pid, SIGTERM);
523 if (carla_gettime_ms() < timeoutEnd)
528 carla_stderr(
"waitForChildToStop() - timed out");
539 carla_stderr(
"waitForChildToStop() - got wrong pid %i (requested was %i)",
int(ret),
int(pid));
558 carla_stderr(
"waitForChildToStopOrKillIt() - process didn't stop, force killing");
560 if (::kill(pid, SIGKILL) != -1)
567 CarlaString
error(std::strerror(errno));
568 carla_stderr(
"waitForChildToStopOrKillIt() - kill failed: %s",
error.buffer());
579 PROCESS_INFORMATION processInfo;
630 carla_zeroStruct(processInfo);
631 processInfo.hProcess = INVALID_HANDLE_VALUE;
632 processInfo.hThread = INVALID_HANDLE_VALUE;
638 carla_zeroChars(
tmpBuf, 0xffff);
646CarlaPipeCommon::CarlaPipeCommon()
noexcept
647 : pData(new PrivateData())
649 carla_debug(
"CarlaPipeCommon::CarlaPipeCommon()");
652CarlaPipeCommon::~CarlaPipeCommon()
654 carla_debug(
"CarlaPipeCommon::~CarlaPipeCommon()");
666void CarlaPipeCommon::idlePipe(
const bool onlyOnce)
noexcept
673 const char*
const msg = _readline(
true, 0, readSucess);
680 pData->isReading =
true;
682 if (std::strcmp(
msg,
"__carla-quit__") == 0)
684 pData->pipeClosed =
true;
686 else if (! pData->clientClosingDown)
693 pData->isReading =
false;
695 std::free(
const_cast<char*
>(
msg));
706 pData->writeLock.lock();
711 return pData->writeLock.tryLock();
716 pData->writeLock.unlock();
721 return pData->writeLock;
726bool CarlaPipeCommon::readNextLineAsBool(
bool&
value)
const noexcept
730 if (
const char*
const msg = _readlineblock(
false))
732 value = (std::strcmp(
msg,
"true") == 0);
739bool CarlaPipeCommon::readNextLineAsByte(
uint8_t&
value)
const noexcept
743 if (
const char*
const msg = _readlineblock(
false))
745 const int asint = std::atoi(
msg);
747 if (asint >= 0 && asint <= 0xFF)
757bool CarlaPipeCommon::readNextLineAsInt(
int32_t&
value)
const noexcept
761 if (
const char*
const msg = _readlineblock(
false))
770bool CarlaPipeCommon::readNextLineAsUInt(
uint32_t&
value)
const noexcept
774 if (
const char*
const msg = _readlineblock(
false))
776#if (defined(__WORDSIZE) && __WORDSIZE < 64) || (defined(__SIZE_WIDTH__) && __SIZE_WIDTH__ < 64) || \
777 defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
778 const long long aslong = std::atoll(
msg);
780 const long aslong = std::atol(
msg);
793bool CarlaPipeCommon::readNextLineAsLong(int64_t&
value)
const noexcept
797 if (
const char*
const msg = _readlineblock(
false))
806bool CarlaPipeCommon::readNextLineAsULong(uint64_t&
value)
const noexcept
810 if (
const char*
const msg = _readlineblock(
false))
812 const int64_t asint64 = std::atol(
msg);
816 value =
static_cast<uint64_t
>(asint64);
824bool CarlaPipeCommon::readNextLineAsFloat(
float&
value)
const noexcept
828 if (
const char*
const msg = _readlineblock(
false))
831 const CarlaScopedLocale csl;
832 value =
static_cast<float>(std::atof(
msg));
840bool CarlaPipeCommon::readNextLineAsDouble(
double&
value)
const noexcept
844 if (
const char*
const msg = _readlineblock(
false))
847 const CarlaScopedLocale csl;
856bool CarlaPipeCommon::readNextLineAsString(
const char*&
value,
const bool allocateString,
uint32_t size)
const noexcept
863 if (
const char*
const msg = _readlineblock(allocateString,
static_cast<uint16_t>(
size)))
876 return const_cast<char*
>(_readlineblock(
true, 0));
882bool CarlaPipeCommon::writeMessage(
const char*
const msg)
const noexcept
886 if (pData->pipeClosed)
889 const std::size_t
size(std::strlen(
msg));
893 return _writeMsgBuffer(
msg,
size);
896bool CarlaPipeCommon::writeMessage(
const char*
const msg, std::size_t
size)
const noexcept
902 if (pData->pipeClosed)
905 return _writeMsgBuffer(
msg,
size);
908bool CarlaPipeCommon::writeAndFixMessage(
const char*
const msg)
const noexcept
912 if (pData->pipeClosed)
915 const std::size_t
size(std::strlen(
msg));
917 char*
const fixedMsg =
static_cast<char*
>(std::malloc(
size+2));
922 std::strcpy(fixedMsg,
msg);
924 for (std::size_t
i=0;
i<
size; ++
i)
926 if (fixedMsg[
i] ==
'\n')
930 if (fixedMsg[
size-1] ==
'\r')
932 fixedMsg[
size-1] =
'\n';
933 fixedMsg[
size ] =
'\0';
934 fixedMsg[
size+1] =
'\0';
938 fixedMsg[
size ] =
'\n';
939 fixedMsg[
size+1] =
'\0';
948 const bool ret = _writeMsgBuffer(fixedMsg,
size+1);
955 if (pData->pipeClosed)
958 return _writeMsgBuffer(
"\n", 1);
965#if defined(CARLA_OS_LINUX) || defined(CARLA_OS_GNU_HURD)
966# if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2014
968 return ::syncfs(pData->pipeSend) == 0;
973 return (::FlushFileBuffers(pData->pipeSend) !=
FALSE);
982bool CarlaPipeCommon::writeErrorMessage(
const char*
const error)
const noexcept
986 const CarlaMutexLocker cml(pData->writeLock);
988 if (! _writeMsgBuffer(
"error\n", 6))
990 if (! writeAndFixMessage(
error))
997bool CarlaPipeCommon::writeControlMessage(
const uint32_t index,
const float value,
const bool withWriteLock)
const noexcept
1001 const CarlaMutexLocker cml(pData->writeLock);
1002 return writeControlMessage(index,
value,
false);
1006 tmpBuf[0xfe] =
'\0';
1008 if (! _writeMsgBuffer(
"control\n", 8))
1011 std::snprintf(tmpBuf, 0xfe,
"%i\n", index);
1012 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1016 const CarlaScopedLocale csl;
1017 std::snprintf(tmpBuf, 0xfe,
"%.12g\n",
static_cast<double>(
value));
1020 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1027bool CarlaPipeCommon::writeConfigureMessage(
const char*
const key,
const char*
const value)
const noexcept
1032 const CarlaMutexLocker cml(pData->writeLock);
1034 if (! _writeMsgBuffer(
"configure\n", 10))
1036 if (! writeAndFixMessage(
key))
1038 if (! writeAndFixMessage(
value))
1045bool CarlaPipeCommon::writeProgramMessage(
const uint32_t index)
const noexcept
1048 tmpBuf[0xfe] =
'\0';
1050 const CarlaMutexLocker cml(pData->writeLock);
1052 if (! _writeMsgBuffer(
"program\n", 8))
1055 std::snprintf(tmpBuf, 0xfe,
"%i\n", index);
1056 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1063bool CarlaPipeCommon::writeProgramMessage(
const uint8_t channel,
const uint32_t bank,
const uint32_t program)
const noexcept
1066 tmpBuf[0xfe] =
'\0';
1068 const CarlaMutexLocker cml(pData->writeLock);
1070 if (! _writeMsgBuffer(
"program\n", 8))
1073 std::snprintf(tmpBuf, 0xfe,
"%i\n", channel);
1074 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1077 std::snprintf(tmpBuf, 0xfe,
"%i\n", bank);
1078 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1081 std::snprintf(tmpBuf, 0xfe,
"%i\n", program);
1082 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1089bool CarlaPipeCommon::writeMidiProgramMessage(
const uint32_t bank,
const uint32_t program)
const noexcept
1092 tmpBuf[0xfe] =
'\0';
1094 const CarlaMutexLocker cml(pData->writeLock);
1096 if (! _writeMsgBuffer(
"midiprogram\n", 12))
1099 std::snprintf(tmpBuf, 0xfe,
"%i\n", bank);
1100 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1103 std::snprintf(tmpBuf, 0xfe,
"%i\n", program);
1104 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1111bool CarlaPipeCommon::writeReloadProgramsMessage(
const int32_t index)
const noexcept
1114 tmpBuf[0xfe] =
'\0';
1116 const CarlaMutexLocker cml(pData->writeLock);
1118 if (! _writeMsgBuffer(
"reloadprograms\n", 15))
1121 std::snprintf(tmpBuf, 0xfe,
"%i\n", index);
1122 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1129bool CarlaPipeCommon::writeMidiNoteMessage(
const bool onOff,
const uint8_t channel,
const uint8_t note,
const uint8_t velocity)
const noexcept
1136 tmpBuf[0xfe] =
'\0';
1138 const CarlaMutexLocker cml(pData->writeLock);
1140 if (! _writeMsgBuffer(
"note\n", 5))
1143 std::snprintf(tmpBuf, 0xfe,
"%s\n", bool2str(onOff));
1144 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1147 std::snprintf(tmpBuf, 0xfe,
"%i\n", channel);
1148 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1151 std::snprintf(tmpBuf, 0xfe,
"%i\n", note);
1152 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1155 std::snprintf(tmpBuf, 0xfe,
"%i\n", velocity);
1156 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1163bool CarlaPipeCommon::writeLv2AtomMessage(
const uint32_t index,
const LV2_Atom*
const atom)
const noexcept
1168 tmpBuf[0xfe] =
'\0';
1171 CarlaString base64atom(CarlaString::asBase64(atom, atomTotalSize));
1173 const CarlaMutexLocker cml(pData->writeLock);
1175 if (! _writeMsgBuffer(
"atom\n", 5))
1178 std::snprintf(tmpBuf, 0xfe,
"%i\n", index);
1179 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1182 std::snprintf(tmpBuf, 0xfe,
"%i\n", atomTotalSize);
1183 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1186 std::snprintf(tmpBuf, 0xfe,
"%lu\n",
static_cast<long unsigned>(base64atom.length()));
1187 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1190 if (! writeAndFixMessage(base64atom.buffer()))
1197bool CarlaPipeCommon::writeLv2ParameterMessage(
const char*
const uri,
const float value,
const bool withWriteLock)
const noexcept
1201 const CarlaMutexLocker cml(pData->writeLock);
1202 return writeLv2ParameterMessage(
uri,
value,
false);
1206 tmpBuf[0xfe] =
'\0';
1208 if (! _writeMsgBuffer(
"parameter\n", 10))
1211 if (! writeAndFixMessage(
uri))
1215 const CarlaScopedLocale csl;
1216 std::snprintf(tmpBuf, 0xfe,
"%.12g\n",
static_cast<double>(
value));
1219 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1226bool CarlaPipeCommon::writeLv2UridMessage(
const uint32_t urid,
const char*
const uri)
const noexcept
1232 tmpBuf[0xfe] =
'\0';
1234 const CarlaMutexLocker cml(pData->writeLock);
1236 if (! _writeMsgBuffer(
"urid\n", 5))
1239 std::snprintf(tmpBuf, 0xfe,
"%i\n", urid);
1240 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1243 std::snprintf(tmpBuf, 0xfe,
"%lu\n",
static_cast<long unsigned>(std::strlen(
uri)));
1244 if (! _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)))
1247 if (! writeAndFixMessage(
uri))
1257const char* CarlaPipeCommon::_readline(
const bool allocReturn,
const uint16_t size,
bool& readSucess)
const noexcept
1262 char*
ptr = pData->tmpBuf;
1264 bool tooBig =
false;
1266 pData->tmpStr.clear();
1270 for (
int i=0;
i<0xfffe; ++
i)
1274 ret = ReadFileWin32(pData->pipeRecv, pData->ovRecv, &
c, 1);
1276 ret =
::read(pData->pipeRecv, &
c, 1);
1299 pData->tmpStr += pData->tmpBuf;
1300 ptr = pData->tmpBuf;
1313 ret = ReadFileWin32(pData->pipeRecv, pData->ovRecv, ptr, remaining);
1315 ret =
::read(pData->pipeRecv, ptr, remaining);
1319 if (ret == -1 && errno == EAGAIN)
1325 for (ssize_t
i=0;
i<ret; ++
i)
1333 remaining =
static_cast<uint16_t>(remaining - ret);
1342 pData->tmpStr = pData->tmpBuf;
1343 return pData->tmpStr.releaseBufferPointer();
1346 return pData->tmpBuf;
1350 if (ptr != pData->tmpBuf)
1354 if (! allocReturn && ! tooBig)
1357 return pData->tmpBuf;
1360 pData->tmpStr += pData->tmpBuf;
1362 else if (pData->tmpStr.isEmpty() && ret != 1)
1370 if (! allocReturn && ! tooBig)
1371 return pData->tmpBuf;
1373 return allocReturn ? pData->tmpStr.releaseBufferPointer() : pData->tmpStr.buffer();
1376const char* CarlaPipeCommon::_readlineblock(
const bool allocReturn,
1378 const uint32_t timeOutMilliseconds)
const noexcept
1380 const uint32_t timeoutEnd = carla_gettime_ms() + timeOutMilliseconds;
1386 const char*
const msg = _readline(allocReturn,
size, readSucess);
1391 if (carla_gettime_ms() >= timeoutEnd)
1397 static const bool testingForValgrind = std::getenv(
"CARLA_VALGRIND_TEST") !=
nullptr;
1399 if (testingForValgrind)
1401 const uint32_t timeoutEnd2 = carla_gettime_ms() + 1000;
1406 const char*
const msg = _readline(allocReturn,
size, readSucess);
1411 if (carla_gettime_ms() >= timeoutEnd2)
1418 carla_stderr(
"readlineblock timed out");
1422bool CarlaPipeCommon::_writeMsgBuffer(
const char*
const msg,
const std::size_t
size)
const noexcept
1424 if (pData->pipeClosed)
1429 carla_stderr2(
"CarlaPipe write error, isServer:%s, message was:\n%s", bool2str(pData->isServer),
msg);
1437 ret = WriteFileWin32(pData->pipeSend, pData->ovSend,
msg,
static_cast<DWORD>(
size));
1439 ret = ::write(pData->pipeSend,
msg,
size);
1446 pData->pipeClosed =
true;
1451 if (ret ==
static_cast<ssize_t
>(
size))
1453 if (pData->lastMessageFailed)
1454 pData->lastMessageFailed =
false;
1458 if (! pData->lastMessageFailed)
1460 pData->lastMessageFailed =
true;
1462 "CarlaPipeCommon::_writeMsgBuffer(..., " P_SIZE ") - failed with " P_SSIZE " (%s), message was:\n%s",
1463 size, ret, bool2str(pData->isServer),
msg);
1471CarlaPipeServer::CarlaPipeServer()
noexcept
1474 carla_debug(
"CarlaPipeServer::CarlaPipeServer()");
1475 pData->isServer =
true;
1478CarlaPipeServer::~CarlaPipeServer()
1480 carla_debug(
"CarlaPipeServer::~CarlaPipeServer()");
1482 stopPipeServer(5*1000);
1488 return static_cast<uintptr_t
>(pData->pid);
1496bool CarlaPipeServer::startPipeServer(
const char*
const helperTool,
1498 const char*
const arg1,
1499 const char*
const arg2,
1501 int timeOutMilliseconds)
noexcept
1514 carla_debug(
"CarlaPipeServer::startPipeServer(\"%s\", \"%s\", \"%s\")",
filename, arg1, arg2);
1516 if (timeOutMilliseconds < 0)
1517 timeOutMilliseconds = 10 * 1000;
1519 char pipeRecvServerStr[100+1];
1520 char pipeSendServerStr[100+1];
1521 char pipeRecvClientStr[100+1];
1522 char pipeSendClientStr[100+1];
1524 pipeRecvServerStr[100] =
'\0';
1525 pipeSendServerStr[100] =
'\0';
1526 pipeRecvClientStr[100] =
'\0';
1527 pipeSendClientStr[100] =
'\0';
1529 const CarlaMutexLocker cml(pData->writeLock);
1537 std::srand(
static_cast<uint>(std::time(
nullptr)));
1539 static ulong sCounter = 0;
1542 const int randint = std::rand();
1544 std::snprintf(pipeRecvServerStr, 100,
"\\\\.\\pipe\\carla-pipe1-%i-%li", randint, sCounter);
1545 std::snprintf(pipeSendServerStr, 100,
"\\\\.\\pipe\\carla-pipe2-%i-%li", randint, sCounter);
1546 std::snprintf(pipeRecvClientStr, 100,
"ignored");
1547 std::snprintf(pipeSendClientStr, 100,
"ignored");
1549 SECURITY_ATTRIBUTES sa;
1550 carla_zeroStruct(sa);
1551 sa.nLength =
sizeof(sa);
1552 sa.bInheritHandle =
TRUE;
1554 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);
1556 if (pipe1 == INVALID_HANDLE_VALUE)
1558 fail(
"pipe creation failed");
1562 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);
1564 if (pipe2 == INVALID_HANDLE_VALUE)
1567 fail(
"pipe creation failed");
1571 const HANDLE pipeRecvClient = pipe2;
1572 const HANDLE pipeSendClient = pipe1;
1577 if (::pipe(pipe1) != 0)
1579 fail(
"pipe1 creation failed");
1583 if (::pipe(pipe2) != 0)
1587 fail(
"pipe2 creation failed");
1591 int pipeRecvServer = pipe1[0];
1592 int pipeSendServer = pipe2[1];
1593 const int pipeRecvClient = pipe2[0];
1594 const int pipeSendClient = pipe1[1];
1596 std::snprintf(pipeRecvServerStr, 100,
"%i", pipeRecvServer);
1597 std::snprintf(pipeSendServerStr, 100,
"%i", pipeSendServer);
1598 std::snprintf(pipeRecvClientStr, 100,
"%i", pipeRecvClient);
1599 std::snprintf(pipeSendClientStr, 100,
"%i", pipeSendClient);
1604# ifdef CARLA_OS_LINUX
1606 ::fcntl(pipeRecvClient, F_SETPIPE_SZ,
size);
1610 ::fcntl(pipeRecvServer, F_SETPIPE_SZ,
size);
1620 ret = ::fcntl(pipeRecvClient, F_SETFL, ::fcntl(pipeRecvClient, F_GETFL) | O_NONBLOCK);
1623 fail(
"failed to set pipe as non-block");
1629 ret = ::fcntl(pipeRecvServer, F_SETFL, ::fcntl(pipeRecvServer, F_GETFL) | O_NONBLOCK);
1632 fail(
"failed to set pipe as non-block");
1649 const char*
argv[9] = {};
1652 if (helperTool !=
nullptr)
1653 argv[
i++] = helperTool;
1669 argv[
i++] = pipeRecvServerStr;
1670 argv[
i++] = pipeSendServerStr;
1671 argv[
i++] = pipeRecvClientStr;
1672 argv[
i++] = pipeSendClientStr;
1680 carla_zeroStruct(pData->processInfo);
1681 pData->processInfo.hProcess = INVALID_HANDLE_VALUE;
1682 pData->processInfo.hThread = INVALID_HANDLE_VALUE;
1699 fail(
"startProcess() failed");
1714 void*
const ovRecv = pData->ovRecv;
1715 void*
const process = pData->processInfo.hProcess;
1717 void*
const ovRecv =
nullptr;
1718 void*
const process =
nullptr;
1723 pData->pipeRecv = pipeRecvClient;
1724 pData->pipeSend = pipeSendClient;
1725 pData->pipeClosed =
false;
1726 carla_debug(
"ALL OK!");
1734 if (::TerminateProcess(pData->processInfo.hProcess, 9) !=
FALSE)
1737 waitForProcessToStop(pData->processInfo.hProcess, 2*1000,
false);
1743 carla_zeroStruct(pData->processInfo);
1744 pData->processInfo.hProcess = INVALID_HANDLE_VALUE;
1745 pData->processInfo.hThread = INVALID_HANDLE_VALUE;
1747 if (::kill(pData->pid, SIGKILL) != -1)
1772bool CarlaPipeServer::startPipeServer(
const char*
const filename,
1773 const char*
const arg1,
1774 const char*
const arg2,
1776 const int timeOutMilliseconds)
noexcept
1778 return startPipeServer(
nullptr,
filename, arg1, arg2,
size, timeOutMilliseconds);
1781void CarlaPipeServer::stopPipeServer(
const uint32_t timeOutMilliseconds)
noexcept
1783 carla_debug(
"CarlaPipeServer::stopPipeServer(%i)", timeOutMilliseconds);
1786 if (pData->processInfo.hThread != INVALID_HANDLE_VALUE || pData->processInfo.hProcess != INVALID_HANDLE_VALUE)
1788 const CarlaMutexLocker cml(pData->writeLock);
1792 if (_writeMsgBuffer(
"__carla-quit__\n", 15))
1796 waitForProcessToStopOrKillIt(pData->processInfo.hProcess, timeOutMilliseconds);
1799 carla_zeroStruct(pData->processInfo);
1800 pData->processInfo.hProcess = INVALID_HANDLE_VALUE;
1801 pData->processInfo.hThread = INVALID_HANDLE_VALUE;
1804 if (pData->pid != -1)
1806 const CarlaMutexLocker cml(pData->writeLock);
1810 if (_writeMsgBuffer(
"__carla-quit__\n", 15))
1822void CarlaPipeServer::closePipeServer()
noexcept
1824 carla_debug(
"CarlaPipeServer::closePipeServer()");
1826 pData->pipeClosed =
true;
1828 const CarlaMutexLocker cml(pData->writeLock);
1833 DisconnectNamedPipe(pData->pipeRecv);
1845 DisconnectNamedPipe(pData->pipeSend);
1857 const CarlaMutexLocker cml(pData->writeLock);
1859 if (! _writeMsgBuffer(
"show\n", 5))
1867 const CarlaMutexLocker cml(pData->writeLock);
1869 if (! _writeMsgBuffer(
"focus\n", 6))
1877 const CarlaMutexLocker cml(pData->writeLock);
1879 if (! _writeMsgBuffer(
"show\n", 5))
1887CarlaPipeClient::CarlaPipeClient()
noexcept
1890 carla_debug(
"CarlaPipeClient::CarlaPipeClient()");
1893CarlaPipeClient::~CarlaPipeClient()
1895 carla_debug(
"CarlaPipeClient::~CarlaPipeClient()");
1900bool CarlaPipeClient::initPipeClient(
const char*
argv[])
noexcept
1904 carla_debug(
"CarlaPipeClient::initPipeClient(%p)",
argv);
1906 const CarlaMutexLocker cml(pData->writeLock);
1912 const char*
const pipeRecvServerStr =
argv[3];
1913 const char*
const pipeSendServerStr =
argv[4];
1915 HANDLE pipeRecvServer = ::CreateFileA(pipeRecvServerStr, GENERIC_READ, 0x0,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
1916 HANDLE pipeSendServer = ::CreateFileA(pipeSendServerStr, GENERIC_WRITE, 0x0,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
1921 const int pipeRecvServer = std::atoi(
argv[3]);
1922 const int pipeSendServer = std::atoi(
argv[4]);
1923 int pipeRecvClient = std::atoi(
argv[5]);
1924 int pipeSendClient = std::atoi(
argv[6]);
1940 carla_terminateProcessOnParentExit(
false);
1946 pData->pipeRecv = pipeRecvServer;
1947 pData->pipeSend = pipeSendServer;
1948 pData->pipeClosed =
false;
1949 pData->clientClosingDown =
false;
1951 if (writeMessage(
"\n", 1))
1957void CarlaPipeClient::closePipeClient()
noexcept
1959 carla_debug(
"CarlaPipeClient::closePipeClient()");
1961 pData->pipeClosed =
true;
1963 const CarlaMutexLocker cml(pData->writeLock);
1986void CarlaPipeClient::writeExitingMessageAndWait()
noexcept
1989 const CarlaMutexLocker cml(pData->writeLock);
1991 if (_writeMsgBuffer(
"exiting\n", 8))
1996 pData->clientClosingDown =
true;
1998 for (
int i=0;
i < 100 && ! pData->pipeClosed; ++
i)
2004 if (! pData->pipeClosed)
2005 carla_stderr2(
"writeExitingMessageAndWait pipe is still running!");
2010#undef INVALID_PIPE_VALUE
#define P_SSIZE
Definition CarlaDefines.h:141
#define CARLA_SAFE_EXCEPTION(msg)
Definition CarlaDefines.h:228
#define CARLA_SAFE_EXCEPTION_RETURN(msg, ret)
Definition CarlaDefines.h:231
#define CARLA_SAFE_ASSERT_RETURN(cond, ret)
Definition CarlaDefines.h:190
unsigned int uint
Definition CarlaDefines.h:327
unsigned long int ulong
Definition CarlaDefines.h:328
#define CARLA_SAFE_EXCEPTION_BREAK(msg)
Definition CarlaDefines.h:229
#define P_SIZE
Definition CarlaDefines.h:140
#define CARLA_SAFE_ASSERT_INT2_RETURN(cond, v1, v2, ret)
Definition CarlaDefines.h:207
#define CARLA_SAFE_ASSERT(cond)
Definition CarlaDefines.h:182
#define CARLA_DECLARE_NON_COPYABLE(ClassName)
Definition CarlaDefines.h:242
#define MAX_MIDI_CHANNELS
Definition CarlaMIDI.h:21
#define MAX_MIDI_NOTE
Definition CarlaMIDI.h:22
#define MAX_MIDI_VALUE
Definition CarlaMIDI.h:23
#define noexcept
Definition DistrhoDefines.h:72
float arg(const fft_t *freqs, off_t x)
Definition OscilGen.cpp:58
void process(Alg_seq_ptr seq, bool tempo_flag, double tempo, bool flatten_flag)
Definition allegroconvert.cpp:42
Definition CarlaMutex.cpp:8
String trim() const
Definition String.cpp:1540
const char * toRawUTF8() const
Definition String.cpp:1925
register unsigned i
Definition inflate.c:1575
static char filename[]
Definition features.c:5
static PuglViewHint int value
Definition pugl.h:1708
char * argv[]
Definition unzip.c:738
static uint32_t lv2_atom_total_size(const LV2_Atom *atom)
Definition atom-util.h:55
unsigned short uint16_t
Definition mid.cpp:99
int int32_t
Definition mid.cpp:97
unsigned int uint32_t
Definition mid.cpp:100
unsigned char uint8_t
Definition mid.cpp:98
const char * msg
Definition missing_descriptor.c:20
jack_client_t client jack_client_t client jack_client_t client jack_client_t JackInfoShutdownCallback void arg jack_client_t jack_port_t port void func jack_client_t const char const char unsigned long flags const jack_port_t port jack_client_t jack_port_id_t port_id const jack_port_t const char port_name const jack_port_t port void * ptr
Definition juce_linux_JackAudio.cpp:79
#define P(protos)
Definition proto.h:37
Definition CarlaPipeUtils.cpp:576
CarlaString tmpStr
Definition CarlaPipeUtils.cpp:610
pid_t pid
Definition CarlaPipeUtils.cpp:585
int pipeSend
Definition CarlaPipeUtils.cpp:587
char tmpBuf[0xffff]
Definition CarlaPipeUtils.cpp:609
bool isReading
Definition CarlaPipeUtils.cpp:591
int pipeRecv
Definition CarlaPipeUtils.cpp:586
PrivateData() noexcept
Definition CarlaPipeUtils.cpp:612
bool clientClosingDown
Definition CarlaPipeUtils.cpp:594
bool lastMessageFailed
Definition CarlaPipeUtils.cpp:600
CarlaMutex writeLock
Definition CarlaPipeUtils.cpp:606
bool isServer
Definition CarlaPipeUtils.cpp:603
bool pipeClosed
Definition CarlaPipeUtils.cpp:597
Definition swell-types.h:254
char * LPSTR
Definition swell-types.h:189
unsigned int DWORD
Definition swell-types.h:164
void * HANDLE
Definition swell-types.h:212
DWORD WaitForSingleObject(HANDLE hand, DWORD msTO)
Definition swell.cpp:297
HANDLE CreateEvent(void *SA, BOOL manualReset, BOOL initialSig, const char *ignored)
Definition swell.cpp:476
BOOL CloseHandle(HANDLE hand)
Definition swell.cpp:157
return c
Definition crypt.c:175
ZCONST char * key
Definition crypt.c:587
#define void
Definition unzip.h:396
#define TRUE
Definition unzpriv.h:1295
#define FALSE
Definition unzpriv.h:1298
static bool waitForClientFirstMessage(const P &pipe, void *const ovRecv, void *const process, const uint32_t timeOutMilliseconds) noexcept
Definition CarlaPipeUtils.cpp:364
static void waitForChildToStopOrKillIt(pid_t &pid, const uint32_t timeOutMilliseconds) noexcept
Definition CarlaPipeUtils.cpp:551
static bool startProcess(const char *const argv[], pid_t &pidinst) noexcept
Definition CarlaPipeUtils.cpp:324
#define INVALID_PIPE_VALUE
Definition CarlaPipeUtils.cpp:56
static bool waitForChildToStop(const pid_t pid, const uint32_t timeOutMilliseconds, bool sendTerminate) noexcept
Definition CarlaPipeUtils.cpp:487
#define const
Definition zconf.h:137