- fd_set fset;
- struct timeval tv;
- int r;
- time_t last_ping_check;
-cp
- last_ping_check = time(NULL);
-
- for(;;)
- {
- tv.tv_sec = timeout;
- tv.tv_usec = 0;
-
- prune_conn_list();
- build_fdset(&fset);
-
- if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
- {
- if(errno != EINTR) /* because of alarm */
- {
- syslog(LOG_ERR, _("Error while waiting for input: %m"));
- return;
- }
- }
-
- if(sighup)
- {
- sighup = 0;
-/* FIXME: reprogram this.
- if(debug_lvl > 1)
- syslog(LOG_INFO, _("Rereading configuration file"));
- close_network_connections();
- clear_config();
- if(read_config_file(&config, configfilename))
- {
- syslog(LOG_ERR, _("Unable to reread configuration file, exiting"));
- exit(0);
- }
- sleep(5);
- setup_network_connections();
-*/
- continue;
- }
-
- if(last_ping_check + timeout < time(NULL))
- /* Let's check if everybody is still alive */
- {
- check_dead_connections();
- last_ping_check = time(NULL);
+ struct timeval tv;
+ int r;
+ time_t last_ping_check;
+ struct event timeout;
+ struct event sighup_event;
+ struct event sigint_event;
+ struct event sigterm_event;
+ struct event sigquit_event;
+ struct event sigusr1_event;
+ struct event sigusr2_event;
+ struct event sigwinch_event;
+ struct event sigalrm_event;
+
+ cp();
+
+ signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
+ signal_add(&sighup_event, NULL);
+ signal_set(&sigint_event, SIGINT, sigint_handler, NULL);
+ signal_add(&sigint_event, NULL);
+ signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
+ signal_add(&sigterm_event, NULL);
+ signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
+ signal_add(&sigquit_event, NULL);
+ signal_set(&sigusr1_event, SIGUSR1, sigusr1_handler, NULL);
+ signal_add(&sigusr1_event, NULL);
+ signal_set(&sigusr2_event, SIGUSR2, sigusr2_handler, NULL);
+ signal_add(&sigusr2_event, NULL);
+ signal_set(&sigwinch_event, SIGWINCH, sigwinch_handler, NULL);
+ signal_add(&sigwinch_event, NULL);
+ signal_set(&sigalrm_event, SIGALRM, sigalrm_handler, NULL);
+ signal_add(&sigalrm_event, NULL);
+
+ last_ping_check = now;
+
+ srand(now);
+
+ running = true;
+
+ while(running) {
+ now = time(NULL);
+
+ // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+
+ /* XXX: libevent transition: old timeout code in this loop */
+ timeout_set(&timeout, dummy, NULL);
+ timeout_add(&timeout, &tv);
+
+ r = build_fdset();
+ if(r < 0) {
+ logger(LOG_ERR, _("Error building fdset: %s"), strerror(errno));
+ cp_trace();
+ dump_connections();
+ return 1;
+ }
+
+ r = event_loop(EVLOOP_ONCE);
+ now = time(NULL);
+ if(r < 0) {
+ logger(LOG_ERR, _("Error while waiting for input: %s"),
+ strerror(errno));
+ cp_trace();
+ dump_connections();
+ return 1;
+ }
+
+ /* XXX: more libevent transition */
+ timeout_del(&timeout);
+
+ /* Let's check if everybody is still alive */
+
+ if(last_ping_check + pingtimeout < now) {
+ check_dead_connections();
+ last_ping_check = now;
+ }