static void signal_cb(struct ev_loop *loop, struct ev_signal *w, int revents) { if (revents & EV_SIGNAL) { switch (w->signum) { case SIGHUP: reload_config(config, loop); break; case SIGUSR1: print_connections(); break; case SIGINT: case SIGTERM: ev_unloop(loop, EVUNLOOP_ALL); } } }
void run_server() { int maxfd; fd_set rfds, wfds; init_connections(); running = 1; while (running) { FD_ZERO(&rfds); FD_ZERO(&wfds); maxfd = fd_set_listeners(&config->listeners, &rfds, 0); maxfd = fd_set_connections(&rfds, &wfds, maxfd); if (select(maxfd + 1, &rfds, &wfds, NULL, NULL) < 0) { /* select() might have failed because we received a signal, so we need to check */ if (errno != EINTR) { perror("select"); return; } /* We where interrupted by a signal */ if (sighup_received) { sighup_received = 0; reload_config(config); } if (sigusr1_received) { sigusr1_received = 0; print_connections(); } continue; /* our file descriptor sets are undefined, so select again */ } handle_listeners(&config->listeners, &rfds, accept_connection); handle_connections(&rfds, &wfds); } free_connections(); }