void *data;
};
+static ssize_t write_safe(int fd, const void *data, size_t bufsz)
+{
+ int ret;
+ int again;
+
+ do {
+ again = 0;
+ ret = write(fd, data, bufsz);
+ if (ret < 0) {
+ ret = -errno;
+ switch (ret) {
+ case -EAGAIN:
+ case -EINTR:
+ again = 1;
+ ErrPrint("Interrupted[%d] Again[%d]\n", fd, -ret);
+ break;
+ default:
+ ErrPrint("Failed to write: %s\n", strerror(-ret));
+ return ret;
+ }
+ }
+ } while (again);
+
+ return ret;
+}
+
/*!
* \NOTE
* Running thread: Main
void *res = NULL;
struct chunk *chunk;
- if (write(tcb->ctrl_pipe[PIPE_WRITE], &tcb, sizeof(tcb)) != sizeof(tcb)) {
- ErrPrint("Unable to write CTRL pipe\n");
+ if (write_safe(tcb->ctrl_pipe[PIPE_WRITE], &tcb, sizeof(tcb)) != sizeof(tcb)) {
+ ErrPrint("Unable to write CTRL pipe (%d)\n", sizeof(tcb));
}
secure_socket_destroy_handle(tcb->handle);
* \NOTE
* Running thread: Other
*/
-static inline void chunk_append(struct tcb *tcb, struct chunk *chunk)
+static inline int chunk_append(struct tcb *tcb, struct chunk *chunk)
{
char event_ch = EVENT_READY;
int ret;
CRITICAL_SECTION_END(&tcb->chunk_lock);
- ret = write(tcb->evt_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch));
+ ret = write_safe(tcb->evt_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch));
if (ret < 0) {
- ErrPrint("write: %s\n", strerror(errno));
- return;
+ CRITICAL_SECTION_BEGIN(&tcb->chunk_lock);
+
+ dlist_remove_data(tcb->chunk_list, chunk);
+
+ CRITICAL_SECTION_END(&tcb->chunk_lock);
+ return ret;
}
if (ret != sizeof(event_ch)) {
/* Take a breathe */
pthread_yield();
+ return 0;
}
/*!
int readsize;
char event_ch;
int fd;
+ int w_ret;
DbgPrint("Thread is created for %d (server: %d)\n", tcb->handle, tcb->server_handle);
/*!
/*!
* Count of chunk elements are same with PIPE'd data
*/
- chunk_append(tcb, chunk);
+ if (chunk_append(tcb, chunk) < 0) {
+ destroy_chunk(chunk);
+ }
}
DbgPrint("Client CB is terminated (%d)\n", tcb->handle);
/* Wake up main thread to get disconnected event */
event_ch = EVENT_TERM;
- if (write(tcb->evt_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
- ErrPrint("write: %s\n", strerror(errno));
+
+ if (write_safe(tcb->evt_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
+ ErrPrint("%d byte is not written\n", sizeof(event_ch));
}
return (void *)(unsigned long)ret;