GNUnet  0.17.6
testing_api_cmd_netjail_stop.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"
30 
31 
32 #define NETJAIL_STOP_SCRIPT "netjail_stop.sh"
33 
38 struct NetJailState
39 {
44 
45  // Child Wait handle
47 
51  char *topology_config;
52 
57 
61  unsigned int *read_file;
62 
63 };
64 
65 
70 static void
72 {
73  struct NetJailState *ns = cls;
74 
75  if (NULL != ns->cwh)
76  {
78  ns->cwh = NULL;
79  }
80  if (NULL != ns->stop_proc)
81  {
82  GNUNET_assert (0 ==
83  GNUNET_OS_process_kill (ns->stop_proc,
84  SIGKILL));
86  GNUNET_OS_process_wait (ns->stop_proc));
87  GNUNET_OS_process_destroy (ns->stop_proc);
88  ns->stop_proc = NULL;
89  }
90  GNUNET_free (ns);
91 }
92 
93 
98 static void
101  long unsigned int exit_code)
102 {
103  struct NetJailState *ns = cls;
104 
105  ns->cwh = NULL;
106  GNUNET_OS_process_destroy (ns->stop_proc);
107  ns->stop_proc = NULL;
108  if (0 == exit_code)
109  {
111  }
112  else
113  {
115  }
116 }
117 
118 
125 static void
126 netjail_stop_run (void *cls,
128 {
129  struct NetJailState *ns = cls;
130  char *pid;
131  char *data_dir;
132  char *script_name;
133  char *read_file;
134 
135 
137  GNUNET_asprintf (&script_name, "%s%s", data_dir, NETJAIL_STOP_SCRIPT);
138  GNUNET_asprintf (&read_file, "%u", *(ns->read_file));
139  unsigned int helper_check = GNUNET_OS_check_helper_binary (
140  script_name,
141  GNUNET_YES,
142  NULL);
143 
144  if (GNUNET_NO == helper_check)
145  {
147  "No SUID for %s!\n",
148  script_name);
150  }
151  else if (GNUNET_NO == helper_check)
152  {
154  "%s not found!\n",
155  script_name);
157  }
158 
160  "%u",
161  getpid ());
162  {
163  char *const script_argv[] = {script_name,
164  ns->topology_config,
165  pid,
166  read_file,
167  NULL};
169  NULL,
170  NULL,
171  NULL,
172  script_name,
173  script_argv);
174  }
175  ns->cwh = GNUNET_wait_child (ns->stop_proc,
177  ns);
178  GNUNET_break (NULL != ns->cwh);
179 }
180 
181 
184  char *topology_config,
185  unsigned int *read_file)
186 {
187  struct NetJailState *ns;
188 
189  ns = GNUNET_new (struct NetJailState);
190  ns->topology_config = topology_config;
191  ns->read_file = read_file;
192  {
193  struct GNUNET_TESTING_Command cmd = {
194  .cls = ns,
195  .label = label,
196  .run = &netjail_stop_run,
197  .ac = &ns->ac,
198  .cleanup = &netjail_stop_cleanup
199  };
200 
201  return cmd;
202  }
203 }
static struct GNUNET_NAMESTORE_Handle * ns
Handle to the namestore.
Definition: gnunet-abd.c:41
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
static int exit_code
Global exit code.
Definition: gnunet-qr.c:42
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
void GNUNET_TESTING_async_finish(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has finished.
void GNUNET_TESTING_async_fail(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has failed.
void GNUNET_TESTING_interpreter_fail(struct GNUNET_TESTING_Interpreter *is)
Current command failed, clean up and fail the test case.
#define GNUNET_log(kind,...)
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.
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_YES
@ GNUNET_NO
Definition: gnunet_common.h:98
#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
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.
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:598
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:981
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:280
enum GNUNET_GenericReturnValue GNUNET_OS_check_helper_binary(const char *binary, bool check_suid, const char *params)
Check whether an executable exists and possibly if the suid bit is set on the file.
char * GNUNET_OS_installation_get_path(enum GNUNET_OS_InstallationPathKind dirkind)
Get the path to a specific GNUnet installation directory or, with GNUNET_OS_IPK_SELF_PREFIX,...
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:217
@ GNUNET_OS_INHERIT_STD_ERR
When this flag is set, the child process will inherit stderr of the parent.
Definition: gnunet_os_lib.h:95
@ GNUNET_OS_IPK_DATADIR
Return the directory where data is installed (share/gnunet/)
enum GNUNET_GenericReturnValue read_file(char const *const filename, char **buffer)
Definition: pabc_helper.c:70
Struct which defines a Child Wait handle.
State each asynchronous command must have in its closure.
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.
Struct to hold information for callbacks.
struct GNUNET_OS_Process * stop_proc
The process id of the start script.
struct GNUNET_ChildWaitHandle * cwh
unsigned int * read_file
Shall we read the topology from file, or from a string.
char * topology_config
Configuration file for the test topology.
struct GNUNET_TESTING_AsyncContext ac
Context for our asynchronous completion.
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.
#define NETJAIL_STOP_SCRIPT
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_netjail_stop(const char *label, char *topology_config, unsigned int *read_file)
Create command.
static void netjail_stop_cleanup(void *cls)
The cleanup function of this cmd frees resources the cmd allocated.
static void netjail_stop_run(void *cls, struct GNUNET_TESTING_Interpreter *is)
The run method starts the script which deletes the network namespaces.