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