2 This file is part of GNUnet.
3 Copyright (C) 2011, 2012 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @brief API for dealing with (SUID) helper processes that communicate via
24 * GNUNET_MessageHeaders on stdin/stdout
25 * @author Philipp Toelke
26 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
33 * Entry in the queue of messages we need to transmit to the helper.
35 struct GNUNET_HELPER_SendHandle
39 * This is an entry in a DLL.
41 struct GNUNET_HELPER_SendHandle *next;
44 * This is an entry in a DLL.
46 struct GNUNET_HELPER_SendHandle *prev;
49 * Message to transmit (allocated at the end of this struct)
51 const struct GNUNET_MessageHeader *msg;
54 * The handle to a helper process.
56 struct GNUNET_HELPER_Handle *h;
59 * Function to call upon completion.
61 GNUNET_HELPER_Continuation cont;
69 * Current write position.
77 * The handle to a helper process.
79 struct GNUNET_HELPER_Handle
83 * PipeHandle to receive data from the helper
85 struct GNUNET_DISK_PipeHandle *helper_in;
88 * PipeHandle to send data to the helper
90 struct GNUNET_DISK_PipeHandle *helper_out;
93 * FileHandle to receive data from the helper
95 const struct GNUNET_DISK_FileHandle *fh_from_helper;
98 * FileHandle to send data to the helper
100 const struct GNUNET_DISK_FileHandle *fh_to_helper;
103 * The process id of the helper
105 struct GNUNET_OS_Process *helper_proc;
108 * The Message-Tokenizer that tokenizes the messages comming from the helper
110 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
113 * The exception callback
115 GNUNET_HELPER_ExceptionCallback exp_cb;
118 * The closure for callbacks
123 * First message queued for transmission to helper.
125 struct GNUNET_HELPER_SendHandle *sh_head;
128 * Last message queued for transmission to helper.
130 struct GNUNET_HELPER_SendHandle *sh_tail;
138 * NULL-terminated list of command-line arguments.
143 * Task to read from the helper.
145 struct GNUNET_SCHEDULER_Task * read_task;
148 * Task to read from the helper.
150 struct GNUNET_SCHEDULER_Task * write_task;
155 struct GNUNET_SCHEDULER_Task * restart_task;
158 * Does the helper support the use of a control pipe for signalling?
160 int with_control_pipe;
163 * Count start attempts to increase linear back off
165 unsigned int retry_back_off;
170 * Sends termination signal to the helper process. The helper process is not
171 * reaped; call GNUNET_HELPER_wait() for reaping the dead helper process.
173 * @param h the helper handle
174 * @param soft_kill if GNUNET_YES, signals termination by closing the helper's
175 * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
176 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
179 GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h,
182 struct GNUNET_HELPER_SendHandle *sh;
185 while (NULL != (sh = h->sh_head))
187 GNUNET_CONTAINER_DLL_remove (h->sh_head,
190 if (NULL != sh->cont)
191 sh->cont (sh->cont_cls, GNUNET_NO);
194 if (NULL != h->restart_task)
196 GNUNET_SCHEDULER_cancel (h->restart_task);
197 h->restart_task = NULL;
199 if (NULL != h->read_task)
201 GNUNET_SCHEDULER_cancel (h->read_task);
204 if (NULL == h->helper_proc)
205 return GNUNET_SYSERR;
206 if (GNUNET_YES == soft_kill)
208 /* soft-kill only possible with pipes */
209 GNUNET_assert (NULL != h->helper_in);
210 ret = GNUNET_DISK_pipe_close (h->helper_in);
212 h->fh_to_helper = NULL;
215 if (0 != GNUNET_OS_process_kill (h->helper_proc, GNUNET_TERM_SIG))
216 return GNUNET_SYSERR;
222 * Reap the helper process. This call is blocking(!). The helper process
223 * should either be sent a termination signal before or should be dead before
224 * calling this function
226 * @param h the helper handle
227 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
230 GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
232 struct GNUNET_HELPER_SendHandle *sh;
236 if (NULL != h->helper_proc)
238 ret = GNUNET_OS_process_wait (h->helper_proc);
239 GNUNET_OS_process_destroy (h->helper_proc);
240 h->helper_proc = NULL;
242 if (NULL != h->read_task)
244 GNUNET_SCHEDULER_cancel (h->read_task);
247 if (NULL != h->write_task)
249 GNUNET_SCHEDULER_cancel (h->write_task);
250 h->write_task = NULL;
252 if (NULL != h->helper_in)
254 GNUNET_DISK_pipe_close (h->helper_in);
256 h->fh_to_helper = NULL;
258 if (NULL != h->helper_out)
260 GNUNET_DISK_pipe_close (h->helper_out);
261 h->helper_out = NULL;
262 h->fh_from_helper = NULL;
264 while (NULL != (sh = h->sh_head))
266 GNUNET_CONTAINER_DLL_remove (h->sh_head,
269 if (NULL != sh->cont)
270 sh->cont (sh->cont_cls, GNUNET_NO);
273 /* purge MST buffer */
275 (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO);
281 * Stop the helper process, we're closing down or had an error.
283 * @param h handle to the helper process
284 * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
285 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
288 stop_helper (struct GNUNET_HELPER_Handle *h,
291 if (NULL != h->restart_task)
293 GNUNET_SCHEDULER_cancel (h->restart_task);
294 h->restart_task = NULL;
298 GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
299 GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (h));
305 * Restart the helper process.
307 * @param cls handle to the helper process
310 restart_task (void *cls);
314 * Read from the helper-process
316 * @param cls handle to the helper process
319 helper_read (void *cls)
321 struct GNUNET_HELPER_Handle *h = cls;
322 const struct GNUNET_SCHEDULER_TaskContext *tc;
323 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
327 tc = GNUNET_SCHEDULER_get_task_context ();
328 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
331 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
332 h->fh_from_helper, &helper_read, h);
335 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
338 /* On read-error, restart the helper */
339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
340 _("Error reading from `%s': %s\n"),
343 if (NULL != h->exp_cb)
345 h->exp_cb (h->cb_cls);
346 GNUNET_HELPER_stop (h, GNUNET_NO);
349 stop_helper (h, GNUNET_NO);
350 /* Restart the helper */
351 h->restart_task = GNUNET_SCHEDULER_add_delayed(
352 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
353 h->retry_back_off), &restart_task, h);
358 /* this happens if the helper is shut down via a
359 signal, so it is not a "hard" error */
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Got 0 bytes from helper `%s' (EOF)\n",
363 if (NULL != h->exp_cb)
365 h->exp_cb (h->cb_cls);
366 GNUNET_HELPER_stop (h, GNUNET_NO);
369 stop_helper (h, GNUNET_NO);
370 /* Restart the helper */
371 h->restart_task = GNUNET_SCHEDULER_add_delayed(
372 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
373 h->retry_back_off), &restart_task, h);
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Got %u bytes from helper `%s'\n",
380 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
381 h->fh_from_helper, &helper_read, h);
383 GNUNET_SERVER_mst_receive (h->mst, NULL, buf, t, GNUNET_NO, GNUNET_NO))
385 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
386 _("Failed to parse inbound message from helper `%s'\n"),
388 if (NULL != h->exp_cb)
390 h->exp_cb (h->cb_cls);
391 GNUNET_HELPER_stop (h, GNUNET_NO);
394 stop_helper (h, GNUNET_NO);
395 /* Restart the helper */
396 h->restart_task = GNUNET_SCHEDULER_add_delayed(
397 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
398 h->retry_back_off), &restart_task, h);
405 * Start the helper process.
407 * @param h handle to the helper process
410 start_helper (struct GNUNET_HELPER_Handle *h)
412 h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
413 h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
414 if ( (h->helper_in == NULL) || (h->helper_out == NULL))
416 /* out of file descriptors? try again later... */
417 stop_helper (h, GNUNET_NO);
419 GNUNET_SCHEDULER_add_delayed(
420 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
421 h->retry_back_off), &restart_task, h);
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Starting HELPER process `%s'\n",
428 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
430 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
432 GNUNET_OS_start_process_vap (h->with_control_pipe, GNUNET_OS_INHERIT_STD_ERR,
433 h->helper_in, h->helper_out, NULL,
436 if (NULL == h->helper_proc)
438 /* failed to start process? try again later... */
439 stop_helper (h, GNUNET_NO);
440 h->restart_task = GNUNET_SCHEDULER_add_delayed(
441 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
442 h->retry_back_off), &restart_task, h);
445 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
446 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
448 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
456 * Restart the helper process.
458 * @param cls handle to the helper process
461 restart_task (void *cls)
463 struct GNUNET_HELPER_Handle*h = cls;
465 h->restart_task = NULL;
467 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
468 "Restarting helper with back-off %u\n",
475 * Starts a helper and begins reading from it. The helper process is
476 * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
477 * or when the exp_cb callback is not NULL.
479 * @param with_control_pipe does the helper support the use of a control pipe for signalling?
480 * @param binary_name name of the binary to run
481 * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
482 * argument must not be modified by the client for
483 * the lifetime of the helper handle)
484 * @param cb function to call if we get messages from the helper
485 * @param exp_cb the exception callback to call. Set this to NULL if the helper
486 * process has to be restarted automatically when it dies/crashes
487 * @param cb_cls closure for the above callback
488 * @return the new Handle, NULL on error
490 struct GNUNET_HELPER_Handle *
491 GNUNET_HELPER_start (int with_control_pipe,
492 const char *binary_name,
493 char *const binary_argv[],
494 GNUNET_SERVER_MessageTokenizerCallback cb,
495 GNUNET_HELPER_ExceptionCallback exp_cb,
498 struct GNUNET_HELPER_Handle *h;
501 h = GNUNET_new (struct GNUNET_HELPER_Handle);
502 h->with_control_pipe = with_control_pipe;
503 /* Lookup in libexec path only if we are starting gnunet helpers */
504 if (NULL != strstr (binary_name, "gnunet"))
505 h->binary_name = GNUNET_OS_get_libexec_binary_path (binary_name);
507 h->binary_name = GNUNET_strdup (binary_name);
508 for (c = 0; NULL != binary_argv[c]; c++);
509 h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1));
510 for (c = 0; NULL != binary_argv[c]; c++)
511 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
512 h->binary_argv[c] = NULL;
515 h->mst = GNUNET_SERVER_mst_create (cb, h->cb_cls);
517 h->retry_back_off = 0;
524 * Free's the resources occupied by the helper handle
526 * @param h the helper handle to free
529 GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
532 struct GNUNET_HELPER_SendHandle *sh;
534 if (NULL != h->write_task)
536 GNUNET_SCHEDULER_cancel (h->write_task);
537 h->write_task = NULL;
539 GNUNET_assert (NULL == h->read_task);
540 GNUNET_assert (NULL == h->restart_task);
541 while (NULL != (sh = h->sh_head))
543 GNUNET_CONTAINER_DLL_remove (h->sh_head,
546 if (NULL != sh->cont)
547 sh->cont (sh->cont_cls, GNUNET_SYSERR);
551 GNUNET_SERVER_mst_destroy (h->mst);
552 GNUNET_free (h->binary_name);
553 for (c = 0; h->binary_argv[c] != NULL; c++)
554 GNUNET_free (h->binary_argv[c]);
555 GNUNET_free (h->binary_argv);
561 * Kills the helper, closes the pipe and frees the handle
563 * @param h handle to helper to stop
564 * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
565 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
568 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h,
572 stop_helper (h, soft_kill);
573 GNUNET_HELPER_destroy (h);
578 * Write to the helper-process
580 * @param cls handle to the helper process
583 helper_write (void *cls)
585 struct GNUNET_HELPER_Handle *h = cls;
586 const struct GNUNET_SCHEDULER_TaskContext *tc;
587 struct GNUNET_HELPER_SendHandle *sh;
591 h->write_task = NULL;
592 tc = GNUNET_SCHEDULER_get_task_context ();
593 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597 "Helper write triggered during shutdown, retrying\n");
598 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
599 h->fh_to_helper, &helper_write, h);
602 if (NULL == (sh = h->sh_head))
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "Helper write had no work!\n");
606 return; /* how did this happen? */
608 buf = (const char*) sh->msg;
609 t = GNUNET_DISK_file_write (h->fh_to_helper,
611 ntohs (sh->msg->size) - sh->wpos);
614 /* On write-error, restart the helper */
615 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
616 _("Error writing to `%s': %s\n"),
619 if (NULL != h->exp_cb)
621 h->exp_cb (h->cb_cls);
622 GNUNET_HELPER_stop (h, GNUNET_NO);
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "Stopping and restarting helper task!\n");
627 stop_helper (h, GNUNET_NO);
628 /* Restart the helper */
629 h->restart_task = GNUNET_SCHEDULER_add_delayed(
630 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
631 h->retry_back_off), &restart_task, h);
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Transmitted %u bytes to %s\n",
639 if (sh->wpos == ntohs (sh->msg->size))
641 GNUNET_CONTAINER_DLL_remove (h->sh_head,
644 if (NULL != sh->cont)
645 sh->cont (sh->cont_cls, GNUNET_YES);
648 if (NULL != h->sh_head)
649 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
657 * Send an message to the helper.
659 * @param h helper to send message to
660 * @param msg message to send
661 * @param can_drop can the message be dropped if there is already one in the queue?
662 * @param cont continuation to run once the message is out (#GNUNET_OK on succees, #GNUNET_NO
663 * if the helper process died, #GNUNET_SYSERR during #GNUNET_HELPER_destroy).
664 * @param cont_cls closure for @a cont
665 * @return NULL if the message was dropped,
666 * otherwise handle to cancel *cont* (actual transmission may
669 struct GNUNET_HELPER_SendHandle *
670 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
671 const struct GNUNET_MessageHeader *msg,
673 GNUNET_HELPER_Continuation cont,
676 struct GNUNET_HELPER_SendHandle *sh;
679 if (NULL == h->fh_to_helper)
681 if ( (GNUNET_YES == can_drop) &&
682 (NULL != h->sh_head) )
684 mlen = ntohs (msg->size);
685 sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
686 sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
687 memcpy (&sh[1], msg, mlen);
690 sh->cont_cls = cont_cls;
691 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head,
694 if (NULL == h->write_task)
695 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
704 * Cancel a #GNUNET_HELPER_send operation. If possible, transmitting the
705 * message is also aborted, but at least 'cont' won't be
708 * @param sh operation to cancel
711 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
713 struct GNUNET_HELPER_Handle *h = sh->h;
719 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
721 if (NULL == h->sh_head)
723 GNUNET_SCHEDULER_cancel (h->write_task);
724 h->write_task = NULL;
730 /* end of helper.c */