ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd_epoll.c
Revision: 8339
Committed: Sat Mar 3 22:47:06 2018 UTC (7 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 4105 byte(s)
Log Message:
- Restore fd_table. No longer allocate fde_t items from within any other structures like the AuthRequest, or Connection structure
- struct AuthRequest once again is no longer allocated from within the Connection structure

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) 2005-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_epoll.c
23     * \brief Linux epoll() compatible network routines.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 stu 908 #if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_EPOLL
29 adx 30 #include "fdlist.h"
30     #include "ircd.h"
31     #include "s_bsd.h"
32 michael 1309 #include "log.h"
33 adx 30 #include <sys/epoll.h>
34     #include <sys/syscall.h>
35    
36 michael 8339 static int epoll_fd;
37 adx 30
38    
39     /*
40 michael 7566 * netio_init
41 adx 30 *
42     * This is a needed exported function which will be called to initialise
43     * the network loop code.
44     */
45     void
46 michael 7566 netio_init(void)
47 adx 30 {
48 michael 8339 if ((epoll_fd = epoll_create(hard_fdlimit)) < 0)
49 adx 30 {
50 michael 7566 ilog(LOG_TYPE_IRCD, "netio_init: couldn't open epoll fd: %s",
51 michael 6560 strerror(errno));
52 michael 6553 exit(EXIT_FAILURE); /* Whee! */
53 adx 30 }
54    
55 michael 8339 fd_open(epoll_fd, 0, "epoll file descriptor");
56 adx 30 }
57    
58     /*
59     * comm_setselect
60     *
61     * This is a needed exported function which will be called to register
62     * and deregister interest in a pending IO state for a given FD.
63     */
64     void
65 michael 4461 comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *),
66 michael 7330 void *client_data, uintmax_t timeout)
67 adx 30 {
68     int new_events, op;
69 michael 966 struct epoll_event ep_event = { 0, { 0 } };
70 adx 30
71     if ((type & COMM_SELECT_READ))
72     {
73     F->read_handler = handler;
74     F->read_data = client_data;
75     }
76    
77     if ((type & COMM_SELECT_WRITE))
78     {
79     F->write_handler = handler;
80     F->write_data = client_data;
81     }
82    
83     new_events = (F->read_handler ? EPOLLIN : 0) |
84     (F->write_handler ? EPOLLOUT : 0);
85    
86     if (timeout != 0)
87 michael 2725 {
88 adx 30 F->timeout = CurrentTime + (timeout / 1000);
89 michael 2725 F->timeout_handler = handler;
90     F->timeout_data = client_data;
91     }
92 adx 30
93     if (new_events != F->evcache)
94     {
95     if (new_events == 0)
96     op = EPOLL_CTL_DEL;
97     else if (F->evcache == 0)
98     op = EPOLL_CTL_ADD;
99     else
100     op = EPOLL_CTL_MOD;
101    
102     ep_event.events = F->evcache = new_events;
103     ep_event.data.fd = F->fd;
104    
105 michael 8339 if (epoll_ctl(epoll_fd, op, F->fd, &ep_event) != 0)
106 adx 30 {
107 michael 1247 ilog(LOG_TYPE_IRCD, "comm_setselect: epoll_ctl() failed: %s", strerror(errno));
108 adx 30 abort();
109     }
110     }
111     }
112    
113     /*
114     * comm_select()
115     *
116     * Called to do the new-style IO, courtesy of of squid (like most of this
117     * new IO code). This routine handles the stuff we've hidden in
118     * comm_setselect and fd_table[] and calls callbacks for IO ready
119     * events.
120     */
121     void
122     comm_select(void)
123     {
124     struct epoll_event ep_fdlist[128];
125     int num, i;
126 michael 4461 void (*hdl)(fde_t *, void *);
127 adx 30
128 michael 8339 num = epoll_wait(epoll_fd, ep_fdlist, 128, SELECT_DELAY);
129 adx 30
130     set_time();
131    
132     if (num < 0)
133     {
134 michael 6542 const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 };
135     nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */
136 adx 30 return;
137     }
138    
139     for (i = 0; i < num; i++)
140     {
141 michael 8339 fde_t *F = &fd_table[ep_fdlist[i].data.fd];
142    
143     if (F->flags.open == 0)
144 adx 30 continue;
145    
146     if ((ep_fdlist[i].events & (EPOLLIN | EPOLLHUP | EPOLLERR)))
147 michael 2650 {
148 michael 5902 if ((hdl = F->read_handler))
149 adx 30 {
150     F->read_handler = NULL;
151     hdl(F, F->read_data);
152 michael 8339
153     if (F->flags.open == 0)
154 michael 2650 continue;
155 adx 30 }
156 michael 2650 }
157 adx 30
158     if ((ep_fdlist[i].events & (EPOLLOUT | EPOLLHUP | EPOLLERR)))
159 michael 2650 {
160 michael 5902 if ((hdl = F->write_handler))
161 adx 30 {
162     F->write_handler = NULL;
163     hdl(F, F->write_data);
164 michael 8339
165     if (F->flags.open == 0)
166 michael 2650 continue;
167 adx 30 }
168 michael 2650 }
169 adx 30
170     comm_setselect(F, 0, NULL, NULL, 0);
171     }
172     }
173 stu 908 #endif

Properties

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