2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier read_task;
148 * Task to read from the helper.
150 GNUNET_SCHEDULER_TaskIdentifier write_task;
155 GNUNET_SCHEDULER_TaskIdentifier restart_task;
158 * Does the helper support the use of a control pipe for signalling?
160 int with_control_pipe;
166 * Sends termination signal to the helper process. The helper process is not
167 * reaped; call GNUNET_HELPER_wait() for reaping the dead helper process.
169 * @param h the helper handle
170 * @param soft_kill if GNUNET_YES, signals termination by closing the helper's
171 * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
172 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
175 GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h,
178 struct GNUNET_HELPER_SendHandle *sh;
181 while (NULL != (sh = h->sh_head))
183 GNUNET_CONTAINER_DLL_remove (h->sh_head,
186 if (NULL != sh->cont)
187 sh->cont (sh->cont_cls, GNUNET_NO);
190 if (GNUNET_SCHEDULER_NO_TASK != h->restart_task)
192 GNUNET_SCHEDULER_cancel (h->restart_task);
193 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
195 if (GNUNET_SCHEDULER_NO_TASK != h->read_task)
197 GNUNET_SCHEDULER_cancel (h->read_task);
198 h->read_task = GNUNET_SCHEDULER_NO_TASK;
200 if (NULL == h->helper_proc)
201 return GNUNET_SYSERR;
202 if (GNUNET_YES == soft_kill)
204 /* soft-kill only possible with pipes */
205 GNUNET_assert (NULL != h->helper_in);
206 ret = GNUNET_DISK_pipe_close (h->helper_in);
208 h->fh_to_helper = NULL;
211 if (0 != GNUNET_OS_process_kill (h->helper_proc, GNUNET_TERM_SIG))
212 return GNUNET_SYSERR;
218 * Reap the helper process. This call is blocking(!). The helper process
219 * should either be sent a termination signal before or should be dead before
220 * calling this function
222 * @param h the helper handle
223 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
226 GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
228 struct GNUNET_HELPER_SendHandle *sh;
232 if (NULL != h->helper_proc)
234 ret = GNUNET_OS_process_wait (h->helper_proc);
235 GNUNET_OS_process_destroy (h->helper_proc);
236 h->helper_proc = NULL;
238 if (GNUNET_SCHEDULER_NO_TASK != h->read_task)
240 GNUNET_SCHEDULER_cancel (h->read_task);
241 h->read_task = GNUNET_SCHEDULER_NO_TASK;
243 if (GNUNET_SCHEDULER_NO_TASK != h->write_task)
245 GNUNET_SCHEDULER_cancel (h->write_task);
246 h->write_task = GNUNET_SCHEDULER_NO_TASK;
248 if (NULL != h->helper_in)
250 GNUNET_DISK_pipe_close (h->helper_in);
252 h->fh_to_helper = NULL;
254 if (NULL != h->helper_out)
256 GNUNET_DISK_pipe_close (h->helper_out);
257 h->helper_out = NULL;
258 h->fh_from_helper = NULL;
260 while (NULL != (sh = h->sh_head))
262 GNUNET_CONTAINER_DLL_remove (h->sh_head,
265 if (NULL != sh->cont)
266 sh->cont (sh->cont_cls, GNUNET_NO);
269 /* purge MST buffer */
271 (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO);
277 * Stop the helper process, we're closing down or had an error.
279 * @param h handle to the helper process
280 * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
281 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
284 stop_helper (struct GNUNET_HELPER_Handle *h,
287 if (GNUNET_SCHEDULER_NO_TASK != h->restart_task)
289 GNUNET_SCHEDULER_cancel (h->restart_task);
290 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
294 GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
295 GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (h));
301 * Restart the helper process.
303 * @param cls handle to the helper process
304 * @param tc scheduler context
307 restart_task (void *cls,
308 const struct GNUNET_SCHEDULER_TaskContext *tc);
312 * Read from the helper-process
314 * @param cls handle to the helper process
315 * @param tc scheduler context
318 helper_read (void *cls,
319 const struct GNUNET_SCHEDULER_TaskContext *tc)
321 struct GNUNET_HELPER_Handle *h = cls;
322 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
325 h->read_task = GNUNET_SCHEDULER_NO_TASK;
326 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
329 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
330 h->fh_from_helper, &helper_read, h);
333 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
336 /* On read-error, restart the helper */
337 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
338 _("Error reading from `%s': %s\n"),
341 if (NULL != h->exp_cb)
343 h->exp_cb (h->cb_cls);
344 GNUNET_HELPER_stop (h, GNUNET_NO);
347 stop_helper (h, GNUNET_NO);
348 /* Restart the helper */
350 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &restart_task, h);
355 /* this happens if the helper is shut down via a
356 signal, so it is not a "hard" error */
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "Got 0 bytes from helper `%s' (EOF)\n",
360 if (NULL != h->exp_cb)
362 h->exp_cb (h->cb_cls);
363 GNUNET_HELPER_stop (h, GNUNET_NO);
366 stop_helper (h, GNUNET_NO);
367 /* Restart the helper */
369 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 "Got %u bytes from helper `%s'\n",
377 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
378 h->fh_from_helper, &helper_read, h);
380 GNUNET_SERVER_mst_receive (h->mst, NULL, buf, t, GNUNET_NO, GNUNET_NO))
382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
383 _("Failed to parse inbound message from helper `%s'\n"),
385 if (NULL != h->exp_cb)
387 h->exp_cb (h->cb_cls);
388 GNUNET_HELPER_stop (h, GNUNET_NO);
391 stop_helper (h, GNUNET_NO);
392 /* Restart the helper */
394 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
402 * Start the helper process.
404 * @param h handle to the helper process
407 start_helper (struct GNUNET_HELPER_Handle *h)
409 h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
410 h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
411 if ( (h->helper_in == NULL) || (h->helper_out == NULL))
413 /* out of file descriptors? try again later... */
414 stop_helper (h, GNUNET_NO);
416 GNUNET_SCHEDULER_add_delayed (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);
428 GNUNET_OS_start_process_vap (h->with_control_pipe, GNUNET_OS_INHERIT_STD_ERR,
429 h->helper_in, h->helper_out, NULL,
432 if (NULL == h->helper_proc)
434 /* failed to start process? try again later... */
435 stop_helper (h, GNUNET_NO);
437 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
441 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
442 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
444 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
452 * Restart the helper process.
454 * @param cls handle to the helper process
455 * @param tc scheduler context
458 restart_task (void *cls,
459 const struct GNUNET_SCHEDULER_TaskContext *tc)
461 struct GNUNET_HELPER_Handle*h = cls;
463 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
469 * Starts a helper and begins reading from it. The helper process is
470 * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
471 * or when the exp_cb callback is not NULL.
473 * @param with_control_pipe does the helper support the use of a control pipe for signalling?
474 * @param binary_name name of the binary to run
475 * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
476 * argument must not be modified by the client for
477 * the lifetime of the helper handle)
478 * @param cb function to call if we get messages from the helper
479 * @param exp_cb the exception callback to call. Set this to NULL if the helper
480 * process has to be restarted automatically when it dies/crashes
481 * @param cb_cls closure for the above callback
482 * @return the new Handle, NULL on error
484 struct GNUNET_HELPER_Handle *
485 GNUNET_HELPER_start (int with_control_pipe,
486 const char *binary_name,
487 char *const binary_argv[],
488 GNUNET_SERVER_MessageTokenizerCallback cb,
489 GNUNET_HELPER_ExceptionCallback exp_cb,
492 struct GNUNET_HELPER_Handle *h;
495 h = GNUNET_new (struct GNUNET_HELPER_Handle);
496 h->with_control_pipe = with_control_pipe;
497 /* Lookup in libexec path only if we are starting gnunet helpers */
498 if (NULL != strstr (binary_name, "gnunet"))
499 h->binary_name = GNUNET_OS_get_libexec_binary_path (binary_name);
501 h->binary_name = strdup (binary_name);
502 for (c = 0; NULL != binary_argv[c]; c++);
503 h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1));
504 for (c = 0; NULL != binary_argv[c]; c++)
505 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
506 h->binary_argv[c] = NULL;
509 h->mst = GNUNET_SERVER_mst_create (cb, h->cb_cls);
517 * Free's the resources occupied by the helper handle
519 * @param h the helper handle to free
522 GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
525 struct GNUNET_HELPER_SendHandle *sh;
527 if (GNUNET_SCHEDULER_NO_TASK != h->write_task)
529 GNUNET_SCHEDULER_cancel (h->write_task);
530 h->write_task = GNUNET_SCHEDULER_NO_TASK;
532 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->read_task);
533 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->restart_task);
534 while (NULL != (sh = h->sh_head))
536 GNUNET_CONTAINER_DLL_remove (h->sh_head,
539 if (NULL != sh->cont)
540 sh->cont (sh->cont_cls, GNUNET_SYSERR);
544 GNUNET_SERVER_mst_destroy (h->mst);
545 GNUNET_free (h->binary_name);
546 for (c = 0; h->binary_argv[c] != NULL; c++)
547 GNUNET_free (h->binary_argv[c]);
548 GNUNET_free (h->binary_argv);
554 * Kills the helper, closes the pipe and frees the handle
556 * @param h handle to helper to stop
557 * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
558 * stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
561 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h,
565 stop_helper (h, soft_kill);
566 GNUNET_HELPER_destroy (h);
571 * Write to the helper-process
573 * @param cls handle to the helper process
574 * @param tc scheduler context
577 helper_write (void *cls,
578 const struct GNUNET_SCHEDULER_TaskContext *tc)
580 struct GNUNET_HELPER_Handle *h = cls;
581 struct GNUNET_HELPER_SendHandle *sh;
585 h->write_task = GNUNET_SCHEDULER_NO_TASK;
586 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "Helper write triggered during shutdown, retrying\n");
591 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
592 h->fh_to_helper, &helper_write, h);
595 if (NULL == (sh = h->sh_head))
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Helper write had no work!\n");
599 return; /* how did this happen? */
601 buf = (const char*) sh->msg;
602 t = GNUNET_DISK_file_write (h->fh_to_helper,
604 ntohs (sh->msg->size) - sh->wpos);
607 /* On write-error, restart the helper */
608 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
609 _("Error writing to `%s': %s\n"),
612 if (NULL != h->exp_cb)
614 h->exp_cb (h->cb_cls);
615 GNUNET_HELPER_stop (h, GNUNET_NO);
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "Stopping and restarting helper task!\n");
620 stop_helper (h, GNUNET_NO);
621 /* Restart the helper */
623 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 "Transmitted %u bytes to %s\n",
632 if (sh->wpos == ntohs (sh->msg->size))
634 GNUNET_CONTAINER_DLL_remove (h->sh_head,
637 if (NULL != sh->cont)
638 sh->cont (sh->cont_cls, GNUNET_YES);
641 if (NULL != h->sh_head)
642 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
650 * Send an message to the helper.
652 * @param h helper to send message to
653 * @param msg message to send
654 * @param can_drop can the message be dropped if there is already one in the queue?
655 * @param cont continuation to run once the message is out (#GNUNET_OK on succees, #GNUNET_NO
656 * if the helper process died, #GNUNET_SYSERR during #GNUNET_HELPER_destroy).
657 * @param cont_cls closure for @a cont
658 * @return NULL if the message was dropped,
659 * otherwise handle to cancel *cont* (actual transmission may
662 struct GNUNET_HELPER_SendHandle *
663 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
664 const struct GNUNET_MessageHeader *msg,
666 GNUNET_HELPER_Continuation cont,
669 struct GNUNET_HELPER_SendHandle *sh;
672 if (NULL == h->fh_to_helper)
674 if ( (GNUNET_YES == can_drop) &&
675 (NULL != h->sh_head) )
677 mlen = ntohs (msg->size);
678 sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
679 sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
680 memcpy (&sh[1], msg, mlen);
683 sh->cont_cls = cont_cls;
684 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head,
687 if (GNUNET_SCHEDULER_NO_TASK == h->write_task)
688 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
697 * Cancel a #GNUNET_HELPER_send operation. If possible, transmitting the
698 * message is also aborted, but at least 'cont' won't be
701 * @param sh operation to cancel
704 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
706 struct GNUNET_HELPER_Handle *h = sh->h;
712 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
714 if (NULL == h->sh_head)
716 GNUNET_SCHEDULER_cancel (h->write_task);
717 h->write_task = GNUNET_SCHEDULER_NO_TASK;
723 /* end of helper.c */