15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "mojo/embedder/platform_channel_pair.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <fcntl.h>
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <sys/socket.h>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <sys/types.h>
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <unistd.h>
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/command_line.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/logging.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/posix/global_descriptors.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/string_number_conversions.h"
16effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "build/build_config.h"
1723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "mojo/embedder/platform_handle.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace mojo {
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace embedder {
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool IsTargetDescriptorUsed(
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::FileHandleMappingVector& file_handle_mapping,
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int target_fd) {
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (size_t i = 0; i < file_handle_mapping.size(); i++) {
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (file_handle_mapping[i].second == target_fd)
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return true;
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PlatformChannelPair::PlatformChannelPair() {
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Create the Unix domain socket and set the ends to nonblocking.
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int fds[2];
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(vtl): Maybe fail gracefully if |socketpair()| fails.
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PCHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0);
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PCHECK(fcntl(fds[0], F_SETFL, O_NONBLOCK) == 0);
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PCHECK(fcntl(fds[1], F_SETFL, O_NONBLOCK) == 0);
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
44effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#if defined(OS_MACOSX)
45effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // This turns off |SIGPIPE| when writing to a closed socket (causing it to
46effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // fail with |EPIPE| instead). On Linux, we have to use |send...()| with
47effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // |MSG_NOSIGNAL| -- which is not supported on Mac -- instead.
48effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  int no_sigpipe = 1;
495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  PCHECK(
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      setsockopt(
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          fds[0], SOL_SOCKET, SO_NOSIGPIPE, &no_sigpipe, sizeof(no_sigpipe)) ==
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      0);
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  PCHECK(
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      setsockopt(
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          fds[1], SOL_SOCKET, SO_NOSIGPIPE, &no_sigpipe, sizeof(no_sigpipe)) ==
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      0);
57effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif  // defined(OS_MACOSX)
58effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  server_handle_.reset(PlatformHandle(fds[0]));
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(server_handle_.is_valid());
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  client_handle_.reset(PlatformHandle(fds[1]));
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(client_handle_.is_valid());
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ScopedPlatformHandle PlatformChannelPair::PassClientHandleFromParentProcess(
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::CommandLine& command_line) {
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string client_fd_string =
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      command_line.GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch);
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int client_fd = -1;
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (client_fd_string.empty() ||
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      !base::StringToInt(client_fd_string, &client_fd) ||
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      client_fd < base::GlobalDescriptors::kBaseDescriptor) {
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch;
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return ScopedPlatformHandle();
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return ScopedPlatformHandle(PlatformHandle(client_fd));
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PlatformChannelPair::PrepareToPassClientHandleToChildProcess(
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::CommandLine* command_line,
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::FileHandleMappingVector* handle_passing_info) const {
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(command_line);
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(handle_passing_info);
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This is an arbitrary sanity check. (Note that this guarantees that the loop
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // below will terminate sanely.)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CHECK_LT(handle_passing_info->size(), 1000u);
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(client_handle_.is_valid());
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Find a suitable FD to map our client handle to in the child process.
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This has quadratic time complexity in the size of |*handle_passing_info|,
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // but |*handle_passing_info| should be very small (usually/often empty).
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int target_fd = base::GlobalDescriptors::kBaseDescriptor;
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  while (IsTargetDescriptorUsed(*handle_passing_info, target_fd))
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    target_fd++;
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  handle_passing_info->push_back(
1005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      std::pair<int, int>(client_handle_.get().fd, target_fd));
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Log a warning if the command line already has the switch, but "clobber" it
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // anyway, since it's reasonably likely that all the switches were just copied
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // from the parent.
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  LOG_IF(WARNING, command_line->HasSwitch(kMojoPlatformChannelHandleSwitch))
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      << "Child command line already has switch --"
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      << kMojoPlatformChannelHandleSwitch << "="
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      << command_line->GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch);
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // (Any existing switch won't actually be removed from the command line, but
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the last one appended takes precedence.)
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  command_line->AppendSwitchASCII(kMojoPlatformChannelHandleSwitch,
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  base::IntToString(target_fd));
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace embedder
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace mojo
116