2006-06-25 20:48:02 -04:00
|
|
|
/*
|
|
|
|
|
* File descriptors management functions.
|
|
|
|
|
*
|
2007-04-08 10:39:58 -04:00
|
|
|
* Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
|
2006-06-25 20:48:02 -04:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
#include <string.h>
|
2006-06-25 20:48:02 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
2006-06-29 11:53:05 -04:00
|
|
|
#include <common/compat.h>
|
|
|
|
|
#include <common/config.h>
|
2006-06-25 20:48:02 -04:00
|
|
|
|
|
|
|
|
#include <types/fd.h>
|
|
|
|
|
#include <types/global.h>
|
|
|
|
|
|
2006-10-15 08:52:29 -04:00
|
|
|
#include <proto/fd.h>
|
2006-06-25 20:48:02 -04:00
|
|
|
|
|
|
|
|
struct fdtab *fdtab = NULL; /* array of all the file descriptors */
|
|
|
|
|
int maxfd; /* # of the highest fd + 1 */
|
|
|
|
|
int totalconn; /* total # of terminated sessions */
|
|
|
|
|
int actconn; /* # of active sessions */
|
|
|
|
|
|
|
|
|
|
int cfg_polling_mechanism = 0; /* POLL_USE_{SELECT|POLL|EPOLL} */
|
|
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
struct poller pollers[MAX_POLLERS];
|
|
|
|
|
struct poller cur_poller;
|
|
|
|
|
int nbpollers = 0;
|
2006-06-25 20:48:02 -04:00
|
|
|
|
2006-10-15 08:52:29 -04:00
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
/*********************
|
|
|
|
|
* generic functions
|
|
|
|
|
*********************/
|
2006-10-15 08:52:29 -04:00
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
extern int select_register(struct poller *p);
|
|
|
|
|
#if defined(ENABLE_POLL)
|
|
|
|
|
extern int poll_register(struct poller *p);
|
2006-10-15 08:52:29 -04:00
|
|
|
#endif
|
2006-06-25 20:48:02 -04:00
|
|
|
#if defined(ENABLE_EPOLL)
|
2007-04-08 10:39:58 -04:00
|
|
|
extern int epoll_register(struct poller *p);
|
2006-06-25 20:48:02 -04:00
|
|
|
#endif
|
2007-04-09 10:11:49 -04:00
|
|
|
#if defined(ENABLE_KQUEUE)
|
|
|
|
|
extern int kqueue_register(struct poller *p);
|
|
|
|
|
#endif
|
2006-06-25 20:48:02 -04:00
|
|
|
|
|
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
|
|
|
|
|
* The file descriptor is also closed.
|
|
|
|
|
*/
|
|
|
|
|
void fd_delete(int fd)
|
2006-06-25 20:48:02 -04:00
|
|
|
{
|
2007-04-08 10:39:58 -04:00
|
|
|
EV_FD_CLO(fd);
|
|
|
|
|
close(fd);
|
|
|
|
|
fdtab[fd].state = FD_STCLOSE;
|
2006-06-25 20:48:02 -04:00
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE))
|
|
|
|
|
maxfd--;
|
2006-06-25 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
/* registers all known pollers */
|
|
|
|
|
void register_pollers()
|
2006-06-25 20:48:02 -04:00
|
|
|
{
|
2007-04-08 10:39:58 -04:00
|
|
|
if (select_register(&pollers[nbpollers]))
|
|
|
|
|
nbpollers++;
|
|
|
|
|
#if defined(ENABLE_POLL)
|
|
|
|
|
poll_register(&pollers[nbpollers]);
|
|
|
|
|
nbpollers++;
|
2006-06-25 20:48:02 -04:00
|
|
|
#endif
|
|
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
#if defined(ENABLE_EPOLL)
|
|
|
|
|
epoll_register(&pollers[nbpollers]);
|
|
|
|
|
nbpollers++;
|
2006-06-25 20:48:02 -04:00
|
|
|
#endif
|
2007-04-09 10:11:49 -04:00
|
|
|
|
|
|
|
|
#if defined(ENABLE_KQUEUE)
|
|
|
|
|
kqueue_register(&pollers[nbpollers]);
|
|
|
|
|
nbpollers++;
|
|
|
|
|
#endif
|
2007-04-08 10:39:58 -04:00
|
|
|
}
|
2006-06-25 20:48:02 -04:00
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
/* disable the specified poller */
|
|
|
|
|
void disable_poller(const char *poller_name)
|
|
|
|
|
{
|
|
|
|
|
int p;
|
2006-06-25 20:48:02 -04:00
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
for (p = 0; p < nbpollers; p++)
|
|
|
|
|
if (strcmp(pollers[p].name, poller_name) == 0)
|
|
|
|
|
pollers[p].pref = 0;
|
|
|
|
|
}
|
2006-06-25 20:48:02 -04:00
|
|
|
|
|
|
|
|
/*
|
2007-04-08 10:39:58 -04:00
|
|
|
* Initialize the pollers till the best one is found.
|
|
|
|
|
* If none works, returns 0, otherwise 1.
|
2006-06-25 20:48:02 -04:00
|
|
|
*/
|
2007-04-08 10:39:58 -04:00
|
|
|
int init_pollers()
|
2006-06-25 20:48:02 -04:00
|
|
|
{
|
2007-04-08 10:39:58 -04:00
|
|
|
int p;
|
|
|
|
|
struct poller *bp;
|
2006-06-25 20:48:02 -04:00
|
|
|
|
|
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
do {
|
|
|
|
|
bp = NULL;
|
|
|
|
|
for (p = 0; p < nbpollers; p++)
|
|
|
|
|
if (!bp || (pollers[p].pref > bp->pref))
|
|
|
|
|
bp = &pollers[p];
|
2006-06-25 20:48:02 -04:00
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
if (!bp || bp->pref == 0)
|
2006-06-25 20:48:02 -04:00
|
|
|
break;
|
|
|
|
|
|
2007-04-08 10:39:58 -04:00
|
|
|
if (bp->init(bp)) {
|
|
|
|
|
memcpy(&cur_poller, bp, sizeof(*bp));
|
|
|
|
|
return 1;
|
2006-06-25 20:48:02 -04:00
|
|
|
}
|
2007-04-08 10:39:58 -04:00
|
|
|
} while (!bp || bp->pref == 0);
|
|
|
|
|
return 0;
|
2006-06-25 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Local variables:
|
|
|
|
|
* c-indent-level: 8
|
|
|
|
|
* c-basic-offset: 8
|
|
|
|
|
* End:
|
|
|
|
|
*/
|