|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- /*
- * Carla Pipe utils based on lv2fil UI code
- * Copyright (C) 2009 Nedko Arnaudov <nedko@arnaudov.name>
- * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * For a full copy of the GNU General Public License see the doc/GPL.txt file.
- */
-
- #ifndef CARLA_PIPE_UTILS_HPP_INCLUDED
- #define CARLA_PIPE_UTILS_HPP_INCLUDED
-
- #include "CarlaString.hpp"
-
- #include <cerrno>
- #include <clocale>
-
- #include <fcntl.h>
- #include <signal.h>
- #include <sys/wait.h>
-
- #define WAIT_START_TIMEOUT 3000 /* ms */
- #define WAIT_ZOMBIE_TIMEOUT 3000 /* ms */
- #define WAIT_STEP 100 /* ms */
-
- // -----------------------------------------------------------------------
-
- class CarlaPipeServer
- {
- protected:
- CarlaPipeServer()
- : fPipeRecv(-1),
- fPipeSend(-1),
- fIsReading(false),
- fPid(-1)
- {
- carla_debug("CarlaPipeServer::CarlaPipeServer()");
-
- fTmpBuf[0xff] = '\0';
- }
-
- // -------------------------------------------------------------------
-
- public:
- virtual ~CarlaPipeServer()
- {
- carla_debug("CarlaPipeServer::~CarlaPipeServer()");
-
- stop();
- }
-
- bool isOk() const noexcept
- {
- return (fPipeRecv != -1 && fPipeSend != -1 && fPid != -1);
- }
-
- bool start(const char* const filename, const char* const arg1, const char* const arg2)
- {
- CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
- CARLA_SAFE_ASSERT_RETURN(arg1 != nullptr, false);
- CARLA_SAFE_ASSERT_RETURN(arg2 != nullptr, false);
- carla_debug("CarlaPipeServer::start(\"%s\", \"%s\", \"%s\")", filename, arg1, arg2);
-
- //----------------------------------------------------------------
-
- const char* argv[5];
-
- //----------------------------------------------------------------
- // argv[0] => filename
-
- argv[0] = filename;
-
- //----------------------------------------------------------------
- // argv[1-2] => args
-
- argv[1] = arg1;
- argv[2] = arg2;
-
- //----------------------------------------------------------------
- // argv[3-4] => pipes
-
- int pipe1[2]; // written by host process, read by plugin UI process
- int pipe2[2]; // written by plugin UI process, read by host process
-
- if (::pipe(pipe1) != 0)
- {
- fail("pipe1 creation failed");
- return false;
- }
-
- if (::pipe(pipe2) != 0)
- {
- ::close(pipe1[0]);
- ::close(pipe1[1]);
- fail("pipe2 creation failed");
- return false;
- }
-
- char pipeRecv[100+1];
- char pipeSend[100+1];
-
- std::snprintf(pipeRecv, 100, "%d", pipe1[0]); // [0] means reading end
- std::snprintf(pipeSend, 100, "%d", pipe2[1]); // [1] means writting end
-
- pipeRecv[100] = '\0';
- pipeSend[100] = '\0';
-
- argv[3] = pipeRecv; // reading end
- argv[4] = pipeSend; // writting end
-
- //----------------------------------------------------------------
- // fork
-
- int ret = -1;
-
- if ((! fork_exec(argv, &ret)) || ret == -1)
- {
- ::close(pipe1[0]);
- ::close(pipe1[1]);
- ::close(pipe2[0]);
- ::close(pipe2[1]);
- fail("fork_exec() failed");
- return false;
- }
-
- fPid = ret;
-
- // fork duplicated the handles, close pipe ends that are used by the child process
- ::close(pipe1[0]);
- ::close(pipe2[1]);
-
- fPipeSend = pipe1[1]; // [1] means writting end
- fPipeRecv = pipe2[0]; // [0] means reading end
-
- // set non-block
- try {
- ret = fcntl(fPipeRecv, F_SETFL, fcntl(fPipeRecv, F_GETFL) | O_NONBLOCK);
- } catch (...) {
- ret = -1;
- fail("failed to set pipe as non-block");
- }
-
- //----------------------------------------------------------------
- // wait a while for child process to confirm it is alive
-
- if (ret != -1)
- {
- char ch;
- ssize_t ret2;
-
- for (int i=0; ;)
- {
- try {
- ret2 = ::read(fPipeRecv, &ch, 1);
- }
- catch (...) {
- fail("failed to read from pipe");
- break;
- }
-
- switch (ret2)
- {
- case -1:
- if (errno == EAGAIN)
- {
- if (i < WAIT_START_TIMEOUT / WAIT_STEP)
- {
- carla_msleep(WAIT_STEP);
- ++i;
- continue;
- }
- carla_stderr("we have waited for child with pid %d to appear for %.1f seconds and we are giving up", (int)fPid, (float)WAIT_START_TIMEOUT / 1000.0f);
- }
- else
- carla_stderr("read() failed: %s", std::strerror(errno));
- break;
-
- case 1:
- if (ch == '\n') {
- // success
- return true;
- }
- carla_stderr("read() has wrong first char '%c'", ch);
- break;
-
- default:
- carla_stderr("read() returned %i", int(ret2));
- break;
- }
-
- break;
- }
-
- carla_stderr("force killing misbehaved child %i (start)", int(fPid));
- }
-
- if (kill(fPid, SIGKILL) == -1)
- {
- carla_stderr("kill() failed: %s (start)\n", strerror(errno));
- }
-
- // wait a while child to exit, we dont like zombie processes
- wait_child(fPid);
-
- // close pipes
- try {
- ::close(fPipeRecv);
- } catch (...) {}
-
- try {
- ::close(fPipeSend);
- } catch (...) {}
-
- fPipeRecv = -1;
- fPipeSend = -1;
- fPid = -1;
-
- return false;
- }
-
- void stop() noexcept
- {
- carla_debug("CarlaPipeServer::stop()");
-
- if (fPipeSend == -1 || fPipeRecv == -1 || fPid == -1)
- return;
-
- try {
- ssize_t ignore = ::write(fPipeSend, "quit\n", 5);
- (void)ignore;
- } catch (...) {}
-
- waitChildClose();
-
- try {
- ::close(fPipeRecv);
- } catch (...) {}
-
- try {
- ::close(fPipeSend);
- } catch (...) {}
-
- fPipeRecv = -1;
- fPipeSend = -1;
- fPid = -1;
- }
-
- void idle()
- {
- const char* locale = nullptr;
-
- for (;;)
- {
- const char* const msg(readline());
-
- if (msg == nullptr)
- break;
-
- if (locale == nullptr)
- {
- locale = carla_strdup(setlocale(LC_NUMERIC, nullptr));
- setlocale(LC_NUMERIC, "POSIX");
- }
-
- fIsReading = true;
- msgReceived(msg);
- fIsReading = false;
-
- delete[] msg;
- }
-
- if (locale != nullptr)
- {
- setlocale(LC_NUMERIC, locale);
- delete[] locale;
- }
- }
-
- // -------------------------------------------------------------------
-
- bool readNextLineAsBool(bool& value)
- {
- CARLA_SAFE_ASSERT_RETURN(fIsReading, false);
-
- if (const char* const msg = readline())
- {
- value = (std::strcmp(msg, "true") == 0);
- delete[] msg;
- return true;
- }
-
- return false;
- }
-
- bool readNextLineAsInt(int& value)
- {
- CARLA_SAFE_ASSERT_RETURN(fIsReading, false);
-
- if (const char* const msg = readline())
- {
- value = std::atoi(msg);
- delete[] msg;
- return true;
- }
-
- return false;
- }
-
- bool readNextLineAsLong(long& value)
- {
- CARLA_SAFE_ASSERT_RETURN(fIsReading, false);
-
- if (const char* const msg = readline())
- {
- value = std::atol(msg);
- delete[] msg;
- return true;
- }
-
- return false;
- }
-
- bool readNextLineAsFloat(float& value)
- {
- CARLA_SAFE_ASSERT_RETURN(fIsReading, false);
-
- if (const char* const msg = readline())
- {
- const bool ret(std::sscanf(msg, "%f", &value) == 1);
- delete[] msg;
- return ret;
- }
-
- return false;
- }
-
- bool readNextLineAsString(const char*& value)
- {
- CARLA_SAFE_ASSERT_RETURN(fIsReading, false);
-
- if (const char* const msg = readline())
- {
- value = msg;
- return true;
- }
-
- return false;
- }
-
- // -------------------------------------------------------------------
-
- void writeMsg(const char* const msg) const noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(fPipeSend != -1,);
-
- try {
- ssize_t ignore = ::write(fPipeSend, msg, std::strlen(msg));
- (void)ignore;
- } catch (...) {}
- }
-
- void writeMsg(const char* const msg, size_t size) const noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(fPipeSend != -1,);
-
- try {
- ssize_t ignore = ::write(fPipeSend, msg, size);
- (void)ignore;
- } catch (...) {}
- }
-
- void writeAndFixMsg(const char* const msg)
- {
- CARLA_SAFE_ASSERT_RETURN(fPipeSend != -1,);
-
- const size_t size(msg != nullptr ? std::strlen(msg) : 0);
-
- char fixedMsg[size+2];
-
- if (size > 0)
- {
- std::strcpy(fixedMsg, msg);
-
- for (size_t i=0; i < size; ++i)
- {
- if (fixedMsg[i] == '\n')
- fixedMsg[i] = '\r';
- }
-
- if (fixedMsg[size+1] == '\r')
- {
- fixedMsg[size-1] = '\n';
- fixedMsg[size] = '\0';
- fixedMsg[size+1] = '\0';
- }
- else
- {
- fixedMsg[size] = '\n';
- fixedMsg[size+1] = '\0';
- }
- }
- else
- {
- fixedMsg[0] = '\n';
- fixedMsg[1] = '\0';
- }
-
- try {
- ssize_t ignore = ::write(fPipeSend, fixedMsg, size+1);
- (void)ignore;
- } catch (...) {}
- }
-
- void waitChildClose()
- {
- if (! wait_child(fPid))
- {
- carla_stderr2("force killing misbehaved child %i (exit)", int(fPid));
-
- if (kill(fPid, SIGKILL) == -1)
- carla_stderr2("kill() failed: %s (exit)", strerror(errno));
- else
- wait_child(fPid);
- }
- }
-
- // -------------------------------------------------------------------
-
- protected:
- virtual void fail(const char* const error)
- {
- carla_stderr2(error);
- }
-
- virtual void msgReceived(const char* const msg) = 0;
-
- // -------------------------------------------------------------------
-
- private:
- int fPipeRecv; // the pipe end that is used for receiving messages from UI
- int fPipeSend; // the pipe end that is used for sending messages to UI
- bool fIsReading;
- pid_t fPid;
-
- char fTmpBuf[0xff+1];
- CarlaString fTmpStr;
-
- // -------------------------------------------------------------------
-
- const char* readline()
- {
- char ch;
- char* ptr = fTmpBuf;
- ssize_t ret;
-
- fTmpStr.clear();
-
- for (int i=0; i < 0xff; ++i)
- {
- try {
- ret = read(fPipeRecv, &ch, 1);
- }
- catch (...) {
- break;
- }
-
- if (ret == 1 && ch != '\n')
- {
- if (ch == '\r')
- ch = '\n';
-
- *ptr++ = ch;
-
- if (i+1 == 0xff)
- {
- i = 0;
- ptr = fTmpBuf;
- fTmpStr += fTmpBuf;
- }
-
- continue;
- }
-
- if (fTmpStr.isNotEmpty() || ptr != fTmpBuf)
- {
- if (ptr != fTmpBuf)
- {
- *ptr = '\0';
- fTmpStr += fTmpBuf;
- }
-
- return fTmpStr.dup();
- }
-
- break;
- }
-
- return nullptr;
- }
-
- static bool fork_exec(const char* const argv[5], int* const retp)
- {
- const pid_t ret = *retp = vfork();
-
- switch (ret)
- {
- case 0: /* child process */
- execlp(argv[0], argv[0], argv[1], argv[2], argv[3], argv[4], nullptr);
- carla_stderr2("exec failed: %s", std::strerror(errno));
- _exit(0);
- return false;
- case -1: /* error */
- carla_stderr2("fork() failed: %s", std::strerror(errno));
- _exit(0);
- return false;
- }
-
- return true;
- }
-
- static bool wait_child(const pid_t pid)
- {
- if (pid == -1)
- {
- carla_stderr2("Can't wait for pid -1");
- return false;
- }
-
- pid_t ret;
-
- for (int i=0, maxTime=WAIT_ZOMBIE_TIMEOUT/WAIT_STEP; i < maxTime; ++i)
- {
- try {
- ret = ::waitpid(pid, nullptr, WNOHANG);
- }
- catch (...) {
- break;
- }
-
- if (ret != 0)
- {
- if (ret == pid)
- return true;
-
- if (ret == -1)
- {
- carla_stderr2("waitpid(%i) failed: %s", int(pid), std::strerror(errno));
- return false;
- }
-
- carla_stderr2("we waited for child pid %i to exit but we got pid %i instead", int(pid), int(ret));
- return false;
- }
-
- carla_msleep(WAIT_STEP); /* wait 100 ms */
- }
-
- carla_stderr2("we waited for child with pid %i to exit for %.1f seconds and we are giving up", int(pid), float(WAIT_START_TIMEOUT)/1000.0f);
- return false;
- }
- };
-
- // -----------------------------------------------------------------------
-
- class CarlaPipeClient
- {
- protected:
- CarlaPipeClient()
- {
- carla_debug("CarlaPipeClient::CarlaPipeClient()");
- }
-
- // -------------------------------------------------------------------
-
- public:
- virtual ~CarlaPipeClient()
- {
- carla_debug("CarlaPipeClient::~CarlaPipeClient()");
-
- stop();
- }
-
- void stop()
- {
- }
- };
-
- // -----------------------------------------------------------------------
-
- #endif // CARLA_PIPE_UTILS_HPP_INCLUDED
|