OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <dirent.h> | 5 #include <dirent.h> |
6 #include <errno.h> | 6 #include <errno.h> |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <signal.h> | 8 #include <signal.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <sys/resource.h> | 10 #include <sys/resource.h> |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 RAW_LOG(FATAL, "Cound not fix sa_restorer."); | 155 RAW_LOG(FATAL, "Cound not fix sa_restorer."); |
156 } | 156 } |
157 #endif // !defined(NDEBUG) | 157 #endif // !defined(NDEBUG) |
158 } | 158 } |
159 } | 159 } |
160 #endif // !defined(OS_LINUX) || | 160 #endif // !defined(OS_LINUX) || |
161 // (!defined(__i386__) && !defined(__x86_64__) && !defined(__arm__)) | 161 // (!defined(__i386__) && !defined(__x86_64__) && !defined(__arm__)) |
162 | 162 |
163 } // anonymous namespace | 163 } // anonymous namespace |
164 | 164 |
165 ProcessId GetCurrentProcId() { | |
166 return getpid(); | |
167 } | |
168 | |
169 ProcessHandle GetCurrentProcessHandle() { | |
170 return GetCurrentProcId(); | |
171 } | |
172 | |
173 bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle) { | |
174 // On Posix platforms, process handles are the same as PIDs, so we | |
175 // don't need to do anything. | |
176 *handle = pid; | |
177 return true; | |
178 } | |
179 | |
180 bool OpenPrivilegedProcessHandle(ProcessId pid, ProcessHandle* handle) { | |
181 // On POSIX permissions are checked for each operation on process, | |
182 // not when opening a "handle". | |
183 return OpenProcessHandle(pid, handle); | |
184 } | |
185 | |
186 bool OpenProcessHandleWithAccess(ProcessId pid, | |
187 uint32 access_flags, | |
188 ProcessHandle* handle) { | |
189 // On POSIX permissions are checked for each operation on process, | |
190 // not when opening a "handle". | |
191 return OpenProcessHandle(pid, handle); | |
192 } | |
193 | |
194 void CloseProcessHandle(ProcessHandle process) { | |
195 // See OpenProcessHandle, nothing to do. | |
196 return; | |
197 } | |
198 | |
199 ProcessId GetProcId(ProcessHandle process) { | |
200 return process; | |
201 } | |
202 | |
203 // A class to handle auto-closing of DIR*'s. | 165 // A class to handle auto-closing of DIR*'s. |
204 class ScopedDIRClose { | 166 class ScopedDIRClose { |
205 public: | 167 public: |
206 inline void operator()(DIR* x) const { | 168 inline void operator()(DIR* x) const { |
207 if (x) { | 169 if (x) { |
208 closedir(x); | 170 closedir(x); |
209 } | 171 } |
210 } | 172 } |
211 }; | 173 }; |
212 typedef scoped_ptr_malloc<DIR, ScopedDIRClose> ScopedDIR; | 174 typedef scoped_ptr_malloc<DIR, ScopedDIRClose> ScopedDIR; |
213 | 175 |
214 #if defined(OS_LINUX) | 176 #if defined(OS_LINUX) |
215 static const rlim_t kSystemDefaultMaxFds = 8192; | 177 static const char kFDDir[] = "/proc/self/fd"; |
216 static const char kFDDir[] = "/proc/self/fd"; | |
217 #elif defined(OS_MACOSX) | 178 #elif defined(OS_MACOSX) |
218 static const rlim_t kSystemDefaultMaxFds = 256; | 179 static const char kFDDir[] = "/dev/fd"; |
219 static const char kFDDir[] = "/dev/fd"; | |
220 #elif defined(OS_SOLARIS) | 180 #elif defined(OS_SOLARIS) |
221 static const rlim_t kSystemDefaultMaxFds = 8192; | 181 static const char kFDDir[] = "/dev/fd"; |
222 static const char kFDDir[] = "/dev/fd"; | |
223 #elif defined(OS_FREEBSD) | 182 #elif defined(OS_FREEBSD) |
224 static const rlim_t kSystemDefaultMaxFds = 8192; | 183 static const char kFDDir[] = "/dev/fd"; |
225 static const char kFDDir[] = "/dev/fd"; | |
226 #elif defined(OS_OPENBSD) | 184 #elif defined(OS_OPENBSD) |
227 static const rlim_t kSystemDefaultMaxFds = 256; | 185 static const char kFDDir[] = "/dev/fd"; |
228 static const char kFDDir[] = "/dev/fd"; | |
229 #elif defined(OS_ANDROID) | 186 #elif defined(OS_ANDROID) |
230 static const rlim_t kSystemDefaultMaxFds = 1024; | 187 static const char kFDDir[] = "/proc/self/fd"; |
231 static const char kFDDir[] = "/proc/self/fd"; | |
232 #endif | 188 #endif |
233 | 189 |
234 size_t GetMaxFds() { | |
235 rlim_t max_fds; | |
236 struct rlimit nofile; | |
237 if (getrlimit(RLIMIT_NOFILE, &nofile)) { | |
238 // getrlimit failed. Take a best guess. | |
239 max_fds = kSystemDefaultMaxFds; | |
240 RAW_LOG(ERROR, "getrlimit(RLIMIT_NOFILE) failed"); | |
241 } else { | |
242 max_fds = nofile.rlim_cur; | |
243 } | |
244 | |
245 if (max_fds > INT_MAX) | |
246 max_fds = INT_MAX; | |
247 | |
248 return static_cast<size_t>(max_fds); | |
249 } | |
250 | |
251 void CloseSuperfluousFds(const base::InjectiveMultimap& saved_mapping) { | 190 void CloseSuperfluousFds(const base::InjectiveMultimap& saved_mapping) { |
252 // DANGER: no calls to malloc are allowed from now on: | 191 // DANGER: no calls to malloc are allowed from now on: |
253 // http://crbug.com/36678 | 192 // http://crbug.com/36678 |
254 | 193 |
255 // Get the maximum number of FDs possible. | 194 // Get the maximum number of FDs possible. |
256 size_t max_fds = GetMaxFds(); | 195 size_t max_fds = GetMaxFds(); |
257 | 196 |
258 DirReaderPosix fd_dir(kFDDir); | 197 DirReaderPosix fd_dir(kFDDir); |
259 if (!fd_dir.IsValid()) { | 198 if (!fd_dir.IsValid()) { |
260 // Fallback case: Try every possible fd. | 199 // Fallback case: Try every possible fd. |
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
778 std::string* output, | 717 std::string* output, |
779 int* exit_code) { | 718 int* exit_code) { |
780 // Run |execve()| with the current environment and store "unlimited" data. | 719 // Run |execve()| with the current environment and store "unlimited" data. |
781 GetAppOutputInternalResult result = GetAppOutputInternal( | 720 GetAppOutputInternalResult result = GetAppOutputInternal( |
782 cl.argv(), NULL, output, std::numeric_limits<std::size_t>::max(), true, | 721 cl.argv(), NULL, output, std::numeric_limits<std::size_t>::max(), true, |
783 exit_code); | 722 exit_code); |
784 return result == EXECUTE_SUCCESS; | 723 return result == EXECUTE_SUCCESS; |
785 } | 724 } |
786 | 725 |
787 } // namespace base | 726 } // namespace base |
OLD | NEW |