OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/edk/embedder/platform_channel_pair.h" | 5 #include "mojo/edk/embedder/platform_channel_pair.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/posix/global_descriptors.h" | |
14 #include "build/build_config.h" | 13 #include "build/build_config.h" |
15 #include "mojo/edk/platform/platform_handle.h" | 14 #include "mojo/edk/platform/platform_handle.h" |
16 #include "mojo/edk/util/string_number_conversions.h" | |
17 | 15 |
18 using mojo::platform::PlatformHandle; | 16 using mojo::platform::PlatformHandle; |
19 using mojo::platform::ScopedPlatformHandle; | |
20 using mojo::util::NumberToString; | |
21 using mojo::util::StringToNumberWithError; | |
22 | 17 |
23 namespace mojo { | 18 namespace mojo { |
24 namespace embedder { | 19 namespace embedder { |
25 | 20 |
26 namespace { | |
27 | |
28 bool IsTargetDescriptorUsed( | |
29 const base::FileHandleMappingVector& file_handle_mapping, | |
30 int target_fd) { | |
31 for (size_t i = 0; i < file_handle_mapping.size(); i++) { | |
32 if (file_handle_mapping[i].second == target_fd) | |
33 return true; | |
34 } | |
35 return false; | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 const char PlatformChannelPair::kMojoPlatformChannelHandleSwitch[] = | |
41 "mojo-platform-channel-handle"; | |
42 | |
43 PlatformChannelPair::PlatformChannelPair() { | 21 PlatformChannelPair::PlatformChannelPair() { |
44 // Create the Unix domain socket and set the ends to nonblocking. | 22 // Create the Unix domain socket and set the ends to nonblocking. |
45 int fds[2]; | 23 int fds[2]; |
46 // TODO(vtl): Maybe fail gracefully if |socketpair()| fails. | 24 // TODO(vtl): Maybe fail gracefully if |socketpair()| fails. |
47 PCHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0); | 25 PCHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0); |
48 PCHECK(fcntl(fds[0], F_SETFL, O_NONBLOCK) == 0); | 26 PCHECK(fcntl(fds[0], F_SETFL, O_NONBLOCK) == 0); |
49 PCHECK(fcntl(fds[1], F_SETFL, O_NONBLOCK) == 0); | 27 PCHECK(fcntl(fds[1], F_SETFL, O_NONBLOCK) == 0); |
50 | 28 |
51 #if defined(OS_MACOSX) | 29 #if defined(OS_MACOSX) |
52 // This turns off |SIGPIPE| when writing to a closed socket (causing it to | 30 // This turns off |SIGPIPE| when writing to a closed socket (causing it to |
53 // fail with |EPIPE| instead). On Linux, we have to use |send...()| with | 31 // fail with |EPIPE| instead). On Linux, we have to use |send...()| with |
54 // |MSG_NOSIGNAL| -- which is not supported on Mac -- instead. | 32 // |MSG_NOSIGNAL| -- which is not supported on Mac -- instead. |
55 int no_sigpipe = 1; | 33 int no_sigpipe = 1; |
56 PCHECK(setsockopt(fds[0], SOL_SOCKET, SO_NOSIGPIPE, &no_sigpipe, | 34 PCHECK(setsockopt(fds[0], SOL_SOCKET, SO_NOSIGPIPE, &no_sigpipe, |
57 sizeof(no_sigpipe)) == 0); | 35 sizeof(no_sigpipe)) == 0); |
58 PCHECK(setsockopt(fds[1], SOL_SOCKET, SO_NOSIGPIPE, &no_sigpipe, | 36 PCHECK(setsockopt(fds[1], SOL_SOCKET, SO_NOSIGPIPE, &no_sigpipe, |
59 sizeof(no_sigpipe)) == 0); | 37 sizeof(no_sigpipe)) == 0); |
60 #endif // defined(OS_MACOSX) | 38 #endif // defined(OS_MACOSX) |
61 | 39 |
62 server_handle_.reset(PlatformHandle(fds[0])); | 40 handle0.reset(PlatformHandle(fds[0])); |
63 DCHECK(server_handle_.is_valid()); | 41 DCHECK(handle0.is_valid()); |
64 client_handle_.reset(PlatformHandle(fds[1])); | 42 handle1.reset(PlatformHandle(fds[1])); |
65 DCHECK(client_handle_.is_valid()); | 43 DCHECK(handle1.is_valid()); |
66 } | 44 } |
67 | 45 |
68 PlatformChannelPair::~PlatformChannelPair() { | 46 PlatformChannelPair::~PlatformChannelPair() {} |
69 } | |
70 | |
71 ScopedPlatformHandle PlatformChannelPair::PassServerHandle() { | |
72 return server_handle_.Pass(); | |
73 } | |
74 | |
75 ScopedPlatformHandle PlatformChannelPair::PassClientHandle() { | |
76 return client_handle_.Pass(); | |
77 } | |
78 | |
79 // static | |
80 ScopedPlatformHandle PlatformChannelPair::PassClientHandleFromParentProcess( | |
81 const std::string& string_from_parent) { | |
82 int client_fd = -1; | |
83 if (!StringToNumberWithError<int>(string_from_parent, &client_fd)) { | |
84 LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch; | |
85 return ScopedPlatformHandle(); | |
86 } | |
87 | |
88 return ScopedPlatformHandle(PlatformHandle(client_fd)); | |
89 } | |
90 | |
91 void PlatformChannelPair::PrepareToPassClientHandleToChildProcess( | |
92 std::string* string_for_child, | |
93 HandlePassingInformation* handle_passing_info) const { | |
94 DCHECK(string_for_child); | |
95 DCHECK(handle_passing_info); | |
96 // This is an arbitrary sanity check. (Note that this guarantees that the loop | |
97 // below will terminate sanely.) | |
98 CHECK_LT(handle_passing_info->size(), 1000u); | |
99 | |
100 DCHECK(client_handle_.is_valid()); | |
101 | |
102 // Find a suitable FD to map our client handle to in the child process. | |
103 // This has quadratic time complexity in the size of |*handle_passing_info|, | |
104 // but |*handle_passing_info| should be very small (usually/often empty). | |
105 int target_fd = base::GlobalDescriptors::kBaseDescriptor; | |
106 while (IsTargetDescriptorUsed(*handle_passing_info, target_fd)) | |
107 target_fd++; | |
108 | |
109 handle_passing_info->push_back( | |
110 std::pair<int, int>(client_handle_.get().fd, target_fd)); | |
111 *string_for_child = NumberToString<int>(target_fd); | |
112 } | |
113 | |
114 void PlatformChannelPair::ChildProcessLaunched() { | |
115 DCHECK(client_handle_.is_valid()); | |
116 client_handle_.reset(); | |
117 } | |
118 | 47 |
119 } // namespace embedder | 48 } // namespace embedder |
120 } // namespace mojo | 49 } // namespace mojo |
OLD | NEW |