1
mirror of https://github.com/hashcat/hashcat synced 2024-11-13 17:28:58 +01:00

Move restore specific functions into their own source file

This commit is contained in:
jsteube 2016-09-07 22:01:34 +02:00
parent 1fc22dba03
commit a56855e355
13 changed files with 370 additions and 306 deletions

55
include/restore.h Normal file
View File

@ -0,0 +1,55 @@
/**
* Author......: Jens Steube <jens.steube@gmail.com>
* License.....: MIT
*/
#ifndef _RESTORE_H
#define _RESTORE_H
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#ifdef _POSIX
#include <sys/types.h>
#include <sys/stat.h>
#endif // _POSIX
#ifdef _WIN
#include <windows.h>
#endif // _WIN
#define RESTORE 0
#define RESTORE_TIMER 60
#define RESTORE_DISABLE 0
#define RESTORE_VERSION_MIN 320
#define RESTORE_VERSION_CUR 320
typedef struct
{
int version;
char cwd[256];
u32 pid;
u32 dictpos;
u32 maskpos;
u64 words_cur;
u32 argc;
char **argv;
} restore_data_t;
u64 get_lowest_words_done ();
restore_data_t *init_restore (int argc, char **argv);
void read_restore (const char *eff_restore_file, restore_data_t *rd);
void write_restore (const char *new_restore_file, restore_data_t *rd);
void cycle_restore ();
#endif // _RESTORE_H

View File

@ -63,9 +63,6 @@
* valid project specific global stuff
*/
extern const uint VERSION_BIN;
extern const uint RESTORE_MIN;
extern const char *PROMPT;
extern hc_thread_mutex_t mux_display;
@ -139,12 +136,8 @@ void myquit (void);
void naive_replace (char *s, const u8 key_char, const u8 replace_char);
void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_char);
u64 get_lowest_words_done (void);
restore_data_t *init_restore (int argc, char **argv);
void read_restore (const char *eff_restore_file, restore_data_t *rd);
void write_restore (const char *new_restore_file, restore_data_t *rd);
void cycle_restore (void);
void check_checkpoint (void);
#ifdef _WIN

View File

