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->started = xbt_os_sem_init(0);
22 writer_free(writer_t* writer)
29 writer_write(writer_t writer)
31 writer->thread = xbt_os_thread_create("", writer_start_routine, writer);
36 writer_start_routine(void* p)
38 writer_t writer = (writer_t)p;
39 command_t command = writer->command;
41 char* input = (char*)(command->context->input->data);
43 DWORD number_of_bytes_to_write = command->context->input->used;
44 DWORD number_of_bytes_written = 0;
47 while(!command->failed && !command->interrupted && !command->successeded && ! writer->failed && ! writer->broken_pipe && number_of_bytes_to_write)
49 if(!WriteFile(writer->command->stdin_fd, input, number_of_bytes_to_write, &number_of_bytes_written, NULL))
51 if(GetLastError() == ERROR_NO_DATA)
52 writer->broken_pipe = 1;
59 input += number_of_bytes_written;
60 number_of_bytes_to_write -= number_of_bytes_written;
64 command->context->input->data[0]='\0';
65 command->context->input->used=0;
67 if(writer->failed && !command->successeded && !command->failed && !command->interrupted)
69 command_kill(command);
71 command_handle_failure(command, csr_write_failure);
73 /*else if(writer->broken_pipe && !command->successeded && !command->failed && !command->interrupted)
75 command_kill(command);
76 command_handle_failure(command, csr_write_pipe_broken);
84 writer_start_routine(void* p)
86 writer_t writer = (writer_t)p;
87 command_t command = writer->command;
88 int number_of_bytes_written = 0;
89 int number_of_bytes_to_write = command->context->input->used;
90 char* input = (char*)(command->context->input->data);
93 xbt_os_sem_release(writer->started);
97 while(!command->failed && !command->interrupted && !command->successeded && ! writer->failed && ! writer->broken_pipe && (number_of_bytes_written < number_of_bytes_to_write))
99 got = write(writer->command->stdin_fd, input + number_of_bytes_written, number_of_bytes_to_write - number_of_bytes_written);
102 number_of_bytes_written += got;
108 writer->broken_pipe = 1;
110 else if(errno != EINTR && errno != EAGAIN && errno != EPIPE)
120 command->context->input->data[0]='\0';
121 command->context->input->used=0;
123 if(writer->failed && !command->successeded && !command->failed && !command->interrupted)
125 command_kill(command);
127 command_handle_failure(command, csr_write_failure);
129 else if(writer->broken_pipe && !command->successeded && !command->failed && !command->interrupted)
131 command_kill(command);
132 exit_code = EWRITEPIPE;
133 command_handle_failure(command, csr_write_pipe_broken);
137 close(command->stdin_fd);
138 command->stdin_fd = INDEFINITE_FD;
144 writer_start_routine(void* p)
146 writer_t writer = (writer_t)p;
147 command_t command = writer->command;
148 long number_of_bytes_to_write = command->context->input->used;
149 char* input = (char*)(command->context->input->data);
152 xbt_os_sem_release(writer->started);
154 while(!command->failed && !command->interrupted && !command->successeded && number_of_bytes_to_write > 0)
156 got = number_of_bytes_to_write > SSIZE_MAX ? SSIZE_MAX : number_of_bytes_to_write;
157 got = write( writer->command->stdin_fd, input, got );
161 if(EINTR == errno || EAGAIN == errno)
165 else if(EPIPE == errno)
167 writer->broken_pipe = 1;
178 number_of_bytes_to_write -= got;
185 command->context->input->data[0]='\0';
186 command->context->input->used=0;
188 if(writer->failed && !command->successeded && !command->failed && !command->interrupted)
190 command_kill(command);
192 command_handle_failure(command, csr_write_failure);
194 else if(writer->broken_pipe && !command->successeded && !command->failed && !command->interrupted)
196 command_kill(command);
197 exit_code = EWRITEPIPE;
198 command_handle_failure(command, csr_write_pipe_broken);
202 close(command->stdin_fd);
203 command->stdin_fd = INDEFINITE_FD;
211 writer_wait(writer_t writer)
213 xbt_os_thread_join(writer->thread, NULL);