From 447f908aff7763f8f5d9b6aa13c2551ce17c9b86 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 2 Oct 2001 19:22:24 +0000 Subject: [PATCH] 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 --- src/gtk/utilsgtk.cpp | 9 ++++++++- src/gtk1/utilsgtk.cpp | 9 ++++++++- src/unix/utilsunx.cpp | 28 +++------------------------- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/gtk/utilsgtk.cpp b/src/gtk/utilsgtk.cpp index f1346abc17..1296a0f762 100644 --- a/src/gtk/utilsgtk.cpp +++ b/src/gtk/utilsgtk.cpp @@ -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); diff --git a/src/gtk1/utilsgtk.cpp b/src/gtk1/utilsgtk.cpp index f1346abc17..1296a0f762 100644 --- a/src/gtk1/utilsgtk.cpp +++ b/src/gtk1/utilsgtk.cpp @@ -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); diff --git a/src/unix/utilsunx.cpp b/src/unix/utilsunx.cpp index fbf5377f76..0581126a28 100644 --- a/src/unix/utilsunx.cpp +++ b/src/unix/utilsunx.cpp @@ -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; } }