diff options
author | Federico G. Schwindt <fgsch@cvs.openbsd.org> | 2012-02-26 21:08:07 +0000 |
---|---|---|
committer | Federico G. Schwindt <fgsch@cvs.openbsd.org> | 2012-02-26 21:08:07 +0000 |
commit | 6f149871a168c7e45dd4d5c0788112458155d1f3 (patch) | |
tree | 0d8acdcb016d20dc4438a23588a42be527cda248 | |
parent | 628c511979cd7df4036d99ea9e46b4501b2a3027 (diff) |
similar change to the setsockopt1.c one: use the resolution of the monotonic
clock from clock_getres(). while here use a different port when binding.
-rw-r--r-- | regress/lib/libpthread/setsockopt/2/setsockopt2.c | 55 |
1 files changed, 35 insertions, 20 deletions
diff --git a/regress/lib/libpthread/setsockopt/2/setsockopt2.c b/regress/lib/libpthread/setsockopt/2/setsockopt2.c index 3e38b5a0021..e7cc8c7967b 100644 --- a/regress/lib/libpthread/setsockopt/2/setsockopt2.c +++ b/regress/lib/libpthread/setsockopt/2/setsockopt2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: setsockopt2.c,v 1.4 2012/02/20 01:31:12 fgsch Exp $ */ +/* $OpenBSD: setsockopt2.c,v 1.5 2012/02/26 21:08:06 fgsch Exp $ */ /* * Federico G. Schwindt <fgsch@openbsd.org>, 2009. Public Domain. */ @@ -6,6 +6,7 @@ #include <sys/types.h> #include <sys/socket.h> #include <sys/wait.h> +#include <sys/time.h> #include <netinet/in.h> #include <err.h> #include <fcntl.h> @@ -15,6 +16,9 @@ #include <unistd.h> #include "test.h" +/* resolution of the monotonic clock */ +struct timespec mono_res; + static void alarm_handler(int sig) { @@ -22,22 +26,28 @@ alarm_handler(int sig) } void -check_timeout(int s, int sec, struct timeval *to) +check_timeout(int s, int sec, struct timespec *to) { - struct timeval t1, t2; - struct timeval e; + struct timespec t1, t2, e; char buf[BUFSIZ]; ASSERT(signal(SIGALRM, alarm_handler) != SIG_ERR); CHECKe(alarm(sec)); - CHECKe(gettimeofday(&t1, NULL)); + CHECKe(clock_gettime(CLOCK_MONOTONIC, &t1)); ASSERT(read(s, &buf, sizeof(buf)) == -1); - CHECKe(gettimeofday(&t2, NULL)); + CHECKe(clock_gettime(CLOCK_MONOTONIC, &t2)); ASSERT(errno == EAGAIN); - timersub(&t2, &t1, &e); - if (!timercmp(&e, to, <)) - PANIC("%ld.%ld > %ld.%ld", - e.tv_sec, e.tv_usec, to->tv_sec, to->tv_usec); + timespecsub(&t2, &t1, &e); + + /* + * verify that the difference between the duration and the + * timeout is less than the resolution of the clock + */ + if (timespeccmp(&e, to, <)) + timespecsub(to, &e, &t1); + else + timespecsub(&e, to, &t1); + ASSERT(timespeccmp(&t1, &mono_res, <=)); } static void * @@ -45,40 +55,45 @@ sock_connect(void *arg) { struct sockaddr_in sin; struct timeval to; + struct timespec ts; pid_t child_pid; int status; int s, s2, s3; + CHECKe(clock_getres(CLOCK_MONOTONIC, &mono_res)); CHECKe(s = socket(AF_INET, SOCK_STREAM, 0)); CHECKe(s2 = dup(s)); CHECKe(s3 = fcntl(s, F_DUPFD, s)); bzero(&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_len = sizeof(sin); - sin.sin_port = htons(6543); + sin.sin_port = htons(6544); sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); CHECKe(connect(s, (struct sockaddr *)&sin, sizeof(sin))); to.tv_sec = 2; to.tv_usec = 0.5 * 1e6; + TIMEVAL_TO_TIMESPEC(&to, &ts); CHECKe(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to))); CHECKe(child_pid = fork()); if (child_pid == 0) { to.tv_sec = 1; to.tv_usec = 0.5 * 1e6; + TIMEVAL_TO_TIMESPEC(&to, &ts); CHECKe(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to))); - check_timeout(s, 2, &to); - check_timeout(s2, 2, &to); - check_timeout(s3, 2, &to); + check_timeout(s, 2, &ts); + check_timeout(s2, 2, &ts); + check_timeout(s3, 2, &ts); return (NULL); } sleep(2); - check_timeout(s, 2, &to); - check_timeout(s2, 2, &to); - check_timeout(s3, 2, &to); + ts.tv_sec = 1; /* the fd timeout was changed in the child */ + check_timeout(s, 2, &ts); + check_timeout(s2, 2, &ts); + check_timeout(s3, 2, &ts); CHECKe(s2 = dup(s)); CHECKe(s3 = fcntl(s, F_DUPFD, s)); - check_timeout(s2, 2, &to); - check_timeout(s3, 2, &to); + check_timeout(s2, 2, &ts); + check_timeout(s3, 2, &ts); CHECKe(close(s)); CHECKe(close(s2)); CHECKe(close(s3)); @@ -99,7 +114,7 @@ sock_accept(void *arg) bzero(&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_len = sizeof(sin); - sin.sin_port = htons(6543); + sin.sin_port = htons(6544); sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); CHECKe(bind(s, (struct sockaddr *)&sin, sizeof(sin))); CHECKe(listen(s, 2)); |