Bug 1642744 [wpt PR 23920] - [ScrollTimeline] Update compositor timeline from blink...
[gecko.git] / ipc / glue / Transport_posix.cpp
blobd484628ec3179674f8a64112c1832ab014b3f520
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/. */
7 #include <unistd.h>
9 #include <string>
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;
19 namespace mozilla {
20 namespace ipc {
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();
28 int fd2, dontcare;
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
35 // dup them here
36 fd1 = dup(fd1);
37 if (fd1 < 0) {
38 AnnotateCrashReportWithErrno(
39 CrashReporter::Annotation::IpcCreateTransportDupErrno, errno);
41 fd2 = dup(fd2);
42 if (fd2 < 0) {
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*/);
55 return NS_OK;
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");
76 return result;
79 void CloseDescriptor(const TransportDescriptor& aTd) { close(aTd.mFd.fd); }
81 } // namespace ipc
82 } // namespace mozilla