diff options
-rw-r--r-- | hardinfo2/arch/linux/common/sensors.h | 11 | ||||
-rw-r--r-- | hardinfo2/socket.c | 30 | ||||
-rw-r--r-- | hardinfo2/socket.h | 1 |
3 files changed, 31 insertions, 11 deletions
diff --git a/hardinfo2/arch/linux/common/sensors.h b/hardinfo2/arch/linux/common/sensors.h index 946de968..652273d2 100644 --- a/hardinfo2/arch/linux/common/sensors.h +++ b/hardinfo2/arch/linux/common/sensors.h @@ -276,7 +276,7 @@ read_sensors_hddtemp(void) gint len = 0; if ((s = sock_connect("127.0.0.1", 7634))) { - while (len <= 2) + while (!len) len = sock_read(s, buffer, sizeof(buffer)); sock_close(s); @@ -309,13 +309,10 @@ read_sensors_hddtemp(void) } g_strfreev(disks); - } else { - /* FIXME: This might go crazy in an infinite loop. */ - g_warning("reading hddtemp failed. retrying in 100ms"); - nonblock_sleep(100); - read_sensors_hddtemp(); - return; } + } else { + g_free(old); + old = NULL; } if (old) { diff --git a/hardinfo2/socket.c b/hardinfo2/socket.c index c17c9f5c..d1bd4af3 100644 --- a/hardinfo2/socket.c +++ b/hardinfo2/socket.c @@ -61,14 +61,36 @@ int sock_write(Socket * s, gchar * str) return write(s->sock, str, strlen(str)); } +/* From: http://www.erlenstar.demon.co.uk/unix/faq_3.html#SEC26 */ +int sock_is_ready(Socket *s) +{ + int rc, fd = s->sock; + fd_set fds; + struct timeval tv; + + FD_ZERO(&fds); + FD_SET(fd, &fds); + tv.tv_sec = tv.tv_usec = 0; + + rc = select(fd+1, &fds, NULL, NULL, &tv); + if (rc < 0) + return -1; + + return FD_ISSET(fd, &fds) ? 1 : 0; +} + int sock_read(Socket * s, gchar * buffer, gint size) { - gint n; + if (sock_is_ready(s)) { + gint n; - n = read(s->sock, buffer, size); - buffer[n] = '\0'; + n = read(s->sock, buffer, size); + buffer[n] = '\0'; + + return n; + } - return n; + return 0; } void sock_close(Socket * s) diff --git a/hardinfo2/socket.h b/hardinfo2/socket.h index db34c552..77938b3f 100644 --- a/hardinfo2/socket.h +++ b/hardinfo2/socket.h @@ -29,5 +29,6 @@ Socket *sock_connect(gchar * host, gint port); int sock_write(Socket * s, gchar * str); int sock_read(Socket * s, gchar * buffer, gint size); void sock_close(Socket * s); +int sock_is_ready(Socket *s); #endif /* __HI_SOCKET_H__ */ |