From 79e8b6a6b9c056ce511334753ca837f972531f96 Mon Sep 17 00:00:00 2001 From: jsteube Date: Fri, 17 Feb 2017 08:59:22 +0100 Subject: [PATCH] Make sure to unlink the pidfile only if our instance created it, but even in case of error --- include/pidfile.h | 2 -- include/types.h | 2 ++ src/hashcat.c | 4 ---- src/pidfile.c | 18 ++++++++++-------- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/include/pidfile.h b/include/pidfile.h index 57af5e3f9..7a024689e 100644 --- a/include/pidfile.h +++ b/include/pidfile.h @@ -20,8 +20,6 @@ #include #endif // _WIN -void unlink_pidfile (hashcat_ctx_t *hashcat_ctx); - int pidfile_ctx_init (hashcat_ctx_t *hashcat_ctx); void pidfile_ctx_destroy (hashcat_ctx_t *hashcat_ctx); diff --git a/include/types.h b/include/types.h index 3ed9d4f76..196841e36 100644 --- a/include/types.h +++ b/include/types.h @@ -1319,6 +1319,8 @@ typedef struct pidfile_ctx pidfile_data_t *pd; + bool pidfile_written; + } pidfile_ctx_t; typedef struct kernel_rule diff --git a/src/hashcat.c b/src/hashcat.c index f672fa6af..67d1f67e4 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -1125,10 +1125,6 @@ int hashcat_session_execute (hashcat_ctx_t *hashcat_ctx) unlink_restore (hashcat_ctx); - // unlink the pidfile - - unlink_pidfile (hashcat_ctx); - // final update dictionary cache dictstat_write (hashcat_ctx); diff --git a/src/pidfile.c b/src/pidfile.c index 106820a0b..421040c6a 100644 --- a/src/pidfile.c +++ b/src/pidfile.c @@ -143,13 +143,6 @@ static int write_pidfile (hashcat_ctx_t *hashcat_ctx) return 0; } -void unlink_pidfile (hashcat_ctx_t *hashcat_ctx) -{ - pidfile_ctx_t *pidfile_ctx = hashcat_ctx->pidfile_ctx; - - unlink (pidfile_ctx->filename); -} - int pidfile_ctx_init (hashcat_ctx_t *hashcat_ctx) { folder_config_t *folder_config = hashcat_ctx->folder_config; @@ -158,11 +151,15 @@ int pidfile_ctx_init (hashcat_ctx_t *hashcat_ctx) hc_asprintf (&pidfile_ctx->filename, "%s/%s.pid", folder_config->session_dir, user_options->session); + pidfile_ctx->pidfile_written = false; + const int rc_init_pidfile = init_pidfile (hashcat_ctx); if (rc_init_pidfile == -1) return -1; - write_pidfile (hashcat_ctx); + const int rc = write_pidfile (hashcat_ctx); + + if (rc == 0) pidfile_ctx->pidfile_written = true; return 0; } @@ -171,6 +168,11 @@ void pidfile_ctx_destroy (hashcat_ctx_t *hashcat_ctx) { pidfile_ctx_t *pidfile_ctx = hashcat_ctx->pidfile_ctx; + if (pidfile_ctx->pidfile_written == true) + { + unlink (pidfile_ctx->filename); + } + hcfree (pidfile_ctx->filename); hcfree (pidfile_ctx->pd);