-void main_loop(void)
-{
- fd_set fset;
- struct timeval tv;
- int r;
- time_t last_ping_check;
- int t;
-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)
- {
- syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds"));
- sighup = 0;
- close_network_connections();
- clear_config(&config);
-
- if(read_server_config())
- {
- syslog(LOG_ERR, _("Unable to reread configuration file, exiting"));
- exit(0);
- }
-
- sleep(5);
-
- if(setup_network_connections())
- return;
-
- continue;
- }
-
- t = time(NULL);
-
- /* Let's check if everybody is still alive */
-
- if(last_ping_check + timeout < t)
- {
- check_dead_connections();
- last_ping_check = time(NULL);
-
- /* Should we regenerate our key? */
-
- if(keyexpires < t)
- {
- if(debug_lvl >= DEBUG_STATUS)
- syslog(LOG_INFO, _("Regenerating symmetric key"));
-
- RAND_bytes(myself->cipher_pktkey, myself->cipher_pktkeylength);
- send_key_changed(myself, NULL);
- keyexpires = time(NULL) + keylifetime;
- }
+int main_loop(void) {
+ struct timeval tv;
+ int r;
+ struct event timeout_event;
+ 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();
+
+ timeout_set(&timeout_event, timeout_handler, &timeout_event);
+ event_add(&timeout_event, &(struct timeval){pingtimeout, 0});
+ 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);
+
+ if(event_loop(0) < 0) {
+ logger(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
+ return 1;