Incorporated the recent problems in Comon into the QA test for vsys.
[tests.git] / qaapi / qa / tests / node / support / 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 <stdlib.h>
8 #include <unistd.h>
9 #include <errno.h>
10
11 int main()
12 {
13   FILE *fp = NULL, *fp_in = NULL;
14   FILE *out_fp = NULL, *diff_fp = NULL;
15   const char* topcmd = "/vservers/pl_netflow/vsys/test.out";
16   const char* top_in_file = "/vservers/pl_netflow/vsys/test.in";
17   char buf[4096];
18   int fd_in = -1, fd_out;
19   int res;
20   int flag,flag2;
21   int count = 1;
22   struct timeval tv={.tv_sec=5,.tv_usec=0};
23
24   while (count < 200000) {
25     fd_set readSet;
26     int res;
27     int nlines=0;
28
29     
30     printf("(%d)", count);fflush(stdout);
31     if ((fd_out = open(topcmd, O_RDONLY | O_NONBLOCK)) < 0) {
32       fprintf(stderr, "error executing top\n");
33       exit(-1);
34     }
35
36     
37     while ((fd_in = open(top_in_file, O_WRONLY| O_NONBLOCK)) < 0) {
38       fprintf(stderr, "Waiting for %s (%s)\n", top_in_file,strerror(errno));
39         usleep (50); 
40     }
41
42     
43     if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
44       printf("fcntl get failed\n");
45       exit(-1);
46     }
47     
48
49     
50         if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
51       printf("fcntl get failed\n");
52       exit(-1);
53     }
54     
55
56
57     while (1) {
58             FD_ZERO(&readSet);
59             FD_SET(fd_out, &readSet);
60
61     
62             res = select(fd_out + 1, &readSet, NULL, NULL, NULL);
63     
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     
76     if (fcntl(fd_out, F_SETFL, flag & ~O_NONBLOCK) == -1) {
77       printf("fcntl set failed\n");
78       exit(-1);
79     }
80     
81
82     
83     if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
84       printf("fcntl get failed\n");
85       exit(-1);
86     }
87     
88
89
90     
91     if (fcntl(fd_in, F_SETFL, flag2 & ~O_NONBLOCK) == -1) {
92       printf("fcntl set failed\n");
93       exit(-1);
94     }
95
96     
97     if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
98       printf("fcntl get failed\n");
99       exit(-1);
100     }
101     
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 }