aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/testbed_api_cmd_netjail_start.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testbed/testbed_api_cmd_netjail_start.c')
-rw-r--r--src/testbed/testbed_api_cmd_netjail_start.c193
1 files changed, 193 insertions, 0 deletions
diff --git a/src/testbed/testbed_api_cmd_netjail_start.c b/src/testbed/testbed_api_cmd_netjail_start.c
new file mode 100644
index 000000000..320537a61
--- /dev/null
+++ b/src/testbed/testbed_api_cmd_netjail_start.c
@@ -0,0 +1,193 @@
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#include "gnunet_testbed_ng_service.h"
30
31#define NETJAIL_START_SCRIPT "./netjail_start.sh"
32
33struct NetJailState
34{
35 struct GNUNET_ChildWaitHandle *cwh;
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 *start_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*/
56static void
57netjail_start_cleanup (void *cls,
58 const struct GNUNET_TESTING_Command *cmd)
59{
60 struct NetJailState *ns = cls;
61
62 if (NULL != ns->cwh)
63 {
64 GNUNET_wait_child_cancel (ns->cwh);
65 ns->cwh = NULL;
66 }
67 if (NULL != ns->start_proc)
68 {
69 GNUNET_assert (0 ==
70 GNUNET_OS_process_kill (ns->start_proc,
71 SIGKILL));
72 GNUNET_assert (GNUNET_OK ==
73 GNUNET_OS_process_wait (ns->start_proc));
74 GNUNET_OS_process_destroy (ns->start_proc);
75 ns->start_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*/
89static int
90netjail_start_traits (void *cls,
91 const void **ret,
92 const char *trait,
93 unsigned int index)
94{
95 return GNUNET_OK;
96}
97
98static void
99child_completed_callback (void *cls,
100 enum GNUNET_OS_ProcessStatusType type,
101 long unsigned int exit_code)
102{
103 struct NetJailState *ns = cls;
104
105 if (0 == exit_code)
106 {
107 ns->finished = GNUNET_YES;
108 }
109 else
110 {
111 ns->finished = GNUNET_SYSERR;
112 }
113 GNUNET_OS_process_destroy (ns->start_proc);
114 ns->start_proc = NULL;
115}
116
117
118
119/**
120* Run the "hello world" CMD.
121*
122* @param cls closure.
123* @param cmd CMD being run.
124* @param is interpreter state.
125*/
126static void
127netjail_start_run (void *cls,
128 const struct GNUNET_TESTING_Command *cmd,
129 struct GNUNET_TESTING_Interpreter *is)
130{
131 struct NetJailState *ns = cls;
132 char *const script_argv[] = {NETJAIL_START_SCRIPT,
133 ns->local_m,
134 ns->global_n,
135 NULL};
136
137 ns->start_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR,
138 NULL,
139 NULL,
140 NULL,
141 NETJAIL_START_SCRIPT,
142 script_argv);
143
144 ns->cwh = GNUNET_wait_child (ns->start_proc,
145 &child_completed_callback,
146 ns);
147 GNUNET_break (NULL != ns->cwh);
148}
149
150static int
151netjail_start_finish (void *cls,
152 GNUNET_SCHEDULER_TaskCallback cont,
153 void *cont_cls)
154{
155 struct NetJailState *ns = cls;
156
157 if (ns->finished)
158 {
159 cont (cont_cls);
160 }
161 return ns->finished;
162}
163
164/**
165 * Create command.
166 *
167 * @param label name for command.
168 * @param binaryname to start.
169 * @return command.
170 */
171struct GNUNET_TESTING_Command
172GNUNET_TESTBED_cmd_netjail_start (const char *label,
173 char *local_m,
174 char *global_n)
175{
176 struct NetJailState *ns;
177
178 ns = GNUNET_new (struct NetJailState);
179 ns->local_m = local_m;
180 ns->global_n = global_n;
181 ns->finished = GNUNET_NO;
182
183 struct GNUNET_TESTING_Command cmd = {
184 .cls = ns,
185 .label = label,
186 .run = &netjail_start_run,
187 .finish = &netjail_start_finish,
188 .cleanup = &netjail_start_cleanup,
189 .traits = &netjail_start_traits
190 };
191
192 return cmd;
193}