diff options
Diffstat (limited to 'src/testing/testing_api_cmd_netjail_start.c')
-rw-r--r-- | src/testing/testing_api_cmd_netjail_start.c | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/src/testing/testing_api_cmd_netjail_start.c b/src/testing/testing_api_cmd_netjail_start.c new file mode 100644 index 000000000..536b356a6 --- /dev/null +++ b/src/testing/testing_api_cmd_netjail_start.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.sh" | ||
31 | |||
32 | /** | ||
33 | * Struct to hold information for callbacks. | ||
34 | * | ||
35 | */ | ||
36 | struct NetJailState | ||
37 | { | ||
38 | // Child Wait handle | ||
39 | struct GNUNET_ChildWaitHandle *cwh; | ||
40 | |||
41 | // Number of local nodes in each namespace. | ||
42 | char *local_m; | ||
43 | |||
44 | // The number of namespaces. | ||
45 | char *global_n; | ||
46 | |||
47 | /** | ||
48 | * The process id of the start script. | ||
49 | */ | ||
50 | struct GNUNET_OS_Process *start_proc; | ||
51 | |||
52 | // Flag indication if the script finished. | ||
53 | unsigned int finished; | ||
54 | }; | ||
55 | |||
56 | |||
57 | /** | ||
58 | * The cleanup function of this cmd frees resources the cmd allocated. | ||
59 | * | ||
60 | */ | ||
61 | static void | ||
62 | netjail_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 | */ | ||
93 | static int | ||
94 | netjail_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 | */ | ||
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 | if (0 == exit_code) | ||
115 | { | ||
116 | ns->finished = GNUNET_YES; | ||
117 | } | ||
118 | else | ||
119 | { | ||
120 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
121 | "Child completed with an error!\n"); | ||
122 | ns->finished = GNUNET_SYSERR; | ||
123 | } | ||
124 | GNUNET_OS_process_destroy (ns->start_proc); | ||
125 | ns->start_proc = NULL; | ||
126 | } | ||
127 | |||
128 | |||
129 | |||
130 | /** | ||
131 | * The run method starts the script which setup the network namespaces. | ||
132 | * | ||
133 | * @param cls closure. | ||
134 | * @param cmd CMD being run. | ||
135 | * @param is interpreter state. | ||
136 | */ | ||
137 | static void | ||
138 | netjail_start_run (void *cls, | ||
139 | const struct GNUNET_TESTING_Command *cmd, | ||
140 | struct GNUNET_TESTING_Interpreter *is) | ||
141 | { | ||
142 | struct NetJailState *ns = cls; | ||
143 | char *const script_argv[] = {NETJAIL_START_SCRIPT, | ||
144 | ns->local_m, | ||
145 | ns->global_n, | ||
146 | NULL}; | ||
147 | unsigned int helper_check = GNUNET_OS_check_helper_binary ( | ||
148 | NETJAIL_START_SCRIPT, | ||
149 | GNUNET_YES, | ||
150 | NULL); | ||
151 | |||
152 | if (GNUNET_NO == helper_check) | ||
153 | { | ||
154 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
155 | "No SUID for %s!\n", | ||
156 | NETJAIL_START_SCRIPT); | ||
157 | GNUNET_TESTING_interpreter_fail (); | ||
158 | } | ||
159 | else if (GNUNET_NO == helper_check) | ||
160 | { | ||
161 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
162 | "%s not found!\n", | ||
163 | NETJAIL_START_SCRIPT); | ||
164 | GNUNET_TESTING_interpreter_fail (); | ||
165 | } | ||
166 | |||
167 | ns->start_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR, | ||
168 | NULL, | ||
169 | NULL, | ||
170 | NULL, | ||
171 | NETJAIL_START_SCRIPT, | ||
172 | script_argv); | ||
173 | |||
174 | ns->cwh = GNUNET_wait_child (ns->start_proc, | ||
175 | &child_completed_callback, | ||
176 | ns); | ||
177 | GNUNET_break (NULL != ns->cwh); | ||
178 | } | ||
179 | |||
180 | |||
181 | /** | ||
182 | * This function checks the flag NetJailState#finished, if this cmd finished. | ||
183 | * | ||
184 | */ | ||
185 | static int | ||
186 | netjail_start_finish (void *cls, | ||
187 | GNUNET_SCHEDULER_TaskCallback cont, | ||
188 | void *cont_cls) | ||
189 | { | ||
190 | struct NetJailState *ns = cls; | ||
191 | |||
192 | if (ns->finished) | ||
193 | { | ||
194 | cont (cont_cls); | ||
195 | } | ||
196 | return ns->finished; | ||
197 | } | ||
198 | |||
199 | /** | ||
200 | * Create command. | ||
201 | * | ||
202 | * @param label name for command. | ||
203 | * @param local_m Number of local nodes in each namespace. | ||
204 | * @param global_n The number of namespaces. | ||
205 | * @return command. | ||
206 | */ | ||
207 | struct GNUNET_TESTING_Command | ||
208 | GNUNET_TESTING_cmd_netjail_start (const char *label, | ||
209 | char *local_m, | ||
210 | char *global_n) | ||
211 | { | ||
212 | struct NetJailState *ns; | ||
213 | |||
214 | ns = GNUNET_new (struct NetJailState); | ||
215 | ns->local_m = local_m; | ||
216 | ns->global_n = global_n; | ||
217 | ns->finished = GNUNET_NO; | ||
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 | } | ||