GNUnet 0.27.0
 
Loading...
Searching...
No Matches
Helper library

Dealing with SUID helper processes. More...

Collaboration diagram for Helper library:

Typedefs

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

Functions

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

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 64 of file gnunet_helper_lib.h.

◆ GNUNET_HELPER_Continuation

typedef void(* GNUNET_HELPER_Continuation) (void *cls, enum GNUNET_GenericReturnValue result)

Continuation function.

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

Definition at line 151 of file gnunet_helper_lib.h.

Function Documentation

◆ GNUNET_HELPER_start()

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

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
pdproject data to use to determine paths
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

Definition at line 484 of file helper.c.

491{
492 struct GNUNET_HELPER_Handle *h;
493 unsigned int c;
494
496 h->with_control_pipe = with_control_pipe;
497 /* Lookup in libexec path only if we are starting gnunet helpers */
498 if (NULL != strstr (binary_name, "gnunet"))
499 h->binary_name = GNUNET_OS_get_libexec_binary_path (pd,
501 else
502 h->binary_name = GNUNET_strdup (binary_name);
503 for (c = 0; NULL != binary_argv[c]; c++)
504 ;
505 h->binary_argv = GNUNET_malloc (sizeof(char *) * (c + 1));
506 for (c = 0; NULL != binary_argv[c]; c++)
507 h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
508 h->binary_argv[c] = NULL;
509 h->cb_cls = cb_cls;
510 if (NULL != cb)
511 h->mst = GNUNET_MST_create (cb, h->cb_cls);
512 h->exp_cb = exp_cb;
513 h->retry_back_off = 0;
514 start_helper (h);
515 return h;
516}
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition gnunet-arm.c:98
#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 struct GNUNET_OS_ProjectData *pd, 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:86
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
int with_control_pipe
Does the helper support the use of a control pipe for signalling?
Definition helper.c:156
void * cb_cls
The closure for callbacks.
Definition helper.c:116
char ** binary_argv
NULL-terminated list of command-line arguments.
Definition helper.c:136
char * binary_name
Binary to run.
Definition helper.c:131
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(), enable(), GNUNET_FS_directory_scan_start(), run(), run(), run(), and start_helper().

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

◆ GNUNET_HELPER_kill()

enum GNUNET_GenericReturnValue 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

Definition at line 166 of file helper.c.

167{
169 int ret;
170
171 while (NULL != (sh = h->sh_head))
172 {
173 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
174 if (NULL != sh->cont)
175 sh->cont (sh->cont_cls, GNUNET_NO);
176 GNUNET_free (sh);
177 }
178 if (NULL != h->restart_task)
179 {
180 GNUNET_SCHEDULER_cancel (h->restart_task);
181 h->restart_task = NULL;
182 }
183 if (NULL != h->read_task)
184 {
185 GNUNET_SCHEDULER_cancel (h->read_task);
186 h->read_task = NULL;
187 }
188 if (NULL == h->helper_proc)
189 return GNUNET_SYSERR;
190 if (GNUNET_YES == soft_kill)
191 {
192 /* soft-kill only possible with pipes */
193 GNUNET_assert (NULL != h->helper_in);
194 ret = GNUNET_DISK_pipe_close (h->helper_in);
195 h->helper_in = NULL;
196 h->fh_to_helper = NULL;
197 return ret;
198 }
199 if (GNUNET_OK !=
200 GNUNET_process_kill (h->helper_proc,
202 return GNUNET_SYSERR;
203 return GNUNET_OK;
204}
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_IDENTITY_Handle * sh
Handle to IDENTITY service.
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition disk.c:1671
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_process_kill(struct GNUNET_Process *proc, int sig)
Sends a signal to the process.
Definition os_process.c:307
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition scheduler.c:986
#define GNUNET_TERM_SIG
The termination signal.
Definition platform.h:235
Entry in the queue of messages we need to transmit to the helper.
Definition helper.c:35

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

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

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

◆ GNUNET_HELPER_wait()

enum GNUNET_GenericReturnValue 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

Definition at line 208 of file helper.c.

209{
212
214 if (NULL != h->helper_proc)
215 {
216 ret = GNUNET_process_wait (h->helper_proc,
217 true,
218 NULL,
219 NULL);
220 GNUNET_process_destroy (h->helper_proc);
221 h->helper_proc = NULL;
222 }
223 if (NULL != h->read_task)
224 {
225 GNUNET_SCHEDULER_cancel (h->read_task);
226 h->read_task = NULL;
227 }
228 if (NULL != h->write_task)
229 {
230 GNUNET_SCHEDULER_cancel (h->write_task);
231 h->write_task = NULL;
232 }
233 if (NULL != h->helper_in)
234 {
235 GNUNET_DISK_pipe_close (h->helper_in);
236 h->helper_in = NULL;
237 h->fh_to_helper = NULL;
238 }
239 if (NULL != h->helper_out)
240 {
241 GNUNET_DISK_pipe_close (h->helper_out);
242 h->helper_out = NULL;
243 h->fh_from_helper = NULL;
244 }
245 while (NULL != (sh = h->sh_head))
246 {
248 h->sh_tail,
249 sh);
250 if (NULL != sh->cont)
251 sh->cont (sh->cont_cls,
252 GNUNET_NO);
253 GNUNET_free (sh);
254 }
255 /* purge MST buffer */
256 if (NULL != h->mst)
257 (void) GNUNET_MST_from_buffer (h->mst,
258 NULL,
259 0,
261 GNUNET_NO);
262 return ret;
263}
GNUNET_GenericReturnValue
Named constants for return values.
enum GNUNET_GenericReturnValue GNUNET_process_wait(struct GNUNET_Process *proc, bool blocking, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Wait for a process to terminate.
void GNUNET_process_destroy(struct GNUNET_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition os_process.c:363
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

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

Referenced by cleanup(), 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 525 of file helper.c.

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

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

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

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

563{
564 h->exp_cb = NULL;
565 stop_helper (h, soft_kill);
567}
void GNUNET_HELPER_destroy(struct GNUNET_HELPER_Handle *h)
Free's the resources occupied by the helper handle.
Definition helper.c:525
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

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

Referenced by cleanup(), cleanup_task(), finish_scan(), GNUNET_FS_directory_scan_abort(), handle_helper_local_finished(), helper_read(), helper_write(), and netjail_exec_cleanup().

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,
bool  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)

Definition at line 639 of file helper.c.

644{
646 uint16_t mlen;
647
648 if (NULL == h->fh_to_helper)
649 return NULL;
650 if (can_drop && (NULL != h->sh_head))
651 return NULL;
652 mlen = ntohs (msg->size);
653 sh = GNUNET_malloc (sizeof(struct GNUNET_HELPER_SendHandle) + mlen);
654 sh->msg = (const struct GNUNET_MessageHeader *) &sh[1];
655 GNUNET_memcpy (&sh[1], msg, mlen);
656 sh->h = h;
657 sh->cont = cont;
658 sh->cont_cls = cont_cls;
659 GNUNET_CONTAINER_DLL_insert_tail (h->sh_head, h->sh_tail, sh);
660 if (NULL == h->write_task)
661 h->write_task =
663 h->fh_to_helper,
665 h);
666
667 return sh;
668}
struct GNUNET_MessageHeader * msg
Definition 005.c:2
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
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:576
Header for all communications.

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

Referenced by GNUNET_TESTING_loop_notify_children_(), handle_icmp_back(), handle_tcp_back(), handle_udp_back(), play(), request_done(), send_icmp_packet_via_tun(), send_start_messages(), send_tcp_packet_via_tun(), and send_udp_packet_via_tun().

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

680{
681 struct GNUNET_HELPER_Handle *h = sh->h;
682
683 sh->cont = NULL;
684 sh->cont_cls = NULL;
685 if (0 == sh->wpos)
686 {
687 GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
688 GNUNET_free (sh);
689 if (NULL == h->sh_head)
690 {
691 GNUNET_SCHEDULER_cancel (h->write_task);
692 h->write_task = NULL;
693 }
694 }
695}

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

Referenced by finish_test().

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