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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
30 #include "gnunet_mst_lib.h"
34 * Entry in the queue of messages we need to transmit to the helper.
36 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.
76 * The handle to a helper process.
78 struct GNUNET_HELPER_Handle
81 * PipeHandle to receive data from the helper
83 struct GNUNET_DISK_PipeHandle *helper_in;
86 * PipeHandle to send data to the helper
88 struct GNUNET_DISK_PipeHandle *helper_out;
91 * FileHandle to receive data from the helper
93 const struct GNUNET_DISK_FileHandle *fh_from_helper;
96 * FileHandle to send data to the helper
98 const struct GNUNET_DISK_FileHandle *fh_to_helper;
101 * The process id of the helper
103 struct GNUNET_OS_Process *helper_proc;
106 * The Message-Tokenizer that tokenizes the messages comming from the helper
108 struct GNUNET_MessageStreamTokenizer *mst;
111 * The exception callback
113 GNUNET_HELPER_ExceptionCallback exp_cb;
116 * The closure for callbacks
121 * First message queued for transmission to helper.
123 struct GNUNET_HELPER_SendHandle *sh_head;
126 * Last message queued for transmission to helper.
128 struct GNUNET_HELPER_SendHandle *sh_tail;
136 * NULL-terminated list of command-line arguments.
141 * Task to read from the helper.
143 struct GNUNET_SCHEDULER_Task *read_task;
146 * Task to read from the helper.
148 struct GNUNET_SCHEDULER_Task *write_task;
153 struct GNUNET_SCHEDULER_Task *restart_task;
156 * Does the helper support the use of a control pipe for signalling?
158 int with_control_pipe;
161 * Count start attempts to increase linear back off
163 unsigned int retry_back_off;
168 * Sends termination signal to the helper process. The helper process is not
169 * reaped; call GNUNET_HELPER_wait() for reaping the dead helper process.
171 * @param h the helper handle
172 * @param soft_kill if GNUNET_YES, signals termination by closing the helper's
173 * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
174 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
177 GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill)
179 struct GNUNET_HELPER_SendHandle *sh;
182 while (NULL != (sh = h->sh_head))
184 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
185 if (NULL != sh->cont)
186 sh->cont (sh->cont_cls, GNUNET_NO);
189 if (NULL != h->restart_task)
191 GNUNET_SCHEDULER_cancel (h->restart_task);
192 h->restart_task = NULL;
194 if (NULL != h->read_task)
196 GNUNET_SCHEDULER_cancel (h->read_task);
199 if (NULL == h->helper_proc)
200 return GNUNET_SYSERR;
201 if (GNUNET_YES == soft_kill)
203 /* soft-kill only possible with pipes */
204 GNUNET_assert (NULL != h->helper_in);
205 ret = GNUNET_DISK_pipe_close (h->helper_in);
207 h->fh_to_helper = NULL;
210 if (0 != GNUNET_OS_process_kill (h->helper_proc, GNUNET_TERM_SIG))
211 return GNUNET_SYSERR;
217 * Reap the helper process. This call is blocking(!). The helper process
218 * should either be sent a termination signal before or should be dead before
219 * calling this function
221 * @param h the helper handle
222 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
225 GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
227 struct GNUNET_HELPER_SendHandle *sh;
231 if (NULL != h->helper_proc)
233 ret = GNUNET_OS_process_wait (h->helper_proc);
234 GNUNET_OS_process_destroy (h->helper_proc);
235 h->helper_proc = NULL;
237 if (NULL != h->read_task)
239 GNUNET_SCHEDULER_cancel (h->read_task);
242 if (NULL != h->write_task)
244 GNUNET_SCHEDULER_cancel (h->write_task);
245 h->write_task = NULL;
247 if (NULL != h->helper_in)
249 GNUNET_DISK_pipe_close (h->helper_in);
251 h->fh_to_helper = NULL;
253 if (NULL != h->helper_out)
255 GNUNET_DISK_pipe_close (h->helper_out);
256 h->helper_out = NULL;
257 h->fh_from_helper = NULL;
259 while (NULL != (sh = h->sh_head))
261 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
262 if (NULL != sh->cont)
263 sh->cont (sh->cont_cls, GNUNET_NO);
266 /* purge MST buffer */
268 (void) GNUNET_MST_from_buffer (h->mst, NULL, 0, GNUNET_YES, GNUNET_NO);
274 * Stop the helper process, we're closing down or had an error.
276 * @param h handle to the helper process
277 * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
278 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
281 stop_helper (struct GNUNET_HELPER_Handle *h, int soft_kill)
283 if (NULL != h->restart_task)
285 GNUNET_SCHEDULER_cancel (h->restart_task);
286 h->restart_task = NULL;
290 GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
291 GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (h));
297 * Restart the helper process.
299 * @param cls handle to the helper process
302 restart_task (void *cls);
306 * Read from the helper-process
308 * @param cls handle to the helper process
311 helper_read (void *cls)
313 struct GNUNET_HELPER_Handle *h = cls;
314 char buf[GNUNET_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
318 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof(buf));
321 /* On read-error, restart the helper */
322 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
323 _ ("Error reading from `%s': %s\n"),
326 if (NULL != h->exp_cb)
328 h->exp_cb (h->cb_cls);
329 GNUNET_HELPER_stop (h, GNUNET_NO);
332 stop_helper (h, GNUNET_NO);
333 /* Restart the helper */
334 h->restart_task = GNUNET_SCHEDULER_add_delayed (
335 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
343 /* this happens if the helper is shut down via a
344 signal, so it is not a "hard" error */
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Got 0 bytes from helper `%s' (EOF)\n",
348 if (NULL != h->exp_cb)
350 h->exp_cb (h->cb_cls);
351 GNUNET_HELPER_stop (h, GNUNET_NO);
354 stop_helper (h, GNUNET_NO);
355 /* Restart the helper */
356 h->restart_task = GNUNET_SCHEDULER_add_delayed (
357 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364 "Got %u bytes from helper `%s'\n",
367 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
372 GNUNET_MST_from_buffer (h->mst, buf, t, GNUNET_NO, GNUNET_NO))
374 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
375 _ ("Failed to parse inbound message from helper `%s'\n"),
377 if (NULL != h->exp_cb)
379 h->exp_cb (h->cb_cls);
380 GNUNET_HELPER_stop (h, GNUNET_NO);
383 stop_helper (h, GNUNET_NO);
384 /* Restart the helper */
385 h->restart_task = GNUNET_SCHEDULER_add_delayed (
386 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
396 * Start the helper process.
398 * @param h handle to the helper process
401 start_helper (struct GNUNET_HELPER_Handle *h)
404 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
406 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
407 if ((h->helper_in == NULL) || (h->helper_out == NULL))
409 /* out of file descriptors? try again later... */
410 stop_helper (h, GNUNET_NO);
411 h->restart_task = GNUNET_SCHEDULER_add_delayed (
412 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Starting HELPER process `%s'\n",
422 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
424 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
425 h->helper_proc = GNUNET_OS_start_process_vap (h->with_control_pipe,
426 GNUNET_OS_INHERIT_STD_ERR,
432 if (NULL == h->helper_proc)
434 /* failed to start process? try again later... */
435 stop_helper (h, GNUNET_NO);
436 h->restart_task = GNUNET_SCHEDULER_add_delayed (
437 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++)
508 h->binary_argv = GNUNET_malloc (sizeof(char *) * (c + 1));
509 for (c = 0; NULL != binary_argv[c]; c++)
510 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
511 h->binary_argv[c] = NULL;
514 h->mst = GNUNET_MST_create (cb, h->cb_cls);
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, h->sh_tail, sh);
543 if (NULL != sh->cont)
544 sh->cont (sh->cont_cls, GNUNET_SYSERR);
548 GNUNET_MST_destroy (h->mst);
549 GNUNET_free (h->binary_name);
550 for (c = 0; h->binary_argv[c] != NULL; c++)
551 GNUNET_free (h->binary_argv[c]);
552 GNUNET_free (h->binary_argv);
558 * Kills the helper, closes the pipe and frees the handle
560 * @param h handle to helper to stop
561 * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
562 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
565 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, int soft_kill)
568 stop_helper (h, soft_kill);
569 GNUNET_HELPER_destroy (h);
574 * Write to the helper-process
576 * @param cls handle to the helper process
579 helper_write (void *cls)
581 struct GNUNET_HELPER_Handle *h = cls;
582 struct GNUNET_HELPER_SendHandle *sh;
586 h->write_task = NULL;
587 if (NULL == (sh = h->sh_head))
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Helper write had no work!\n");
590 return; /* how did this happen? */
592 buf = (const char *) sh->msg;
593 t = GNUNET_DISK_file_write (h->fh_to_helper,
595 ntohs (sh->msg->size) - sh->wpos);
598 /* On write-error, restart the helper */
599 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
600 _ ("Error writing to `%s': %s\n"),
603 if (NULL != h->exp_cb)
605 h->exp_cb (h->cb_cls);
606 GNUNET_HELPER_stop (h, GNUNET_NO);
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 "Stopping and restarting helper task!\n");
611 stop_helper (h, GNUNET_NO);
612 /* Restart the helper */
613 h->restart_task = GNUNET_SCHEDULER_add_delayed (
614 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Transmitted %u bytes to %s\n",
625 if (sh->wpos == ntohs (sh->msg->size))
627 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
628 if (NULL != sh->cont)
629 sh->cont (sh->cont_cls, GNUNET_YES);
632 if (NULL != h->sh_head)
634 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
642 * Send an message to the helper.
644 * @param h helper to send message to
645 * @param msg message to send
646 * @param can_drop can the message be dropped if there is already one in the queue?
647 * @param cont continuation to run once the message is out (#GNUNET_OK on succees, #GNUNET_NO
648 * if the helper process died, #GNUNET_SYSERR during #GNUNET_HELPER_destroy).
649 * @param cont_cls closure for @a cont
650 * @return NULL if the message was dropped,
651 * otherwise handle to cancel *cont* (actual transmission may
654 struct GNUNET_HELPER_SendHandle *
655 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
656 const struct GNUNET_MessageHeader *msg,
658 GNUNET_HELPER_Continuation cont,
661 struct GNUNET_HELPER_SendHandle *sh;
664 if (NULL == h->fh_to_helper)
666 if ((GNUNET_YES == can_drop) && (NULL != h->sh_head))
668 mlen = ntohs (msg->size);
669 sh = GNUNET_malloc (sizeof(struct GNUNET_HELPER_SendHandle) + mlen);
670 sh->msg = (const struct GNUNET_MessageHeader *) &sh[1];
671 GNUNET_memcpy (&sh[1], msg, mlen);
674 sh->cont_cls = cont_cls;
675 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head, h->sh_tail, sh);
676 if (NULL == h->write_task)
678 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
687 * Cancel a #GNUNET_HELPER_send operation. If possible, transmitting the
688 * message is also aborted, but at least 'cont' won't be
691 * @param sh operation to cancel
694 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
696 struct GNUNET_HELPER_Handle *h = sh->h;
702 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
704 if (NULL == h->sh_head)
706 GNUNET_SCHEDULER_cancel (h->write_task);
707 h->write_task = NULL;
713 /* end of helper.c */