Skip to content

Commit

Permalink
ModDevPoll.cc cleanup as discussed in squid-cache#1551
Browse files Browse the repository at this point in the history
Also partially addresses https://bugs.squid-cache.org/show_bug.cgi?id=5314
by replacing the devpoll_state array (that was sized to SQUID_MAXFD) with
use of the epoll_state member of the fde structure, to match ModEpoll, but
doesn't solve the problem of devpoll_fd not being opened in that path.
  • Loading branch information
alanc committed Oct 29, 2023
1 parent 4947672 commit adfb320
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions src/comm/ModDevPoll.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
*
* Ported by Peter Payne from Squid 2.7.STABLE9 comm_devpoll.c
* on August 11, 2010 at 3pm (GMT+0100 Europe/London).
*
* Last modified 2010-10-08
*/

re
/*
* There are several poll types in Squid, ALL of which are compiled and linked
* in. Thus conditional compile-time flags are used to prevent the different
Expand Down Expand Up @@ -48,18 +46,15 @@

#define DEBUG_DEVPOLL 0

#define DEVPOLL_UPDATESIZE Squid_MaxFD
#define DEVPOLL_QUERYSIZE Squid_MaxFD
// Maximum number of fd's to buffer requests to change polling state
#define DEVPOLL_UPDATESIZE 256
// Maximum number of fd's to receive polling updates for in a single syscall
#define DEVPOLL_QUERYSIZE 1024

/* TYPEDEFS */
typedef short pollfd_events_t; /* type of pollfd.events from sys/poll.h */

/* STRUCTURES */
/** \brief Current state */
struct _devpoll_state {
pollfd_events_t state; /**< current known state of file handle */
};

/** \brief Update list
*
* This structure contains an array of settings to send to the /dev/poll
Expand All @@ -78,7 +73,6 @@ static struct {
static int devpoll_fd; /**< handle to /dev/poll device */
static int max_poll_time = 1000; /**< maximum milliseconds to spend in poll */

static struct _devpoll_state *devpoll_state; /**< array of socket states */
static struct dvpoll do_poll; /**< data struct for storing poll results */
static int dpoll_nfds; /**< maximum number of poll results */

Expand Down Expand Up @@ -127,7 +121,7 @@ comm_flush_updates(void)
* @param events events to register (usually POLLIN, POLLOUT, or POLLREMOVE)
*/
static void
comm_update_fd(int fd, int events)
comm_update_fd(int fd, pollfd_events_t events)
{
debugs(
5,
Expand Down Expand Up @@ -175,22 +169,24 @@ void
Comm::SelectLoopInit(void)
{
/* allocate memory first before attempting to open poll device */
/* This tracks the FD devpoll offset+state */
devpoll_state = (struct _devpoll_state *)xcalloc(
SQUID_MAXFD, sizeof(struct _devpoll_state)
);

/* And this is the stuff we use to read events */
/* This is the stuff we use to read events. If it's larger than
the current RLIMIT_NOFILE, the Solaris kernel returns EINVAL. */
dpoll_nfds = DEVPOLL_QUERYSIZE;
if (dpoll_nfds > Squid_MaxFD)
dpoll_nfds = Squid_MaxFD;
do_poll.dp_fds = (struct pollfd *)xcalloc(
DEVPOLL_QUERYSIZE, sizeof(struct pollfd)
dpoll_nfds, sizeof(struct pollfd)
);
dpoll_nfds = DEVPOLL_QUERYSIZE;

devpoll_update.pfds = (struct pollfd *)xcalloc(
DEVPOLL_UPDATESIZE, sizeof(struct pollfd)
);
/* This is the stuff we use to write requests to change tracking state.
It's also limited to the current RLIMIT_NOFILE by the Solaris kernel. */
devpoll_update.cur = -1;
devpoll_update.size = DEVPOLL_UPDATESIZE;
if (devpoll_update.size > Squid_MaxFD)
devpoll_update.size = Squid_MaxFD;
devpoll_update.pfds = (struct pollfd *)xcalloc(
devpoll_update.size, sizeof(struct pollfd)
);

/* attempt to open /dev/poll device */
devpoll_fd = open("/dev/poll", O_RDWR);
Expand Down Expand Up @@ -231,11 +227,11 @@ Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time
if (!F->flags.open) {
/* remove from poll set */
comm_update_fd( fd, POLLREMOVE );
devpoll_state[fd].state = 0;
F->epoll_state = 0;
return;
}

pollfd_events_t state_old = devpoll_state[fd].state;
pollfd_events_t state_old = (pollfd_events_t) F->epoll_state;
pollfd_events_t state_new = 0; /* new state (derive from old state) */

if ( type & COMM_SELECT_READ ) {
Expand Down Expand Up @@ -286,7 +282,7 @@ Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time
comm_update_fd( fd, newly_set_only );
}

devpoll_state[fd].state = state_new;
F->epoll_state = state_new;
}

if (timeout)
Expand Down Expand Up @@ -351,7 +347,7 @@ Comm::DoSelect(int msec)
DEBUG_DEVPOLL ? 0 : 8,
"got FD " << fd
<< ",events=" << std::hex << do_poll.dp_fds[i].revents
<< ",monitoring=" << devpoll_state[fd].state
<< ",monitoring=" << F->epoll_state
<< ",F->read_handler=" << F->read_handler
<< ",F->write_handler=" << F->write_handler
);
Expand Down

0 comments on commit adfb320

Please sign in to comment.