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