GNUnet  0.11.x
Typedefs | Functions
Helper library

Dealing with SUID helper processes. More...

Typedefs

typedef void(* GNUNET_HELPER_ExceptionCallback) (void *cls)
 Callback that will be called when the helper process dies. More...
 
typedef void(* GNUNET_HELPER_Continuation) (void *cls, int result)
 Continuation function. More...
 

Functions

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...
 
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...
 
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, frees the handle and calls wait() on 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

Dealing with SUID helper processes.

Provides an API for dealing with (SUID) helper processes that communicate via GNUNET_MessageHeaders on STDIN/STDOUT.

Typedef Documentation

◆ GNUNET_HELPER_ExceptionCallback

typedef void(* GNUNET_HELPER_ExceptionCallback) (void *cls)

Callback that will be called when the helper process dies.

This is not called when the helper process is stopped using GNUNET_HELPER_stop()

Parameters
clsthe closure from GNUNET_HELPER_start()

Definition at line 56 of file gnunet_helper_lib.h.

◆ GNUNET_HELPER_Continuation

typedef void(* GNUNET_HELPER_Continuation) (void *cls, int result)

Continuation function.

Parameters
clsclosure
resultGNUNET_OK on success, GNUNET_NO if helper process died GNUNET_SYSERR during GNUNET_HELPER_destroy

Definition at line 139 of file gnunet_helper_lib.h.

Function Documentation

◆ GNUNET_HELPER_start()

struct GNUNET_HELPER_Handle* GNUNET_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.

The helper process is restarted when it dies except when it is stopped using GNUNET_HELPER_stop() or when the exp_cb callback is not NULL.

Parameters
with_control_pipedoes the helper support the use of a control pipe for signalling?
binary_namename of the binary to run
binary_argvNULL-terminated list of arguments to give when starting the binary (this argument must not be modified by the client for the lifetime of the helper handle)
cbfunction to call if we get messages from the helper
exp_cbthe exception callback to call. Set this to NULL if the helper process has to be restarted automatically when it dies/crashes
cb_clsclosure for the above callbacks
Returns
the new Handle, NULL on error

The helper process is restarted when it dies except when it is stopped using GNUNET_HELPER_stop() or when the exp_cb callback is not NULL.

Parameters
with_control_pipedoes the helper support the use of a control pipe for signalling?
binary_namename of the binary to run
binary_argvNULL-terminated list of arguments to give when starting the binary (this argument must not be modified by the client for the lifetime of the helper handle)
cbfunction to call if we get messages from the helper
exp_cbthe exception callback to call. Set this to NULL if the helper process has to be restarted automatically when it dies/crashes
cb_clsclosure for the above callback
Returns
the new Handle, NULL on error

Definition at line 491 of file helper.c.

497 {
498  struct GNUNET_HELPER_Handle *h;
499  unsigned int c;
500 
501  h = GNUNET_new (struct GNUNET_HELPER_Handle);
502  h->with_control_pipe = with_control_pipe;
503  /* Lookup in libexec path only if we are starting gnunet helpers */
504  if (NULL != strstr (binary_name, "gnunet"))
506  else
507  h->binary_name = GNUNET_strdup (binary_name);
508  for (c = 0; NULL != binary_argv[c]; c++)
509  ;
510  h->binary_argv = GNUNET_malloc (sizeof(char *) * (c + 1));
511  for (c = 0; NULL != binary_argv[c]; c++)
512  h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
513  h->binary_argv[c] = NULL;
514  h->cb_cls = cb_cls;
515  if (NULL != cb)
516  h->mst = GNUNET_MST_create (cb, h->cb_cls);
517  h->exp_cb = exp_cb;
518  h->retry_back_off = 0;
519  start_helper (h);
520  return h;
521 }
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
char * GNUNET_OS_get_libexec_binary_path(const char *progname)
Given the name of a gnunet-helper, gnunet-service or gnunet-daemon binary, try to prefix it with the ...
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition: mst.c:85
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
int with_control_pipe
Does the helper support the use of a control pipe for signalling?
Definition: helper.c:158
void * cb_cls
The closure for callbacks.
Definition: helper.c:118
char ** binary_argv
NULL-terminated list of command-line arguments.
Definition: helper.c:138
char * binary_name
Binary to run.
Definition: helper.c:133
static void exp_cb(void *cls)
Callback called if there was an exception during execution of the helper.

