* the tesh reader type.\r
*\r
*/\r
- \r
+\r
+#include <unit.h>\r
#include <reader.h>\r
#include <command.h>\r
\r
int\r
reader_free(reader_t* ptr)\r
{\r
- /* TODO : check the parameter */\r
+ if((*ptr)->started)\r
+ xbt_os_sem_destroy((*ptr)->started);\r
\r
free(*ptr);\r
+ \r
*ptr = NULL;\r
\r
return 0;\r
}\r
\r
#ifdef WIN32\r
-/*static void*\r
-reader_start_routine(void* p)\r
-{\r
- reader_t reader = (reader_t)p;\r
- command_t command = reader->command;\r
- \r
- xbt_strbuff_t output = command->output;\r
- HANDLE stdout_fd = command->stdout_fd;\r
- \r
- DWORD number_of_bytes_to_read = 4096;\r
- DWORD number_of_bytes_readed = 0;\r
- \r
- char* buffer = (char*)calloc(number_of_bytes_to_read + 1,sizeof(char));\r
- char* clean;\r
- char* cp = buffer;\r
- size_t i, j;\r
-\r
- while(!command->failed && !command->interrupted && !command->successeded && !reader->failed && !reader->broken_pipe)\r
- {\r
- if(!ReadFile(reader->command->stdout_fd, cp, number_of_bytes_to_read, &number_of_bytes_readed, NULL) || (0 == number_of_bytes_readed))\r
- {\r
- if(GetLastError() == ERROR_BROKEN_PIPE)\r
- reader->broken_pipe = 1;\r
- else\r
- reader->failed = 1;\r
- \r
- }\r
- else\r
- {\r
- if(number_of_bytes_readed > 0) \r
- {\r
- number_of_bytes_to_read -= number_of_bytes_readed;\r
- cp +=number_of_bytes_readed;\r
- } \r
- else \r
- {\r
- xbt_os_thread_yield();\r
- }\r
- }\r
- }\r
-\r
- if(reader->failed && !command->failed && !command->interrupted && !command->successeded)\r
- {\r
- error_register("read() function failed", (int)GetLastError(), command->context->command_line, command->unit->fstream->name);\r
- \r
- command_kill(command);\r
- command_handle_failure(command, csr_read_failure);\r
- }\r
- \r
- clean = (char*)calloc((unsigned int)strlen(buffer) + 1, sizeof(char));\r
- \r
- j = 0;\r
- \r
- for(i= 0; i < strlen(buffer); i++)\r
- if((int)(buffer[i]) != 13)\r
- clean[j++] = buffer[i];\r
-\r
- xbt_strbuff_append(output,clean);\r
- \r
- free(clean);\r
- free(buffer);\r
-\r
- reader->done = 1;\r
- \r
- return NULL;\r
-}*/\r
-\r
static void*\r
reader_start_routine(void* p)\r
{\r
char* clean = (char*)calloc(number_of_bytes_to_read + 1,sizeof(char));\r
size_t i, j;\r
\r
+ xbt_os_sem_release(reader->started);\r
+\r
while(!command->failed && !command->interrupted && !command->successeded && !reader->failed && !reader->broken_pipe)\r
{\r
- if(!ReadFile(reader->command->stdout_fd, buffer, number_of_bytes_to_read, &number_of_bytes_readed, NULL) || (0 == number_of_bytes_readed))\r
+ if(!ReadFile(stdout_fd, buffer, number_of_bytes_to_read, &number_of_bytes_readed, NULL) || (0 == number_of_bytes_readed))\r
{\r
if(GetLastError() == ERROR_BROKEN_PIPE)\r
reader->broken_pipe = 1;\r
free(clean);\r
free(buffer);\r
\r
- if(reader->failed && !command->failed && !command->interrupted && !command->successeded)\r
+ xbt_strbuff_chomp(command->output);\r
+ xbt_strbuff_trim(command->output);\r
+\r
+ reader->done = 1;\r
+ \r
+ if(command->failed)\r
+ {\r
+ if(command->reason == csr_write_failure)\r
+ {\r
+ if(command->output->used)\r
+ INFO2("[%s] Output on write failure:\n%s",command->context->pos, command->output->data);\r
+ else\r
+ INFO1("[%s] No output before write failure",command->context->pos);\r
+ }\r
+ else if(command->reason == csr_write_pipe_broken)\r
+ {\r
+ if(command->output->used)\r
+ INFO2("[%s] Output on broken pipe:\n%s",command->context->pos, command->output->data);\r
+ else\r
+ INFO1("[%s] No output before broken pipe",command->context->pos);\r
+ }\r
+ }\r
+ else if(command->interrupted)\r
+ {\r
+ if(command->output->used)\r
+ INFO2("[%s] Output on interruption:\n%s",command->context->pos, command->output->data);\r
+ else\r
+ INFO1("[%s] No output before interruption",command->context->pos);\r
+ }\r
+ else if(reader->failed && !command->failed && !command->interrupted && !command->successeded)\r
{\r
- error_register("read() function failed", (int)GetLastError(), command->context->command_line, command->unit->fstream->name);\r
+ ERROR2("[%s] Error while reading output of child `%s'", command->context->pos, command->context->command_line);\r
+ \r
+ if(command->output->used)\r
+ INFO2("[%s] Output on read failure:\n%s",command->context->pos, command->output->data);\r
+ else\r
+ INFO1("[%s] No output before read failure",command->context->pos);\r
\r
+ unit_set_error(command->unit, errno, 0);\r
command_kill(command);\r
command_handle_failure(command, csr_read_failure);\r
}\r
\r
- reader->done = 1;\r
+ \r
\r
return NULL;\r
}\r
int stdout_fd = command->stdout_fd;\r
int number_of_bytes_readed;\r
int number_of_bytes_to_read = (1024 > SSIZE_MAX) ? SSIZE_MAX : 1024;\r
+\r
+ int total = 0;\r
\r
char* buffer = (char*)calloc(number_of_bytes_to_read,sizeof(char));\r
xbt_os_sem_release(reader->started);\r
{\r
buffer[number_of_bytes_readed]='\0';\r
xbt_strbuff_append(output,buffer);\r
+ total += total;\r
} \r
else \r
{\r
}\r
else\r
command->stdout_fd = INDEFINITE_FD;\r
+ \r
+ xbt_strbuff_chomp(command->output);\r
+ xbt_strbuff_trim(command->output);\r
+\r
+ reader->done = 1;\r
\r
- if(reader->failed && !command->failed && !command->interrupted && !command->successeded)\r
+ if(command->failed)\r
{\r
- error_register("read() function failed", errno, command->context->command_line, command->unit->fstream->name);\r
+ if(command->reason == csr_write_failure)\r
+ {\r
+ if(command->output->used)\r
+ INFO2("[%s] Output on write failure:\n%s",command->context->pos, command->output->data);\r
+ else\r
+ INFO1("[%s] No output before write failur",command->context->pos);\r
+ }\r
+ else if(command->reason == csr_write_pipe_broken)\r
+ {\r
+ if(command->output->used)\r
+ INFO2("[%s] Output on broken pipe:\n%s",command->context->pos, command->output->data);\r
+ else\r
+ INFO1("[%s] No output before broken pipe",command->context->pos);\r
+ }\r
+ }\r
+ else if(command->interrupted)\r
+ {\r
+ if(command->output->used)\r
+ INFO2("[%s] Output on interruption:\n%s",command->context->pos, command->output->data);\r
+ else\r
+ INFO1("[%s] No output before interruption",command->context->pos);\r
+ }\r
+ else if(reader->failed && !command->failed && !command->interrupted && !command->successeded)\r
+ {\r
+ ERROR2("[%s] Error while reading output of child `%s'", command->context->pos, command->context->command_line);\r
\r
+ if(command->output->used)\r
+ INFO2("[%s] Output on read failure:\n%s",command->context->pos, command->output->data);\r
+ else\r
+ INFO1("[%s] No output before read failure",command->context->pos);\r
+ \r
+ unit_set_error(command->unit, errno, 0);\r
command_kill(command);\r
command_handle_failure(command, csr_read_failure);\r
}\r
\r
- reader->done = 1;\r
\r
return NULL;\r
} \r