2016-05-28 00:54:31 +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: */
|
2012-05-21 15:12:37 +04: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/. */
|
2011-06-03 22:33:56 +04:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2015-12-04 02:23:00 +03:00
|
|
|
#include "base/eintr_wrapper.h"
|
|
|
|
|
2011-06-03 22:33:56 +04:00
|
|
|
#include "mozilla/ipc/Transport.h"
|
2013-06-03 14:14:37 +04:00
|
|
|
#include "mozilla/ipc/FileDescriptor.h"
|
2016-04-01 09:33:52 +03:00
|
|
|
#include "ProtocolUtils.h"
|
2011-06-03 22:33:56 +04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2015-02-11 23:01:26 +03:00
|
|
|
using base::ProcessHandle;
|
|
|
|
|
2011-06-03 22:33:56 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2015-07-10 03:07:49 +03:00
|
|
|
nsresult
|
|
|
|
CreateTransport(base::ProcessId aProcIdOne,
|
|
|
|
TransportDescriptor* aOne,
|
|
|
|
TransportDescriptor* aTwo)
|
2011-06-03 22:33:56 +04:00
|
|
|
{
|
2015-04-23 13:09:04 +03:00
|
|
|
wstring id = IPC::Channel::GenerateVerifiedChannelID(std::wstring());
|
2011-06-03 22:33:56 +04:00
|
|
|
// Use MODE_SERVER to force creation of the socketpair
|
2012-07-30 18:20:58 +04:00
|
|
|
Transport t(id, Transport::MODE_SERVER, nullptr);
|
2013-05-31 17:16:54 +04:00
|
|
|
int fd1 = t.GetFileDescriptor();
|
2011-06-03 22:33:56 +04:00
|
|
|
int fd2, dontcare;
|
|
|
|
t.GetClientFileDescriptorMapping(&fd2, &dontcare);
|
|
|
|
if (fd1 < 0 || fd2 < 0) {
|
2015-07-10 03:07:49 +03:00
|
|
|
return NS_ERROR_TRANSPORT_INIT;
|
2011-06-03 22:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The Transport closes these fds when it goes out of scope, so we
|
|
|
|
// dup them here
|
|
|
|
fd1 = dup(fd1);
|
2016-08-23 08:52:04 +03:00
|
|
|
if (fd1 < 0) {
|
|
|
|
AnnotateCrashReportWithErrno("IpcCreateTransportDupErrno", errno);
|
|
|
|
}
|
2011-06-03 22:33:56 +04:00
|
|
|
fd2 = dup(fd2);
|
2016-08-23 08:52:04 +03:00
|
|
|
if (fd2 < 0) {
|
|
|
|
AnnotateCrashReportWithErrno("IpcCreateTransportDupErrno", errno);
|
|
|
|
}
|
|
|
|
|
2011-06-03 22:33:56 +04:00
|
|
|
if (fd1 < 0 || fd2 < 0) {
|
2018-04-10 23:36:00 +03:00
|
|
|
IGNORE_EINTR(close(fd1));
|
|
|
|
IGNORE_EINTR(close(fd2));
|
2015-07-10 03:07:49 +03:00
|
|
|
return NS_ERROR_DUPLICATE_HANDLE;
|
2011-06-03 22:33:56 +04:00
|
|
|
}
|
|
|
|
|
2013-06-03 14:14:37 +04:00
|
|
|
aOne->mFd = base::FileDescriptor(fd1, true/*close after sending*/);
|
|
|
|
aTwo->mFd = base::FileDescriptor(fd2, true/*close after sending*/);
|
2015-07-10 03:07:49 +03:00
|
|
|
return NS_OK;
|
2011-06-03 22:33:56 +04:00
|
|
|
}
|
|
|
|
|
2016-07-07 04:51:20 +03:00
|
|
|
UniquePtr<Transport>
|
2011-06-03 22:33:56 +04:00
|
|
|
OpenDescriptor(const TransportDescriptor& aTd, Transport::Mode aMode)
|
|
|
|
{
|
2016-07-07 04:51:20 +03:00
|
|
|
return MakeUnique<Transport>(aTd.mFd.fd, aMode, nullptr);
|
2011-06-03 22:33:56 +04:00
|
|
|
}
|
|
|
|
|
2016-07-07 04:51:20 +03:00
|
|
|
UniquePtr<Transport>
|
2013-06-03 14:14:37 +04:00
|
|
|
OpenDescriptor(const FileDescriptor& aFd, Transport::Mode aMode)
|
|
|
|
{
|
2016-05-27 11:12:51 +03:00
|
|
|
auto rawFD = aFd.ClonePlatformHandle();
|
|
|
|
return MakeUnique<Transport>(rawFD.release(), aMode, nullptr);
|
2013-06-03 14:14:37 +04:00
|
|
|
}
|
|
|
|
|
2015-11-01 06:02:08 +03:00
|
|
|
TransportDescriptor
|
|
|
|
DuplicateDescriptor(const TransportDescriptor& aTd)
|
|
|
|
{
|
|
|
|
TransportDescriptor result = aTd;
|
|
|
|
result.mFd.fd = dup(aTd.mFd.fd);
|
2016-04-01 09:33:52 +03:00
|
|
|
if (result.mFd.fd == -1) {
|
|
|
|
AnnotateSystemError();
|
|
|
|
}
|
2015-11-01 06:02:08 +03:00
|
|
|
MOZ_RELEASE_ASSERT(result.mFd.fd != -1, "DuplicateDescriptor failed");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-06-03 22:33:56 +04:00
|
|
|
void
|
|
|
|
CloseDescriptor(const TransportDescriptor& aTd)
|
|
|
|
{
|
|
|
|
close(aTd.mFd.fd);
|
|
|
|
}
|
|
|
|
|
2011-06-03 22:33:56 +04:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|