~ubuntu-dev/ubuntu/lucid/zabbix/lucid-201002110857

« back to all changes in this revision

Viewing changes to src/alphacode/poller/poller_epoll.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Ablassmeier
  • Date: 2007-07-02 09:06:51 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070702090651-8l6fl3fjw9rh6l2u
Tags: 1:1.4.1-2
Add patch from SVN in order to fix Incorrect processing of character '%'
in user parameters and remote commands.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <unistd.h>
3
 
#include <fcntl.h> 
4
 
#include <sys/types.h> 
5
 
#include <sys/socket.h> 
6
 
#include <sys/epoll.h> 
7
 
#include <netdb.h> 
8
 
#include <errno.h> 
9
 
 
10
 
/* 
11
 
** ZABBIX
12
 
** Copyright (C) 2000-2005 SIA Zabbix
13
 
**
14
 
** This program is free software; you can redistribute it and/or modify
15
 
** it under the terms of the GNU General Public License as published by
16
 
** the Free Software Foundation; either version 2 of the License, or
17
 
** (at your option) any later version.
18
 
**
19
 
** This program is distributed in the hope that it will be useful,
20
 
** but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 
** GNU General Public License for more details.
23
 
**
24
 
** You should have received a copy of the GNU General Public License
25
 
** along with this program; if not, write to the Free Software
26
 
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27
 
**/
28
 
 
29
 
#define NUM     1000
30
 
 
31
 
int     s;
32
 
 
33
 
int     epfd;
34
 
struct epoll_event ev; 
35
 
struct epoll_event *events; 
36
 
 
37
 
void    wait_connect()
38
 
{
39
 
        int len;
40
 
        char c[1024];
41
 
        int retval,i,j;
42
 
 
43
 
        printf("Waiting for connect\n");
44
 
 
45
 
        for(;;)
46
 
        {
47
 
                
48
 
                printf("epfd [%d]\n", epfd);
49
 
                printf("NUM [%d]\n", NUM);
50
 
                events=malloc(NUM*sizeof(struct epoll_event));
51
 
                retval = epoll_wait(epfd, events, NUM, -1);
52
 
                if(retval == -1)
53
 
                {
54
 
                        perror("epoll_wait");
55
 
                        printf("Retval [%d]\n", errno);
56
 
                        exit(-1);
57
 
                }
58
 
                printf("Retval [%d]\n", retval);
59
 
                sleep(1);
60
 
                continue;
61
 
 
62
 
                for(i=0;i<retval;i++)
63
 
                {
64
 
                        printf("[%d] fd [%X]\n",i,events[i].data.fd);
65
 
                }
66
 
        }
67
 
}
68
 
 
69
 
int     main()
70
 
{
71
 
        int     len,i;
72
 
        char    c[1024];
73
 
        char    error[1024];
74
 
        char    ip[128]="127.0.0.1";
75
 
        int     port=10050;
76
 
 
77
 
        struct hostent *hp;
78
 
 
79
 
        struct sockaddr_in servaddr_in;
80
 
 
81
 
        int     retval;
82
 
 
83
 
 
84
 
        epfd = epoll_create(NUM); 
85
 
        if(!epfd)
86
 
        {
87
 
                perror("epoll_create\n");
88
 
                exit(1);
89
 
        } 
90
 
 
91
 
        for(i=0;i<NUM;i++)
92
 
        {
93
 
                servaddr_in.sin_family=AF_INET;
94
 
                hp=gethostbyname(ip);
95
 
 
96
 
                if(hp==NULL)
97
 
                {
98
 
                        perror("gethostbyname() failed");
99
 
                }
100
 
 
101
 
                servaddr_in.sin_addr.s_addr=((struct in_addr *)(hp->h_addr))->s_addr;
102
 
 
103
 
                servaddr_in.sin_port=htons(port);
104
 
 
105
 
                s=socket(AF_INET,SOCK_STREAM,0);
106
 
 
107
 
                if(s == -1)
108
 
                {
109
 
                        perror("socket() failed");
110
 
                }
111
 
 
112
 
                if(fcntl(s, F_SETFL, O_NONBLOCK) == -1)
113
 
                {
114
 
                        perror("fcntl() failed\n");
115
 
                        exit(-1);
116
 
                }
117
 
 
118
 
                retval = connect(s,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in));
119
 
                if(retval == 0)
120
 
                {
121
 
                        printf("Socket connected immediately");
122
 
                }
123
 
                else if(retval == -1)
124
 
                {
125
 
                        if(errno == EINPROGRESS)
126
 
                        {
127
 
//                              printf("Connection in progress\n");
128
 
                        }
129
 
                        else
130
 
                        {
131
 
                                perror("connect");
132
 
                                exit(-1);
133
 
                        }
134
 
                }
135
 
 
136
 
//              if(fcntl(s, F_SETFL, O_NONBLOCK) == -1)
137
 
//              {
138
 
//                      perror("fcntl() failed\n");
139
 
//                      exit(-1);
140
 
//              }
141
 
 
142
 
                ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLOUT;
143
 
                ev.data.fd = s;
144
 
                if(epoll_ctl(epfd, EPOLL_CTL_ADD, s, &ev) < 0)
145
 
                {
146
 
                        perror("epoll_ctl, adding listenfd\n");
147
 
                        exit(1);
148
 
                } 
149
 
//              printf("epoll_ctl ok fd [%d]\n", s);
150
 
        }
151
 
 
152
 
        wait_connect();
153
 
}