References GNUNET_HELPER_Handle::binary_argv, GNUNET_HELPER_Handle::binary_name, GNUNET_HELPER_Handle::cb_cls, exp_cb(), GNUNET_malloc, GNUNET_MST_create(), GNUNET_new, GNUNET_OS_get_libexec_binary_path(), GNUNET_strdup, h, start_helper(), and GNUNET_HELPER_Handle::with_control_pipe.

Referenced by enable(), GNUNET_FS_directory_scan_start(), GNUNET_TESTBED_controller_start(), run(), and start_helper().

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

◆ GNUNET_HELPER_kill()

int GNUNET_HELPER_kill ( struct GNUNET_HELPER_Handle h,
int  soft_kill 
)

Sends termination signal to the helper process.

The helper process is not reaped; call GNUNET_HELPER_wait() for reaping the dead helper process.

Parameters
hthe helper handle
soft_killif GNUNET_YES, signals termination by closing the helper's stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
Returns
GNUNET_OK on success; GNUNET_SYSERR on error

The helper process is not reaped; call GNUNET_HELPER_wait() for reaping the dead helper process.

Parameters
hthe helper handle
soft_killif GNUNET_YES, signals termination by closing the helper's stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
Returns
GNUNET_OK on success; GNUNET_SYSERR on error

Definition at line 177 of file helper.c.

