extended HELPER api to notify when child crashes
authorSree Harsha Totakura <totakura@in.tum.de>
Mon, 16 Jul 2012 18:58:55 +0000 (18:58 +0000)
committerSree Harsha Totakura <totakura@in.tum.de>
Mon, 16 Jul 2012 18:58:55 +0000 (18:58 +0000)
src/dns/gnunet-service-dns.c
src/exit/gnunet-daemon-exit.c
src/fs/fs_dirmetascan.c
src/include/gnunet_helper_lib.h
src/testbed/test_gnunet_testbed_helper.c
src/transport/plugin_transport_wlan.c
src/util/helper.c
src/vpn/gnunet-service-vpn.c

index 644f2e1c8b5467e1a450846ea2da25b26e47a717..570f742d8cc24fdc7f96364dd7475761bd15d0bb 100644 (file)
@@ -1656,7 +1656,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   hijacker = GNUNET_HELPER_start ("gnunet-helper-dns",
                                  helper_argv,
                                  &process_helper_messages,
-                                 NULL);
+                                 NULL, NULL);
   GNUNET_SERVER_add_handlers (server, handlers);
   GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
 }
index e9e961fd5b1373a07c84b3943ad59e0c1b0b31c3..2614ba90866e05578d4c2707b8dbd20a79e84c3f 100644 (file)
@@ -3213,7 +3213,8 @@ run (void *cls, char *const *args GNUNET_UNUSED,
   }
   helper_handle = GNUNET_HELPER_start ("gnunet-helper-exit", 
                                       exit_argv,
-                                      &message_token, NULL);
+                                      &message_token,
+                                      NULL, NULL);
 }
 
 
index 6dac690ff525370e36a3222c2a5f19dc08d5294c..3e2512206182936d463201558a2038e30e3fb9a2 100644 (file)
@@ -457,7 +457,7 @@ GNUNET_FS_directory_scan_start (const char *filename,
   ds->helper = GNUNET_HELPER_start ("gnunet-helper-fs-publish",
                                    ds->args,
                                    &process_helper_msgs,
-                                   ds);
+                                   NULL, ds);
   if (NULL == ds->helper)
   {
     GNUNET_free (filename_expanded);
index 9c1bc21e2841925927bb038f01b9e761b3660837..2756cdf4edf9946624e6af1cb0eadc292058f62d 100644 (file)
@@ -24,6 +24,7 @@
  * @author Philipp Toelke
  * @author Christian Grothoff
  */
+
 #ifndef GNUNET_HELPER_LIB_H
 #define GNUNET_HELPER_LIB_H
 
@@ -37,24 +38,43 @@ struct GNUNET_HELPER_Handle;
 
 
 /**
- * @brief Starts a helper and begins reading from it
+ * Callback that will be called when the helper process dies. This is not called
+ * when the helper process is stoped using GNUNET_HELPER_stop()
+ *
+ * @param cls the closure from GNUNET_HELPER_start()
+ * @param h the handle representing the helper process. This handle is invalid
+ *          in this callback. It is only presented for reference. No operations
+ *          can be performed using it.
+ */
+typedef void (*GNUNET_HELPER_ExceptionCallback) (void *cls,
+                                                const struct GNUNET_HELPER_Handle *h);
+
+
+/**
+ * Starts a helper and begins reading from it. The helper process is
+ * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
+ * or when the exp_cb callback is not NULL.
  *
  * @param binary_name name of the binary to run
  * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
  *                    argument must not be modified by the client for
  *                     the lifetime of the helper handle)
  * @param cb function to call if we get messages from the helper
- * @param cb_cls Closure for the callback
+ * @param exp_cb the exception callback to call. Set this to NULL if the helper
+ *          process has to be restarted automatically when it dies/crashes
+ * @param cb_cls closure for the above callbacks
  * @return the new Handle, NULL on error
  */
 struct GNUNET_HELPER_Handle *
 GNUNET_HELPER_start (const char *binary_name,
                     char *const binary_argv[],
-                    GNUNET_SERVER_MessageTokenizerCallback cb, void *cb_cls);
+                    GNUNET_SERVER_MessageTokenizerCallback cb,
+                    GNUNET_HELPER_ExceptionCallback exp_cb,
+                    void *cb_cls);
 
 
 /**
- * @brief Kills the helper, closes the pipe and frees the handle
+ * Kills the helper, closes the pipe and frees the handle
  *
  * @param h handle to helper to stop
  */
index a2f8936abb4af9eb08230d980fe07ef377d38c95..654e5dd3eebc309dcdf1c25430c8b6e990799c2f 100644 (file)
@@ -151,7 +151,7 @@ run (void *cls, char *const *args, const char *cfgfile,
 
   helper = GNUNET_HELPER_start ("gnunet-testbed-helper", 
                                binary_argv,
-                                NULL, NULL);
+                                NULL, NULL, NULL);
   GNUNET_assert (NULL != helper);
   cfg = GNUNET_CONFIGURATION_dup (cfg2);  
   config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
index c2565a4a8fb9f198de1c3343266d03008b54525a..2d786e2a915ee2bf060f0c51ddaeeaa347a4618b 100644 (file)
@@ -1713,6 +1713,7 @@ libgnunet_plugin_transport_wlan_init (void *cls)
     plugin->suid_helper = GNUNET_HELPER_start ("gnunet-helper-transport-wlan",
                                               plugin->helper_argv,
                                               &handle_helper_message,
+                                              NULL,
                                               plugin);
     break;
   case 1: /* testmode, peer 1 */
