2020-02-12 19:57:39 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2019-12-05 03:05:21 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "ForkServiceChild.h"
|
|
|
|
#include "ForkServer.h"
|
|
|
|
#include "mozilla/ipc/IPDLParamTraits.h"
|
|
|
|
#include "mozilla/Logging.h"
|
|
|
|
#include "mozilla/ipc/GeckoChildProcessHost.h"
|
2020-11-23 19:06:42 +03:00
|
|
|
#include "mozilla/ipc/ProtocolMessageUtils.h"
|
2020-02-12 19:57:39 +03:00
|
|
|
#include "mozilla/StaticPrefs_dom.h"
|
2019-12-05 03:05:21 +03:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
extern LazyLogModule gForkServiceLog;
|
|
|
|
|
|
|
|
mozilla::UniquePtr<ForkServiceChild> ForkServiceChild::sForkServiceChild;
|
|
|
|
|
|
|
|
void ForkServiceChild::StartForkServer() {
|
|
|
|
std::vector<std::string> extraArgs;
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2019-12-05 03:05:21 +03:00
|
|
|
GeckoChildProcessHost* subprocess =
|
|
|
|
new GeckoChildProcessHost(GeckoProcessType_ForkServer, false);
|
|
|
|
subprocess->LaunchAndWaitForProcessHandle(std::move(extraArgs));
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2019-12-05 03:05:21 +03:00
|
|
|
int fd = subprocess->GetChannel()->GetFileDescriptor();
|
|
|
|
fd = dup(fd); // Dup it because the channel will close it.
|
|
|
|
int fs_flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
fcntl(fd, F_SETFL, fs_flags & ~O_NONBLOCK);
|
|
|
|
int fd_flags = fcntl(fd, F_GETFD, 0);
|
|
|
|
fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC);
|
|
|
|
|
|
|
|
sForkServiceChild = mozilla::MakeUnique<ForkServiceChild>(fd, subprocess);
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2019-12-05 03:05:21 +03:00
|
|
|
// Without doing this, IO thread may intercept messages since the
|
|
|
|
// IPC::Channel created by it is still open.
|
|
|
|
subprocess->GetChannel()->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForkServiceChild::StopForkServer() { sForkServiceChild = nullptr; }
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2019-12-05 03:05:21 +03:00
|
|
|
ForkServiceChild::ForkServiceChild(int aFd, GeckoChildProcessHost* aProcess)
|
2020-12-01 16:43:52 +03:00
|
|
|
: mWaitForHello(true), mFailed(false), mProcess(aProcess) {
|
2019-12-05 03:05:21 +03:00
|
|
|
mTcver = MakeUnique<MiniTransceiver>(aFd);
|
|
|
|
}
|
|
|
|
|
|
|
|
ForkServiceChild::~ForkServiceChild() {
|
|
|
|
mProcess->Destroy();
|
|
|
|
close(mTcver->GetFD());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ForkServiceChild::SendForkNewSubprocess(
|
|
|
|
const nsTArray<nsCString>& aArgv, const nsTArray<EnvVar>& aEnvMap,
|
|
|
|
const nsTArray<FdMapping>& aFdsRemap, pid_t* aPid) {
|
|
|
|
if (mWaitForHello) {
|
|
|
|
// IPC::Channel created by the GeckoChildProcessHost has
|
|
|
|
// already send a HELLO. It is expected to receive a hello
|
|
|
|
// message from the fork server too.
|
|
|
|
IPC::Message hello;
|
|
|
|
mTcver->RecvInfallible(hello, "Fail to receive HELLO message");
|
|
|
|
MOZ_ASSERT(hello.type() == ForkServer::kHELLO_MESSAGE_TYPE);
|
|
|
|
mWaitForHello = false;
|
|
|
|
}
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2019-12-05 03:05:21 +03:00
|
|
|
mRecvPid = -1;
|
|
|
|
IPC::Message msg(MSG_ROUTING_CONTROL, Msg_ForkNewSubprocess__ID);
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2019-12-05 03:05:21 +03:00
|
|
|
WriteIPDLParam(&msg, nullptr, aArgv);
|
|
|
|
WriteIPDLParam(&msg, nullptr, aEnvMap);
|
|
|
|
WriteIPDLParam(&msg, nullptr, aFdsRemap);
|
|
|
|
if (!mTcver->Send(msg)) {
|
|
|
|
MOZ_LOG(gForkServiceLog, LogLevel::Verbose,
|
|
|
|
("the pipe to the fork server is closed or having errors"));
|
2020-12-01 16:43:52 +03:00
|
|
|
OnError();
|
2019-12-05 03:05:21 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2019-12-05 03:05:21 +03:00
|
|
|
IPC::Message reply;
|
|
|
|
if (!mTcver->Recv(reply)) {
|
|
|
|
MOZ_LOG(gForkServiceLog, LogLevel::Verbose,
|
|
|
|
("the pipe to the fork server is closed or having errors"));
|
2020-12-01 16:43:52 +03:00
|
|
|
OnError();
|
2019-12-05 03:05:21 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
OnMessageReceived(std::move(reply));
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2019-12-05 03:05:21 +03:00
|
|
|
MOZ_ASSERT(mRecvPid != -1);
|
|
|
|
*aPid = mRecvPid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForkServiceChild::OnMessageReceived(IPC::Message&& message) {
|
|
|
|
if (message.type() != Reply_ForkNewSubprocess__ID) {
|
|
|
|
MOZ_LOG(gForkServiceLog, LogLevel::Verbose,
|
|
|
|
("unknown reply type %d", message.type()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PickleIterator iter__(message);
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2019-12-05 03:05:21 +03:00
|
|
|
if (!ReadIPDLParam(&message, &iter__, nullptr, &mRecvPid)) {
|
|
|
|
MOZ_CRASH("Error deserializing 'pid_t'");
|
|
|
|
}
|
|
|
|
message.EndRead(iter__, message.type());
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:43:52 +03:00
|
|
|
void ForkServiceChild::OnError() {
|
|
|
|
mFailed = true;
|
|
|
|
ForkServerLauncher::RestartForkServer();
|
|
|
|
}
|
|
|
|
|
2019-12-05 03:02:40 +03:00
|
|
|
NS_IMPL_ISUPPORTS(ForkServerLauncher, nsIObserver)
|
|
|
|
|
|
|
|
bool ForkServerLauncher::mHaveStartedClient = false;
|
|
|
|
StaticRefPtr<ForkServerLauncher> ForkServerLauncher::mSingleton;
|
|
|
|
|
|
|
|
ForkServerLauncher::ForkServerLauncher() {}
|
|
|
|
|
|
|
|
ForkServerLauncher::~ForkServerLauncher() {}
|
|
|
|
|
|
|
|
already_AddRefed<ForkServerLauncher> ForkServerLauncher::Create() {
|
|
|
|
if (mSingleton == nullptr) {
|
|
|
|
mSingleton = new ForkServerLauncher();
|
|
|
|
}
|
|
|
|
RefPtr<ForkServerLauncher> launcher = mSingleton;
|
|
|
|
return launcher.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ForkServerLauncher::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
2020-03-18 04:01:17 +03:00
|
|
|
if (strcmp(aTopic, NS_XPCOM_STARTUP_CATEGORY) == 0) {
|
|
|
|
nsCOMPtr<nsIObserverService> obsSvc =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
MOZ_ASSERT(obsSvc != nullptr);
|
|
|
|
// preferences are not available until final-ui-startup
|
|
|
|
obsSvc->AddObserver(this, "final-ui-startup", false);
|
|
|
|
} else if (!mHaveStartedClient && strcmp(aTopic, "final-ui-startup") == 0) {
|
2020-02-14 19:50:00 +03:00
|
|
|
if (StaticPrefs::dom_ipc_forkserver_enable_AtStartup()) {
|
|
|
|
mHaveStartedClient = true;
|
|
|
|
ForkServiceChild::StartForkServer();
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2020-02-14 19:50:00 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obsSvc =
|
2019-12-05 03:02:40 +03:00
|
|
|
mozilla::services::GetObserverService();
|
2020-02-14 19:50:00 +03:00
|
|
|
MOZ_ASSERT(obsSvc != nullptr);
|
|
|
|
obsSvc->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
|
|
|
} else {
|
|
|
|
mSingleton = nullptr;
|
|
|
|
}
|
2019-12-05 03:02:40 +03:00
|
|
|
}
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2020-02-14 19:50:00 +03:00
|
|
|
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
|
|
|
|
if (mHaveStartedClient) {
|
|
|
|
mHaveStartedClient = false;
|
|
|
|
ForkServiceChild::StopForkServer();
|
|
|
|
}
|
2020-01-10 00:50:11 +03:00
|
|
|
|
2019-12-19 18:48:31 +03:00
|
|
|
// To make leak checker happy!
|
|
|
|
mSingleton = nullptr;
|
2019-12-05 03:02:40 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:43:52 +03:00
|
|
|
void ForkServerLauncher::RestartForkServer() {
|
|
|
|
// Restart fork server
|
|
|
|
NS_SUCCEEDED(NS_DispatchToMainThreadQueue(
|
|
|
|
NS_NewRunnableFunction("OnForkServerError",
|
|
|
|
[] {
|
|
|
|
if (mSingleton) {
|
|
|
|
ForkServiceChild::StopForkServer();
|
|
|
|
ForkServiceChild::StartForkServer();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
EventQueuePriority::Idle));
|
|
|
|
}
|
|
|
|
|
2019-12-05 03:05:21 +03:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|