- fix for access of freed meter after ctrl-c
[oweals/gnunet.git] / src / testing / testing_peergroup.c
index 470fdf8faf526d78b786e6ecef8f1d09518b5594..0421b000a6a24852d586d408a87ea76a354b2d2b 100644 (file)
@@ -33,7 +33,7 @@
 #include "gnunet_disk_lib.h"
 
 /** Globals **/
-#define DEFAULT_CONNECT_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
+#define DEFAULT_CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
 
 #define DEFAULT_CONNECT_ATTEMPTS 2
 
@@ -46,7 +46,17 @@ struct PeerGroupStartupContext
   unsigned int total;
   unsigned int peers_left;
   unsigned long long max_concurrent_connections;
+
+  /**
+   * Maximum attemps to connect two daemons.
+   */
   unsigned long long connect_attempts;
+
+  /**
+   * How long to spend trying to establish all the connections?
+   */
+  struct GNUNET_TIME_Relative connect_timeout;
+
   unsigned long long max_concurrent_ssh;
   struct GNUNET_TIME_Absolute timeout;
   GNUNET_TESTING_NotifyConnection connect_cb;
@@ -173,7 +183,7 @@ create_meter (unsigned int total, char *start_string, int print)
   ret = GNUNET_malloc (sizeof (struct ProgressMeter));
   ret->print = print;
   ret->total = total;
-  ret->modnum = total / 4;
+  ret->modnum = (total / 4 == 0) ? 1 : (total / 4);
   ret->dotnum = (total / 50) + 1;
   if (start_string != NULL)
     ret->startup_string = GNUNET_strdup (start_string);
@@ -200,17 +210,17 @@ update_meter (struct ProgressMeter *meter)
     {
       if (meter->completed == 0)
       {
-        fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
+        FPRINTF (stdout, "%sProgress: [0%%", meter->startup_string);
       }
       else
-        fprintf (stdout, "%d%%",
+        FPRINTF (stdout, "%d%%",
                  (int) (((float) meter->completed / meter->total) * 100));
     }
     else if (meter->completed % meter->dotnum == 0)
-      fprintf (stdout, ".");
+      FPRINTF (stdout, "%s",  ".");
 
     if (meter->completed + 1 == meter->total)
-      fprintf (stdout, "%d%%]\n", 100);
+      FPRINTF (stdout, "%d%%]\n", 100);
     fflush (stdout);
   }
   meter->completed++;
@@ -416,7 +426,7 @@ internal_topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
 #if VERBOSE
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Created %d total connections, which is our target number!  Starting next phase of testing.\n",
-                total_connections);
+                pg_start_ctx->total_connections);
 #endif
 
 #if TIMING
@@ -453,6 +463,16 @@ internal_topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
   }
 }
 
+
+/**
+ * Callback called for each started daemon.
+ *
+ * @param cls Clause (PG Context).
+ * @param id PeerIdentidy of started daemon.
+ * @param cfg Configuration used by the daemon.
+ * @param d Handle for the daemon.
+ * @param emsg Error message, NULL on success.
+ */
 static void
 internal_peers_started_callback (void *cls,
                                  const struct GNUNET_PeerIdentity *id,
@@ -472,21 +492,29 @@ internal_peers_started_callback (void *cls,
 
 #if VERBOSE > 1
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
-              (num_peers - peers_left) + 1, num_peers);
+              (pg_start_ctx->total - pg_start_ctx->peers_left) + 1,
+              pg_start_ctx->total);
 #endif
 
   pg_start_ctx->peers_left--;
 
+  if (NULL == pg_start_ctx->peer_start_meter)
+  {
+    /* Cancelled Ctrl-C or error */
+    return;
+  }
   if (GNUNET_YES == update_meter (pg_start_ctx->peer_start_meter))
   {
 #if VERBOSE
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "All %d daemons started, now connecting peers!\n", num_peers);
+                "All %d daemons started, now connecting peers!\n",
+                pg_start_ctx->total);
 #endif
     GNUNET_assert (pg_start_ctx->die_task != GNUNET_SCHEDULER_NO_TASK);
     GNUNET_SCHEDULER_cancel (pg_start_ctx->die_task);
 
     pg_start_ctx->expected_connections = UINT_MAX;
