aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_netjail_start.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testing/testing_api_cmd_netjail_start.c')
-rw-r--r--src/testing/testing_api_cmd_netjail_start.c236
1 files changed, 0 insertions, 236 deletions
diff --git a/src/testing/testing_api_cmd_netjail_start.c b/src/testing/testing_api_cmd_netjail_start.c
deleted file mode 100644
index 6cd648c3a..000000000
--- a/src/testing/testing_api_cmd_netjail_start.c
+++ /dev/null
@@ -1,236 +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_netjail_start.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#include "gnunet_testing_netjail_lib.h"
30
31#define NETJAIL_START_SCRIPT "netjail_start.sh"
32
33#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
34
35/**
36 * Struct to hold information for callbacks.
37 *
38 */
39struct NetJailState
40{
41 /**
42 * Context for our asynchronous completion.
43 */
44 struct GNUNET_TESTING_AsyncContext ac;
45
46 // Child Wait handle
47 struct GNUNET_ChildWaitHandle *cwh;
48
49 /**
50 * The process id of the start script.
51 */
52 struct GNUNET_OS_Process *start_proc;
53
54 /**
55 * Configuration file for the test topology.
56 */
57 char *topology_config;
58
59 /**
60 * Shall we read the topology from file, or from a string.
61 */
62 unsigned int *read_file;
63};
64
65
66/**
67 * The cleanup function of this cmd frees resources the cmd allocated.
68 *
69 */
70static void
71netjail_start_cleanup (void *cls)
72{
73 struct NetJailState *ns = cls;
74
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76 "netjail_start_cleanup!\n");
77
78 if (NULL != ns->cwh)
79 {
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81 "Cancel child\n");
82 GNUNET_wait_child_cancel (ns->cwh);
83 ns->cwh = NULL;
84 }
85 if (NULL != ns->start_proc)
86 {
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88 "Kill process\n");
89 GNUNET_assert (0 ==
90 GNUNET_OS_process_kill (ns->start_proc,
91 SIGKILL));
92 GNUNET_assert (GNUNET_OK ==
93 GNUNET_OS_process_wait (ns->start_proc));
94 GNUNET_OS_process_destroy (ns->start_proc);
95 ns->start_proc = NULL;
96 }
97 GNUNET_free (ns);
98}
99
100
101/**
102 * Callback which will be called if the setup script finished.
103 *
104 */
105static void
106child_completed_callback (void *cls,
107 enum GNUNET_OS_ProcessStatusType type,
108 long unsigned int exit_code)
109{
110 struct NetJailState *ns = cls;
111
112 GNUNET_OS_process_destroy (ns->start_proc);
113 ns->start_proc = NULL;
114 ns->cwh = NULL;
115 if (0 == exit_code)
116 {
117 GNUNET_TESTING_async_finish (&ns->ac);
118 }
119 else
120 {
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
122 "Child failed with error %lu!\n",
123 exit_code);
124 GNUNET_TESTING_async_fail (&ns->ac);
125 }
126}
127
128
129/**
130* The run method starts the script which setup the network namespaces.
131*
132* @param cls closure.
133* @param is interpreter state.
134*/
135static void
136netjail_start_run (void *cls,
137 struct GNUNET_TESTING_Interpreter *is)
138{
139 struct NetJailState *ns = cls;
140 char pid[15];
141 enum GNUNET_GenericReturnValue helper_check;
142 char *data_dir;
143 char *script_name;
144 char *read_file;
145
146 data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
147 GNUNET_asprintf (&script_name, "%s%s", data_dir, NETJAIL_START_SCRIPT);
148 GNUNET_asprintf (&read_file, "%u", *(ns->read_file));
149
150 helper_check = GNUNET_OS_check_helper_binary (
151 script_name,
152 GNUNET_YES,
153 NULL);
154
155 LOG (GNUNET_ERROR_TYPE_DEBUG,
156 "script_name %s\n",
157 script_name);
158
159 if (GNUNET_NO == helper_check)
160 {
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162 "No SUID for %s!\n",
163 script_name);
164 GNUNET_TESTING_interpreter_fail (is);
165 return;
166 }
167 if (GNUNET_SYSERR == helper_check)
168 {
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170 "%s not found!\n",
171 script_name);
172 GNUNET_TESTING_interpreter_fail (is);
173 return;
174 }
175
176 GNUNET_snprintf (pid,
177 sizeof (pid),
178 "%u",
179 getpid ());
180 {
181 char *const script_argv[] = {
182 script_name,
183 ns->topology_config,
184 pid,
185 read_file,
186 NULL
187 };
188
189 ns->start_proc
190 = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR,
191 NULL,
192 NULL,
193 NULL,
194 script_name,
195 script_argv);
196
197 }
198 ns->cwh = GNUNET_wait_child (ns->start_proc,
199 &child_completed_callback,
200 ns);
201 GNUNET_break (NULL != ns->cwh);
202 GNUNET_free (read_file);
203 GNUNET_free (script_name);
204 GNUNET_free (data_dir);
205}
206
207
208/**
209 * Create command.
210 *
211 * @param label name for command.
212 * @param topology_config Configuration file for the test topology.
213 * @return command.
214 */
215struct GNUNET_TESTING_Command
216GNUNET_TESTING_cmd_netjail_start (const char *label,
217 char *topology_config,
218 unsigned int *read_file)
219{
220 struct NetJailState *ns;
221
222 ns = GNUNET_new (struct NetJailState);
223 ns->topology_config = topology_config;
224 ns->read_file = read_file;
225 {
226 struct GNUNET_TESTING_Command cmd = {
227 .cls = ns,
228 .label = label,
229 .run = &netjail_start_run,
230 .ac = &ns->ac,
231 .cleanup = &netjail_start_cleanup
232 };
233
234 return cmd;
235 }
236}