| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "services/service_manager/runner/host/child_process_base.h" | 5 #include "services/service_manager/public/cpp/standalone_service/standalone_serv
ice.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/debug/stack_trace.h" | 8 #include "base/debug/stack_trace.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 13 #include "base/single_thread_task_runner.h" | |
| 14 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 16 #include "mojo/edk/embedder/embedder.h" | 13 #include "mojo/edk/embedder/embedder.h" |
| 17 #include "mojo/edk/embedder/process_delegate.h" | 14 #include "mojo/edk/embedder/process_delegate.h" |
| 15 #include "mojo/public/cpp/system/message_pipe.h" |
| 16 #include "services/service_manager/public/cpp/service_context.h" |
| 18 #include "services/service_manager/runner/common/client_util.h" | 17 #include "services/service_manager/runner/common/client_util.h" |
| 19 #include "services/service_manager/runner/common/switches.h" | 18 #include "services/service_manager/runner/common/switches.h" |
| 20 | 19 |
| 21 #if defined(OS_LINUX) | 20 #if defined(OS_LINUX) |
| 22 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
| 23 #include "base/sys_info.h" | 22 #include "base/sys_info.h" |
| 24 #include "services/service_manager/runner/host/linux_sandbox.h" | 23 #include "services/service_manager/public/cpp/standalone_service/linux_sandbox.h
" |
| 25 #endif | 24 #endif |
| 26 | 25 |
| 27 #if defined(OS_MACOSX) | 26 #if defined(OS_MACOSX) |
| 28 #include "services/service_manager/runner/host/mach_broker.h" | 27 #include "services/service_manager/public/cpp/standalone_service/mach_broker.h" |
| 29 #endif | 28 #endif |
| 30 | 29 |
| 31 namespace service_manager { | 30 namespace service_manager { |
| 32 | |
| 33 namespace { | 31 namespace { |
| 34 | 32 |
| 35 #if defined(OS_LINUX) | 33 #if defined(OS_LINUX) |
| 36 std::unique_ptr<LinuxSandbox> InitializeSandbox() { | 34 std::unique_ptr<LinuxSandbox> InitializeSandbox() { |
| 37 using sandbox::syscall_broker::BrokerFilePermission; | 35 using sandbox::syscall_broker::BrokerFilePermission; |
| 38 // Warm parts of base in the copy of base in the mojo runner. | 36 // Warm parts of base in the copy of base in the mojo runner. |
| 39 base::RandUint64(); | 37 base::RandUint64(); |
| 40 base::SysInfo::AmountOfPhysicalMemory(); | 38 base::SysInfo::AmountOfPhysicalMemory(); |
| 41 base::SysInfo::NumberOfProcessors(); | 39 base::SysInfo::NumberOfProcessors(); |
| 42 | 40 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 58 | 56 |
| 59 // Should be created and initialized on the main thread and kept alive as long | 57 // Should be created and initialized on the main thread and kept alive as long |
| 60 // a Service is running in the current process. | 58 // a Service is running in the current process. |
| 61 class ScopedAppContext : public mojo::edk::ProcessDelegate { | 59 class ScopedAppContext : public mojo::edk::ProcessDelegate { |
| 62 public: | 60 public: |
| 63 ScopedAppContext() | 61 ScopedAppContext() |
| 64 : io_thread_("io_thread"), | 62 : io_thread_("io_thread"), |
| 65 wait_for_shutdown_event_( | 63 wait_for_shutdown_event_( |
| 66 base::WaitableEvent::ResetPolicy::MANUAL, | 64 base::WaitableEvent::ResetPolicy::MANUAL, |
| 67 base::WaitableEvent::InitialState::NOT_SIGNALED) { | 65 base::WaitableEvent::InitialState::NOT_SIGNALED) { |
| 68 // Initialize Mojo before starting any threads. | |
| 69 mojo::edk::Init(); | 66 mojo::edk::Init(); |
| 70 | 67 io_thread_.StartWithOptions( |
| 71 // Create and start our I/O thread. | 68 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 72 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); | 69 mojo::edk::InitIPCSupport(this, io_thread_.task_runner()); |
| 73 CHECK(io_thread_.StartWithOptions(io_thread_options)); | |
| 74 io_runner_ = io_thread_.task_runner().get(); | |
| 75 CHECK(io_runner_.get()); | |
| 76 | |
| 77 mojo::edk::InitIPCSupport(this, io_runner_); | |
| 78 mojo::edk::SetParentPipeHandleFromCommandLine(); | 70 mojo::edk::SetParentPipeHandleFromCommandLine(); |
| 79 } | 71 } |
| 80 | 72 |
| 81 ~ScopedAppContext() override { | 73 ~ScopedAppContext() override { |
| 82 mojo::edk::ShutdownIPCSupport(); | 74 mojo::edk::ShutdownIPCSupport(); |
| 83 wait_for_shutdown_event_.Wait(); | 75 wait_for_shutdown_event_.Wait(); |
| 84 } | 76 } |
| 85 | 77 |
| 86 private: | 78 private: |
| 87 // ProcessDelegate implementation. | 79 // ProcessDelegate implementation. |
| 88 void OnShutdownComplete() override { | 80 void OnShutdownComplete() override { |
| 89 wait_for_shutdown_event_.Signal(); | 81 wait_for_shutdown_event_.Signal(); |
| 90 } | 82 } |
| 91 | 83 |
| 92 base::Thread io_thread_; | 84 base::Thread io_thread_; |
| 93 scoped_refptr<base::SingleThreadTaskRunner> io_runner_; | |
| 94 | 85 |
| 95 // Used to unblock the main thread on shutdown. | 86 // Used to unblock the main thread on shutdown. |
| 96 base::WaitableEvent wait_for_shutdown_event_; | 87 base::WaitableEvent wait_for_shutdown_event_; |
| 97 | 88 |
| 98 DISALLOW_COPY_AND_ASSIGN(ScopedAppContext); | 89 DISALLOW_COPY_AND_ASSIGN(ScopedAppContext); |
| 99 }; | 90 }; |
| 100 | 91 |
| 101 } // namespace | 92 } // namespace |
| 102 | 93 |
| 103 void ChildProcessMainWithCallback(const RunCallback& callback) { | 94 void RunStandaloneService(const StandaloneServiceCallback& callback) { |
| 104 DCHECK(!base::MessageLoop::current()); | 95 DCHECK(!base::MessageLoop::current()); |
| 105 | 96 |
| 106 #if defined(OS_MACOSX) | 97 #if defined(OS_MACOSX) |
| 107 // Send our task port to the parent. | 98 // Send our task port to the parent. |
| 108 MachBroker::SendTaskPortToParent(); | 99 MachBroker::SendTaskPortToParent(); |
| 109 #endif | 100 #endif |
| 110 | 101 |
| 111 #if !defined(OFFICIAL_BUILD) | 102 #if !defined(OFFICIAL_BUILD) |
| 112 // Initialize stack dumping just before initializing sandbox to make | 103 // Initialize stack dumping just before initializing sandbox to make |
| 113 // sure symbol names in all loaded libraries will be cached. | 104 // sure symbol names in all loaded libraries will be cached. |
| 114 base::debug::EnableInProcessStackDumping(); | 105 base::debug::EnableInProcessStackDumping(); |
| 115 #endif | 106 #endif |
| 116 #if defined(OS_LINUX) | 107 #if defined(OS_LINUX) |
| 117 std::unique_ptr<LinuxSandbox> sandbox; | 108 std::unique_ptr<LinuxSandbox> sandbox; |
| 118 const base::CommandLine& command_line = | 109 const base::CommandLine& command_line = |
| 119 *base::CommandLine::ForCurrentProcess(); | 110 *base::CommandLine::ForCurrentProcess(); |
| 120 if (command_line.HasSwitch(switches::kEnableSandbox)) | 111 if (command_line.HasSwitch(switches::kEnableSandbox)) |
| 121 sandbox = InitializeSandbox(); | 112 sandbox = InitializeSandbox(); |
| 122 #endif | 113 #endif |
| 123 | 114 |
| 124 ScopedAppContext app_context; | 115 ScopedAppContext app_context; |
| 125 callback.Run(GetServiceRequestFromCommandLine()); | 116 callback.Run(GetServiceRequestFromCommandLine()); |
| 126 } | 117 } |
| 127 | 118 |
| 128 } // namespace service_manager | 119 } // namespace service_manager |
| OLD | NEW |