@ -252,21 +252,7 @@ typedef struct
} comb_t;
typedef struct
{
u32 version_bin;
char cwd[256];
u32 pid;
u32 dictpos;
u32 maskpos;
u64 words_cur;
u32 argc;
char **argv;
} restore_data_t;
typedef struct
{

View File

@ -166,7 +166,7 @@ LFLAGS_CROSS_WIN := -lpsapi
## Objects
##
NATIVE_OBJS := obj/bitmap.NATIVE.o obj/opencl.NATIVE.o obj/affinity.NATIVE.o obj/filehandling.NATIVE.o obj/tuningdb.NATIVE.o obj/locking.NATIVE.o obj/folder.NATIVE.o obj/bitops.NATIVE.o obj/convert.NATIVE.o obj/cpu_aes.NATIVE.o obj/cpu_crc32.NATIVE.o obj/cpu_des.NATIVE.o obj/cpu_md5.NATIVE.o obj/cpu_sha1.NATIVE.o obj/cpu_sha256.NATIVE.o obj/data.NATIVE.o obj/ext_OpenCL.NATIVE.o obj/hwmon.NATIVE.o obj/interface.NATIVE.o obj/logfile.NATIVE.o obj/logging.NATIVE.o obj/memory.NATIVE.o obj/mpsp.NATIVE.o obj/rp_cpu.NATIVE.o obj/rp_kernel_on_cpu.NATIVE.o obj/shared.NATIVE.o obj/terminal.NATIVE.o obj/usage.NATIVE.o
NATIVE_OBJS := obj/restore.NATIVE.o obj/bitmap.NATIVE.o obj/opencl.NATIVE.o obj/affinity.NATIVE.o obj/filehandling.NATIVE.o obj/tuningdb.NATIVE.o obj/locking.NATIVE.o obj/folder.NATIVE.o obj/bitops.NATIVE.o obj/convert.NATIVE.o obj/cpu_aes.NATIVE.o obj/cpu_crc32.NATIVE.o obj/cpu_des.NATIVE.o obj/cpu_md5.NATIVE.o obj/cpu_sha1.NATIVE.o obj/cpu_sha256.NATIVE.o obj/data.NATIVE.o obj/ext_OpenCL.NATIVE.o obj/hwmon.NATIVE.o obj/interface.NATIVE.o obj/logfile.NATIVE.o obj/logging.NATIVE.o obj/memory.NATIVE.o obj/mpsp.NATIVE.o obj/rp_cpu.NATIVE.o obj/rp_kernel_on_cpu.NATIVE.o obj/shared.NATIVE.o obj/terminal.NATIVE.o obj/usage.NATIVE.o
ifeq ($(UNAME),Linux)
NATIVE_OBJS += obj/ext_ADL.NATIVE.o
@ -175,8 +175,8 @@ NATIVE_OBJS += obj/ext_nvml.NATIVE.o
NATIVE_OBJS += obj/ext_xnvctrl.NATIVE.o
endif
LINUX_32_OBJS := obj/bitmap.LINUX.32.o obj/opencl.LINUX.32.o obj/affinity.LINUX.32.o obj/filehandling.LINUX.32.o obj/tuningdb.LINUX.32.o obj/locking.LINUX.32.o obj/folder.LINUX.32.o obj/bitops.LINUX.32.o obj/convert.LINUX.32.o obj/cpu_aes.LINUX.32.o obj/cpu_crc32.LINUX.32.o obj/cpu_des.LINUX.32.o obj/cpu_md5.LINUX.32.o obj/cpu_sha1.LINUX.32.o obj/cpu_sha256.LINUX.32.o obj/data.LINUX.32.o obj/ext_ADL.LINUX.32.o obj/ext_nvapi.LINUX.32.o obj/ext_nvml.LINUX.32.o obj/ext_OpenCL.LINUX.32.o obj/ext_xnvctrl.LINUX.32.o obj/hwmon.LINUX.32.o obj/interface.LINUX.32.o obj/logfile.LINUX.32.o obj/logging.LINUX.32.o obj/memory.LINUX.32.o obj/mpsp.LINUX.32.o obj/rp_cpu.LINUX.32.o obj/rp_kernel_on_cpu.LINUX.32.o obj/shared.LINUX.32.o obj/terminal.LINUX.32.o obj/usage.LINUX.32.o
LINUX_64_OBJS := obj/bitmap.LINUX.64.o obj/opencl.LINUX.64.o obj/affinity.LINUX.64.o obj/filehandling.LINUX.64.o obj/tuningdb.LINUX.64.o obj/locking.LINUX.64.o obj/folder.LINUX.64.o obj/bitops.LINUX.64.o obj/convert.LINUX.64.o obj/cpu_aes.LINUX.64.o obj/cpu_crc32.LINUX.64.o obj/cpu_des.LINUX.64.o obj/cpu_md5.LINUX.64.o obj/cpu_sha1.LINUX.64.o obj/cpu_sha256.LINUX.64.o obj/data.LINUX.64.o obj/ext_ADL.LINUX.64.o obj/ext_nvapi.LINUX.64.o obj/ext_nvml.LINUX.64.o obj/ext_OpenCL.LINUX.64.o obj/ext_xnvctrl.LINUX.64.o obj/hwmon.LINUX.64.o obj/interface.LINUX.64.o obj/logfile.LINUX.64.o obj/logging.LINUX.64.o obj/memory.LINUX.64.o obj/mpsp.LINUX.64.o obj/rp_cpu.LINUX.64.o obj/rp_kernel_on_cpu.LINUX.64.o obj/shared.LINUX.64.o obj/terminal.LINUX.64.o obj/usage.LINUX.64.o
LINUX_32_OBJS := obj/restore.LINUX.32.o obj/bitmap.LINUX.32.o obj/opencl.LINUX.32.o obj/affinity.LINUX.32.o obj/filehandling.LINUX.32.o obj/tuningdb.LINUX.32.o obj/locking.LINUX.32.o obj/folder.LINUX.32.o obj/bitops.LINUX.32.o obj/convert.LINUX.32.o obj/cpu_aes.LINUX.32.o obj/cpu_crc32.LINUX.32.o obj/cpu_des.LINUX.32.o obj/cpu_md5.LINUX.32.o obj/cpu_sha1.LINUX.32.o obj/cpu_sha256.LINUX.32.o obj/data.LINUX.32.o obj/ext_ADL.LINUX.32.o obj/ext_nvapi.LINUX.32.o obj/ext_nvml.LINUX.32.o obj/ext_OpenCL.LINUX.32.o obj/ext_xnvctrl.LINUX.32.o obj/hwmon.LINUX.32.o obj/interface.LINUX.32.o obj/logfile.LINUX.32.o obj/logging.LINUX.32.o obj/memory.LINUX.32.o obj/mpsp.LINUX.32.o obj/rp_cpu.LINUX.32.o obj/rp_kernel_on_cpu.LINUX.32.o obj/shared.LINUX.32.o obj/terminal.LINUX.32.o obj/usage.LINUX.32.o
LINUX_64_OBJS := obj/restore.LINUX.64.o obj/bitmap.LINUX.64.o obj/opencl.LINUX.64.o obj/affinity.LINUX.64.o obj/filehandling.LINUX.64.o obj/tuningdb.LINUX.64.o obj/locking.LINUX.64.o obj/folder.LINUX.64.o obj/bitops.LINUX.64.o obj/convert.LINUX.64.o obj/cpu_aes.LINUX.64.o obj/cpu_crc32.LINUX.64.o obj/cpu_des.LINUX.64.o obj/cpu_md5.LINUX.64.o obj/cpu_sha1.LINUX.64.o obj/cpu_sha256.LINUX.64.o obj/data.LINUX.64.o obj/ext_ADL.LINUX.64.o obj/ext_nvapi.LINUX.64.o obj/ext_nvml.LINUX.64.o obj/ext_OpenCL.LINUX.64.o obj/ext_xnvctrl.LINUX.64.o obj/hwmon.LINUX.64.o obj/interface.LINUX.64.o obj/logfile.LINUX.64.o obj/logging.LINUX.64.o obj/memory.LINUX.64.o obj/mpsp.LINUX.64.o obj/rp_cpu.LINUX.64.o obj/rp_kernel_on_cpu.LINUX.64.o obj/shared.LINUX.64.o obj/terminal.LINUX.64.o obj/usage.LINUX.64.o
# Windows CRT file globbing:
@ -186,8 +186,8 @@ CRT_GLOB_INCLUDE_FOLDER := $(dir $(lastword $(MAKEFILE_LIST)))
include $(CRT_GLOB_INCLUDE_FOLDER)/win_file_globbing.mk
WIN_32_OBJS := obj/bitmap.WIN.32.o obj/opencl.WIN.32.o obj/affinity.WIN.32.o obj/filehandling.WIN.32.o obj/tuningdb.WIN.32.o obj/locking.WIN.32.o obj/folder.WIN.32.o obj/bitops.WIN.32.o obj/convert.WIN.32.o obj/cpu_aes.WIN.32.o obj/cpu_crc32.WIN.32.o obj/cpu_des.WIN.32.o obj/cpu_md5.WIN.32.o obj/cpu_sha1.WIN.32.o obj/cpu_sha256.WIN.32.o obj/data.WIN.32.o obj/ext_ADL.WIN.32.o obj/ext_nvapi.WIN.32.o obj/ext_nvml.WIN.32.o obj/ext_OpenCL.WIN.32.o obj/ext_xnvctrl.WIN.32.o obj/hwmon.WIN.32.o obj/interface.WIN.32.o obj/logfile.WIN.32.o obj/logging.WIN.32.o obj/memory.WIN.32.o obj/mpsp.WIN.32.o obj/rp_cpu.WIN.32.o obj/rp_kernel_on_cpu.WIN.32.o obj/shared.WIN.32.o obj/terminal.WIN.32.o obj/usage.WIN.32.o $(CRT_GLOB_32)
WIN_64_OBJS := obj/bitmap.WIN.64.o obj/opencl.WIN.64.o obj/affinity.WIN.64.o obj/filehandling.WIN.64.o obj/tuningdb.WIN.64.o obj/locking.WIN.64.o obj/folder.WIN.64.o obj/bitops.WIN.64.o obj/convert.WIN.64.o obj/cpu_aes.WIN.64.o obj/cpu_crc32.WIN.64.o obj/cpu_des.WIN.64.o obj/cpu_md5.WIN.64.o obj/cpu_sha1.WIN.64.o obj/cpu_sha256.WIN.64.o obj/data.WIN.64.o obj/ext_ADL.WIN.64.o obj/ext_nvapi.WIN.64.o obj/ext_nvml.WIN.64.o obj/ext_OpenCL.WIN.64.o obj/ext_xnvctrl.WIN.64.o obj/hwmon.WIN.64.o obj/interface.WIN.64.o obj/logfile.WIN.64.o obj/logging.WIN.64.o obj/memory.WIN.64.o obj/mpsp.WIN.64.o obj/rp_cpu.WIN.64.o obj/rp_kernel_on_cpu.WIN.64.o obj/shared.WIN.64.o obj/terminal.WIN.64.o obj/usage.WIN.64.o $(CRT_GLOB_64)
WIN_32_OBJS := obj/restore.WIN.32.o obj/bitmap.WIN.32.o obj/opencl.WIN.32.o obj/affinity.WIN.32.o obj/filehandling.WIN.32.o obj/tuningdb.WIN.32.o obj/locking.WIN.32.o obj/folder.WIN.32.o obj/bitops.WIN.32.o obj/convert.WIN.32.o obj/cpu_aes.WIN.32.o obj/cpu_crc32.WIN.32.o obj/cpu_des.WIN.32.o obj/cpu_md5.WIN.32.o obj/cpu_sha1.WIN.32.o obj/cpu_sha256.WIN.32.o obj/data.WIN.32.o obj/ext_ADL.WIN.32.o obj/ext_nvapi.WIN.32.o obj/ext_nvml.WIN.32.o obj/ext_OpenCL.WIN.32.o obj/ext_xnvctrl.WIN.32.o obj/hwmon.WIN.32.o obj/interface.WIN.32.o obj/logfile.WIN.32.o obj/logging.WIN.32.o obj/memory.WIN.32.o obj/mpsp.WIN.32.o obj/rp_cpu.WIN.32.o obj/rp_kernel_on_cpu.WIN.32.o obj/shared.WIN.32.o obj/terminal.WIN.32.o obj/usage.WIN.32.o $(CRT_GLOB_32)
WIN_64_OBJS := obj/restore.WIN.64.o obj/bitmap.WIN.64.o obj/opencl.WIN.64.o obj/affinity.WIN.64.o obj/filehandling.WIN.64.o obj/tuningdb.WIN.64.o obj/locking.WIN.64.o obj/folder.WIN.64.o obj/bitops.WIN.64.o obj/convert.WIN.64.o obj/cpu_aes.WIN.64.o obj/cpu_crc32.WIN.64.o obj/cpu_des.WIN.64.o obj/cpu_md5.WIN.64.o obj/cpu_sha1.WIN.64.o obj/cpu_sha256.WIN.64.o obj/data.WIN.64.o obj/ext_ADL.WIN.64.o obj/ext_nvapi.WIN.64.o obj/ext_nvml.WIN.64.o obj/ext_OpenCL.WIN.64.o obj/ext_xnvctrl.WIN.64.o obj/hwmon.WIN.64.o obj/interface.WIN.64.o obj/logfile.WIN.64.o obj/logging.WIN.64.o obj/memory.WIN.64.o obj/mpsp.WIN.64.o obj/rp_cpu.WIN.64.o obj/rp_kernel_on_cpu.WIN.64.o obj/shared.WIN.64.o obj/terminal.WIN.64.o obj/usage.WIN.64.o $(CRT_GLOB_64)
##
## Targets: Global

View File

@ -15,6 +15,7 @@
#include "hwmon.h"
#include "mpsp.h"
#include "rp_cpu.h"
#include "restore.h"
#include "opencl.h"
#include "data.h"

View File

@ -57,6 +57,7 @@
#include "shared.h"
#include "hwmon.h"
#include "mpsp.h"
#include "restore.h"
#include "data.h"
#include "affinity.h"
#include "bitmap.h"
@ -66,10 +67,8 @@ extern hc_global_data_t data;
extern int SUPPRESS_OUTPUT;
static const char *PROGNAME = "hashcat";
const uint VERSION_BIN = 310;
const uint RESTORE_MIN = 300;
static const char *PROGNAME = "hashcat";
static double TARGET_MS_PROFILE[4] = { 2, 12, 96, 480 };
@ -86,9 +85,7 @@ static double TARGET_MS_PROFILE[4] = { 2, 12, 96, 480 };
#define MARKOV_CLASSIC 0
#define BENCHMARK 0
#define STDOUT_FLAG 0
#define RESTORE 0
#define RESTORE_TIMER 60
#define RESTORE_DISABLE 0
#define STATUS 0
#define STATUS_TIMER 10
#define MACHINE_READABLE 0
@ -6529,7 +6526,7 @@ int main (int argc, char **argv)
{
read_restore (eff_restore_file, rd);
if (rd->version_bin < RESTORE_MIN)
if (rd->version < RESTORE_VERSION_MIN)
{
log_error ("ERROR: Incompatible restore-file version");

View File

@ -19,6 +19,7 @@
#include "hwmon.h"
#include "mpsp.h"
#include "rp_cpu.h"
#include "restore.h"
#include "opencl.h"
#include "data.h"

View File

@ -34,6 +34,7 @@
#include "mpsp.h"
#include "rp_cpu.h"
#include "opencl.h"
#include "restore.h"
#include "data.h"
#include "interface.h"

View File

@ -24,6 +24,7 @@
#include "rp_cpu.h"
#include "mpsp.h"
#include "opencl.h"
#include "restore.h"
#include "data.h"
#include "logfile.h"

View File

@ -27,6 +27,7 @@
#include "mpsp.h"
#include "rp_cpu.h"
#include "opencl.h"
#include "restore.h"
#include "data.h"
#include "shared.h"

297
src/restore.c Normal file
View File

@ -0,0 +1,297 @@
/**
* Authors.....: Jens Steube <jens.steube@gmail.com>
* License.....: MIT
*/
#include "common.h"
#include "types_int.h"
#include "types.h"
#include "timer.h"
#include "memory.h"
#include "logging.h"
#include "ext_OpenCL.h"
#include "ext_ADL.h"
#include "ext_nvapi.h"
#include "ext_nvml.h"
#include "ext_xnvctrl.h"
#include "thread.h"
#include "rp_cpu.h"
#include "hwmon.h"
#include "mpsp.h"
#include "opencl.h"
#include "restore.h"
#include "data.h"
extern hc_global_data_t data;
#ifdef _WIN
static void fsync (int fd)
{
HANDLE h = (HANDLE) _get_osfhandle (fd);
FlushFileBuffers (h);
}
#endif
u64 get_lowest_words_done ()
{
u64 words_cur = -1llu;
for (uint device_id = 0; device_id < data.devices_cnt; device_id++)
{
hc_device_param_t *device_param = &data.devices_param[device_id];
if (device_param->skipped) continue;
const u64 words_done = device_param->words_done;
if (words_done < words_cur) words_cur = words_done;
}
// It's possible that a device's workload isn't finished right after a restore-case.
// In that case, this function would return 0 and overwrite the real restore point
// There's also data.words_cur which is set to rd->words_cur but it changes while
// the attack is running therefore we should stick to rd->words_cur.
// Note that -s influences rd->words_cur we should keep a close look on that.
if (words_cur < data.rd->words_cur) words_cur = data.rd->words_cur;
return words_cur;
}
restore_data_t *init_restore (int argc, char **argv)
{
restore_data_t *rd = (restore_data_t *) mymalloc (sizeof (restore_data_t));
if (data.restore_disable == 0)
{
FILE *fp = fopen (data.eff_restore_file, "rb");
if (fp)
{
size_t nread = fread (rd, sizeof (restore_data_t), 1, fp);
if (nread != 1)
{
log_error ("ERROR: Cannot read %s", data.eff_restore_file);
exit (-1);
}
fclose (fp);
if (rd->pid)
{
char *pidbin = (char *) mymalloc (HCBUFSIZ);
int pidbin_len = -1;
#ifdef _POSIX
snprintf (pidbin, HCBUFSIZ - 1, "/proc/%d/cmdline", rd->pid);
FILE *fd = fopen (pidbin, "rb");
if (fd)
{
pidbin_len = fread (pidbin, 1, HCBUFSIZ, fd);
pidbin[pidbin_len] = 0;
fclose (fd);
char *argv0_r = strrchr (argv[0], '/');
char *pidbin_r = strrchr (pidbin, '/');
if (argv0_r == NULL) argv0_r = argv[0];
if (pidbin_r == NULL) pidbin_r = pidbin;
if (strcmp (argv0_r, pidbin_r) == 0)
{
log_error ("ERROR: Already an instance %s running on pid %d", pidbin, rd->pid);
exit (-1);
}
}
#elif _WIN
HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, rd->pid);
char *pidbin2 = (char *) mymalloc (HCBUFSIZ);
int pidbin2_len = -1;
pidbin_len = GetModuleFileName (NULL, pidbin, HCBUFSIZ);
pidbin2_len = GetModuleFileNameEx (hProcess, NULL, pidbin2, HCBUFSIZ);
pidbin[pidbin_len] = 0;
pidbin2[pidbin2_len] = 0;
if (pidbin2_len)
{
if (strcmp (pidbin, pidbin2) == 0)
{
log_error ("ERROR: Already an instance %s running on pid %d", pidbin2, rd->pid);
exit (-1);
}
}
myfree (pidbin2);
#endif
myfree (pidbin);
}
if (rd->version < RESTORE_VERSION_MIN)
{
log_error ("ERROR: Cannot use outdated %s. Please remove it.", data.eff_restore_file);
exit (-1);
}
}
}
memset (rd, 0, sizeof (restore_data_t));
rd->version = RESTORE_VERSION_CUR;
#ifdef _POSIX
rd->pid = getpid ();
#elif _WIN
rd->pid = GetCurrentProcessId ();
#endif
if (getcwd (rd->cwd, 255) == NULL)
{
myfree (rd);
return (NULL);
}
rd->argc = argc;
rd->argv = argv;
return (rd);
}
void read_restore (const char *eff_restore_file, restore_data_t *rd)
{
FILE *fp = fopen (eff_restore_file, "rb");
if (fp == NULL)
{
log_error ("ERROR: Restore file '%s': %s", eff_restore_file, strerror (errno));
exit (-1);
}
if (fread (rd, sizeof (restore_data_t), 1, fp) != 1)
{
log_error ("ERROR: Can't read %s", eff_restore_file);
exit (-1);
}
rd->argv = (char **) mycalloc (rd->argc, sizeof (char *));
char *buf = (char *) mymalloc (HCBUFSIZ);
for (uint i = 0; i < rd->argc; i++)
{
if (fgets (buf, HCBUFSIZ - 1, fp) == NULL)
{
log_error ("ERROR: Can't read %s", eff_restore_file);
exit (-1);
}
size_t len = strlen (buf);
if (len) buf[len - 1] = 0;
rd->argv[i] = mystrdup (buf);
}
myfree (buf);
fclose (fp);
log_info ("INFO: Changing current working directory to the path found within the .restore file: '%s'", rd->cwd);
if (chdir (rd->cwd))
{
log_error ("ERROR: The directory '%s' does not exist. It is needed to restore (--restore) the session.\n"
" You could either create this directory (or link it) or update the .restore file using e.g. the analyze_hc_restore.pl tool:\n"
" https://github.com/philsmd/analyze_hc_restore\n"
" The directory must be relative to (or contain) all files/folders mentioned within the command line.", rd->cwd);
exit (-1);
}
}
void write_restore (const char *new_restore_file, restore_data_t *rd)
{
u64 words_cur = get_lowest_words_done ();
rd->words_cur = words_cur;
FILE *fp = fopen (new_restore_file, "wb");
if (fp == NULL)
{
log_error ("ERROR: %s: %s", new_restore_file, strerror (errno));
exit (-1);
}
if (setvbuf (fp, NULL, _IONBF, 0))
{
log_error ("ERROR: setvbuf file '%s': %s", new_restore_file, strerror (errno));
exit (-1);
}
fwrite (rd, sizeof (restore_data_t), 1, fp);
for (uint i = 0; i < rd->argc; i++)
{
fprintf (fp, "%s", rd->argv[i]);
fputc ('\n', fp);
}
fflush (fp);
fsync (fileno (fp));
fclose (fp);
}
void cycle_restore ()
{
const char *eff_restore_file = data.eff_restore_file;
const char *new_restore_file = data.new_restore_file;
restore_data_t *rd = data.rd;
write_restore (new_restore_file, rd);
struct stat st;
memset (&st, 0, sizeof(st));
if (stat (eff_restore_file, &st) == 0)
{
if (unlink (eff_restore_file))
{
log_info ("WARN: Unlink file '%s': %s", eff_restore_file, strerror (errno));
}
}
if (rename (new_restore_file, eff_restore_file))
{
log_info ("WARN: Rename file '%s' to '%s': %s", new_restore_file, eff_restore_file, strerror (errno));
}
}

View File

@ -26,6 +26,7 @@
#include "mpsp.h"
#include "rp_cpu.h"
#include "opencl.h"
#include "restore.h"
#include "data.h"
#include "shared.h"
#include "rp_cpu.h"

View File

@ -30,6 +30,7 @@
#include "mpsp.h"
#include "rp_cpu.h"
#include "opencl.h"
#include "restore.h"
#include "data.h"
#include "shared.h"
@ -1100,278 +1101,6 @@ void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_cha
* restore
*/
restore_data_t *init_restore (int argc, char **argv)
{
restore_data_t *rd = (restore_data_t *) mymalloc (sizeof (restore_data_t));
if (data.restore_disable == 0)
{
FILE *fp = fopen (data.eff_restore_file, "rb");
if (fp)
{
size_t nread = fread (rd, sizeof (restore_data_t), 1, fp);
if (nread != 1)
{
log_error ("ERROR: Cannot read %s", data.eff_restore_file);
exit (-1);
}
fclose (fp);
if (rd->pid)
{
char *pidbin = (char *) mymalloc (HCBUFSIZ);
int pidbin_len = -1;
#ifdef _POSIX
snprintf (pidbin, HCBUFSIZ - 1, "/proc/%d/cmdline", rd->pid);
FILE *fd = fopen (pidbin, "rb");
if (fd)
{
pidbin_len = fread (pidbin, 1, HCBUFSIZ, fd);
pidbin[pidbin_len] = 0;
fclose (fd);
char *argv0_r = strrchr (argv[0], '/');
char *pidbin_r = strrchr (pidbin, '/');
if (argv0_r == NULL) argv0_r = argv[0];
if (pidbin_r == NULL) pidbin_r = pidbin;
if (strcmp (argv0_r, pidbin_r) == 0)
{
log_error ("ERROR: Already an instance %s running on pid %d", pidbin, rd->pid);
exit (-1);
}
}
#elif _WIN
HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, rd->pid);
char *pidbin2 = (char *) mymalloc (HCBUFSIZ);
int pidbin2_len = -1;
pidbin_len = GetModuleFileName (NULL, pidbin, HCBUFSIZ);
pidbin2_len = GetModuleFileNameEx (hProcess, NULL, pidbin2, HCBUFSIZ);
pidbin[pidbin_len] = 0;
pidbin2[pidbin2_len] = 0;
if (pidbin2_len)
{
if (strcmp (pidbin, pidbin2) == 0)
{
log_error ("ERROR: Already an instance %s running on pid %d", pidbin2, rd->pid);
exit (-1);
}
}
myfree (pidbin2);
#endif
myfree (pidbin);
}
if (rd->version_bin < RESTORE_MIN)
{
log_error ("ERROR: Cannot use outdated %s. Please remove it.", data.eff_restore_file);
exit (-1);
}
}
}
memset (rd, 0, sizeof (restore_data_t));
rd->version_bin = VERSION_BIN;
#ifdef _POSIX
rd->pid = getpid ();
#elif _WIN
rd->pid = GetCurrentProcessId ();
#endif
if (getcwd (rd->cwd, 255) == NULL)
{
myfree (rd);
return (NULL);
}
rd->argc = argc;
rd->argv = argv;
return (rd);
}
void read_restore (const char *eff_restore_file, restore_data_t *rd)
{
FILE *fp = fopen (eff_restore_file, "rb");
if (fp == NULL)
{
log_error ("ERROR: Restore file '%s': %s", eff_restore_file, strerror (errno));
exit (-1);
}
if (fread (rd, sizeof (restore_data_t), 1, fp) != 1)
{
log_error ("ERROR: Can't read %s", eff_restore_file);
exit (-1);
}
rd->argv = (char **) mycalloc (rd->argc, sizeof (char *));
char *buf = (char *) mymalloc (HCBUFSIZ);
for (uint i = 0; i < rd->argc; i++)
{
if (fgets (buf, HCBUFSIZ - 1, fp) == NULL)
{
log_error ("ERROR: Can't read %s", eff_restore_file);
exit (-1);
}
size_t len = strlen (buf);
if (len) buf[len - 1] = 0;
rd->argv[i] = mystrdup (buf);
}
myfree (buf);
fclose (fp);
log_info ("INFO: Changing current working directory to the path found within the .restore file: '%s'", rd->cwd);
if (chdir (rd->cwd))
{
log_error ("ERROR: The directory '%s' does not exist. It is needed to restore (--restore) the session.\n"
" You could either create this directory (or link it) or update the .restore file using e.g. the analyze_hc_restore.pl tool:\n"
" https://github.com/philsmd/analyze_hc_restore\n"
" The directory must be relative to (or contain) all files/folders mentioned within the command line.", rd->cwd);
exit (-1);
}
}
u64 get_lowest_words_done ()
{
u64 words_cur = -1llu;
for (uint device_id = 0; device_id < data.devices_cnt; device_id++)
{
hc_device_param_t *device_param = &data.devices_param[device_id];
if (device_param->skipped) continue;
const u64 words_done = device_param->words_done;
if (words_done < words_cur) words_cur = words_done;
}
// It's possible that a device's workload isn't finished right after a restore-case.
// In that case, this function would return 0 and overwrite the real restore point
// There's also data.words_cur which is set to rd->words_cur but it changes while
// the attack is running therefore we should stick to rd->words_cur.
// Note that -s influences rd->words_cur we should keep a close look on that.
if (words_cur < data.rd->words_cur) words_cur = data.rd->words_cur;
return words_cur;
}
#ifdef _WIN
static void fsync (int fd)
{
HANDLE h = (HANDLE) _get_osfhandle (fd);
FlushFileBuffers (h);
}
#endif
void write_restore (const char *new_restore_file, restore_data_t *rd)
{
u64 words_cur = get_lowest_words_done ();
rd->words_cur = words_cur;
FILE *fp = fopen (new_restore_file, "wb");
if (fp == NULL)
{
log_error ("ERROR: %s: %s", new_restore_file, strerror (errno));
exit (-1);
}
if (setvbuf (fp, NULL, _IONBF, 0))
{
log_error ("ERROR: setvbuf file '%s': %s", new_restore_file, strerror (errno));
exit (-1);
}
fwrite (rd, sizeof (restore_data_t), 1, fp);
for (uint i = 0; i < rd->argc; i++)
{
fprintf (fp, "%s", rd->argv[i]);
fputc ('\n', fp);
}
fflush (fp);
fsync (fileno (fp));
fclose (fp);
}
void cycle_restore ()
{
const char *eff_restore_file = data.eff_restore_file;
const char *new_restore_file = data.new_restore_file;
restore_data_t *rd = data.rd;
write_restore (new_restore_file, rd);
struct stat st;
memset (&st, 0, sizeof(st));
if (stat (eff_restore_file, &st) == 0)
{
if (unlink (eff_restore_file))
{
log_info ("WARN: Unlink file '%s': %s", eff_restore_file, strerror (errno));
}
}
if (rename (new_restore_file, eff_restore_file))
{
log_info ("WARN: Rename file '%s' to '%s': %s", new_restore_file, eff_restore_file, strerror (errno));
}
}
void check_checkpoint ()
{
// if (data.restore_disable == 1) break; (this is already implied by previous checks)
@ -1385,6 +1114,7 @@ void check_checkpoint ()
}
/**
* parallel running threads
*/