GNUnet  0.11.x
Data Structures | Functions
helper.c File Reference

API for dealing with (SUID) helper processes that communicate via GNUNET_MessageHeaders on stdin/stdout. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_mst_lib.h"
Include dependency graph for helper.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_HELPER_SendHandle
 Entry in the queue of messages we need to transmit to the helper. More...
 
struct  GNUNET_HELPER_Handle
 The handle to a helper process. More...
 

Functions

int GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill)
 Sends termination signal to the helper process. More...
 
int GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
 Reap the helper process. More...
 
static void stop_helper (struct GNUNET_HELPER_Handle *h, int soft_kill)
 Stop the helper process, we're closing down or had an error. More...
 
static void restart_task (void *cls)
 Restart the helper process. More...
 
static void helper_read (void *cls)
 Read from the helper-process. More...
 
static void start_helper (struct GNUNET_HELPER_Handle *h)
 Start the helper process. More...
 
struct GNUNET_HELPER_HandleGNUNET_HELPER_start (int with_control_pipe, const char *binary_name, char *const binary_argv[], GNUNET_MessageTokenizerCallback cb, GNUNET_HELPER_ExceptionCallback exp_cb, void *cb_cls)
 Starts a helper and begins reading from it. More...
 
void GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
 Free's the resources occupied by the helper handle. More...
 
void GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, int soft_kill)
 Kills the helper, closes the pipe and frees the handle. More...
 
static void helper_write (void *cls)
 Write to the helper-process. More...
 
struct GNUNET_HELPER_SendHandleGNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h, const struct GNUNET_MessageHeader *msg, int can_drop, GNUNET_HELPER_Continuation cont, void *cont_cls)
 Send an message to the helper. More...
 
void GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
 Cancel a GNUNET_HELPER_send operation. More...
 

Detailed Description

API for dealing with (SUID) helper processes that communicate via GNUNET_MessageHeaders on stdin/stdout.

Author
Philipp Toelke
Christian Grothoff

Definition in file helper.c.

Function Documentation

◆ stop_helper()

static void stop_helper ( struct GNUNET_HELPER_Handle h,
int  soft_kill 
)
static

Stop the helper process, we're closing down or had an error.

Parameters
hhandle to the helper process
soft_killif GNUNET_YES, signals termination by closing the helper's stdin; GNUNET_NO to signal termination by sending SIGTERM to helper

Definition at line 281 of file helper.c.

282 {
283  if (NULL != h->restart_task)
284  {
285  GNUNET_SCHEDULER_cancel (h->restart_task);
286  h->restart_task = NULL;
287  }
288  else
289  {
290  GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
292  }
293 }
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
@ GNUNET_OK
Definition: gnunet_common.h:95
int GNUNET_HELPER_kill(struct GNUNET_HELPER_Handle *h, int soft_kill)
Sends termination signal to the helper process.
Definition: helper.c:177
int GNUNET_HELPER_wait(struct GNUNET_HELPER_Handle *h)
Reap the helper process.
Definition: helper.c:225
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972

References GNUNET_break, GNUNET_HELPER_kill(), GNUNET_HELPER_wait(), GNUNET_OK, GNUNET_SCHEDULER_cancel(), and h.

Referenced by GNUNET_HELPER_stop(), helper_read(), helper_write(), and start_helper().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ restart_task()

static void restart_task ( void *  cls)
static

Restart the helper process.

Parameters
clshandle to the helper process

Definition at line 461 of file helper.c.

462 {
463  struct GNUNET_HELPER_Handle *h = cls;
464 
465  h->restart_task = NULL;
466  h->retry_back_off++;
468  "Restarting helper with back-off %u\n",
469  h->retry_back_off);
470  start_helper (h);
471 }
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_INFO
static void start_helper(struct GNUNET_HELPER_Handle *h)
Start the helper process.
Definition: helper.c:401
The handle to a helper process.
Definition: helper.c:79

References GNUNET_ERROR_TYPE_INFO, GNUNET_log, h, and start_helper().

Referenced by helper_read(), helper_write(), and start_helper().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ helper_read()

static void helper_read ( void *  cls)
static

Read from the helper-process.

Parameters
clshandle to the helper process

Definition at line 311 of file helper.c.

