Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: mojo/system/embedder/platform_channel_pair_posix.cc

Issue 203373004: Mojo: Move mojo/system/embedder to mojo/embedder. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/embedder/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/embedder/platform_handle.h"
17
18 namespace mojo {
19 namespace embedder {
20
21 namespace {
22
23 bool IsTargetDescriptorUsed(
24 const base::FileHandleMappingVector& file_handle_mapping,
25 int target_fd) {
26 for (size_t i = 0; i < file_handle_mapping.size(); i++) {
27 if (file_handle_mapping[i].second == target_fd)
28 return true;
29 }
30 return false;
31 }
32
33 } // namespace
34
35 PlatformChannelPair::PlatformChannelPair() {
36 // Create the Unix domain socket and set the ends to nonblocking.
37 int fds[2];
38 // TODO(vtl): Maybe fail gracefully if |socketpair()| fails.
39 PCHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0);
40 PCHECK(fcntl(fds[0], F_SETFL, O_NONBLOCK) == 0);
41 PCHECK(fcntl(fds[1], F_SETFL, O_NONBLOCK) == 0);
42
43 server_handle_.reset(PlatformHandle(fds[0]));
44 DCHECK(server_handle_.is_valid());
45 client_handle_.reset(PlatformHandle(fds[1]));
46 DCHECK(client_handle_.is_valid());
47 }
48
49 // static
50 ScopedPlatformHandle PlatformChannelPair::PassClientHandleFromParentProcess(
51 const CommandLine& command_line) {
52 std::string client_fd_string =
53 command_line.GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch);
54 int client_fd = -1;
55 if (client_fd_string.empty() ||
56 !base::StringToInt(client_fd_string, &client_fd) ||
57 client_fd < base::GlobalDescriptors::kBaseDescriptor) {
58 LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch;
59 return ScopedPlatformHandle();
60 }
61
62 return ScopedPlatformHandle(PlatformHandle(client_fd));
63 }
64
65 void PlatformChannelPair::PrepareToPassClientHandleToChildProcess(
66 CommandLine* command_line,
67 base::FileHandleMappingVector* handle_passing_info) const {
68 DCHECK(command_line);
69 DCHECK(handle_passing_info);
70 // This is an arbitrary sanity check. (Note that this guarantees that the loop
71 // below will terminate sanely.)
72 CHECK_LT(handle_passing_info->size(), 1000u);
73
74 DCHECK(client_handle_.is_valid());
75
76 // Find a suitable FD to map our client handle to in the child process.
77 // This has quadratic time complexity in the size of |*handle_passing_info|,
78 // but |*handle_passing_info| should be very small (usually/often empty).
79 int target_fd = base::GlobalDescriptors::kBaseDescriptor;
80 while (IsTargetDescriptorUsed(*handle_passing_info, target_fd))
81 target_fd++;
82
83 handle_passing_info->push_back(std::pair<int, int>(client_handle_.get().fd,
84 target_fd));
85 // Log a warning if the command line already has the switch, but "clobber" it
86 // anyway, since it's reasonably likely that all the switches were just copied
87 // from the parent.
88 LOG_IF(WARNING, command_line->HasSwitch(kMojoPlatformChannelHandleSwitch))
89 << "Child command line already has switch --"
90 << kMojoPlatformChannelHandleSwitch << "="
91 << command_line->GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch);
92 // (Any existing switch won't actually be removed from the command line, but
93 // the last one appended takes precedence.)
94 command_line->AppendSwitchASCII(kMojoPlatformChannelHandleSwitch,
95 base::IntToString(target_fd));
96 }
97
98 } // namespace embedder
99 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/embedder/platform_channel_pair.cc ('k') | mojo/system/embedder/platform_channel_pair_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698