178 {
180  int ret;
181 
182  while (NULL != (sh = h->sh_head))
183  {
184  GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
185  if (NULL != sh->cont)
186  sh->cont (sh->cont_cls, GNUNET_NO);
187  GNUNET_free (sh);
188  }
189  if (NULL != h->restart_task)
190  {
191  GNUNET_SCHEDULER_cancel (h->restart_task);
192  h->restart_task = NULL;
193  }
194  if (NULL != h->read_task)
195  {
196  GNUNET_SCHEDULER_cancel (h->read_task);
197  h->read_task = NULL;
198  }
199  if (NULL == h->helper_proc)
200  return GNUNET_SYSERR;
201  if (GNUNET_YES == soft_kill)
202  {
203  /* soft-kill only possible with pipes */
204  GNUNET_assert (NULL != h->helper_in);
205  ret = GNUNET_DISK_pipe_close (h->helper_in);
206  h->helper_in = NULL;
207  h->fh_to_helper = NULL;
208  return ret;
209  }
210  if (0 != GNUNET_OS_process_kill (h->helper_proc, GNUNET_TERM_SIG))
211  return GNUNET_SYSERR;
212  return GNUNET_OK;
213 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct SolverHandle * sh
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1585
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_free(ptr)
Wrapper around free.
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:224
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
#define GNUNET_TERM_SIG
The termination signal.
Definition: platform.h:233
Entry in the queue of messages we need to transmit to the helper.
Definition: helper.c:37

References GNUNET_assert, GNUNET_CONTAINER_DLL_remove, GNUNET_DISK_pipe_close(), GNUNET_free, GNUNET_NO, GNUNET_OK, GNUNET_OS_process_kill(), GNUNET_SCHEDULER_cancel(), GNUNET_SYSERR, GNUNET_TERM_SIG, GNUNET_YES, h, ret, and sh.

Referenced by cleanup(), disable(), GNUNET_TESTBED_controller_kill_(), and stop_helper().

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

◆ GNUNET_HELPER_wait()

int GNUNET_HELPER_wait ( struct GNUNET_HELPER_Handle h)

Reap the helper process.

This call is blocking (!). The helper process should either be sent a termination signal before or should be dead before calling this function

Parameters
hthe helper handle
Returns
GNUNET_OK on success; GNUNET_SYSERR on error

This call is blocking(!). The helper process should either be sent a termination signal before or should be dead before calling this function

Parameters
hthe helper handle
Returns
GNUNET_OK on success; GNUNET_SYSERR on error

Definition at line 225 of file helper.c.

226 {
228  int ret;
229 
230  ret = GNUNET_SYSERR;
231  if (NULL != h->helper_proc)
232  {
233  ret = GNUNET_OS_process_wait (h->helper_proc);
234  GNUNET_OS_process_destroy (h->helper_proc);
235  h->helper_proc = NULL;
236  }
237  if (NULL != h->read_task)
238  {
239  GNUNET_SCHEDULER_cancel (h->read_task);
240  h->read_task = NULL;
241  }
242  if (NULL != h->write_task)
243  {
244  GNUNET_SCHEDULER_cancel (h->write_task);
245  h->write_task = NULL;
246  }
247  if (NULL != h->helper_in)
248  {
249  GNUNET_DISK_pipe_close (h->helper_in);
250  h->helper_in = NULL;
251  h->fh_to_helper = NULL;
252  }
253  if (NULL != h->helper_out)
254  {
255  GNUNET_DISK_pipe_close (h->helper_out);
256  h->helper_out = NULL;
257  h->fh_from_helper = NULL;
258  }
259  while (NULL != (sh = h->sh_head))
260  {
261  GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
262  if (NULL != sh->cont)
263  sh->cont (sh->cont_cls, GNUNET_NO);
264  GNUNET_free (sh);
265  }
266  /* purge MST buffer */
267  if (NULL != h->mst)
268  (void) GNUNET_MST_from_buffer (h->mst, NULL, 0, GNUNET_YES, GNUNET_NO);
269  return ret;
270 }
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:983
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:286
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

References GNUNET_CONTAINER_DLL_remove, GNUNET_DISK_pipe_close(), GNUNET_free, GNUNET_MST_from_buffer(), GNUNET_NO, GNUNET_OS_process_destroy(), GNUNET_OS_process_wait(), GNUNET_SCHEDULER_cancel(), GNUNET_SYSERR, GNUNET_YES, h, ret, and sh.

Referenced by cleanup(), GNUNET_TESTBED_controller_destroy_(), and stop_helper().

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

◆ GNUNET_HELPER_destroy()

void GNUNET_HELPER_destroy ( struct GNUNET_HELPER_Handle h)

Free's the resources occupied by the helper handle.

Parameters
hthe helper handle to free

Definition at line 530 of file helper.c.

531 {
532  unsigned int c;
534 
535  if (NULL != h->write_task)
536  {
537  GNUNET_SCHEDULER_cancel (h->write_task);
538  h->write_task = NULL;
539  }
540  GNUNET_assert (NULL == h->read_task);
541  GNUNET_assert (NULL == h->restart_task);
542  while (NULL != (sh = h->sh_head))
543  {
544  GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
545  if (NULL != sh->cont)
546  sh->cont (sh->cont_cls, GNUNET_SYSERR);
547  GNUNET_free (sh);
548  }
549  if (NULL != h->mst)
550  GNUNET_MST_destroy (h->mst);
551  GNUNET_free (h->binary_name);
552  for (c = 0; h->binary_argv[c] != NULL; c++)
553  GNUNET_free (h->binary_argv[c]);
554  GNUNET_free (h->binary_argv);
555  GNUNET_free (h);
556 }
void GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst)
Destroys a tokenizer.
Definition: mst.c:411

References GNUNET_assert, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_MST_destroy(), GNUNET_SCHEDULER_cancel(), GNUNET_SYSERR, h, and sh.

Referenced by disable(), GNUNET_HELPER_stop(), and GNUNET_TESTBED_controller_destroy_().

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

◆ GNUNET_HELPER_stop()

void GNUNET_HELPER_stop ( struct GNUNET_HELPER_Handle h,
int  soft_kill 
)

Kills the helper, closes the pipe, frees the handle and calls wait() on the helper process.

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

Kills the helper, closes the pipe, frees the handle and calls wait() on the helper process.

Parameters
hhandle to helper to stop
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 567 of file helper.c.

