From cb65bc53489a3881783e8213e3c4b2a5cc507337 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Thu, 29 Aug 2013 17:12:25 +0200 Subject: [PATCH] avoid using 99% CPU in case gpsd dies This addresses and issue when gpsdate is running and the gpsd will be killed (testing coredump handling). The issue is within the libgps cod itself that doesn't handle the result(0) of the recv syscall correctly and keeps on looping. Now in a normal system gpsdate should only execute at the beginning and exit once there is a date. So the window for this runtime failure is quite low. Bug reported by Holger Freyther. --- gpsdate.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/gpsdate.c b/gpsdate.c index 3d8447a..6b4f303 100644 --- a/gpsdate.c +++ b/gpsdate.c @@ -123,6 +123,26 @@ static int osmo_daemonize(void) return 0; } +/* local copy, as the libgps official version ignores gps_read() result */ +static int my_gps_mainloop(struct gps_data_t *gdata, + int timeout, + void (*hook)(struct gps_data_t *gdata)) +{ + int rc; + + for (;;) { + if (!gps_waiting(gdata, timeout)) { + return -1; + } else { + rc = gps_read(gdata); + if (rc < 0) + return rc; + (*hook)(gdata); + } + } + return 0; +} + int main(int argc, char **argv) { char *host = "localhost"; @@ -155,7 +175,7 @@ int main(int argc, char **argv) /* We run in an endless loop. The only reasonable way to exit is after * a correct GPS timestamp has been received in callback() */ while (1) - gps_mainloop(&gpsdata, INT_MAX, callback); + my_gps_mainloop(&gpsdata, INT_MAX, callback); gps_close(&gpsdata);