Line data Source code
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/. */
6 :
7 : #include <unistd.h>
8 :
9 : #include <string>
10 :
11 : #include "base/eintr_wrapper.h"
12 :
13 : #include "mozilla/ipc/Transport.h"
14 : #include "mozilla/ipc/FileDescriptor.h"
15 : #include "ProtocolUtils.h"
16 :
17 : using namespace std;
18 :
19 : using base::ProcessHandle;
20 :
21 : namespace mozilla {
22 : namespace ipc {
23 :
24 : nsresult
25 13 : CreateTransport(base::ProcessId aProcIdOne,
26 : TransportDescriptor* aOne,
27 : TransportDescriptor* aTwo)
28 : {
29 26 : wstring id = IPC::Channel::GenerateVerifiedChannelID(std::wstring());
30 : // Use MODE_SERVER to force creation of the socketpair
31 26 : Transport t(id, Transport::MODE_SERVER, nullptr);
32 13 : int fd1 = t.GetFileDescriptor();
33 : int fd2, dontcare;
34 13 : t.GetClientFileDescriptorMapping(&fd2, &dontcare);
35 13 : if (fd1 < 0 || fd2 < 0) {
36 0 : return NS_ERROR_TRANSPORT_INIT;
37 : }
38 :
39 : // The Transport closes these fds when it goes out of scope, so we
40 : // dup them here
41 13 : fd1 = dup(fd1);
42 13 : if (fd1 < 0) {
43 0 : AnnotateCrashReportWithErrno("IpcCreateTransportDupErrno", errno);
44 : }
45 13 : fd2 = dup(fd2);
46 13 : if (fd2 < 0) {
47 0 : AnnotateCrashReportWithErrno("IpcCreateTransportDupErrno", errno);
48 : }
49 :
50 13 : if (fd1 < 0 || fd2 < 0) {
51 0 : HANDLE_EINTR(close(fd1));
52 0 : HANDLE_EINTR(close(fd2));
53 0 : return NS_ERROR_DUPLICATE_HANDLE;
54 : }
55 :
56 13 : aOne->mFd = base::FileDescriptor(fd1, true/*close after sending*/);
57 13 : aTwo->mFd = base::FileDescriptor(fd2, true/*close after sending*/);
58 13 : return NS_OK;
59 : }
60 :
61 : UniquePtr<Transport>
62 24 : OpenDescriptor(const TransportDescriptor& aTd, Transport::Mode aMode)
63 : {
64 24 : return MakeUnique<Transport>(aTd.mFd.fd, aMode, nullptr);
65 : }
66 :
67 : UniquePtr<Transport>
68 0 : OpenDescriptor(const FileDescriptor& aFd, Transport::Mode aMode)
69 : {
70 0 : auto rawFD = aFd.ClonePlatformHandle();
71 0 : return MakeUnique<Transport>(rawFD.release(), aMode, nullptr);
72 : }
73 :
74 : TransportDescriptor
75 13 : DuplicateDescriptor(const TransportDescriptor& aTd)
76 : {
77 13 : TransportDescriptor result = aTd;
78 13 : result.mFd.fd = dup(aTd.mFd.fd);
79 13 : if (result.mFd.fd == -1) {
80 0 : AnnotateSystemError();
81 : }
82 13 : MOZ_RELEASE_ASSERT(result.mFd.fd != -1, "DuplicateDescriptor failed");
83 13 : return result;
84 : }
85 :
86 : void
87 13 : CloseDescriptor(const TransportDescriptor& aTd)
88 : {
89 13 : close(aTd.mFd.fd);
90 13 : }
91 :
92 : } // namespace ipc
93 : } // namespace mozilla
|