1*33400f82Sfgsch /* $OpenBSD: setsockopt3a.c,v 1.6 2012/02/22 20:33:51 fgsch Exp $ */
2ab66dad1Sfgsch /*
3ab66dad1Sfgsch * Federico G. Schwindt <fgsch@openbsd.org>, 2009. Public Domain.
4ab66dad1Sfgsch */
5ab66dad1Sfgsch
6ab66dad1Sfgsch #include <sys/types.h>
7*33400f82Sfgsch #include <sys/time.h>
8ab66dad1Sfgsch #include <sys/socket.h>
9ab66dad1Sfgsch #include <netinet/in.h>
10ab66dad1Sfgsch #include <err.h>
11ab66dad1Sfgsch #include <fcntl.h>
12ab66dad1Sfgsch #include <netdb.h>
13ab66dad1Sfgsch #include <stdlib.h>
14ab66dad1Sfgsch #include <string.h>
15ab66dad1Sfgsch #include <unistd.h>
16ab66dad1Sfgsch #include "test.h"
17ab66dad1Sfgsch
18*33400f82Sfgsch /* resolution of the monotonic clock */
19*33400f82Sfgsch struct timespec mono_res;
20*33400f82Sfgsch
21ab66dad1Sfgsch static void
alarm_handler(int sig)22ab66dad1Sfgsch alarm_handler(int sig)
23ab66dad1Sfgsch {
24ab66dad1Sfgsch _exit(NOTOK);
25ab66dad1Sfgsch }
26ab66dad1Sfgsch
27eb7aa524Sfgsch void
check_timeout(int s,int sec,const struct timespec * to)28*33400f82Sfgsch check_timeout(int s, int sec, const struct timespec *to)
29ab66dad1Sfgsch {
30*33400f82Sfgsch struct timespec t1, t2, e;
31ab66dad1Sfgsch char buf[BUFSIZ];
32ab66dad1Sfgsch
33ab66dad1Sfgsch ASSERT(signal(SIGALRM, alarm_handler) != SIG_ERR);
34ab66dad1Sfgsch CHECKe(alarm(sec));
35*33400f82Sfgsch CHECKe(clock_gettime(CLOCK_MONOTONIC, &t1));
36ab66dad1Sfgsch ASSERT(read(s, &buf, sizeof(buf)) == -1);
37*33400f82Sfgsch CHECKe(clock_gettime(CLOCK_MONOTONIC, &t2));
38ab66dad1Sfgsch ASSERT(errno == EAGAIN);
39*33400f82Sfgsch timespecsub(&t2, &t1, &e);
40*33400f82Sfgsch
41*33400f82Sfgsch /*
42*33400f82Sfgsch * verify that the difference between the duration and the
43*33400f82Sfgsch * timeout is less than the resolution of the clock
44*33400f82Sfgsch */
45*33400f82Sfgsch if (timespeccmp(&e, to, <))
46*33400f82Sfgsch timespecsub(to, &e, &t1);
47*33400f82Sfgsch else
48*33400f82Sfgsch timespecsub(&e, to, &t1);
49*33400f82Sfgsch ASSERT(timespeccmp(&t1, &mono_res, <=));
50ab66dad1Sfgsch }
51ab66dad1Sfgsch
52ab66dad1Sfgsch static void *
sock_accept(void * arg)53ab66dad1Sfgsch sock_accept(void *arg)
54ab66dad1Sfgsch {
55ab66dad1Sfgsch struct sockaddr_in sin;
56ab66dad1Sfgsch struct timeval to;
57*33400f82Sfgsch struct timespec ts;
58ab66dad1Sfgsch int s, s2, s3;
59ab66dad1Sfgsch
60*33400f82Sfgsch CHECKe(clock_getres(CLOCK_MONOTONIC, &mono_res));
61ab66dad1Sfgsch CHECKe(s = strtol(arg, NULL, 10));
62ab66dad1Sfgsch bzero(&sin, sizeof(sin));
63ab66dad1Sfgsch sin.sin_family = AF_INET;
64ab66dad1Sfgsch sin.sin_len = sizeof(sin);
65*33400f82Sfgsch sin.sin_port = htons(6545);
66ab66dad1Sfgsch sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
67ab66dad1Sfgsch CHECKe(connect(s, (struct sockaddr *)&sin, sizeof(sin)));
68ab66dad1Sfgsch to.tv_sec = 2;
69ab66dad1Sfgsch to.tv_usec = 0.5 * 1e6;
70*33400f82Sfgsch TIMEVAL_TO_TIMESPEC(&to, &ts);
71*33400f82Sfgsch check_timeout(s, 3, &ts);
72ab66dad1Sfgsch CHECKe(s2 = dup(s));
73ab66dad1Sfgsch CHECKe(s3 = fcntl(s, F_DUPFD, s));
74*33400f82Sfgsch check_timeout(s2, 3, &ts);
75*33400f82Sfgsch check_timeout(s3, 3, &ts);
76ab66dad1Sfgsch return (NULL);
77ab66dad1Sfgsch }
78ab66dad1Sfgsch
79ab66dad1Sfgsch int
main(int argc,char ** argv)80ab66dad1Sfgsch main(int argc, char **argv)
81ab66dad1Sfgsch {
82ab66dad1Sfgsch pthread_t accept_thread;
83ab66dad1Sfgsch
84ab66dad1Sfgsch if (argc != 2)
85ab66dad1Sfgsch exit(NOTOK);
86ab66dad1Sfgsch CHECKr(pthread_create(&accept_thread, NULL, sock_accept, argv[1]));
87ab66dad1Sfgsch CHECKr(pthread_join(accept_thread, NULL));
88ab66dad1Sfgsch SUCCEED;
89ab66dad1Sfgsch }
90