ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd_devpoll.c
Revision: 2916
Committed: Sat Jan 25 21:09:18 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 4577 byte(s)
Log Message:
- Clean up all files in include/ (fixed indentation, removed whitespaces/tabs)
- Fixed copyright years

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 2916 * Copyright (c) 2001-2014 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     * USA
20     */
21    
22 michael 2916 /*! \file s_bsd_devpoll.c
23     * \brief /dev/poll compatible network routines.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 stu 908 #if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_DEVPOLL
29 adx 30 #include <sys/ioctl.h>
30     /* HPUX uses devpoll.h and not sys/devpoll.h */
31     #ifdef HAVE_DEVPOLL_H
32     # include <devpoll.h>
33     #else
34     # ifdef HAVE_SYS_DEVPOLL_H
35     # include <sys/devpoll.h>
36     # else
37     # error "No devpoll.h found! Try ./configuring and letting the script choose for you."
38     # endif
39     #endif
40     #include "fdlist.h"
41     #include "ircd.h"
42     #include "s_bsd.h"
43 michael 1309 #include "log.h"
44 adx 30
45     static fde_t dpfd;
46    
47     /*
48     * init_netio
49     *
50     * This is a needed exported function which will be called to initialise
51     * the network loop code.
52     */
53     void
54     init_netio(void)
55     {
56     int fd;
57    
58     if ((fd = open("/dev/poll", O_RDWR)) < 0)
59     {
60 michael 1247 ilog(LOG_TYPE_IRCD, "init_netio: Couldn't open /dev/poll - %d: %s",
61 adx 30 errno, strerror(errno));
62     exit(115); /* Whee! */
63     }
64    
65     fd_open(&dpfd, fd, 0, "/dev/poll file descriptor");
66     }
67    
68     /*
69     * Write an update to the devpoll filter.
70     * See, we end up having to do a seperate (?) remove before we do an
71     * add of a new polltype, so we have to have this function seperate from
72     * the others.
73     */
74     static void
75     devpoll_write_update(int fd, int events)
76     {
77     struct pollfd pfd;
78    
79     /* Build the pollfd entry */
80     pfd.revents = 0;
81     pfd.fd = fd;
82     pfd.events = events;
83    
84     /* Write the thing to our poll fd */
85     if (write(dpfd.fd, &pfd, sizeof(pfd)) != sizeof(pfd))
86 michael 1247 ilog(LOG_TYPE_IRCD, "devpoll_write_update: dpfd write failed %d: %s",
87 adx 30 errno, strerror(errno));
88     }
89    
90     /*
91     * comm_setselect
92     *
93     * This is a needed exported function which will be called to register
94     * and deregister interest in a pending IO state for a given FD.
95     */
96     void
97     comm_setselect(fde_t *F, unsigned int type, PF *handler,
98     void *client_data, time_t timeout)
99     {
100     int new_events;
101    
102     if ((type & COMM_SELECT_READ))
103     {
104     F->read_handler = handler;
105     F->read_data = client_data;
106     }
107    
108     if ((type & COMM_SELECT_WRITE))
109     {
110     F->write_handler = handler;
111     F->write_data = client_data;
112     }
113    
114 adx 282 new_events = (F->read_handler ? POLLIN : 0) |
115     (F->write_handler ? POLLOUT : 0);
116 adx 30
117     if (timeout != 0)
118 michael 2725 {
119 adx 30 F->timeout = CurrentTime + (timeout / 1000);
120 michael 2725 F->timeout_handler = handler;
121     F->timeout_data = client_data;
122     }
123 adx 30
124     if (new_events != F->evcache)
125     {
126     devpoll_write_update(F->fd, POLLREMOVE);
127     if ((F->evcache = new_events))
128     devpoll_write_update(F->fd, new_events);
129     }
130     }
131    
132     /*
133     * comm_select
134     *
135     * Called to do the new-style IO, courtesy of squid (like most of this
136     * new IO code). This routine handles the stuff we've hidden in
137     * comm_setselect and fd_table[] and calls callbacks for IO ready
138     * events.
139     */
140     void
141     comm_select(void)
142     {
143     int num, i;
144     struct pollfd pollfds[128];
145     struct dvpoll dopoll;
146     PF *hdl;
147     fde_t *F;
148    
149     dopoll.dp_timeout = SELECT_DELAY;
150     dopoll.dp_nfds = 128;
151     dopoll.dp_fds = &pollfds[0];
152     num = ioctl(dpfd.fd, DP_POLL, &dopoll);
153    
154     set_time();
155    
156     if (num < 0)
157     {
158     #ifdef HAVE_USLEEP
159     usleep(50000); /* avoid 99% CPU in comm_select */
160     #endif
161     return;
162     }
163    
164     for (i = 0; i < num; i++)
165     {
166     F = lookup_fd(dopoll.dp_fds[i].fd);
167     if (F == NULL || !F->flags.open)
168     continue;
169    
170 adx 282 if ((dopoll.dp_fds[i].revents & POLLIN))
171 michael 2650 {
172 adx 30 if ((hdl = F->read_handler) != NULL)
173     {
174     F->read_handler = NULL;
175     hdl(F, F->read_data);
176 adx 242 if (!F->flags.open)
177     continue;
178 adx 30 }
179 michael 2650 }
180 adx 30
181 adx 282 if ((dopoll.dp_fds[i].revents & POLLOUT))
182 michael 2650 {
183 michael 2916 if ((hdl = F->write_handler) != NULL)
184 adx 30 {
185     F->write_handler = NULL;
186     hdl(F, F->write_data);
187 adx 242 if (!F->flags.open)
188     continue;
189 adx 30 }
190 michael 2650 }
191 adx 30
192     comm_setselect(F, 0, NULL, NULL, 0);
193     }
194     }
195 stu 908 #endif

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision