-dce
[oweals/gnunet.git] / src / util / helper.c
index 6ebc3a444996dcfaf16e7d1888fc06ca947f6f68..03e71585c07330a428902459445ade51ec609b4c 100644 (file)
 /**
  * Entry in the queue of messages we need to transmit to the helper.
  */
-struct HelperMessageQueueEntry
+struct GNUNET_HELPER_SendHandle
 {
 
   /**
    * This is an entry in a DLL.
    */
-  struct HelperMessageQueueEntry *next;
+  struct GNUNET_HELPER_SendHandle *next;
 
   /**
    * This is an entry in a DLL.
    */
-  struct HelperMessageQueueEntry *prev;
+  struct GNUNET_HELPER_SendHandle *prev;
 
   /**
    * Message to transmit (allocated at the end of this struct)
    */
   const struct GNUNET_MessageHeader *msg;
-  
+  /**
+   * The handle to a helper process.
+   */
+  struct GNUNET_HELPER_Handle *h;
   /**
    * Function to call upon completion.
    */
@@ -103,15 +108,25 @@ 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.
    */
-  struct HelperMessageQueueEntry *mq_head;
+  struct GNUNET_HELPER_SendHandle *sh_head;
 
   /**
    * Last message queued for transmission to helper.
    */
-  struct HelperMessageQueueEntry *mq_tail;
+  struct GNUNET_HELPER_SendHandle *sh_tail;
 
   /**
    * Binary to run.
@@ -121,7 +136,7 @@ struct GNUNET_HELPER_Handle
   /**
    * NULL-terminated list of command-line arguments.
    */
-  char *const *binary_argv;
+  char **binary_argv;
                    
   /**
    * Task to read from the helper.
@@ -137,6 +152,12 @@ struct GNUNET_HELPER_Handle
    * Restart task.
    */
   GNUNET_SCHEDULER_TaskIdentifier restart_task;
+
+  /**
+   * Does the helper support the use of a control pipe for signalling?
+   */
+  int with_control_pipe;
+
 };
 
 
@@ -148,13 +169,13 @@ struct GNUNET_HELPER_Handle
 static void
 stop_helper (struct GNUNET_HELPER_Handle *h)
 {
-  struct HelperMessageQueueEntry *qe;
+  struct GNUNET_HELPER_SendHandle *sh;
 
   if (NULL != h->helper_proc)
   {
     GNUNET_break (0 == GNUNET_OS_process_kill (h->helper_proc, SIGTERM));
     GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (h->helper_proc));
-    GNUNET_OS_process_close (h->helper_proc);
+    GNUNET_OS_process_destroy (h->helper_proc);
     h->helper_proc = NULL;
   }
   if (GNUNET_SCHEDULER_NO_TASK != h->restart_task)
@@ -184,16 +205,17 @@ stop_helper (struct GNUNET_HELPER_Handle *h)
     h->helper_out = NULL;
     h->fh_from_helper = NULL;
   }
-  while (NULL != (qe = h->mq_head))
+  while (NULL != (sh = h->sh_head))
   {
-    GNUNET_CONTAINER_DLL_remove (h->mq_head,
-                                h->mq_tail,
-                                qe);
-    qe->cont (qe->cont_cls, GNUNET_NO);
-    GNUNET_free (qe);
+    GNUNET_CONTAINER_DLL_remove (h->sh_head,
+                                h->sh_tail,
+                                sh);
+    if (NULL != sh->cont)
+      sh->cont (sh->cont_cls, GNUNET_NO);
+    GNUNET_free (sh);
   }
   /* purge MST buffer */
-  GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO);
+  (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO);
 }
 
 
@@ -216,14 +238,14 @@ restart_task (void *cls,
  */
 static void
 helper_read (void *cls,
-            const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
+            const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_HELPER_Handle *h = cls;
-  char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
+  char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
   ssize_t t;
 
   h->read_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
   {
     /* try again */
     h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
@@ -238,20 +260,31 @@ 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);
+      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)
   {
     /* this happens if the helper is shut down via a 
        signal, so it is not a "hard" error */
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO
-               _("Got 0 bytes from helper `%s' (EOF)\n"),
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG
+               "Got 0 bytes from helper `%s' (EOF)\n",
                h->binary_name);
+    if (NULL != h->exp_cb)
+    {
+      h->exp_cb (h->cb_cls);
+      GNUNET_HELPER_stop (h);
+      return;
+    }
     stop_helper (h);
     /* Restart the helper */
     h->restart_task =
@@ -259,26 +292,31 @@ helper_read (void *cls,
                                    &restart_task, h);
     return;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO
-             _("Got %u bytes from helper `%s'\n"),
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG
+             "Got %u bytes from helper `%s'\n",
              (unsigned int) t,
              h->binary_name);
