mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-15 07:14:46 +02:00
Don't call CloseHandle() twice on Windows (as it causes crashes, or unexpected behavior). This would happen with the following test case:
ProcessHandle handle = Process.launch(...); handle.kill(); Then as handle gets out of scope, ~ProcessHandle would call CloseHandle() on an already closed handle.
This commit is contained in:
@@ -237,7 +237,7 @@ public:
|
|||||||
/// Waits for the process specified by handle to terminate
|
/// Waits for the process specified by handle to terminate
|
||||||
/// and returns the exit code of the process.
|
/// and returns the exit code of the process.
|
||||||
|
|
||||||
static void kill(const ProcessHandle& handle);
|
static void kill(ProcessHandle& handle);
|
||||||
/// Kills the process specified by handle.
|
/// Kills the process specified by handle.
|
||||||
///
|
///
|
||||||
/// This is preferable on Windows where process IDs
|
/// This is preferable on Windows where process IDs
|
||||||
|
@@ -84,7 +84,7 @@ public:
|
|||||||
Pipe* outPipe,
|
Pipe* outPipe,
|
||||||
Pipe* errPipe,
|
Pipe* errPipe,
|
||||||
const EnvImpl& env);
|
const EnvImpl& env);
|
||||||
static void killImpl(const ProcessHandleImpl& handle);
|
static void killImpl(ProcessHandleImpl& handle);
|
||||||
static void killImpl(PIDImpl pid);
|
static void killImpl(PIDImpl pid);
|
||||||
static void requestTerminationImpl(PIDImpl pid);
|
static void requestTerminationImpl(PIDImpl pid);
|
||||||
|
|
||||||
|
@@ -85,7 +85,7 @@ public:
|
|||||||
Pipe* errPipe,
|
Pipe* errPipe,
|
||||||
const EnvImpl& env);
|
const EnvImpl& env);
|
||||||
static int waitImpl(PIDImpl pid);
|
static int waitImpl(PIDImpl pid);
|
||||||
static void killImpl(const ProcessHandleImpl& handle);
|
static void killImpl(ProcessHandleImpl& handle);
|
||||||
static void killImpl(PIDImpl pid);
|
static void killImpl(PIDImpl pid);
|
||||||
static void requestTerminationImpl(PIDImpl pid);
|
static void requestTerminationImpl(PIDImpl pid);
|
||||||
};
|
};
|
||||||
|
@@ -86,7 +86,7 @@ public:
|
|||||||
Pipe* outPipe,
|
Pipe* outPipe,
|
||||||
Pipe* errPipe,
|
Pipe* errPipe,
|
||||||
const EnvImpl& env);
|
const EnvImpl& env);
|
||||||
static void killImpl(const ProcessHandleImpl& handle);
|
static void killImpl(ProcessHandleImpl& handle);
|
||||||
static void killImpl(PIDImpl pid);
|
static void killImpl(PIDImpl pid);
|
||||||
static void requestTerminationImpl(PIDImpl pid);
|
static void requestTerminationImpl(PIDImpl pid);
|
||||||
};
|
};
|
||||||
|
@@ -62,6 +62,7 @@ public:
|
|||||||
UInt32 id() const;
|
UInt32 id() const;
|
||||||
HANDLE process() const;
|
HANDLE process() const;
|
||||||
int wait() const;
|
int wait() const;
|
||||||
|
void closeHandle();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HANDLE _hProcess;
|
HANDLE _hProcess;
|
||||||
@@ -89,7 +90,7 @@ public:
|
|||||||
Pipe* outPipe,
|
Pipe* outPipe,
|
||||||
Pipe* errPipe,
|
Pipe* errPipe,
|
||||||
const EnvImpl& env);
|
const EnvImpl& env);
|
||||||
static void killImpl(const ProcessHandleImpl& handle);
|
static void killImpl(ProcessHandleImpl& handle);
|
||||||
static void killImpl(PIDImpl pid);
|
static void killImpl(PIDImpl pid);
|
||||||
static void requestTerminationImpl(PIDImpl pid);
|
static void requestTerminationImpl(PIDImpl pid);
|
||||||
static std::string terminationEventName(PIDImpl pid);
|
static std::string terminationEventName(PIDImpl pid);
|
||||||
|
@@ -62,6 +62,7 @@ public:
|
|||||||
UInt32 id() const;
|
UInt32 id() const;
|
||||||
HANDLE process() const;
|
HANDLE process() const;
|
||||||
int wait() const;
|
int wait() const;
|
||||||
|
void closeHandle();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HANDLE _hProcess;
|
HANDLE _hProcess;
|
||||||
@@ -89,7 +90,7 @@ public:
|
|||||||
Pipe* outPipe,
|
Pipe* outPipe,
|
||||||
Pipe* errPipe,
|
Pipe* errPipe,
|
||||||
const EnvImpl& env);
|
const EnvImpl& env);
|
||||||
static void killImpl(const ProcessHandleImpl& handle);
|
static void killImpl(ProcessHandleImpl& handle);
|
||||||
static void killImpl(PIDImpl pid);
|
static void killImpl(PIDImpl pid);
|
||||||
static void requestTerminationImpl(PIDImpl pid);
|
static void requestTerminationImpl(PIDImpl pid);
|
||||||
static std::string terminationEventName(PIDImpl pid);
|
static std::string terminationEventName(PIDImpl pid);
|
||||||
|
@@ -62,6 +62,7 @@ public:
|
|||||||
UInt32 id() const;
|
UInt32 id() const;
|
||||||
HANDLE process() const;
|
HANDLE process() const;
|
||||||
int wait() const;
|
int wait() const;
|
||||||
|
void closeHandle();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HANDLE _hProcess;
|
HANDLE _hProcess;
|
||||||
@@ -89,7 +90,7 @@ public:
|
|||||||
Pipe* outPipe,
|
Pipe* outPipe,
|
||||||
Pipe* errPipe,
|
Pipe* errPipe,
|
||||||
const EnvImpl& env);
|
const EnvImpl& env);
|
||||||
static void killImpl(const ProcessHandleImpl& handle);
|
static void killImpl(ProcessHandleImpl& handle);
|
||||||
static void killImpl(PIDImpl pid);
|
static void killImpl(PIDImpl pid);
|
||||||
static void requestTerminationImpl(PIDImpl pid);
|
static void requestTerminationImpl(PIDImpl pid);
|
||||||
static std::string terminationEventName(PIDImpl pid);
|
static std::string terminationEventName(PIDImpl pid);
|
||||||
|
@@ -200,7 +200,7 @@ int Process::wait(const ProcessHandle& handle)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Process::kill(const ProcessHandle& handle)
|
void Process::kill(ProcessHandle& handle)
|
||||||
{
|
{
|
||||||
killImpl(*handle._pImpl);
|
killImpl(*handle._pImpl);
|
||||||
}
|
}
|
||||||
|
@@ -218,7 +218,7 @@ ProcessHandleImpl* ProcessImpl::launchByForkExecImpl(const std::string& command,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||||
{
|
{
|
||||||
killImpl(handle.id());
|
killImpl(handle.id());
|
||||||
}
|
}
|
||||||
|
@@ -137,7 +137,7 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||||
{
|
{
|
||||||
killImpl(handle.id());
|
killImpl(handle.id());
|
||||||
}
|
}
|
||||||
|
@@ -89,7 +89,7 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||||
{
|
{
|
||||||
throw Poco::NotImplementedException("Process::kill()");
|
throw Poco::NotImplementedException("Process::kill()");
|
||||||
}
|
}
|
||||||
|
@@ -56,9 +56,17 @@ ProcessHandleImpl::ProcessHandleImpl(HANDLE hProcess, UInt32 pid):
|
|||||||
|
|
||||||
ProcessHandleImpl::~ProcessHandleImpl()
|
ProcessHandleImpl::~ProcessHandleImpl()
|
||||||
{
|
{
|
||||||
CloseHandle(_hProcess);
|
closeHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ProcessHandleImpl::closeHandle()
|
||||||
|
{
|
||||||
|
if (_hProcess)
|
||||||
|
{
|
||||||
|
CloseHandle(_hProcess);
|
||||||
|
_hProcess = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
UInt32 ProcessHandleImpl::id() const
|
UInt32 ProcessHandleImpl::id() const
|
||||||
{
|
{
|
||||||
@@ -228,17 +236,19 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||||
{
|
{
|
||||||
|
if (handle.process())
|
||||||
|
{
|
||||||
if (TerminateProcess(handle.process(), 0) == 0)
|
if (TerminateProcess(handle.process(), 0) == 0)
|
||||||
{
|
{
|
||||||
CloseHandle(handle.process());
|
handle.closeHandle();
|
||||||
throw SystemException("cannot kill process");
|
throw SystemException("cannot kill process");
|
||||||
}
|
}
|
||||||
CloseHandle(handle.process());
|
handle.closeHandle();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ProcessImpl::killImpl(PIDImpl pid)
|
void ProcessImpl::killImpl(PIDImpl pid)
|
||||||
{
|
{
|
||||||
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
|
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
|
||||||
|
@@ -57,9 +57,17 @@ ProcessHandleImpl::ProcessHandleImpl(HANDLE hProcess, UInt32 pid):
|
|||||||
|
|
||||||
ProcessHandleImpl::~ProcessHandleImpl()
|
ProcessHandleImpl::~ProcessHandleImpl()
|
||||||
{
|
{
|
||||||
CloseHandle(_hProcess);
|
closeHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ProcessHandleImpl::closeHandle()
|
||||||
|
{
|
||||||
|
if (_hProcess)
|
||||||
|
{
|
||||||
|
CloseHandle(_hProcess);
|
||||||
|
_hProcess = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
UInt32 ProcessHandleImpl::id() const
|
UInt32 ProcessHandleImpl::id() const
|
||||||
{
|
{
|
||||||
@@ -234,17 +242,19 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||||
{
|
{
|
||||||
|
if (handle.process())
|
||||||
|
{
|
||||||
if (TerminateProcess(handle.process(), 0) == 0)
|
if (TerminateProcess(handle.process(), 0) == 0)
|
||||||
{
|
{
|
||||||
CloseHandle(handle.process());
|
handle.closeHandle();
|
||||||
throw SystemException("cannot kill process");
|
throw SystemException("cannot kill process");
|
||||||
}
|
}
|
||||||
CloseHandle(handle.process());
|
handle.closeHandle();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ProcessImpl::killImpl(PIDImpl pid)
|
void ProcessImpl::killImpl(PIDImpl pid)
|
||||||
{
|
{
|
||||||
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
|
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
|
||||||
|
@@ -57,9 +57,17 @@ ProcessHandleImpl::ProcessHandleImpl(HANDLE hProcess, UInt32 pid):
|
|||||||
|
|
||||||
ProcessHandleImpl::~ProcessHandleImpl()
|
ProcessHandleImpl::~ProcessHandleImpl()
|
||||||
{
|
{
|
||||||
CloseHandle(_hProcess);
|
closeHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ProcessHandleImpl::closeHandle()
|
||||||
|
{
|
||||||
|
if (_hProcess)
|
||||||
|
{
|
||||||
|
CloseHandle(_hProcess);
|
||||||
|
_hProcess = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
UInt32 ProcessHandleImpl::id() const
|
UInt32 ProcessHandleImpl::id() const
|
||||||
{
|
{
|
||||||
@@ -158,14 +166,17 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||||
{
|
{
|
||||||
|
if (handle.process())
|
||||||
|
{
|
||||||
if (TerminateProcess(handle.process(), 0) == 0)
|
if (TerminateProcess(handle.process(), 0) == 0)
|
||||||
{
|
{
|
||||||
CloseHandle(handle.process());
|
handle.closeHandle();
|
||||||
throw SystemException("cannot kill process");
|
throw SystemException("cannot kill process");
|
||||||
}
|
}
|
||||||
CloseHandle(handle.process());
|
handle.closeHandle();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user