GNUnet 0.26.2-44-g5a6b2f9a5
 
Loading...
Searching...
No Matches
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 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

enum GNUNET_GenericReturnValue GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill)
 Sends termination signal to the helper process.
 
enum GNUNET_GenericReturnValue GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
 Reap the helper process.
 
static void stop_helper (struct GNUNET_HELPER_Handle *h, int soft_kill)
 Stop the helper process, we're closing down or had an error.
 
static void restart_task (void *cls)
 Restart the helper process.
 
static void helper_read (void *cls)
 Read from the helper-process.
 
static void start_helper (struct GNUNET_HELPER_Handle *h)
 Start the helper process.
 
struct GNUNET_HELPER_HandleGNUNET_HELPER_start (const struct GNUNET_OS_ProjectData *pd, 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.
 
void GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
 Free's the resources occupied by the helper handle.
 
void GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, int soft_kill)
 Kills the helper, closes the pipe and frees the handle.
 
static void helper_write (void *cls)
 Write to the helper-process.
 
struct GNUNET_HELPER_SendHandleGNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h, const struct GNUNET_MessageHeader *msg, bool can_drop, GNUNET_HELPER_Continuation cont, void *cont_cls)
 Send an message to the helper.
 
void GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
 Cancel a GNUNET_HELPER_send operation.
 

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 274 of file helper.c.

276{
277 if (NULL != h->restart_task)
278 {
279 GNUNET_SCHEDULER_cancel (h->restart_task);
280 h->restart_task = NULL;
281 }
282 else
283 {
286 soft_kill));
289 }
290}
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition gnunet-arm.c:98
enum GNUNET_GenericReturnValue GNUNET_HELPER_wait(struct GNUNET_HELPER_Handle *h)
Reap the helper process.
Definition helper.c:208
enum GNUNET_GenericReturnValue GNUNET_HELPER_kill(struct GNUNET_HELPER_Handle *h, int soft_kill)
Sends termination signal to the helper process.
Definition helper.c:166
@ GNUNET_OK
#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:986

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 474 of file helper.c.

475{
476 struct GNUNET_HELPER_Handle *h = cls;
477
478 h->restart_task = NULL;
479 h->retry_back_off++;
481 "Restarting helper with back-off %u\n",
482 h->retry_back_off);
483 start_helper (h);
484}
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_INFO
static void start_helper(struct GNUNET_HELPER_Handle *h)
Start the helper process.
Definition helper.c:398
The handle to a helper process.
Definition helper.c:77

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 308 of file helper.c.

309{
310 struct GNUNET_HELPER_Handle *h = cls;
312 ssize_t t;
313
314 h->read_task = NULL;
315 t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof(buf));
316 if (t < 0)
317 {
318 /* On read-error, restart the helper */
320 _ ("Error reading from `%s': %s\n"),
321 h->binary_name,
322 strerror (errno));
323 if (NULL != h->exp_cb)
324 {
325 h->exp_cb (h->cb_cls);
327 return;
328 }
330 /* Restart the helper */
331 h->restart_task = GNUNET_SCHEDULER_add_delayed (
333 h->retry_back_off),
335 h);
336 return;
337 }
338 if (0 == t)
339 {
340 /* this happens if the helper is shut down via a
341 signal, so it is not a "hard" error */
343 "Got 0 bytes from helper `%s' (EOF)\n",
344 h->binary_name);
345 if (NULL != h->exp_cb)
346 {
347 h->exp_cb (h->cb_cls);
349 return;
350 }
352 /* Restart the helper */
353 h->restart_task = GNUNET_SCHEDULER_add_delayed (
355 h->retry_back_off),
357 h);
358 return;
359 }
361 "Got %u bytes from helper `%s'\n",
362 (unsigned int) t,
363 h->binary_name);
365 h->fh_from_helper,
367 h);
368 if (GNUNET_SYSERR ==
370 {
372 _ ("Failed to parse inbound message from helper `%s'\n"),
373 h->binary_name);
374 if (NULL != h->exp_cb)
375 {
376 h->exp_cb (h->cb_cls);
378 return;
379 }
381 /* Restart the helper */
382 h->restart_task = GNUNET_SCHEDULER_add_delayed (
384 h->retry_back_off),
386 h);
387 return;
388 }
389}
static struct GNUNET_SCHEDULER_Task * t
Main task.
#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:704
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:566
#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
@ GNUNET_SYSERR
@ 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:1283
enum GNUNET_GenericReturnValue 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:101
#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:486
static void restart_task(void *cls)
Restart the helper process.
Definition helper.c:474
static void helper_read(void *cls)
Read from the helper-process.
Definition helper.c:308
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:274
#define _(String)
GNU gettext support macro.
Definition platform.h:179

References _, 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, helper_read(), restart_task(), stop_helper(), and t.

Referenced by helper_read(), and 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 398 of file helper.c.

