- Debug((DEBUG_INFO, "poll: %d %d", delay, delay2));
-
- timeout = (IRCD_MIN(delay2, delay)) * 1000;
-
- nfds = poll(poll_fds, pfd_count, timeout);
-
- CurrentTime = time(0);
- if (-1 < nfds)
- break;
-
- if (EINTR == errno)
- return -1;
- report_error(POLL_ERROR_MSG, me.name, errno);
- ++res;
- if (res > 5)
- server_restart("too many poll errors");
- sleep(1);
- CurrentTime = time(0);
- }
-
- if (uping_pfd && (uping_pfd->revents & (POLLREADFLAGS | POLLERRORS))) {
- uping_echo();
- --nfds;
- }
- /*
- * check uping replies
- */
- for (uping = uping_begin(); uping; uping = uping_next) {
- uping_next = uping->next;
- if (uping->active) {
- assert(-1 < uping->index);
- if (poll_fds[uping->index].revents) {
- uping_read(uping);
- if (0 == --nfds)
- break;
- }
- else if (CurrentTime > uping->lastsent) {
- uping->lastsent = CurrentTime;
- uping_send(uping);
- }
- }
- }
-
- if (res_pfd && (res_pfd->revents & (POLLREADFLAGS | POLLERRORS))) {
- resolver_read();
- --nfds;
- }
- /*
- * check auth queries
- */
- for (auth = AuthPollList; auth; auth = auth_next) {
- auth_next = auth->next;
- i = auth->index;
- /*
- * check for any event, we only ask for one at a time
- */
- if (poll_fds[i].revents) {
- if (IsAuthConnect(auth))
- send_auth_query(auth);
- else
- read_auth_reply(auth);
- if (0 == --nfds)
- break;
- }
- }
- /*
- * check listeners
- */
- for (listener = ListenerPollList; listener; listener = listener->next) {
- i = listener->index;
- if (poll_fds[i].revents) {
- accept_connection(listener);
- if (0 == --nfds)
- break;
- }
- }
- /*
- * i contains the next non-auth/non-listener index, since we put the
- * resolver, auth and listener, file descriptors in poll_fds first,
- * the very next one should be the start of the clients
- */
- pfd = &poll_fds[++i];
-
- for ( ; (i < pfd_count); ++i, ++pfd) {
- if (!(cptr = LocalClientArray[pfd->fd]))
- continue;
- read_ready = write_ready = 0;
-
- if (0 < nfds && pfd->revents) {
- --nfds;
-
- read_ready = pfd->revents & POLLREADFLAGS;
- write_ready = pfd->revents & POLLWRITEFLAGS;
-
- if (pfd->revents & POLLERRORS) {
- if (pfd->events & POLLREADFLAGS)
- ++read_ready;
- if (pfd->events & POLLWRITEFLAGS)
- ++write_ready;
- }
- }
- if (write_ready) {
- if (!on_write_unblocked(cptr) || IsDead(cptr)) {
- const char* msg = (cptr->error) ? strerror(cptr->error) : cptr->info;
- if (!msg)
- msg = "Unknown error";
- exit_client(cptr, cptr, &me, msg);
- continue;
- }
- }
- length = 1; /* for fall through case */
- if ((!NoNewLine(cptr) || read_ready) && !IsDead(cptr)) {
- if (CPTR_KILLED == (length = read_packet(cptr, read_ready)))
- continue;
- }
-#if 0
- /* Bullshit, why would we want to flush sockets while using non-blocking?
- * This uses > 4% cpu! --Run */
- if (length > 0)
- flush_connections(poll_cptr[i]);
-#endif
- if (IsDead(cptr)) {
- const char* msg = (cptr->error) ? strerror(cptr->error) : cptr->info;
- if (!msg)
- msg = "Unknown error";
- exit_client(cptr, cptr, &me, (char*) msg);
- continue;
- }
- if (length > 0)
- continue;
- cptr->flags |= FLAGS_DEADSOCKET;
- /*
- * ...hmm, with non-blocking sockets we might get
- * here from quite valid reasons, although.. why
- * would select report "data available" when there
- * wasn't... So, this must be an error anyway... --msa
- * actually, EOF occurs when read() returns 0 and
- * in due course, select() returns that fd as ready
- * for reading even though it ends up being an EOF. -avalon
- */
- Debug((DEBUG_ERROR, "READ ERROR: fd = %d %d %d", pfd->fd, errno, length));
-
- if ((IsServer(cptr) || IsHandshake(cptr)) && cptr->error == 0 && length == 0)
- exit_client_msg(cptr, cptr, &me, "Server %s closed the connection (%s)",
- cptr->name, cptr->serv->last_error_msg);
- else {
- const char* msg = (cptr->error) ? strerror(cptr->error) : "EOF from client";
- if (!msg)
- msg = "Unknown error";
- exit_client_msg(cptr, cptr, &me, "Read error: %s", msg);
- }
- }
- return 0;
-}
-#else /* USE_SELECT */
-
-/*
- * Check all connections for new connections and input data that is to be
- * processed. Also check for connections with data queued and whether we can
- * write it out.
- *
- * Don't ever use ZERO for `delay', unless you mean to poll and then
- * you have to have sleep/wait somewhere else in the code.--msa
- */
-int read_message(time_t delay)
-{
- struct Client* cptr;
- struct Listener* listener;
- struct AuthRequest* auth = 0;
- struct AuthRequest* auth_next = 0;
- struct UPing* uping;
- struct UPing* uping_next;
- int nfds;
- struct timeval wait;
- time_t delay2 = delay;
- unsigned int usec = 0;
- int res = 0;
- int length;
- int i;
- int read_ready;
- fd_set read_set;
- fd_set write_set;
-
- for ( ; ; )
- {
- FD_ZERO(&read_set);
- FD_ZERO(&write_set);
-
- if (-1 < ResolverFileDescriptor)
- FD_SET(ResolverFileDescriptor, &read_set);
- if (-1 < UPingFileDescriptor)
- FD_SET(UPingFileDescriptor, &read_set);
- /*
- * set up uping file descriptors
- */
- for (uping = uping_begin(); uping; uping = uping_next) {
- uping_next = uping->next;
- if (uping->active) {
- delay2 = 1;
- if (uping->lastsent && CurrentTime > uping->timeout) {
- uping_end(uping);
- continue;
- }
- assert(-1 < uping->fd);
- FD_SET(uping->fd, &read_set);
- }
- }
- /*
- * set auth file descriptors
- */
- for (auth = AuthPollList; auth; auth = auth->next) {
- assert(-1 < auth->fd);
- if (IsAuthConnect(auth))
- FD_SET(auth->fd, &write_set);
- else /* if (IsAuthPending(auth)) */
- FD_SET(auth->fd, &read_set);
- }
- /*
- * set listener file descriptors
- */
- for (listener = ListenerPollList; listener; listener = listener->next) {
- assert(-1 < listener->fd);
- FD_SET(listener->fd, &read_set);
- }
-
- for (i = HighestFd; i > -1; --i) {
- if ((cptr = LocalClientArray[i])) {
- if (DBufLength(&cptr->recvQ))
- delay2 = 1;
- if (DBufLength(&cptr->recvQ) < 4088 || IsServer(cptr))
- FD_SET(i, &read_set);
- if (DBufLength(&cptr->sendQ) || IsConnecting(cptr) ||
- (cptr->listing && DBufLength(&cptr->sendQ) < 2048))
- FD_SET(i, &write_set);
- }
- }
-
- wait.tv_sec = IRCD_MIN(delay2, delay);
- wait.tv_usec = usec;
-
- Debug((DEBUG_INFO, "select: %d %d", delay, delay2));
-
- nfds = select(FD_SETSIZE, &read_set, &write_set, 0, &wait);
-
- CurrentTime = time(0);
-
- if (-1 < nfds)
- break;
-
- if (errno == EINTR)
- return -1;
- report_error(SELECT_ERROR_MSG, me.name, errno);
- if (++res > 5)
- server_restart("too many select errors");
- sleep(1);
- CurrentTime = time(0);
- }
-
- if (-1 < UPingFileDescriptor && FD_ISSET(UPingFileDescriptor, &read_set)) {
- uping_echo();
- --nfds;
- }
- for (uping = uping_begin(); uping; uping = uping_next) {
- uping_next = uping->next;
- if (uping->active) {
- assert(-1 < uping->fd);
- if (FD_ISSET(uping->fd, &read_set)) {
- uping_read(uping);
- if (0 == --nfds)
- break;
- }
- else if (CurrentTime > uping->lastsent) {
- uping->lastsent = CurrentTime;
- uping_send(uping);
- }
- }
- }
- if (-1 < ResolverFileDescriptor && FD_ISSET(ResolverFileDescriptor, &read_set)) {
- resolver_read();
- --nfds;
- }
- /*
- * Check fd sets for the auth fd's (if set and valid!) first
- * because these can not be processed using the normal loops below.
- * -avalon
- */
- for (auth = AuthPollList; auth; auth = auth_next) {
- auth_next = auth->next;
- assert(-1 < auth->fd);
- if (IsAuthConnect(auth) && FD_ISSET(auth->fd, &write_set)) {
- send_auth_query(auth);
- if (0 == --nfds)
- break;
- }
- else if (FD_ISSET(auth->fd, &read_set)) {
- read_auth_reply(auth);
- if (0 == --nfds)
- break;
- }
- }
- /*
- * next accept connections from active listeners
- */
- for (listener = ListenerPollList; listener; listener = listener->next) {
- assert(-1 < listener->fd);
- if (0 < nfds && FD_ISSET(listener->fd, &read_set))
- accept_connection(listener);
- }
-
- for (i = HighestFd; -1 < i; --i) {
- if (!(cptr = LocalClientArray[i]))
- continue;
- read_ready = 0;
- if (0 < nfds) {
- if (FD_ISSET(i, &write_set)) {
- --nfds;
- if (!on_write_unblocked(cptr) || IsDead(cptr)) {
- const char* msg = (cptr->error) ? strerror(cptr->error) : cptr->info;
- if (!msg)
- msg = "Unknown error";
- if (FD_ISSET(i, &read_set))
- --nfds;
- exit_client(cptr, cptr, &me, msg);
- continue;
- }
- }
- if ((read_ready = FD_ISSET(i, &read_set)))
- --nfds;
- }
- length = 1; /* for fall through case */
- if ((!NoNewLine(cptr) || read_ready) && !IsDead(cptr)) {
- if (CPTR_KILLED == (length = read_packet(cptr, read_ready)))
- continue;
- }
- if (IsDead(cptr)) {
- const char* msg = (cptr->error) ? strerror(cptr->error) : cptr->info;
- if (!msg)
- msg = "Unknown error";
- exit_client(cptr, cptr, &me, msg);
- continue;
- }
- if (length > 0)
- continue;
-
- /*
- * ...hmm, with non-blocking sockets we might get
- * here from quite valid reasons, although.. why
- * would select report "data available" when there
- * wasn't... So, this must be an error anyway... --msa
- * actually, EOF occurs when read() returns 0 and
- * in due course, select() returns that fd as ready
- * for reading even though it ends up being an EOF. -avalon
- */
- Debug((DEBUG_ERROR, "READ ERROR: fd = %d %d %d", i, cptr->error, length));
-
- if ((IsServer(cptr) || IsHandshake(cptr)) && cptr->error == 0 && length == 0)
- exit_client_msg(cptr, cptr, &me, "Server %s closed the connection (%s)",
- cptr->name, cptr->serv->last_error_msg);
- else {
- const char* msg = (cptr->error) ? strerror(cptr->error) : "EOF from client";
- if (!msg)
- msg = "Unknown error";
- exit_client_msg(cptr, cptr, &me, "Read error: %s", msg);