* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
- * $Id: commio.c 3237 2007-03-05 17:35:17Z nenolod $
+ * $Id: commio.c 3354 2007-04-03 09:21:31Z nenolod $
*/
#include "libcharybdis.h"
static PF comm_connect_timeout;
static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply);
static PF comm_connect_tryconnect;
+static int comm_max_connections = 0;
+
+static int
+comm_read_raw(fde_t *F, void *buf, size_t count)
+{
+ s_assert(F != NULL);
+ s_assert(buf != NULL);
+ s_assert(count > 0);
+
+ return read(F->fd, buf, count);
+}
+
+static int
+comm_write_raw(fde_t *F, const void *buf, size_t count)
+{
+ s_assert(F != NULL);
+ s_assert(buf != NULL);
+ s_assert(count > 0);
+
+ return write(F->fd, buf, count);
+}
inline fde_t *
comm_locate_fd(int fd)
if (F != NULL)
return F;
- F = calloc(sizeof(fde_t), 1);
+ F = MyMalloc(sizeof(fde_t));
F->fd = fd;
+
+ F->read_impl = comm_read_raw;
+ F->write_impl = comm_write_raw;
+
list = &fd_table[fd % FD_HASH_SIZE];
dlinkAdd(F, &F->node, list);
int fd;
#endif
- /* XXX someone tell me why we care about 4 fd's ? */
- /* XXX btw, fd 3 is used for profiler ! */
-
- for (i = 4; i < MAXCONNECTIONS; ++i)
+ /*
+ * we start at 4 to avoid giving fds where malloc messages
+ * could be written --nenolod
+ */
+ for (i = 4; i < comm_max_connections; ++i)
{
fde_t *F = comm_locate_fd(i);
{
int fd;
/* First, make sure we aren't going to run out of file descriptors */
- if(number_fd >= MASTER_MAX)
+ if(number_fd >= comm_max_connections)
{
errno = ENFILE;
return -1;
comm_accept(int fd, struct sockaddr *pn, socklen_t *addrlen)
{
int newfd;
- if(number_fd >= MASTER_MAX)
+ if(number_fd >= comm_max_connections)
{
errno = ENFILE;
return -1;
{
if(fd < highest_fd)
return;
- s_assert(fd < MAXCONNECTIONS);
+ s_assert(fd < comm_max_connections);
if(fd > highest_fd)
{
* re-opening it
*/
s_assert(!opening);
- while (highest_fd >= 0 && comm_locate_fd(fd)->flags.open) /* XXX */
+ while (highest_fd >= 0 && comm_locate_fd(fd) != NULL)
highest_fd--;
}
fdlist_init(void)
{
static int initialized = 0;
+ struct rlimit limit;
if(!initialized)
{
memset(&fd_table, '\0', sizeof(dlink_list) * FD_HASH_SIZE);
+
+ /* set up comm_max_connections. */
+ if(!getrlimit(RLIMIT_NOFILE, &limit))
+ comm_max_connections = limit.rlim_cur;
+
initialized = 1;
}
}
}
comm_setselect(F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
comm_setflush(F->fd, 0, NULL, NULL);
+ F->timeout = 0;
if (F->dns_query != NULL)
{
number_fd--;
comm_remove_fd(fd);
- F->timeout = 0;
/* Unlike squid, we're actually closing the FD here! -- adrian */
close(fd);
}
F->desc[0] = '\0';
}
+extern int
+comm_get_maxconnections(void)
+{
+ fdlist_init();
+ return comm_max_connections;
+}