]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/io.c
Add Doxygen @file documentation to each source and header file
[ngircd-alex.git] / src / ngircd / io.c
index 34066b2b3958928910e588cf6ba879b99caeb722..fb39ecdd99f0b07cb96c6afd61ef05a40468eaa3 100644 (file)
@@ -5,14 +5,16 @@
  * (at your option) any later version.
  * Please read the file COPYING, README and AUTHORS for more information.
  *
- * I/O abstraction interface.
  * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
  *
  */
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: io.c,v 1.29 2008/03/27 15:47:21 fw Exp $";
+/**
+ * @file
+ * I/O abstraction interface.
+ */
 
 #include <assert.h>
 #include <stdlib.h>
@@ -104,14 +106,18 @@ static bool io_event_change_devpoll(int fd, short what);
 
 #ifdef IO_USE_SELECT
 #include "defines.h"   /* for conn.h */
-#include "conn.h"      /* for CONN_IDX (needed by resolve.h) */
-#include "resolve.h"   /* for RES_STAT (needed by conf.h) */
+#include "proc.h"      /* for PROC_STAT (needed by conf.h) */
+#include "conn.h"      /* for CONN_ID (needed by conf.h) */
 #include "conf.h"      /* for Conf_MaxConnections */
 
 static fd_set readers;
 static fd_set writers;
-static int select_maxfd;               /* the select() interface sucks badly */
-static int io_dispatch_select(struct timeval *tv);
+/*
+ * this is the first argument for select(), i.e.
+ * the largest fd registered, plus one.
+ */
+static int select_maxfd;
+static int io_dispatch_select PARAMS((struct timeval *tv));
 
 #ifndef IO_USE_EPOLL
 #define io_masterfd -1
@@ -123,12 +129,15 @@ static array io_events;
 static void io_docallback PARAMS((int fd, short what));
 
 #ifdef DEBUG_IO
-static void io_debug(const char *s, int fd, int what)
+static void
+io_debug(const char *s, int fd, int what)
 {
        Log(LOG_DEBUG, "%s: %d, %d\n", s, fd, what);
 }
 #else
-static inline void io_debug(const char UNUSED *s,int UNUSED a, int UNUSED b) {/*NOTHING*/}
+static inline void
+io_debug(const char UNUSED *s,int UNUSED a, int UNUSED b)
+{ /* NOTHING */ }
 #endif
 
 static io_event *
@@ -223,8 +232,12 @@ io_library_init_devpoll(unsigned int eventsize)
                eventsize, io_masterfd);
 }
 #else
-static inline void io_close_devpoll(int UNUSED x) {/* NOTHING */}
-static inline void io_library_init_devpoll(unsigned int UNUSED ev) {/*NOTHING*/}
+static inline void
+io_close_devpoll(int UNUSED x)
+{ /* NOTHING */ }
+static inline void
+io_library_init_devpoll(unsigned int UNUSED ev)
+{ /* NOTHING */ }
 #endif
 
 
@@ -327,8 +340,12 @@ io_library_init_poll(unsigned int eventsize)
        }
 }
 #else
-static inline void io_close_poll(int UNUSED x) {/* NOTHING */}
-static inline void io_library_init_poll(unsigned int UNUSED ev) {/*NOTHING*/}
+static inline void
+io_close_poll(int UNUSED x)
+{ /* NOTHING */ }
+static inline void
+io_library_init_poll(unsigned int UNUSED ev)
+{ /* NOTHING */ }
 #endif
 
 
@@ -336,11 +353,15 @@ static inline void io_library_init_poll(unsigned int UNUSED ev) {/*NOTHING*/}
 static int
 io_dispatch_select(struct timeval *tv)
 {
-       fd_set readers_tmp = readers;
-       fd_set writers_tmp = writers;
+       fd_set readers_tmp;
+       fd_set writers_tmp;
        short what;
        int ret, i;
        int fds_ready;
+
+       readers_tmp = readers;
+       writers_tmp = writers;
+
        ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
        if (ret <= 0)
                return ret;
@@ -384,6 +405,9 @@ io_library_init_select(unsigned int eventsize)
 
                Conf_MaxConnections = FD_SETSIZE - 1;
        }
+#else
+       Log(LOG_WARNING,
+           "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
 #endif /* FD_SETSIZE */
        library_initialized = true;
 }
@@ -411,8 +435,12 @@ io_close_select(int fd)
        }
 }
 #else
-static inline void io_library_init_select(int UNUSED x) {/* NOTHING */}
-static inline void io_close_select(int UNUSED x) {/* NOTHING */}
+static inline void
+io_library_init_select(int UNUSED x)
+{ /* NOTHING */ }
+static inline void
+io_close_select(int UNUSED x)
+{ /* NOTHING */ }
 #endif /* SELECT */
 
 
@@ -487,7 +515,9 @@ io_library_init_epoll(unsigned int eventsize)
 #endif
 }
 #else
-static inline void io_library_init_epoll(unsigned int UNUSED ev) {/* NOTHING */}
+static inline void
+io_library_init_epoll(unsigned int UNUSED ev)
+{ /* NOTHING */ }
 #endif /* IO_USE_EPOLL */
 
 
@@ -613,7 +643,9 @@ io_library_init_kqueue(unsigned int eventsize)
                library_initialized = true;
 }
 #else
-static inline void io_library_init_kqueue(unsigned int UNUSED ev) {/* NOTHING */}
+static inline void
+io_library_init_kqueue(unsigned int UNUSED ev)
+{ /* NOTHING */ }
 #endif
 
 
@@ -622,15 +654,7 @@ io_library_init(unsigned int eventsize)
 {
        if (library_initialized)
                return true;
-#ifdef IO_USE_SELECT
-#ifndef FD_SETSIZE
-       Log(LOG_WARNING,
-           "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
-#else
-       if (eventsize >= FD_SETSIZE)
-               eventsize = FD_SETSIZE - 1;
-#endif /* FD_SETSIZE */
-#endif /* IO_USE_SELECT */
+
        if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
                eventsize = 0;
 
@@ -707,7 +731,7 @@ io_event_create(int fd, short what, void (*cbfunc) (int, short))
 
        assert(fd >= 0);
 #if defined(IO_USE_SELECT) && defined(FD_SETSIZE)
-       if (fd >= FD_SETSIZE) {
+       if (io_masterfd < 0 && fd >= FD_SETSIZE) {
                Log(LOG_ERR,
                    "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
                    fd, FD_SETSIZE);
@@ -786,6 +810,18 @@ io_setnonblock(int fd)
        return fcntl(fd, F_SETFL, flags) == 0;
 }
 
+bool
+io_setcloexec(int fd)
+{
+       int flags = fcntl(fd, F_GETFD);
+       if (flags == -1)
+               return false;
+#ifdef FD_CLOEXEC
+       flags |= FD_CLOEXEC;
+#endif
+
+       return fcntl(fd, F_SETFD, flags) == 0;
+}
 
 bool
 io_close(int fd)