cedric pushed a commit to branch master.
http://git.enlightenment.org/core/efl.git/commit/?id=f1a2c22dc35db870cd5eb16031fb2143100413eb
commit f1a2c22dc35db870cd5eb16031fb2143100413eb
Author: Vincent Torri <vincent dot torri at gmail dot com>
Date: Sun Sep 27 09:31:44 2015 +0200
ecore_exe: read remaining data when the child application has closed
Output and error threads could not read all the data sent by the child.
Based on a patch by Guillaume Friloux
@fix
Signed-off-by: Cedric BAIL <[email protected]>
---
src/lib/ecore/ecore_exe_win32.c | 144 +++++++++++++++++++++++-----------------
1 file changed, 84 insertions(+), 60 deletions(-)
diff --git a/src/lib/ecore/ecore_exe_win32.c b/src/lib/ecore/ecore_exe_win32.c
index f03b05b..24782e2 100644
--- a/src/lib/ecore/ecore_exe_win32.c
+++ b/src/lib/ecore/ecore_exe_win32.c
@@ -36,6 +36,40 @@
static int run_pri = NORMAL_PRIORITY_CLASS;
+static void
+_ecore_exe_threads_terminate(Ecore_Exe *obj)
+{
+ Ecore_Exe_Data *exe = eo_data_scope_get(obj, ECORE_EXE_CLASS);
+ HANDLE threads[2] = { NULL, NULL };
+ int i = 0;
+
+ if (exe->pipe_read.thread)
+ {
+ threads[i] = exe->pipe_read.thread;
+ i++;
+ }
+ if (exe->pipe_error.thread)
+ {
+ threads[i] = exe->pipe_error.thread;
+ i++;
+ }
+ if (i > 0)
+ {
+ exe->close_threads = 1;
+ WaitForMultipleObjects(i, threads, TRUE, INFINITE);
+ if (exe->pipe_error.thread)
+ {
+ CloseHandle(exe->pipe_error.thread);
+ exe->pipe_error.thread = NULL;
+ }
+ if (exe->pipe_read.thread)
+ {
+ CloseHandle(exe->pipe_read.thread);
+ exe->pipe_read.thread = NULL;
+ }
+ }
+}
+
static Eina_Bool
_ecore_exe_close_cb(void *data,
Ecore_Win32_Handler *wh EINA_UNUSED)
@@ -45,6 +79,8 @@ _ecore_exe_close_cb(void *data,
Ecore_Exe_Data *exe = eo_data_scope_get(obj, ECORE_EXE_CLASS);
DWORD exit_code = 0;
+ _ecore_exe_threads_terminate(obj);
+
e = calloc(1, sizeof(Ecore_Exe_Event_Del));
if (!e) return 0;
@@ -83,16 +119,22 @@ _ecore_exe_pipe_read_thread_cb(void *data)
DWORD current_size = 0;
BOOL res;
- while (!exe->close_threads)
+ while (1)
{
- if (!PeekNamedPipe(exe->pipe_read.child_pipe,
- buf, sizeof(buf) - 1, &size, ¤t_size, NULL))
- continue;
- if (size == 0)
- continue;
+ res = PeekNamedPipe(exe->pipe_read.child_pipe,
+ buf, sizeof(buf) - 1, &size, ¤t_size, NULL);
+ if (!res || (size == 0))
+ {
+ if (exe->close_threads)
+ break;
+
+ continue;
+ }
+
current_buf = (char *)malloc(current_size);
if (!current_buf)
continue;
+
res = ReadFile(exe->pipe_read.child_pipe, current_buf, current_size,
&size, NULL);
if (!res || (size == 0))
{
@@ -100,16 +142,21 @@ _ecore_exe_pipe_read_thread_cb(void *data)
current_buf = NULL;
continue;
}
- if (current_size != size)
+
+ current_size = size;
+
+ if (!exe->pipe_read.data_buf)
+ {
+ exe->pipe_read.data_buf = current_buf;
+ exe->pipe_read.data_size = current_size;
+ }
+ else
{
+ exe->pipe_read.data_buf = realloc(exe->pipe_read.data_buf,
exe->pipe_read.data_size + current_size);
+ memcpy((unsigned char *)exe->pipe_read.data_buf +
exe->pipe_read.data_size, current_buf, current_size);
+ exe->pipe_read.data_size += current_size;
free(current_buf);
- current_buf = NULL;
- continue;
}
- current_size = size;
-
- exe->pipe_read.data_buf = current_buf;
- exe->pipe_read.data_size = current_size;
event_data = ecore_exe_event_data_get(obj, ECORE_EXE_PIPE_READ);
if (event_data)
@@ -141,16 +188,22 @@ _ecore_exe_pipe_error_thread_cb(void *data)
DWORD current_size = 0;
BOOL res;
- while (!exe->close_threads)
+ while (1)
{
- if (!PeekNamedPipe(exe->pipe_error.child_pipe,
- buf, sizeof(buf) - 1, &size, ¤t_size, NULL))
- continue;
- if (size == 0)
- continue;
+ res = PeekNamedPipe(exe->pipe_error.child_pipe,
+ buf, sizeof(buf) - 1, &size, ¤t_size, NULL);
+ if (!res || (size == 0))
+ {
+ if (exe->close_threads)
+ break;
+
+ continue;
+ }
+
current_buf = (char *)malloc(current_size);
if (!current_buf)
continue;
+
res = ReadFile(exe->pipe_error.child_pipe, current_buf, current_size,
&size, NULL);
if (!res || (size == 0))
{
@@ -158,16 +211,21 @@ _ecore_exe_pipe_error_thread_cb(void *data)
current_buf = NULL;
continue;
}
- if (current_size != size)
+
+ current_size = size;
+
+ if (!exe->pipe_error.data_buf)
{
+ exe->pipe_error.data_buf = current_buf;
+ exe->pipe_error.data_size = current_size;
+ }
+ else
+ {
+ exe->pipe_error.data_buf = realloc(exe->pipe_error.data_buf,
exe->pipe_error.data_size + current_size);
+ memcpy((unsigned char *)exe->pipe_error.data_buf +
exe->pipe_error.data_size, current_buf, current_size);
+ exe->pipe_error.data_size += current_size;
free(current_buf);
- current_buf = NULL;
- continue;
}
- current_size = size;
-
- exe->pipe_error.data_buf = current_buf;
- exe->pipe_error.data_size = current_size;
event_data = ecore_exe_event_data_get(obj, ECORE_EXE_PIPE_ERROR);
if (event_data)
@@ -187,40 +245,6 @@ _ecore_exe_pipe_error_thread_cb(void *data)
return 0;
}
-static void
-_ecore_exe_threads_terminate(Ecore_Exe *obj)
-{
- Ecore_Exe_Data *exe = eo_data_scope_get(obj, ECORE_EXE_CLASS);
- HANDLE threads[2] = { NULL, NULL };
- int i = 0;
-
- if (exe->pipe_read.thread)
- {
- threads[i] = exe->pipe_read.thread;
- i++;
- }
- if (exe->pipe_error.thread)
- {
- threads[i] = exe->pipe_error.thread;
- i++;
- }
- if (i > 0)
- {
- exe->close_threads = 1;
- WaitForMultipleObjects(i, threads, TRUE, INFINITE);
- if (exe->pipe_error.thread)
- {
- CloseHandle(exe->pipe_error.thread);
- exe->pipe_error.thread = NULL;
- }
- if (exe->pipe_read.thread)
- {
- CloseHandle(exe->pipe_read.thread);
- exe->pipe_read.thread = NULL;
- }
- }
-}
-
static DWORD WINAPI
_ecore_exe_thread_procedure(LPVOID data EINA_UNUSED)
{
--