| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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> |
| 11 #include <sys/time.h> | 11 #include <sys/time.h> |
| 12 #include <sys/types.h> | 12 #include <sys/types.h> |
| 13 #include <sys/wait.h> | 13 #include <sys/wait.h> |
| 14 #include <unistd.h> | 14 #include <unistd.h> |
| 15 | 15 |
| 16 #include <limits> | 16 #include <limits> |
| 17 #include <set> | 17 #include <set> |
| 18 | 18 |
| 19 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
| 20 #include "base/debug_util.h" | 20 #include "base/debug_util.h" |
| 21 #include "base/dir_reader_posix.h" | 21 #include "base/dir_reader_posix.h" |
| 22 #include "base/eintr_wrapper.h" | 22 #include "base/eintr_wrapper.h" |
| 23 #include "base/logging.h" | 23 #include "base/logging.h" |
| 24 #include "base/platform_thread.h" | 24 #include "base/platform_thread.h" |
| 25 #include "base/process_util.h" | 25 #include "base/process_util.h" |
| 26 #include "base/rand_util.h" | 26 #include "base/rand_util.h" |
| 27 #include "base/scoped_ptr.h" | 27 #include "base/scoped_ptr.h" |
| 28 #include "base/sys_info.h" | |
| 29 #include "base/time.h" | 28 #include "base/time.h" |
| 30 #include "base/waitable_event.h" | 29 #include "base/waitable_event.h" |
| 31 | 30 |
| 32 #if defined(OS_MACOSX) | 31 #if defined(OS_MACOSX) |
| 33 #include <crt_externs.h> | 32 #include <crt_externs.h> |
| 34 #define environ (*_NSGetEnviron()) | 33 #define environ (*_NSGetEnviron()) |
| 35 #include "base/mach_ipc_mac.h" | 34 #include "base/mach_ipc_mac.h" |
| 36 #else | 35 #else |
| 37 extern char** environ; | 36 extern char** environ; |
| 38 #endif | 37 #endif |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 return LaunchApp(argv, no_env, fds_to_remap, wait, process_handle); | 569 return LaunchApp(argv, no_env, fds_to_remap, wait, process_handle); |
| 571 } | 570 } |
| 572 | 571 |
| 573 bool LaunchApp(const CommandLine& cl, | 572 bool LaunchApp(const CommandLine& cl, |
| 574 bool wait, bool start_hidden, | 573 bool wait, bool start_hidden, |
| 575 ProcessHandle* process_handle) { | 574 ProcessHandle* process_handle) { |
| 576 file_handle_mapping_vector no_files; | 575 file_handle_mapping_vector no_files; |
| 577 return LaunchApp(cl.argv(), no_files, wait, process_handle); | 576 return LaunchApp(cl.argv(), no_files, wait, process_handle); |
| 578 } | 577 } |
| 579 | 578 |
| 580 #if !defined(OS_MACOSX) | |
| 581 ProcessMetrics::ProcessMetrics(ProcessHandle process) | |
| 582 #else | |
| 583 ProcessMetrics::ProcessMetrics(ProcessHandle process, | |
| 584 ProcessMetrics::PortProvider* port_provider) | |
| 585 #endif | |
| 586 : process_(process), | |
| 587 last_time_(0), | |
| 588 last_system_time_(0) | |
| 589 #if defined(OS_MACOSX) | |
| 590 , port_provider_(port_provider) | |
| 591 #elif defined(OS_POSIX) | |
| 592 , last_cpu_(0) | |
| 593 #endif | |
| 594 { | |
| 595 processor_count_ = base::SysInfo::NumberOfProcessors(); | |
| 596 } | |
| 597 | |
| 598 // static | |
| 599 #if !defined(OS_MACOSX) | |
| 600 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { | |
| 601 return new ProcessMetrics(process); | |
| 602 } | |
| 603 #else | |
| 604 ProcessMetrics* ProcessMetrics::CreateProcessMetrics( | |
| 605 ProcessHandle process, | |
| 606 ProcessMetrics::PortProvider* port_provider) { | |
| 607 return new ProcessMetrics(process, port_provider); | |
| 608 } | |
| 609 #endif | |
| 610 | |
| 611 ProcessMetrics::~ProcessMetrics() { } | 579 ProcessMetrics::~ProcessMetrics() { } |
| 612 | 580 |
| 613 void EnableTerminationOnHeapCorruption() { | 581 void EnableTerminationOnHeapCorruption() { |
| 614 // On POSIX, there nothing to do AFAIK. | 582 // On POSIX, there nothing to do AFAIK. |
| 615 } | 583 } |
| 616 | 584 |
| 617 bool EnableInProcessStackDumping() { | 585 bool EnableInProcessStackDumping() { |
| 618 // When running in an application, our code typically expects SIGPIPE | 586 // When running in an application, our code typically expects SIGPIPE |
| 619 // to be ignored. Therefore, when testing that same code, it should run | 587 // to be ignored. Therefore, when testing that same code, it should run |
| 620 // with SIGPIPE ignored as well. | 588 // with SIGPIPE ignored as well. |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 870 | 838 |
| 871 // TODO(viettrungluu): Conceivably, we should have a timeout as well, so we | 839 // TODO(viettrungluu): Conceivably, we should have a timeout as well, so we |
| 872 // don't hang if what we're calling hangs. | 840 // don't hang if what we're calling hangs. |
| 873 bool GetAppOutputRestricted(const CommandLine& cl, | 841 bool GetAppOutputRestricted(const CommandLine& cl, |
| 874 std::string* output, size_t max_output) { | 842 std::string* output, size_t max_output) { |
| 875 // Run |execve()| with the empty environment. | 843 // Run |execve()| with the empty environment. |
| 876 char* const empty_environ = NULL; | 844 char* const empty_environ = NULL; |
| 877 return GetAppOutputInternal(cl, &empty_environ, output, max_output, false); | 845 return GetAppOutputInternal(cl, &empty_environ, output, max_output, false); |
| 878 } | 846 } |
| 879 | 847 |
| 880 int GetProcessCount(const std::wstring& executable_name, | |
| 881 const ProcessFilter* filter) { | |
| 882 int count = 0; | |
| 883 | |
| 884 NamedProcessIterator iter(executable_name, filter); | |
| 885 while (iter.NextProcessEntry()) | |
| 886 ++count; | |
| 887 return count; | |
| 888 } | |
| 889 | |
| 890 bool KillProcesses(const std::wstring& executable_name, int exit_code, | |
| 891 const ProcessFilter* filter) { | |
| 892 bool result = true; | |
| 893 const ProcessEntry* entry; | |
| 894 | |
| 895 NamedProcessIterator iter(executable_name, filter); | |
| 896 while ((entry = iter.NextProcessEntry()) != NULL) | |
| 897 result = KillProcess((*entry).pid, exit_code, true) && result; | |
| 898 | |
| 899 return result; | |
| 900 } | |
| 901 | |
| 902 bool WaitForProcessesToExit(const std::wstring& executable_name, | 848 bool WaitForProcessesToExit(const std::wstring& executable_name, |
| 903 int64 wait_milliseconds, | 849 int64 wait_milliseconds, |
| 904 const ProcessFilter* filter) { | 850 const ProcessFilter* filter) { |
| 905 bool result = false; | 851 bool result = false; |
| 906 | 852 |
| 907 // TODO(port): This is inefficient, but works if there are multiple procs. | 853 // TODO(port): This is inefficient, but works if there are multiple procs. |
| 908 // TODO(port): use waitpid to avoid leaving zombies around | 854 // TODO(port): use waitpid to avoid leaving zombies around |
| 909 | 855 |
| 910 base::Time end_time = base::Time::Now() + | 856 base::Time end_time = base::Time::Now() + |
| 911 base::TimeDelta::FromMilliseconds(wait_milliseconds); | 857 base::TimeDelta::FromMilliseconds(wait_milliseconds); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 927 const ProcessFilter* filter) { | 873 const ProcessFilter* filter) { |
| 928 bool exited_cleanly = | 874 bool exited_cleanly = |
| 929 WaitForProcessesToExit(executable_name, wait_milliseconds, | 875 WaitForProcessesToExit(executable_name, wait_milliseconds, |
| 930 filter); | 876 filter); |
| 931 if (!exited_cleanly) | 877 if (!exited_cleanly) |
| 932 KillProcesses(executable_name, exit_code, filter); | 878 KillProcesses(executable_name, exit_code, filter); |
| 933 return exited_cleanly; | 879 return exited_cleanly; |
| 934 } | 880 } |
| 935 | 881 |
| 936 } // namespace base | 882 } // namespace base |
| OLD | NEW |