ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd_devpoll.c
Revision: 8431
Committed: Tue Mar 27 18:49:15 2018 UTC (7 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 4678 byte(s)
Log Message:
- Stylistic changes

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 8279 * Copyright (c) 2001-2018 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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * 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 michael 8339 static int devpoll_fd;
46 adx 30
47     /*
48 michael 8414 * comm_select_init
49 adx 30 *
50     * This is a needed exported function which will be called to initialise
51     * the network loop code.
52     */
53     void
54 michael 8414 comm_select_init(void)
55 adx 30 {
56 michael 8339 if ((devpoll_fd = open("/dev/poll", O_RDWR)) < 0)
57 adx 30 {
58 michael 8414 ilog(LOG_TYPE_IRCD, "comm_select_init: couldn't open /dev/poll: %s",
59 michael 6560 strerror(errno));
60 michael 6553 exit(EXIT_FAILURE); /* Whee! */
61 adx 30 }
62    
63 michael 8339 fd_open(devpoll_fd, 0, "/dev/poll file descriptor");
64 adx 30 }
65    
66     /*
67     * Write an update to the devpoll filter.
68     * See, we end up having to do a seperate (?) remove before we do an
69     * add of a new polltype, so we have to have this function seperate from
70     * the others.
71     */
72     static void
73     devpoll_write_update(int fd, int events)
74     {
75     struct pollfd pfd;
76    
77     /* Build the pollfd entry */
78     pfd.revents = 0;
79     pfd.fd = fd;
80     pfd.events = events;
81    
82     /* Write the thing to our poll fd */
83 michael 8339 if (write(devpoll_fd, &pfd, sizeof(pfd)) != sizeof(pfd))
84 michael 1247 ilog(LOG_TYPE_IRCD, "devpoll_write_update: dpfd write failed %d: %s",
85 adx 30 errno, strerror(errno));
86     }
87    
88     /*
89     * comm_setselect
90     *
91     * This is a needed exported function which will be called to register
92     * and deregister interest in a pending IO state for a given FD.
93     */
94     void
95 michael 4461 comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *),
96 michael 7330 void *client_data, uintmax_t timeout)
97 adx 30 {
98     int new_events;
99    
100     if ((type & COMM_SELECT_READ))
101     {
102     F->read_handler = handler;
103     F->read_data = client_data;
104     }
105    
106     if ((type & COMM_SELECT_WRITE))
107     {
108     F->write_handler = handler;
109     F->write_data = client_data;
110     }
111    
112 adx 282 new_events = (F->read_handler ? POLLIN : 0) |
113     (F->write_handler ? POLLOUT : 0);
114 adx 30
115 michael 8431 if (timeout)
116 michael 2725 {
117 adx 30 F->timeout = CurrentTime + (timeout / 1000);
118 michael 2725 F->timeout_handler = handler;
119     F->timeout_data = client_data;
120     }
121 adx 30
122     if (new_events != F->evcache)
123     {
124     devpoll_write_update(F->fd, POLLREMOVE);
125     if ((F->evcache = new_events))
126     devpoll_write_update(F->fd, new_events);
127     }
128     }
129    
130     /*
131     * comm_select
132     *
133     * Called to do the new-style IO, courtesy of squid (like most of this
134     * new IO code). This routine handles the stuff we've hidden in
135     * comm_setselect and fd_table[] and calls callbacks for IO ready
136     * events.
137     */
138     void
139     comm_select(void)
140     {
141 michael 8431 int num;
142 adx 30 struct pollfd pollfds[128];
143     struct dvpoll dopoll;
144 michael 4461 void (*hdl)(fde_t *, void *);
145 adx 30
146     dopoll.dp_timeout = SELECT_DELAY;
147     dopoll.dp_nfds = 128;
148     dopoll.dp_fds = &pollfds[0];
149 michael 8339 num = ioctl(devpoll_fd, DP_POLL, &dopoll);
150 adx 30
151     set_time();
152    
153     if (num < 0)
154     {
155 michael 6542 const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 };
156     nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */
157 adx 30 return;
158     }
159    
160 michael 8431 for (int i = 0; i < num; ++i)
161 adx 30 {
162 michael 8339 fde_t *F = &fd_table[dopoll.dp_fds[i].fd];
163    
164     if (F->flags.open == 0)
165 adx 30 continue;
166    
167 adx 282 if ((dopoll.dp_fds[i].revents & POLLIN))
168 michael 2650 {
169 michael 8431 if ((hdl = F->read_handler))
170 adx 30 {
171     F->read_handler = NULL;
172     hdl(F, F->read_data);
173 michael 8339
174     if (F->flags.open == 0)
175 adx 242 continue;
176 adx 30 }
177 michael 2650 }
178 adx 30
179 adx 282 if ((dopoll.dp_fds[i].revents & POLLOUT))
180 michael 2650 {
181 michael 8431 if ((hdl = F->write_handler))
182 adx 30 {
183     F->write_handler = NULL;
184     hdl(F, F->write_data);
185 michael 8339
186     if (F->flags.open == 0)
187 adx 242 continue;
188 adx 30 }
189 michael 2650 }
190 adx 30
191     comm_setselect(F, 0, NULL, NULL, 0);
192     }
193     }
194 stu 908 #endif

Properties

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