GNUnet  0.17.6
child_management.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2021 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
31 
35 #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
36 
37 
42 {
62  void *cb_cls;
63 };
64 
65 
70 
72 
74 
76 
78 
85 static void
86 maint_child_death (void *cls)
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  /* drain pipe */
98  (void) GNUNET_DISK_file_read (pr,
99  buf,
100  sizeof(buf));
101 
102  /* find applicable processes that exited */
103  for (struct GNUNET_ChildWaitHandle *cwh = cwh_head;
104  NULL != cwh;
105  cwh = nxt)
106  {
108  long unsigned int exit_code = 0;
109 
110  nxt = cwh->next;
111  if (GNUNET_OK ==
112  GNUNET_OS_process_status (cwh->proc,
113  &type,
114  &exit_code))
115  {
117  cwh_tail,
118  cwh);
119  cwh->cb (cwh->cb_cls,
120  type,
121  exit_code);
122  GNUNET_free (cwh);
123  }
124  }
125  if (NULL == cwh_head)
126  return;
127  /* wait for more */
133  NULL);
134 }
135 
136 
141 static void
143 {
144  static char c;
145  int old_errno = errno; /* back-up errno */
146 
147  GNUNET_break (
148  1 ==
151  &c,
152  sizeof(c)));
153  errno = old_errno; /* restore errno */
154 }
155 
159 static void
161 {
163  "Trying to start child management.\n");
164  if (NULL != sigpipe)
165  return; /* already initialized */
167  GNUNET_assert (sigpipe != NULL);
168  shc_chld =
172  "Child management started.\n");
173 }
174 
175 
179 static void
181 {
182  if (NULL != sig_task)
183  {
185  sig_task = NULL;
186  }
188  shc_chld = NULL;
190  sigpipe = NULL;
192  "Child management stopped.\n");
193 }
194 
195 
204 struct GNUNET_ChildWaitHandle *
207  void *cb_cls)
208 {
209  struct GNUNET_ChildWaitHandle *cwh;
210 
212  cwh = GNUNET_new (struct GNUNET_ChildWaitHandle);
213  cwh->proc = proc;
214  cwh->cb = cb;
215  cwh->cb_cls = cb_cls;
217  cwh_tail,
218  cwh);
219  if (NULL == sig_task)
220  {
226  NULL);
227  }
228  return cwh;
229 }
230 
231 
237 void
239 {
241  cwh_tail,
242  cwh);
243  GNUNET_free (cwh);
244  if (NULL != cwh_head)
245  return;
247 }
static void sighandler_child_death(void)
Signal handler called for SIGCHLD.
static void child_management_start(void)
Initializing the signal pipe for child handling.
static void child_management_done(void)
Clean up.
static struct GNUNET_ChildWaitHandle * cwh_tail
static void maint_child_death(void *cls)
Task triggered whenever we receive a SIGCHLD (child process died) or when user presses CTRL-C.
static struct GNUNET_SIGNAL_Context * shc_chld
static struct GNUNET_DISK_PipeHandle * sigpipe
Pipe used to communicate shutdown via signal.
static struct GNUNET_ChildWaitHandle * cwh_head
static struct GNUNET_SCHEDULER_Task * sig_task
static int exit_code
Global exit code.
Definition: gnunet-qr.c:42
static char buf[2048]
GNUnet child management api.
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
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1585
enum GNUNET_GenericReturnValue GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h)
Checks whether a handle is invalid.
Definition: disk.c:186
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
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
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(enum GNUNET_DISK_PipeFlags pf)
Creates an interprocess channel.
Definition: disk.c:1442
@ GNUNET_DISK_PF_NONE
No special options, use non-blocking read/write operations.
@ GNUNET_DISK_PIPE_END_WRITE
The writing-end of a pipe.
@ GNUNET_DISK_PIPE_END_READ
The reading-end of a pipe.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_log(kind,...)
void GNUNET_wait_child_cancel(struct GNUNET_ChildWaitHandle *cwh)
Removing child handle.
void(* GNUNET_ChildCompletedCallback)(void *cls, enum GNUNET_OS_ProcessStatusType type, long unsigned int exit_code)
Defines a GNUNET_ChildCompletedCallback which is sent back upon death or completion of a child proces...
struct GNUNET_ChildWaitHandle * GNUNET_wait_child(struct GNUNET_OS_Process *proc, GNUNET_ChildCompletedCallback cb, void *cb_cls)
Adding a child process to be monitored by the child management.
@ GNUNET_OK
Definition: gnunet_common.h:99
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
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:944
GNUNET_OS_ProcessStatusType
Process status types.
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:1652
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:957
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
void GNUNET_SIGNAL_handler_uninstall(struct GNUNET_SIGNAL_Context *ctx)
Uninstall a previously installed signal handler.
Definition: signal.c:77
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_SIGCHLD
Definition: platform.h:42
Struct which defines a Child Wait handle.
struct GNUNET_ChildWaitHandle * prev
Linked list to the previous child.
GNUNET_ChildCompletedCallback cb
Callback which is called upon completion/death of the child task.
void * cb_cls
Closure for the handle.
struct GNUNET_ChildWaitHandle * next
Linked list to the next child.
struct GNUNET_OS_Process * proc
Child process which is managed.
Handle used to access files (and pipes).
Handle used to manage a pipe.
Definition: disk.c:69
Entry in list of pending tasks.
Definition: scheduler.c:135
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model