GNUnet  0.11.x
Data Structures | Macros | Functions | Variables
child_management.c File Reference
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_child_management_lib.h"
Include dependency graph for child_management.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_ChildWaitHandle
 Struct which defines a Child Wait handle. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log (kind, __VA_ARGS__)
 Generic logging shortcut. More...
 

Functions

static void maint_child_death (void *cls)
 Task triggered whenever we receive a SIGCHLD (child process died) or when user presses CTRL-C. More...
 
static void sighandler_child_death (void)
 Signal handler called for SIGCHLD. More...
 
static void child_management_start ()
 
static void child_management_done ()
 Clean up. More...
 
struct GNUNET_ChildWaitHandleGNUNET_wait_child (struct GNUNET_OS_Process *proc, GNUNET_ChildCompletedCallback cb, void *cb_cls)
 Starts the handling of the child processes. More...
 
void GNUNET_wait_child_cancel (struct GNUNET_ChildWaitHandle *cwh)
 Stop waiting on this child. More...
 

Variables

static struct GNUNET_DISK_PipeHandlesigpipe
 Pipe used to communicate shutdown via signal. More...
 
static struct GNUNET_SIGNAL_Contextshc_chld
 
static struct GNUNET_SCHEDULER_Tasksig_task
 
static struct GNUNET_ChildWaitHandlecwh_head
 
static struct GNUNET_ChildWaitHandlecwh_tail
 

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log (kind, __VA_ARGS__)

Generic logging shortcut.

Definition at line 35 of file child_management.c.

Referenced by GNUNET_wait_child().

Function Documentation

◆ maint_child_death()

static void maint_child_death ( void *  cls)
static

Task triggered whenever we receive a SIGCHLD (child process died) or when user presses CTRL-C.

Parameters
clsclosure, NULL

Definition at line 86 of file child_management.c.

References buf, GNUNET_ChildWaitHandle::cb, GNUNET_ChildWaitHandle::cb_cls, cwh, exit_code, GNUNET_assert, GNUNET_CONTAINER_DLL_remove, GNUNET_DISK_file_read(), GNUNET_DISK_handle_invalid(), GNUNET_DISK_PIPE_END_READ, GNUNET_DISK_pipe_handle(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_OK, GNUNET_OS_process_status(), GNUNET_SCHEDULER_add_read_file(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_ChildWaitHandle::next, GNUNET_ChildWaitHandle::proc, and type.

Referenced by GNUNET_wait_child().

87 {
88  char buf[16];
89  const struct GNUNET_DISK_FileHandle *pr;
90  struct GNUNET_ChildWaitHandle *nxt;
91 
92  (void) cls;
93  sig_task = NULL;
94 
96  "Received SIGCHLD.\n");
97 
98  /* drain pipe */
102 
103  (void) GNUNET_DISK_file_read (pr,
104  buf,
105  sizeof(buf));
106 
107  /* find applicable processes that exited */
108  for (struct GNUNET_ChildWaitHandle *cwh = cwh_head;
109  NULL != cwh;
110  cwh = nxt)
111  {
113  long unsigned int exit_code = 0;
114 
115  nxt = cwh->next;
116  if (GNUNET_OK ==
118  &type,
119  &exit_code))
120  {
122  cwh_tail,
123  cwh);
124  cwh->cb (cwh->cb_cls,
125  type,
126  exit_code);
127  GNUNET_free (cwh);
128  }
129  }
130  if (NULL == cwh_head)
131  return;
132  /* wait for more */
138  NULL);
139 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static struct GNUNET_DISK_PipeHandle * sigpipe
Pipe used to communicate shutdown via signal.
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
int GNUNET_OS_process_status(struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Retrieve the status of a process.
Definition: os_priority.c:946
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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
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:1615
struct GNUNET_ChildWaitHandle * cwh
struct GNUNET_OS_Process * proc
Child process which is managed.
void * cb_cls
Closure for the handle.
GNUNET_OS_ProcessStatusType
Process status types.
static char buf[2048]
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
The reading-end of a pipe.
static void maint_child_death(void *cls)
Task triggered whenever we receive a SIGCHLD (child process died) or when user presses CTRL-C...
struct GNUNET_ChildWaitHandle * next
Linked list to the next child.
GNUNET_ChildCompletedCallback cb
Callback which is called upon completion/death of the child task.
static struct GNUNET_ChildWaitHandle * cwh_tail
enum GNUNET_GenericReturnValue GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h)
Checks whether a handle is invalid.
Definition: disk.c:186
static struct GNUNET_ChildWaitHandle * cwh_head
#define GNUNET_log(kind,...)
static struct GNUNET_SCHEDULER_Task * sig_task
Struct which defines a Child Wait handle.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Handle used to access files (and pipes).
#define GNUNET_free(ptr)
Wrapper around free.
static long unsigned int exit_code
Handler exit code.
Definition: gnunet-qr.c:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sighandler_child_death()

