diff --git a/include/common.h b/include/common.h index 680ddbe6c..806cd740a 100644 --- a/include/common.h +++ b/include/common.h @@ -28,6 +28,7 @@ #include #include #include +#include #ifdef _POSIX #include @@ -62,7 +63,6 @@ typedef void *HM_LIB; #include #include #include -#include typedef UINT8 uint8_t; typedef UINT16 uint16_t; diff --git a/include/shared.h b/include/shared.h index 259e93bdd..d1362f976 100644 --- a/include/shared.h +++ b/include/shared.h @@ -1832,6 +1832,14 @@ char *logfile_generate_topid (); char *logfile_generate_subid (); void logfile_append (const char *fmt, ...); +#if F_SETLKW +void lock_file (FILE *fp); +void unlock_file (FILE *fp); +#else +#define lock_file(dummy) {} +#define unlock_file(dummy) {} +#endif + #ifdef _WIN void fsync (int fd); #endif diff --git a/src/oclHashcat.c b/src/oclHashcat.c index 6c53b56af..ea5ef21ed 100644 --- a/src/oclHashcat.c +++ b/src/oclHashcat.c @@ -2081,6 +2081,8 @@ static void check_hash (hc_device_param_t *device_param, const uint salt_pos, co if (pot_fp) { + lock_file (pot_fp); + fprintf (pot_fp, "%s:", out_buf); format_plain (pot_fp, plain_ptr, plain_len, 1); @@ -2088,6 +2090,8 @@ static void check_hash (hc_device_param_t *device_param, const uint salt_pos, co fputc ('\n', pot_fp); fflush (pot_fp); + + unlock_file (pot_fp); } // outfile @@ -2102,6 +2106,7 @@ static void check_hash (hc_device_param_t *device_param, const uint salt_pos, co out_fp = stdout; } + lock_file (out_fp); } else { @@ -2141,6 +2146,8 @@ static void check_hash (hc_device_param_t *device_param, const uint salt_pos, co if ((fb_fp = fopen (loopback_file, "ab")) != NULL) { + lock_file (fb_fp); + format_plain (fb_fp, plain_ptr, plain_len, 1); fputc ('\n', fb_fp); @@ -16179,6 +16186,8 @@ int main (int argc, char **argv) if (dictstat_fp) { + lock_file (dictstat_fp); + fwrite (dictstat_base, sizeof (dictstat_t), dictstat_nmemb, dictstat_fp); fclose (dictstat_fp); diff --git a/src/shared.c b/src/shared.c index 7d12036ed..7058b64b9 100644 --- a/src/shared.c +++ b/src/shared.c @@ -2627,6 +2627,32 @@ char *logfile_generate_subid () * system */ +#if F_SETLKW +void lock_file (FILE *fp) +{ + struct flock lock = { 0 }; + + lock.l_type = F_WRLCK; + while (fcntl(fileno(fp), F_SETLKW, &lock)) + { + if (errno != EINTR) + { + log_error ("ERROR: failed acquiring write lock: %s", strerror (errno)); + + exit (-1); + } + } +} + +void unlock_file (FILE *fp) +{ + struct flock lock = { 0 }; + + lock.l_type = F_UNLCK; + fcntl(fileno(fp), F_SETLK, &lock); +} +#endif /* F_SETLKW */ + #ifdef _WIN void fsync (int fd) { @@ -4733,6 +4759,8 @@ void format_debug (char *debug_file, uint debug_mode, unsigned char *orig_plain_ if (debug_file != NULL) { debug_fp = fopen (debug_file, "ab"); + + lock_file (debug_fp); } else { @@ -8676,6 +8704,7 @@ void writeProgramBin (char *dst, u8 *binary, size_t binary_size) { FILE *fp = fopen (dst, "wb"); + lock_file (fp); fwrite (binary, sizeof (u8), binary_size, fp); fflush (fp);