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 * First message queued for transmission to helper.
114 struct GNUNET_HELPER_SendHandle *sh_head;
117 * Last message queued for transmission to helper.
119 struct GNUNET_HELPER_SendHandle *sh_tail;
124 const char *binary_name;
127 * NULL-terminated list of command-line arguments.
129 char *const *binary_argv;
132 * Task to read from the helper.
134 GNUNET_SCHEDULER_TaskIdentifier read_task;
137 * Task to read from the helper.
139 GNUNET_SCHEDULER_TaskIdentifier write_task;
144 GNUNET_SCHEDULER_TaskIdentifier restart_task;
149 * Stop the helper process, we're closing down or had an error.
151 * @param h handle to the helper process
154 stop_helper (struct GNUNET_HELPER_Handle *h)
156 struct GNUNET_HELPER_SendHandle *sh;
158 if (NULL != h->helper_proc)
160 GNUNET_break (0 == GNUNET_OS_process_kill (h->helper_proc, SIGTERM));
161 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (h->helper_proc));
162 GNUNET_OS_process_destroy (h->helper_proc);
163 h->helper_proc = NULL;
165 if (GNUNET_SCHEDULER_NO_TASK != h->restart_task)
167 GNUNET_SCHEDULER_cancel (h->restart_task);
168 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
170 if (GNUNET_SCHEDULER_NO_TASK != h->read_task)
172 GNUNET_SCHEDULER_cancel (h->read_task);
173 h->read_task = GNUNET_SCHEDULER_NO_TASK;
175 if (GNUNET_SCHEDULER_NO_TASK != h->write_task)
177 GNUNET_SCHEDULER_cancel (h->write_task);
178 h->write_task = GNUNET_SCHEDULER_NO_TASK;
180 if (NULL != h->helper_in)
182 GNUNET_DISK_pipe_close (h->helper_in);
184 h->fh_to_helper = NULL;
186 if (NULL != h->helper_out)
188 GNUNET_DISK_pipe_close (h->helper_out);
189 h->helper_out = NULL;
190 h->fh_from_helper = NULL;
192 while (NULL != (sh = h->sh_head))
194 GNUNET_CONTAINER_DLL_remove (h->sh_head,
197 if (NULL != sh->cont)
198 sh->cont (sh->cont_cls, GNUNET_NO);
201 /* purge MST buffer */
202 (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO);
207 * Restart the helper process.
209 * @param cls handle to the helper process
210 * @param tc scheduler context
213 restart_task (void *cls,
214 const struct GNUNET_SCHEDULER_TaskContext *tc);
218 * Read from the helper-process
220 * @param cls handle to the helper process
221 * @param tc scheduler context
224 helper_read (void *cls,
225 const struct GNUNET_SCHEDULER_TaskContext *tc)
227 struct GNUNET_HELPER_Handle *h = cls;
228 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
231 h->read_task = GNUNET_SCHEDULER_NO_TASK;
232 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
235 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
236 h->fh_from_helper, &helper_read, h);
239 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
242 /* On read-error, restart the helper */
243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
244 _("Error reading from `%s': %s\n"),
248 /* Restart the helper */
250 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
256 /* this happens if the helper is shut down via a
257 signal, so it is not a "hard" error */
258 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
259 _("Got 0 bytes from helper `%s' (EOF)\n"),
262 /* Restart the helper */
264 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
268 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
269 _("Got %u bytes from helper `%s'\n"),
272 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
273 h->fh_from_helper, &helper_read, h);
275 GNUNET_SERVER_mst_receive (h->mst, NULL, buf, t, GNUNET_NO, GNUNET_NO))
277 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
278 _("Failed to parse inbound message from helper `%s'\n"),
281 /* Restart the helper */
283 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
291 * Start the helper process.
293 * @param h handle to the helper process
296 start_helper (struct GNUNET_HELPER_Handle *h)
298 h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
299 h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
300 if ( (h->helper_in == NULL) || (h->helper_out == NULL))
302 /* out of file descriptors? try again later... */
305 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
309 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
310 _("Starting HELPER process `%s'\n"),
313 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
315 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
317 GNUNET_OS_start_process_vap (GNUNET_NO,
318 h->helper_in, h->helper_out,
321 if (NULL == h->helper_proc)
323 /* failed to start process? try again later... */
326 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
330 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
331 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
332 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
340 * Restart the helper process.
342 * @param cls handle to the helper process
343 * @param tc scheduler context
346 restart_task (void *cls,
347 const struct GNUNET_SCHEDULER_TaskContext *tc)
349 struct GNUNET_HELPER_Handle*h = cls;
351 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
357 * @brief Starts a helper and begins reading from it
359 * @param binary_name name of the binary to run
360 * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
361 * argument must not be modified by the client for
362 * the lifetime of the helper h)
363 * @param cb function to call if we get messages from the helper
364 * @param cb_cls Closure for the callback
365 * @return the new H, NULL on error
367 struct GNUNET_HELPER_Handle*
368 GNUNET_HELPER_start (const char *binary_name,
369 char *const binary_argv[],
370 GNUNET_SERVER_MessageTokenizerCallback cb, void *cb_cls)
372 struct GNUNET_HELPER_Handle*h;
374 h = GNUNET_malloc (sizeof (struct GNUNET_HELPER_Handle));
375 h->binary_name = binary_name;
376 h->binary_argv = binary_argv;
377 h->mst = GNUNET_SERVER_mst_create (cb, cb_cls);
384 * @brief Kills the helper, closes the pipe and frees the h
386 * @param h h to helper to stop
389 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h)
391 struct GNUNET_HELPER_SendHandle *sh;
393 /* signal pending writes that we were stopped */
394 while (NULL != (sh = h->sh_head))
396 GNUNET_CONTAINER_DLL_remove (h->sh_head,
399 if (NULL != sh->cont)
400 sh->cont (sh->cont_cls, GNUNET_SYSERR);
404 GNUNET_SERVER_mst_destroy (h->mst);
410 * Write to the helper-process
412 * @param cls handle to the helper process
413 * @param tc scheduler context
416 helper_write (void *cls,
417 const struct GNUNET_SCHEDULER_TaskContext *tc)
419 struct GNUNET_HELPER_Handle *h = cls;
420 struct GNUNET_HELPER_SendHandle *sh;
424 h->write_task = GNUNET_SCHEDULER_NO_TASK;
425 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
428 h->write_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
429 h->fh_to_helper, &helper_write, h);
432 if (NULL == (sh = h->sh_head))
433 return; /* how did this happen? */
434 buf = (const char*) sh->msg;
435 t = GNUNET_DISK_file_write (h->fh_to_helper, &buf[sh->wpos], ntohs (sh->msg->size) - sh->wpos);
438 /* On write-error, restart the helper */
439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
440 _("Error writing to `%s': %s\n"),
444 /* Restart the helper */
446 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
451 if (sh->wpos == ntohs (sh->msg->size))
453 GNUNET_CONTAINER_DLL_remove (h->sh_head,
456 if (NULL != sh->cont)
457 sh->cont (sh->cont_cls, GNUNET_YES);
460 if (NULL != h->sh_head)
461 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
469 * Send an message to the helper.
471 * @param h helper to send message to
472 * @param msg message to send
473 * @param can_drop can the message be dropped if there is already one in the queue?
474 * @param cont continuation to run once the message is out (PREREQ_DONE on succees, CANCEL
475 * if the helper process died, NULL during GNUNET_HELPER_stop).
476 * @param cont_cls closure for 'cont'
477 * @return NULL if the message was dropped,
478 * otherwise handle to cancel *cont* (actual transmission may
481 struct GNUNET_HELPER_SendHandle *
482 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
483 const struct GNUNET_MessageHeader *msg,
485 GNUNET_HELPER_Continuation cont,
488 struct GNUNET_HELPER_SendHandle *sh;
491 if (NULL == h->fh_to_helper)
493 if ( (GNUNET_YES == can_drop) &&
494 (NULL != h->sh_head) )
496 mlen = ntohs (msg->size);
497 sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
498 sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
499 memcpy (&sh[1], msg, mlen);
502 sh->cont_cls = cont_cls;
503 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head,
506 if (GNUNET_SCHEDULER_NO_TASK == h->write_task)
507 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
516 * Cancel a 'send' operation. If possible, transmitting the
517 * message is also aborted, but at least 'cont' won't be
520 * @param sh operation to cancel
523 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
525 struct GNUNET_HELPER_Handle *h = sh->h;
531 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
532 if (NULL == h->sh_head)
534 GNUNET_SCHEDULER_cancel (h->write_task);
535 h->write_task = GNUNET_SCHEDULER_NO_TASK;
542 /* end of helper.c */