312 {
313  struct GNUNET_HELPER_Handle *h = cls;
315  ssize_t t;
316 
317  h->read_task = NULL;
318  t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof(buf));
319  if (t < 0)
320  {
321  /* On read-error, restart the helper */
323  _ ("Error reading from `%s': %s\n"),
324  h->binary_name,
325  strerror (errno));
326  if (NULL != h->exp_cb)
327  {
328  h->exp_cb (h->cb_cls);
330  return;
331  }
333  /* Restart the helper */
334  h->restart_task = GNUNET_SCHEDULER_add_delayed (
336  h->retry_back_off),
337  &restart_task,
338  h);
339  return;
340  }
341  if (0 == t)
342  {
343  /* this happens if the helper is shut down via a
344  signal, so it is not a "hard" error */
346  "Got 0 bytes from helper `%s' (EOF)\n",
347  h->binary_name);
348  if (NULL != h->exp_cb)
349  {
350  h->exp_cb (h->cb_cls);
352  return;
353  }
355  /* Restart the helper */
356  h->restart_task = GNUNET_SCHEDULER_add_delayed (
358  h->retry_back_off),
359  &restart_task,
360  h);
361  return;
362  }
364  "Got %u bytes from helper `%s'\n",
365  (unsigned int) t,
366  h->binary_name);
368  h->fh_from_helper,
369  &helper_read,
370  h);
371  if (GNUNET_SYSERR ==
373  {
375  _ ("Failed to parse inbound message from helper `%s'\n"),
376  h->binary_name);
377  if (NULL != h->exp_cb)
378  {
379  h->exp_cb (h->cb_cls);
381  return;
382  }
384  /* Restart the helper */
385  h->restart_task = GNUNET_SCHEDULER_add_delayed (
387  h->retry_back_off),
388  &restart_task,
389  h);
390  return;
391  }
392 }
static char buf[2048]
static struct GNUNET_SCHEDULER_Task * t
Main task.
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to 'struct's.
@ GNUNET_NO
Definition: gnunet_common.h:94
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:622
void GNUNET_HELPER_stop(struct GNUNET_HELPER_Handle *h, int soft_kill)
Kills the helper, closes the pipe and frees the handle.
Definition: helper.c:567
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_file(struct GNUNET_TIME_Relative delay, const struct GNUNET_DISK_FileHandle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1667
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1269
int GNUNET_MST_from_buffer(struct GNUNET_MessageStreamTokenizer *mst, const char *buf, size_t size, int purge, int one_shot)
Add incoming data to the receive buffer and call the callback for all complete messages.
Definition: mst.c:114
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_TIME_UNIT_SECONDS
One second.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:311
static void restart_task(void *cls)
Restart the helper process.
Definition: helper.c:461
static void helper_read(void *cls)
Read from the helper-process.
Definition: helper.c:311
static void stop_helper(struct GNUNET_HELPER_Handle *h, int soft_kill)
Stop the helper process, we're closing down or had an error.
Definition: helper.c:281
#define _(String)
GNU gettext support macro.
Definition: platform.h:177

References _, buf, GNUNET_ALIGN, GNUNET_DISK_file_read(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_HELPER_stop(), GNUNET_log, GNUNET_MAX_MESSAGE_SIZE, GNUNET_MST_from_buffer(), GNUNET_NO, GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_add_read_file(), GNUNET_SYSERR, GNUNET_TIME_relative_multiply(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TIME_UNIT_SECONDS, h, restart_task(), stop_helper(), and t.

Referenced by start_helper().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ start_helper()

static void start_helper ( struct GNUNET_HELPER_Handle h)
static

Start the helper process.

Parameters
hhandle to the helper process

Definition at line 401 of file helper.c.

402 {
403  h->helper_in =
405  h->helper_out =
407  if ((h->helper_in == NULL) || (h->helper_out == NULL))
408  {
409  /* out of file descriptors? try again later... */
411  h->restart_task = GNUNET_SCHEDULER_add_delayed (
413  h->retry_back_off),
414  &restart_task,
415  h);
416  return;
417  }
419  "Starting HELPER process `%s'\n",
420  h->binary_name);
421  h->fh_from_helper =
423  h->fh_to_helper =
425  h->helper_proc = GNUNET_OS_start_process_vap (h->with_control_pipe
429  h->helper_in,
430  h->helper_out,
431  NULL,
432  h->binary_name,
433  h->binary_argv);
434  if (NULL == h->helper_proc)
435  {
436  /* failed to start process? try again later... */
438  h->restart_task = GNUNET_SCHEDULER_add_delayed (
440  h->retry_back_off),
441  &restart_task,
442  h);
443  return;
444  }
447  if (NULL != h->mst)
449  h->fh_from_helper,
450  &helper_read,
451  h);
452 }
const struct GNUNET_DISK_FileHandle * GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd n)
Get the handle to a particular pipe end.
Definition: disk.c:1616
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close_end(struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end)
Closes one half of an interprocess channel.
Definition: disk.c:1532
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(enum GNUNET_DISK_PipeFlags pf)
Creates an interprocess channel.
Definition: disk.c:1442
@ GNUNET_DISK_PF_BLOCKING_RW
Configure both pipe ends for blocking operations if set.
@ GNUNET_DISK_PIPE_END_WRITE
The writing-end of a pipe.
@ GNUNET_DISK_PIPE_END_READ
The reading-end of a pipe.
struct GNUNET_OS_Process * GNUNET_OS_start_process_vap(enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename, char *const argv[])
Start a process.
Definition: os_priority.c:604
@ GNUNET_OS_INHERIT_STD_ERR
When this flag is set, the child process will inherit stderr of the parent.
Definition: gnunet_os_lib.h:91
@ GNUNET_OS_USE_PIPE_CONTROL
Should a pipe be used to send signals to the child?

