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 "base/process/launch.h" | 5 #include "base/process/launch.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <signal.h> | 10 #include <signal.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
12 #include <sys/resource.h> | 12 #include <sys/resource.h> |
13 #include <sys/time.h> | 13 #include <sys/time.h> |
14 #include <sys/types.h> | 14 #include <sys/types.h> |
15 #include <sys/wait.h> | 15 #include <sys/wait.h> |
16 #include <unistd.h> | 16 #include <unistd.h> |
17 | 17 |
18 #include <iterator> | 18 #include <iterator> |
19 #include <limits> | 19 #include <limits> |
20 #include <set> | 20 #include <set> |
21 | 21 |
22 #include "base/allocator/type_profiler_control.h" | 22 #include "base/allocator/type_profiler_control.h" |
23 #include "base/command_line.h" | 23 #include "base/command_line.h" |
24 #include "base/compiler_specific.h" | 24 #include "base/compiler_specific.h" |
25 #include "base/debug/debugger.h" | 25 #include "base/debug/debugger.h" |
26 #include "base/debug/stack_trace.h" | 26 #include "base/debug/stack_trace.h" |
27 #include "base/file_util.h" | 27 #include "base/file_util.h" |
28 #include "base/files/dir_reader_posix.h" | 28 #include "base/files/dir_reader_posix.h" |
29 #include "base/files/scoped_file.h" | |
30 #include "base/logging.h" | 29 #include "base/logging.h" |
31 #include "base/memory/scoped_ptr.h" | 30 #include "base/memory/scoped_ptr.h" |
32 #include "base/posix/eintr_wrapper.h" | 31 #include "base/posix/eintr_wrapper.h" |
33 #include "base/process/kill.h" | 32 #include "base/process/kill.h" |
34 #include "base/process/process_metrics.h" | 33 #include "base/process/process_metrics.h" |
35 #include "base/strings/stringprintf.h" | 34 #include "base/strings/stringprintf.h" |
36 #include "base/synchronization/waitable_event.h" | 35 #include "base/synchronization/waitable_event.h" |
37 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 36 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
38 #include "base/threading/platform_thread.h" | 37 #include "base/threading/platform_thread.h" |
39 #include "base/threading/thread_restrictions.h" | 38 #include "base/threading/thread_restrictions.h" |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 | 325 |
327 // DANGER: fork() rule: in the child, if you don't end up doing exec*(), | 326 // DANGER: fork() rule: in the child, if you don't end up doing exec*(), |
328 // you call _exit() instead of exit(). This is because _exit() does not | 327 // you call _exit() instead of exit(). This is because _exit() does not |
329 // call any previously-registered (in the parent) exit handlers, which | 328 // call any previously-registered (in the parent) exit handlers, which |
330 // might do things like block waiting for threads that don't even exist | 329 // might do things like block waiting for threads that don't even exist |
331 // in the child. | 330 // in the child. |
332 | 331 |
333 // If a child process uses the readline library, the process block forever. | 332 // If a child process uses the readline library, the process block forever. |
334 // In BSD like OSes including OS X it is safe to assign /dev/null as stdin. | 333 // In BSD like OSes including OS X it is safe to assign /dev/null as stdin. |
335 // See http://crbug.com/56596. | 334 // See http://crbug.com/56596. |
336 base::ScopedFD null_fd(HANDLE_EINTR(open("/dev/null", O_RDONLY))); | 335 int null_fd = HANDLE_EINTR(open("/dev/null", O_RDONLY)); |
337 if (!null_fd.is_valid()) { | 336 if (null_fd < 0) { |
338 RAW_LOG(ERROR, "Failed to open /dev/null"); | 337 RAW_LOG(ERROR, "Failed to open /dev/null"); |
339 _exit(127); | 338 _exit(127); |
340 } | 339 } |
341 | 340 |
342 int new_fd = HANDLE_EINTR(dup2(null_fd.get(), STDIN_FILENO)); | 341 file_util::ScopedFD null_fd_closer(&null_fd); |
| 342 int new_fd = HANDLE_EINTR(dup2(null_fd, STDIN_FILENO)); |
343 if (new_fd != STDIN_FILENO) { | 343 if (new_fd != STDIN_FILENO) { |
344 RAW_LOG(ERROR, "Failed to dup /dev/null for stdin"); | 344 RAW_LOG(ERROR, "Failed to dup /dev/null for stdin"); |
345 _exit(127); | 345 _exit(127); |
346 } | 346 } |
347 | 347 |
348 if (options.new_process_group) { | 348 if (options.new_process_group) { |
349 // Instead of inheriting the process group ID of the parent, the child | 349 // Instead of inheriting the process group ID of the parent, the child |
350 // starts off a new process group with pgid equal to its process ID. | 350 // starts off a new process group with pgid equal to its process ID. |
351 if (setpgid(0, 0) < 0) { | 351 if (setpgid(0, 0) < 0) { |
352 RAW_LOG(ERROR, "setpgid failed"); | 352 RAW_LOG(ERROR, "setpgid failed"); |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 std::string* output, | 631 std::string* output, |
632 int* exit_code) { | 632 int* exit_code) { |
633 // Run |execve()| with the current environment and store "unlimited" data. | 633 // Run |execve()| with the current environment and store "unlimited" data. |
634 GetAppOutputInternalResult result = GetAppOutputInternal( | 634 GetAppOutputInternalResult result = GetAppOutputInternal( |
635 cl.argv(), NULL, output, std::numeric_limits<std::size_t>::max(), true, | 635 cl.argv(), NULL, output, std::numeric_limits<std::size_t>::max(), true, |
636 exit_code); | 636 exit_code); |
637 return result == EXECUTE_SUCCESS; | 637 return result == EXECUTE_SUCCESS; |
638 } | 638 } |
639 | 639 |
640 } // namespace base | 640 } // namespace base |
OLD | NEW |