OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "services/service_manager/embedder/main.h" |
| 6 #include "base/allocator/features.h" |
| 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/process/memory.h" |
| 11 #include "mojo/edk/embedder/embedder.h" |
| 12 #include "services/service_manager/embedder/main_delegate.h" |
| 13 #include "services/service_manager/embedder/set_process_title.h" |
| 14 #include "services/service_manager/embedder/shared_file_util.h" |
| 15 #include "services/service_manager/embedder/switches.h" |
| 16 |
| 17 #if defined(OS_WIN) |
| 18 #include "base/win/process_startup_helper.h" |
| 19 #include "ui/base/win/atl_module.h" |
| 20 #endif |
| 21 |
| 22 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 23 #include <locale.h> |
| 24 #include <signal.h> |
| 25 |
| 26 #include "base/file_descriptor_store.h" |
| 27 #include "base/posix/global_descriptors.h" |
| 28 #endif |
| 29 |
| 30 #if defined(OS_MACOSX) |
| 31 #include "base/mac/scoped_nsautorelease_pool.h" |
| 32 #include "services/service_manager/embedder/mac_init.h" |
| 33 |
| 34 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 35 #include "base/allocator/allocator_shim.h" |
| 36 #endif |
| 37 #endif // defined(OS_MACOSX) |
| 38 |
| 39 namespace service_manager { |
| 40 |
| 41 namespace { |
| 42 |
| 43 // Maximum message size allowed to be read from a Mojo message pipe in any |
| 44 // service manager embedder process. |
| 45 constexpr size_t kMaximumMojoMessageSize = 128 * 1024 * 1024; |
| 46 |
| 47 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 48 |
| 49 // Setup signal-handling state: resanitize most signals, ignore SIGPIPE. |
| 50 void SetupSignalHandlers() { |
| 51 // Sanitise our signal handling state. Signals that were ignored by our |
| 52 // parent will also be ignored by us. We also inherit our parent's sigmask. |
| 53 sigset_t empty_signal_set; |
| 54 CHECK_EQ(0, sigemptyset(&empty_signal_set)); |
| 55 CHECK_EQ(0, sigprocmask(SIG_SETMASK, &empty_signal_set, NULL)); |
| 56 |
| 57 struct sigaction sigact; |
| 58 memset(&sigact, 0, sizeof(sigact)); |
| 59 sigact.sa_handler = SIG_DFL; |
| 60 static const int signals_to_reset[] = { |
| 61 SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, SIGSEGV, |
| 62 SIGALRM, SIGTERM, SIGCHLD, SIGBUS, SIGTRAP}; // SIGPIPE is set below. |
| 63 for (unsigned i = 0; i < arraysize(signals_to_reset); i++) { |
| 64 CHECK_EQ(0, sigaction(signals_to_reset[i], &sigact, NULL)); |
| 65 } |
| 66 |
| 67 // Always ignore SIGPIPE. We check the return value of write(). |
| 68 CHECK_NE(SIG_ERR, signal(SIGPIPE, SIG_IGN)); |
| 69 } |
| 70 |
| 71 void PopulateFDsFromCommandLine() { |
| 72 const std::string& shared_file_param = |
| 73 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 74 switches::kSharedFiles); |
| 75 if (shared_file_param.empty()) |
| 76 return; |
| 77 |
| 78 base::Optional<std::map<int, std::string>> shared_file_descriptors = |
| 79 service_manager::ParseSharedFileSwitchValue(shared_file_param); |
| 80 if (!shared_file_descriptors) |
| 81 return; |
| 82 |
| 83 for (const auto& descriptor : *shared_file_descriptors) { |
| 84 base::MemoryMappedFile::Region region; |
| 85 const std::string& key = descriptor.second; |
| 86 base::ScopedFD fd = base::GlobalDescriptors::GetInstance()->TakeFD( |
| 87 descriptor.first, ®ion); |
| 88 base::FileDescriptorStore::GetInstance().Set(key, std::move(fd), region); |
| 89 } |
| 90 } |
| 91 |
| 92 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
| 93 |
| 94 } // namespace |
| 95 |
| 96 MainParams::MainParams(MainDelegate* delegate) : delegate(delegate) {} |
| 97 |
| 98 MainParams::~MainParams() {} |
| 99 |
| 100 int Main(const MainParams& params) { |
| 101 MainDelegate* delegate = params.delegate; |
| 102 DCHECK(delegate); |
| 103 |
| 104 #if defined(OS_MACOSX) && BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 105 base::allocator::InitializeAllocatorShim(); |
| 106 #endif |
| 107 base::EnableTerminationOnOutOfMemory(); |
| 108 |
| 109 #if defined(OS_WIN) |
| 110 base::win::RegisterInvalidParamHandler(); |
| 111 ui::win::CreateATLModuleIfNeeded(); |
| 112 #endif // defined(OS_WIN) |
| 113 |
| 114 // On Android setlocale() is not supported, and we don't override the signal |
| 115 // handlers so we can get a stack trace when crashing. |
| 116 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 117 // Set C library locale to make sure CommandLine can parse argument values in |
| 118 // the correct encoding. |
| 119 setlocale(LC_ALL, ""); |
| 120 |
| 121 SetupSignalHandlers(); |
| 122 #endif |
| 123 |
| 124 #if !defined(OS_ANDROID) |
| 125 // On Android, the command line is initialized when library is loaded. |
| 126 int argc = 0; |
| 127 const char** argv = nullptr; |
| 128 |
| 129 #if defined(OS_POSIX) |
| 130 // argc/argv are ignored on Windows; see command_line.h for details. |
| 131 argc = params.argc; |
| 132 argv = params.argv; |
| 133 #endif |
| 134 |
| 135 base::CommandLine::Init(argc, argv); |
| 136 |
| 137 #if defined(OS_POSIX) |
| 138 PopulateFDsFromCommandLine(); |
| 139 #endif |
| 140 |
| 141 base::EnableTerminationOnHeapCorruption(); |
| 142 |
| 143 #if defined(OS_WIN) |
| 144 base::win::SetupCRT(*base::CommandLine::ForCurrentProcess()); |
| 145 #endif |
| 146 |
| 147 SetProcessTitleFromCommandLine(argv); |
| 148 #endif // !defined(OS_ANDROID) |
| 149 |
| 150 MainDelegate::InitializeParams init_params; |
| 151 |
| 152 #if defined(OS_MACOSX) |
| 153 // We need this pool for all the objects created before we get to the event |
| 154 // loop, but we don't want to leave them hanging around until the app quits. |
| 155 // Each "main" needs to flush this pool right before it goes into its main |
| 156 // event loop to get rid of the cruft. |
| 157 std::unique_ptr<base::mac::ScopedNSAutoreleasePool> autorelease_pool = |
| 158 base::MakeUnique<base::mac::ScopedNSAutoreleasePool>(); |
| 159 init_params.autorelease_pool = autorelease_pool.get(); |
| 160 InitializeMac(); |
| 161 #endif |
| 162 |
| 163 mojo::edk::SetMaxMessageSize(kMaximumMojoMessageSize); |
| 164 mojo::edk::Init(); |
| 165 |
| 166 int exit_code = 0; |
| 167 if (!delegate->Initialize(init_params, &exit_code)) |
| 168 return exit_code; |
| 169 |
| 170 exit_code = delegate->Run(); |
| 171 delegate->ShutDown(); |
| 172 |
| 173 return exit_code; |
| 174 } |
| 175 |
| 176 } // namespace service_manager |
OLD | NEW |