2 * src/reader.c - type representing a stdout reader.
\r
4 * Copyright 2008,2009 Martin Quinson, Malek Cherier All right reserved.
\r
6 * This program is free software; you can redistribute it and/or modify it
\r
7 * under the terms of the license (GNU LGPL) which comes with this package.
\r
10 * This file contains all the definitions of the functions related with
\r
11 * the tesh reader type.
\r
17 #include <command.h>
\r
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
\r
24 reader_start_routine(void* p);
\r
27 reader_new(command_t command)
\r
31 /* TODO : check the parameter */
\r
33 reader = xbt_new0(s_reader_t, 1);
\r
35 reader->thread = NULL;
\r
36 reader->command = command;
\r
37 reader->broken_pipe = 0;
\r
41 reader->started = xbt_os_sem_init(0);
\r
47 reader_free(reader_t* ptr)
\r
50 xbt_os_sem_destroy((*ptr)->started);
\r
60 reader_read(reader_t reader)
\r
62 reader->thread = xbt_os_thread_create("", reader_start_routine, reader);
\r
67 reader_start_routine(void* p)
\r
69 reader_t reader = (reader_t)p;
\r
70 command_t command = reader->command;
\r
72 xbt_strbuff_t output = command->output;
\r
73 HANDLE stdout_fd = command->stdout_fd;
\r
75 DWORD number_of_bytes_to_read = 1024; /*command->context->output->used;*/
\r
76 DWORD number_of_bytes_readed = 0;
\r
78 char* buffer = (char*)calloc(number_of_bytes_to_read + 1,sizeof(char));
\r
79 char* clean = (char*)calloc(number_of_bytes_to_read + 1,sizeof(char));
\r
82 while(!command->failed && !command->interrupted && !command->successeded && !reader->failed && !reader->broken_pipe)
\r
84 if(!ReadFile(stdout_fd, buffer, number_of_bytes_to_read, &number_of_bytes_readed, NULL) || (0 == number_of_bytes_readed))
\r
86 if(GetLastError() == ERROR_BROKEN_PIPE)
\r
87 reader->broken_pipe = 1;
\r
94 if(number_of_bytes_readed > 0)
\r
96 for(i= 0, j= 0; i < number_of_bytes_readed; i++)
\r
97 if((int)(buffer[i]) != 13)
\r
98 clean[j++] = buffer[i];
\r
100 xbt_strbuff_append(output,clean);
\r
102 memset(buffer, 0, 1024);
\r
103 memset(clean, 0, 1024);
\r
107 xbt_os_thread_yield();
\r
115 xbt_strbuff_chomp(command->output);
\r
116 xbt_strbuff_trim(command->output);
\r
120 if(command->failed)
\r
122 if(command->reason == csr_write_failure)
\r
124 if(command->output->used)
\r
125 INFO2("[%s] Output on write failure:\n%s",command->context->pos, command->output->data);
\r
127 INFO1("[%s] No output before write failure",command->context->pos);
\r
129 else if(command->reason == csr_write_pipe_broken)
\r
131 if(command->output->used)
\r
132 INFO2("[%s] Output on broken pipe:\n%s",command->context->pos, command->output->data);
\r
134 INFO1("[%s] No output before broken pipe",command->context->pos);
\r
137 else if(command->interrupted)
\r
139 if(command->output->used)
\r
140 INFO2("[%s] Output on interruption:\n%s",command->context->pos, command->output->data);
\r
142 INFO1("[%s] No output before interruption",command->context->pos);
\r
144 else if(reader->failed && !command->failed && !command->interrupted && !command->successeded)
\r
146 ERROR2("[%s] Error while reading output of child `%s'", command->context->pos, command->context->command_line);
\r
148 if(command->output->used)
\r
149 INFO2("[%s] Output on read failure:\n%s",command->context->pos, command->output->data);
\r
151 INFO1("[%s] No output before read failure",command->context->pos);
\r
153 unit_set_error(command->unit, errno, 0);
\r
154 command_kill(command);
\r
155 command_handle_failure(command, csr_read_failure);
\r
165 reader_start_routine(void* p)
\r
167 reader_t reader = (reader_t)p;
\r
168 command_t command = reader->command;
\r
169 xbt_strbuff_t output = command->output;
\r
170 int stdout_fd = command->stdout_fd;
\r
171 int number_of_bytes_readed;
\r
172 int number_of_bytes_to_read = (1024 > SSIZE_MAX) ? SSIZE_MAX : 1024;
\r
176 char* buffer = (char*)calloc(number_of_bytes_to_read,sizeof(char));
\r
177 xbt_os_sem_release(reader->started);
\r
181 number_of_bytes_readed = read(stdout_fd, buffer, number_of_bytes_to_read);
\r
183 if(number_of_bytes_readed < 0 && errno != EINTR && errno != EAGAIN)
\r
185 reader->failed = 1;
\r
188 if(number_of_bytes_readed > 0)
\r
190 buffer[number_of_bytes_readed]='\0';
\r
191 xbt_strbuff_append(output,buffer);
\r
196 xbt_os_thread_yield();
\r
199 }while(!command->failed && !command->interrupted && !command->successeded && !reader->failed && (number_of_bytes_readed != 0 /* end of file <-> normal exit */));
\r
203 if(close(command->stdout_fd) < 0)
\r
208 command->stdout_fd = INDEFINITE_FD;
\r
210 xbt_strbuff_chomp(command->output);
\r
211 xbt_strbuff_trim(command->output);
\r
215 if(command->failed)
\r
217 if(command->reason == csr_write_failure)
\r
219 if(command->output->used)
\r
220 INFO2("[%s] Output on write failure:\n%s",command->context->pos, command->output->data);
\r
222 INFO1("[%s] No output before write failur",command->context->pos);
\r
224 else if(command->reason == csr_write_pipe_broken)
\r
226 if(command->output->used)
\r
227 INFO2("[%s] Output on broken pipe:\n%s",command->context->pos, command->output->data);
\r
229 INFO1("[%s] No output before broken pipe",command->context->pos);
\r
232 else if(command->interrupted)
\r
234 if(command->output->used)
\r
235 INFO2("[%s] Output on interruption:\n%s",command->context->pos, command->output->data);
\r
237 INFO1("[%s] No output before interruption",command->context->pos);
\r
239 else if(reader->failed && !command->failed && !command->interrupted && !command->successeded)
\r
241 ERROR2("[%s] Error while reading output of child `%s'", command->context->pos, command->context->command_line);
\r
243 if(command->output->used)
\r
244 INFO2("[%s] Output on read failure:\n%s",command->context->pos, command->output->data);
\r
246 INFO1("[%s] No output before read failure",command->context->pos);
\r
248 unit_set_error(command->unit, errno, 0);
\r
249 command_kill(command);
\r
250 command_handle_failure(command, csr_read_failure);
\r
259 reader_wait(reader_t reader)
\r
261 xbt_os_thread_join(reader->thread, NULL);
\r