diff options
Diffstat (limited to 'src/testing/testing_api_cmd_netjail_stop.c')
-rw-r--r-- | src/testing/testing_api_cmd_netjail_stop.c | 225 |
1 files changed, 0 insertions, 225 deletions
diff --git a/src/testing/testing_api_cmd_netjail_stop.c b/src/testing/testing_api_cmd_netjail_stop.c deleted file mode 100644 index 99084d9af..000000000 --- a/src/testing/testing_api_cmd_netjail_stop.c +++ /dev/null | |||
@@ -1,225 +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 stop 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 | |||
31 | #define NETJAIL_STOP_SCRIPT "./../testing/netjail_stop.sh" | ||
32 | |||
33 | // Child Wait handle | ||
34 | struct GNUNET_ChildWaitHandle *cwh; | ||
35 | |||
36 | /** | ||
37 | * Struct to hold information for callbacks. | ||
38 | * | ||
39 | */ | ||
40 | struct NetJailState | ||
41 | { | ||
42 | // Number of local nodes in each namespace. | ||
43 | char *local_m; | ||
44 | |||
45 | // The number of namespaces. | ||
46 | char *global_n; | ||
47 | |||
48 | /** | ||
49 | * The process id of the start script. | ||
50 | */ | ||
51 | struct GNUNET_OS_Process *stop_proc; | ||
52 | |||
53 | // Flag indication if the script finished. | ||
54 | unsigned int finished; | ||
55 | }; | ||
56 | |||
57 | |||
58 | /** | ||
59 | * The cleanup function of this cmd frees resources the cmd allocated. | ||
60 | * | ||
61 | */ | ||
62 | static void | ||
63 | netjail_stop_cleanup (void *cls, | ||
64 | const struct GNUNET_TESTING_Command *cmd) | ||
65 | { | ||
66 | struct NetJailState *ns = cls; | ||
67 | |||
68 | if (NULL != cwh) | ||
69 | { | ||
70 | GNUNET_wait_child_cancel (cwh); | ||
71 | cwh = NULL; | ||
72 | } | ||
73 | if (NULL != ns->stop_proc) | ||
74 | { | ||
75 | GNUNET_assert (0 == | ||
76 | GNUNET_OS_process_kill (ns->stop_proc, | ||
77 | SIGKILL)); | ||
78 | GNUNET_assert (GNUNET_OK == | ||
79 | GNUNET_OS_process_wait (ns->stop_proc)); | ||
80 | GNUNET_OS_process_destroy (ns->stop_proc); | ||
81 | ns->stop_proc = NULL; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | |||
86 | /** | ||
87 | * Trait function of this cmd does nothing. | ||
88 | * | ||
89 | */ | ||
90 | static int | ||
91 | netjail_stop_traits (void *cls, | ||
92 | const void **ret, | ||
93 | const char *trait, | ||
94 | unsigned int index) | ||
95 | { | ||
96 | return GNUNET_OK; | ||
97 | } | ||
98 | |||
99 | |||
100 | /** | ||
101 | * Callback which will be called if the setup script finished. | ||
102 | * | ||
103 | */ | ||
104 | static void | ||
105 | child_completed_callback (void *cls, | ||
106 | enum GNUNET_OS_ProcessStatusType type, | ||
107 | long unsigned int exit_code) | ||
108 | { | ||
109 | struct NetJailState *ns = cls; | ||
110 | |||
111 | cwh = NULL; | ||
112 | if (0 == exit_code) | ||
113 | { | ||
114 | ns->finished = GNUNET_YES; | ||
115 | } | ||
116 | else | ||
117 | { | ||
118 | ns->finished = GNUNET_SYSERR; | ||
119 | } | ||
120 | GNUNET_OS_process_destroy (ns->stop_proc); | ||
121 | ns->stop_proc = NULL; | ||
122 | } | ||
123 | |||
124 | |||
125 | /** | ||
126 | * The run method starts the script which deletes the network namespaces. | ||
127 | * | ||
128 | * @param cls closure. | ||
129 | * @param cmd CMD being run. | ||
130 | * @param is interpreter state. | ||
131 | */ | ||
132 | static void | ||
133 | netjail_stop_run (void *cls, | ||
134 | const struct GNUNET_TESTING_Command *cmd, | ||
135 | struct GNUNET_TESTING_Interpreter *is) | ||
136 | { | ||
137 | struct NetJailState *ns = cls; | ||
138 | char *const script_argv[] = {NETJAIL_STOP_SCRIPT, | ||
139 | ns->local_m, | ||
140 | ns->global_n, | ||
141 | NULL}; | ||
142 | unsigned int helper_check = GNUNET_OS_check_helper_binary ( | ||
143 | NETJAIL_STOP_SCRIPT, | ||
144 | GNUNET_YES, | ||
145 | NULL); | ||
146 | |||
147 | if (GNUNET_NO == helper_check) | ||
148 | { | ||
149 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
150 | "No SUID for %s!\n", | ||
151 | NETJAIL_STOP_SCRIPT); | ||
152 | GNUNET_TESTING_interpreter_fail (); | ||
153 | } | ||
154 | else if (GNUNET_NO == helper_check) | ||
155 | { | ||
156 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
157 | "%s not found!\n", | ||
158 | NETJAIL_STOP_SCRIPT); | ||
159 | GNUNET_TESTING_interpreter_fail (); | ||
160 | } | ||
161 | |||
162 | ns->stop_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR, | ||
163 | NULL, | ||
164 | NULL, | ||
165 | NULL, | ||
166 | NETJAIL_STOP_SCRIPT, | ||
167 | script_argv); | ||
168 | |||
169 | cwh = GNUNET_wait_child (ns->stop_proc, | ||
170 | &child_completed_callback, | ||
171 | ns); | ||
172 | GNUNET_break (NULL != cwh); | ||
173 | |||
174 | } | ||
175 | |||
176 | |||
177 | /** | ||
178 | * This function checks the flag NetJailState#finished, if this cmd finished. | ||
179 | * | ||
180 | */ | ||
181 | static int | ||
182 | netjail_stop_finish (void *cls, | ||
183 | GNUNET_SCHEDULER_TaskCallback cont, | ||
184 | void *cont_cls) | ||
185 | { | ||
186 | struct NetJailState *ns = cls; | ||
187 | |||
188 | if (ns->finished) | ||
189 | { | ||
190 | cont (cont_cls); | ||
191 | } | ||
192 | return ns->finished; | ||
193 | } | ||
194 | |||
195 | |||
196 | /** | ||
197 | * Create command. | ||
198 | * | ||
199 | * @param label name for command. | ||
200 | * @param local_m Number of local nodes in each namespace. | ||
201 | * @param global_n The number of namespaces. | ||
202 | * @return command. | ||
203 | */ | ||
204 | struct GNUNET_TESTING_Command | ||
205 | GNUNET_TESTING_cmd_netjail_stop (const char *label, | ||
206 | char *local_m, | ||
207 | char *global_n) | ||
208 | { | ||
209 | struct NetJailState *ns; | ||
210 | |||
211 | ns = GNUNET_new (struct NetJailState); | ||
212 | ns->local_m = local_m; | ||
213 | ns->global_n = global_n; | ||
214 | |||
215 | struct GNUNET_TESTING_Command cmd = { | ||
216 | .cls = ns, | ||
217 | .label = label, | ||
218 | .run = &netjail_stop_run, | ||
219 | .finish = &netjail_stop_finish, | ||
220 | .cleanup = &netjail_stop_cleanup, | ||
221 | .traits = &netjail_stop_traits | ||
222 | }; | ||
223 | |||
224 | return cmd; | ||
225 | } | ||