1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 // Fill the engine with client file descriptors pending an action
19 #define epoll_fill int i = epoll_wait(ep, event, 1, 5); \
22 log(DEBUG,"epoll_wait call: ep=%d, i=%d",ep,i); \
23 userrec* cu = fd_ref_table[event[0].data.fd];
25 #define kqueue_fill ts.tv_sec = 0; \
27 int i = kevent(kq, NULL, 0, &ke, 1, &ts); \
30 log(DEBUG,"kevent call: kq=%d, i=%d",kq,i); \
31 userrec* cu = fd_ref_table[ke.ident];
33 #define select_fill tval.tv_sec = 0; \
34 tval.tv_usec = 1000L; \
35 selectResult2 = select(FD_SETSIZE, &sfd, NULL, NULL, &tval); \
36 if ((selectResult2 > 0) && (xcount != clientlist.end())) \
37 for (user_hash::iterator count2a = xcount; count2a != endingiter; count2a++) \
39 if (count2a == clientlist.end()) \
41 userrec* cu = count2a->second;
44 #define engine_fill epoll_fill
47 #define engine_fill kqueue_fill
50 #define engine_fill select_fill
53 // how to determine if a socket needs attention if further checks are needed
55 #define epoll_check ((cu->fd != FD_MAGIC_NUMBER) && (cu->fd != -1))
57 #define kqueue_check ((cu->fd != FD_MAGIC_NUMBER) && (cu->fd != -1))
59 #define select_check ((cu->fd != FD_MAGIC_NUMBER) && (cu->fd != -1) && (FD_ISSET (cu->fd, &sfd)))
62 #define engine_check epoll_check
65 #define engine_check kqueue_check
68 #define engine_check select_check
71 // how to clean up an exiting client
73 #define epoll_cleanup log(DEBUG,"InspIRCd: Exited: %s",cu->nick); \
74 kill_link(cu,"Client exited"); \
75 log(DEBUG,"Bailing from client exit"); \
78 #define kqueue_cleanup log(DEBUG,"InspIRCd: Exited: %s",cu->nick); \
79 kill_link(cu,"Client exited"); \
80 log(DEBUG,"Bailing from client exit"); \
83 #define select_cleanup if (count2->second) \
85 log(DEBUG,"InspIRCd: Exited: %s",cu->nick); \
86 kill_link(cu,"Client exited"); \
87 log(DEBUG,"Bailing from client exit"); \
92 #define engine_cleanup epoll_cleanup
95 #define engine_cleanup kqueue_cleanup
98 #define engine_cleanup select_cleanup
102 // how to scan the set for fd's requiring action
104 #define select_scanset for (count = 0; count < boundPortCount; count++) \
106 FD_SET (openSockfd[count], &selectFds); \
108 tv.tv_usec = 30000L; \
109 selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv); \
110 if (selectResult > 0) \
112 for (count = 0; count < boundPortCount; count++) \
114 if (FD_ISSET (openSockfd[count], &selectFds)) \
117 #define kqueue_scanset ts.tv_sec = 0; \
118 ts.tv_nsec = 30000L; \
119 i = kevent(lkq, NULL, 0, ke_list, 32, &ts); \
120 if (i > 0) for (j = 0; j < i; j++) \
122 log(DEBUG,"kqueue: Listening socket event, i=%d, ke.ident=%d",i,ke_list[j].ident); \
123 for (count = 0; count < boundPortCount; count++) \
125 if ((unsigned)ke_list[j].ident == (unsigned)openSockfd[count]) \
128 #define epoll_scanset i = epoll_wait(lep, event, 32, EP_DELAY); \
129 if (i > 0) for (j = 0; j < i; j++) \
131 log(DEBUG,"epoll: Listening socket event, i=%d,events[j].data.fd=%d",i,event[j].data.fd); \
132 for (count = 0; count < boundPortCount; count++) \
134 if ((unsigned)event[j].data.fd == (unsigned)openSockfd[count]) \
138 #define engine_scanset epoll_scanset
141 #define engine_scanset kqueue_scanset
144 #define engine_scanset select_scanset
147 // a list of variables used specifically by this engine
149 #define kqueue_structs struct kevent ke; \
150 struct kevent ke_list[33]; \
153 #define epoll_structs struct epoll_event event[33];
155 #define select_structs fd_set sfd;
158 #define engine_structs epoll_structs
161 #define engine_structs kqueue_structs
164 #define engine_structs select_structs
167 // how to initialise the engine before using it
169 #define select_init while(0);
171 #define kqueue_init kq = kqueue(); \
174 if ((kq == -1) || (lkq == -1) || (skq == -1)) \
176 log(DEFAULT,"main: kqueue() failed!"); \
177 printf("ERROR: could not initialise kqueue event system. Shutting down.\n"); \
181 #define epoll_init ep = epoll_create(MAXCLIENTS); \
182 lep = epoll_create(32); \
183 sep = epoll_create(128); \
184 if ((ep == -1) || (lep == -1) || (sep == -1)) \
186 log(DEFAULT,"main: epoll_create() failed!"); \
187 printf("ERROR: could not initialise epoll event system. Shutting down.\n"); \
193 #define engine_init epoll_init
196 #define engine_init kqueue_init
199 #define engine_init select_init
202 // how to delete a client fd from the engine
204 #define kqueue_delete_fd struct kevent ke; \
205 EV_SET(&ke, user->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); \
206 int i = kevent(kq, &ke, 1, 0, 0, NULL); \
209 log(DEBUG,"kqueue: Failed to remove user from queue!"); \
212 #define epoll_delete_fd struct epoll_event ev; \
213 ev.events = EPOLLIN | EPOLLET; \
214 ev.data.fd = user->fd; \
215 int i = epoll_ctl(ep, EPOLL_CTL_DEL, user->fd, &ev); \
218 log(DEBUG,"epoll: List deletion failure!"); \
221 #define select_delete_fd while(0);
225 #define engine_delete_fd epoll_delete_fd
228 #define engine_delete_fd kqueue_delete_fd
231 #define engine_delete_fd select_delete_fd
234 // how to add a client fd to the engine
236 #define select_add_fd while(0);
238 #define epoll_add_fd struct epoll_event ev; \
239 log(DEBUG,"epoll: Adduser to events, ep=%d socket=%d",ep,socket); \
240 ev.events = EPOLLIN | EPOLLET; \
241 ev.data.fd = socket; \
242 int i = epoll_ctl(ep, EPOLL_CTL_ADD, socket, &ev); \
245 log(DEBUG,"epoll: List insertion failure!"); \
248 #define kqueue_add_fd struct kevent ke; \
249 log(DEBUG,"kqueue: Add user to events, kq=%d socket=%d",kq,socket);\
250 EV_SET(&ke, socket, EVFILT_READ, EV_ADD, 0, 0, NULL); \
251 int i = kevent(kq, &ke, 1, 0, 0, NULL); \
254 log(DEBUG,"kqueue: List insertion failure!"); \
258 #define engine_add_fd epoll_add_fd
261 #define engine_add_fd kqueue_add_fd
264 #define engine_add_fd select_add_fd
267 #define select_server_fill log(DEFAULT,"Using standard select socket engine.");
269 #define epoll_server_fill log(DEFAULT,"epoll socket engine is enabled. Filling listen list. boundPortcount=%d",boundPortCount); \
270 for (count = 0; count < boundPortCount; count++) \
272 struct epoll_event ev; \
273 log(DEBUG,"epoll: Add listening socket to events, ep=%d socket=%d",lep,openSockfd[count]); \
274 ev.events = EPOLLIN | EPOLLET; \
275 ev.data.fd = openSockfd[count]; \
276 int i = epoll_ctl(lep, EPOLL_CTL_ADD, openSockfd[count], &ev); \
279 log(DEFAULT,"main: add listen ports, epoll_ctl failed!"); \
280 printf("ERROR: could not initialise listening sockets in epoll list. Shutting down.\n"); \
285 #define kqueue_server_fill log(DEFAULT,"kqueue socket engine is enabled. Filling listen list."); \
286 for (count = 0; count < boundPortCount; count++) \
289 log(DEBUG,"kqueue: Add listening socket to events, kq=%d socket=%d",lkq,openSockfd[count]); \
290 EV_SET(&ke, openSockfd[count], EVFILT_READ, EV_ADD, 0, MaxConn, NULL); \
291 int i = kevent(lkq, &ke, 1, 0, 0, NULL); \
294 log(DEFAULT,"main: add listen ports to kqueue failed!"); \
295 printf("ERROR: could not initialise listening sockets in kqueue. Shutting down.\n"); \
301 #define engine_server_fill epoll_server_fill
304 #define engine_server_fill kqueue_server_fill
307 #define engine_server_fill select_server_fill
311 // what is this engine called?
314 #define engine_name "kqueue"
317 #define engine_name "select"
320 #define engine_name "epoll"