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;
134 const char *binary_name;
137 * NULL-terminated list of command-line arguments.
139 char *const *binary_argv;
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;
159 * Stop the helper process, we're closing down or had an error.
161 * @param h handle to the helper process
164 stop_helper (struct GNUNET_HELPER_Handle *h)
166 struct GNUNET_HELPER_SendHandle *sh;
168 if (NULL != h->helper_proc)
170 GNUNET_break (0 == GNUNET_OS_process_kill (h->helper_proc, SIGTERM));
171 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (h->helper_proc));
172 GNUNET_OS_process_destroy (h->helper_proc);
173 h->helper_proc = NULL;
175 if (GNUNET_SCHEDULER_NO_TASK != h->restart_task)
177 GNUNET_SCHEDULER_cancel (h->restart_task);
178 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
180 if (GNUNET_SCHEDULER_NO_TASK != h->read_task)
182 GNUNET_SCHEDULER_cancel (h->read_task);
183 h->read_task = GNUNET_SCHEDULER_NO_TASK;
185 if (GNUNET_SCHEDULER_NO_TASK != h->write_task)
187 GNUNET_SCHEDULER_cancel (h->write_task);
188 h->write_task = GNUNET_SCHEDULER_NO_TASK;
190 if (NULL != h->helper_in)
192 GNUNET_DISK_pipe_close (h->helper_in);
194 h->fh_to_helper = NULL;
196 if (NULL != h->helper_out)
198 GNUNET_DISK_pipe_close (h->helper_out);
199 h->helper_out = NULL;
200 h->fh_from_helper = NULL;
202 while (NULL != (sh = h->sh_head))
204 GNUNET_CONTAINER_DLL_remove (h->sh_head,
207 if (NULL != sh->cont)
208 sh->cont (sh->cont_cls, GNUNET_NO);
211 /* purge MST buffer */
212 (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO);
217 * Restart the helper process.
219 * @param cls handle to the helper process
220 * @param tc scheduler context
223 restart_task (void *cls,
224 const struct GNUNET_SCHEDULER_TaskContext *tc);
228 * Read from the helper-process
230 * @param cls handle to the helper process
231 * @param tc scheduler context
234 helper_read (void *cls,
235 const struct GNUNET_SCHEDULER_TaskContext *tc)
237 struct GNUNET_HELPER_Handle *h = cls;
238 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
241 h->read_task = GNUNET_SCHEDULER_NO_TASK;
242 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
245 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
246 h->fh_from_helper, &helper_read, h);
249 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
252 /* On read-error, restart the helper */
253 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
254 _("Error reading from `%s': %s\n"),
257 if (NULL != h->exp_cb)
259 h->exp_cb (h->cb_cls);
260 GNUNET_HELPER_stop (h);
264 /* Restart the helper */
266 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &restart_task, h);
271 /* this happens if the helper is shut down via a
272 signal, so it is not a "hard" error */
273 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
274 _("Got 0 bytes from helper `%s' (EOF)\n"),
276 if (NULL != h->exp_cb)
278 h->exp_cb (h->cb_cls);
279 GNUNET_HELPER_stop (h);
283 /* Restart the helper */
285 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
289 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
290 _("Got %u bytes from helper `%s'\n"),
293 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
294 h->fh_from_helper, &helper_read, h);
296 GNUNET_SERVER_mst_receive (h->mst, NULL, buf, t, GNUNET_NO, GNUNET_NO))
298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
299 _("Failed to parse inbound message from helper `%s'\n"),
301 if (NULL != h->exp_cb)
303 h->exp_cb (h->cb_cls);
304 GNUNET_HELPER_stop (h);
308 /* Restart the helper */
310 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
318 * Start the helper process.
320 * @param h handle to the helper process
323 start_helper (struct GNUNET_HELPER_Handle *h)
325 h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
326 h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
327 if ( (h->helper_in == NULL) || (h->helper_out == NULL))
329 /* out of file descriptors? try again later... */
332 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
336 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
337 _("Starting HELPER process `%s'\n"),
340 GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
342 GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
344 GNUNET_OS_start_process_vap (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
345 h->helper_in, h->helper_out,
348 if (NULL == h->helper_proc)
350 /* failed to start process? try again later... */
353 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
357 GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
358 GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
359 h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
367 * Restart the helper process.
369 * @param cls handle to the helper process
370 * @param tc scheduler context
373 restart_task (void *cls,
374 const struct GNUNET_SCHEDULER_TaskContext *tc)
376 struct GNUNET_HELPER_Handle*h = cls;
378 h->restart_task = GNUNET_SCHEDULER_NO_TASK;
384 * Starts a helper and begins reading from it. The helper process is
385 * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
386 * or when the exp_cb callback is not NULL.
388 * @param binary_name name of the binary to run
389 * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
390 * argument must not be modified by the client for
391 * the lifetime of the helper handle)
392 * @param cb function to call if we get messages from the helper
393 * @param exp_cb the exception callback to call. Set this to NULL if the helper
394 * process has to be restarted automatically when it dies/crashes
395 * @param cb_cls closure for the above callback
396 * @return the new Handle, NULL on error
398 struct GNUNET_HELPER_Handle *
399 GNUNET_HELPER_start (const char *binary_name,
400 char *const binary_argv[],
401 GNUNET_SERVER_MessageTokenizerCallback cb,
402 GNUNET_HELPER_ExceptionCallback exp_cb,
405 struct GNUNET_HELPER_Handle*h;
407 h = GNUNET_malloc (sizeof (struct GNUNET_HELPER_Handle));
408 h->binary_name = binary_name;
409 h->binary_argv = binary_argv;
411 h->mst = GNUNET_SERVER_mst_create (cb, h->cb_cls);
419 * @brief Kills the helper, closes the pipe and frees the h
421 * @param h h to helper to stop
424 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h)
426 struct GNUNET_HELPER_SendHandle *sh;
429 /* signal pending writes that we were stopped */
430 while (NULL != (sh = h->sh_head))
432 GNUNET_CONTAINER_DLL_remove (h->sh_head,
435 if (NULL != sh->cont)
436 sh->cont (sh->cont_cls, GNUNET_SYSERR);
440 GNUNET_SERVER_mst_destroy (h->mst);
446 * Write to the helper-process
448 * @param cls handle to the helper process
449 * @param tc scheduler context
452 helper_write (void *cls,
453 const struct GNUNET_SCHEDULER_TaskContext *tc)
455 struct GNUNET_HELPER_Handle *h = cls;
456 struct GNUNET_HELPER_SendHandle *sh;
460 h->write_task = GNUNET_SCHEDULER_NO_TASK;
461 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
464 h->write_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
465 h->fh_to_helper, &helper_write, h);
468 if (NULL == (sh = h->sh_head))
469 return; /* how did this happen? */
470 buf = (const char*) sh->msg;
471 t = GNUNET_DISK_file_write (h->fh_to_helper, &buf[sh->wpos], ntohs (sh->msg->size) - sh->wpos);
474 /* On write-error, restart the helper */
475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
476 _("Error writing to `%s': %s\n"),
480 /* Restart the helper */
482 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
487 if (sh->wpos == ntohs (sh->msg->size))
489 GNUNET_CONTAINER_DLL_remove (h->sh_head,
492 if (NULL != sh->cont)
493 sh->cont (sh->cont_cls, GNUNET_YES);
496 if (NULL != h->sh_head)
497 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
505 * Send an message to the helper.
507 * @param h helper to send message to
508 * @param msg message to send
509 * @param can_drop can the message be dropped if there is already one in the queue?
510 * @param cont continuation to run once the message is out (PREREQ_DONE on succees, CANCEL
511 * if the helper process died, NULL during GNUNET_HELPER_stop).
512 * @param cont_cls closure for 'cont'
513 * @return NULL if the message was dropped,
514 * otherwise handle to cancel *cont* (actual transmission may
517 struct GNUNET_HELPER_SendHandle *
518 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
519 const struct GNUNET_MessageHeader *msg,
521 GNUNET_HELPER_Continuation cont,
524 struct GNUNET_HELPER_SendHandle *sh;
527 if (NULL == h->fh_to_helper)
529 if ( (GNUNET_YES == can_drop) &&
530 (NULL != h->sh_head) )
532 mlen = ntohs (msg->size);
533 sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
534 sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
535 memcpy (&sh[1], msg, mlen);
538 sh->cont_cls = cont_cls;
539 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head,
542 if (GNUNET_SCHEDULER_NO_TASK == h->write_task)
543 h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
552 * Cancel a 'send' operation. If possible, transmitting the
553 * message is also aborted, but at least 'cont' won't be
556 * @param sh operation to cancel
559 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
561 struct GNUNET_HELPER_Handle *h = sh->h;
567 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
568 if (NULL == h->sh_head)
570 GNUNET_SCHEDULER_cancel (h->write_task);
571 h->write_task = GNUNET_SCHEDULER_NO_TASK;
578 /* end of helper.c */