/* Update polling on connection <c>'s file descriptor depending on its current * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*. * The connection flags are updated with the new flags at the end of the * operation. */ void conn_update_sock_polling(struct connection *c) { unsigned int f = c->flags; /* update read status if needed */ if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) { f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL); fd_stop_recv(c->t.sock.fd); } else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) && (f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) { f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL); fd_poll_recv(c->t.sock.fd); } else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) { f |= CO_FL_CURR_RD_ENA; fd_want_recv(c->t.sock.fd); } /* update write status if needed */ if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) { f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL); fd_stop_send(c->t.sock.fd); } else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) && (f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) { f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL); fd_poll_send(c->t.sock.fd); } else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) { f |= CO_FL_CURR_WR_ENA; fd_want_send(c->t.sock.fd); } c->flags = f; }
/* Update polling on connection <c>'s file descriptor depending on its current * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*. * The connection flags are updated with the new flags at the end of the * operation. Polling is totally disabled if an error was reported. */ void conn_update_sock_polling(struct connection *c) { unsigned int f = c->flags; if (!conn_ctrl_ready(c)) return; /* update read status if needed */ if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) { fd_want_recv(c->t.sock.fd); f |= CO_FL_CURR_RD_ENA; } else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) { fd_stop_recv(c->t.sock.fd); f &= ~CO_FL_CURR_RD_ENA; } /* update write status if needed */ if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) { fd_want_send(c->t.sock.fd); f |= CO_FL_CURR_WR_ENA; } else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) { fd_stop_send(c->t.sock.fd); f &= ~CO_FL_CURR_WR_ENA; } c->flags = f; }
/* Marks a ready listener as limited so that we only try to re-enable it when * resources are free again. It will be queued into the specified queue. */ void limit_listener(struct listener *l, struct list *list) { if (l->state == LI_READY) { LIST_ADDQ(list, &l->wait_queue); fd_stop_recv(l->fd); l->state = LI_LIMITED; } }
/* This function removes the specified listener's file descriptor from the * polling lists if it is in the LI_READY or in the LI_FULL state. The listener * enters LI_LISTEN. */ void disable_listener(struct listener *listener) { if (listener->state < LI_READY) return; if (listener->state == LI_READY) fd_stop_recv(listener->fd); if (listener->state == LI_LIMITED) LIST_DEL(&listener->wait_queue); listener->state = LI_LISTEN; }
/* Marks a ready listener as full so that the session code tries to re-enable * it upon next close() using resume_listener(). */ void listener_full(struct listener *l) { if (l->state >= LI_READY) { if (l->state == LI_LIMITED) LIST_DEL(&l->wait_queue); fd_stop_recv(l->fd); l->state = LI_FULL; } }
/* This function adds the specified listener's file descriptor to the polling * lists if it is in the LI_LISTEN state. The listener enters LI_READY or * LI_FULL state depending on its number of connections. In deamon mode, we * also support binding only the relevant processes to their respective * listeners. We don't do that in debug mode however. */ void enable_listener(struct listener *listener) { if (listener->state == LI_LISTEN) { if ((global.mode & (MODE_DAEMON | MODE_SYSTEMD)) && listener->bind_conf->bind_proc && !(listener->bind_conf->bind_proc & (1UL << (relative_pid - 1)))) { /* we don't want to enable this listener and don't * want any fd event to reach it. */ fd_stop_recv(listener->fd); listener->state = LI_PAUSED; } else if (listener->nbconn < listener->maxconn) { fd_want_recv(listener->fd); listener->state = LI_READY; } else { listener->state = LI_FULL; } } }
/* This function tries to temporarily disable a listener, depending on the OS * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but * closes upon SHUT_WR and refuses to rebind. So a common validation path * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling * is disabled. It normally returns non-zero, unless an error is reported. */ int pause_listener(struct listener *l) { if (l->state <= LI_PAUSED) return 1; if (shutdown(l->fd, SHUT_WR) != 0) return 0; /* Solaris dies here */ if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) return 0; /* OpenBSD dies here */ if (shutdown(l->fd, SHUT_RD) != 0) return 0; /* should always be OK */ if (l->state == LI_LIMITED) LIST_DEL(&l->wait_queue); fd_stop_recv(l->fd); l->state = LI_PAUSED; return 1; }
/* This function tries to temporarily disable a listener, depending on the OS * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but * closes upon SHUT_WR and refuses to rebind. So a common validation path * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling * is disabled. It normally returns non-zero, unless an error is reported. */ int pause_listener(struct listener *l) { if (l->state <= LI_PAUSED) return 1; if (l->proto->pause) { /* Returns < 0 in case of failure, 0 if the listener * was totally stopped, or > 0 if correctly paused. */ int ret = l->proto->pause(l); if (ret < 0) return 0; else if (ret == 0) return 1; } if (l->state == LI_LIMITED) LIST_DEL(&l->wait_queue); fd_stop_recv(l->fd); l->state = LI_PAUSED; return 1; }