From bcee67abd0b4df888f9d6ce3f7fc62283e85315c Mon Sep 17 00:00:00 2001 From: Richard Scheffenegger Date: Mon, 3 Oct 2022 11:45:25 +0200 Subject: [PATCH] execute all scripts local to the script directory for relative paths to always work --- gtests/net/packetdrill/run.c | 18 ++++++++++++++++++ gtests/net/packetdrill/run_command.c | 10 ++++++++++ 2 files changed, 28 insertions(+) diff --git a/gtests/net/packetdrill/run.c b/gtests/net/packetdrill/run.c index 0c9a36b6..6523d52a 100644 --- a/gtests/net/packetdrill/run.c +++ b/gtests/net/packetdrill/run.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -541,14 +542,22 @@ int run_cleanup_command(void) { if (cleanup_cmd != NULL && init_cmd_exed) { char *error = NULL; + char cwd[300]; + char *sp = NULL; + getcwd((char *)&cwd, sizeof(cwd)); + sp = strdup(script_path); + chdir(dirname(sp)); + free(sp); if (safe_system(cleanup_cmd, &error)) { fprintf(stderr, "%s: error executing cleanup command: %s\n", script_path, error); free(error); + chdir(cwd); return STATUS_ERR; } + chdir(cwd); } return STATUS_OK; } @@ -594,13 +603,22 @@ void run_script(struct config *config, struct script *script) init_cmd_exed = false; if (script->init_command != NULL) { + char cwd[300]; + char *sp = NULL; + + getcwd((char *)&cwd, sizeof(cwd)); + sp = strdup(state->config->script_path); + chdir(dirname(sp)); + free(sp); if (safe_system(script->init_command->command_line, &error)) { asprintf(&error, "%s: error executing init command: %s\n", config->script_path, error); free(error); + chdir(cwd); exit(EXIT_FAILURE); } + chdir(cwd); init_cmd_exed = true; } diff --git a/gtests/net/packetdrill/run_command.c b/gtests/net/packetdrill/run_command.c index a55e596d..e39660c1 100644 --- a/gtests/net/packetdrill/run_command.c +++ b/gtests/net/packetdrill/run_command.c @@ -36,6 +36,14 @@ void run_command_event( struct state *state, struct event *event, struct command_spec *command) { + char *script_path = NULL; + char cwd[300]; + + getcwd((char *)&cwd, sizeof(cwd)); + script_path = strdup(state->config->script_path); + chdir(dirname(script_path)); + free(script_path); + DEBUGP("%d: command: `%s`\n", event->line_number, command->command_line); @@ -45,9 +53,11 @@ void run_command_event( char *error = NULL; if (safe_system(command->command_line, &error)) goto error_out; + chdir(cwd); return; error_out: + chdir(cwd); die("%s:%d: error executing `%s` command: %s\n", state->config->script_path, event->line_number, command->command_line, error);