References GNUNET_DISK_PF_BLOCKING_RW, GNUNET_DISK_pipe(), GNUNET_DISK_pipe_close_end(), GNUNET_DISK_PIPE_END_READ, GNUNET_DISK_PIPE_END_WRITE, GNUNET_DISK_pipe_handle(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, GNUNET_OS_start_process_vap(), GNUNET_OS_USE_PIPE_CONTROL, GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_add_read_file(), GNUNET_TIME_relative_multiply(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TIME_UNIT_SECONDS, h, helper_read(), restart_task(), and stop_helper().

Referenced by GNUNET_HELPER_start(), and restart_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ helper_write()

static void helper_write ( void *  cls)
static

Write to the helper-process.

Parameters
clshandle to the helper process

Definition at line 581 of file helper.c.

582 {
583  struct GNUNET_HELPER_Handle *h = cls;
585  const char *buf;
586  ssize_t t;
587 
588  h->write_task = NULL;
589  if (NULL == (sh = h->sh_head))
590  {
591  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Helper write had no work!\n");
592  return; /* how did this happen? */
593  }
594  buf = (const char *) sh->msg;
595  t = GNUNET_DISK_file_write (h->fh_to_helper,
596  &buf[sh->wpos],
597  ntohs (sh->msg->size) - sh->wpos);
598  if (-1 == t)
599  {
600  /* On write-error, restart the helper */
602  _ ("Error writing to `%s': %s\n"),
603  h->binary_name,
604  strerror (errno));
605  if (NULL != h->exp_cb)
606  {
607  h->exp_cb (h->cb_cls);
609  return;
610  }
612  "Stopping and restarting helper task!\n");
614  /* Restart the helper */
615  h->restart_task = GNUNET_SCHEDULER_add_delayed (
617  h->retry_back_off),
618  &restart_task,
619  h);
620  return;
621  }
623  "Transmitted %u bytes to %s\n",
624  (unsigned int) t,
625  h->binary_name);
626  sh->wpos += t;
627  if (sh->wpos == ntohs (sh->msg->size))
628  {
629  GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
630  if (NULL != sh->cont)
631  sh->cont (sh->cont_cls, GNUNET_YES);
632  GNUNET_free (sh);
633  }
634  if (NULL != h->sh_head)
635  h->write_task =
637  h->fh_to_helper,
638  &helper_write,
639  h);
640 }
static struct SolverHandle * sh
@ GNUNET_YES
Definition: gnunet_common.h:97
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:686
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_write_file(struct GNUNET_TIME_Relative delay, const struct GNUNET_DISK_FileHandle *wfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1700
static void helper_write(void *cls)
Write to the helper-process.
Definition: helper.c:581
Entry in the queue of messages we need to transmit to the helper.
Definition: helper.c:37

References _, buf, GNUNET_CONTAINER_DLL_remove, GNUNET_DISK_file_write(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_HELPER_stop(), GNUNET_log, GNUNET_NO, GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_add_write_file(), GNUNET_TIME_relative_multiply(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TIME_UNIT_SECONDS, GNUNET_YES, h, restart_task(), sh, stop_helper(), and t.

Referenced by GNUNET_HELPER_send().

Here is the call graph for this function:
Here is the caller graph for this function: