+ last_ping_check = now;
+ last_config_check = now;
+ last_graph_dump = 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;
+
+ maxfd = build_fdset(&readset, &writeset);
+
+ r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
+
+ if(r < 0) {
+ if(errno != EINTR && errno != EAGAIN) {
+ logger(LOG_ERR, _("Error while waiting for input: %s"),
+ strerror(errno));
+ cp_trace();
+ dump_connections();
+ return 1;
+ }
+
+ continue;
+ }
+
+ check_network_activity(&readset, &writeset);
+
+ if(do_purge) {
+ purge();
+ do_purge = false;
+ }
+
+ /* Let's check if everybody is still alive */
+
+ if(last_ping_check + pingtimeout < now) {
+ check_dead_connections();
+ last_ping_check = now;
+
+ if(routing_mode == RMODE_SWITCH)
+ age_subnets();
+
+ age_past_requests();
+
+ /* Should we regenerate our key? */
+
+ if(keyexpires < now) {
+ avl_node_t *node;
+ node_t *n;
+
+ ifdebug(STATUS) logger(LOG_INFO, _("Expiring symmetric keys"));
+
+ for(node = node_tree->head; node; node = node->next) {
+ n = node->data;
+ if(n->inkey) {
+ free(n->inkey);
+ n->inkey = NULL;
+ }
+ }
+
+ send_key_changed(broadcast, myself);
+ keyexpires = now + keylifetime;
+ }
+ }
+
+ if(sigalrm) {
+ logger(LOG_INFO, _("Flushing event queue"));
+ expire_events();
+ sigalrm = false;
+ }
+
+ while((event = get_expired_event())) {
+ event->handler(event->data);
+ free_event(event);
+ }
+
+ if(sighup) {
+ connection_t *c;
+ avl_node_t *node;
+ char *fname;
+ struct stat s;
+
+ sighup = false;
+
+ /* Reread our own configuration file */
+
+ exit_configuration(&config_tree);
+ init_configuration(&config_tree);
+
+ if(!read_server_config()) {
+ logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
+ return 1;
+ }
+
+ /* Close connections to hosts that have a changed or deleted host config file */
+
+ for(node = connection_tree->head; node; node = node->next) {
+ c = node->data;
+
+ xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
+ if(stat(fname, &s) || s.st_mtime > last_config_check)
+ terminate_connection(c, c->status.active);
+ free(fname);
+ }
+
+ last_config_check = now;
+
+ /* Try to make outgoing connections */
+
+ try_outgoing_connections();
+ }
+
+ /* Dump graph if wanted every 60 seconds*/
+
+ if(last_graph_dump + 60 < now) {
+ dump_graph();
+ last_graph_dump = now;
+ }
+ }