568 {
569  h->exp_cb = NULL;
570  stop_helper (h, soft_kill);
572 }
void GNUNET_HELPER_destroy(struct GNUNET_HELPER_Handle *h)
Free's the resources occupied by the helper handle.
Definition: helper.c:530
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

References GNUNET_HELPER_destroy(), h, and stop_helper().

Referenced by cleanup(), cleanup_task(), finish_scan(), GNUNET_FS_directory_scan_abort(), helper_read(), helper_write(), LIBGNUNET_PLUGIN_TRANSPORT_DONE(), and stop_testing_system_run().

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

◆ GNUNET_HELPER_send()

struct GNUNET_HELPER_SendHandle* GNUNET_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.

Parameters
hhelper to send message to
msgmessage to send
can_dropcan the message be dropped if there is already one in the queue?
contcontinuation to run once the message is out (GNUNET_OK on success, GNUNET_NO if the helper process died, GNUNET_SYSERR during GNUNET_HELPER_destroy).
cont_clsclosure for cont
Returns
NULL if the message was dropped, otherwise handle to cancel cont (actual transmission may not be abortable)
Parameters
hhelper to send message to
msgmessage to send
can_dropcan the message be dropped if there is already one in the queue?
contcontinuation to run once the message is out (GNUNET_OK on success, GNUNET_NO if the helper process died, GNUNET_SYSERR during GNUNET_HELPER_destroy).
cont_clsclosure for cont
Returns
NULL if the message was dropped, otherwise handle to cancel cont (actual transmission may not be abortable)

Definition at line 657 of file helper.c.

662 {
664  uint16_t mlen;
665 
666  if (NULL == h->fh_to_helper)
667  return NULL;
668  if ((GNUNET_YES == can_drop) && (NULL != h->sh_head))
669  return NULL;
670  mlen = ntohs (msg->size);
671  sh = GNUNET_malloc (sizeof(struct GNUNET_HELPER_SendHandle) + mlen);
672  sh->msg = (const struct GNUNET_MessageHeader *) &sh[1];
673  GNUNET_memcpy (&sh[1], msg, mlen);
674  sh->h = h;
675  sh->cont = cont;
676  sh->cont_cls = cont_cls;
677  GNUNET_CONTAINER_DLL_insert_tail (h->sh_head, h->sh_tail, sh);
678  if (NULL == h->write_task)
679  h->write_task =
681  h->fh_to_helper,
682  &helper_write,
683  h);
684 
685  return sh;
686 }
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
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
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
static void helper_write(void *cls)
Write to the helper-process.
Definition: helper.c:581
Header for all communications.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.

References GNUNET_CONTAINER_DLL_insert_tail, GNUNET_malloc, GNUNET_memcpy, GNUNET_SCHEDULER_add_write_file(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, h, helper_write(), msg, sh, and GNUNET_MessageHeader::size.

Referenced by GNUNET_TESTBED_controller_start(), handle_icmp_back(), handle_tcp_back(), handle_udp_back(), play(), request_done(), send_ack(), send_all_peers_started(), send_hello_beacon(), send_icmp_packet_via_tun(), send_tcp_packet_via_tun(), send_udp_packet_via_tun(), start_helper(), and transmit_fragment().

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

◆ GNUNET_HELPER_send_cancel()

void GNUNET_HELPER_send_cancel ( struct GNUNET_HELPER_SendHandle sh)

Cancel a GNUNET_HELPER_send operation.

If possible, transmitting the message is also aborted, but at least 'cont' won't be called.

Parameters
shoperation to cancel

Definition at line 697 of file helper.c.

698 {
699  struct GNUNET_HELPER_Handle *h = sh->h;
700 
701  sh->cont = NULL;
702  sh->cont_cls = NULL;
703  if (0 == sh->wpos)
704  {
705  GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
706  GNUNET_free (sh);
707  if (NULL == h->sh_head)
708  {
709  GNUNET_SCHEDULER_cancel (h->write_task);
710  h->write_task = NULL;
711  }
712  }
713 }

References GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_SCHEDULER_cancel(), h, and sh.

Referenced by free_fragment_message(), and GNUNET_TESTBED_controller_kill_().

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