| 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/command_line.h" | |
| 13 #include "base/logging.h" | 12 #include "base/logging.h" |
| 14 #include "base/posix/global_descriptors.h" | 13 #include "base/posix/global_descriptors.h" |
| 15 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 16 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 17 #include "mojo/edk/embedder/platform_handle.h" | 16 #include "mojo/edk/embedder/platform_handle.h" |
| 18 | 17 |
| 19 namespace mojo { | 18 namespace mojo { |
| 20 namespace embedder { | 19 namespace embedder { |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 ScopedPlatformHandle PlatformChannelPair::PassServerHandle() { | 66 ScopedPlatformHandle PlatformChannelPair::PassServerHandle() { |
| 68 return server_handle_.Pass(); | 67 return server_handle_.Pass(); |
| 69 } | 68 } |
| 70 | 69 |
| 71 ScopedPlatformHandle PlatformChannelPair::PassClientHandle() { | 70 ScopedPlatformHandle PlatformChannelPair::PassClientHandle() { |
| 72 return client_handle_.Pass(); | 71 return client_handle_.Pass(); |
| 73 } | 72 } |
| 74 | 73 |
| 75 // static | 74 // static |
| 76 ScopedPlatformHandle PlatformChannelPair::PassClientHandleFromParentProcess( | 75 ScopedPlatformHandle PlatformChannelPair::PassClientHandleFromParentProcess( |
| 77 const base::CommandLine& command_line) { | 76 const std::string& string_from_parent) { |
| 78 std::string client_fd_string = | |
| 79 command_line.GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch); | |
| 80 int client_fd = -1; | 77 int client_fd = -1; |
| 81 if (client_fd_string.empty() || | 78 if (!base::StringToInt(string_from_parent, &client_fd)) { |
| 82 !base::StringToInt(client_fd_string, &client_fd) || | |
| 83 client_fd < base::GlobalDescriptors::kBaseDescriptor) { | |
| 84 LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch; | 79 LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch; |
| 85 return ScopedPlatformHandle(); | 80 return ScopedPlatformHandle(); |
| 86 } | 81 } |
| 87 | 82 |
| 88 return ScopedPlatformHandle(PlatformHandle(client_fd)); | 83 return ScopedPlatformHandle(PlatformHandle(client_fd)); |
| 89 } | 84 } |
| 90 | 85 |
| 91 void PlatformChannelPair::PrepareToPassClientHandleToChildProcess( | 86 void PlatformChannelPair::PrepareToPassClientHandleToChildProcess( |
| 92 base::CommandLine* command_line, | 87 std::string* string_for_child, |
| 93 base::FileHandleMappingVector* handle_passing_info) const { | 88 HandlePassingInformation* handle_passing_info) const { |
| 94 DCHECK(command_line); | 89 DCHECK(string_for_child); |
| 95 DCHECK(handle_passing_info); | 90 DCHECK(handle_passing_info); |
| 96 // This is an arbitrary sanity check. (Note that this guarantees that the loop | 91 // This is an arbitrary sanity check. (Note that this guarantees that the loop |
| 97 // below will terminate sanely.) | 92 // below will terminate sanely.) |
| 98 CHECK_LT(handle_passing_info->size(), 1000u); | 93 CHECK_LT(handle_passing_info->size(), 1000u); |
| 99 | 94 |
| 100 DCHECK(client_handle_.is_valid()); | 95 DCHECK(client_handle_.is_valid()); |
| 101 | 96 |
| 102 // Find a suitable FD to map our client handle to in the child process. | 97 // 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|, | 98 // This has quadratic time complexity in the size of |*handle_passing_info|, |
| 104 // but |*handle_passing_info| should be very small (usually/often empty). | 99 // but |*handle_passing_info| should be very small (usually/often empty). |
| 105 int target_fd = base::GlobalDescriptors::kBaseDescriptor; | 100 int target_fd = base::GlobalDescriptors::kBaseDescriptor; |
| 106 while (IsTargetDescriptorUsed(*handle_passing_info, target_fd)) | 101 while (IsTargetDescriptorUsed(*handle_passing_info, target_fd)) |
| 107 target_fd++; | 102 target_fd++; |
| 108 | 103 |
| 109 handle_passing_info->push_back( | 104 handle_passing_info->push_back( |
| 110 std::pair<int, int>(client_handle_.get().fd, target_fd)); | 105 std::pair<int, int>(client_handle_.get().fd, target_fd)); |
| 111 // Log a warning if the command line already has the switch, but "clobber" it | 106 *string_for_child = base::IntToString(target_fd); |
| 112 // anyway, since it's reasonably likely that all the switches were just copied | |
| 113 // from the parent. | |
| 114 LOG_IF(WARNING, command_line->HasSwitch(kMojoPlatformChannelHandleSwitch)) | |
| 115 << "Child command line already has switch --" | |
| 116 << kMojoPlatformChannelHandleSwitch << "=" | |
| 117 << command_line->GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch); | |
| 118 // (Any existing switch won't actually be removed from the command line, but | |
| 119 // the last one appended takes precedence.) | |
| 120 command_line->AppendSwitchASCII(kMojoPlatformChannelHandleSwitch, | |
| 121 base::IntToString(target_fd)); | |
| 122 } | 107 } |
| 123 | 108 |
| 124 void PlatformChannelPair::ChildProcessLaunched() { | 109 void PlatformChannelPair::ChildProcessLaunched() { |
| 125 DCHECK(client_handle_.is_valid()); | 110 DCHECK(client_handle_.is_valid()); |
| 126 client_handle_.reset(); | 111 client_handle_.reset(); |
| 127 } | 112 } |
| 128 | 113 |
| 129 } // namespace embedder | 114 } // namespace embedder |
| 130 } // namespace mojo | 115 } // namespace mojo |
| OLD | NEW |