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
170 stop_helper (struct GNUNET_HELPER_Handle *h)
172 struct GNUNET_HELPER_SendHandle *sh;
174 if (NULL != h->helper_proc)
176 GNUNET_break (0 == GNUNET_OS_process_kill (h->helper_proc, SIGTERM));
177 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (h->helper_proc));
178 GNUNET_OS_process_destroy (h->helper_proc);
179 h->helper_proc = NULL;
181 if (GNUNET_SCHEDULER_NO_TASK != h->restart_task)
183 GNUNET_SCHEDULER_cancel (h->restart_task);
184 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
186 if (GNUNET_SCHEDULER_NO_TASK != h->read_task)
188 GNUNET_SCHEDULER_cancel (h->read_task);
189 h->read_task = GNUNET_SCHEDULER_NO_TASK;
191 if (GNUNET_SCHEDULER_NO_TASK != h->write_task)
193 GNUNET_SCHEDULER_cancel (h->write_task);
194 h->write_task = GNUNET_SCHEDULER_NO_TASK;
196 if (NULL != h->helper_in)
198 GNUNET_DISK_pipe_close (h->helper_in);
200 h->fh_to_helper = NULL;
202 if (NULL != h->helper_out)
204 GNUNET_DISK_pipe_close (h->helper_out);
205 h->helper_out = NULL;
206 h->fh_from_helper = NULL;
208 while (NULL != (sh = h->sh_head))
210 GNUNET_CONTAINER_DLL_remove (h->sh_head,
213 if (NULL != sh->cont)
214 sh->cont (sh->cont_cls, GNUNET_NO);
217 /* purge MST buffer */
218 (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO);
223 * Restart the helper process.
225 * @param cls handle to the helper process
226 * @param tc scheduler context
229 restart_task (void *cls,
230 const struct GNUNET_SCHEDULER_TaskContext *tc);
234 * Read from the helper-process
236 * @param cls handle to the helper process
237 * @param tc scheduler context
240 helper_read (void *cls,
241 const struct GNUNET_SCHEDULER_TaskContext *tc)
243 struct GNUNET_HELPER_Handle *h = cls;
244 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
247 h->read_task = GNUNET_SCHEDULER_NO_TASK;
248 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
251 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
252 h->fh_from_helper, &helper_read, h);
255 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
258 /* On read-error, restart the helper */
259 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
260 _("Error reading from `%s': %s\n"),
263 if (NULL != h->exp_cb)
265 h->exp_cb (h->cb_cls);
266 GNUNET_HELPER_stop (h);
270 /* Restart the helper */
272 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &restart_task, h);
277 /* this happens if the helper is shut down via a
278 signal, so it is not a "hard" error */
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Got 0 bytes from helper `%s' (EOF)\n",
282 if (NULL != h->exp_cb)
284 h->exp_cb (h->cb_cls);
285 GNUNET_HELPER_stop (h);
289 /* Restart the helper */
291 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Got %u bytes from helper `%s'\n",
299 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
300 h->fh_from_helper, &helper_read, h);
302 GNUNET_SERVER_mst_receive (h->mst, NULL, buf, t, GNUNET_NO, GNUNET_NO))
304 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
305 _("Failed to parse inbound message from helper `%s'\n"),
307 if (NULL != h->exp_cb)
309 h->exp_cb (h->cb_cls);
310 GNUNET_HELPER_stop (h);
314 /* Restart the helper */
316 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
324 * Start the helper process.
326 * @param h handle to the helper process
329 start_helper (struct GNUNET_HELPER_Handle *h)
331 h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
332 h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
333 if ( (h->helper_in == NULL) || (h->helper_out == NULL))
335 /* out of file descriptors? try again later... */
338 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Starting HELPER process `%s'\n",
346 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
348 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
350 GNUNET_OS_start_process_vap (h->with_control_pipe, GNUNET_OS_INHERIT_STD_ERR,
351 h->helper_in, h->helper_out,
354 if (NULL == h->helper_proc)
356 /* failed to start process? try again later... */
359 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
363 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
364 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
365 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
373 * Restart the helper process.
375 * @param cls handle to the helper process
376 * @param tc scheduler context
379 restart_task (void *cls,
380 const struct GNUNET_SCHEDULER_TaskContext *tc)
382 struct GNUNET_HELPER_Handle*h = cls;
384 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
390 * Starts a helper and begins reading from it. The helper process is
391 * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
392 * or when the exp_cb callback is not NULL.
394 * @param with_control_pipe does the helper support the use of a control pipe for signalling?
395 * @param binary_name name of the binary to run
396 * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
397 * argument must not be modified by the client for
398 * the lifetime of the helper handle)
399 * @param cb function to call if we get messages from the helper
400 * @param exp_cb the exception callback to call. Set this to NULL if the helper
401 * process has to be restarted automatically when it dies/crashes
402 * @param cb_cls closure for the above callback
403 * @return the new Handle, NULL on error
405 struct GNUNET_HELPER_Handle *
406 GNUNET_HELPER_start (int with_control_pipe,
407 const char *binary_name,
408 char *const binary_argv[],
409 GNUNET_SERVER_MessageTokenizerCallback cb,
410 GNUNET_HELPER_ExceptionCallback exp_cb,
413 struct GNUNET_HELPER_Handle *h;
416 h = GNUNET_malloc (sizeof (struct GNUNET_HELPER_Handle));
417 h->with_control_pipe = with_control_pipe;
418 /* Lookup in libexec path only if we are starting gnunet helpers */
419 if (NULL != strstr (binary_name, "gnunet"))
420 h->binary_name = GNUNET_OS_get_libexec_binary_path (binary_name);
422 h->binary_name = strdup (binary_name);
423 for (c = 0; NULL != binary_argv[c]; c++);
424 h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1));
425 for (c = 0; NULL != binary_argv[c]; c++)
426 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
427 h->binary_argv[c] = NULL;
429 h->mst = GNUNET_SERVER_mst_create (cb, h->cb_cls);
437 * @brief Kills the helper, closes the pipe and frees the h
439 * @param h h to helper to stop
442 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h)
444 struct GNUNET_HELPER_SendHandle *sh;
448 /* signal pending writes that we were stopped */
449 while (NULL != (sh = h->sh_head))
451 GNUNET_CONTAINER_DLL_remove (h->sh_head,
454 if (NULL != sh->cont)
455 sh->cont (sh->cont_cls, GNUNET_SYSERR);
459 GNUNET_SERVER_mst_destroy (h->mst);
460 GNUNET_free (h->binary_name);
461 for (c = 0; h->binary_argv[c] != NULL; c++)
462 GNUNET_free (h->binary_argv[c]);
463 GNUNET_free (h->binary_argv);
469 * Write to the helper-process
471 * @param cls handle to the helper process
472 * @param tc scheduler context
475 helper_write (void *cls,
476 const struct GNUNET_SCHEDULER_TaskContext *tc)
478 struct GNUNET_HELPER_Handle *h = cls;
479 struct GNUNET_HELPER_SendHandle *sh;
483 h->write_task = GNUNET_SCHEDULER_NO_TASK;
484 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
487 h->write_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
488 h->fh_to_helper, &helper_write, h);
491 if (NULL == (sh = h->sh_head))
492 return; /* how did this happen? */
493 buf = (const char*) sh->msg;
494 t = GNUNET_DISK_file_write (h->fh_to_helper, &buf[sh->wpos], ntohs (sh->msg->size) - sh->wpos);
497 /* On write-error, restart the helper */
498 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
499 _("Error writing to `%s': %s\n"),
502 if (NULL != h->exp_cb)
504 h->exp_cb (h->cb_cls);
505 GNUNET_HELPER_stop (h);
509 /* Restart the helper */
511 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
516 if (sh->wpos == ntohs (sh->msg->size))
518 GNUNET_CONTAINER_DLL_remove (h->sh_head,
521 if (NULL != sh->cont)
522 sh->cont (sh->cont_cls, GNUNET_YES);
525 if (NULL != h->sh_head)
526 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
534 * Send an message to the helper.
536 * @param h helper to send message to
537 * @param msg message to send
538 * @param can_drop can the message be dropped if there is already one in the queue?
539 * @param cont continuation to run once the message is out (PREREQ_DONE on succees, CANCEL
540 * if the helper process died, NULL during GNUNET_HELPER_stop).
541 * @param cont_cls closure for 'cont'
542 * @return NULL if the message was dropped,
543 * otherwise handle to cancel *cont* (actual transmission may
546 struct GNUNET_HELPER_SendHandle *
547 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
548 const struct GNUNET_MessageHeader *msg,
550 GNUNET_HELPER_Continuation cont,
553 struct GNUNET_HELPER_SendHandle *sh;
556 if (NULL == h->fh_to_helper)
558 if ( (GNUNET_YES == can_drop) &&
559 (NULL != h->sh_head) )
561 mlen = ntohs (msg->size);
562 sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
563 sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
564 memcpy (&sh[1], msg, mlen);
567 sh->cont_cls = cont_cls;
568 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head,
571 if (GNUNET_SCHEDULER_NO_TASK == h->write_task)
572 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
581 * Cancel a 'send' operation. If possible, transmitting the
582 * message is also aborted, but at least 'cont' won't be
585 * @param sh operation to cancel
588 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
590 struct GNUNET_HELPER_Handle *h = sh->h;
596 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
597 if (NULL == h->sh_head)
599 GNUNET_SCHEDULER_cancel (h->write_task);
600 h->write_task = GNUNET_SCHEDULER_NO_TASK;
607 /* end of helper.c */