From 26a81747cd79a663c8e70dc6be478efcb3f3578c Mon Sep 17 00:00:00 2001 From: Michael Heimpold Date: Tue, 17 Oct 2017 11:07:58 +0200 Subject: [PATCH] mosquitto_loop_forever: use nanosleep instead of sleep The advantage of nanosleep(2) is, that -according to POSIX spec- it does not interact with signals as sleep(3) does. So it is not affected when used by a program which is e.g. using alarm(3). Signed-off-by: Michael Heimpold --- lib/mosquitto.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/mosquitto.c b/lib/mosquitto.c index f526d538..9797113e 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -23,6 +23,7 @@ Contributors: #include #include #include +#include #else #include #include @@ -983,6 +984,9 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets) int rc; unsigned int reconnects = 0; unsigned long reconnect_delay; +#ifndef WIN32 + struct timespec req, rem; +#endif if(!mosq) return MOSQ_ERR_INVAL; @@ -1042,7 +1046,10 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets) #ifdef WIN32 Sleep(reconnect_delay*1000); #else - sleep(reconnect_delay); + req.tv_sec = reconnect_delay; + req.tv_nsec = 0; + while(nanosleep(&req, &rem) == -1 && errno == EINTR) + req = rem; #endif pthread_mutex_lock(&mosq->state_mutex);