1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11 #include "base/eintr_wrapper.h"
13 #include "mozilla/ipc/Transport.h"
14 #include "mozilla/ipc/FileDescriptor.h"
15 #include "ProtocolUtils.h"
17 using base::ProcessHandle
;
22 nsresult
CreateTransport(base::ProcessId aProcIdOne
, TransportDescriptor
* aOne
,
23 TransportDescriptor
* aTwo
) {
24 std::wstring id
= IPC::Channel::GenerateVerifiedChannelID(std::wstring());
25 // Use MODE_SERVER to force creation of the socketpair
26 Transport
t(id
, Transport::MODE_SERVER
, nullptr);
27 int fd1
= t
.GetFileDescriptor();
29 t
.GetClientFileDescriptorMapping(&fd2
, &dontcare
);
30 if (fd1
< 0 || fd2
< 0) {
31 return NS_ERROR_TRANSPORT_INIT
;
34 // The Transport closes these fds when it goes out of scope, so we
38 AnnotateCrashReportWithErrno(
39 CrashReporter::Annotation::IpcCreateTransportDupErrno
, errno
);
43 AnnotateCrashReportWithErrno(
44 CrashReporter::Annotation::IpcCreateTransportDupErrno
, errno
);
47 if (fd1
< 0 || fd2
< 0) {
48 IGNORE_EINTR(close(fd1
));
49 IGNORE_EINTR(close(fd2
));
50 return NS_ERROR_DUPLICATE_HANDLE
;
53 aOne
->mFd
= base::FileDescriptor(fd1
, true /*close after sending*/);
54 aTwo
->mFd
= base::FileDescriptor(fd2
, true /*close after sending*/);
58 UniquePtr
<Transport
> OpenDescriptor(const TransportDescriptor
& aTd
,
59 Transport::Mode aMode
) {
60 return MakeUnique
<Transport
>(aTd
.mFd
.fd
, aMode
, nullptr);
63 UniquePtr
<Transport
> OpenDescriptor(const FileDescriptor
& aFd
,
64 Transport::Mode aMode
) {
65 auto rawFD
= aFd
.ClonePlatformHandle();
66 return MakeUnique
<Transport
>(rawFD
.release(), aMode
, nullptr);
69 TransportDescriptor
DuplicateDescriptor(const TransportDescriptor
& aTd
) {
70 TransportDescriptor result
= aTd
;
71 result
.mFd
.fd
= dup(aTd
.mFd
.fd
);
72 if (result
.mFd
.fd
== -1) {
73 AnnotateSystemError();
75 MOZ_RELEASE_ASSERT(result
.mFd
.fd
!= -1, "DuplicateDescriptor failed");
79 void CloseDescriptor(const TransportDescriptor
& aTd
) { close(aTd
.mFd
.fd
); }
82 } // namespace mozilla