Try to make indent formatting less horrible
[oweals/busybox.git] / util-linux / rdate.c
index 3c3b152a22657462375cd673a4d479255fb6c0cc..8d156cc783835643a10ec5781d96207051cff0d0 100644 (file)
 #include <time.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <signal.h>
+
 #include "busybox.h"
 
 
 static const int RFC_868_BIAS = 2208988800UL;
 
+static void socket_timeout(int sig)
+{
+       bb_error_msg_and_die("timeout connecting to time server");
+}
+
 static time_t askremotedate(const char *host)
 {
        unsigned long int nett, localt;
@@ -46,9 +53,13 @@ static time_t askremotedate(const char *host)
        if (getservbyname("time", "tcp") != NULL)
                port="time";
 
+       /* Add a timeout for dead or non accessable servers */
+       alarm(10);
+       signal(SIGALRM, socket_timeout);
+
        fd = xconnect(host, port);
 
-       if (read(fd, (void *)&nett, 4) != 4)    /* read time from server */
+       if (safe_read(fd, (void *)&nett, 4) != 4)    /* read time from server */
                bb_error_msg_and_die("%s did not send the complete time", host);
 
        close(fd);