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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @brief API for dealing with (SUID) helper processes that communicate via
22 * GNUNET_MessageHeaders on stdin/stdout
23 * @author Philipp Toelke
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_mst_lib.h"
32 * Entry in the queue of messages we need to transmit to the helper.
34 struct GNUNET_HELPER_SendHandle
38 * This is an entry in a DLL.
40 struct GNUNET_HELPER_SendHandle *next;
43 * This is an entry in a DLL.
45 struct GNUNET_HELPER_SendHandle *prev;
48 * Message to transmit (allocated at the end of this struct)
50 const struct GNUNET_MessageHeader *msg;
53 * The handle to a helper process.
55 struct GNUNET_HELPER_Handle *h;
58 * Function to call upon completion.
60 GNUNET_HELPER_Continuation cont;
68 * Current write position.
76 * The handle to a helper process.
78 struct GNUNET_HELPER_Handle
82 * PipeHandle to receive data from the helper
84 struct GNUNET_DISK_PipeHandle *helper_in;
87 * PipeHandle to send data to the helper
89 struct GNUNET_DISK_PipeHandle *helper_out;
92 * FileHandle to receive data from the helper
94 const struct GNUNET_DISK_FileHandle *fh_from_helper;
97 * FileHandle to send data to the helper
99 const struct GNUNET_DISK_FileHandle *fh_to_helper;
102 * The process id of the helper
104 struct GNUNET_OS_Process *helper_proc;
107 * The Message-Tokenizer that tokenizes the messages comming from the helper
109 struct GNUNET_MessageStreamTokenizer *mst;
112 * The exception callback
114 GNUNET_HELPER_ExceptionCallback exp_cb;
117 * The closure for callbacks
122 * First message queued for transmission to helper.
124 struct GNUNET_HELPER_SendHandle *sh_head;
127 * Last message queued for transmission to helper.
129 struct GNUNET_HELPER_SendHandle *sh_tail;
137 * NULL-terminated list of command-line arguments.
142 * Task to read from the helper.
144 struct GNUNET_SCHEDULER_Task *read_task;
147 * Task to read from the helper.
149 struct GNUNET_SCHEDULER_Task *write_task;
154 struct GNUNET_SCHEDULER_Task *restart_task;
157 * Does the helper support the use of a control pipe for signalling?
159 int with_control_pipe;
162 * Count start attempts to increase linear back off
164 unsigned int retry_back_off;
169 * Sends termination signal to the helper process. The helper process is not
170 * reaped; call GNUNET_HELPER_wait() for reaping the dead helper process.
172 * @param h the helper handle
173 * @param soft_kill if GNUNET_YES, signals termination by closing the helper's
174 * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
175 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
178 GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h,
181 struct GNUNET_HELPER_SendHandle *sh;
184 while (NULL != (sh = h->sh_head))
186 GNUNET_CONTAINER_DLL_remove (h->sh_head,
189 if (NULL != sh->cont)
190 sh->cont (sh->cont_cls, GNUNET_NO);
193 if (NULL != h->restart_task)
195 GNUNET_SCHEDULER_cancel (h->restart_task);
196 h->restart_task = NULL;
198 if (NULL != h->read_task)
200 GNUNET_SCHEDULER_cancel (h->read_task);
203 if (NULL == h->helper_proc)
204 return GNUNET_SYSERR;
205 if (GNUNET_YES == soft_kill)
207 /* soft-kill only possible with pipes */
208 GNUNET_assert (NULL != h->helper_in);
209 ret = GNUNET_DISK_pipe_close (h->helper_in);
211 h->fh_to_helper = NULL;
214 if (0 != GNUNET_OS_process_kill (h->helper_proc, GNUNET_TERM_SIG))
215 return GNUNET_SYSERR;
221 * Reap the helper process. This call is blocking(!). The helper process
222 * should either be sent a termination signal before or should be dead before
223 * calling this function
225 * @param h the helper handle
226 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
229 GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
231 struct GNUNET_HELPER_SendHandle *sh;
235 if (NULL != h->helper_proc)
237 ret = GNUNET_OS_process_wait (h->helper_proc);
238 GNUNET_OS_process_destroy (h->helper_proc);
239 h->helper_proc = NULL;
241 if (NULL != h->read_task)
243 GNUNET_SCHEDULER_cancel (h->read_task);
246 if (NULL != h->write_task)
248 GNUNET_SCHEDULER_cancel (h->write_task);
249 h->write_task = NULL;
251 if (NULL != h->helper_in)
253 GNUNET_DISK_pipe_close (h->helper_in);
255 h->fh_to_helper = NULL;
257 if (NULL != h->helper_out)
259 GNUNET_DISK_pipe_close (h->helper_out);
260 h->helper_out = NULL;
261 h->fh_from_helper = NULL;
263 while (NULL != (sh = h->sh_head))
265 GNUNET_CONTAINER_DLL_remove (h->sh_head,
268 if (NULL != sh->cont)
269 sh->cont (sh->cont_cls, GNUNET_NO);
272 /* purge MST buffer */
274 (void) GNUNET_MST_from_buffer (h->mst,
283 * Stop the helper process, we're closing down or had an error.
285 * @param h handle to the helper process
286 * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
287 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
290 stop_helper (struct GNUNET_HELPER_Handle *h,
293 if (NULL != h->restart_task)
295 GNUNET_SCHEDULER_cancel (h->restart_task);
296 h->restart_task = NULL;
300 GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
301 GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (h));
307 * Restart the helper process.
309 * @param cls handle to the helper process
312 restart_task (void *cls);
316 * Read from the helper-process
318 * @param cls handle to the helper process
321 helper_read (void *cls)
323 struct GNUNET_HELPER_Handle *h = cls;
324 char buf[GNUNET_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
328 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
331 /* On read-error, restart the helper */
332 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
333 _("Error reading from `%s': %s\n"),
336 if (NULL != h->exp_cb)
338 h->exp_cb (h->cb_cls);
339 GNUNET_HELPER_stop (h, GNUNET_NO);
342 stop_helper (h, GNUNET_NO);
343 /* Restart the helper */
344 h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
351 /* this happens if the helper is shut down via a
352 signal, so it is not a "hard" error */
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "Got 0 bytes from helper `%s' (EOF)\n",
356 if (NULL != h->exp_cb)
358 h->exp_cb (h->cb_cls);
359 GNUNET_HELPER_stop (h, GNUNET_NO);
362 stop_helper (h, GNUNET_NO);
363 /* Restart the helper */
365 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Got %u bytes from helper `%s'\n",
374 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
378 GNUNET_MST_from_buffer (h->mst,
383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
384 _("Failed to parse inbound message from helper `%s'\n"),
386 if (NULL != h->exp_cb)
388 h->exp_cb (h->cb_cls);
389 GNUNET_HELPER_stop (h, GNUNET_NO);
392 stop_helper (h, GNUNET_NO);
393 /* Restart the helper */
394 h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
403 * Start the helper process.
405 * @param h handle to the helper process
408 start_helper (struct GNUNET_HELPER_Handle *h)
410 h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
411 h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
412 if ( (h->helper_in == NULL) || (h->helper_out == NULL))
414 /* out of file descriptors? try again later... */
415 stop_helper (h, GNUNET_NO);
417 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Starting HELPER process `%s'\n",
426 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
428 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
430 GNUNET_OS_start_process_vap (h->with_control_pipe, GNUNET_OS_INHERIT_STD_ERR,
431 h->helper_in, h->helper_out, NULL,
434 if (NULL == h->helper_proc)
436 /* failed to start process? try again later... */
437 stop_helper (h, GNUNET_NO);
438 h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
443 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
444 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
446 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
454 * Restart the helper process.
456 * @param cls handle to the helper process
459 restart_task (void *cls)
461 struct GNUNET_HELPER_Handle*h = cls;
463 h->restart_task = NULL;
465 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
466 "Restarting helper with back-off %u\n",
473 * Starts a helper and begins reading from it. The helper process is
474 * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
475 * or when the exp_cb callback is not NULL.
477 * @param with_control_pipe does the helper support the use of a control pipe for signalling?
478 * @param binary_name name of the binary to run
479 * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
480 * argument must not be modified by the client for
481 * the lifetime of the helper handle)
482 * @param cb function to call if we get messages from the helper
483 * @param exp_cb the exception callback to call. Set this to NULL if the helper
484 * process has to be restarted automatically when it dies/crashes
485 * @param cb_cls closure for the above callback
486 * @return the new Handle, NULL on error
488 struct GNUNET_HELPER_Handle *
489 GNUNET_HELPER_start (int with_control_pipe,
490 const char *binary_name,
491 char *const binary_argv[],
492 GNUNET_MessageTokenizerCallback cb,
493 GNUNET_HELPER_ExceptionCallback exp_cb,
496 struct GNUNET_HELPER_Handle *h;
499 h = GNUNET_new (struct GNUNET_HELPER_Handle);
500 h->with_control_pipe = with_control_pipe;
501 /* Lookup in libexec path only if we are starting gnunet helpers */
502 if (NULL != strstr (binary_name, "gnunet"))
503 h->binary_name = GNUNET_OS_get_libexec_binary_path (binary_name);
505 h->binary_name = GNUNET_strdup (binary_name);
506 for (c = 0; NULL != binary_argv[c]; c++);
507 h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1));
508 for (c = 0; NULL != binary_argv[c]; c++)
509 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
510 h->binary_argv[c] = NULL;
513 h->mst = GNUNET_MST_create (cb,
516 h->retry_back_off = 0;
523 * Free's the resources occupied by the helper handle
525 * @param h the helper handle to free
528 GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
531 struct GNUNET_HELPER_SendHandle *sh;
533 if (NULL != h->write_task)
535 GNUNET_SCHEDULER_cancel (h->write_task);
536 h->write_task = NULL;
538 GNUNET_assert (NULL == h->read_task);
539 GNUNET_assert (NULL == h->restart_task);
540 while (NULL != (sh = h->sh_head))
542 GNUNET_CONTAINER_DLL_remove (h->sh_head,
545 if (NULL != sh->cont)
546 sh->cont (sh->cont_cls, GNUNET_SYSERR);
550 GNUNET_MST_destroy (h->mst);
551 GNUNET_free (h->binary_name);
552 for (c = 0; h->binary_argv[c] != NULL; c++)
553 GNUNET_free (h->binary_argv[c]);
554 GNUNET_free (h->binary_argv);
560 * Kills the helper, closes the pipe and frees the handle
562 * @param h handle to helper to stop
563 * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
564 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
567 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h,
571 stop_helper (h, soft_kill);
572 GNUNET_HELPER_destroy (h);
577 * Write to the helper-process
579 * @param cls handle to the helper process
582 helper_write (void *cls)
584 struct GNUNET_HELPER_Handle *h = cls;
585 struct GNUNET_HELPER_SendHandle *sh;
589 h->write_task = NULL;
590 if (NULL == (sh = h->sh_head))
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "Helper write had no work!\n");
594 return; /* how did this happen? */
596 buf = (const char*) sh->msg;
597 t = GNUNET_DISK_file_write (h->fh_to_helper,
599 ntohs (sh->msg->size) - sh->wpos);
602 /* On write-error, restart the helper */
603 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
604 _("Error writing to `%s': %s\n"),
607 if (NULL != h->exp_cb)
609 h->exp_cb (h->cb_cls);
610 GNUNET_HELPER_stop (h, GNUNET_NO);
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614 "Stopping and restarting helper task!\n");
615 stop_helper (h, GNUNET_NO);
616 /* Restart the helper */
617 h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623 "Transmitted %u bytes to %s\n",
627 if (sh->wpos == ntohs (sh->msg->size))
629 GNUNET_CONTAINER_DLL_remove (h->sh_head,
632 if (NULL != sh->cont)
633 sh->cont (sh->cont_cls, GNUNET_YES);
636 if (NULL != h->sh_head)
637 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
645 * Send an message to the helper.
647 * @param h helper to send message to
648 * @param msg message to send
649 * @param can_drop can the message be dropped if there is already one in the queue?
650 * @param cont continuation to run once the message is out (#GNUNET_OK on succees, #GNUNET_NO
651 * if the helper process died, #GNUNET_SYSERR during #GNUNET_HELPER_destroy).
652 * @param cont_cls closure for @a cont
653 * @return NULL if the message was dropped,
654 * otherwise handle to cancel *cont* (actual transmission may
657 struct GNUNET_HELPER_SendHandle *
658 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
659 const struct GNUNET_MessageHeader *msg,
661 GNUNET_HELPER_Continuation cont,
664 struct GNUNET_HELPER_SendHandle *sh;
667 if (NULL == h->fh_to_helper)
669 if ( (GNUNET_YES == can_drop) &&
670 (NULL != h->sh_head) )
672 mlen = ntohs (msg->size);
673 sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
674 sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
675 GNUNET_memcpy (&sh[1], msg, mlen);
678 sh->cont_cls = cont_cls;
679 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head,
682 if (NULL == h->write_task)
683 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
692 * Cancel a #GNUNET_HELPER_send operation. If possible, transmitting the
693 * message is also aborted, but at least 'cont' won't be
696 * @param sh operation to cancel
699 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
701 struct GNUNET_HELPER_Handle *h = sh->h;
707 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
709 if (NULL == h->sh_head)
711 GNUNET_SCHEDULER_cancel (h->write_task);
712 h->write_task = NULL;
718 /* end of helper.c */