|
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/. */ |
|
7 |
|
8 #include <unistd.h> |
|
9 |
|
10 #include <string> |
|
11 |
|
12 #include "chrome/common/child_process_info.h" |
|
13 |
|
14 #include "mozilla/ipc/Transport.h" |
|
15 #include "mozilla/ipc/FileDescriptor.h" |
|
16 |
|
17 using namespace base; |
|
18 using namespace std; |
|
19 |
|
20 namespace mozilla { |
|
21 namespace ipc { |
|
22 |
|
23 bool |
|
24 CreateTransport(ProcessHandle /*unused*/, ProcessHandle /*unused*/, |
|
25 TransportDescriptor* aOne, TransportDescriptor* aTwo) |
|
26 { |
|
27 // Gecko doesn't care about this random ID, and the argument to this |
|
28 // function isn't really necessary, it can be just any random |
|
29 // pointer value |
|
30 wstring id = ChildProcessInfo::GenerateRandomChannelID(aOne); |
|
31 // Use MODE_SERVER to force creation of the socketpair |
|
32 Transport t(id, Transport::MODE_SERVER, nullptr); |
|
33 int fd1 = t.GetFileDescriptor(); |
|
34 int fd2, dontcare; |
|
35 t.GetClientFileDescriptorMapping(&fd2, &dontcare); |
|
36 if (fd1 < 0 || fd2 < 0) { |
|
37 return false; |
|
38 } |
|
39 |
|
40 // The Transport closes these fds when it goes out of scope, so we |
|
41 // dup them here |
|
42 fd1 = dup(fd1); |
|
43 fd2 = dup(fd2); |
|
44 if (fd1 < 0 || fd2 < 0) { |
|
45 return false; |
|
46 } |
|
47 |
|
48 aOne->mFd = base::FileDescriptor(fd1, true/*close after sending*/); |
|
49 aTwo->mFd = base::FileDescriptor(fd2, true/*close after sending*/); |
|
50 return true; |
|
51 } |
|
52 |
|
53 Transport* |
|
54 OpenDescriptor(const TransportDescriptor& aTd, Transport::Mode aMode) |
|
55 { |
|
56 return new Transport(aTd.mFd.fd, aMode, nullptr); |
|
57 } |
|
58 |
|
59 Transport* |
|
60 OpenDescriptor(const FileDescriptor& aFd, Transport::Mode aMode) |
|
61 { |
|
62 return new Transport(aFd.PlatformHandle(), aMode, nullptr); |
|
63 } |
|
64 |
|
65 void |
|
66 CloseDescriptor(const TransportDescriptor& aTd) |
|
67 { |
|
68 close(aTd.mFd.fd); |
|
69 } |
|
70 |
|
71 } // namespace ipc |
|
72 } // namespace mozilla |