2 * Copyright 2009 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* Helper to test linux pipe's */
25 #include <sys/socket.h>
27 static void print_events(int events) {
28 if (events & POLLIN) printf("POLLIN ");
29 if (events & POLLPRI) printf("POLLPRI ");
30 if (events & POLLOUT) printf("POLLOUT ");
31 if (events & POLLERR) printf("POLLERR ");
32 if (events & POLLHUP) printf("POLLHUP ");
33 if (events & POLLNVAL) printf("POLLNVAL ");
37 static int _socketpair(int fd[2]) {
39 printf("%d: socketpair()\n", gettid());
40 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
41 printf("%d: socketpair() = %d\n", gettid(), ret);
42 if (ret) printf("\terr %d (%s)\n", errno, strerror(errno));
46 static int _close(int fd) {
48 printf("%d: close(%d)\n", gettid(), fd);
50 printf("%d: close(%d) = %d\n", gettid(), fd, ret);
51 if (ret) printf("\terr %d (%s)\n", errno, strerror(errno));
55 static int _poll(struct pollfd *ufds, nfds_t nfds, int timeout) {
58 printf("%d: poll()\n", gettid());
59 ret = poll(ufds, nfds, timeout);
60 printf("%d: poll() = %d\n", gettid(), ret);
61 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
63 for (i=0; i<nfds; i++) {
64 if (ufds[i].revents) {
65 printf("\tfd %d ", ufds[i].fd); print_events(ufds[i].revents);
72 static int _write(int fd, char *buf, int len) {
75 printf("%d: write(%d)\n", gettid(), fd);
76 ret = write(fd, buf, len);
77 printf("%d: write(%d) = %d\n", gettid(), fd, ret);
78 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
83 static int _read(int fd) {
87 printf("%d: read(%d)\n", gettid(), fd);
88 ret = read(fd, &buf, 1);
89 printf("%d: read(%d) = %d [%d]\n", gettid(), fd, ret, (int)buf);
90 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
95 static int _shutdown(int fd, int how) {
98 printf("%d: shutdown(%d)\n", gettid(), fd);
99 ret = shutdown(fd, how);
100 printf("%d: shutdown(%d) = %d\n", gettid(), fd, ret);
101 if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
105 static void thread_poll(void *args) {
108 printf("%d: START\n", gettid());
112 printf("%d: END\n", gettid());
115 static void thread_pollin(void *args) {
118 printf("%d: START\n", gettid());
122 printf("%d: END\n", gettid());
125 static void thread_pollin_rand_delay(void *args) {
128 int delay = (int)((double)random() * (10000000.0 / 2147483647.0));
129 printf("%d: START (delay = %d)\n", gettid(), delay);
134 printf("%d: END\n", gettid());
137 static void thread_read(void *args) {
139 printf("%d: START\n", gettid());
141 printf("%d: END\n", gettid());
144 static void thread_close(void *args) {
146 printf("%d: START\n", gettid());
148 printf("%d: END\n", gettid());
151 static int do_poll_poll_close() {
156 if (pipe(fd)) return -1;
158 pthread_create(&t1, NULL, (void *)thread_poll, NULL);
159 pthread_create(&t2, NULL, (void *)thread_poll, NULL);
165 pthread_join(t1, NULL);
166 pthread_join(t2, NULL);
171 static int do_socketpair_poll1_shutdown2() {
175 if (_socketpair(fd)) return -1;
177 pthread_create(&t, NULL, (void *)thread_poll, (void *)fd[1]);
181 _shutdown(fd[0], SHUT_RDWR);
187 pthread_join(t, NULL);
192 static int do_socketpair_poll1_shutdown1() {
196 if (_socketpair(fd)) return -1;
198 pthread_create(&t, NULL, (void *)thread_poll, (void *)fd[0]);
202 _shutdown(fd[0], SHUT_RDWR);
208 pthread_join(t, NULL);
213 static int do_socketpair_poll1_close1() {
217 if (_socketpair(fd)) return -1;
219 pthread_create(&t, NULL, (void *)thread_poll, (void *)fd[0]);
225 pthread_join(t, NULL);
230 static int do_socketpair_read1_shutdown1() {
234 if (_socketpair(fd)) return -1;
236 pthread_create(&t, NULL, (void *)thread_read, (void *)fd[0]);
240 _shutdown(fd[0], SHUT_RDWR);
246 pthread_join(t, NULL);
251 static int do_pipe_pipe_pipe() {
257 printf("pipe: %s\n", strerror(errno));
260 printf("%d %d\n", fd[0], fd[1]);
267 static int do_pollin_pollin_write() {
274 if (pipe(fd)) return -1;
276 pthread_create(&t1, NULL, (void *)thread_pollin, (void *)fd[0]);
277 pthread_create(&t2, NULL, (void *)thread_pollin, (void *)fd[0]);
281 for (i = 0; i < 100; i++)
282 _write(fd[1], &buf, 1);
284 pthread_join(t1, NULL);
285 pthread_join(t2, NULL);
290 static int do_pollin_pollin_pollin_write_pollin_pollin_pollin() {
291 const int MAX_T = 10;
297 if (pipe(fd)) return -1;
299 for (i=0; i<MAX_T; i++)
300 pthread_create(&t[i], NULL, (void *)thread_pollin_rand_delay, (void *)fd[0]);
304 _write(fd[1], &buf, 1);
306 for (i=0; i<MAX_T; i++)
307 pthread_join(t[i], NULL);
315 static int do_poll_poll_shutdown() {
321 if (pipe(fd)) return -1;
323 pthread_create(&t1, NULL, (void *)thread_poll, (void *)fd[0]);
324 pthread_create(&t2, NULL, (void *)thread_poll, (void *)fd[0]);
328 _shutdown(fd[1], SHUT_RDWR);
330 pthread_join(t1, NULL);
331 pthread_join(t2, NULL);
337 static int THREADS = 100;
339 static int do_close_poll_poll_poll() {
340 pthread_t t[THREADS];
344 if (pipe(fd)) return -1;
348 for (i = 0; i < THREADS; i++)
349 pthread_create(&t[i], NULL, (void *)thread_poll, (void *)fd[0]);
351 for (i = 0; i < THREADS; i++)
352 pthread_join(t[i], NULL);
357 static int do_close_close_close() {
358 pthread_t t[THREADS];
362 if (pipe(fd)) return -1;
364 for (i = 0; i < THREADS; i++)
365 pthread_create(&t[i], NULL, (void *)thread_close, (void *)fd[i%2]);
370 static int pipe_close_w_close_r_repeat() {
375 for (i = 0; i < THREADS; i++) {
376 if (pipe(fd)) return -1;
377 pthread_create(&t, NULL, (void *)thread_poll, (void *)fd[0]);
380 pthread_join(t, NULL);
389 } function_table[] = {
390 {"socketpair_poll1_shutdown2", do_socketpair_poll1_shutdown2},
391 {"socketpair_poll1_shutdown1", do_socketpair_poll1_shutdown1},
392 {"socketpair_poll1_close1", do_socketpair_poll1_close1},
393 {"socketpair_read1_shutdown1", do_socketpair_read1_shutdown1},
394 {"pipe_pipe_pipe", do_pipe_pipe_pipe},
395 {"poll_poll_close", do_poll_poll_close},
396 {"pollin_pollin_write", do_pollin_pollin_write},
397 {"pollin_pollin_pollin_write_pollin_pollin_pollin", do_pollin_pollin_pollin_write_pollin_pollin_pollin},
398 {"poll_poll_shutdown", do_poll_poll_shutdown},
399 {"close_poll_poll_poll", do_close_poll_poll_poll},
400 {"close_close_close", do_close_close_close},
401 {"pipe_close_w_close_w_repeat", pipe_close_w_close_r_repeat},
405 static void usage() {
409 for (i = 0; function_table[i].name; i++) {
410 printf("\tpipetest %s\n", function_table[i].name);
414 int main(int argc, char **argv) {
421 for (i = 0; function_table[i].name; i++) {
422 if (!strcmp(argv[1], function_table[i].name)) {
423 printf("%s\n", function_table[i].name);
424 return (*function_table[i].ptr)();