summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
Diffstat (limited to 'base')
-rw-r--r--base/process_util.h4
-rw-r--r--base/process_util_posix.cc2
-rw-r--r--base/process_util_unittest.cc3
-rw-r--r--base/process_util_win.cc35
-rw-r--r--base/stats_table_unittest.cc1
5 files changed, 26 insertions, 19 deletions
diff --git a/base/process_util.h b/base/process_util.h
index c921d7a..029f307 100644
--- a/base/process_util.h
+++ b/base/process_util.h
@@ -151,9 +151,9 @@ bool KillProcesses(const std::wstring& executable_name, int exit_code,
// entry structure, giving it the specified exit code. If |wait| is true, wait
// for the process to be actually terminated before returning.
// Returns true if this is successful, false otherwise.
-bool KillProcess(int process_id, int exit_code, bool wait);
+bool KillProcess(ProcessHandle process, int exit_code, bool wait);
#if defined(OS_WIN)
-bool KillProcess(HANDLE process, int exit_code, bool wait);
+bool KillProcessById(DWORD process_id, int exit_code, bool wait);
#endif
// Get the termination status (exit code) of the process and return true if the
diff --git a/base/process_util_posix.cc b/base/process_util_posix.cc
index 7cabb3a..e8845fb 100644
--- a/base/process_util_posix.cc
+++ b/base/process_util_posix.cc
@@ -52,7 +52,7 @@ int GetProcId(ProcessHandle process) {
// Attempts to kill the process identified by the given process
// entry structure. Ignores specified exit_code; posix can't force that.
// Returns true if this is successful, false otherwise.
-bool KillProcess(int process_id, int exit_code, bool wait) {
+bool KillProcess(ProcessHandle process_id, int exit_code, bool wait) {
bool result = false;
int status = kill(process_id, SIGTERM);
diff --git a/base/process_util_unittest.cc b/base/process_util_unittest.cc
index 09922cc..97ba756 100644
--- a/base/process_util_unittest.cc
+++ b/base/process_util_unittest.cc
@@ -34,6 +34,7 @@ TEST_F(ProcessUtilTest, SpawnChild) {
ASSERT_NE(static_cast<ProcessHandle>(NULL), handle);
EXPECT_TRUE(WaitForSingleProcess(handle, 5000));
+ base::CloseProcessHandle(handle);
}
MULTIPROCESS_TEST_MAIN(SlowChildProcess) {
@@ -65,6 +66,7 @@ TEST_F(ProcessUtilTest, KillSlowChild) {
fclose(fp);
EXPECT_TRUE(base::WaitForSingleProcess(handle, 5000));
EXPECT_EQ(oldcount, GetProcessCount(L"base_unittests" EXE_SUFFIX, 0));
+ base::CloseProcessHandle(handle);
}
// TODO(estade): if possible, port these 2 tests.
@@ -216,6 +218,7 @@ TEST_F(ProcessUtilTest, FDRemapping) {
ASSERT_EQ(0, num_open_files);
EXPECT_TRUE(WaitForSingleProcess(handle, 1000));
+ base::CloseProcessHandle(handle);
close(fds[0]);
close(sockets[0]);
close(sockets[1]);
diff --git a/base/process_util_win.cc b/base/process_util_win.cc
index bb0e626..2edf5ec 100644
--- a/base/process_util_win.cc
+++ b/base/process_util_win.cc
@@ -160,17 +160,20 @@ bool LaunchApp(const CommandLine& cl,
// Attempts to kill the process identified by the given process
// entry structure, giving it the specified exit code.
// Returns true if this is successful, false otherwise.
-bool KillProcess(int process_id, int exit_code, bool wait) {
+bool KillProcessById(DWORD process_id, int exit_code, bool wait) {
HANDLE process = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE,
FALSE, // Don't inherit handle
process_id);
- if (process)
- return KillProcess(process, exit_code, wait);
- return false;
+ if (!process)
+ return false;
+
+ bool ret = KillProcess(process, exit_code, wait);
+ CloseHandle(process);
+ return ret;
}
-bool KillProcess(HANDLE process, int exit_code, bool wait) {
- bool result = !!TerminateProcess(process, exit_code);
+bool KillProcess(ProcessHandle process, int exit_code, bool wait) {
+ bool result = (TerminateProcess(process, exit_code) != FALSE);
if (result && wait) {
// The process may not end immediately due to pending I/O
if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000))
@@ -178,7 +181,6 @@ bool KillProcess(HANDLE process, int exit_code, bool wait) {
} else {
DLOG(ERROR) << "Unable to terminate process: " << GetLastError();
}
- CloseHandle(process);
return result;
}
@@ -252,12 +254,12 @@ bool WaitForExitCode(ProcessHandle handle, int* exit_code) {
}
NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name,
- const ProcessFilter* filter) :
- started_iteration_(false),
- executable_name_(executable_name),
- filter_(filter) {
- snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- }
+ const ProcessFilter* filter)
+ : started_iteration_(false),
+ executable_name_(executable_name),
+ filter_(filter) {
+ snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+}
NamedProcessIterator::~NamedProcessIterator() {
CloseHandle(snapshot_);
@@ -315,8 +317,10 @@ bool KillProcesses(const std::wstring& executable_name, int exit_code,
const ProcessEntry* entry;
NamedProcessIterator iter(executable_name, filter);
- while (entry = iter.NextProcessEntry())
- result = KillProcess((*entry).th32ProcessID, exit_code, true) && result;
+ while (entry = iter.NextProcessEntry()) {
+ if (!KillProcessById((*entry).th32ProcessID, exit_code, true))
+ result = false;
+ }
return result;
}
@@ -346,7 +350,6 @@ bool WaitForProcessesToExit(const std::wstring& executable_name,
bool WaitForSingleProcess(ProcessHandle handle, int wait_milliseconds) {
bool retval = WaitForSingleObject(handle, wait_milliseconds) == WAIT_OBJECT_0;
- CloseHandle(handle);
return retval;
}
diff --git a/base/stats_table_unittest.cc b/base/stats_table_unittest.cc
index 7c4c4f6..5d1c841 100644
--- a/base/stats_table_unittest.cc
+++ b/base/stats_table_unittest.cc
@@ -210,6 +210,7 @@ TEST_F(StatsTableTest, MultipleProcesses) {
// Wait for the processes to finish.
for (int index = 0; index < kMaxProcs; index++) {
EXPECT_TRUE(WaitForSingleProcess(procs[index], 60 * 1000));
+ base::CloseProcessHandle(procs[index]);
}
StatsCounter zero_counter(kCounterZero);