protect stream poll against race condition - release

This commit is contained in:
Philippe G
2020-08-28 17:13:05 -07:00
parent e0e02f1e5f
commit c8054ff9d2

View File

@@ -40,6 +40,7 @@
static log_level loglevel; static log_level loglevel;
static struct buffer buf; static struct buffer buf;
static mutex_type poll_mutex;
struct buffer *streambuf = &buf; struct buffer *streambuf = &buf;
#define LOCK mutex_lock(streambuf->mutex) #define LOCK mutex_lock(streambuf->mutex)
@@ -196,7 +197,11 @@ static void *stream_thread() {
UNLOCK; UNLOCK;
if (_poll(ssl, &pollinfo, 100)) { mutex_lock(poll_mutex);
int pending = _poll(ssl, &pollinfo, 100);
mutex_unlock(poll_mutex);
if (pending) {
LOCK; LOCK;
@@ -350,7 +355,6 @@ static void *stream_thread() {
UNLOCK; UNLOCK;
} else { } else {
LOG_SDEBUG("poll timeout"); LOG_SDEBUG("poll timeout");
} }
} }
@@ -403,6 +407,7 @@ void stream_init(log_level level, unsigned stream_buf_size) {
*stream.header = '\0'; *stream.header = '\0';
fd = -1; fd = -1;
mutex_create_p(poll_mutex);
#if LINUX || FREEBSD #if LINUX || FREEBSD
touch_memory(streambuf->buf, streambuf->size); touch_memory(streambuf->buf, streambuf->size);
@@ -432,6 +437,7 @@ void stream_close(void) {
#endif #endif
free(stream.header); free(stream.header);
buf_destroy(streambuf); buf_destroy(streambuf);
mutex_destroy(poll_mutex);
} }
void stream_file(const char *header, size_t header_len, unsigned threshold) { void stream_file(const char *header, size_t header_len, unsigned threshold) {
@@ -573,6 +579,7 @@ void stream_sock(u32_t ip, u16_t port, const char *header, size_t header_len, un
bool stream_disconnect(void) { bool stream_disconnect(void) {
bool disc = false; bool disc = false;
mutex_lock(poll_mutex);
LOCK; LOCK;
#if USE_SSL #if USE_SSL
if (ssl) { if (ssl) {
@@ -588,5 +595,6 @@ bool stream_disconnect(void) {
} }
stream.state = STOPPED; stream.state = STOPPED;
UNLOCK; UNLOCK;
mutex_unlock(poll_mutex);
return disc; return disc;
} }