FINALLY...
[vsys.git] / tests / vsys_conctest.c
1 #include <stdio.h>
2 #include <sys/select.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <errno.h>
9
10 int main()
11 {
12   FILE *fp = NULL, *fp_in = NULL;
13   FILE *out_fp = NULL, *diff_fp = NULL;
14   const char* topcmd = "fe/test.out";
15   const char* top_in_file = "fe/test.in";
16   char buf[4096];
17   int fd_in = -1, fd_out;
18   int res;
19   int flag,flag2;
20   int count = 1;
21   struct timeval tv={.tv_sec=5,.tv_usec=0};
22
23   while (count < 100000) {
24     fd_set readSet;
25     int res;
26     int nlines=0;
27
28     //usleep(200);
29     printf("(%d)", count);fflush(stdout);
30     if ((fd_out = open(topcmd, O_RDONLY | O_NONBLOCK)) < 0) {
31       fprintf(stderr, "error executing top\n");
32       exit(-1);
33     }
34
35     //printf("((0))");fflush(stdout);
36     while ((fd_in = open(top_in_file, O_WRONLY| O_NONBLOCK)) < 0) {
37       fprintf(stderr, "Waiting for %s (%s)\n", top_in_file,strerror(errno));
38         sleep (1); 
39     }
40     printf("%d open\n",fd_in);
41
42     //printf("(1)");
43     if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
44       printf("fcntl get failed\n");
45       exit(-1);
46     }
47     //printf("(2)");
48
49     //printf("(3)");
50         if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
51       printf("fcntl get failed\n");
52       exit(-1);
53     }
54     //printf("(4)");
55
56
57     while (1) {
58             FD_ZERO(&readSet);
59             FD_SET(fd_out, &readSet);
60
61     //printf("(5)");
62             res = select(fd_out + 1, &readSet, NULL, NULL, NULL);
63     //printf("(6)");
64             if (res < 0) {
65                     if (errno == EINTR || errno == EAGAIN) {
66                             printf(".");
67                             continue;
68                     }
69                     fprintf(stderr,"select failed errno=%d errstr=%s\n", errno, strerror(errno));
70                     exit(-1);
71             }
72             break; /* we're done */
73     }
74
75     //printf("(7)");
76     if (fcntl(fd_out, F_SETFL, flag & ~O_NONBLOCK) == -1) {
77       printf("fcntl set failed\n");
78       exit(-1);
79     }
80     //printf("(8)");
81
82     //printf("(9)");
83     if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
84       printf("fcntl get failed\n");
85       exit(-1);
86     }
87     //printf("(10)");
88
89
90     //printf("(11)");
91     if (fcntl(fd_in, F_SETFL, flag2 & ~O_NONBLOCK) == -1) {
92       printf("fcntl set failed\n");
93       exit(-1);
94     }
95
96     //printf("(11)");
97     if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
98       printf("fcntl get failed\n");
99       exit(-1);
100     }
101     //printf("(12)");
102
103     if (flag & O_NONBLOCK == 0) {
104       printf("fd_out still nonblocking\n");
105       exit(-1);
106     }
107
108     if (flag & O_NONBLOCK == 0) {
109       printf("fd_in still nonblocking\n");
110       exit(-1);
111     }
112     if ((fp = fdopen(fd_out, "r")) == NULL) {
113       printf("fdopen failed\n");
114       exit(-1);
115     }
116
117     while (fgets(buf, sizeof(buf), fp) != NULL) {
118             nlines++;
119     }
120
121     if (nlines<5) {
122             printf("Test returned different results - run again to verify\n");
123             exit(-1);
124     }
125
126     fclose(fp);
127     close(fd_in);
128     close(fd_out);
129     count++;
130   }
131   printf("test successful.\n");
132   exit(0);
133
134 }