GNUnet 0.21.1
testing_api_cmd_exec_bash_script.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2023 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"
29
30#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
31
33{
38
44
45 // Child Wait handle
47
52
56 const char *script;
57
58
62 char *const*script_argv;
63
67 int argc;
68};
69
74static void
76{
77 struct BashScriptState *bss = cls;
78
79 if (NULL != bss->cwh)
80 {
82 "Cancel child\n");
84 bss->cwh = NULL;
85 }
86 if (NULL != bss->start_proc)
87 {
89 "Kill process\n");
90 GNUNET_assert (0 ==
92 SIGKILL));
96 bss->start_proc = NULL;
97 }
98 GNUNET_free (bss);
99}
100
105static void
108 long unsigned int exit_code)
109{
110 struct BashScriptState *bss = cls;
111
113 bss->start_proc = NULL;
114 bss->cwh = NULL;
115 if (0 == exit_code)
116 {
118 "Child succeeded!\n");
120 }
121 else
122 {
124 "Child failed with error %lu!\n",
125 exit_code);
127 }
128 bss->cb (cls, type, exit_code);
129}
130
136static void
139{
140 struct BashScriptState *bss = cls;
141 enum GNUNET_GenericReturnValue helper_check;
142 char *argv[bss->argc + 2];
143
144 char *data_dir;
145 char *script_name;
146
148 GNUNET_asprintf (&script_name, "%s%s", data_dir, bss->script);
149
150 helper_check = GNUNET_OS_check_helper_binary (
151 script_name,
153 NULL);
154
156 "script_name %s\n",
157 script_name);
158
159 if (GNUNET_NO == helper_check)
160 {
162 "No SUID for %s!\n",
163 script_name);
165 return;
166 }
167 if (GNUNET_SYSERR == helper_check)
168 {
170 "%s not found!\n",
171 script_name);
173 return;
174 }
175 argv[0] = script_name;
176 if (NULL != bss->script_argv)
177 {
178 for (int i = 0; i < bss->argc;i++)
179 argv[i + 1] = bss->script_argv[i];
180 }
181 argv[bss->argc] = NULL;
182
184 NULL,
185 NULL,
186 NULL,
187 script_name,
188 argv);
189 bss->cwh = GNUNET_wait_child (bss->start_proc,
191 bss);
192 GNUNET_break (NULL != bss->cwh);
193}
194
195const struct GNUNET_TESTING_Command
197 const char *script,
198 char *const script_argv[],
199 int argc,
201{
202 struct BashScriptState *bss;
203
204 bss = GNUNET_new (struct BashScriptState);
205 bss->script = script;
206 bss->script_argv = script_argv; // FIXME this is not just a cast to fix
207 bss->argc = argc;
208 bss->cb = cb;
209
210 return GNUNET_TESTING_command_new (bss,
211 label,
214 NULL,
215 &bss->ac);
216}
struct GNUNET_TESTING_Interpreter * is
static uint32_t type
Type string converted to DNS type value.
static int exit_code
Global exit code.
Definition: gnunet-qr.c:43
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.
struct GNUNET_TESTING_Command GNUNET_TESTING_command_new(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.
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.
void(* GNUNET_ChildCompletedCallback)(void *cls, enum GNUNET_OS_ProcessStatusType type, long unsigned int exit_code)
Defines a GNUNET_ChildCompletedCallback which is sent back upon death or completion of a child proces...
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ 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.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
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,...
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:260
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.
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:210
enum GNUNET_GenericReturnValue GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:877
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:573
@ 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/)
struct GNUNET_TESTING_AsyncContext ac
Context for our asynchronous completion.
struct GNUNET_ChildWaitHandle * cwh
const char * script
Script this cmd will execute.
char *const * script_argv
Arguments for the script.
int argc
Size of script_argv.
GNUNET_ChildCompletedCallback cb
Callback handed over to the command, which should be called upon death or completion of the script.
struct GNUNET_OS_Process * start_proc
The process id of the script.
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.
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.
const struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_exec_bash_script(const char *label, const char *script, char *const script_argv[], int argc, GNUNET_ChildCompletedCallback cb)
Command to execute a script synchronously.
static void exec_bash_script_cleanup(void *cls)
The cleanup function of this cmd frees resources the cmd allocated.
#define LOG(kind,...)
static void exec_bash_script_run(void *cls, struct GNUNET_TESTING_Interpreter *is)
Run method of the command created by the interpreter to wait for another command to finish.