1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/* $OpenBSD: poll.c,v 1.3 2001/09/20 16:43:15 todd Exp $ */
/* David Leonard <d@openbsd.org>, 2001. Public Domain. */
#include <pthread.h>
#include <fcntl.h>
#include <poll.h>
#include <paths.h>
#include <unistd.h>
#include <stdlib.h>
#include "test.h"
#define POLLALL (POLLIN|POLLOUT|POLLERR|POLLNVAL)
static void
print_pollfd(p)
struct pollfd *p;
{
printf("{fd=%d, events=< %s%s%s> revents=< %s%s%s%s%s>}",
p->fd,
p->events & POLLIN ? "in " : "",
p->events & POLLOUT ? "out " : "",
p->events & ~(POLLIN|POLLOUT) ? "XXX " : "",
p->revents & POLLIN ? "in " : "",
p->revents & POLLOUT ? "out " : "",
p->revents & POLLERR ? "err " : "",
p->revents & POLLHUP ? "hup " : "",
p->revents & POLLNVAL ? "nval " : ""
);
}
static void *
writer(arg)
void *arg;
{
int fd = *(int *)arg;
const char msg[1] = { '!' };
ASSERTe(write(fd, &msg, sizeof msg), == sizeof msg);
return NULL;
}
static void *
reader(arg)
void *arg;
{
int fd = *(int *)arg;
char buf[1];
ASSERTe(read(fd, &buf, sizeof buf), == sizeof buf);
return NULL;
}
int
main(argc, argv)
int argc;
char **argv;
{
pthread_t t;
void *result;
int null, zero, tty;
int tube[2];
struct pollfd p[3];
/* Try an empty poll set */
ASSERTe(poll(NULL, 0, 0), == 0);
CHECKe(zero = open(_PATH_DEV "zero", O_RDONLY));
CHECKe(null = open(_PATH_DEV "null", O_WRONLY));
CHECKe(tty = open(_PATH_DEV "tty", O_WRONLY));
/* Try both descriptors being ready */
p[0].fd = zero;
p[0].events = POLLIN|POLLOUT;
p[0].revents = 0;
p[1].fd = null;
p[1].events = POLLIN|POLLOUT;
p[1].revents = 0;
ASSERTe(poll(p, 2, 0), == 2); /* if 4 then bug in kernel not fixed */
printf("zero p[0]="); print_pollfd(&p[0]); putchar('\n');
printf("null p[1]="); print_pollfd(&p[1]); putchar('\n');
ASSERT((p[0].revents & POLLIN) == POLLIN);
ASSERT((p[1].revents & POLLOUT) == POLLOUT);
/*
* Try one of the descriptors being invalid
* and the other ready
*/
printf("closing zero\n");
close(zero);
p[0].fd = zero;
p[0].events = POLLIN|POLLOUT;
p[1].fd = null;
p[1].events = POLLIN|POLLOUT;
ASSERTe(poll(p, 2, 0), == 2); /* again, old kernels had this bug */
printf("zero p[0]="); print_pollfd(&p[0]); putchar('\n');
printf("null p[1]="); print_pollfd(&p[1]); putchar('\n');
ASSERT((p[0].revents & POLLNVAL) == POLLNVAL);
ASSERT((p[1].revents & POLLOUT) == POLLOUT);
printf("closing null\n");
close(null);
/*
* New pipes. the write end should be writable (buffered)
*/
CHECKe(pipe(tube));
CHECKe(fcntl(tube[0], F_SETFL, O_NONBLOCK));
CHECKe(fcntl(tube[1], F_SETFL, O_NONBLOCK));
p[0].fd = tube[0];
p[0].events = POLLIN;
p[1].fd = tube[1];
p[1].events = POLLOUT;
ASSERTe(poll(p, 2, 0), == 1);
printf("rpipe p[0]="); print_pollfd(&p[0]); putchar('\n');
printf("wpipe p[1]="); print_pollfd(&p[1]); putchar('\n');
ASSERT(p[0].revents == 0);
ASSERT(p[1].revents == POLLOUT);
/* Start a writing thread to the write end [1] */
printf("bg writing to wpipe\n");
CHECKr(pthread_create(&t, NULL, writer, (void *)&tube[1]));
/* The read end [0] should soon be ready for read (POLLIN) */
p[0].fd = tube[0];
p[0].events = POLLIN;
ASSERTe(poll(p, 1, INFTIM), == 1);
printf("rpipe p[0]="); print_pollfd(&p[0]); putchar('\n');
ASSERT(p[0].revents == POLLIN);
reader((void *)&tube[0]); /* consume */
CHECKr(pthread_join(t, &result));
SUCCEED;
}
|