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