| OLD | NEW |
| 1 // Copyright 2016 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/public/cpp/standalone_service/standalone_serv
ice.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/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "mojo/edk/embedder/embedder.h" | 13 #include "mojo/edk/embedder/embedder.h" |
| 14 #include "mojo/edk/embedder/process_delegate.h" | 14 #include "mojo/edk/embedder/scoped_ipc_support.h" |
| 15 #include "mojo/public/cpp/system/message_pipe.h" | 15 #include "mojo/public/cpp/system/message_pipe.h" |
| 16 #include "services/service_manager/public/cpp/service_context.h" | 16 #include "services/service_manager/public/cpp/service_context.h" |
| 17 #include "services/service_manager/runner/common/client_util.h" | 17 #include "services/service_manager/runner/common/client_util.h" |
| 18 #include "services/service_manager/runner/common/switches.h" | 18 #include "services/service_manager/runner/common/switches.h" |
| 19 | 19 |
| 20 #if defined(OS_LINUX) | 20 #if defined(OS_LINUX) |
| 21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
| 22 #include "base/sys_info.h" | 22 #include "base/sys_info.h" |
| 23 #include "services/service_manager/public/cpp/standalone_service/linux_sandbox.h
" | 23 #include "services/service_manager/public/cpp/standalone_service/linux_sandbox.h
" |
| 24 #endif | 24 #endif |
| (...skipping 22 matching lines...) Expand all Loading... |
| 47 BrokerFilePermission::ReadWriteCreateUnlinkRecursive("/dev/shm/")); | 47 BrokerFilePermission::ReadWriteCreateUnlinkRecursive("/dev/shm/")); |
| 48 std::unique_ptr<LinuxSandbox> sandbox(new LinuxSandbox(permissions)); | 48 std::unique_ptr<LinuxSandbox> sandbox(new LinuxSandbox(permissions)); |
| 49 sandbox->Warmup(); | 49 sandbox->Warmup(); |
| 50 sandbox->EngageNamespaceSandbox(); | 50 sandbox->EngageNamespaceSandbox(); |
| 51 sandbox->EngageSeccompSandbox(); | 51 sandbox->EngageSeccompSandbox(); |
| 52 sandbox->Seal(); | 52 sandbox->Seal(); |
| 53 return sandbox; | 53 return sandbox; |
| 54 } | 54 } |
| 55 #endif | 55 #endif |
| 56 | 56 |
| 57 // Should be created and initialized on the main thread and kept alive as long | |
| 58 // a Service is running in the current process. | |
| 59 class ScopedAppContext : public mojo::edk::ProcessDelegate { | |
| 60 public: | |
| 61 ScopedAppContext() | |
| 62 : io_thread_("io_thread"), | |
| 63 wait_for_shutdown_event_( | |
| 64 base::WaitableEvent::ResetPolicy::MANUAL, | |
| 65 base::WaitableEvent::InitialState::NOT_SIGNALED) { | |
| 66 mojo::edk::Init(); | |
| 67 io_thread_.StartWithOptions( | |
| 68 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | |
| 69 mojo::edk::InitIPCSupport(this, io_thread_.task_runner()); | |
| 70 mojo::edk::SetParentPipeHandleFromCommandLine(); | |
| 71 } | |
| 72 | |
| 73 ~ScopedAppContext() override { | |
| 74 mojo::edk::ShutdownIPCSupport(); | |
| 75 wait_for_shutdown_event_.Wait(); | |
| 76 } | |
| 77 | |
| 78 private: | |
| 79 // ProcessDelegate implementation. | |
| 80 void OnShutdownComplete() override { | |
| 81 wait_for_shutdown_event_.Signal(); | |
| 82 } | |
| 83 | |
| 84 base::Thread io_thread_; | |
| 85 | |
| 86 // Used to unblock the main thread on shutdown. | |
| 87 base::WaitableEvent wait_for_shutdown_event_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(ScopedAppContext); | |
| 90 }; | |
| 91 | |
| 92 } // namespace | 57 } // namespace |
| 93 | 58 |
| 94 void RunStandaloneService(const StandaloneServiceCallback& callback) { | 59 void RunStandaloneService(const StandaloneServiceCallback& callback) { |
| 95 DCHECK(!base::MessageLoop::current()); | 60 DCHECK(!base::MessageLoop::current()); |
| 96 | 61 |
| 97 #if defined(OS_MACOSX) | 62 #if defined(OS_MACOSX) |
| 98 // Send our task port to the parent. | 63 // Send our task port to the parent. |
| 99 MachBroker::SendTaskPortToParent(); | 64 MachBroker::SendTaskPortToParent(); |
| 100 #endif | 65 #endif |
| 101 | 66 |
| 102 #if !defined(OFFICIAL_BUILD) | 67 #if !defined(OFFICIAL_BUILD) |
| 103 // Initialize stack dumping just before initializing sandbox to make | 68 // Initialize stack dumping just before initializing sandbox to make |
| 104 // sure symbol names in all loaded libraries will be cached. | 69 // sure symbol names in all loaded libraries will be cached. |
| 105 base::debug::EnableInProcessStackDumping(); | 70 base::debug::EnableInProcessStackDumping(); |
| 106 #endif | 71 #endif |
| 107 #if defined(OS_LINUX) | 72 #if defined(OS_LINUX) |
| 108 std::unique_ptr<LinuxSandbox> sandbox; | 73 std::unique_ptr<LinuxSandbox> sandbox; |
| 109 const base::CommandLine& command_line = | 74 const base::CommandLine& command_line = |
| 110 *base::CommandLine::ForCurrentProcess(); | 75 *base::CommandLine::ForCurrentProcess(); |
| 111 if (command_line.HasSwitch(switches::kEnableSandbox)) | 76 if (command_line.HasSwitch(switches::kEnableSandbox)) |
| 112 sandbox = InitializeSandbox(); | 77 sandbox = InitializeSandbox(); |
| 113 #endif | 78 #endif |
| 114 | 79 |
| 115 ScopedAppContext app_context; | 80 mojo::edk::Init(); |
| 81 |
| 82 base::Thread io_thread("io_thread"); |
| 83 io_thread.StartWithOptions( |
| 84 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 85 |
| 86 mojo::edk::ScopedIPCSupport ipc_support( |
| 87 io_thread.task_runner(), |
| 88 mojo::edk::ScopedIPCSupport::ShutdownPolicy::CLEAN); |
| 89 mojo::edk::SetParentPipeHandleFromCommandLine(); |
| 90 |
| 116 callback.Run(GetServiceRequestFromCommandLine()); | 91 callback.Run(GetServiceRequestFromCommandLine()); |
| 117 } | 92 } |
| 118 | 93 |
| 119 } // namespace service_manager | 94 } // namespace service_manager |
| OLD | NEW |