GNUnet 0.27.0
 
Loading...
Searching...
No Matches
testing_api_cmd_netjail_start.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_lib.h"
30
31#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
32
38{
43
45
50
54 const char *topology_cmd_label;
55
59 const char *script;
60
61};
62
63
68static void
70{
71 struct NetJailState *ns = cls;
72
74 "netjail_start_cleanup!\n");
75 if (NULL != ns->cwh)
76 {
78 ns->cwh = NULL;
79 }
80 if (NULL != ns->start_proc)
81 {
83 GNUNET_process_kill (ns->start_proc,
84 SIGKILL));
86 GNUNET_process_wait (ns->start_proc,
87 true,
88 NULL,
89 NULL));
90 GNUNET_process_destroy (ns->start_proc);
91 ns->start_proc = NULL;
92 }
94}
95
96
100static void
103 unsigned long int exit_code)
104{
105 struct NetJailState *ns = cls;
106
107 GNUNET_process_destroy (ns->start_proc);
108 ns->start_proc = NULL;
109 ns->cwh = NULL;
110 if ( (GNUNET_OS_PROCESS_EXITED != type) ||
111 (0 != exit_code) )
112 {
114 "Child failed with error %lu!\n",
115 exit_code);
117 return;
118 }
120}
121
122
129static void
132{
133 struct NetJailState *ns = cls;
134 const struct GNUNET_TESTING_Command *topo_cmd;
135 char pid[15];
136 enum GNUNET_GenericReturnValue helper_check;
137 char *data_dir;
138 char *script_name;
139 const char *topology_data;
140
142 is,
143 ns->topology_cmd_label);
144 if (NULL == topo_cmd)
146 if (GNUNET_OK !=
147 GNUNET_TESTING_get_trait_topology_string (topo_cmd,
148 &topology_data))
153 GNUNET_asprintf (&script_name,
154 "%s%s",
155 data_dir,
156 ns->script);
157 helper_check = GNUNET_OS_check_helper_binary (
158 script_name,
159 true,
160 NULL);
161 if (GNUNET_NO == helper_check)
162 {
164 "No SUID for %s!\n",
165 script_name);
167 return;
168 }
169 if (GNUNET_SYSERR == helper_check)
170 {
172 "%s not found!\n",
173 script_name);
175 return;
176 }
177
179 sizeof (pid),
180 "%u",
181 getpid ());
183 if (GNUNET_OK !=
185 script_name,
186 script_name,
187 (char *) topology_data,
188 pid,
189 (char*) "0",
190 NULL))
191 {
192 GNUNET_break (0);
194 return;
195 }
196 ns->cwh = GNUNET_wait_child (ns->start_proc,
198 ns);
199 GNUNET_break (NULL != ns->cwh);
200 GNUNET_free (script_name);
201 GNUNET_free (data_dir);
202}
203
204
207 const char *label,
208 const char *script,
209 const char *topology_cmd_label)
210{
211 struct NetJailState *ns;
212
213 ns = GNUNET_new (struct NetJailState);
214 ns->script = script;
215 ns->topology_cmd_label = topology_cmd_label;
217 ns,
218 label,
221 NULL,
222 &ns->ac);
223}
static struct GNUNET_TESTING_Interpreter * is
static struct GNUNET_NAMECACHE_Handle * ns
Handle to the namecache.
static uint32_t type
Type string converted to DNS type value.
static int exit_code
Global exit code.
Definition gnunet-qr.c:43
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
struct GNUNET_TESTING_Command GNUNET_TESTING_command_new_ac(void *cls, const char *label, GNUNET_TESTING_CommandRunRoutine run, GNUNET_TESTING_CommandCleanupRoutine cleanup, GNUNET_TESTING_CommandGetTraits traits, struct GNUNET_TESTING_AsyncContext *ac)
Create a new command that may be asynchronous.
void GNUNET_TESTING_async_finish(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has finished.
#define GNUNET_TESTING_FAIL(is)
Print failing line number and trigger shutdown.
void GNUNET_TESTING_async_fail(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has failed.
const struct GNUNET_TESTING_Command * GNUNET_TESTING_interpreter_lookup_command(struct GNUNET_TESTING_Interpreter *is, const char *label)
Lookup command by label.
void GNUNET_TESTING_interpreter_skip(struct GNUNET_TESTING_Interpreter *is)
Skips the current test, the environment is not prepared correctly.
#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_Process *proc, GNUNET_ChildCompletedCallback cb, void *cb_cls)
Starts the handling of the child processes.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_NO
@ GNUNET_SYSERR
#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.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
char * GNUNET_OS_installation_get_path(const struct GNUNET_OS_ProjectData *pd, enum GNUNET_OS_InstallationPathKind dirkind)
Get the path to a specific GNUnet installation directory or, with GNUNET_OS_IPK_SELF_PREFIX,...
enum GNUNET_GenericReturnValue GNUNET_process_run_command_va(struct GNUNET_Process *p, const char *filename,...)
Set the command and start a process.
Definition os_process.c:903
enum GNUNET_GenericReturnValue GNUNET_process_wait(struct GNUNET_Process *proc, bool blocking, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Wait for a process to terminate.
void GNUNET_process_destroy(struct GNUNET_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition os_process.c:363
GNUNET_OS_ProcessStatusType
Process status types.
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.
enum GNUNET_GenericReturnValue GNUNET_process_kill(struct GNUNET_Process *proc, int sig)
Sends a signal to the process.
Definition os_process.c:307
struct GNUNET_Process * GNUNET_process_create(enum GNUNET_OS_InheritStdioFlags std_inheritance)
Create a process handle.
Definition os_process.c:462
@ GNUNET_OS_INHERIT_STD_ERR
When this flag is set, the child process will inherit stderr of the parent.
@ GNUNET_OS_PROCESS_EXITED
The process exited with a return code.
@ GNUNET_OS_IPK_DATADIR
Return the directory where data is installed (share/gnunet/)
Struct which defines a Child Wait handle.
State each asynchronous command must have in its closure.
A command to be run by the interpreter.
struct GNUNET_TESTING_CommandLabel label
Label for the command.
Global state of the interpreter, used by a command to access information about other commands.
Struct to hold information for callbacks.
struct GNUNET_ChildWaitHandle * cwh
const char * topology_cmd_label
Configuration file for the test topology.
struct GNUNET_Process * start_proc
The process id of the start script.
const char * script
Start or stop?
struct GNUNET_TESTING_AsyncContext ac
Context for our asynchronous completion.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_netjail_setup(const char *label, const char *script, const char *topology_cmd_label)
This command executes a shell script to setup the netjail environment.
static void child_completed_callback(void *cls, enum GNUNET_OS_ProcessStatusType type, unsigned long int exit_code)
Callback which will be called if the setup script finished.
static void netjail_start_run(void *cls, struct GNUNET_TESTING_Interpreter *is)
The run method starts the script which setup the network namespaces.
static void netjail_start_cleanup(void *cls)
The cleanup function of this cmd frees resources the cmd allocated.