+  h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
+                                                h->fh_from_helper, &helper_read, h);
   if (GNUNET_SYSERR ==
       GNUNET_SERVER_mst_receive (h->mst, NULL, buf, t, GNUNET_NO, GNUNET_NO))
   {
     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);
+      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);
     return;
-
   }
-  h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
-                                                h->fh_from_helper, &helper_read, h);
 }
 
 
@@ -301,14 +339,18 @@ start_helper (struct GNUNET_HELPER_Handle *h)
                                    &restart_task, h);    
     return;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Starting HELPER process `%s'\n",
+             h->binary_name);
   h->fh_from_helper =
       GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
   h->fh_to_helper =
       GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
   h->helper_proc =
-      GNUNET_OS_start_process_vap (h->helper_in, h->helper_out,
-                                  h->binary_name,
-                                  h->binary_argv);
+    GNUNET_OS_start_process_vap (h->with_control_pipe, GNUNET_OS_INHERIT_STD_ERR, 
+                                h->helper_in, h->helper_out,
+                                h->binary_name,
+                                h->binary_argv);
   if (NULL == h->helper_proc)
   {
     /* failed to start process? try again later... */
@@ -345,27 +387,45 @@ 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 with_control_pipe does the helper support the use of a control pipe for signalling?
  * @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*
-GNUNET_HELPER_start (const char *binary_name,
+struct GNUNET_HELPER_Handle *
+GNUNET_HELPER_start (int with_control_pipe,
+                    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;
-
+  int c = 0;
   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->with_control_pipe = with_control_pipe;
+  h->binary_name = GNUNET_strdup (binary_name);
+
+  for (c = 0; binary_argv[c] != NULL; c++)
+    c ++;
+  h->binary_argv = GNUNET_malloc (sizeof (char *[c + 1]));
+  for (c = 0; binary_argv[c] != NULL; c++)
+    h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
+  h->binary_argv[c] = NULL;
+
+  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;
 }
@@ -379,19 +439,25 @@ GNUNET_HELPER_start (const char *binary_name,
 void
 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h)
 {
-  struct HelperMessageQueueEntry *qe;
-
+  struct GNUNET_HELPER_SendHandle *sh;
+  int c;
+  h->exp_cb = NULL;
   /* signal pending writes that we were stopped */
-  while (NULL != (qe = h->mq_head))
+  while (NULL != (sh = h->sh_head))
   {
-    GNUNET_CONTAINER_DLL_remove (h->mq_head,
-                                h->mq_tail,
-                                qe);
-    qe->cont (qe->cont_cls, GNUNET_SYSERR);
-    GNUNET_free (qe);
+    GNUNET_CONTAINER_DLL_remove (h->sh_head,
+                                h->sh_tail,
+                                sh);
+    if (NULL != sh->cont)
+      sh->cont (sh->cont_cls, GNUNET_SYSERR);
+    GNUNET_free (sh);
   }
   stop_helper (h);
   GNUNET_SERVER_mst_destroy (h->mst);
+  GNUNET_free ((char *) h->binary_name);
+  for (c = 0; h->binary_argv[c] != NULL; c++)
+    GNUNET_free (h->binary_argv[c]);
+  GNUNET_free (h->binary_argv);
   GNUNET_free (h);
 }
 
@@ -404,25 +470,25 @@ GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h)
  */
 static void
 helper_write (void *cls,
-            const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
+            const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_HELPER_Handle *h = cls;
-  struct HelperMessageQueueEntry *qe;
+  struct GNUNET_HELPER_SendHandle *sh;
   const char *buf;
   ssize_t t;
 
   h->write_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
   {
     /* try again */
     h->write_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
                                                    h->fh_to_helper, &helper_write, h);
     return;
   }  
-  if (NULL == (qe = h->mq_head))
+  if (NULL == (sh = h->sh_head))
     return; /* how did this happen? */
-  buf = (const char*) qe->msg;
-  t = GNUNET_DISK_file_write (h->fh_to_helper, &buf[qe->wpos], ntohs (qe->msg->size) - qe->wpos);
+  buf = (const char*) sh->msg;
+  t = GNUNET_DISK_file_write (h->fh_to_helper, &buf[sh->wpos], ntohs (sh->msg->size) - sh->wpos);
   if (t <= 0)
   {
     /* On write-error, restart the helper */
@@ -430,6 +496,12 @@ helper_write (void *cls,
                 _("Error writing to `%s': %s\n"),
                h->binary_name,
                STRERROR (errno));