static void sighandler_child_death ( void  )
static

Signal handler called for SIGCHLD.

Triggers the respective handler by writing to the trigger pipe.

Definition at line 147 of file child_management.c.

References GNUNET_break, GNUNET_DISK_file_write(), GNUNET_DISK_PIPE_END_WRITE, and GNUNET_DISK_pipe_handle().

Referenced by child_management_start().

148 {
149  static char c;
150  int old_errno = errno; /* back-up errno */
151 
152  GNUNET_break (
153  1 ==
156  &c,
157  sizeof(c)));
158  errno = old_errno; /* restore errno */
159 }
static struct GNUNET_DISK_PipeHandle * sigpipe
Pipe used to communicate shutdown via signal.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
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:1615
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
The writing-end of a pipe.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ child_management_start()

static void child_management_start ( )
static

Definition at line 164 of file child_management.c.

References GNUNET_assert, GNUNET_DISK_PF_NONE, GNUNET_DISK_pipe(), GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_SIGCHLD, GNUNET_SIGNAL_handler_install(), and sighandler_child_death().

Referenced by GNUNET_wait_child().

165 {
167  "Trying to start child management.\n");
168  if (NULL != sigpipe)
169  return; /* already initialized */
171  GNUNET_assert (sigpipe != NULL);
172  shc_chld =
175  "Child management started.\n");
176 }
static struct GNUNET_SIGNAL_Context * shc_chld
No special options, use non-blocking read/write operations.
static struct GNUNET_DISK_PipeHandle * sigpipe
Pipe used to communicate shutdown via signal.
#define GNUNET_SIGCHLD
Definition: platform.h:42
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void sighandler_child_death(void)
Signal handler called for SIGCHLD.
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(enum GNUNET_DISK_PipeFlags pf)
Creates an interprocess channel.
Definition: disk.c:1441
struct GNUNET_SIGNAL_Context * GNUNET_SIGNAL_handler_install(int signal, GNUNET_SIGNAL_Handler handler)
Install a signal handler that will be run if the given signal is received.
Definition: signal.c:51
#define GNUNET_log(kind,...)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ child_management_done()

static void child_management_done ( )
static

Clean up.

Definition at line 183 of file child_management.c.

References GNUNET_assert, GNUNET_DISK_pipe_close(), GNUNET_ERROR_TYPE_INFO, GNUNET_log, and GNUNET_SIGNAL_handler_uninstall().

Referenced by GNUNET_wait_child_cancel().

184 {
185  GNUNET_assert (NULL == sig_task);
187  shc_chld = NULL;
189  sigpipe = NULL;
191  "Child management stopped.\n");
192 }
static struct GNUNET_SIGNAL_Context * shc_chld
static struct GNUNET_DISK_PipeHandle * sigpipe
Pipe used to communicate shutdown via signal.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_SIGNAL_handler_uninstall(struct GNUNET_SIGNAL_Context *ctx)
Uninstall a previously installed signal handler.
Definition: signal.c:77
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1584
#define GNUNET_log(kind,...)
static struct GNUNET_SCHEDULER_Task * sig_task
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_wait_child()

