4 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
7 writer_start_routine(void* p);
10 writer_new(command_t command)
12 writer_t writer = xbt_new0(s_writer_t, 1);
14 writer->thread = NULL;
15 writer->command = command;
16 writer->written = xbt_os_sem_init(0);
17 writer->can_write = xbt_os_sem_init(0);
25 writer_free(writer_t* writer)
28 /*xbt_os_sem_destroy((*writer)->started);
29 xbt_os_sem_destroy((*writer)->can_write);*/
36 writer_write(writer_t writer)
38 writer->thread = xbt_os_thread_create("", writer_start_routine, writer);
43 writer_start_routine(void* p)
45 writer_t writer = (writer_t)p;
46 command_t command = writer->command;
48 char* input = (char*)(command->context->input->data);
50 DWORD number_of_bytes_to_write = command->context->input->used;
51 DWORD number_of_bytes_written = 0;
54 while(!command->failed && !command->interrupted && !command->successeded && ! writer->failed && ! writer->broken_pipe && number_of_bytes_to_write)
56 if(!WriteFile(writer->command->stdin_fd, input, number_of_bytes_to_write, &number_of_bytes_written, NULL))
58 if(GetLastError() == ERROR_NO_DATA)
59 writer->broken_pipe = 1;
66 input += number_of_bytes_written;
67 number_of_bytes_to_write -= number_of_bytes_written;
71 command->context->input->data[0]='\0';
72 command->context->input->used=0;
74 if(writer->failed && !command->successeded && !command->failed && !command->interrupted)
76 command_kill(command);
78 command_handle_failure(command, csr_write_failure);
80 /*else if(writer->broken_pipe && !command->successeded && !command->failed && !command->interrupted)
82 command_kill(command);
83 command_handle_failure(command, csr_write_pipe_broken);
91 writer_start_routine(void* p)
93 writer_t writer = (writer_t)p;
94 command_t command = writer->command;
95 int number_of_bytes_written = 0;
96 int number_of_bytes_to_write = command->context->input->used;
97 char* input = (char*)(command->context->input->data);
100 xbt_os_sem_release(writer->started);
104 while(!command->failed && !command->interrupted && !command->successeded && ! writer->failed && ! writer->broken_pipe && (number_of_bytes_written < number_of_bytes_to_write))
106 got = write(writer->command->stdin_fd, input + number_of_bytes_written, number_of_bytes_to_write - number_of_bytes_written);
109 number_of_bytes_written += got;
115 writer->broken_pipe = 1;
117 else if(errno != EINTR && errno != EAGAIN && errno != EPIPE)
127 command->context->input->data[0]='\0';
128 command->context->input->used=0;
130 if(writer->failed && !command->successeded && !command->failed && !command->interrupted)
132 command_kill(command);
134 command_handle_failure(command, csr_write_failure);
136 else if(writer->broken_pipe && !command->successeded && !command->failed && !command->interrupted)
138 command_kill(command);
139 exit_code = EWRITEPIPE;
140 command_handle_failure(command, csr_write_pipe_broken);
144 close(command->stdin_fd);
145 command->stdin_fd = INDEFINITE_FD;
151 writer_start_routine(void* p)
153 writer_t writer = (writer_t)p;
154 command_t command = writer->command;
155 int number_of_bytes_to_write = command->context->input->used;
156 char* input = (char*)(command->context->input->data);
161 xbt_os_sem_acquire(writer->can_write);
164 while(!command->failed && !command->interrupted && !command->successeded && number_of_bytes_to_write > 0)
166 got = number_of_bytes_to_write > PIPE_BUF ? PIPE_BUF : number_of_bytes_to_write;
167 got = write(writer->command->stdin_fd, input, got );
174 else if(EAGAIN == errno)
175 {/* the pipe is full */
178 xbt_os_sem_release(writer->written);
184 else if(EPIPE == errno)
186 writer->broken_pipe = 1;
197 number_of_bytes_to_write -= got;
201 xbt_os_thread_yield();
207 xbt_os_sem_release(writer->written);
212 close(command->stdin_fd);
213 command->stdin_fd = INDEFINITE_FD;
215 command->context->input->data[0]='\0';
216 command->context->input->used=0;
218 if(writer->failed && !command->successeded && !command->failed && !command->interrupted)
220 command_kill(command);
222 command_handle_failure(command, csr_write_failure);
224 else if(writer->broken_pipe && !command->successeded && !command->failed && !command->interrupted)
226 command_kill(command);
227 exit_code = EWRITEPIPE;
228 command_handle_failure(command, csr_write_pipe_broken);
239 writer_wait(writer_t writer)
241 xbt_os_thread_join(writer->thread, NULL);