1*fe017454Sfgsch /* $OpenBSD: setsockopt2.c,v 1.5 2012/02/26 21:08:06 fgsch Exp $ */
2ab66dad1Sfgsch /*
3ab66dad1Sfgsch * Federico G. Schwindt <fgsch@openbsd.org>, 2009. Public Domain.
4ab66dad1Sfgsch */
5ab66dad1Sfgsch
6ab66dad1Sfgsch #include <sys/types.h>
7ab66dad1Sfgsch #include <sys/socket.h>
8ab66dad1Sfgsch #include <sys/wait.h>
9*fe017454Sfgsch #include <sys/time.h>
10ab66dad1Sfgsch #include <netinet/in.h>
11ab66dad1Sfgsch #include <err.h>
12ab66dad1Sfgsch #include <fcntl.h>
13ab66dad1Sfgsch #include <netdb.h>
14ab66dad1Sfgsch #include <stdlib.h>
15ab66dad1Sfgsch #include <string.h>
16ab66dad1Sfgsch #include <unistd.h>
17ab66dad1Sfgsch #include "test.h"
18ab66dad1Sfgsch
19*fe017454Sfgsch /* resolution of the monotonic clock */
20*fe017454Sfgsch struct timespec mono_res;
21*fe017454Sfgsch
22ab66dad1Sfgsch static void
alarm_handler(int sig)23ab66dad1Sfgsch alarm_handler(int sig)
24ab66dad1Sfgsch {
25ab66dad1Sfgsch _exit(NOTOK);
26ab66dad1Sfgsch }
27ab66dad1Sfgsch
28eb7aa524Sfgsch void
check_timeout(int s,int sec,struct timespec * to)29*fe017454Sfgsch check_timeout(int s, int sec, struct timespec *to)
30ab66dad1Sfgsch {
31*fe017454Sfgsch struct timespec t1, t2, e;
32ab66dad1Sfgsch char buf[BUFSIZ];
33ab66dad1Sfgsch
34ab66dad1Sfgsch ASSERT(signal(SIGALRM, alarm_handler) != SIG_ERR);
35ab66dad1Sfgsch CHECKe(alarm(sec));
36*fe017454Sfgsch CHECKe(clock_gettime(CLOCK_MONOTONIC, &t1));
37ab66dad1Sfgsch ASSERT(read(s, &buf, sizeof(buf)) == -1);
38*fe017454Sfgsch CHECKe(clock_gettime(CLOCK_MONOTONIC, &t2));
39ab66dad1Sfgsch ASSERT(errno == EAGAIN);
40*fe017454Sfgsch timespecsub(&t2, &t1, &e);
41*fe017454Sfgsch
42*fe017454Sfgsch /*
43*fe017454Sfgsch * verify that the difference between the duration and the
44*fe017454Sfgsch * timeout is less than the resolution of the clock
45*fe017454Sfgsch */
46*fe017454Sfgsch if (timespeccmp(&e, to, <))
47*fe017454Sfgsch timespecsub(to, &e, &t1);
48*fe017454Sfgsch else
49*fe017454Sfgsch timespecsub(&e, to, &t1);
50*fe017454Sfgsch ASSERT(timespeccmp(&t1, &mono_res, <=));
51ab66dad1Sfgsch }
52ab66dad1Sfgsch
53ab66dad1Sfgsch static void *
sock_connect(void * arg)54ab66dad1Sfgsch sock_connect(void *arg)
55ab66dad1Sfgsch {
56ab66dad1Sfgsch struct sockaddr_in sin;
57ab66dad1Sfgsch struct timeval to;
58*fe017454Sfgsch struct timespec ts;
59ab66dad1Sfgsch pid_t child_pid;
60ab66dad1Sfgsch int status;
61ab66dad1Sfgsch int s, s2, s3;
62ab66dad1Sfgsch
63*fe017454Sfgsch CHECKe(clock_getres(CLOCK_MONOTONIC, &mono_res));
64ab66dad1Sfgsch CHECKe(s = socket(AF_INET, SOCK_STREAM, 0));
65ab66dad1Sfgsch CHECKe(s2 = dup(s));
66ab66dad1Sfgsch CHECKe(s3 = fcntl(s, F_DUPFD, s));
67ab66dad1Sfgsch bzero(&sin, sizeof(sin));
68ab66dad1Sfgsch sin.sin_family = AF_INET;
69ab66dad1Sfgsch sin.sin_len = sizeof(sin);
70*fe017454Sfgsch sin.sin_port = htons(6544);
71ab66dad1Sfgsch sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
72ab66dad1Sfgsch CHECKe(connect(s, (struct sockaddr *)&sin, sizeof(sin)));
73ab66dad1Sfgsch to.tv_sec = 2;
74ab66dad1Sfgsch to.tv_usec = 0.5 * 1e6;
75*fe017454Sfgsch TIMEVAL_TO_TIMESPEC(&to, &ts);
76ab66dad1Sfgsch CHECKe(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to)));
77ab66dad1Sfgsch CHECKe(child_pid = fork());
78ab66dad1Sfgsch if (child_pid == 0) {
79ab66dad1Sfgsch to.tv_sec = 1;
80ab66dad1Sfgsch to.tv_usec = 0.5 * 1e6;
81*fe017454Sfgsch TIMEVAL_TO_TIMESPEC(&to, &ts);
82ab66dad1Sfgsch CHECKe(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to)));
83*fe017454Sfgsch check_timeout(s, 2, &ts);
84*fe017454Sfgsch check_timeout(s2, 2, &ts);
85*fe017454Sfgsch check_timeout(s3, 2, &ts);
86ab66dad1Sfgsch return (NULL);
87ab66dad1Sfgsch }
88ab66dad1Sfgsch sleep(2);
89*fe017454Sfgsch ts.tv_sec = 1; /* the fd timeout was changed in the child */
90*fe017454Sfgsch check_timeout(s, 2, &ts);
91*fe017454Sfgsch check_timeout(s2, 2, &ts);
92*fe017454Sfgsch check_timeout(s3, 2, &ts);
93ab66dad1Sfgsch CHECKe(s2 = dup(s));
94ab66dad1Sfgsch CHECKe(s3 = fcntl(s, F_DUPFD, s));
95*fe017454Sfgsch check_timeout(s2, 2, &ts);
96*fe017454Sfgsch check_timeout(s3, 2, &ts);
97ab66dad1Sfgsch CHECKe(close(s));
98ab66dad1Sfgsch CHECKe(close(s2));
99ab66dad1Sfgsch CHECKe(close(s3));
100ab66dad1Sfgsch ASSERTe(wait(&status), == child_pid);
101ab66dad1Sfgsch ASSERT(WIFEXITED(status));
102ab66dad1Sfgsch CHECKr(WEXITSTATUS(status));
103ab66dad1Sfgsch return (NULL);
104ab66dad1Sfgsch }
105ab66dad1Sfgsch
106ab66dad1Sfgsch static void *
sock_accept(void * arg)107ab66dad1Sfgsch sock_accept(void *arg)
108ab66dad1Sfgsch {
109ab66dad1Sfgsch pthread_t connect_thread;
110ab66dad1Sfgsch struct sockaddr_in sin;
111ab66dad1Sfgsch int s;
112ab66dad1Sfgsch
113ab66dad1Sfgsch CHECKe(s = socket(AF_INET, SOCK_STREAM, 0));
114ab66dad1Sfgsch bzero(&sin, sizeof(sin));
115ab66dad1Sfgsch sin.sin_family = AF_INET;
116ab66dad1Sfgsch sin.sin_len = sizeof(sin);
117*fe017454Sfgsch sin.sin_port = htons(6544);
118ab66dad1Sfgsch sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
119ab66dad1Sfgsch CHECKe(bind(s, (struct sockaddr *)&sin, sizeof(sin)));
120ab66dad1Sfgsch CHECKe(listen(s, 2));
121ab66dad1Sfgsch
122ab66dad1Sfgsch CHECKr(pthread_create(&connect_thread, NULL, sock_connect, NULL));
123ab66dad1Sfgsch CHECKr(pthread_join(connect_thread, NULL));
124ab66dad1Sfgsch return (NULL);
125ab66dad1Sfgsch }
126ab66dad1Sfgsch
127ab66dad1Sfgsch int
main(int argc,char ** argv)128ab66dad1Sfgsch main(int argc, char **argv)
129ab66dad1Sfgsch {
130ab66dad1Sfgsch pthread_t accept_thread;
131ab66dad1Sfgsch
132ab66dad1Sfgsch CHECKr(pthread_create(&accept_thread, NULL, sock_accept, NULL));
133ab66dad1Sfgsch CHECKr(pthread_join(accept_thread, NULL));
134ab66dad1Sfgsch SUCCEED;
135ab66dad1Sfgsch }
136