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 GNUNET_MessageHeaders on stdin/stdout
24 * @author Philipp Toelke
25 * @author Christian Grothoff
28 #include "gnunet_util_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_SERVER_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 GNUNET_SCHEDULER_TaskIdentifier read_task;
147 * Task to read from the helper.
149 GNUNET_SCHEDULER_TaskIdentifier write_task;
154 GNUNET_SCHEDULER_TaskIdentifier restart_task;
157 * Does the helper support the use of a control pipe for signalling?
159 int with_control_pipe;
165 * Stop the helper process, we're closing down or had an error.
167 * @param h handle to the helper process
168 * @param soft_kill if GNUNET_YES, signals termination by closing the helper's
169 * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
172 stop_helper (struct GNUNET_HELPER_Handle *h, int soft_kill)
174 struct GNUNET_HELPER_SendHandle *sh;
176 if (NULL != h->helper_proc)
178 if (GNUNET_YES == soft_kill)
180 /* soft-kill only possible with pipes */
181 GNUNET_assert (NULL != h->helper_in);
182 GNUNET_DISK_pipe_close (h->helper_in);
184 h->fh_to_helper = NULL;
187 GNUNET_break (0 == GNUNET_OS_process_kill (h->helper_proc, SIGTERM));
188 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (h->helper_proc));
189 GNUNET_OS_process_destroy (h->helper_proc);
190 h->helper_proc = NULL;
192 if (GNUNET_SCHEDULER_NO_TASK != h->restart_task)
194 GNUNET_SCHEDULER_cancel (h->restart_task);
195 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
197 if (GNUNET_SCHEDULER_NO_TASK != h->read_task)
199 GNUNET_SCHEDULER_cancel (h->read_task);
200 h->read_task = GNUNET_SCHEDULER_NO_TASK;
202 if (GNUNET_SCHEDULER_NO_TASK != h->write_task)
204 GNUNET_SCHEDULER_cancel (h->write_task);
205 h->write_task = GNUNET_SCHEDULER_NO_TASK;
207 if (NULL != h->helper_in)
209 GNUNET_DISK_pipe_close (h->helper_in);
211 h->fh_to_helper = NULL;
213 if (NULL != h->helper_out)
215 GNUNET_DISK_pipe_close (h->helper_out);
216 h->helper_out = NULL;
217 h->fh_from_helper = NULL;
219 while (NULL != (sh = h->sh_head))
221 GNUNET_CONTAINER_DLL_remove (h->sh_head,
224 if (NULL != sh->cont)
225 sh->cont (sh->cont_cls, GNUNET_NO);
228 /* purge MST buffer */
229 (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO);
234 * Restart the helper process.
236 * @param cls handle to the helper process
237 * @param tc scheduler context
240 restart_task (void *cls,
241 const struct GNUNET_SCHEDULER_TaskContext *tc);
245 * Read from the helper-process
247 * @param cls handle to the helper process
248 * @param tc scheduler context
251 helper_read (void *cls,
252 const struct GNUNET_SCHEDULER_TaskContext *tc)
254 struct GNUNET_HELPER_Handle *h = cls;
255 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
258 h->read_task = GNUNET_SCHEDULER_NO_TASK;
259 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
262 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
263 h->fh_from_helper, &helper_read, h);
266 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
269 /* On read-error, restart the helper */
270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
271 _("Error reading from `%s': %s\n"),
274 if (NULL != h->exp_cb)
276 h->exp_cb (h->cb_cls);
277 GNUNET_HELPER_stop (h);
280 stop_helper (h, GNUNET_NO);
281 /* Restart the helper */
283 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &restart_task, h);
288 /* this happens if the helper is shut down via a
289 signal, so it is not a "hard" error */
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Got 0 bytes from helper `%s' (EOF)\n",
293 if (NULL != h->exp_cb)
295 h->exp_cb (h->cb_cls);
296 GNUNET_HELPER_stop (h);
299 stop_helper (h, GNUNET_NO);
300 /* Restart the helper */
302 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Got %u bytes from helper `%s'\n",
310 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
311 h->fh_from_helper, &helper_read, h);
313 GNUNET_SERVER_mst_receive (h->mst, NULL, buf, t, GNUNET_NO, GNUNET_NO))
315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
316 _("Failed to parse inbound message from helper `%s'\n"),
318 if (NULL != h->exp_cb)
320 h->exp_cb (h->cb_cls);
321 GNUNET_HELPER_stop (h);
324 stop_helper (h, GNUNET_NO);
325 /* Restart the helper */
327 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
335 * Start the helper process.
337 * @param h handle to the helper process
340 start_helper (struct GNUNET_HELPER_Handle *h)
342 h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
343 h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
344 if ( (h->helper_in == NULL) || (h->helper_out == NULL))
346 /* out of file descriptors? try again later... */
347 stop_helper (h, GNUNET_NO);
349 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "Starting HELPER process `%s'\n",
357 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
359 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
361 GNUNET_OS_start_process_vap (h->with_control_pipe, GNUNET_OS_INHERIT_STD_ERR,
362 h->helper_in, h->helper_out,
365 if (NULL == h->helper_proc)
367 /* failed to start process? try again later... */
368 stop_helper (h, GNUNET_NO);
370 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
374 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
375 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
376 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
384 * Restart the helper process.
386 * @param cls handle to the helper process
387 * @param tc scheduler context
390 restart_task (void *cls,
391 const struct GNUNET_SCHEDULER_TaskContext *tc)
393 struct GNUNET_HELPER_Handle*h = cls;
395 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
401 * Starts a helper and begins reading from it. The helper process is
402 * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
403 * or when the exp_cb callback is not NULL.
405 * @param with_control_pipe does the helper support the use of a control pipe for signalling?
406 * @param binary_name name of the binary to run
407 * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
408 * argument must not be modified by the client for
409 * the lifetime of the helper handle)
410 * @param cb function to call if we get messages from the helper
411 * @param exp_cb the exception callback to call. Set this to NULL if the helper
412 * process has to be restarted automatically when it dies/crashes
413 * @param cb_cls closure for the above callback
414 * @return the new Handle, NULL on error
416 struct GNUNET_HELPER_Handle *
417 GNUNET_HELPER_start (int with_control_pipe,
418 const char *binary_name,
419 char *const binary_argv[],
420 GNUNET_SERVER_MessageTokenizerCallback cb,
421 GNUNET_HELPER_ExceptionCallback exp_cb,
424 struct GNUNET_HELPER_Handle *h;
427 h = GNUNET_malloc (sizeof (struct GNUNET_HELPER_Handle));
428 h->with_control_pipe = with_control_pipe;
429 /* Lookup in libexec path only if we are starting gnunet helpers */
430 if (NULL != strstr (binary_name, "gnunet"))
431 h->binary_name = GNUNET_OS_get_libexec_binary_path (binary_name);
433 h->binary_name = strdup (binary_name);
434 for (c = 0; NULL != binary_argv[c]; c++);
435 h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1));
436 for (c = 0; NULL != binary_argv[c]; c++)
437 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
438 h->binary_argv[c] = NULL;
440 h->mst = GNUNET_SERVER_mst_create (cb, h->cb_cls);
448 * @brief Kills the helper, closes the pipe and frees the h
450 * @param h h to helper to stop
451 * @param soft_kill if GNUNET_YES, signals termination by closing the helper's
452 * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
455 kill_helper (struct GNUNET_HELPER_Handle *h, int soft_kill)
457 struct GNUNET_HELPER_SendHandle *sh;
461 /* signal pending writes that we were stopped */
462 while (NULL != (sh = h->sh_head))
464 GNUNET_CONTAINER_DLL_remove (h->sh_head,
467 if (NULL != sh->cont)
468 sh->cont (sh->cont_cls, GNUNET_SYSERR);
471 stop_helper (h, soft_kill);
472 GNUNET_SERVER_mst_destroy (h->mst);
473 GNUNET_free (h->binary_name);
474 for (c = 0; h->binary_argv[c] != NULL; c++)
475 GNUNET_free (h->binary_argv[c]);
476 GNUNET_free (h->binary_argv);
482 * Kills the helper, closes the pipe and frees the handle
484 * @param h handle to helper to stop
487 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h)
489 kill_helper (h, GNUNET_NO);
494 * Kills the helper by closing its stdin (the helper is expected to catch the
495 * resulting SIGPIPE and shutdown), closes the pipe and frees the handle
497 * @param h handle to helper to stop
500 GNUNET_HELPER_soft_stop (struct GNUNET_HELPER_Handle *h)
502 kill_helper (h, GNUNET_YES);
507 * Write to the helper-process
509 * @param cls handle to the helper process
510 * @param tc scheduler context
513 helper_write (void *cls,
514 const struct GNUNET_SCHEDULER_TaskContext *tc)
516 struct GNUNET_HELPER_Handle *h = cls;
517 struct GNUNET_HELPER_SendHandle *sh;
521 h->write_task = GNUNET_SCHEDULER_NO_TASK;
522 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
525 h->write_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
526 h->fh_to_helper, &helper_write, h);
529 if (NULL == (sh = h->sh_head))
530 return; /* how did this happen? */
531 buf = (const char*) sh->msg;
532 t = GNUNET_DISK_file_write (h->fh_to_helper, &buf[sh->wpos], ntohs (sh->msg->size) - sh->wpos);
535 /* On write-error, restart the helper */
536 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
537 _("Error writing to `%s': %s\n"),
540 if (NULL != h->exp_cb)
542 h->exp_cb (h->cb_cls);
543 GNUNET_HELPER_stop (h);
546 stop_helper (h, GNUNET_NO);
547 /* Restart the helper */
549 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
554 if (sh->wpos == ntohs (sh->msg->size))
556 GNUNET_CONTAINER_DLL_remove (h->sh_head,
559 if (NULL != sh->cont)
560 sh->cont (sh->cont_cls, GNUNET_YES);
563 if (NULL != h->sh_head)
564 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
572 * Send an message to the helper.
574 * @param h helper to send message to
575 * @param msg message to send
576 * @param can_drop can the message be dropped if there is already one in the queue?
577 * @param cont continuation to run once the message is out (PREREQ_DONE on succees, CANCEL
578 * if the helper process died, NULL during GNUNET_HELPER_stop).
579 * @param cont_cls closure for 'cont'
580 * @return NULL if the message was dropped,
581 * otherwise handle to cancel *cont* (actual transmission may
584 struct GNUNET_HELPER_SendHandle *
585 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
586 const struct GNUNET_MessageHeader *msg,
588 GNUNET_HELPER_Continuation cont,
591 struct GNUNET_HELPER_SendHandle *sh;
594 if (NULL == h->fh_to_helper)
596 if ( (GNUNET_YES == can_drop) &&
597 (NULL != h->sh_head) )
599 mlen = ntohs (msg->size);
600 sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
601 sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
602 memcpy (&sh[1], msg, mlen);
605 sh->cont_cls = cont_cls;
606 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head,
609 if (GNUNET_SCHEDULER_NO_TASK == h->write_task)
610 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
619 * Cancel a 'send' operation. If possible, transmitting the
620 * message is also aborted, but at least 'cont' won't be
623 * @param sh operation to cancel
626 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
628 struct GNUNET_HELPER_Handle *h = sh->h;
634 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
635 if (NULL == h->sh_head)
637 GNUNET_SCHEDULER_cancel (h->write_task);
638 h->write_task = GNUNET_SCHEDULER_NO_TASK;
645 /* end of helper.c */