This directory has been moved to vsys-scripts.
[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 = "frontend/test.out";
15   const char* top_in_file = "frontend/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 < 1000000) {
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         usleep (50); 
39     }
40
41     //printf("(1)");
42     if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
43       printf("fcntl get failed\n");
44       exit(-1);
45     }
46     //printf("(2)");
47
48     //printf("(3)");
49         if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
50       printf("fcntl get failed\n");
51       exit(-1);
52     }
53     //printf("(4)");
54
55
56     while (1) {
57             FD_ZERO(&readSet);
58             FD_SET(fd_out, &readSet);
59
60     //printf("(5)");
61             res = select(fd_out + 1, &readSet, NULL, NULL, NULL);
62     //printf("(6)");
63             if (res < 0) {
64                     if (errno == EINTR || errno == EAGAIN) {
65                             printf(".");
66                             continue;
67                     }
68                     fprintf(stderr,"select failed errno=%d errstr=%s\n", errno, strerror(errno));
69                     exit(-1);
70             }
71             break; /* we're done */
72     }
73
74     //printf("(7)");
75     if (fcntl(fd_out, F_SETFL, flag & ~O_NONBLOCK) == -1) {
76       printf("fcntl set failed\n");
77       exit(-1);
78     }
79     //printf("(8)");
80
81     //printf("(9)");
82     if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
83       printf("fcntl get failed\n");
84       exit(-1);
85     }
86     //printf("(10)");
87
88
89     //printf("(11)");
90     if (fcntl(fd_in, F_SETFL, flag2 & ~O_NONBLOCK) == -1) {
91       printf("fcntl set failed\n");
92       exit(-1);
93     }
94
95     //printf("(11)");
96     if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
97       printf("fcntl get failed\n");
98       exit(-1);
99     }
100     //printf("(12)");
101
102     if (flag & O_NONBLOCK == 0) {
103       printf("fd_out still nonblocking\n");
104       exit(-1);
105     }
106
107     if (flag & O_NONBLOCK == 0) {
108       printf("fd_in still nonblocking\n");
109       exit(-1);
110     }
111     if ((fp = fdopen(fd_out, "r")) == NULL) {
112       printf("fdopen failed\n");
113       exit(-1);
114     }
115
116     while (fgets(buf, sizeof(buf), fp) != NULL) {
117             nlines++;
118     }
119
120     if (nlines<5) {
121             printf("Test returned different results - run again to verify\n");
122             exit(-1);
123     }
124
125     fclose(fp);
126     close(fd_in);
127     close(fd_out);
128     count++;
129   }
130   printf("test successful.\n");
131   exit(0);
132
133 }