aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/include/gnunet_child_management_lib.h72
-rw-r--r--src/util/child_management.c219
2 files changed, 291 insertions, 0 deletions
diff --git a/src/include/gnunet_child_management_lib.h b/src/include/gnunet_child_management_lib.h
new file mode 100644
index 000000000..465f71f0e
--- /dev/null
+++ b/src/include/gnunet_child_management_lib.h
@@ -0,0 +1,72 @@
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
21/**
22 * @file include/anastasis_util_lib.h
23 * @brief GNUnet child management api
24 * @author Christian Grothoff
25 * @author Dominik Meister
26 * @author Dennis Neufeld
27 * @author t3sserakt
28 */
29#ifndef GNUNET_CHILD_MANAGEMENT_LIB_H
30#define GNUNET_CHILD_MANAGEMENT_LIB_H
31
32/**
33 * Handle for the child management
34 */
35struct GNUNET_ChildWaitHandle;
36
37/**
38 * Defines a GNUNET_ChildCompletedCallback which is sent back
39 * upon death or completion of a child process.
40 *
41 * @param cls handle for the callback
42 * @param type type of the process
43 * @param exit_code status code of the process
44 *
45*/
46typedef void
47(*GNUNET_ChildCompletedCallback)(void *cls,
48 enum GNUNET_OS_ProcessStatusType type,
49 long unsigned int exit_code);
50
51/**
52 * Starts the handling of the child processes.
53 * Function checks the status of the child process and sends back a
54 * GNUNET_ChildCompletedCallback upon completion/death of the child.
55 *
56 * @param proc child process which is monitored
57 * @param cb reference to the callback which is called after completion
58 * @param cb_cls closure for the callback
59 * @return GNUNET_ChildWaitHandle is returned
60 */
61struct GNUNET_ChildWaitHandle *
62GNUNET_wait_child (struct GNUNET_OS_Process *proc,
63 GNUNET_ChildCompletedCallback cb,
64 void *cb_cls);
65
66/**
67 * Stop waiting on this child.
68 */
69void
70GNUNET_wait_child_cancel (struct GNUNET_ChildWaitHandle *cwh);
71
72#endif
diff --git a/src/util/child_management.c b/src/util/child_management.c
new file mode 100644
index 000000000..7edc33dc1
--- /dev/null
+++ b/src/util/child_management.c
@@ -0,0 +1,219 @@
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
21/**
22 * @file testing/child_management.c
23 * @brief Handling of child processes in GNUnet.
24 * @author Christian Grothoff (ANASTASIS)
25 * @author Dominik Meister (ANASTASIS)
26 * @author t3sserakt
27 */
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_child_management_lib.h"
31
32
33/**
34 * Struct which defines a Child Wait handle
35 */
36struct GNUNET_ChildWaitHandle
37{
38 /**
39 * Linked list to the next child
40 */
41 struct GNUNET_ChildWaitHandle *next;
42 /**
43 * Linked list to the previous child
44 */
45 struct GNUNET_ChildWaitHandle *prev;
46 /**
47 * Child process which is managed
48 */
49 struct GNUNET_OS_Process *proc;
50 /**
51 * Callback which is called upon completion/death of the child task
52 */
53 GNUNET_ChildCompletedCallback cb;
54 /**
55 * Closure for the handle
56 */
57 void *cb_cls;
58};
59
60
61/**
62 * Pipe used to communicate shutdown via signal.
63 */
64static struct GNUNET_DISK_PipeHandle *sigpipe;
65
66static struct GNUNET_SIGNAL_Context *shc_chld;
67
68static struct GNUNET_SCHEDULER_Task *sig_task;
69
70static struct GNUNET_ChildWaitHandle *cwh_head;
71
72static struct GNUNET_ChildWaitHandle *cwh_tail;
73
74/**
75 * Task triggered whenever we receive a SIGCHLD (child
76 * process died) or when user presses CTRL-C.
77 *
78 * @param cls closure, NULL
79 */
80static void
81maint_child_death (void *cls)
82{
83 char buf[16];
84 const struct GNUNET_DISK_FileHandle *pr;
85 struct GNUNET_ChildWaitHandle *nxt;
86
87 (void) cls;
88 sig_task = NULL;
89 /* drain pipe */
90 pr = GNUNET_DISK_pipe_handle (sigpipe,
91 GNUNET_DISK_PIPE_END_READ);
92 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
93
94 (void) GNUNET_DISK_file_read (pr,
95 buf,
96 sizeof(buf));
97
98 /* find applicable processes that exited */
99 for (struct GNUNET_ChildWaitHandle *cwh = cwh_head;
100 NULL != cwh;
101 cwh = nxt)
102 {
103 enum GNUNET_OS_ProcessStatusType type;
104 long unsigned int exit_code = 0;
105
106 nxt = cwh->next;
107 if (GNUNET_OK ==
108 GNUNET_OS_process_status (cwh->proc,
109 &type,
110 &exit_code))
111 {
112 GNUNET_CONTAINER_DLL_remove (cwh_head,
113 cwh_tail,
114 cwh);
115 cwh->cb (cwh->cb_cls,
116 type,
117 exit_code);
118 GNUNET_free (cwh);
119 }
120 }
121 if (NULL == cwh_head)
122 return;
123 /* wait for more */
124 sig_task = GNUNET_SCHEDULER_add_read_file (
125 GNUNET_TIME_UNIT_FOREVER_REL,
126 GNUNET_DISK_pipe_handle (sigpipe,
127 GNUNET_DISK_PIPE_END_READ),
128 &maint_child_death,
129 NULL);
130}
131
132
133/**
134 * Signal handler called for SIGCHLD. Triggers the
135 * respective handler by writing to the trigger pipe.
136 */
137static void
138sighandler_child_death (void)
139{
140 static char c;
141 int old_errno = errno; /* back-up errno */
142
143 GNUNET_break (
144 1 ==
145 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
146 GNUNET_DISK_PIPE_END_WRITE),
147 &c,
148 sizeof(c)));
149 errno = old_errno; /* restore errno */
150}
151
152
153void __attribute__ ((constructor))
154child_management_start ()
155{
156 if (NULL != sigpipe)
157 return; /* already initialized */
158 sigpipe = GNUNET_DISK_pipe (GNUNET_DISK_PF_NONE);
159 GNUNET_assert (sigpipe != NULL);
160 shc_chld =
161 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
162}
163
164
165struct GNUNET_ChildWaitHandle *
166GNUNET_wait_child (struct GNUNET_OS_Process *proc,
167 GNUNET_ChildCompletedCallback cb,
168 void *cb_cls)
169{
170 struct GNUNET_ChildWaitHandle *cwh;
171
172 child_management_start ();
173 cwh = GNUNET_new (struct GNUNET_ChildWaitHandle);
174 cwh->proc = proc;
175 cwh->cb = cb;
176 cwh->cb_cls = cb_cls;
177 GNUNET_CONTAINER_DLL_insert (cwh_head,
178 cwh_tail,
179 cwh);
180 if (NULL == sig_task)
181 {
182 sig_task = GNUNET_SCHEDULER_add_read_file (
183 GNUNET_TIME_UNIT_FOREVER_REL,
184 GNUNET_DISK_pipe_handle (sigpipe,
185 GNUNET_DISK_PIPE_END_READ),
186 &maint_child_death,
187 NULL);
188 }
189 return cwh;
190}
191
192
193void
194GNUNET_wait_child_cancel (struct GNUNET_ChildWaitHandle *cwh)
195{
196 GNUNET_CONTAINER_DLL_remove (cwh_head,
197 cwh_tail,
198 cwh);
199 if (NULL == cwh_head)
200 {
201 GNUNET_SCHEDULER_cancel (sig_task);
202 sig_task = NULL;
203 }
204 GNUNET_free (cwh);
205}
206
207
208/**
209 * Clean up.
210 */
211void __attribute__ ((destructor))
212GNUNET_CM_done ()
213{
214 GNUNET_assert (NULL == sig_task);
215 GNUNET_SIGNAL_handler_uninstall (shc_chld);
216 shc_chld = NULL;
217 GNUNET_DISK_pipe_close (sigpipe);
218 sigpipe = NULL;
219}