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