+    // FIXME: why whould peers_left be != 0?? Or pg NULL?
     if ((pg_start_ctx->pg != NULL) && (pg_start_ctx->peers_left == 0))
     {
       pg_start_ctx->connect_start_time = GNUNET_TIME_absolute_get ();
@@ -495,7 +523,7 @@ internal_peers_started_callback (void *cls,
                                            pg_start_ctx->connect_topology,
                                            pg_start_ctx->connect_topology_option,
                                            pg_start_ctx->connect_topology_option_modifier,
-                                           DEFAULT_CONNECT_TIMEOUT,
+                                           pg_start_ctx->connect_timeout,
                                            pg_start_ctx->connect_attempts, NULL,
                                            NULL);
 
@@ -549,17 +577,18 @@ internal_hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
 
 #if VERBOSE > 1
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Hostkey (%d/%d) created for peer `%s'\n", num_peers - peers_left,
-              num_peers, GNUNET_i2s (id));
+              "Hostkey (%d/%d) created for peer `%s'\n",
+              pg_start_ctx->total - pg_start_ctx->peers_left + 1,
+              pg_start_ctx->total, GNUNET_i2s (id));
 #endif
 
   pg_start_ctx->peers_left--;
   if (GNUNET_YES == update_meter (pg_start_ctx->hostkey_meter))
   {
     GNUNET_SCHEDULER_cancel (pg_start_ctx->die_task);
+    GNUNET_free_non_null (pg_start_ctx->fail_reason);
     /* Set up task in case topology creation doesn't finish
      * within a reasonable amount of time */
-    GNUNET_free_non_null (pg_start_ctx->fail_reason);
     pg_start_ctx->fail_reason = GNUNET_strdup ("from create_topology");
     pg_start_ctx->die_task =
         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
@@ -591,8 +620,8 @@ internal_hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
 
     GNUNET_SCHEDULER_cancel (pg_start_ctx->die_task);
     GNUNET_free_non_null (pg_start_ctx->fail_reason);
-      pg_start_ctx->fail_reason =
-          GNUNET_strdup ("from continue startup (timeout)");
+    pg_start_ctx->fail_reason =
+        GNUNET_strdup ("from continue startup (timeout)");
     pg_start_ctx->die_task =
         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
                                       (pg_start_ctx->timeout), &end_badly,
@@ -720,9 +749,9 @@ GNUNET_TESTING_peergroup_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
                                 const struct GNUNET_TESTING_Host *hostnames)
 {
   struct PeerGroupStartupContext *pg_start_ctx;
-  unsigned long long temp_config_number;
   char *temp_str;
   int temp;
+  struct GNUNET_TIME_Relative rtimeout;
 
   GNUNET_assert (total > 0);
   GNUNET_assert (cfg != NULL);
@@ -739,6 +768,13 @@ GNUNET_TESTING_peergroup_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
     return NULL;
   }
 
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_time (cfg, "testing", "CONNECT_TIMEOUT",
+                                           &pg_start_ctx->connect_timeout))
+  {
+    pg_start_ctx->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
+  }
+
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
                                              "max_outstanding_connections",
@@ -772,21 +808,16 @@ GNUNET_TESTING_peergroup_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
     return NULL;
   }
 
-  if (GNUNET_OK ==
-      GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
-                                             "peergroup_timeout",
-                                             &temp_config_number))
-    pg_start_ctx->timeout =
-        GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
-                                          (GNUNET_TIME_UNIT_SECONDS,
-                                           temp_config_number));
-  else
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_time (cfg, "testing", "PEERGROUP_TIMEOUT",
+                                           &rtimeout))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
-                "testing", "peergroup_timeout");
+                "testing", "PEERGROUP_TIMEOUT");
     GNUNET_free (pg_start_ctx);
     return NULL;
   }
+  pg_start_ctx->timeout = GNUNET_TIME_relative_to_absolute (rtimeout);
 
 
   /* Read topology related options from the configuration file */
@@ -928,7 +959,8 @@ GNUNET_TESTING_peergroup_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   /* Make compilers happy */
   reset_meter (pg_start_ctx->peer_start_meter);
   pg_start_ctx->fail_reason =
-        GNUNET_strdup ("didn't generate all hostkeys within allowed startup time!");
+      GNUNET_strdup
+      ("didn't generate all hostkeys within allowed startup time!");
   pg_start_ctx->die_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
                                     (pg_start_ctx->timeout), &end_badly,