- fix for access of freed meter after ctrl-c
[oweals/gnunet.git] / src / testing / testing_peergroup.c
index e44238444242967266663bae84eb19101f6397e0..0421b000a6a24852d586d408a87ea76a354b2d2b 100644 (file)
@@ -46,7 +46,7 @@ struct PeerGroupStartupContext
   unsigned int total;
   unsigned int peers_left;
   unsigned long long max_concurrent_connections;
-  
+
   /**
    * Maximum attemps to connect two daemons.
    */
@@ -56,7 +56,7 @@ struct PeerGroupStartupContext
    * 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;
@@ -183,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);
@@ -210,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++;
@@ -463,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,
@@ -488,6 +498,11 @@ internal_peers_started_callback (void *cls,
 
   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
@@ -508,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,
-                                          pg_start_ctx->connect_timeout,
+                                           pg_start_ctx->connect_timeout,
                                            pg_start_ctx->connect_attempts, NULL,
                                            NULL);
 
@@ -734,7 +749,6 @@ 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;
@@ -756,9 +770,9 @@ GNUNET_TESTING_peergroup_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
 
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_time (cfg, "testing", "CONNECT_TIMEOUT",
-                                          &pg_start_ctx->connect_timeout))
+                                           &pg_start_ctx->connect_timeout))
   {
-      pg_start_ctx->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
+    pg_start_ctx->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
   }
 
   if (GNUNET_OK !=
@@ -795,17 +809,15 @@ GNUNET_TESTING_peergroup_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   }
 
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_time (cfg, "testing",
-                                             "PEERGROUP_TIMEOUT",
-                                             &rtimeout))
+      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");
     GNUNET_free (pg_start_ctx);
     return NULL;
   }
-  pg_start_ctx->timeout =
-    GNUNET_TIME_relative_to_absolute (rtimeout);
+  pg_start_ctx->timeout = GNUNET_TIME_relative_to_absolute (rtimeout);
 
 
   /* Read topology related options from the configuration file */