diff options
Diffstat (limited to 'src/testing/testing_api_cmd_netjail_stop_v2.c')
-rw-r--r-- | src/testing/testing_api_cmd_netjail_stop_v2.c | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/src/testing/testing_api_cmd_netjail_stop_v2.c b/src/testing/testing_api_cmd_netjail_stop_v2.c new file mode 100644 index 000000000..1d13407d7 --- /dev/null +++ b/src/testing/testing_api_cmd_netjail_stop_v2.c | |||
@@ -0,0 +1,225 @@ | |||
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_v2.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 | /** | ||
43 | * Configuration file for the test topology. | ||
44 | */ | ||
45 | char *topology_config; | ||
46 | |||
47 | /** | ||
48 | * The process id of the start script. | ||
49 | */ | ||
50 | struct GNUNET_OS_Process *stop_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_stop_cleanup (void *cls, | ||
63 | const struct GNUNET_TESTING_Command *cmd) | ||
64 | { | ||
65 | struct NetJailState *ns = cls; | ||
66 | |||
67 | if (NULL != cwh) | ||
68 | { | ||
69 | GNUNET_wait_child_cancel (cwh); | ||
70 | cwh = NULL; | ||
71 | } | ||
72 | if (NULL != ns->stop_proc) | ||
73 | { | ||
74 | GNUNET_assert (0 == | ||
75 | GNUNET_OS_process_kill (ns->stop_proc, | ||
76 | SIGKILL)); | ||
77 | GNUNET_assert (GNUNET_OK == | ||
78 | GNUNET_OS_process_wait (ns->stop_proc)); | ||
79 | GNUNET_OS_process_destroy (ns->stop_proc); | ||
80 | ns->stop_proc = NULL; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | |||
85 | /** | ||
86 | * Trait function of this cmd does nothing. | ||
87 | * | ||
88 | */ | ||
89 | static int | ||
90 | netjail_stop_traits (void *cls, | ||
91 | const void **ret, | ||
92 | const char *trait, | ||
93 | unsigned int index) | ||
94 | { | ||
95 | return GNUNET_OK; | ||
96 | } | ||
97 | |||
98 | |||
99 | /** | ||
100 | * Callback which will be called if the setup script finished. | ||
101 | * | ||
102 | */ | ||
103 | static void | ||
104 | child_completed_callback (void *cls, | ||
105 | enum GNUNET_OS_ProcessStatusType type, | ||
106 | long unsigned int exit_code) | ||
107 | { | ||
108 | struct NetJailState *ns = cls; | ||
109 | |||
110 | cwh = NULL; | ||
111 | if (0 == exit_code) | ||
112 | { | ||
113 | ns->finished = GNUNET_YES; | ||
114 | } | ||
115 | else | ||
116 | { | ||
117 | ns->finished = GNUNET_SYSERR; | ||
118 | } | ||
119 | GNUNET_OS_process_destroy (ns->stop_proc); | ||
120 | ns->stop_proc = NULL; | ||
121 | } | ||
122 | |||
123 | |||
124 | /** | ||
125 | * The run method starts the script which deletes the network namespaces. | ||
126 | * | ||
127 | * @param cls closure. | ||
128 | * @param cmd CMD being run. | ||
129 | * @param is interpreter state. | ||
130 | */ | ||
131 | static void | ||
132 | netjail_stop_run (void *cls, | ||
133 | const struct GNUNET_TESTING_Command *cmd, | ||
134 | struct GNUNET_TESTING_Interpreter *is) | ||
135 | { | ||
136 | struct NetJailState *ns = cls; | ||
137 | char *pid; | ||
138 | GNUNET_asprintf (&pid, | ||
139 | "%u", | ||
140 | getpid ()); | ||
141 | char *const script_argv[] = {NETJAIL_STOP_SCRIPT, | ||
142 | ns->topology_config, | ||
143 | pid, | ||
144 | NULL}; | ||
145 | unsigned int helper_check = GNUNET_OS_check_helper_binary ( | ||
146 | NETJAIL_STOP_SCRIPT, | ||
147 | GNUNET_YES, | ||
148 | NULL); | ||
149 | |||
150 | if (GNUNET_NO == helper_check) | ||
151 | { | ||
152 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
153 | "No SUID for %s!\n", | ||
154 | NETJAIL_STOP_SCRIPT); | ||
155 | GNUNET_TESTING_interpreter_fail (); | ||
156 | } | ||
157 | else if (GNUNET_NO == helper_check) | ||
158 | { | ||
159 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
160 | "%s not found!\n", | ||
161 | NETJAIL_STOP_SCRIPT); | ||
162 | GNUNET_TESTING_interpreter_fail (); | ||
163 | } | ||
164 | |||
165 | ns->stop_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR, | ||
166 | NULL, | ||
167 | NULL, | ||
168 | NULL, | ||
169 | NETJAIL_STOP_SCRIPT, | ||
170 | script_argv); | ||
171 | |||
172 | cwh = GNUNET_wait_child (ns->stop_proc, | ||
173 | &child_completed_callback, | ||
174 | ns); | ||
175 | GNUNET_break (NULL != cwh); | ||
176 | |||
177 | } | ||
178 | |||
179 | |||
180 | /** | ||
181 | * This function checks the flag NetJailState#finished, if this cmd finished. | ||
182 | * | ||
183 | */ | ||
184 | static int | ||
185 | netjail_stop_finish (void *cls, | ||
186 | GNUNET_SCHEDULER_TaskCallback cont, | ||
187 | void *cont_cls) | ||
188 | { | ||
189 | struct NetJailState *ns = cls; | ||
190 | |||
191 | if (ns->finished) | ||
192 | { | ||
193 | cont (cont_cls); | ||
194 | } | ||
195 | return ns->finished; | ||
196 | } | ||
197 | |||
198 | |||
199 | /** | ||
200 | * Create command. | ||
201 | * | ||
202 | * @param label name for command. | ||
203 | * @param topology_config Configuration file for the test topology. | ||
204 | * @return command. | ||
205 | */ | ||
206 | struct GNUNET_TESTING_Command | ||
207 | GNUNET_TESTING_cmd_netjail_stop_v2 (const char *label, | ||
208 | char *topology_config) | ||
209 | { | ||
210 | struct NetJailState *ns; | ||
211 | |||
212 | ns = GNUNET_new (struct NetJailState); | ||
213 | ns->topology_config = topology_config; | ||
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 | } | ||