From a52b96583f6988ed34d7b854fa6d72f16dfce45a Mon Sep 17 00:00:00 2001 From: magnum Date: Sat, 5 Nov 2016 11:33:29 +0100 Subject: [PATCH] Fix file locking (again). --- src/debugfile.c | 8 ++++++++ src/dictstat.c | 8 ++++++++ src/hashes.c | 8 ++++++++ src/locking.c | 6 +++++- src/logfile.c | 3 +++ src/loopback.c | 8 ++++++++ src/outfile.c | 8 ++++++++ src/potfile.c | 9 +++++++++ 8 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/debugfile.c b/src/debugfile.c index ff8ed38e6..e7a8cf03a 100644 --- a/src/debugfile.c +++ b/src/debugfile.c @@ -8,6 +8,7 @@ #include "memory.h" #include "event.h" #include "debugfile.h" +#include "locking.h" static void debugfile_format_plain (hashcat_ctx_t *hashcat_ctx, const u8 *plain_ptr, const u32 plain_len) { @@ -112,6 +113,13 @@ int debugfile_init (hashcat_ctx_t *hashcat_ctx) return -1; } + + if (lock_file (debugfile_ctx->fp) == -1) + { + event_log_error (hashcat_ctx, "%s: %s", debugfile_ctx->filename, strerror (errno)); + + return -1; + } } else { diff --git a/src/dictstat.c b/src/dictstat.c index b240da726..a331109fc 100644 --- a/src/dictstat.c +++ b/src/dictstat.c @@ -8,6 +8,7 @@ #include "memory.h" #include "event.h" #include "dictstat.h" +#include "locking.h" int sort_by_dictstat (const void *s1, const void *s2) { @@ -125,6 +126,13 @@ int dictstat_write (hashcat_ctx_t *hashcat_ctx) return -1; } + if (lock_file (fp) == -1) + { + event_log_error (hashcat_ctx, "%s: %s", dictstat_ctx->filename, strerror (errno)); + + return -1; + } + fwrite (dictstat_ctx->base, sizeof (dictstat_t), dictstat_ctx->cnt, fp); fclose (fp); diff --git a/src/hashes.c b/src/hashes.c index bfb6c79bd..bf97859d4 100644 --- a/src/hashes.c +++ b/src/hashes.c @@ -24,6 +24,7 @@ #include "shared.h" #include "thread.h" #include "timer.h" +#include "locking.h" int sort_by_digest_p0p1 (const void *v1, const void *v2, void *v3) { @@ -144,6 +145,13 @@ int save_hash (hashcat_ctx_t *hashcat_ctx) return -1; } + if (lock_file (fp) == -1) + { + event_log_error (hashcat_ctx, "%s: %s", new_hashfile, strerror (errno)); + + return -1; + } + for (u32 salt_pos = 0; salt_pos < hashes->salts_cnt; salt_pos++) { if (hashes->salts_shown[salt_pos] == 1) continue; diff --git a/src/locking.c b/src/locking.c index f2b80b57e..fbf207ede 100644 --- a/src/locking.c +++ b/src/locking.c @@ -18,7 +18,11 @@ int lock_file (FILE *fp) lock.l_type = F_WRLCK; - if (fcntl (fileno (fp), F_SETLKW, &lock)) return -1; + /* Needs this loop because a signal may interrupt a wait for lock */ + while (fcntl (fileno (fp), F_SETLKW, &lock)) + { + if (errno != EINTR) return -1; + } return 0; } diff --git a/src/logfile.c b/src/logfile.c index 44a289c6e..3a954b078 100644 --- a/src/logfile.c +++ b/src/logfile.c @@ -8,6 +8,7 @@ #include "memory.h" #include "event.h" #include "logfile.h" +#include "locking.h" static int logfile_generate_id (void) { @@ -50,6 +51,8 @@ void logfile_append (hashcat_ctx_t *hashcat_ctx, const char *fmt, ...) FILE *fp = fopen (logfile_ctx->logfile, "ab"); + lock_file (fp); + va_list ap; va_start (ap, fmt); diff --git a/src/loopback.c b/src/loopback.c index 7e47054d9..0f9812f7c 100644 --- a/src/loopback.c +++ b/src/loopback.c @@ -9,6 +9,7 @@ #include "event.h" #include "shared.h" #include "loopback.h" +#include "locking.h" static void loopback_format_plain (hashcat_ctx_t *hashcat_ctx, const u8 *plain_ptr, const unsigned int plain_len) { @@ -111,6 +112,13 @@ int loopback_write_open (hashcat_ctx_t *hashcat_ctx) return -1; } + if (lock_file (loopback_ctx->fp) == -1) + { + event_log_error (hashcat_ctx, "%s: %s", loopback_ctx->filename, strerror (errno)); + + return -1; + } + loopback_ctx->unused = true; return 0; diff --git a/src/outfile.c b/src/outfile.c index 1cb7c8645..1cf0a653b 100644 --- a/src/outfile.c +++ b/src/outfile.c @@ -16,6 +16,7 @@ #include "opencl.h" #include "shared.h" #include "outfile.h" +#include "locking.h" int build_plain (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain, u32 *plain_buf, int *out_len) { @@ -312,6 +313,13 @@ int outfile_write_open (hashcat_ctx_t *hashcat_ctx) return -1; } + if (lock_file (outfile_ctx->fp) == -1) + { + event_log_error (hashcat_ctx, "%s: %s", outfile_ctx->filename, strerror (errno)); + + return -1; + } + return 0; } diff --git a/src/potfile.c b/src/potfile.c index 3e7fd378c..9ca8eadc9 100644 --- a/src/potfile.c +++ b/src/potfile.c @@ -12,6 +12,7 @@ #include "filehandling.h" #include "outfile.h" #include "potfile.h" +#include "locking.h" #if defined (_WIN) #define __WINDOWS__ @@ -315,7 +316,15 @@ void potfile_write_append (hashcat_ctx_t *hashcat_ctx, const char *out_buf, u8 * tmp_buf[tmp_len] = 0; + if (lock_file (potfile_ctx->fp) == -1) + { + event_log_error (hashcat_ctx, "%s: %s", potfile_ctx->filename, strerror (errno)); + } + fprintf (potfile_ctx->fp, "%s" EOL, tmp_buf); + + fflush(potfile_ctx->fp); + unlock_file (potfile_ctx->fp); } int potfile_remove_parse (hashcat_ctx_t *hashcat_ctx)