ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd_devpoll.c
Revision: 9101
Committed: Wed Jan 1 09:58:45 2020 UTC (5 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 4757 byte(s)
Log Message:
- Bump copyright years everywhere

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 2001-2020 ircd-hybrid development team
5 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file s_bsd_devpoll.c
23 * \brief /dev/poll compatible network routines.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_DEVPOLL
29 #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 #include "log.h"
44
45 static int devpoll_fd;
46
47 /*
48 * comm_select_init
49 *
50 * This is a needed exported function which will be called to initialise
51 * the network loop code.
52 */
53 void
54 comm_select_init(void)
55 {
56 if ((devpoll_fd = open("/dev/poll", O_RDWR)) < 0)
57 {
58 ilog(LOG_TYPE_IRCD, "comm_select_init: couldn't open /dev/poll: %s",
59 strerror(errno));
60 exit(EXIT_FAILURE); /* Whee! */
61 }
62
63 fd_open(devpoll_fd, false, "/dev/poll file descriptor");
64 }
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 if (write(devpoll_fd, &pfd, sizeof(pfd)) != sizeof(pfd))
84 ilog(LOG_TYPE_IRCD, "devpoll_write_update: dpfd write failed %d: %s",
85 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 comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *),
96 void *client_data, uintmax_t timeout)
97 {
98 int new_events;
99
100 assert(F);
101 assert(F->flags.open == true);
102
103 if ((type & COMM_SELECT_READ))
104 {
105 F->read_handler = handler;
106 F->read_data = client_data;
107 }
108
109 if ((type & COMM_SELECT_WRITE))
110 {
111 F->write_handler = handler;
112 F->write_data = client_data;
113 }
114
115 new_events = (F->read_handler ? POLLIN : 0) |
116 (F->write_handler ? POLLOUT : 0);
117
118 if (timeout)
119 {
120 F->timeout = event_base->time.sec_monotonic + timeout;
121 F->timeout_handler = handler;
122 F->timeout_data = client_data;
123 }
124
125 if (new_events != F->evcache)
126 {
127 devpoll_write_update(F->fd, POLLREMOVE);
128 if ((F->evcache = new_events))
129 devpoll_write_update(F->fd, new_events);
130 }
131 }
132
133 /*
134 * comm_select
135 *
136 * Called to do the new-style IO, courtesy of squid (like most of this
137 * new IO code). This routine handles the stuff we've hidden in
138 * comm_setselect and fd_table[] and calls callbacks for IO ready
139 * events.
140 */
141 void
142 comm_select(void)
143 {
144 int num;
145 struct pollfd pollfds[128];
146 struct dvpoll dopoll;
147 void (*hdl)(fde_t *, void *);
148
149 dopoll.dp_timeout = SELECT_DELAY;
150 dopoll.dp_nfds = 128;
151 dopoll.dp_fds = &pollfds[0];
152 num = ioctl(devpoll_fd, DP_POLL, &dopoll);
153
154 event_time_set();
155
156 if (num < 0)
157 {
158 const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 };
159 nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */
160 return;
161 }
162
163 for (int i = 0; i < num; ++i)
164 {
165 fde_t *F = &fd_table[dopoll.dp_fds[i].fd];
166
167 if (F->flags.open == false)
168 continue;
169
170 if ((dopoll.dp_fds[i].revents & POLLIN))
171 {
172 if ((hdl = F->read_handler))
173 {
174 F->read_handler = NULL;
175 hdl(F, F->read_data);
176
177 if (F->flags.open == false)
178 continue;
179 }
180 }
181
182 if ((dopoll.dp_fds[i].revents & POLLOUT))
183 {
184 if ((hdl = F->write_handler))
185 {
186 F->write_handler = NULL;
187 hdl(F, F->write_data);
188
189 if (F->flags.open == false)
190 continue;
191 }
192 }
193
194 comm_setselect(F, 0, NULL, NULL, 0);
195 }
196 }
197 #endif

Properties

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