socket/socketpair: Add SOCK_{NONBLOCK,CLOEXEC} support.
[dragonfly.git] / test / sockext / socketpair / nonblock / sockpair_nblock.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <sys/time.h>
4 #include <sys/un.h>
5
6 #include <err.h>
7 #include <errno.h>
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #define READ_BLOCK_TIME         5       /* unit: sec */
15
16 static void
17 sig_alarm(int sig __unused)
18 {
19 #define PANIC_STRING    "read blocks\n"
20
21         write(2, PANIC_STRING, strlen(PANIC_STRING));
22         abort();
23 }
24
25 int
26 main(void)
27 {
28         struct itimerval it;
29         int s[2], n, error;
30         char buf;
31
32         if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, s) < 0)
33                 err(1, "socketpair failed");
34
35         memset(&it, 0, sizeof(it));
36         it.it_value.tv_sec = READ_BLOCK_TIME;
37         if (signal(SIGALRM, sig_alarm) == SIG_ERR)
38                 err(1, "signal failed");
39         if (setitimer(ITIMER_REAL, &it, NULL) < 0)
40                 err(1, "setitimer failed");
41
42         n = read(s[0], &buf, 1);
43         if (n < 0) {
44                 error = errno;
45                 if (error != EAGAIN) {
46                         fprintf(stderr, "invalid errno %d\n", error);
47                         abort();
48                 }
49         } else {
50                 fprintf(stderr, "read0 works\n");
51                 abort();
52         }
53
54         n = read(s[1], &buf, 1);
55         if (n < 0) {
56                 error = errno;
57                 if (error != EAGAIN) {
58                         fprintf(stderr, "invalid errno %d\n", error);
59                         abort();
60                 }
61         } else {
62                 fprintf(stderr, "read1 works\n");
63                 abort();
64         }
65
66         fprintf(stderr, "passed\n");
67         exit(0);
68 }