1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2015 - ROLI Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE 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.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#include "../jucer_Headers.h"
#include "../Utility/jucer_PresetIDs.h"
#include "../Utility/jucer_FileHelpers.h"
#include "../Application/jucer_AppearanceSettings.h"
#include "../Application/jucer_Application.h"
#include "../Utility/jucer_CodeHelpers.h"
#include "projucer_CompileEngineDLL.h"
#include "projucer_MessageIDs.h"
#include "projucer_CppHelpers.h"
#include "projucer_SourceCodeRange.h"
#include "projucer_ClassDatabase.h"
#include "projucer_DiagnosticMessage.h"
#include "projucer_ProjectBuildInfo.h"
#include "projucer_ClientServerMessages.h"
#if JUCE_LINUX
#include <sys/types.h>
#include <unistd.h>
#endif
#ifndef RUN_CLANG_IN_CHILD_PROCESS
#error
#endif
#if RUN_CLANG_IN_CHILD_PROCESS
static bool parentProcessHasExited();
#endif
#if JUCE_WINDOWS
static void setParentProcessID (int);
static int getCurrentProcessID();
#endif
//==============================================================================
/** Detects whether this process has hung, and kills it if so. */
struct ZombiePatrol : private Thread,
private AsyncUpdater,
private Timer
{
ZombiePatrol (MessageHandler& mh)
: Thread ("Ping"), owner (mh)
{
startThread (2);
startTimer (1000);
}
~ZombiePatrol()
{
stopThread (1000);
}
private:
MessageHandler& owner;
int failedPings = 0;
void run() override
{
while (! threadShouldExit())
{
#if RUN_CLANG_IN_CHILD_PROCESS
if (parentProcessHasExited())
{
killProcess();
break;
}
#endif
wait (1000);
}
}
void handleAsyncUpdate() override
{
DBG ("Server: quitting");
stopTimer();
ProjucerApplication::getApp().systemRequestedQuit();
}
void timerCallback() override
{
if (! MessageTypes::sendPing (owner))
{
if (++failedPings == 10)
{
killProcess();
return;
}
}
else
{
failedPings = 0;
}
}
void killProcess()
{
triggerAsyncUpdate(); // give the messagequeue a chance to do things cleanly.
static UnstoppableKillerThread* k = new UnstoppableKillerThread(); // (allowed to leak, but static so only one is created)
ignoreUnused (k);
}
struct UnstoppableKillerThread : public Thread
{
UnstoppableKillerThread() : Thread ("Killer") { startThread(); }
void run() override
{
wait (15000);
if (! threadShouldExit())
Process::terminate();
}
};
};
//==============================================================================
class ServerIPC : public InterprocessConnection,
public MessageHandler
{
public:
ServerIPC (const StringArray& info)
: InterprocessConnection (true), liveCodeBuilder (nullptr)
{
if (! createPipe (info[0], -1))
{
Logger::writeToLog ("*** Couldn't create pipe!");
ProjucerApplication::getApp().systemRequestedQuit();
return;
}
if (dll.isLoaded())
liveCodeBuilder = dll.projucer_createBuilder (sendMessageCallback, this, info[1].toRawUTF8(), info[2].toRawUTF8());
#if JUCE_WINDOWS
setParentProcessID (info[3].getHexValue32());
#endif
zombieKiller = new ZombiePatrol (*this);
}
~ServerIPC()
{
zombieKiller = nullptr;
if (dll.isLoaded())
dll.projucer_deleteBuilder (liveCodeBuilder);
dll.shutdown();
DBG ("Server: finished closing down");
}
void connectionMade() override
{
DBG ("Server: client connected");
}
void connectionLost() override
{
Logger::writeToLog ("Server: client lost");
JUCEApplication::quit();
}
void sendQuitMessageToIDE()
{
MessageTypes::sendShouldCloseIDE (*this);
}
bool sendMessage (const ValueTree& m) override
{
return InterprocessConnection::sendMessage (MessageHandler::convertMessage (m));
}
void messageReceived (const MemoryBlock& message) override
{
jassert (dll.isLoaded());
dll.projucer_sendMessage (liveCodeBuilder, message.getData(), message.getSize());
}
static bool sendMessageCallback (void* userInfo, const void* data, size_t dataSize)
{
return static_cast<InterprocessConnection*> (static_cast<ServerIPC*> (userInfo))
->sendMessage (MemoryBlock (data, dataSize));
}
CompileEngineDLL dll;
LiveCodeBuilder liveCodeBuilder;
ScopedPointer<ZombiePatrol> zombieKiller;
};
//==============================================================================
const char* commandPrefix = "--server:";
const char* commandTokenSeparator = "\x01";
String createCommandLineForLaunchingServer (const String& pipeName, const String& projectUID, const File& cacheLocation)
{
StringArray info;
info.add (pipeName);
info.add (projectUID);
info.add (cacheLocation.getFullPathName());
#if JUCE_WINDOWS
info.add (String::toHexString (getCurrentProcessID()));
#endif
const File exe (File::getSpecialLocation (File::currentExecutableFile).getFullPathName());
return "\"" + exe.getFullPathName() + "\" " + commandPrefix + info.joinIntoString (commandTokenSeparator);
}
static ServerIPC* currentServer = nullptr;
static void crashCallback (const char* message)
{
if (currentServer != nullptr)
{
#if RUN_CLANG_IN_CHILD_PROCESS
MessageTypes::sendCrash (*currentServer, message);
Logger::writeToLog ("*** Crashed! " + String (message));
#else
ignoreUnused (message);
jassertfalse;
#endif
currentServer->disconnect();
}
}
static void quitCallback()
{
ProjucerApplication::getApp().systemRequestedQuit();
}
void* createClangServer (const String& commandLine)
{
StringArray info;
info.addTokens (commandLine.fromFirstOccurrenceOf (commandPrefix, false, false), commandTokenSeparator, "");
ScopedPointer<ServerIPC> ipc = new ServerIPC (info);
if (ipc->dll.isLoaded())
{
ipc->dll.initialise (crashCallback, quitCallback, (bool) RUN_CLANG_IN_CHILD_PROCESS);
currentServer = ipc.release();
return currentServer;
}
return nullptr;
}
void destroyClangServer (void* server)
{
currentServer = nullptr;
delete static_cast<ServerIPC*> (server);
}
void sendQuitMessageToIDE (void* server)
{
static_cast<ServerIPC*> (server)->sendQuitMessageToIDE();
}
//==============================================================================
#if JUCE_WINDOWS
#define STRICT 1
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
static HANDLE parentProcessHandle = 0;
static void setParentProcessID (int pid) { parentProcessHandle = OpenProcess (SYNCHRONIZE, FALSE, (DWORD) pid); }
static int getCurrentProcessID() { return (int) GetCurrentProcessId(); }
#endif
#if RUN_CLANG_IN_CHILD_PROCESS
bool parentProcessHasExited()
{
#if JUCE_WINDOWS
return WaitForSingleObject (parentProcessHandle, 0) == WAIT_OBJECT_0;
#else
return getppid() == 1;
#endif
}
#endif
|