diff options
Diffstat (limited to 'src/testing/testing_api_cmd_netjail_stop.c')
-rw-r--r-- | src/testing/testing_api_cmd_netjail_stop.c | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/src/testing/testing_api_cmd_netjail_stop.c b/src/testing/testing_api_cmd_netjail_stop.c new file mode 100644 index 000000000..710b4fbf4 --- /dev/null +++ b/src/testing/testing_api_cmd_netjail_stop.c | |||
@@ -0,0 +1,215 @@ | |||
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 | struct GNUNET_ChildWaitHandle *cwh; | ||
34 | |||
35 | struct NetJailState | ||
36 | { | ||
37 | char *local_m; | ||
38 | |||
39 | char *global_n; | ||
40 | |||
41 | /** | ||
42 | * The process id of the start script. | ||
43 | */ | ||
44 | struct GNUNET_OS_Process *stop_proc; | ||
45 | |||
46 | unsigned int finished; | ||
47 | }; | ||
48 | |||
49 | |||
50 | /** | ||
51 | * | ||
52 | * | ||
53 | * @param cls closure | ||
54 | * @param cmd current CMD being cleaned up. | ||
55 | */ | ||
56 | static void | ||
57 | netjail_stop_cleanup (void *cls, | ||
58 | const struct GNUNET_TESTING_Command *cmd) | ||
59 | { | ||
60 | struct NetJailState *ns = cls; | ||
61 | |||
62 | if (NULL != cwh) | ||
63 | { | ||
64 | GNUNET_wait_child_cancel (cwh); | ||
65 | cwh = NULL; | ||
66 | } | ||
67 | if (NULL != ns->stop_proc) | ||
68 | { | ||
69 | GNUNET_assert (0 == | ||
70 | GNUNET_OS_process_kill (ns->stop_proc, | ||
71 | SIGKILL)); | ||
72 | GNUNET_assert (GNUNET_OK == | ||
73 | GNUNET_OS_process_wait (ns->stop_proc)); | ||
74 | GNUNET_OS_process_destroy (ns->stop_proc); | ||
75 | ns->stop_proc = NULL; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | |||
80 | /** | ||
81 | * | ||
82 | * | ||
83 | * @param cls closure. | ||
84 | * @param[out] ret result | ||
85 | * @param trait name of the trait. | ||
86 | * @param index index number of the object to offer. | ||
87 | * @return #GNUNET_OK on success. | ||
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 | static void | ||
100 | child_completed_callback (void *cls, | ||
101 | enum GNUNET_OS_ProcessStatusType type, | ||
102 | long unsigned int exit_code) | ||
103 | { | ||
104 | struct NetJailState *ns = cls; | ||
105 | |||
106 | cwh = NULL; | ||
107 | if (0 == exit_code) | ||
108 | { | ||
109 | ns->finished = GNUNET_YES; | ||
110 | } | ||
111 | else | ||
112 | { | ||
113 | ns->finished = GNUNET_SYSERR; | ||
114 | } | ||
115 | GNUNET_OS_process_destroy (ns->stop_proc); | ||
116 | ns->stop_proc = NULL; | ||
117 | } | ||
118 | |||
119 | |||
120 | /** | ||
121 | * Run the "hello world" CMD. | ||
122 | * | ||
123 | * @param cls closure. | ||
124 | * @param cmd CMD being run. | ||
125 | * @param is interpreter state. | ||
126 | */ | ||
127 | static void | ||
128 | netjail_stop_run (void *cls, | ||
129 | const struct GNUNET_TESTING_Command *cmd, | ||
130 | struct GNUNET_TESTING_Interpreter *is) | ||
131 | { | ||
132 | struct NetJailState *ns = cls; | ||
133 | char *const script_argv[] = {NETJAIL_STOP_SCRIPT, | ||
134 | ns->local_m, | ||
135 | ns->global_n, | ||
136 | NULL}; | ||
137 | unsigned int helper_check = GNUNET_OS_check_helper_binary ( | ||
138 | NETJAIL_STOP_SCRIPT, | ||
139 | GNUNET_YES, | ||
140 | NULL); | ||
141 | |||
142 | if (GNUNET_NO == helper_check) | ||
143 | { | ||
144 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
145 | "No SUID for %s!\n", | ||
146 | NETJAIL_STOP_SCRIPT); | ||
147 | GNUNET_TESTING_interpreter_fail (); | ||
148 | } | ||
149 | else if (GNUNET_NO == helper_check) | ||
150 | { | ||
151 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
152 | "%s not found!\n", | ||
153 | NETJAIL_STOP_SCRIPT); | ||
154 | GNUNET_TESTING_interpreter_fail (); | ||
155 | } | ||
156 | |||
157 | ns->stop_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR, | ||
158 | NULL, | ||
159 | NULL, | ||
160 | NULL, | ||
161 | NETJAIL_STOP_SCRIPT, | ||
162 | script_argv); | ||
163 | |||
164 | cwh = GNUNET_wait_child (ns->stop_proc, | ||
165 | &child_completed_callback, | ||
166 | ns); | ||
167 | GNUNET_break (NULL != cwh); | ||
168 | |||
169 | } | ||
170 | |||
171 | |||
172 | static int | ||
173 | netjail_stop_finish (void *cls, | ||
174 | GNUNET_SCHEDULER_TaskCallback cont, | ||
175 | void *cont_cls) | ||
176 | { | ||
177 | struct NetJailState *ns = cls; | ||
178 | |||
179 | if (ns->finished) | ||
180 | { | ||
181 | cont (cont_cls); | ||
182 | } | ||
183 | return ns->finished; | ||
184 | } | ||
185 | |||
186 | |||
187 | /** | ||
188 | * Create command. | ||
189 | * | ||
190 | * @param label name for command. | ||
191 | * @param binaryname to stop. | ||
192 | * @return command. | ||
193 | */ | ||
194 | struct GNUNET_TESTING_Command | ||
195 | GNUNET_TESTING_cmd_netjail_stop (const char *label, | ||
196 | char *local_m, | ||
197 | char *global_n) | ||
198 | { | ||
199 | struct NetJailState *ns; | ||
200 | |||
201 | ns = GNUNET_new (struct NetJailState); | ||
202 | ns->local_m = local_m; | ||
203 | ns->global_n = global_n; | ||
204 | |||
205 | struct GNUNET_TESTING_Command cmd = { | ||
206 | .cls = ns, | ||
207 | .label = label, | ||
208 | .run = &netjail_stop_run, | ||
209 | .finish = &netjail_stop_finish, | ||
210 | .cleanup = &netjail_stop_cleanup, | ||
211 | .traits = &netjail_stop_traits | ||
212 | }; | ||
213 | |||
214 | return cmd; | ||
215 | } | ||