@@ -1722,6 +1723,7 @@ libgnunet_plugin_transport_wlan_init (void *cls)
     plugin->suid_helper = GNUNET_HELPER_start ("gnunet-helper-transport-wlan-dummy",
                                               plugin->helper_argv,
                                               &handle_helper_message,
+                                              NULL,
                                               plugin);
     break;
   case 2: /* testmode, peer 2 */
@@ -1731,6 +1733,7 @@ libgnunet_plugin_transport_wlan_init (void *cls)
     plugin->suid_helper = GNUNET_HELPER_start ("gnunet-helper-transport-wlan-dummy",
                                               plugin->helper_argv,
                                               &handle_helper_message,
+                                              NULL,
                                               plugin);
     break;
   default:
index 16b3465c04b9c019dbfdbb5ba98619b70ce31957..3fe3705b1f917941a54dabd105caaeba8b705758 100644 (file)
@@ -108,6 +108,16 @@ struct GNUNET_HELPER_Handle
    */
   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
 
+  /**
+   * The exception callback
+   */
+  GNUNET_HELPER_ExceptionCallback exp_cb;
+
+  /**
+   * The closure for callbacks
+   */
+  void *cb_cls;
+
   /**
    * First message queued for transmission to helper.
    */
@@ -244,11 +254,16 @@ helper_read (void *cls,
                 _("Error reading from `%s': %s\n"),
                h->binary_name,
                STRERROR (errno));
+    if (NULL != h->exp_cb)
+    {
+      h->exp_cb (h->cb_cls, h);
+      GNUNET_HELPER_stop (h);
+      return;
+    }
     stop_helper (h);
     /* Restart the helper */
     h->restart_task =
-      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
-                                   &restart_task, h);
+       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &restart_task, h);
     return;
   }
   if (0 == t)
@@ -258,6 +273,12 @@ helper_read (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
                _("Got 0 bytes from helper `%s' (EOF)\n"),
                h->binary_name);
+    if (NULL != h->exp_cb)
+    {
+      h->exp_cb (h->cb_cls, h);
+      GNUNET_HELPER_stop (h);
+      return;
+    }
     stop_helper (h);
     /* Restart the helper */
     h->restart_task =
@@ -277,6 +298,12 @@ helper_read (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 
                _("Failed to parse inbound message from helper `%s'\n"),
                h->binary_name);
+    if (NULL != h->exp_cb)
+    {
+      h->exp_cb (h->cb_cls, h);
+      GNUNET_HELPER_stop (h);
+      return;
+    }     
     stop_helper (h);
     /* Restart the helper */
     h->restart_task =
@@ -354,27 +381,35 @@ restart_task (void *cls,
 
 
 /**
- * @brief Starts a helper and begins reading from it
+ * Starts a helper and begins reading from it. The helper process is
+ * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
+ * or when the exp_cb callback is not NULL.
  *
  * @param binary_name name of the binary to run
  * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
  *                    argument must not be modified by the client for
- *                     the lifetime of the helper h)
+ *                     the lifetime of the helper handle)
  * @param cb function to call if we get messages from the helper
- * @param cb_cls Closure for the callback
- * @return the new H, NULL on error
+ * @param exp_cb the exception callback to call. Set this to NULL if the helper
+ *          process has to be restarted automatically when it dies/crashes
+ * @param cb_cls closure for the above callback
+ * @return the new Handle, NULL on error
  */
-struct GNUNET_HELPER_Handle*
+struct GNUNET_HELPER_Handle *
 GNUNET_HELPER_start (const char *binary_name,
                     char *const binary_argv[],
-                    GNUNET_SERVER_MessageTokenizerCallback cb, void *cb_cls)
+                    GNUNET_SERVER_MessageTokenizerCallback cb,
+                    GNUNET_HELPER_ExceptionCallback exp_cb,
+                    void *cb_cls)
 {
   struct GNUNET_HELPER_Handle*h;
 
   h =  GNUNET_malloc (sizeof (struct GNUNET_HELPER_Handle));
   h->binary_name = binary_name;
   h->binary_argv = binary_argv;
-  h->mst = GNUNET_SERVER_mst_create (cb, cb_cls);
+  h->cb_cls = cb_cls;
+  h->mst = GNUNET_SERVER_mst_create (cb, h->cb_cls);
+  h->exp_cb = exp_cb;
   start_helper (h);
   return h;
 }
@@ -390,6 +425,7 @@ GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h)
 {
   struct GNUNET_HELPER_SendHandle *sh;
 
+  h->exp_cb = NULL;
   /* signal pending writes that we were stopped */
   while (NULL != (sh = h->sh_head))
   {
index 219949bd31791115d9d6a46259d8836de8500ee0..0b9a4dcb2348550f9949177f25f87e9aef9e8cbf 100644 (file)
@@ -3183,7 +3183,7 @@ run (void *cls,
                         mesh_handlers,
                         types);
   helper_handle = GNUNET_HELPER_start ("gnunet-helper-vpn", vpn_argv,
-                                      &message_token, NULL);
+                                      &message_token, NULL, NULL);
   nc = GNUNET_SERVER_notification_context_create (server, 1);
   GNUNET_SERVER_add_handlers (server, service_handlers);
   GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);