struct GNUNET_ChildWaitHandle* GNUNET_wait_child ( struct GNUNET_OS_Process proc,
GNUNET_ChildCompletedCallback  cb,
void *  cb_cls 
)

Starts the handling of the child processes.

Function checks the status of the child process and sends back a GNUNET_ChildCompletedCallback upon completion/death of the child.

Parameters
procchild process which is monitored
cbreference to the callback which is called after completion
cb_clsclosure for the callback
Returns
GNUNET_ChildWaitHandle is returned

Definition at line 195 of file child_management.c.

References GNUNET_ChildWaitHandle::cb, GNUNET_ChildWaitHandle::cb_cls, child_management_start(), cwh, GNUNET_CONTAINER_DLL_insert, GNUNET_DISK_PIPE_END_READ, GNUNET_DISK_pipe_handle(), GNUNET_ERROR_TYPE_ERROR, GNUNET_new, GNUNET_SCHEDULER_add_read_file(), GNUNET_TIME_UNIT_FOREVER_REL, LOG, maint_child_death(), and GNUNET_ChildWaitHandle::proc.

Referenced by netjail_start_run(), and netjail_stop_run().

198 {
199  struct GNUNET_ChildWaitHandle *cwh;
200 
202  "Adding child!\n");
203 
205  cwh = GNUNET_new (struct GNUNET_ChildWaitHandle);
206  cwh->proc = proc;
207  cwh->cb = cb;
208  cwh->cb_cls = cb_cls;
210  cwh_tail,
211  cwh);
212  if (NULL == sig_task)
213  {
219  NULL);
220  }
221  return cwh;
222 }
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
static struct GNUNET_DISK_PipeHandle * sigpipe
Pipe used to communicate shutdown via signal.
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
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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:1615
struct GNUNET_ChildWaitHandle * cwh
struct GNUNET_OS_Process * proc
Child process which is managed.
void * cb_cls
Closure for the handle.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
The reading-end of a pipe.
static void maint_child_death(void *cls)
Task triggered whenever we receive a SIGCHLD (child process died) or when user presses CTRL-C...
GNUNET_ChildCompletedCallback cb
Callback which is called upon completion/death of the child task.
static struct GNUNET_ChildWaitHandle * cwh_tail
static void child_management_start()
static struct GNUNET_ChildWaitHandle * cwh_head
static struct GNUNET_SCHEDULER_Task * sig_task
Struct which defines a Child Wait handle.
#define LOG(kind,...)
Generic logging shortcut.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_wait_child_cancel()

void GNUNET_wait_child_cancel ( struct GNUNET_ChildWaitHandle cwh)

Stop waiting on this child.

Definition at line 225 of file child_management.c.

References child_management_done(), GNUNET_CONTAINER_DLL_remove, GNUNET_free, and GNUNET_SCHEDULER_cancel().

Referenced by netjail_start_cleanup(), and netjail_stop_cleanup().

226 {
227  if ((NULL != cwh_head))
228  {
230  cwh_tail,
231  cwh);
232  }
233  if (NULL == cwh_head)
234  {
236  }
237  if (NULL != sig_task)
238  {
240  sig_task = NULL;
241  }
242  GNUNET_free (cwh);
243 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static void child_management_done()
Clean up.
static struct GNUNET_ChildWaitHandle * cwh_tail
static struct GNUNET_ChildWaitHandle * cwh_head
static struct GNUNET_SCHEDULER_Task * sig_task
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ sigpipe

struct GNUNET_DISK_PipeHandle* sigpipe
static

Pipe used to communicate shutdown via signal.

Definition at line 69 of file child_management.c.

◆ shc_chld

struct GNUNET_SIGNAL_Context* shc_chld
static

Definition at line 71 of file child_management.c.

Referenced by main().

◆ sig_task

struct GNUNET_SCHEDULER_Task* sig_task
static

Definition at line 73 of file child_management.c.

◆ cwh_head

struct GNUNET_ChildWaitHandle* cwh_head
static

Definition at line 75 of file child_management.c.

◆ cwh_tail

struct GNUNET_ChildWaitHandle* cwh_tail
static

Definition at line 77 of file child_management.c.