aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_netjail_start_v2.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testing/testing_api_cmd_netjail_start_v2.c')
-rw-r--r--src/testing/testing_api_cmd_netjail_start_v2.c249
1 files changed, 0 insertions, 249 deletions
diff --git a/src/testing/testing_api_cmd_netjail_start_v2.c b/src/testing/testing_api_cmd_netjail_start_v2.c
deleted file mode 100644
index 9123148a7..000000000
--- a/src/testing/testing_api_cmd_netjail_start_v2.c
+++ /dev/null
@@ -1,249 +0,0 @@
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/testing_api_cmd_hello_world.c
23 * @brief Command to start the netjail script.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testing_ng_lib.h"
29
30#define NETJAIL_START_SCRIPT "./../testing/netjail_start_v2.sh"
31
32/**
33 * Struct to hold information for callbacks.
34 *
35 */
36struct NetJailState
37{
38 // Child Wait handle
39 struct GNUNET_ChildWaitHandle *cwh;
40
41 /**
42 * Configuration file for the test topology.
43 */
44 char *topology_config;
45
46 /**
47 * The process id of the start script.
48 */
49 struct GNUNET_OS_Process *start_proc;
50
51 // Flag indication if the script finished.
52 // FIXME: document 3 values
53 enum GNUNET_GenericReturnValue finished;
54};
55
56
57/**
58 * The cleanup function of this cmd frees resources the cmd allocated.
59 *
60 */
61static void
62netjail_start_cleanup (void *cls,
63 const struct GNUNET_TESTING_Command *cmd)
64{
65 struct NetJailState *ns = cls;
66
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
68 "netjail_start_cleanup!\n");
69
70 if (NULL != ns->cwh)
71 {
72 GNUNET_wait_child_cancel (ns->cwh);
73 ns->cwh = NULL;
74 }
75 if (NULL != ns->start_proc)
76 {
77 GNUNET_assert (0 ==
78 GNUNET_OS_process_kill (ns->start_proc,
79 SIGKILL));
80 GNUNET_assert (GNUNET_OK ==
81 GNUNET_OS_process_wait (ns->start_proc));
82 GNUNET_OS_process_destroy (ns->start_proc);
83 ns->start_proc = NULL;
84 }
85 GNUNET_free (ns);
86}
87
88
89/**
90 * Trait function of this cmd does nothing.
91 *
92 */
93static int
94netjail_start_traits (void *cls,
95 const void **ret,
96 const char *trait,
97 unsigned int index)
98{
99 return GNUNET_OK;
100}
101
102
103/**
104 * Callback which will be called if the setup script finished.
105 *
106 */
107static void
108child_completed_callback (void *cls,
109 enum GNUNET_OS_ProcessStatusType type,
110 long unsigned int exit_code)
111{
112 struct NetJailState *ns = cls;
113
114 if (0 == exit_code)
115 {
116 ns->finished = GNUNET_YES;
117 }
118 else
119 {
120 // FIXME: log status code
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
122 "Child completed with an error!\n");
123 ns->finished = GNUNET_SYSERR;
124 }
125 GNUNET_OS_process_destroy (ns->start_proc);
126 ns->start_proc = NULL;
127}
128
129
130
131/**
132* The run method starts the script which setup the network namespaces.
133*
134* @param cls closure.
135* @param cmd CMD being run.
136* @param is interpreter state.
137*/
138static void
139netjail_start_run (void *cls,
140 const struct GNUNET_TESTING_Command *cmd,
141 struct GNUNET_TESTING_Interpreter *is)
142{
143 struct NetJailState *ns = cls;
144 char pid[15];
145 enum GNUNET_GenericReturnValue helper_check;
146
147 // FIXME: NETJAIL_START_SCRIPT like this is bad,
148 // use location from share/gnunet/ of installed
149 // binary in case libgnunettesting is used as a lib!
150 helper_check = GNUNET_OS_check_helper_binary (
151 NETJAIL_START_SCRIPT,
152 GNUNET_YES,
153 NULL);
154
155 if (GNUNET_NO == helper_check)
156 {
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
158 "No SUID for %s!\n",
159 NETJAIL_START_SCRIPT);
160 GNUNET_TESTING_interpreter_fail ();
161 return;
162 }
163 if (GNUNET_SYSERR == helper_check)
164 {
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166 "%s not found!\n",
167 NETJAIL_START_SCRIPT);
168 GNUNET_TESTING_interpreter_fail ();
169 return;
170 }
171
172 GNUNET_snprintf (pid,
173 sizeof (pid),
174 "%u",
175 getpid ());
176 {
177 char *const script_argv[] = {
178 NETJAIL_START_SCRIPT,
179 ns->topology_config,
180 pid,
181 NULL
182 };
183
184 ns->start_proc
185 = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR,
186 NULL,
187 NULL,
188 NULL,
189 NETJAIL_START_SCRIPT,
190 script_argv);
191 }
192 ns->cwh = GNUNET_wait_child (ns->start_proc,
193 &child_completed_callback,
194 ns);
195 GNUNET_break (NULL != ns->cwh);
196}
197
198
199/**
200 * This function checks the flag NetJailState
201 *
202 * FIXME: fix comment!
203 * #finished, if this cmd finished.
204 *
205 */
206static enum GNUNET_GenericReturnValue
207netjail_start_finish (void *cls,
208 GNUNET_SCHEDULER_TaskCallback cont,
209 void *cont_cls)
210{
211 struct NetJailState *ns = cls;
212
213 if (GNUNET_NO != ns->finished)
214 {
215 cont (cont_cls);
216 }
217 // FIXME: cont should be called later in the else case!
218 return ns->finished;
219}
220
221
222/**
223 * Create command.
224 *
225 * @param label name for command.
226 * @param topology_config Configuration file for the test topology.
227 * @return command.
228 */
229struct GNUNET_TESTING_Command
230GNUNET_TESTING_cmd_netjail_start_v2 (const char *label,
231 char *topology_config)
232{
233 struct NetJailState *ns;
234
235 ns = GNUNET_new (struct NetJailState);
236 ns->topology_config = topology_config;
237 {
238 struct GNUNET_TESTING_Command cmd = {
239 .cls = ns,
240 .label = label,
241 .run = &netjail_start_run,
242 .finish = &netjail_start_finish,
243 .cleanup = &netjail_start_cleanup,
244 .traits = &netjail_start_traits
245 };
246
247 return cmd;
248 }
249}