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
40 * This is an entry in a DLL.
42 struct GNUNET_HELPER_SendHandle *next;
45 * This is an entry in a DLL.
47 struct GNUNET_HELPER_SendHandle *prev;
50 * Message to transmit (allocated at the end of this struct)
52 const struct GNUNET_MessageHeader *msg;
55 * The handle to a helper process.
57 struct GNUNET_HELPER_Handle *h;
60 * Function to call upon completion.
62 GNUNET_HELPER_Continuation cont;
70 * 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_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, int soft_kill)
181 struct GNUNET_HELPER_SendHandle *sh;
184 while (NULL != (sh = h->sh_head))
186 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
187 if (NULL != sh->cont)
188 sh->cont (sh->cont_cls, GNUNET_NO);
191 if (NULL != h->restart_task)
193 GNUNET_SCHEDULER_cancel (h->restart_task);
194 h->restart_task = NULL;
196 if (NULL != h->read_task)
198 GNUNET_SCHEDULER_cancel (h->read_task);
201 if (NULL == h->helper_proc)
202 return GNUNET_SYSERR;
203 if (GNUNET_YES == soft_kill)
205 /* soft-kill only possible with pipes */
206 GNUNET_assert (NULL != h->helper_in);
207 ret = GNUNET_DISK_pipe_close (h->helper_in);
209 h->fh_to_helper = NULL;
212 if (0 != GNUNET_OS_process_kill (h->helper_proc, GNUNET_TERM_SIG))
213 return GNUNET_SYSERR;
219 * Reap the helper process. This call is blocking(!). The helper process
220 * should either be sent a termination signal before or should be dead before
221 * calling this function
223 * @param h the helper handle
224 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
227 GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
229 struct GNUNET_HELPER_SendHandle *sh;
233 if (NULL != h->helper_proc)
235 ret = GNUNET_OS_process_wait (h->helper_proc);
236 GNUNET_OS_process_destroy (h->helper_proc);
237 h->helper_proc = NULL;
239 if (NULL != h->read_task)
241 GNUNET_SCHEDULER_cancel (h->read_task);
244 if (NULL != h->write_task)
246 GNUNET_SCHEDULER_cancel (h->write_task);
247 h->write_task = NULL;
249 if (NULL != h->helper_in)
251 GNUNET_DISK_pipe_close (h->helper_in);
253 h->fh_to_helper = NULL;
255 if (NULL != h->helper_out)
257 GNUNET_DISK_pipe_close (h->helper_out);
258 h->helper_out = NULL;
259 h->fh_from_helper = NULL;
261 while (NULL != (sh = h->sh_head))
263 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
264 if (NULL != sh->cont)
265 sh->cont (sh->cont_cls, GNUNET_NO);
268 /* purge MST buffer */
270 (void) GNUNET_MST_from_buffer (h->mst, NULL, 0, GNUNET_YES, GNUNET_NO);
276 * Stop the helper process, we're closing down or had an error.
278 * @param h handle to the helper process
279 * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
280 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
283 stop_helper (struct GNUNET_HELPER_Handle *h, int soft_kill)
285 if (NULL != h->restart_task)
287 GNUNET_SCHEDULER_cancel (h->restart_task);
288 h->restart_task = NULL;
292 GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
293 GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (h));
299 * Restart the helper process.
301 * @param cls handle to the helper process
304 restart_task (void *cls);
308 * Read from the helper-process
310 * @param cls handle to the helper process
313 helper_read (void *cls)
315 struct GNUNET_HELPER_Handle *h = cls;
316 char buf[GNUNET_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
320 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
323 /* On read-error, restart the helper */
324 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
325 _ ("Error reading from `%s': %s\n"),
328 if (NULL != h->exp_cb)
330 h->exp_cb (h->cb_cls);
331 GNUNET_HELPER_stop (h, GNUNET_NO);
334 stop_helper (h, GNUNET_NO);
335 /* Restart the helper */
336 h->restart_task = GNUNET_SCHEDULER_add_delayed (
337 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
345 /* this happens if the helper is shut down via a
346 signal, so it is not a "hard" error */
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "Got 0 bytes from helper `%s' (EOF)\n",
350 if (NULL != h->exp_cb)
352 h->exp_cb (h->cb_cls);
353 GNUNET_HELPER_stop (h, GNUNET_NO);
356 stop_helper (h, GNUNET_NO);
357 /* Restart the helper */
358 h->restart_task = GNUNET_SCHEDULER_add_delayed (
359 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "Got %u bytes from helper `%s'\n",
369 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
374 GNUNET_MST_from_buffer (h->mst, buf, t, GNUNET_NO, GNUNET_NO))
376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
377 _ ("Failed to parse inbound message from helper `%s'\n"),
379 if (NULL != h->exp_cb)
381 h->exp_cb (h->cb_cls);
382 GNUNET_HELPER_stop (h, GNUNET_NO);
385 stop_helper (h, GNUNET_NO);
386 /* Restart the helper */
387 h->restart_task = GNUNET_SCHEDULER_add_delayed (
388 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
398 * Start the helper process.
400 * @param h handle to the helper process
403 start_helper (struct GNUNET_HELPER_Handle *h)
406 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
408 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
409 if ((h->helper_in == NULL) || (h->helper_out == NULL))
411 /* out of file descriptors? try again later... */
412 stop_helper (h, GNUNET_NO);
413 h->restart_task = GNUNET_SCHEDULER_add_delayed (
414 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Starting HELPER process `%s'\n",
424 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
426 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
427 h->helper_proc = GNUNET_OS_start_process_vap (h->with_control_pipe,
428 GNUNET_OS_INHERIT_STD_ERR,
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 (
439 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
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_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++)
510 h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1));
511 for (c = 0; NULL != binary_argv[c]; c++)
512 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
513 h->binary_argv[c] = NULL;
516 h->mst = GNUNET_MST_create (cb, h->cb_cls);
518 h->retry_back_off = 0;
525 * Free's the resources occupied by the helper handle
527 * @param h the helper handle to free
530 GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
533 struct GNUNET_HELPER_SendHandle *sh;
535 if (NULL != h->write_task)
537 GNUNET_SCHEDULER_cancel (h->write_task);
538 h->write_task = NULL;
540 GNUNET_assert (NULL == h->read_task);
541 GNUNET_assert (NULL == h->restart_task);
542 while (NULL != (sh = h->sh_head))
544 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
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, int soft_kill)
570 stop_helper (h, soft_kill);
571 GNUNET_HELPER_destroy (h);
576 * Write to the helper-process
578 * @param cls handle to the helper process
581 helper_write (void *cls)
583 struct GNUNET_HELPER_Handle *h = cls;
584 struct GNUNET_HELPER_SendHandle *sh;
588 h->write_task = NULL;
589 if (NULL == (sh = h->sh_head))
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Helper write had no work!\n");
592 return; /* how did this happen? */
594 buf = (const char *) sh->msg;
595 t = GNUNET_DISK_file_write (h->fh_to_helper,
597 ntohs (sh->msg->size) - sh->wpos);
600 /* On write-error, restart the helper */
601 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
602 _ ("Error writing to `%s': %s\n"),
605 if (NULL != h->exp_cb)
607 h->exp_cb (h->cb_cls);
608 GNUNET_HELPER_stop (h, GNUNET_NO);
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 "Stopping and restarting helper task!\n");
613 stop_helper (h, GNUNET_NO);
614 /* Restart the helper */
615 h->restart_task = GNUNET_SCHEDULER_add_delayed (
616 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, h->sh_tail, sh);
630 if (NULL != sh->cont)
631 sh->cont (sh->cont_cls, GNUNET_YES);
634 if (NULL != h->sh_head)
636 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
644 * Send an message to the helper.
646 * @param h helper to send message to
647 * @param msg message to send
648 * @param can_drop can the message be dropped if there is already one in the queue?
649 * @param cont continuation to run once the message is out (#GNUNET_OK on succees, #GNUNET_NO
650 * if the helper process died, #GNUNET_SYSERR during #GNUNET_HELPER_destroy).
651 * @param cont_cls closure for @a cont
652 * @return NULL if the message was dropped,
653 * otherwise handle to cancel *cont* (actual transmission may
656 struct GNUNET_HELPER_SendHandle *
657 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
658 const struct GNUNET_MessageHeader *msg,
660 GNUNET_HELPER_Continuation cont,
663 struct GNUNET_HELPER_SendHandle *sh;
666 if (NULL == h->fh_to_helper)
668 if ((GNUNET_YES == can_drop) && (NULL != h->sh_head))
670 mlen = ntohs (msg->size);
671 sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
672 sh->msg = (const struct GNUNET_MessageHeader *) &sh[1];
673 GNUNET_memcpy (&sh[1], msg, mlen);
676 sh->cont_cls = cont_cls;
677 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head, h->sh_tail, sh);
678 if (NULL == h->write_task)
680 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
689 * Cancel a #GNUNET_HELPER_send operation. If possible, transmitting the
690 * message is also aborted, but at least 'cont' won't be
693 * @param sh operation to cancel
696 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
698 struct GNUNET_HELPER_Handle *h = sh->h;
704 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
706 if (NULL == h->sh_head)
708 GNUNET_SCHEDULER_cancel (h->write_task);
709 h->write_task = NULL;
715 /* end of helper.c */