Chromium Code Reviews| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 | 28 |
| 29 #if !defined(SO_PEEK_OFF) | 29 #if !defined(SO_PEEK_OFF) |
| 30 #define SO_PEEK_OFF 42 | 30 #define SO_PEEK_OFF 42 |
| 31 #endif | 31 #endif |
| 32 | 32 |
| 33 namespace mojo { | 33 namespace mojo { |
| 34 namespace edk { | 34 namespace edk { |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 #if !defined(OS_ANDROID) | |
| 38 bool IsTargetDescriptorUsed( | 39 bool IsTargetDescriptorUsed( |
| 39 const base::FileHandleMappingVector& file_handle_mapping, | 40 const base::FileHandleMappingVector& file_handle_mapping, |
| 40 int target_fd) { | 41 int target_fd) { |
| 41 for (size_t i = 0; i < file_handle_mapping.size(); i++) { | 42 for (size_t i = 0; i < file_handle_mapping.size(); i++) { |
| 42 if (file_handle_mapping[i].second == target_fd) | 43 if (file_handle_mapping[i].second == target_fd) |
| 43 return true; | 44 return true; |
| 44 } | 45 } |
| 45 return false; | 46 return false; |
| 46 } | 47 } |
| 48 #endif | |
| 47 | 49 |
| 48 } // namespace | 50 } // namespace |
| 49 | 51 |
| 50 PlatformChannelPair::PlatformChannelPair(bool client_is_blocking) { | 52 PlatformChannelPair::PlatformChannelPair(bool client_is_blocking) { |
| 51 // Create the Unix domain socket. | 53 // Create the Unix domain socket. |
| 52 int fds[2]; | 54 int fds[2]; |
| 53 // TODO(vtl): Maybe fail gracefully if |socketpair()| fails. | 55 // TODO(vtl): Maybe fail gracefully if |socketpair()| fails. |
| 54 | 56 |
| 55 #if defined(OS_NACL_SFI) | 57 #if defined(OS_NACL_SFI) |
| 56 PCHECK(imc_socketpair(fds) == 0); | 58 PCHECK(imc_socketpair(fds) == 0); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 85 const base::CommandLine& command_line) { | 87 const base::CommandLine& command_line) { |
| 86 std::string client_fd_string = | 88 std::string client_fd_string = |
| 87 command_line.GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch); | 89 command_line.GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch); |
| 88 return PassClientHandleFromParentProcessFromString(client_fd_string); | 90 return PassClientHandleFromParentProcessFromString(client_fd_string); |
| 89 } | 91 } |
| 90 | 92 |
| 91 ScopedPlatformHandle | 93 ScopedPlatformHandle |
| 92 PlatformChannelPair::PassClientHandleFromParentProcessFromString( | 94 PlatformChannelPair::PassClientHandleFromParentProcessFromString( |
| 93 const std::string& value) { | 95 const std::string& value) { |
| 94 int client_fd = -1; | 96 int client_fd = -1; |
| 97 #if defined(OS_ANDROID) | |
| 98 base::GlobalDescriptors::Key fd_id = -1; | |
| 99 if (value.empty() || !base::StringToUint(value, &fd_id)) { | |
| 100 LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch; | |
| 101 return ScopedPlatformHandle(); | |
| 102 } | |
| 103 client_fd = base::GlobalDescriptors::GetInstance()->Get(fd_id); | |
| 104 #else | |
| 95 if (value.empty() || | 105 if (value.empty() || |
| 96 !base::StringToInt(value, &client_fd) || | 106 !base::StringToInt(value, &client_fd) || |
| 97 client_fd < base::GlobalDescriptors::kBaseDescriptor) { | 107 client_fd < base::GlobalDescriptors::kBaseDescriptor) { |
| 98 LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch; | 108 LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch; |
| 99 return ScopedPlatformHandle(); | 109 return ScopedPlatformHandle(); |
| 100 } | 110 } |
| 101 | 111 #endif |
| 102 return ScopedPlatformHandle(PlatformHandle(client_fd)); | 112 return ScopedPlatformHandle(PlatformHandle(client_fd)); |
| 103 } | 113 } |
| 104 | 114 |
| 105 void PlatformChannelPair::PrepareToPassClientHandleToChildProcess( | 115 void PlatformChannelPair::PrepareToPassClientHandleToChildProcess( |
| 106 base::CommandLine* command_line, | 116 base::CommandLine* command_line, |
| 107 base::FileHandleMappingVector* handle_passing_info) const { | 117 base::FileHandleMappingVector* handle_passing_info) const { |
| 108 DCHECK(command_line); | 118 DCHECK(command_line); |
| 109 | 119 |
| 110 // Log a warning if the command line already has the switch, but "clobber" it | 120 // Log a warning if the command line already has the switch, but "clobber" it |
| 111 // anyway, since it's reasonably likely that all the switches were just copied | 121 // anyway, since it's reasonably likely that all the switches were just copied |
| 112 // from the parent. | 122 // from the parent. |
| 113 LOG_IF(WARNING, command_line->HasSwitch(kMojoPlatformChannelHandleSwitch)) | 123 LOG_IF(WARNING, command_line->HasSwitch(kMojoPlatformChannelHandleSwitch)) |
| 114 << "Child command line already has switch --" | 124 << "Child command line already has switch --" |
| 115 << kMojoPlatformChannelHandleSwitch << "=" | 125 << kMojoPlatformChannelHandleSwitch << "=" |
| 116 << command_line->GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch); | 126 << command_line->GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch); |
| 117 // (Any existing switch won't actually be removed from the command line, but | 127 // (Any existing switch won't actually be removed from the command line, but |
| 118 // the last one appended takes precedence.) | 128 // the last one appended takes precedence.) |
| 119 command_line->AppendSwitchASCII( | 129 command_line->AppendSwitchASCII( |
| 120 kMojoPlatformChannelHandleSwitch, | 130 kMojoPlatformChannelHandleSwitch, |
| 121 PrepareToPassClientHandleToChildProcessAsString(handle_passing_info)); | 131 PrepareToPassClientHandleToChildProcessAsString(handle_passing_info)); |
| 122 } | 132 } |
| 123 | 133 |
| 134 | |
|
Robert Sesek
2016/12/20 20:44:34
nit: extra blank line
Jay Civelli
2016/12/20 23:07:27
Done.
| |
| 124 std::string | 135 std::string |
| 125 PlatformChannelPair::PrepareToPassClientHandleToChildProcessAsString( | 136 PlatformChannelPair::PrepareToPassClientHandleToChildProcessAsString( |
| 126 HandlePassingInformation* handle_passing_info) const { | 137 HandlePassingInformation* handle_passing_info) const { |
| 138 #if defined(OS_ANDROID) | |
| 139 int fd = client_handle_.get().handle; | |
| 140 base::GlobalDescriptors::Key fd_id = | |
| 141 base::GlobalDescriptors::GetInstance()->Register(fd); | |
| 142 handle_passing_info->push_back(std::pair<int, int>(fd, fd_id)); | |
| 143 return base::UintToString(fd_id); | |
| 144 #else | |
| 127 DCHECK(handle_passing_info); | 145 DCHECK(handle_passing_info); |
| 128 // This is an arbitrary sanity check. (Note that this guarantees that the loop | 146 // This is an arbitrary sanity check. (Note that this guarantees that the loop |
| 129 // below will terminate sanely.) | 147 // below will terminate sanely.) |
| 130 CHECK_LT(handle_passing_info->size(), 1000u); | 148 CHECK_LT(handle_passing_info->size(), 1000u); |
| 131 | 149 |
| 132 DCHECK(client_handle_.is_valid()); | 150 DCHECK(client_handle_.is_valid()); |
| 133 | 151 |
| 134 // Find a suitable FD to map our client handle to in the child process. | 152 // Find a suitable FD to map our client handle to in the child process. |
| 135 // This has quadratic time complexity in the size of |*handle_passing_info|, | 153 // This has quadratic time complexity in the size of |*handle_passing_info|, |
| 136 // but |*handle_passing_info| should be very small (usually/often empty). | 154 // but |*handle_passing_info| should be very small (usually/often empty). |
| 137 int target_fd = base::GlobalDescriptors::kBaseDescriptor; | 155 int target_fd = base::GlobalDescriptors::kBaseDescriptor; |
| 138 while (IsTargetDescriptorUsed(*handle_passing_info, target_fd)) | 156 while (IsTargetDescriptorUsed(*handle_passing_info, target_fd)) |
| 139 target_fd++; | 157 target_fd++; |
| 140 | 158 |
| 141 handle_passing_info->push_back( | 159 handle_passing_info->push_back( |
| 142 std::pair<int, int>(client_handle_.get().handle, target_fd)); | 160 std::pair<int, int>(client_handle_.get().handle, target_fd)); |
| 143 return base::IntToString(target_fd); | 161 return base::IntToString(target_fd); |
| 162 #endif | |
| 144 } | 163 } |
| 145 | 164 |
| 146 } // namespace edk | 165 } // namespace edk |
| 147 } // namespace mojo | 166 } // namespace mojo |
| OLD | NEW |