]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/pipe/bigpipetest.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / pipe / bigpipetest.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <sys/select.h>
6 #include <string.h>
7 #include <errno.h>
8
9 #define BIG_PIPE_SIZE  64*1024 /* From sys/pipe.h */
10
11 /*
12  * Test for the non-blocking big pipe bug (write(2) returning
13  * EAGAIN while select(2) returns the descriptor as ready for write).
14  *
15  * $FreeBSD$
16  */
17
18 void write_frame(int fd, char *buf, unsigned long buflen)
19 {
20     fd_set wfd;
21     int i;
22
23     while (buflen) {
24         FD_ZERO(&wfd);
25         FD_SET(fd, &wfd);
26         i = select(fd+1, NULL, &wfd, NULL, NULL);
27         if (i < 0) {
28             perror("select");
29             exit(1);
30         }
31         if (i != 1) {
32             fprintf(stderr, "select returned unexpected value %d\n", i);
33             exit(1);
34         }
35         i = write(fd, buf, buflen);
36         if (i < 0) {
37             if (errno != EAGAIN)
38                 perror("write");
39             exit(1);
40         }
41         buf += i;
42         buflen -= i;
43     }
44 }
45
46 int main()
47 {
48     char buf[BIG_PIPE_SIZE];    /* any value over PIPE_SIZE should do */
49     int i, flags, fd[2];
50
51     printf("1..1\n");
52
53     if (pipe(fd) < 0) { perror("pipe"); exit(1); }
54
55     flags = fcntl(fd[1], F_GETFL);
56     if (flags == -1 || fcntl(fd[1], F_SETFL, flags|O_NONBLOCK) == -1) {
57         perror("fcntl");
58         exit(1);
59     }
60
61     switch (fork()) {
62         case -1:
63             perror("fork");
64             exit(1);
65         case 0:
66             close(fd[1]);
67             for (;;) {
68                 i = read(fd[0], buf, 256); /* any small size should do */
69                 if (i == 0) break;
70                 if (i < 0) { perror("read"); exit(1); }
71             }
72             exit(0);
73         default:
74             break;
75     }
76
77     close(fd[0]);
78     memset(buf, 0, sizeof buf);
79     for (i = 0; i < 1000; i++) write_frame(fd[1], buf, sizeof buf);
80     printf("ok 1\n");
81     exit(0);
82 }