]>
git.saurik.com Git - apple/xnu.git/blob - tests/kqueue_fifo_18776047.c
2 * testname: kqueue_fifo
5 #include <darwintest.h>
14 #include <TargetConditionals.h>
16 #define TMP_FILE_PATH "/tmp/test_kqueue_fifo_18776047"
18 #define READ_BUFFER_LEN 256
21 #define TOTAL_ITERATIONS 5000
23 #define TOTAL_ITERATIONS 10000
27 int write_some_data(int fd
);
28 int read_data(int fd
);
29 void create_fifo(const char * filepath
);
30 void kevent_one_shot(int kq
, int fd
, int filter
);
33 write_some_data(int fd
)
38 char * data
= "ABCDE";
41 retval
= (int)write(fd
, data
, (size_t)len
);
43 if (errno
== EAGAIN
) {
50 T_ASSERT_FAIL("write to fd %d of %s of len %d failed.", fd
, data
, len
);
62 int retval
, count
= 0;
63 char databuffer
[READ_BUFFER_LEN
];
66 retval
= (int)read(fd
, databuffer
, READ_BUFFER_LEN
);
68 if (errno
== EAGAIN
) {
71 T_ASSERT_FAIL("read from fd %d failed.", fd
);
80 create_fifo(const char * filepath
)
85 ret
= stat(filepath
, &f_stat
);
87 /* if file exists, make sure its a fifo */
88 T_ASSERT_TRUE(S_ISFIFO(f_stat
.st_mode
), "ensure %s is a fifo", filepath
);
89 } else if (errno
== ENOENT
) {
90 ret
= mkfifo(filepath
, 0777);
91 T_ASSERT_POSIX_ZERO(ret
, "creating a fifo at path %s", filepath
);
93 T_ASSERT_FAIL("stat operation on %s", filepath
);
98 kevent_one_shot(int kq
, int fd
, int filter
)
101 struct timespec t_zero
= {0, 0};
102 struct kevent kev
[1];
105 T_ASSERT_GE(kq
, 0, "ensure kq is valid");
106 T_LOG("kevent doing ONESHOT %s", filter
== EVFILT_READ
? "read" : "write");
108 EV_SET(kev
, fd
, filter
, EV_ADD
| EV_ONESHOT
, 0, 0, NULL
);
109 retval
= kevent(kq
, kev
, 1, NULL
, 0, &t_zero
);
111 T_ASSERT_POSIX_ZERO(retval
, "ONESHOT kevent for fd %d, filter %d", fd
, filter
);
114 T_DECL(kqueue_fifo_18776047
, "Tests kqueue, kevent for watching a fifo.", T_META_LTEPHASE(LTE_POSTINIT
))
116 struct kevent kev
[1];
117 int read_fd
, write_fd
, kq
;
120 const char * fpath
= TMP_FILE_PATH
;
125 T_ASSERT_GE(kq
, 0, "create a kqueue");
127 read_fd
= open(fpath
, O_RDONLY
| O_APPEND
| O_NONBLOCK
);
128 T_ASSERT_POSIX_SUCCESS(read_fd
, "opening read fd on fifo.");
130 write_fd
= open(fpath
, O_WRONLY
| O_APPEND
| O_NONBLOCK
);
131 T_ASSERT_POSIX_SUCCESS(write_fd
, "opening write fd on fifo.");
135 kevent_one_shot(kq
, write_fd
, EVFILT_WRITE
);
136 kevent_one_shot(kq
, read_fd
, EVFILT_READ
);
138 while (iter
++ < TOTAL_ITERATIONS
) {
139 retval
= kevent(kq
, NULL
, 0, kev
, 1, NULL
);
141 T_ASSERT_GE(retval
, 0, "kevent on kq %d", kq
);
143 if (kev
[0].ident
== (uintptr_t)write_fd
) {
144 retval
= write_some_data(write_fd
);
145 T_LOG("writer ready iter: %d wrote %d bytes", iter
, retval
);
146 kevent_one_shot(kq
, write_fd
, EVFILT_WRITE
);
147 } else if (kev
[0].ident
== (uintptr_t)read_fd
) {
148 retval
= read_data(read_fd
);
149 T_LOG("reader ready iter: %d read %d bytes", iter
, retval
);
150 kevent_one_shot(kq
, read_fd
, EVFILT_READ
);
153 T_PASS("kqueue_fifo_18776047 PASSED");