]>
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 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
18 #define TMP_FILE_PATH "/tmp/test_kqueue_fifo_18776047"
20 #define READ_BUFFER_LEN 256
23 #define TOTAL_ITERATIONS 5000
25 #define TOTAL_ITERATIONS 10000
29 int write_some_data(int fd
);
30 int read_data(int fd
);
31 void create_fifo(const char * filepath
);
32 void kevent_one_shot(int kq
, int fd
, int filter
);
35 write_some_data(int fd
)
40 char * data
= "ABCDE";
43 retval
= (int)write(fd
, data
, (size_t)len
);
45 if (errno
== EAGAIN
) {
52 T_ASSERT_FAIL("write to fd %d of %s of len %d failed.", fd
, data
, len
);
64 int retval
, count
= 0;
65 char databuffer
[READ_BUFFER_LEN
];
68 retval
= (int)read(fd
, databuffer
, READ_BUFFER_LEN
);
70 if (errno
== EAGAIN
) {
73 T_ASSERT_FAIL("read from fd %d failed.", fd
);
82 create_fifo(const char * filepath
)
87 ret
= stat(filepath
, &f_stat
);
89 /* if file exists, make sure its a fifo */
90 T_ASSERT_TRUE(S_ISFIFO(f_stat
.st_mode
), "ensure %s is a fifo", filepath
);
91 } else if (errno
== ENOENT
) {
92 ret
= mkfifo(filepath
, 0777);
93 T_ASSERT_POSIX_ZERO(ret
, "creating a fifo at path %s", filepath
);
95 T_ASSERT_FAIL("stat operation on %s", filepath
);
100 kevent_one_shot(int kq
, int fd
, int filter
)
103 struct timespec t_zero
= {0, 0};
104 struct kevent kev
[1];
107 T_ASSERT_GE(kq
, 0, "ensure kq is valid");
108 T_LOG("kevent doing ONESHOT %s", filter
== EVFILT_READ
? "read" : "write");
110 EV_SET(kev
, fd
, filter
, EV_ADD
| EV_ONESHOT
, 0, 0, NULL
);
111 retval
= kevent(kq
, kev
, 1, NULL
, 0, &t_zero
);
113 T_ASSERT_POSIX_ZERO(retval
, "ONESHOT kevent for fd %d, filter %d", fd
, filter
);
116 T_DECL(kqueue_fifo_18776047
, "Tests kqueue, kevent for watching a fifo.", T_META_LTEPHASE(LTE_POSTINIT
))
118 struct kevent kev
[1];
119 int read_fd
, write_fd
, kq
;
122 const char * fpath
= TMP_FILE_PATH
;
127 T_ASSERT_GE(kq
, 0, "create a kqueue");
129 read_fd
= open(fpath
, O_RDONLY
| O_APPEND
| O_NONBLOCK
);
130 T_ASSERT_POSIX_SUCCESS(read_fd
, "opening read fd on fifo.");
132 write_fd
= open(fpath
, O_WRONLY
| O_APPEND
| O_NONBLOCK
);
133 T_ASSERT_POSIX_SUCCESS(write_fd
, "opening write fd on fifo.");
137 kevent_one_shot(kq
, write_fd
, EVFILT_WRITE
);
138 kevent_one_shot(kq
, read_fd
, EVFILT_READ
);
140 while (iter
++ < TOTAL_ITERATIONS
) {
141 retval
= kevent(kq
, NULL
, 0, kev
, 1, NULL
);
143 T_ASSERT_GE(retval
, 0, "kevent on kq %d", kq
);
145 if (kev
[0].ident
== (uintptr_t)write_fd
) {
146 retval
= write_some_data(write_fd
);
147 T_LOG("writer ready iter: %d wrote %d bytes", iter
, retval
);
148 kevent_one_shot(kq
, write_fd
, EVFILT_WRITE
);
149 } else if (kev
[0].ident
== (uintptr_t)read_fd
) {
150 retval
= read_data(read_fd
);
151 T_LOG("reader ready iter: %d read %d bytes", iter
, retval
);
152 kevent_one_shot(kq
, read_fd
, EVFILT_READ
);
155 T_PASS("kqueue_fifo_18776047 PASSED");