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

Side by Side Diff: services/service_manager/embedder/main.cc

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

Powered by Google App Engine
This is Rietveld 408576698