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.c229
1 files changed, 229 insertions, 0 deletions
diff --git a/src/testing/testing_api_cmd_netjail_start_v2.c b/src/testing/testing_api_cmd_netjail_start_v2.c
new file mode 100644
index 000000000..36fbb0e10
--- /dev/null
+++ b/src/testing/testing_api_cmd_netjail_start_v2.c
@@ -0,0 +1,229 @@
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 unsigned int finished;
53};
54
55
56/**
57 * The cleanup function of this cmd frees resources the cmd allocated.
58 *
59 */
60static void
61netjail_start_cleanup (void *cls,
62 const struct GNUNET_TESTING_Command *cmd)
63{
64 struct NetJailState *ns = cls;
65
66 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67 "netjail_start_cleanup!\n");
68
69 if (NULL != ns->cwh)
70 {
71 GNUNET_wait_child_cancel (ns->cwh);
72 ns->cwh = NULL;
73 }
74 if (NULL != ns->start_proc)
75 {
76 GNUNET_assert (0 ==
77 GNUNET_OS_process_kill (ns->start_proc,
78 SIGKILL));
79 GNUNET_assert (GNUNET_OK ==
80 GNUNET_OS_process_wait (ns->start_proc));
81 GNUNET_OS_process_destroy (ns->start_proc);
82 ns->start_proc = NULL;
83 }
84 GNUNET_free (ns);
85}
86
87
88/**
89 * Trait function of this cmd does nothing.
90 *
91 */
92static int
93netjail_start_traits (void *cls,
94 const void **ret,
95 const char *trait,
96 unsigned int index)
97{
98 return GNUNET_OK;
99}
100
101
102/**
103 * Callback which will be called if the setup script finished.
104 *
105 */
106static void
107child_completed_callback (void *cls,
108 enum GNUNET_OS_ProcessStatusType type,
109 long unsigned int exit_code)
110{
111 struct NetJailState *ns = cls;
112
113 if (0 == exit_code)
114 {
115 ns->finished = GNUNET_YES;
116 }
117 else
118 {
119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
120 "Child completed with an error!\n");
121 ns->finished = GNUNET_SYSERR;
122 }
123 GNUNET_OS_process_destroy (ns->start_proc);
124 ns->start_proc = NULL;
125}
126
127
128
129/**
130* The run method starts the script which setup the network namespaces.
131*
132* @param cls closure.
133* @param cmd CMD being run.
134* @param is interpreter state.
135*/
136static void
137netjail_start_run (void *cls,
138 const struct GNUNET_TESTING_Command *cmd,
139 struct GNUNET_TESTING_Interpreter *is)
140{
141 struct NetJailState *ns = cls;
142 char *pid;
143 GNUNET_asprintf (&pid,
144 "%u",
145 getpid ());
146 char *const script_argv[] = {NETJAIL_START_SCRIPT,
147 ns->topology_config,
148 pid,
149 NULL};
150 unsigned int 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 }
162 else if (GNUNET_NO == helper_check)
163 {
164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
165 "%s not found!\n",
166 NETJAIL_START_SCRIPT);
167 GNUNET_TESTING_interpreter_fail ();
168 }
169
170 ns->start_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR,
171 NULL,
172 NULL,
173 NULL,
174 NETJAIL_START_SCRIPT,
175 script_argv);
176
177 ns->cwh = GNUNET_wait_child (ns->start_proc,
178 &child_completed_callback,
179 ns);
180 GNUNET_break (NULL != ns->cwh);
181}
182
183
184/**
185 * This function checks the flag NetJailState#finished, if this cmd finished.
186 *
187 */
188static int
189netjail_start_finish (void *cls,
190 GNUNET_SCHEDULER_TaskCallback cont,
191 void *cont_cls)
192{
193 struct NetJailState *ns = cls;
194
195 if (ns->finished)
196 {
197 cont (cont_cls);
198 }
199 return ns->finished;
200}
201
202/**
203 * Create command.
204 *
205 * @param label name for command.
206 * @param topology_config Configuration file for the test topology.
207 * @return command.
208 */
209struct GNUNET_TESTING_Command
210GNUNET_TESTING_cmd_netjail_start_v2 (const char *label,
211 char *topology_config)
212{
213 struct NetJailState *ns;
214
215 ns = GNUNET_new (struct NetJailState);
216 ns->finished = GNUNET_NO;
217 ns->topology_config = topology_config;
218
219 struct GNUNET_TESTING_Command cmd = {
220 .cls = ns,
221 .label = label,
222 .run = &netjail_start_run,
223 .finish = &netjail_start_finish,
224 .cleanup = &netjail_start_cleanup,
225 .traits = &netjail_start_traits
226 };
227
228 return cmd;
229}