From dfd78c722b738e8db4c096393b6ecf0e26435f40 Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Mon, 23 Sep 2013 12:16:52 -0700 Subject: [PATCH] chansrv: fix for deadlock --- common/trans.c | 83 ++++++++++++++++++++++++++++++++++++++++ common/trans.h | 3 ++ sesman/chansrv/chansrv.c | 14 ++++++- 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/common/trans.c b/common/trans.c index 8313b606..408b4a7e 100644 --- a/common/trans.c +++ b/common/trans.c @@ -291,6 +291,7 @@ trans_force_write_s(struct trans *self, struct stream *out_s) size = (int)(out_s->end - out_s->data); total = 0; + self->in_write = 1; while (total < size) { sent = g_tcp_send(self->sck, out_s->data + total, size - total, 0); @@ -308,6 +309,7 @@ trans_force_write_s(struct trans *self, struct stream *out_s) { /* term */ self->status = TRANS_STATUS_DOWN; + self->in_write = 0; return 1; } } @@ -317,6 +319,7 @@ trans_force_write_s(struct trans *self, struct stream *out_s) { /* error */ self->status = TRANS_STATUS_DOWN; + self->in_write = 0; return 1; } } @@ -324,6 +327,7 @@ trans_force_write_s(struct trans *self, struct stream *out_s) { /* error */ self->status = TRANS_STATUS_DOWN; + self->in_write = 0; return 1; } else @@ -331,6 +335,7 @@ trans_force_write_s(struct trans *self, struct stream *out_s) total = total + sent; } } + self->in_write = 0; return 0; } @@ -342,6 +347,84 @@ trans_force_write(struct trans *self) return trans_force_write_s(self, self->out_s); } +/*****************************************************************************/ +int APP_CC +trans_write_check(struct trans* self, int timeout) +{ + int size; + int total; + int sent; + int error; + tbus robjs[1]; + tbus wobjs[1]; + struct stream *out_s; + + if (self->status != TRANS_STATUS_UP) + { + return 1; + } + + out_s = self->out_s; + + size = (int)(out_s->end - out_s->data); + total = 0; + + self->in_write = 1; + while (total < size) + { + robjs[0] = self->sck; + wobjs[0] = self->sck; + error = g_obj_wait(robjs, 1, wobjs, 1, timeout); + if (error != 0) + { + /* error */ + self->status = TRANS_STATUS_DOWN; + self->in_write = 0; + return 1; + } + + if (!g_tcp_can_send(self->sck, 0)) + { + trans_check_wait_objs(self); + continue; + } + + sent = g_tcp_send(self->sck, out_s->data + total, size - total, 0); + + if (sent == -1) + { + if (g_tcp_last_error_would_block(self->sck)) + { + if (!g_tcp_can_send(self->sck, 10)) + { + /* check for term here */ + } + } + else + { + /* error */ + self->status = TRANS_STATUS_DOWN; + self->in_write = 0; + return 1; + } + } + else if (sent == 0) + { + /* error */ + self->status = TRANS_STATUS_DOWN; + self->in_write = 0; + return 1; + } + else + { + total = total + sent; + } + } + self->in_write = 0; + + return 0; +} + /*****************************************************************************/ int APP_CC trans_connect(struct trans *self, const char *server, const char *port, diff --git a/common/trans.h b/common/trans.h index 4b8e3b42..d9ad6c19 100644 --- a/common/trans.h +++ b/common/trans.h @@ -54,6 +54,7 @@ struct trans struct stream* out_s; char* listen_filename; tis_term is_term; /* used to test for exit */ + int in_write; }; struct trans* APP_CC @@ -73,6 +74,8 @@ trans_force_read(struct trans* self, int size); int APP_CC trans_force_write(struct trans* self); int APP_CC +trans_write_check(struct trans* self, int timeout); +int APP_CC trans_connect(struct trans* self, const char* server, const char* port, int timeout); int APP_CC diff --git a/sesman/chansrv/chansrv.c b/sesman/chansrv/chansrv.c index c3d1aa79..56626c77 100644 --- a/sesman/chansrv/chansrv.c +++ b/sesman/chansrv/chansrv.c @@ -293,10 +293,20 @@ send_data_from_chan_item(struct chan_item *chan_item) out_uint32_le(s, cod->s->size); out_uint8a(s, cod->s->p, size); s_mark_end(s); - LOGM((LOG_LEVEL_DEBUG, "chansrv::send_channel_data: -- " + LOGM((LOG_LEVEL_DEBUG, "chansrv::send_data_from_chan_item: -- " "size %d chan_flags 0x%8.8x", size, chan_flags)); g_sent = 1; - error = trans_force_write(g_con_trans); + if (g_con_trans->in_write) + { + g_writeln("chansrv::send_data_from_chan_item: error, " + "write while in_write"); + error = 1; + } + else + { + /* write but check for read if blocked */ + error = trans_write_check(g_con_trans, -1); + } if (error != 0) {