X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Futil%2Fhelper.c;h=404dad4fa61d7ccaffcac24417331ae409baf893;hb=fdbe690beeec04066f18302401096eb5212c3f6a;hp=c89f1ecb532121ffdccea9ca50553081fa937edc;hpb=825eee6623a96cdef4ee11b375b7baee4ddf74b8;p=oweals%2Fgnunet.git diff --git a/src/util/helper.c b/src/util/helper.c index c89f1ecb5..404dad4fa 100644 --- a/src/util/helper.c +++ b/src/util/helper.c @@ -49,12 +49,12 @@ struct GNUNET_HELPER_SendHandle * 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. */ @@ -83,22 +83,22 @@ struct GNUNET_HELPER_Handle * PipeHandle to receive data from the helper */ struct GNUNET_DISK_PipeHandle *helper_in; - + /** * PipeHandle to send data to the helper */ struct GNUNET_DISK_PipeHandle *helper_out; - + /** * FileHandle to receive data from the helper */ const struct GNUNET_DISK_FileHandle *fh_from_helper; - + /** * FileHandle to send data to the helper */ const struct GNUNET_DISK_FileHandle *fh_to_helper; - + /** * The process id of the helper */ @@ -138,7 +138,7 @@ struct GNUNET_HELPER_Handle * NULL-terminated list of command-line arguments. */ char **binary_argv; - + /** * Task to read from the helper. */ @@ -169,10 +169,11 @@ struct GNUNET_HELPER_Handle * @param h the helper handle * @param soft_kill if GNUNET_YES, signals termination by closing the helper's * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper - * @return GNUNET_OK on success; GNUNET_SYSERR on error + * @return #GNUNET_OK on success; #GNUNET_SYSERR on error */ int -GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill) +GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, + int soft_kill) { struct GNUNET_HELPER_SendHandle *sh; int ret; @@ -191,10 +192,15 @@ GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill) GNUNET_SCHEDULER_cancel (h->restart_task); h->restart_task = GNUNET_SCHEDULER_NO_TASK; } + if (GNUNET_SCHEDULER_NO_TASK != h->read_task) + { + GNUNET_SCHEDULER_cancel (h->read_task); + h->read_task = GNUNET_SCHEDULER_NO_TASK; + } if (NULL == h->helper_proc) return GNUNET_SYSERR; if (GNUNET_YES == soft_kill) - { + { /* soft-kill only possible with pipes */ GNUNET_assert (NULL != h->helper_in); ret = GNUNET_DISK_pipe_close (h->helper_in); @@ -202,7 +208,7 @@ GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill) h->fh_to_helper = NULL; return ret; } - if (0 != GNUNET_OS_process_kill (h->helper_proc, SIGTERM)) + if (0 != GNUNET_OS_process_kill (h->helper_proc, GNUNET_TERM_SIG)) return GNUNET_SYSERR; return GNUNET_OK; } @@ -214,7 +220,7 @@ GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill) * calling this function * * @param h the helper handle - * @return GNUNET_OK on success; GNUNET_SYSERR on error + * @return #GNUNET_OK on success; #GNUNET_SYSERR on error */ int GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h) @@ -261,7 +267,8 @@ GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h) GNUNET_free (sh); } /* purge MST buffer */ - (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO); + if (NULL != h->mst) + (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO); return ret; } @@ -270,14 +277,23 @@ GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h) * Stop the helper process, we're closing down or had an error. * * @param h handle to the helper process - * @param soft_kill if GNUNET_YES, signals termination by closing the helper's - * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper + * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's + * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper */ static void -stop_helper (struct GNUNET_HELPER_Handle *h, int soft_kill) +stop_helper (struct GNUNET_HELPER_Handle *h, + int soft_kill) { - GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill)); - GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (h)); + if (GNUNET_SCHEDULER_NO_TASK != h->restart_task) + { + GNUNET_SCHEDULER_cancel (h->restart_task); + h->restart_task = GNUNET_SCHEDULER_NO_TASK; + } + else + { + GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill)); + GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (h)); + } } @@ -336,9 +352,9 @@ helper_read (void *cls, } if (0 == t) { - /* this happens if the helper is shut down via a + /* this happens if the helper is shut down via a signal, so it is not a "hard" error */ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got 0 bytes from helper `%s' (EOF)\n", h->binary_name); if (NULL != h->exp_cb) @@ -354,7 +370,7 @@ helper_read (void *cls, &restart_task, h); return; } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes from helper `%s'\n", (unsigned int) t, h->binary_name); @@ -363,7 +379,7 @@ helper_read (void *cls, if (GNUNET_SYSERR == GNUNET_SERVER_mst_receive (h->mst, NULL, buf, t, GNUNET_NO, GNUNET_NO)) { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to parse inbound message from helper `%s'\n"), h->binary_name); if (NULL != h->exp_cb) @@ -371,7 +387,7 @@ helper_read (void *cls, h->exp_cb (h->cb_cls); GNUNET_HELPER_stop (h, GNUNET_NO); return; - } + } stop_helper (h, GNUNET_NO); /* Restart the helper */ h->restart_task = @@ -398,7 +414,7 @@ start_helper (struct GNUNET_HELPER_Handle *h) stop_helper (h, GNUNET_NO); h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, - &restart_task, h); + &restart_task, h); return; } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, @@ -409,7 +425,7 @@ start_helper (struct GNUNET_HELPER_Handle *h) 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->with_control_pipe, GNUNET_OS_INHERIT_STD_ERR, + 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); @@ -419,15 +435,16 @@ start_helper (struct GNUNET_HELPER_Handle *h) stop_helper (h, GNUNET_NO); h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, - &restart_task, h); + &restart_task, h); return; } GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE); GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ); - h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, - h->fh_from_helper, - &helper_read, - h); + if (NULL != h->mst) + h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, + h->fh_from_helper, + &helper_read, + h); } @@ -475,7 +492,7 @@ GNUNET_HELPER_start (int with_control_pipe, struct GNUNET_HELPER_Handle *h; unsigned int c; - h = GNUNET_malloc (sizeof (struct GNUNET_HELPER_Handle)); + h = GNUNET_new (struct GNUNET_HELPER_Handle); h->with_control_pipe = with_control_pipe; /* Lookup in libexec path only if we are starting gnunet helpers */ if (NULL != strstr (binary_name, "gnunet")) @@ -488,7 +505,8 @@ GNUNET_HELPER_start (int with_control_pipe, 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); + if (NULL != cb) + h->mst = GNUNET_SERVER_mst_create (cb, h->cb_cls); h->exp_cb = exp_cb; start_helper (h); return h; @@ -504,8 +522,26 @@ void GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h) { unsigned int c; + struct GNUNET_HELPER_SendHandle *sh; - GNUNET_SERVER_mst_destroy (h->mst); + if (GNUNET_SCHEDULER_NO_TASK != h->write_task) + { + GNUNET_SCHEDULER_cancel (h->write_task); + h->write_task = GNUNET_SCHEDULER_NO_TASK; + } + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->read_task); + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->restart_task); + while (NULL != (sh = h->sh_head)) + { + 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); + } + if (NULL != h->mst) + GNUNET_SERVER_mst_destroy (h->mst); GNUNET_free (h->binary_name); for (c = 0; h->binary_argv[c] != NULL; c++) GNUNET_free (h->binary_argv[c]); @@ -518,11 +554,12 @@ GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h) * Kills the helper, closes the pipe and frees the handle * * @param h handle to helper to stop - * @param soft_kill if GNUNET_YES, signals termination by closing the helper's - * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper + * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's + * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper */ void -GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, int soft_kill) +GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, + int soft_kill) { h->exp_cb = NULL; stop_helper (h, soft_kill); @@ -549,19 +586,27 @@ helper_write (void *cls, 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); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Helper write triggered during shutdown, retrying\n"); + h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, + h->fh_to_helper, &helper_write, h); return; - } + } if (NULL == (sh = h->sh_head)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Helper write had no work!\n"); return; /* how did this happen? */ + } 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) + t = GNUNET_DISK_file_write (h->fh_to_helper, + &buf[sh->wpos], + ntohs (sh->msg->size) - sh->wpos); + if (-1 == t) { /* On write-error, restart the helper */ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Error writing to `%s': %s\n"), + _("Error writing to `%s': %s\n"), h->binary_name, STRERROR (errno)); if (NULL != h->exp_cb) @@ -570,6 +615,8 @@ helper_write (void *cls, GNUNET_HELPER_stop (h, GNUNET_NO); return; } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Stopping and restarting helper task!\n"); stop_helper (h, GNUNET_NO); /* Restart the helper */ h->restart_task = @@ -577,6 +624,10 @@ helper_write (void *cls, &restart_task, h); return; } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Transmitted %u bytes to %s\n", + (unsigned int) t, + h->binary_name); sh->wpos += t; if (sh->wpos == ntohs (sh->msg->size)) { @@ -589,8 +640,8 @@ helper_write (void *cls, } if (NULL != h->sh_head) h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, - h->fh_to_helper, - &helper_write, + h->fh_to_helper, + &helper_write, h); } @@ -601,15 +652,15 @@ helper_write (void *cls, * @param h helper to send message to * @param msg message to send * @param can_drop can the message be dropped if there is already one in the queue? - * @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 NULL if the message was dropped, + * @param cont continuation to run once the message is out (#GNUNET_OK on succees, #GNUNET_NO + * if the helper process died, #GNUNET_SYSERR during #GNUNET_HELPER_destroy). + * @param cont_cls closure for @a cont + * @return NULL if the message was dropped, * otherwise handle to cancel *cont* (actual transmission may * not be abortable) */ struct GNUNET_HELPER_SendHandle * -GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h, +GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h, const struct GNUNET_MessageHeader *msg, int can_drop, GNUNET_HELPER_Continuation cont, @@ -635,15 +686,15 @@ GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h, 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->fh_to_helper, + &helper_write, h); - + return sh; } /** - * Cancel a 'send' operation. If possible, transmitting the + * Cancel a #GNUNET_HELPER_send operation. If possible, transmitting the * message is also aborted, but at least 'cont' won't be * called. * @@ -659,12 +710,12 @@ GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh) if (0 == sh->wpos) { GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh); + GNUNET_free (sh); if (NULL == h->sh_head) { GNUNET_SCHEDULER_cancel (h->write_task); h->write_task = GNUNET_SCHEDULER_NO_TASK; } - GNUNET_free (sh); } }