399{
400 h->helper_in =
402 h->helper_out =
404 if ((h->helper_in == NULL) || (h->helper_out == NULL))
405 {
406 /* out of file descriptors? try again later... */
408 "out of file descriptors? try again later\n");
410 h->restart_task = GNUNET_SCHEDULER_add_delayed (
412 h->retry_back_off),
414 h);
415 return;
416 }
418 "Starting HELPER process `%s'\n",
419 h->binary_name);
420 h->fh_from_helper =
422 h->fh_to_helper =
424 h->helper_proc = GNUNET_process_create ();
427 h->helper_proc,
429 h->with_control_pipe
434 STDIN_FILENO),
436 STDOUT_FILENO)));
437 if ( (GNUNET_OK !=
439 h->binary_name,
440 (const char **) h->binary_argv)) ||
441 (GNUNET_OK !=
442 GNUNET_process_start (h->helper_proc)) )
443 {
444 /* failed to start process? try again later... */
446 "failed to start process? try again later\n");
447 stop_helper (h,
448 GNUNET_NO);
449 h->restart_task = GNUNET_SCHEDULER_add_delayed (
451 h->retry_back_off),
453 h);
454 return;
455 }
456 GNUNET_DISK_pipe_close_end (h->helper_out,
458 GNUNET_DISK_pipe_close_end (h->helper_in,
460 if (NULL != h->mst)
462 h->fh_from_helper,
464 h);
465}
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:1703
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(enum GNUNET_DISK_PipeFlags pf)
Creates an interprocess channel.
Definition disk.c:1524
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:1618
@ 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.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_Process * GNUNET_process_create(void)
Create a process handle.
Definition os_process.c:462
#define GNUNET_process_option_std_inheritance(flags)
Set flags about standard inheritance options.
enum GNUNET_GenericReturnValue GNUNET_process_start(struct GNUNET_Process *proc)
Start a process.
Definition os_process.c:545
#define GNUNET_process_set_options(proc,...)
Set the requested options for the process.
#define GNUNET_process_option_inherit_rpipe(rpipe, child_fd)
Have child process inherit a pipe for reading.
enum GNUNET_GenericReturnValue GNUNET_process_set_command_argv(struct GNUNET_Process *p, const char *filename, const char **argv)
Set the command to start a process.
Definition os_process.c:840
#define GNUNET_process_option_inherit_wpipe(wpipe, child_fd)
Have child process inherit a pipe for writing.
@ GNUNET_OS_INHERIT_STD_ERR
When this flag is set, the child process will inherit stderr of the parent.
@ GNUNET_OS_USE_PIPE_CONTROL
Should a pipe be used to send signals to the child?

References GNUNET_assert, 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_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_OS_INHERIT_STD_ERR, GNUNET_OS_USE_PIPE_CONTROL, GNUNET_process_create(), GNUNET_process_option_inherit_rpipe, GNUNET_process_option_inherit_wpipe, GNUNET_process_option_std_inheritance, GNUNET_process_set_command_argv(), GNUNET_process_set_options, GNUNET_process_start(), 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 580 of file helper.c.

581{
582 struct GNUNET_HELPER_Handle *h = cls;
584 const char *buf;
585 ssize_t t;
586
587 h->write_task = NULL;
588 if (NULL == (sh = h->sh_head))
589 {
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Helper write had no work!\n");
591 return; /* how did this happen? */
592 }
593 buf = (const char *) sh->msg;
594 t = GNUNET_DISK_file_write (h->fh_to_helper,
595 &buf[sh->wpos],
596 ntohs (sh->msg->size) - sh->wpos);
597 if (-1 == t)
598 {
599 /* On write-error, restart the helper */
601 _ ("Error writing to `%s': %s\n"),
602 h->binary_name,
603 strerror (errno));
604 if (NULL != h->exp_cb)
605 {
606 h->exp_cb (h->cb_cls);
608 return;
609 }
611 "Stopping and restarting helper task!\n");
613 /* Restart the helper */
614 h->restart_task = GNUNET_SCHEDULER_add_delayed (
616 h->retry_back_off),
618 h);
619 return;
620 }
622 "Transmitted %u bytes to %s\n",
623 (unsigned int) t,
624 h->binary_name);
625 sh->wpos += t;
626 if (sh->wpos == ntohs (sh->msg->size))
627 {
628 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
629 if (NULL != sh->cont)
630 sh->cont (sh->cont_cls, GNUNET_YES);
631 GNUNET_free (sh);
632 }
633 if (NULL != h->sh_head)
634 h->write_task =
636 h->fh_to_helper,
638 h);
639}
static struct GNUNET_IDENTITY_Handle * sh
Handle to IDENTITY service.
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:745
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
@ GNUNET_YES
#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:580
Entry in the queue of messages we need to transmit to the helper.
Definition helper.c:35

References _, 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, helper_write(), restart_task(), sh, stop_helper(), and t.

Referenced by GNUNET_HELPER_send(), and helper_write().

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