time(&current_time);
current_time = time(0); #ifdef POLL if (debug) log(L_DEBUG, "httpd_main: poll() = %d", rv); #else if (debug) log(L_DEBUG, "httpd_main: select() = %d", rv); #endif
void httpd_main(void){	struct server *s;	struct connection *cn;	int first;	int error;	int rv;#ifdef POLL	int n;	short r;#else	fd_set rfds, wfds;	int m;#endif	first = 1;	error = 0;	while (gotsigterm == 0) {		if (gotsighup) {			gotsighup = 0;			init_logs();			if (first) {				first = 0;				log(L_LOG, "%s starting", server_version);			}			else				log(L_LOG, "logs reopened");		}		if (gotsigusr1) {			gotsigusr1 = 0;			nuke_connections();			log(L_LOG, "connections closed");		}		if (gotsigusr2) {			gotsigusr2 = 0;			nuke_servers();			log(L_LOG, "servers closed");		}		if (gotsigwinch) {			gotsigwinch = 0;			dump();		}#ifdef POLL		n = 0;#else		m = -1;		FD_ZERO(&rfds);		FD_ZERO(&wfds);#endif		s = servers;		while (s) {			if (s->fd != -1) {#ifdef POLL				pollfds[n].events = POLLIN;				pollfds[n].fd = s->fd;				s->pollno = n++;#else				FD_SET(s->fd, &rfds);				if (s->fd > m)					m = s->fd;#endif			}			s = s->next;		}		cn = connections;		while (cn) {			if (cn->state == HC_ACTIVE) {				switch (cn->action) {				case HC_WAITING:				case HC_READING:#ifdef POLL					pollfds[n].events = POLLIN;#else					FD_SET(cn->fd, &rfds);#endif					break;				default:#ifdef POLL					pollfds[n].events = POLLOUT;#else					FD_SET(cn->fd, &wfds);#endif					break;				}#ifdef POLL				pollfds[n].fd = cn->fd;				cn->pollno = n++;#else				if (cn->fd > m)					m = cn->fd;#endif			}#ifdef POLL			else				cn->pollno = -1;#endif			cn = cn->next;		}#ifdef POLL		if (n == 0) {			log(L_ERROR, "no more sockets to poll from");			break;		}#else		if (m == -1) {			log(L_ERROR, "no more sockets to select from");			break;		}#endif#ifdef POLL		rv = poll(pollfds, n, INFTIM);#else		rv = select(m + 1, &rfds, &wfds, 0, 0);#endif		time(&current_time);		if (rv == -1) {			if (errno != EINTR) {#ifdef POLL				lerror("poll");#else				lerror("select");#endif				if (error++) {					log(L_PANIC, "whoops");					break;				}			}		} else {			error = 0;			if (rv) {				s = servers;				while (s) {					if (s->fd != -1) {#ifdef POLL						if (pollfds[s->pollno].revents & POLLIN)#else						if (FD_ISSET(s->fd, &rfds))#endif							accept_connection(s);					}					s = s->next;				}				cn = connections;				while (cn) {					if (cn->state == HC_ACTIVE) {#ifdef POLL						if (cn->pollno != -1) {							r = pollfds[cn->pollno].revents;							if (r & POLLIN)								read_connection(cn);							else if (r & POLLOUT)								write_connection(cn);							else if (r) {								log(L_ERROR, "dropping %s: unexpected event %hd", cn->ip, r);								cn->action = HC_CLOSING;						}#else						if (FD_ISSET(cn->fd, &rfds))							read_connection(cn);						else if (FD_ISSET(cn->fd, &wfds))							write_connection(cn);#endif					}					cn = cn->next;				}			}			cleanup_connections();		}	}	dump();	log(L_LOG, "shutting down", my_pid);}