OLD | NEW |
| (Empty) |
1 // Copyright 2013 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.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 | |
17 namespace mojo { | |
18 namespace system { | |
19 | |
20 namespace { | |
21 | |
22 const char kMojoChannelDescriptorSwitch[] = "mojo-channel-descriptor"; | |
23 | |
24 bool IsTargetDescriptorUsed( | |
25 const base::FileHandleMappingVector& file_handle_mapping, | |
26 int target_fd) { | |
27 for (size_t i = 0; i < file_handle_mapping.size(); i++) { | |
28 if (file_handle_mapping[i].second == target_fd) | |
29 return true; | |
30 } | |
31 return false; | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 PlatformChannelPair::PlatformChannelPair() { | |
37 // Create the Unix domain socket and set the ends to nonblocking. | |
38 int fds[2]; | |
39 // TODO(vtl): Maybe fail gracefully if |socketpair()| fails. | |
40 PCHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0); | |
41 PCHECK(fcntl(fds[0], F_SETFL, O_NONBLOCK) == 0); | |
42 PCHECK(fcntl(fds[1], F_SETFL, O_NONBLOCK) == 0); | |
43 | |
44 server_handle_.fd = fds[0]; | |
45 DCHECK(server_handle_.is_valid()); | |
46 client_handle_.fd = fds[1]; | |
47 DCHECK(client_handle_.is_valid()); | |
48 } | |
49 | |
50 // static | |
51 scoped_ptr<PlatformChannel> | |
52 PlatformChannelPair::CreateClientChannelFromParentProcess( | |
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 scoped_ptr<PlatformChannel>(); | |
62 } | |
63 | |
64 return PlatformChannel::CreateFromHandle(PlatformChannelHandle(client_fd)); | |
65 } | |
66 | |
67 void PlatformChannelPair::PrepareToPassClientChannelToChildProcess( | |
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_.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_.CloseIfNecessary(); | |
103 } | |
104 | |
105 } // namespace system | |
106 } // namespace mojo | |
OLD | NEW |