From 7097345d93a7b1c2ecd5ffd481a45fa171706f73 Mon Sep 17 00:00:00 2001 From: Daniel Willmann Date: Wed, 27 Aug 2014 14:48:46 +0200 Subject: Exit from gps_sock_mainloop() if connection to gpsd is lost The current mainloop function does not evaluate the return value of gps_read(). If the socket is closed (because gpsd is stopped) the mainloop will consume 99% CPU constantly doing a recv() from the socket which returns zero. gps_read already returns -1 if this condition is encountered. This patch checks the return value of gps_read() and if it is -1 the mainloop is exited. This is also what happens in gps_shm_mainloop(). --- libgps_sock.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'libgps_sock.c') diff --git a/libgps_sock.c b/libgps_sock.c index 2a846a13..e84a1eb2 100644 --- a/libgps_sock.c +++ b/libgps_sock.c @@ -572,8 +572,12 @@ int gps_sock_mainloop(struct gps_data_t *gpsdata, int timeout, if (!gps_waiting(gpsdata, timeout)) { return -1; } else { - (void)gps_read(gpsdata); - (*hook)(gpsdata); + int status = gps_read(gpsdata); + + if (status == -1) + return -1; + if (status > 0) + (*hook)(gpsdata); } } //return 0; -- cgit v1.2.1