]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/io.c
removed misleading const qualifier
[ngircd-alex.git] / src / ngircd / io.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  * Please read the file COPYING, README and AUTHORS for more information.
7  *
8  * I/O abstraction interface.
9  * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
10  *
11  */
12
13 #include "portab.h"
14
15 static char UNUSED id[] = "$Id: io.c,v 1.8 2005/08/27 23:23:54 fw Exp $";
16
17 #include <assert.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24
25 #include "array.h"
26 #include "io.h"
27 #include "log.h"
28
29
30 typedef struct {
31  void (*callback)(int, short);
32  int fd;
33  short what;
34 } io_event;
35
36 #define INIT_IOEVENT    { NULL, -1, 0, NULL }
37 #define IO_ERROR        4
38
39 #ifdef HAVE_EPOLL_CREATE
40 #define IO_USE_EPOLL    1
41 #else
42 # ifdef HAVE_KQUEUE
43 #define IO_USE_KQUEUE   1
44 # else
45 #define IO_USE_SELECT   1
46 #endif
47 #endif
48
49 static bool library_initialized;
50
51 #ifdef IO_USE_EPOLL
52 #include <sys/epoll.h>
53
54 static int io_masterfd;
55 static bool io_event_new_epoll(int fd, short what);
56 static bool io_event_change_epoll(int fd, short what);
57 static int io_dispatch_epoll(struct timeval *tv);
58 #endif
59
60 #ifdef IO_USE_KQUEUE
61 #include <sys/types.h>
62 #include <sys/event.h>
63 static array io_evcache;
64 static int io_masterfd;
65
66 static int io_dispatch_kqueue(struct timeval *tv);
67 static bool io_event_add_kqueue(int, short);
68 #endif
69
70 #ifdef IO_USE_SELECT
71 #include "defines.h"    /* for conn.h */
72 #include "conn.h"       /* for CONN_IDX (needed by resolve.h) */
73 #include "resolve.h"    /* for RES_STAT (needed by conf.h) */
74 #include "conf.h"       /* for Conf_MaxConnections */
75
76 static fd_set readers;
77 static fd_set writers;
78 static int select_maxfd;                /* the select() interface sucks badly */
79 static int io_dispatch_select(struct timeval *tv);
80 #endif
81
82 static array io_events;
83
84 static void io_docallback PARAMS((int fd, short what));
85
86 static io_event *
87 io_event_get(int fd)
88 {
89         io_event *i;
90         assert(fd >= 0);
91         i = (io_event *) array_get(&io_events, sizeof(io_event), fd);
92         assert(i);
93
94         return i;
95 }
96
97
98 bool
99 io_library_init(unsigned int eventsize)
100 {
101         bool ret;
102 #ifdef IO_USE_EPOLL
103         int ecreate_hint = (int)eventsize;
104         if (ecreate_hint <= 0)
105                 ecreate_hint = 128;
106 #endif
107
108         if (library_initialized)
109                 return true;
110
111 #ifdef IO_USE_SELECT
112 #ifdef FD_SETSIZE
113         if (eventsize >= FD_SETSIZE)
114                 eventsize = FD_SETSIZE - 1;
115 #endif
116 #endif
117         if (eventsize && !array_alloc(&io_events, sizeof(io_event), eventsize))
118                 eventsize = 0;
119
120 #ifdef IO_USE_EPOLL
121         io_masterfd = epoll_create(ecreate_hint);
122         Log(LOG_INFO,
123             "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
124             ecreate_hint, eventsize, io_masterfd);
125         ret = io_masterfd >= 0;
126         if (ret) library_initialized = true;
127
128         return ret;
129 #endif
130 #ifdef IO_USE_SELECT
131         Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
132             eventsize);
133         FD_ZERO(&readers);
134         FD_ZERO(&writers);
135 #ifdef FD_SETSIZE
136         if (Conf_MaxConnections >= FD_SETSIZE) {
137                 Log(LOG_WARNING,
138                     "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
139                     Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
140
141                 Conf_MaxConnections = FD_SETSIZE - 1;
142         }
143 #else
144         Log(LOG_WARNING,
145             "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
146 #endif /* FD_SETSIZE */
147         library_initialized = true;
148         return true;
149 #endif /* SELECT */
150 #ifdef IO_USE_KQUEUE
151         io_masterfd = kqueue();
152
153         Log(LOG_INFO,
154             "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
155             eventsize, io_masterfd);
156         ret = io_masterfd >= 0;
157         if (ret) library_initialized = true;
158
159         return ret;
160 #endif
161 }
162
163
164 void
165 io_library_shutdown(void)
166 {
167 #ifdef IO_USE_SELECT
168         FD_ZERO(&readers);
169         FD_ZERO(&writers);
170 #else
171         close(io_masterfd);     /* kqueue, epoll */
172         io_masterfd = -1;
173 #endif
174 #ifdef IO_USE_KQUEUE
175         array_free(&io_evcache);
176 #endif
177         library_initialized = false;
178 }
179
180
181 bool
182 io_event_setcb(int fd, void (*cbfunc) (int, short))
183 {
184         io_event *i = io_event_get(fd);
185         if (!i)
186                 return false;
187
188         i->callback = cbfunc;
189         return true;
190 }
191
192
193 bool
194 io_event_create(int fd, short what, void (*cbfunc) (int, short))
195 {
196         io_event *i;
197
198         assert(fd >= 0);
199
200 #ifdef IO_USE_SELECT
201 #ifdef FD_SETSIZE
202         if (fd >= FD_SETSIZE) {
203                 Log(LOG_ERR,
204                     "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
205                     fd, FD_SETSIZE);
206                 return false;
207         }
208 #endif                          /* FD_SETSIZE */
209 #endif                          /* IO_USE_SELECT */
210
211         i = (io_event *) array_alloc(&io_events, sizeof(io_event), fd);
212         if (!i) {
213                 Log(LOG_WARNING,
214                     "array_alloc failed: could not allocate space for %d io_event structures",
215                     fd);
216                 return false;
217         }
218
219         i->fd = fd;
220         i->callback = cbfunc;
221 #ifdef IO_USE_EPOLL
222         i->what = what;
223         return io_event_new_epoll(fd, what);
224 #endif
225 #ifdef IO_USE_KQUEUE
226         i->what = what;
227         return io_event_add_kqueue(fd, what);
228 #endif
229 #ifdef IO_USE_SELECT
230         i->what = 0;
231         return io_event_add(fd, what);
232 #endif
233 }
234
235
236 #ifdef IO_USE_EPOLL
237 static bool
238 io_event_new_epoll(int fd, short what)
239 {
240         struct epoll_event ev = { 0, {0} };
241         ev.data.fd = fd;
242
243         if (what & IO_WANTREAD)
244                 ev.events = EPOLLIN | EPOLLPRI;
245         if (what & IO_WANTWRITE)
246                 ev.events |= EPOLLOUT;
247
248         return epoll_ctl(io_masterfd, EPOLL_CTL_ADD, fd, &ev) == 0;
249 }
250
251
252 static bool
253 io_event_change_epoll(int fd, short what)
254 {
255         struct epoll_event ev = { 0, {0} };
256         ev.data.fd = fd;
257
258         if (what & IO_WANTREAD)
259                 ev.events = EPOLLIN | EPOLLPRI;
260         if (what & IO_WANTWRITE)
261                 ev.events |= EPOLLOUT;
262
263         return epoll_ctl(io_masterfd, EPOLL_CTL_MOD, fd, &ev) == 0;
264 }
265 #endif
266
267 #ifdef IO_USE_KQUEUE
268 static bool
269 io_event_kqueue_commit_cache(void)
270 {
271         struct kevent *events;
272         bool ret;
273         int len = (int) array_length(&io_evcache, sizeof (struct kevent));
274  
275         if (!len) /* nothing to do */
276                 return true;
277
278         assert(len>0);
279
280         if (len < 0) {
281                 array_free(&io_evcache);
282                 return false;
283         }
284
285         events = array_start(&io_evcache);
286
287         assert(events);
288
289         ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
290         if (ret)
291                 array_trunc(&io_evcache);
292         return ret;
293 }
294
295
296 static bool
297 io_event_add_kqueue(int fd, short what)
298 {
299         struct kevent kev;
300         short filter = 0;
301         unsigned int len = array_length(&io_evcache, sizeof kev);
302
303         if (what & IO_WANTREAD)
304                 filter = EVFILT_READ;
305
306         if (what & IO_WANTWRITE)
307                 filter |= EVFILT_WRITE;
308
309         if (len >= 100) {
310                 (void)array_trunc(&io_evcache);
311         }
312
313         EV_SET(&kev, fd, filter, EV_ADD | EV_ENABLE, 0, 0, NULL);
314         return array_catb(&io_evcache, (char*) &kev, sizeof (kev));
315 }
316 #endif
317
318
319 bool
320 io_event_add(int fd, short what)
321 {
322         io_event *i = io_event_get(fd);
323
324         assert(i);
325
326         if (!i)
327                 return false;
328         if (i->what == what)
329                 return true;
330 #ifdef DEBUG
331         Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
332 #endif
333
334         i->what |= what;
335
336 #ifdef IO_USE_EPOLL
337         return io_event_change_epoll(fd, i->what);
338 #endif
339
340 #ifdef IO_USE_KQUEUE
341         return io_event_add_kqueue(fd, what);
342 #endif
343
344 #ifdef IO_USE_SELECT
345         if (fd > select_maxfd)
346                 select_maxfd = fd;
347
348         if (what & IO_WANTREAD)
349                 FD_SET(fd, &readers);
350         if (what & IO_WANTWRITE)
351                 FD_SET(fd, &writers);
352
353         return true;
354 #endif
355 }
356
357
358 bool
359 io_setnonblock(int fd)
360 {
361         int flags = fcntl(fd, F_GETFL);
362         if (flags == -1)
363                 return false;
364
365 #ifndef O_NONBLOCK
366 #define O_NONBLOCK O_NDELAY
367 #endif
368         flags |= O_NONBLOCK;
369
370         return fcntl(fd, F_SETFL, flags) == 0;
371 }
372
373
374 bool
375 io_close(int fd)
376 {
377         io_event *i = io_event_get(fd);
378         if (i) {
379                 memset(i, 0, sizeof(io_event));
380                 i->fd = -1;
381         }
382 #ifdef IO_USE_SELECT
383         FD_CLR(fd, &writers);
384         FD_CLR(fd, &readers);
385
386         if (fd == select_maxfd)
387                 select_maxfd--;
388 #endif
389 #ifdef IO_USE_KQUEUE
390         if (array_length(&io_evcache, sizeof (struct kevent)))  /* pending data in cache? */
391                 io_event_kqueue_commit_cache();
392 #endif
393         return close(fd) == 0;  /* both epoll an kqueue will remove fd from all sets automatically */
394 }
395
396
397 bool
398 io_event_del(int fd, short what)
399 {
400 #ifdef IO_USE_KQUEUE
401         struct kevent kev;
402         short filter = 0;
403 #endif
404         io_event *i = io_event_get(fd);
405 #ifdef DEBUG
406         Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
407 #endif
408         assert(i);
409         if (!i)
410                 return false;
411
412         i->what &= ~what;
413
414 #ifdef IO_USE_EPOLL
415         return io_event_change_epoll(fd, i->what);
416 #endif
417
418 #ifdef IO_USE_KQUEUE
419         if (what & IO_WANTREAD)
420                 filter = EVFILT_READ;
421
422         if (what & IO_WANTWRITE)
423                 filter |= EVFILT_WRITE;
424
425         EV_SET(&kev, fd, filter, EV_DELETE, 0, 0, NULL);
426         return kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
427 #endif
428
429 #ifdef IO_USE_SELECT
430         if (what & IO_WANTWRITE)
431                 FD_CLR(i->fd, &writers);
432
433         if (what & IO_WANTREAD)
434                 FD_CLR(i->fd, &readers);
435
436         return true;
437 #endif
438 }
439
440
441 #ifdef IO_USE_SELECT
442 static int
443 io_dispatch_select(struct timeval *tv)
444 {
445         fd_set readers_tmp = readers;
446         fd_set writers_tmp = writers;
447         short what;
448         int ret, i;
449         int fds_ready;
450         ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
451         if (ret <= 0)
452                 return ret;
453
454         fds_ready = ret;
455
456         for (i = 0; i <= select_maxfd; i++) {
457                 what = 0;
458                 if (FD_ISSET(i, &readers_tmp)) {
459                         what = IO_WANTREAD;
460                         fds_ready--;
461                 }
462
463                 if (FD_ISSET(i, &writers_tmp)) {
464                         what |= IO_WANTWRITE;
465                         fds_ready--;
466                 }
467                 if (what)
468                         io_docallback(i, what);
469                 if (fds_ready <= 0)
470                         break;
471         }
472
473         return ret;
474 }
475 #endif
476
477
478 #ifdef IO_USE_EPOLL
479 static int
480 io_dispatch_epoll(struct timeval *tv)
481 {
482         time_t sec = tv->tv_sec * 1000;
483         int i, total = 0, ret, timeout = tv->tv_usec + sec;
484         struct epoll_event epoll_ev[100];
485         short type;
486
487         if (timeout < 0)
488                 timeout = 1000;
489
490         do {
491                 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
492                 total += ret;
493                 if (ret <= 0)
494                         return total;
495
496                 for (i = 0; i < ret; i++) {
497                         type = 0;
498                         if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
499                                 type = IO_ERROR;
500
501                         if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
502                                 type |= IO_WANTREAD;
503
504                         if (epoll_ev[i].events & EPOLLOUT)
505                                 type |= IO_WANTWRITE;
506
507                         io_docallback(epoll_ev[i].data.fd, type);
508                 }
509
510                 timeout = 0;
511         } while (ret == 100);
512
513         return total;
514 }
515 #endif
516
517
518 #ifdef IO_USE_KQUEUE
519 static int
520 io_dispatch_kqueue(struct timeval *tv)
521 {
522         int i, total = 0, ret;
523         struct kevent kev[100];
524         struct kevent *newevents;
525         struct timespec ts;
526         int newevents_len;
527         short type;
528         ts.tv_sec = tv->tv_sec;
529         ts.tv_nsec = tv->tv_usec * 1000;
530         
531         do {
532                 newevents_len = array_length(&io_evcache, sizeof (struct kevent));
533                 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
534                 assert(newevents_len >= 0);
535                 if (newevents_len < 0)
536                         newevents_len = 0;
537 #ifdef DEBUG
538                 if (newevents_len)
539                         assert(newevents);
540 #endif
541
542                 ret = kevent(io_masterfd, newevents, newevents_len, kev,
543                              100, &ts);
544                 if ((newevents_len>0) && ret != -1)
545                         array_trunc(&io_evcache);
546
547                 total += ret;
548                 if (ret <= 0)
549                         return total;
550
551                 for (i = 0; i < ret; i++) {
552                         type = 0;
553                         if (kev[i].flags & EV_EOF)
554                                 type = IO_ERROR;
555
556                         if (kev[i].filter & EV_ERROR)
557                                 type = IO_ERROR;
558
559                         if (kev[i].filter & EVFILT_READ)
560                                 type |= IO_WANTREAD;
561
562                         if (kev[i].filter & EVFILT_WRITE)
563                                 type |= IO_WANTWRITE;
564
565                         io_docallback(kev[i].ident, type);
566                 }
567
568                 ts.tv_sec = 0;
569                 ts.tv_nsec = 0;
570
571         } while (ret == 100);
572
573         return total;
574 }
575 #endif
576
577
578 int
579 io_dispatch(struct timeval *tv)
580 {
581 #ifdef IO_USE_SELECT
582         return io_dispatch_select(tv);
583 #endif
584 #ifdef IO_USE_KQUEUE
585         return io_dispatch_kqueue(tv);
586 #endif
587 #ifdef IO_USE_EPOLL
588         return io_dispatch_epoll(tv);
589 #endif
590 }
591
592
593 /* call the callback function inside the struct matching fd */
594 static void
595 io_docallback(int fd, short what)
596 {
597         io_event *i;
598 #ifdef DEBUG
599         Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
600 #endif
601         i = io_event_get(fd);
602         assert(i);
603
604         if (i->callback)        /* callback might be 0 if previous callback function called io_close on this fd */
605                 i->callback(fd, (what & IO_ERROR) ? i->what : what);
606         /* if error indicator is set, we return the event(s) the app asked for */
607 }