Fix typo in 9b54bd30006c008b4a951331b273613d5bac3abf
[pm.git] / ipc / glue / Transport_posix.cpp
blob9f32ed952f97a8da5cc0e7998fc0848bea5ad0de
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include <unistd.h>
10 #include <string>
12 #include "chrome/common/child_process_info.h"
14 #include "mozilla/ipc/Transport.h"
15 #include "mozilla/ipc/FileDescriptor.h"
17 using namespace std;
19 using base::ProcessHandle;
21 namespace mozilla {
22 namespace ipc {
24 bool
25 CreateTransport(ProcessHandle /*unused*/, ProcessHandle /*unused*/,
26 TransportDescriptor* aOne, TransportDescriptor* aTwo)
28 wstring id = IPC::Channel::GenerateVerifiedChannelID(std::wstring());
29 // Use MODE_SERVER to force creation of the socketpair
30 Transport t(id, Transport::MODE_SERVER, nullptr);
31 int fd1 = t.GetFileDescriptor();
32 int fd2, dontcare;
33 t.GetClientFileDescriptorMapping(&fd2, &dontcare);
34 if (fd1 < 0 || fd2 < 0) {
35 return false;
38 // The Transport closes these fds when it goes out of scope, so we
39 // dup them here
40 fd1 = dup(fd1);
41 fd2 = dup(fd2);
42 if (fd1 < 0 || fd2 < 0) {
43 return false;
46 aOne->mFd = base::FileDescriptor(fd1, true/*close after sending*/);
47 aTwo->mFd = base::FileDescriptor(fd2, true/*close after sending*/);
48 return true;
51 Transport*
52 OpenDescriptor(const TransportDescriptor& aTd, Transport::Mode aMode)
54 return new Transport(aTd.mFd.fd, aMode, nullptr);
57 Transport*
58 OpenDescriptor(const FileDescriptor& aFd, Transport::Mode aMode)
60 return new Transport(aFd.PlatformHandle(), aMode, nullptr);
63 void
64 CloseDescriptor(const TransportDescriptor& aTd)
66 close(aTd.mFd.fd);
69 } // namespace ipc
70 } // namespace mozilla