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