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, int soft_kill)
177 struct GNUNET_HELPER_SendHandle *sh;
180 while (NULL != (sh = h->sh_head))
182 GNUNET_CONTAINER_DLL_remove (h->sh_head,
185 if (NULL != sh->cont)
186 sh->cont (sh->cont_cls, GNUNET_NO);
189 if (GNUNET_SCHEDULER_NO_TASK != h->restart_task)
191 GNUNET_SCHEDULER_cancel (h->restart_task);
192 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
194 if (NULL == h->helper_proc)
195 return GNUNET_SYSERR;
196 if (GNUNET_YES == soft_kill)
198 /* soft-kill only possible with pipes */
199 GNUNET_assert (NULL != h->helper_in);
200 ret = GNUNET_DISK_pipe_close (h->helper_in);
202 h->fh_to_helper = NULL;
205 if (0 != GNUNET_OS_process_kill (h->helper_proc, SIGTERM))
206 return GNUNET_SYSERR;
212 * Reap the helper process. This call is blocking(!). The helper process
213 * should either be sent a termination signal before or should be dead before
214 * calling this function
216 * @param h the helper handle
217 * @return GNUNET_OK on success; GNUNET_SYSERR on error
220 GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
222 struct GNUNET_HELPER_SendHandle *sh;
226 if (NULL != h->helper_proc)
228 ret = GNUNET_OS_process_wait (h->helper_proc);
229 GNUNET_OS_process_destroy (h->helper_proc);
230 h->helper_proc = NULL;
232 if (GNUNET_SCHEDULER_NO_TASK != h->read_task)
234 GNUNET_SCHEDULER_cancel (h->read_task);
235 h->read_task = GNUNET_SCHEDULER_NO_TASK;
237 if (GNUNET_SCHEDULER_NO_TASK != h->write_task)
239 GNUNET_SCHEDULER_cancel (h->write_task);
240 h->write_task = GNUNET_SCHEDULER_NO_TASK;
242 if (NULL != h->helper_in)
244 GNUNET_DISK_pipe_close (h->helper_in);
246 h->fh_to_helper = NULL;
248 if (NULL != h->helper_out)
250 GNUNET_DISK_pipe_close (h->helper_out);
251 h->helper_out = NULL;
252 h->fh_from_helper = NULL;
254 while (NULL != (sh = h->sh_head))
256 GNUNET_CONTAINER_DLL_remove (h->sh_head,
259 if (NULL != sh->cont)
260 sh->cont (sh->cont_cls, GNUNET_NO);
263 /* purge MST buffer */
264 (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO);
270 * Stop the helper process, we're closing down or had an error.
272 * @param h handle to the helper process
273 * @param soft_kill if GNUNET_YES, signals termination by closing the helper's
274 * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
277 stop_helper (struct GNUNET_HELPER_Handle *h, int soft_kill)
279 GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
280 GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (h));
285 * Restart the helper process.
287 * @param cls handle to the helper process
288 * @param tc scheduler context
291 restart_task (void *cls,
292 const struct GNUNET_SCHEDULER_TaskContext *tc);
296 * Read from the helper-process
298 * @param cls handle to the helper process
299 * @param tc scheduler context
302 helper_read (void *cls,
303 const struct GNUNET_SCHEDULER_TaskContext *tc)
305 struct GNUNET_HELPER_Handle *h = cls;
306 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
309 h->read_task = GNUNET_SCHEDULER_NO_TASK;
310 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
313 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
314 h->fh_from_helper, &helper_read, h);
317 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
320 /* On read-error, restart the helper */
321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
322 _("Error reading from `%s': %s\n"),
325 if (NULL != h->exp_cb)
327 h->exp_cb (h->cb_cls);
328 GNUNET_HELPER_stop (h, GNUNET_NO);
331 stop_helper (h, GNUNET_NO);
332 /* Restart the helper */
334 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &restart_task, h);
339 /* this happens if the helper is shut down via a
340 signal, so it is not a "hard" error */
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Got 0 bytes from helper `%s' (EOF)\n",
344 if (NULL != h->exp_cb)
346 h->exp_cb (h->cb_cls);
347 GNUNET_HELPER_stop (h, GNUNET_NO);
350 stop_helper (h, GNUNET_NO);
351 /* Restart the helper */
353 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "Got %u bytes from helper `%s'\n",
361 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
362 h->fh_from_helper, &helper_read, h);
364 GNUNET_SERVER_mst_receive (h->mst, NULL, buf, t, GNUNET_NO, GNUNET_NO))
366 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
367 _("Failed to parse inbound message from helper `%s'\n"),
369 if (NULL != h->exp_cb)
371 h->exp_cb (h->cb_cls);
372 GNUNET_HELPER_stop (h, GNUNET_NO);
375 stop_helper (h, GNUNET_NO);
376 /* Restart the helper */
378 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
386 * Start the helper process.
388 * @param h handle to the helper process
391 start_helper (struct GNUNET_HELPER_Handle *h)
393 h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
394 h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
395 if ( (h->helper_in == NULL) || (h->helper_out == NULL))
397 /* out of file descriptors? try again later... */
398 stop_helper (h, GNUNET_NO);
400 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "Starting HELPER process `%s'\n",
408 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
410 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
412 GNUNET_OS_start_process_vap (h->with_control_pipe, GNUNET_OS_INHERIT_STD_ERR,
413 h->helper_in, h->helper_out,
416 if (NULL == h->helper_proc)
418 /* failed to start process? try again later... */
419 stop_helper (h, GNUNET_NO);
421 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
425 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
426 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
427 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
435 * Restart the helper process.
437 * @param cls handle to the helper process
438 * @param tc scheduler context
441 restart_task (void *cls,
442 const struct GNUNET_SCHEDULER_TaskContext *tc)
444 struct GNUNET_HELPER_Handle*h = cls;
446 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
452 * Starts a helper and begins reading from it. The helper process is
453 * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
454 * or when the exp_cb callback is not NULL.
456 * @param with_control_pipe does the helper support the use of a control pipe for signalling?
457 * @param binary_name name of the binary to run
458 * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
459 * argument must not be modified by the client for
460 * the lifetime of the helper handle)
461 * @param cb function to call if we get messages from the helper
462 * @param exp_cb the exception callback to call. Set this to NULL if the helper
463 * process has to be restarted automatically when it dies/crashes
464 * @param cb_cls closure for the above callback
465 * @return the new Handle, NULL on error
467 struct GNUNET_HELPER_Handle *
468 GNUNET_HELPER_start (int with_control_pipe,
469 const char *binary_name,
470 char *const binary_argv[],
471 GNUNET_SERVER_MessageTokenizerCallback cb,
472 GNUNET_HELPER_ExceptionCallback exp_cb,
475 struct GNUNET_HELPER_Handle *h;
478 h = GNUNET_malloc (sizeof (struct GNUNET_HELPER_Handle));
479 h->with_control_pipe = with_control_pipe;
480 /* Lookup in libexec path only if we are starting gnunet helpers */
481 if (NULL != strstr (binary_name, "gnunet"))
482 h->binary_name = GNUNET_OS_get_libexec_binary_path (binary_name);
484 h->binary_name = strdup (binary_name);
485 for (c = 0; NULL != binary_argv[c]; c++);
486 h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1));
487 for (c = 0; NULL != binary_argv[c]; c++)
488 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
489 h->binary_argv[c] = NULL;
491 h->mst = GNUNET_SERVER_mst_create (cb, h->cb_cls);
499 * Free's the resources occupied by the helper handle
501 * @param h the helper handle to free
504 GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
508 GNUNET_SERVER_mst_destroy (h->mst);
509 GNUNET_free (h->binary_name);
510 for (c = 0; h->binary_argv[c] != NULL; c++)
511 GNUNET_free (h->binary_argv[c]);
512 GNUNET_free (h->binary_argv);
518 * Kills the helper, closes the pipe and frees the handle
520 * @param h handle to helper to stop
521 * @param soft_kill if GNUNET_YES, signals termination by closing the helper's
522 * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
525 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, int soft_kill)
528 stop_helper (h, soft_kill);
529 GNUNET_HELPER_destroy (h);
534 * Write to the helper-process
536 * @param cls handle to the helper process
537 * @param tc scheduler context
540 helper_write (void *cls,
541 const struct GNUNET_SCHEDULER_TaskContext *tc)
543 struct GNUNET_HELPER_Handle *h = cls;
544 struct GNUNET_HELPER_SendHandle *sh;
548 h->write_task = GNUNET_SCHEDULER_NO_TASK;
549 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
552 h->write_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
553 h->fh_to_helper, &helper_write, h);
556 if (NULL == (sh = h->sh_head))
557 return; /* how did this happen? */
558 buf = (const char*) sh->msg;
559 t = GNUNET_DISK_file_write (h->fh_to_helper, &buf[sh->wpos], ntohs (sh->msg->size) - sh->wpos);
562 /* On write-error, restart the helper */
563 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
564 _("Error writing to `%s': %s\n"),
567 if (NULL != h->exp_cb)
569 h->exp_cb (h->cb_cls);
570 GNUNET_HELPER_stop (h, GNUNET_NO);
573 stop_helper (h, GNUNET_NO);
574 /* Restart the helper */
576 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
581 if (sh->wpos == ntohs (sh->msg->size))
583 GNUNET_CONTAINER_DLL_remove (h->sh_head,
586 if (NULL != sh->cont)
587 sh->cont (sh->cont_cls, GNUNET_YES);
590 if (NULL != h->sh_head)
591 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
599 * Send an message to the helper.
601 * @param h helper to send message to
602 * @param msg message to send
603 * @param can_drop can the message be dropped if there is already one in the queue?
604 * @param cont continuation to run once the message is out (PREREQ_DONE on succees, CANCEL
605 * if the helper process died, NULL during GNUNET_HELPER_stop).
606 * @param cont_cls closure for 'cont'
607 * @return NULL if the message was dropped,
608 * otherwise handle to cancel *cont* (actual transmission may
611 struct GNUNET_HELPER_SendHandle *
612 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
613 const struct GNUNET_MessageHeader *msg,
615 GNUNET_HELPER_Continuation cont,
618 struct GNUNET_HELPER_SendHandle *sh;
621 if (NULL == h->fh_to_helper)
623 if ( (GNUNET_YES == can_drop) &&
624 (NULL != h->sh_head) )
626 mlen = ntohs (msg->size);
627 sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
628 sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
629 memcpy (&sh[1], msg, mlen);
632 sh->cont_cls = cont_cls;
633 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head,
636 if (GNUNET_SCHEDULER_NO_TASK == h->write_task)
637 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
646 * Cancel a 'send' operation. If possible, transmitting the
647 * message is also aborted, but at least 'cont' won't be
650 * @param sh operation to cancel
653 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
655 struct GNUNET_HELPER_Handle *h = sh->h;
661 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
662 if (NULL == h->sh_head)
664 GNUNET_SCHEDULER_cancel (h->write_task);
665 h->write_task = GNUNET_SCHEDULER_NO_TASK;
672 /* end of helper.c */