fixed the exit code detection for async processes

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@11787 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2001-10-02 19:22:24 +00:00
parent 334f0d2c11
commit 447f908aff
3 changed files with 19 additions and 27 deletions

View File

@ -148,12 +148,19 @@ static void GTK_EndProcessDetector(gpointer data, gint source,
// generate G_IO_HUP notification even when it simply tries to read from a
// closed fd and hasn't terminated at all
int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
if ( waitpid(pid, NULL, WNOHANG) == 0 )
int status = 0;
int rc = waitpid(pid, &status, WNOHANG);
if ( rc == 0 )
{
// no, it didn't exit yet, continue waiting
return;
}
// set exit code to -1 if something bad happened
proc_data->exitcode = rc != -1 && WIFEXITED(status) ? WEXITSTATUS(status)
: -1;
// child exited, end waiting
close(source);

View File

@ -148,12 +148,19 @@ static void GTK_EndProcessDetector(gpointer data, gint source,
// generate G_IO_HUP notification even when it simply tries to read from a
// closed fd and hasn't terminated at all
int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
if ( waitpid(pid, NULL, WNOHANG) == 0 )
int status = 0;
int rc = waitpid(pid, &status, WNOHANG);
if ( rc == 0 )
{
// no, it didn't exit yet, continue waiting
return;
}
// set exit code to -1 if something bad happened
proc_data->exitcode = rc != -1 && WIFEXITED(status) ? WEXITSTATUS(status)
: -1;
// child exited, end waiting
close(source);

View File

@ -288,32 +288,12 @@ bool wxShell(const wxString& command, wxArrayString& output)
void wxHandleProcessTermination(wxEndProcessData *proc_data)
{
int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
// waitpid is POSIX so should be available everywhere, however on older
// systems wait() might be used instead in a loop (until the right pid
// terminates)
int status = 0;
int rc;
// wait for child termination and if waitpid() was interrupted, try again
do
{
rc = waitpid(pid, &status, 0);
}
while ( rc == -1 && errno == EINTR );
// notify user about termination if required
if ( proc_data->process )
{
proc_data->process->OnTerminate
(
proc_data->pid,
(rc == 0) && WIFEXITED(status)
? WEXITSTATUS(status)
: -1
);
proc_data->process->OnTerminate(proc_data->pid, proc_data->exitcode);
}
// clean up
if ( proc_data->pid > 0 )
{
@ -321,9 +301,7 @@ void wxHandleProcessTermination(wxEndProcessData *proc_data)
}
else
{
// wxExecute() will know about it
proc_data->exitcode = status;
// let wxExecute() know that the process has terminated
proc_data->pid = 0;
}
}