OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/system/platform_channel_pair.h" | |
6 | |
7 #include <fcntl.h> | |
8 #include <sys/socket.h> | |
9 #include <sys/types.h> | |
10 #include <unistd.h> | |
11 | |
12 #include "base/command_line.h" | |
13 #include "base/logging.h" | |
14 #include "base/posix/global_descriptors.h" | |
15 #include "base/strings/string_number_conversions.h" | |
16 #include "mojo/system/platform_handle.h" | |
17 | |
18 namespace mojo { | |
19 namespace embedder { | |
20 | |
21 namespace { | |
22 | |
23 const char kMojoChannelDescriptorSwitch[] = "mojo-channel-descriptor"; | |
24 | |
25 bool IsTargetDescriptorUsed( | |
26 const base::FileHandleMappingVector& file_handle_mapping, | |
27 int target_fd) { | |
28 for (size_t i = 0; i < file_handle_mapping.size(); i++) { | |
29 if (file_handle_mapping[i].second == target_fd) | |
30 return true; | |
31 } | |
32 return false; | |
33 } | |
34 | |
35 } // namespace | |
36 | |
37 PlatformChannelPair::PlatformChannelPair() { | |
38 // Create the Unix domain socket and set the ends to nonblocking. | |
39 int fds[2]; | |
40 // TODO(vtl): Maybe fail gracefully if |socketpair()| fails. | |
41 PCHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0); | |
42 PCHECK(fcntl(fds[0], F_SETFL, O_NONBLOCK) == 0); | |
43 PCHECK(fcntl(fds[1], F_SETFL, O_NONBLOCK) == 0); | |
44 | |
45 server_handle_.reset(PlatformHandle(fds[0])); | |
46 DCHECK(server_handle_.is_valid()); | |
47 client_handle_.reset(PlatformHandle(fds[1])); | |
48 DCHECK(client_handle_.is_valid()); | |
49 } | |
50 | |
51 // static | |
52 ScopedPlatformHandle PlatformChannelPair::PassClientHandleFromParentProcess( | |
53 const CommandLine& command_line) { | |
54 std::string client_fd_string = | |
55 command_line.GetSwitchValueASCII(kMojoChannelDescriptorSwitch); | |
56 int client_fd = -1; | |
57 if (client_fd_string.empty() || | |
58 !base::StringToInt(client_fd_string, &client_fd) || | |
59 client_fd < base::GlobalDescriptors::kBaseDescriptor) { | |
60 LOG(ERROR) << "Missing or invalid --" << kMojoChannelDescriptorSwitch; | |
61 return ScopedPlatformHandle(); | |
62 } | |
63 | |
64 return ScopedPlatformHandle(PlatformHandle(client_fd)); | |
65 } | |
66 | |
67 void PlatformChannelPair::PrepareToPassClientHandleToChildProcess( | |
68 CommandLine* command_line, | |
69 base::FileHandleMappingVector* file_handle_mapping) const { | |
70 DCHECK(command_line); | |
71 DCHECK(file_handle_mapping); | |
72 // This is an arbitrary sanity check. (Note that this guarantees that the loop | |
73 // below will terminate sanely.) | |
74 CHECK_LT(file_handle_mapping->size(), 1000u); | |
75 | |
76 DCHECK(client_handle_.is_valid()); | |
77 | |
78 // Find a suitable FD to map our client handle to in the child process. | |
79 // This has quadratic time complexity in the size of |*file_handle_mapping|, | |
80 // but |*file_handle_mapping| should be very small (usually/often empty). | |
81 int target_fd = base::GlobalDescriptors::kBaseDescriptor; | |
82 while (IsTargetDescriptorUsed(*file_handle_mapping, target_fd)) | |
83 target_fd++; | |
84 | |
85 file_handle_mapping->push_back(std::pair<int, int>(client_handle_.get().fd, | |
86 target_fd)); | |
87 // Log a warning if the command line already has the switch, but "clobber" it | |
88 // anyway, since it's reasonably likely that all the switches were just copied | |
89 // from the parent. | |
90 LOG_IF(WARNING, command_line->HasSwitch(kMojoChannelDescriptorSwitch)) | |
91 << "Child command line already has switch --" | |
92 << kMojoChannelDescriptorSwitch << "=" | |
93 << command_line->GetSwitchValueASCII(kMojoChannelDescriptorSwitch); | |
94 // (Any existing switch won't actually be removed from the command line, but | |
95 // the last one appended takes precedence.) | |
96 command_line->AppendSwitchASCII(kMojoChannelDescriptorSwitch, | |
97 base::IntToString(target_fd)); | |
98 } | |
99 | |
100 void PlatformChannelPair::ChildProcessLaunched() { | |
101 DCHECK(client_handle_.is_valid()); | |
102 client_handle_.reset(); | |
103 } | |
104 | |
105 } // namespace embedder | |
106 } // namespace mojo | |
OLD | NEW |