+    if (NULL != h->exp_cb)
+    {
+      h->exp_cb (h->cb_cls);
+      GNUNET_HELPER_stop (h);
+      return;
+    }
     stop_helper (h);
     /* Restart the helper */
     h->restart_task =
@@ -437,17 +509,17 @@ helper_write (void *cls,
                                    &restart_task, h);
     return;
   }
-  qe->wpos += t;
-  if (qe->wpos == ntohs (qe->msg->size))
+  sh->wpos += t;
+  if (sh->wpos == ntohs (sh->msg->size))
   {
-    GNUNET_CONTAINER_DLL_remove (h->mq_head,
-                                h->mq_tail,
-                                qe);
-    if (NULL != qe->cont)
-      qe->cont (qe->cont_cls, GNUNET_YES);
-    GNUNET_free (qe);
+    GNUNET_CONTAINER_DLL_remove (h->sh_head,
+                                h->sh_tail,
+                                sh);
+    if (NULL != sh->cont)
+      sh->cont (sh->cont_cls, GNUNET_YES);
+    GNUNET_free (sh);
   }
-  if (NULL != h->mq_head)
+  if (NULL != h->sh_head)
     h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
                                                     h->fh_to_helper, 
                                                     &helper_write, 
@@ -464,40 +536,68 @@ helper_write (void *cls,
  * @param cont continuation to run once the message is out (PREREQ_DONE on succees, CANCEL
  *             if the helper process died, NULL during GNUNET_HELPER_stop).
  * @param cont_cls closure for 'cont'
- * @return GNUNET_YES if the message will be sent
- *         GNUNET_NO if the message was dropped
+ * @return NULL if the message was dropped, 
+ *         otherwise handle to cancel *cont* (actual transmission may
+ *         not be abortable)
  */
-int
+struct GNUNET_HELPER_SendHandle *
 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h, 
                    const struct GNUNET_MessageHeader *msg,
                    int can_drop,
                    GNUNET_HELPER_Continuation cont,
                    void *cont_cls)
 {
-  struct HelperMessageQueueEntry *qe;
+  struct GNUNET_HELPER_SendHandle *sh;
   uint16_t mlen;
 
   if (NULL == h->fh_to_helper)
-    return GNUNET_NO;
+    return NULL;
   if ( (GNUNET_YES == can_drop) &&
-       (h->mq_head != NULL) )
-    return GNUNET_NO;
+       (NULL != h->sh_head) )
+    return NULL;
   mlen = ntohs (msg->size);
-  qe = GNUNET_malloc (sizeof (struct HelperMessageQueueEntry) + mlen);
-  qe->msg = (const struct GNUNET_MessageHeader*) &qe[1];
-  memcpy (&qe[1], msg, mlen);
-  qe->cont = cont;
-  qe->cont_cls = cont_cls;
-  GNUNET_CONTAINER_DLL_insert_tail (h->mq_head,
-                                   h->mq_tail,
-                                   qe);
+  sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
+  sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
+  memcpy (&sh[1], msg, mlen);
+  sh->h = h;
+  sh->cont = cont;
+  sh->cont_cls = cont_cls;
+  GNUNET_CONTAINER_DLL_insert_tail (h->sh_head,
+                                   h->sh_tail,
+                                   sh);
   if (GNUNET_SCHEDULER_NO_TASK == h->write_task)
     h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
                                                     h->fh_to_helper, 
                                                     &helper_write, 
                                                     h);
     
-  return GNUNET_YES;
+  return sh;
+}
+
+/**
+ * Cancel a 'send' operation.  If possible, transmitting the
+ * message is also aborted, but at least 'cont' won't be
+ * called.
+ *
+ * @param sh operation to cancel
+ */
+void
+GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
+{
+  struct GNUNET_HELPER_Handle *h = sh->h;
+
+  sh->cont = NULL;
+  sh->cont_cls = NULL;
+  if (0 == sh->wpos)
+  {
+    GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
+    if (NULL == h->sh_head)
+    {
+      GNUNET_SCHEDULER_cancel (h->write_task);
+      h->write_task = GNUNET_SCHEDULER_NO_TASK;
+    }
+    GNUNET_free (sh);
+  }
 }