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 HelperMessageQueueEntry
38 * This is an entry in a DLL.
40 struct HelperMessageQueueEntry *next;
43 * This is an entry in a DLL.
45 struct HelperMessageQueueEntry *prev;
48 * Message to transmit (allocated at the end of this struct)
50 const struct GNUNET_MessageHeader *msg;
53 * Function to call upon completion.
55 GNUNET_HELPER_Continuation cont;
63 * Current write position.
71 * The handle to a helper process.
73 struct GNUNET_HELPER_Handle
77 * PipeHandle to receive data from the helper
79 struct GNUNET_DISK_PipeHandle *helper_in;
82 * PipeHandle to send data to the helper
84 struct GNUNET_DISK_PipeHandle *helper_out;
87 * FileHandle to receive data from the helper
89 const struct GNUNET_DISK_FileHandle *fh_from_helper;
92 * FileHandle to send data to the helper
94 const struct GNUNET_DISK_FileHandle *fh_to_helper;
97 * The process id of the helper
99 struct GNUNET_OS_Process *helper_proc;
102 * The Message-Tokenizer that tokenizes the messages comming from the helper
104 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
107 * First message queued for transmission to helper.
109 struct HelperMessageQueueEntry *mq_head;
112 * Last message queued for transmission to helper.
114 struct HelperMessageQueueEntry *mq_tail;
119 const char *binary_name;
122 * NULL-terminated list of command-line arguments.
124 char *const *binary_argv;
127 * Task to read from the helper.
129 GNUNET_SCHEDULER_TaskIdentifier read_task;
132 * Task to read from the helper.
134 GNUNET_SCHEDULER_TaskIdentifier write_task;
139 GNUNET_SCHEDULER_TaskIdentifier restart_task;
144 * Stop the helper process, we're closing down or had an error.
146 * @param h handle to the helper process
149 stop_helper (struct GNUNET_HELPER_Handle *h)
151 struct HelperMessageQueueEntry *qe;
153 if (NULL != h->helper_proc)
155 GNUNET_OS_process_kill (h->helper_proc, SIGTERM);
156 GNUNET_OS_process_wait (h->helper_proc);
157 GNUNET_OS_process_close (h->helper_proc);
158 h->helper_proc = NULL;
160 if (GNUNET_SCHEDULER_NO_TASK != h->restart_task)
162 GNUNET_SCHEDULER_cancel (h->restart_task);
163 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
165 if (GNUNET_SCHEDULER_NO_TASK != h->read_task)
167 GNUNET_SCHEDULER_cancel (h->read_task);
168 h->read_task = GNUNET_SCHEDULER_NO_TASK;
170 if (GNUNET_SCHEDULER_NO_TASK != h->write_task)
172 GNUNET_SCHEDULER_cancel (h->write_task);
173 h->write_task = GNUNET_SCHEDULER_NO_TASK;
175 if (NULL != h->helper_in)
177 GNUNET_DISK_pipe_close (h->helper_in);
179 h->fh_to_helper = NULL;
181 if (NULL != h->helper_out)
183 GNUNET_DISK_pipe_close (h->helper_out);
184 h->helper_out = NULL;
185 h->fh_from_helper = NULL;
187 while (NULL != (qe = h->mq_head))
189 GNUNET_CONTAINER_DLL_remove (h->mq_head,
192 qe->cont (qe->cont_cls, GNUNET_NO);
195 /* purge MST buffer */
196 GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO);
201 * Restart the helper process.
203 * @param cls handle to the helper process
204 * @param tc scheduler context
207 restart_task (void *cls,
208 const struct GNUNET_SCHEDULER_TaskContext *tc);
212 * Read from the helper-process
214 * @param cls handle to the helper process
215 * @param tc scheduler context
218 helper_read (void *cls,
219 const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
221 struct GNUNET_HELPER_Handle *h = cls;
222 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
225 h->read_task = GNUNET_SCHEDULER_NO_TASK;
226 if (0 != (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
229 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
230 h->fh_from_helper, &helper_read, h);
233 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
236 /* On read-error, restart the helper */
237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
238 _("Error reading from `%s': %s\n"),
242 /* Restart the helper */
244 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
250 /* this happens if the helper is shut down via a
251 signal, so it is not a "hard" error */
252 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
253 _("Got 0 bytes from helper `%s' (EOF)\n"),
256 /* Restart the helper */
258 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
263 GNUNET_SERVER_mst_receive (h->mst, NULL, buf, t, GNUNET_NO, GNUNET_NO))
265 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
266 _("Failed to parse inbound message from helper `%s'\n"),
269 /* Restart the helper */
271 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
276 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
277 h->fh_from_helper, &helper_read, h);
282 * Start the helper process.
284 * @param h handle to the helper process
287 start_helper (struct GNUNET_HELPER_Handle *h)
289 h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
290 h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
291 if ( (h->helper_in == NULL) || (h->helper_out == NULL))
293 /* out of file descriptors? try again later... */
296 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
301 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
303 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
305 GNUNET_OS_start_process_vap (h->helper_in, h->helper_out,
308 if (NULL == h->helper_proc)
310 /* failed to start process? try again later... */
313 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
317 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
318 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
319 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
327 * Restart the helper process.
329 * @param cls handle to the helper process
330 * @param tc scheduler context
333 restart_task (void *cls,
334 const struct GNUNET_SCHEDULER_TaskContext *tc)
336 struct GNUNET_HELPER_Handle*h = cls;
338 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
344 * @brief Starts a helper and begins reading from it
346 * @param binary_name name of the binary to run
347 * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
348 * argument must not be modified by the client for
349 * the lifetime of the helper h)
350 * @param cb function to call if we get messages from the helper
351 * @param cb_cls Closure for the callback
352 * @return the new H, NULL on error
354 struct GNUNET_HELPER_Handle*
355 GNUNET_HELPER_start (const char *binary_name,
356 char *const binary_argv[],
357 GNUNET_SERVER_MessageTokenizerCallback cb, void *cb_cls)
359 struct GNUNET_HELPER_Handle*h;
361 h = GNUNET_malloc (sizeof (struct GNUNET_HELPER_Handle));
362 h->binary_name = binary_name;
363 h->binary_argv = binary_argv;
364 h->mst = GNUNET_SERVER_mst_create (cb, cb_cls);
371 * @brief Kills the helper, closes the pipe and frees the h
373 * @param h h to helper to stop
376 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h)
378 struct HelperMessageQueueEntry *qe;
380 /* signal pending writes that we were stopped */
381 while (NULL != (qe = h->mq_head))
383 GNUNET_CONTAINER_DLL_remove (h->mq_head,
386 qe->cont (qe->cont_cls, GNUNET_SYSERR);
390 GNUNET_SERVER_mst_destroy (h->mst);
396 * Write to the helper-process
398 * @param cls handle to the helper process
399 * @param tc scheduler context
402 helper_write (void *cls,
403 const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
405 struct GNUNET_HELPER_Handle *h = cls;
406 struct HelperMessageQueueEntry *qe;
410 h->write_task = GNUNET_SCHEDULER_NO_TASK;
411 if (0 != (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
414 h->write_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
415 h->fh_to_helper, &helper_write, h);
418 if (NULL == (qe = h->mq_head))
419 return; /* how did this happen? */
420 buf = (const char*) qe->msg;
421 t = GNUNET_DISK_file_write (h->fh_to_helper, &buf[qe->wpos], ntohs (qe->msg->size) - qe->wpos);
424 /* On write-error, restart the helper */
425 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
426 _("Error writing to `%s': %s\n"),
430 /* Restart the helper */
432 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
437 if (qe->wpos == ntohs (qe->msg->size))
439 GNUNET_CONTAINER_DLL_remove (h->mq_head,
442 if (NULL != qe->cont)
443 qe->cont (qe->cont_cls, GNUNET_YES);
446 if (NULL != h->mq_head)
447 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
455 * Send an message to the helper.
457 * @param h helper to send message to
458 * @param msg message to send
459 * @param can_drop can the message be dropped if there is already one in the queue?
460 * @param cont continuation to run once the message is out (PREREQ_DONE on succees, CANCEL
461 * if the helper process died, NULL during GNUNET_HELPER_stop).
462 * @param cont_cls closure for 'cont'
463 * @return GNUNET_YES if the message will be sent
464 * GNUNET_NO if the message was dropped
467 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
468 const struct GNUNET_MessageHeader *msg,
470 GNUNET_HELPER_Continuation cont,
473 struct HelperMessageQueueEntry *qe;
476 if ( (GNUNET_YES == can_drop) &&
477 (h->mq_head != NULL) )
479 mlen = ntohs (msg->size);
480 qe = GNUNET_malloc (sizeof (struct HelperMessageQueueEntry) + mlen);
481 qe->msg = (const struct GNUNET_MessageHeader*) &qe[1];
482 memcpy (&qe[1], msg, mlen);
484 qe->cont_cls = cont_cls;
485 GNUNET_CONTAINER_DLL_insert_tail (h->mq_head,
488 if (GNUNET_SCHEDULER_NO_TASK == h->write_task)
489 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
498 /* end of helper.c */