GNUnet  0.11.x
testing_api_cmd_netjail_start_v2.c
Go to the documentation of this file.
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 
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_v2.sh"
31 
36 struct NetJailState
37 {
38  // Child Wait handle
40 
45 
50 
51  // Flag indication if the script finished.
52  unsigned int finished;
53 };
54 
55 
60 static void
62  const struct GNUNET_TESTING_Command *cmd)
63 {
64  struct NetJailState *ns = cls;
65 
67  "netjail_start_cleanup!\n");
68 
69  if (NULL != ns->cwh)
70  {
72  ns->cwh = NULL;
73  }
74  if (NULL != ns->start_proc)
75  {
76  GNUNET_assert (0 ==
77  GNUNET_OS_process_kill (ns->start_proc,
78  SIGKILL));
80  GNUNET_OS_process_wait (ns->start_proc));
81  GNUNET_OS_process_destroy (ns->start_proc);
82  ns->start_proc = NULL;
83  }
84  GNUNET_free (ns);
85 }
86 
87 
92 static int
94  const void **ret,
95  const char *trait,
96  unsigned int index)
97 {
98  return GNUNET_OK;
99 }
100 
101 
106 static void
109  long unsigned int exit_code)
110 {
111  struct NetJailState *ns = cls;
112 
113  if (0 == exit_code)
114  {
115  ns->finished = GNUNET_YES;
116  }
117  else
118  {
120  "Child completed with an error!\n");
121  ns->finished = GNUNET_SYSERR;
122  }
123  GNUNET_OS_process_destroy (ns->start_proc);
124  ns->start_proc = NULL;
125 }
126 
127 
128 
136 static void
137 netjail_start_run (void *cls,
138  const struct GNUNET_TESTING_Command *cmd,
140 {
141  struct NetJailState *ns = cls;
142  char *pid;
144  "%u",
145  getpid ());
146  char *const script_argv[] = {NETJAIL_START_SCRIPT,
147  ns->topology_config,
148  pid,
149  NULL};
150  unsigned int helper_check = GNUNET_OS_check_helper_binary (
152  GNUNET_YES,
153  NULL);
154 
155  if (GNUNET_NO == helper_check)
156  {
158  "No SUID for %s!\n",
161  }
162  else if (GNUNET_NO == helper_check)
163  {
165  "%s not found!\n",
168  }
169 
171  NULL,
172  NULL,
173  NULL,
175  script_argv);
176 
177  ns->cwh = GNUNET_wait_child (ns->start_proc,
179  ns);
180  GNUNET_break (NULL != ns->cwh);
181 }
182 
183 
188 static int
191  void *cont_cls)
192 {
193  struct NetJailState *ns = cls;
194 
195  if (ns->finished)
196  {
197  cont (cont_cls);
198  }
199  return ns->finished;
200 }
201 
211  char *topology_config)
212 {
213  struct NetJailState *ns;
214 
215  ns = GNUNET_new (struct NetJailState);
216  ns->finished = GNUNET_NO;
217  ns->topology_config = topology_config;
218 
219  struct GNUNET_TESTING_Command cmd = {
220  .cls = ns,
221  .label = label,
222  .run = &netjail_start_run,
223  .finish = &netjail_start_finish,
224  .cleanup = &netjail_start_cleanup,
225  .traits = &netjail_start_traits
226  };
227 
228  return cmd;
229 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_NAMESTORE_Handle * ns
Handle to the namestore.
Definition: gnunet-abd.c:41
static long unsigned int exit_code
Handler exit code.
Definition: gnunet-qr.c:54
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
void GNUNET_wait_child_cancel(struct GNUNET_ChildWaitHandle *cwh)
Stop waiting on this child.
struct GNUNET_ChildWaitHandle * GNUNET_wait_child(struct GNUNET_OS_Process *proc, GNUNET_ChildCompletedCallback cb, void *cb_cls)
Starts the handling of the child processes.
#define GNUNET_log(kind,...)
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
void GNUNET_TESTING_interpreter_fail()
Current command failed, clean up and fail the test case.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
int GNUNET_OS_check_helper_binary(const char *binary, int check_suid, const char *params)
Check whether an executable exists and possibly if the suid bit is set on the file.
struct GNUNET_OS_Process * GNUNET_OS_start_process_vap(enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename, char *const argv[])
Start a process.
Definition: os_priority.c:604
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:983
GNUNET_OS_ProcessStatusType
Process status types.
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:286
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:224
@ GNUNET_OS_INHERIT_STD_ERR
When this flag is set, the child process will inherit stderr of the parent.
Definition: gnunet_os_lib.h:91
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
Struct which defines a Child Wait handle.
A command to be run by the interpreter.
const char * label
Label for the command.
void * cls
Closure for all commands with command-specific context information.
Global state of the interpreter, used by a command to access information about other commands.
Definition: testing.h:35
Struct to hold information for callbacks.
struct GNUNET_ChildWaitHandle * cwh
struct GNUNET_OS_Process * start_proc
The process id of the start script.
char * topology_config
Configuration file for the test topology.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
static void child_completed_callback(void *cls, enum GNUNET_OS_ProcessStatusType type, long unsigned int exit_code)
Callback which will be called if the setup script finished.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_netjail_start_v2(const char *label, char *topology_config)
Create command.
static int netjail_start_traits(void *cls, const void **ret, const char *trait, unsigned int index)
Trait function of this cmd does nothing.
static void netjail_start_cleanup(void *cls, const struct GNUNET_TESTING_Command *cmd)
The cleanup function of this cmd frees resources the cmd allocated.
static int netjail_start_finish(void *cls, GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
This function checks the flag NetJailState::finished, if this cmd finished.
#define NETJAIL_START_SCRIPT
static void netjail_start_run(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *is)
The run method starts the script which setup the network namespaces.
struct GNUNET_TESTING_Interpreter * is