]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
use select() instead of alarm for better portability
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Thu, 11 Dec 2014 17:59:27 +0000 (18:59 +0100)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Thu, 11 Dec 2014 17:59:30 +0000 (18:59 +0100)
Based on patch by Eli Zaretskii.

src/socket.c

index d2a3e905a7065734cb0ef290b8a43a08932c7263..a1b8b7b293450ada985ef850e465e9c92ae1a856 100644 (file)
@@ -140,10 +140,19 @@ ssize_t wait_for_text(int fd, const char *txt, unsigned txt_size)
        char buf[512];
        char *p;
        int ret;
+       fd_set read_fds;
+       struct timeval tv;
 
-       alarm(10);
        do {
-               ret = recv(fd, buf, sizeof(buf)-1, 0);
+               FD_ZERO(&read_fds);
+               FD_SET(fd, &read_fds);
+               tv.tv_sec = 10;
+               tv.tv_usec = 0;
+               ret = select(fd + 1, &read_fds, NULL, NULL, &tv);
+               if (ret <= 0)
+                       ret = -1;
+               else
+                       ret = recv(fd, buf, sizeof(buf)-1, 0);
                if (ret == -1) {
                        fprintf(stderr, "error receiving %s\n", txt);
                        exit(1);
@@ -158,8 +167,6 @@ ssize_t wait_for_text(int fd, const char *txt, unsigned txt_size)
                }
        } while(ret < (int)txt_size || strncmp(buf, txt, txt_size) != 0);
 
-       alarm(0);
-
        return ret;
 }