Sessions: Move out handling of multiple instance from restore file into separate pidfile

pull/1003/head
jsteube 7 years ago
parent e2b2dc7ae0
commit ac5d274845

@ -40,6 +40,7 @@
- Events: Improved the maximum event message handling. event_log () will now also internally make sure that the message is properly terminated
- Files: Do several file and folder checks on startup rather than when they are actually used to avoid related error after eventual intense operations
- Helper: Added functions to check existence, type, read- and write-permissions and rewrite sources to use them instead of stat()
- Sessions: Move out handling of multiple instance from restore file into separate pidfile
* changes v3.20 -> v3.30:

@ -0,0 +1,29 @@
/**
* Author......: See docs/credits.txt
* License.....: MIT
*/
#ifndef _PIDFILE_H
#define _PIDFILE_H
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#if defined (_POSIX)
#include <sys/types.h>
#include <sys/stat.h>
#endif // _POSIX
#if defined (_WIN)
#include <windows.h>
#include <psapi.h>
#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);
#endif // _PIDFILE_H

@ -20,8 +20,8 @@
#include <psapi.h>
#endif // _WIN
#define RESTORE_VERSION_MIN 320
#define RESTORE_VERSION_CUR 330
#define RESTORE_VERSION_MIN 340
#define RESTORE_VERSION_CUR 340
int cycle_restore (hashcat_ctx_t *hashcat_ctx);

@ -1256,7 +1256,6 @@ typedef struct restore_data
{
int version;
char cwd[256];
u32 pid;
u32 dicts_pos;
u32 masks_pos;
@ -1268,6 +1267,12 @@ typedef struct restore_data
} restore_data_t;
typedef struct pidfile_data
{
u32 pid;
} pidfile_data_t;
typedef struct restore_ctx
{
bool enabled;
@ -1282,6 +1287,15 @@ typedef struct restore_ctx
} restore_ctx_t;
typedef struct pidfile_ctx
{
u32 pid;
char *filename;
pidfile_data_t *pd;
} pidfile_ctx_t;
typedef struct kernel_rule
{
u32 cmds[32];
@ -1819,6 +1833,7 @@ typedef struct hashcat_ctx
opencl_ctx_t *opencl_ctx;
outcheck_ctx_t *outcheck_ctx;
outfile_ctx_t *outfile_ctx;
pidfile_ctx_t *pidfile_ctx;
potfile_ctx_t *potfile_ctx;
restore_ctx_t *restore_ctx;
status_ctx_t *status_ctx;

@ -267,7 +267,7 @@ include $(CRT_GLOB_INCLUDE_FOLDER)/win_file_globbing.mk
## Objects
##
OBJS_ALL := affinity autotune benchmark bitmap bitops combinator common convert cpt cpu_aes cpu_crc32 cpu_des cpu_md4 cpu_md5 cpu_sha1 cpu_sha256 debugfile dictstat dispatch dynloader event ext_ADL ext_nvapi ext_nvml ext_OpenCL ext_sysfs ext_xnvctrl ext_lzma lzma_sdk/Alloc lzma_sdk/Lzma2Dec lzma_sdk/LzmaDec filehandling folder hashcat hashes hlfmt hwmon induct interface locking logfile loopback memory monitor mpsp opencl outfile_check outfile potfile restore rp rp_cpu rp_kernel_on_cpu shared status stdout straight terminal thread timer tuningdb usage user_options weak_hash wordlist
OBJS_ALL := affinity autotune benchmark bitmap bitops combinator common convert cpt cpu_aes cpu_crc32 cpu_des cpu_md4 cpu_md5 cpu_sha1 cpu_sha256 debugfile dictstat dispatch dynloader event ext_ADL ext_nvapi ext_nvml ext_OpenCL ext_sysfs ext_xnvctrl ext_lzma lzma_sdk/Alloc lzma_sdk/Lzma2Dec lzma_sdk/LzmaDec filehandling folder hashcat hashes hlfmt hwmon induct interface locking logfile loopback memory monitor mpsp opencl outfile_check outfile pidfile potfile restore rp rp_cpu rp_kernel_on_cpu shared status stdout straight terminal thread timer tuningdb usage user_options weak_hash wordlist
NATIVE_STATIC_OBJS := $(foreach OBJ,$(OBJS_ALL),obj/$(OBJ).NATIVE.STATIC.o)
NATIVE_SHARED_OBJS := $(foreach OBJ,$(OBJS_ALL),obj/$(OBJ).NATIVE.SHARED.o)
@ -293,7 +293,7 @@ WIN_64_OBJS := $(foreach OBJ,$(OBJS_ALL),obj/$(OBJ).WIN.64.o) $(CRT_
default: hashcat
clean:
$(RM) -f obj/*.o obj/lzma_sdk/*.o *.bin *.exe *.so *.dll *.restore *.out *.pot *.log hashcat core
$(RM) -f obj/*.o obj/lzma_sdk/*.o *.bin *.exe *.so *.dll *.restore *.out *.pid *.log hashcat core
$(RM) -rf *.induct
$(RM) -rf *.outfiles
$(RM) -rf *.dSYM

@ -39,6 +39,7 @@
#include "opencl.h"
#include "outfile_check.h"
#include "outfile.h"
#include "pidfile.h"
#include "potfile.h"
#include "restore.h"
#include "rp.h"
@ -809,6 +810,7 @@ int hashcat_init (hashcat_ctx_t *hashcat_ctx, void (*event) (const u32, struct h
hashcat_ctx->opencl_ctx = (opencl_ctx_t *) hcmalloc (sizeof (opencl_ctx_t));
hashcat_ctx->outcheck_ctx = (outcheck_ctx_t *) hcmalloc (sizeof (outcheck_ctx_t));
hashcat_ctx->outfile_ctx = (outfile_ctx_t *) hcmalloc (sizeof (outfile_ctx_t));
hashcat_ctx->pidfile_ctx = (pidfile_ctx_t *) hcmalloc (sizeof (pidfile_ctx_t));
hashcat_ctx->potfile_ctx = (potfile_ctx_t *) hcmalloc (sizeof (potfile_ctx_t));
hashcat_ctx->restore_ctx = (restore_ctx_t *) hcmalloc (sizeof (restore_ctx_t));
hashcat_ctx->status_ctx = (status_ctx_t *) hcmalloc (sizeof (status_ctx_t));
@ -840,6 +842,7 @@ void hashcat_destroy (hashcat_ctx_t *hashcat_ctx)
hcfree (hashcat_ctx->opencl_ctx);
hcfree (hashcat_ctx->outcheck_ctx);
hcfree (hashcat_ctx->outfile_ctx);
hcfree (hashcat_ctx->pidfile_ctx);
hcfree (hashcat_ctx->potfile_ctx);
hcfree (hashcat_ctx->restore_ctx);
hcfree (hashcat_ctx->status_ctx);
@ -880,6 +883,14 @@ int hashcat_session_init (hashcat_ctx_t *hashcat_ctx, char *install_folder, char
if (rc_folder_config_init == -1) return -1;
/**
* pidfile
*/
const int rc_pidfile_init = pidfile_ctx_init (hashcat_ctx);
if (rc_pidfile_init == -1) return -1;
/**
* restore
*/
@ -1104,6 +1115,10 @@ 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);
@ -1172,6 +1187,7 @@ int hashcat_session_destroy (hashcat_ctx_t *hashcat_ctx)
opencl_ctx_devices_destroy (hashcat_ctx);
outcheck_ctx_destroy (hashcat_ctx);
outfile_destroy (hashcat_ctx);
pidfile_ctx_destroy (hashcat_ctx);
potfile_destroy (hashcat_ctx);
restore_ctx_destroy (hashcat_ctx);
tuning_db_destroy (hashcat_ctx);

@ -0,0 +1,168 @@
/**
* Author......: See docs/credits.txt
* License.....: MIT
*/
#include "common.h"
#include "types.h"
#include "memory.h"
#include "event.h"
#include "user_options.h"
#include "shared.h"
#include "pidfile.h"
static int check_running_process (hashcat_ctx_t *hashcat_ctx)
{
pidfile_ctx_t *pidfile_ctx = hashcat_ctx->pidfile_ctx;
char *pidfile_filename = pidfile_ctx->filename;
FILE *fp = fopen (pidfile_filename, "rb");
if (fp == NULL) return 0;
pidfile_data_t *pd = (pidfile_data_t *) hcmalloc (sizeof (pidfile_data_t));
const size_t nread = fread (pd, sizeof (pidfile_data_t), 1, fp);
fclose (fp);
if (nread != 1)
{
event_log_error (hashcat_ctx, "Cannot read %s", pidfile_filename);
return -1;
}
if (pd->pid)
{
#if defined (_POSIX)
char *pidbin;
hc_asprintf (&pidbin, "/proc/%u/cmdline", pd->pid);
if (hc_path_exist (pidbin) == true)
{
event_log_error (hashcat_ctx, "Already an instance running on pid %u", pd->pid);
return -1;
}
hcfree (pidbin);
#elif defined (_WIN)
HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pd->pid);
char *pidbin = (char *) hcmalloc (HCBUFSIZ_LARGE);
char *pidbin2 = (char *) hcmalloc (HCBUFSIZ_LARGE);
int pidbin_len = GetModuleFileName (NULL, pidbin, HCBUFSIZ_LARGE);
int pidbin2_len = GetModuleFileNameEx (hProcess, NULL, pidbin2, HCBUFSIZ_LARGE);
pidbin[pidbin_len] = 0;
pidbin2[pidbin2_len] = 0;
if (pidbin2_len)
{
if (strcmp (pidbin, pidbin2) == 0)
{
event_log_error (hashcat_ctx, "Already an instance %s running on pid %d", pidbin2, pd->pid);
return -1;
}
}
hcfree (pidbin2);
hcfree (pidbin);
#endif
}
hcfree (pd);
return 0;
}
static int init_pidfile (hashcat_ctx_t *hashcat_ctx)
{
pidfile_ctx_t *pidfile_ctx = hashcat_ctx->pidfile_ctx;
pidfile_data_t *pd = (pidfile_data_t *) hcmalloc (sizeof (pidfile_data_t));
pidfile_ctx->pd = pd;
const int rc = check_running_process (hashcat_ctx);
if (rc == -1) return -1;
#if defined (_POSIX)
pd->pid = getpid ();
#elif defined (_WIN)
pd->pid = GetCurrentProcessId ();
#endif
return 0;
}
static int write_pidfile (hashcat_ctx_t *hashcat_ctx)
{
const pidfile_ctx_t *pidfile_ctx = hashcat_ctx->pidfile_ctx;
pidfile_data_t *pd = pidfile_ctx->pd;
char *pidfile_filename = pidfile_ctx->filename;
FILE *fp = fopen (pidfile_filename, "wb");
if (fp == NULL)
{
event_log_error (hashcat_ctx, "%s: %m", pidfile_filename);
return -1;
}
fwrite (pd, sizeof (pidfile_data_t), 1, fp);
fflush (fp);
fclose (fp);
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;
pidfile_ctx_t *pidfile_ctx = hashcat_ctx->pidfile_ctx;
user_options_t *user_options = hashcat_ctx->user_options;
hc_asprintf (&pidfile_ctx->filename, "%s/%s.pid", folder_config->session_dir, user_options->session);
const int rc_init_pidfile = init_pidfile (hashcat_ctx);
if (rc_init_pidfile == -1) return -1;
write_pidfile (hashcat_ctx);
return 0;
}
void pidfile_ctx_destroy (hashcat_ctx_t *hashcat_ctx)
{
pidfile_ctx_t *pidfile_ctx = hashcat_ctx->pidfile_ctx;
hcfree (pidfile_ctx->filename);
hcfree (pidfile_ctx->pd);
memset (pidfile_ctx, 0, sizeof (pidfile_ctx_t));
}

@ -20,106 +20,6 @@ static void fsync (int fd)
}
#endif
static int check_running_process (hashcat_ctx_t *hashcat_ctx)
{
restore_ctx_t *restore_ctx = hashcat_ctx->restore_ctx;
char *eff_restore_file = restore_ctx->eff_restore_file;
FILE *fp = fopen (eff_restore_file, "rb");
if (fp == NULL) return 0;
restore_data_t *rd = (restore_data_t *) hcmalloc (sizeof (restore_data_t));
const size_t nread = fread (rd, sizeof (restore_data_t), 1, fp);
fclose (fp);
if (nread != 1)
{
event_log_error (hashcat_ctx, "Cannot read %s", eff_restore_file);
return -1;
}
if (rd->pid)
{
#if defined (_POSIX)
char *pidbin;
hc_asprintf (&pidbin, "/proc/%u/cmdline", rd->pid);
FILE *fd = fopen (pidbin, "rb");
if (fd)
{
size_t pidbin_len = fread (pidbin, 1, HCBUFSIZ_LARGE, fd);
pidbin[pidbin_len] = 0;
fclose (fd);
char *argv0_r = strrchr (restore_ctx->argv[0], '/');
char *pidbin_r = strrchr (pidbin, '/');
if (argv0_r == NULL) argv0_r = restore_ctx->argv[0];
if (pidbin_r == NULL) pidbin_r = pidbin;
if (strcmp (argv0_r, pidbin_r) == 0)
{
event_log_error (hashcat_ctx, "Already an instance %s running on pid %u", pidbin, rd->pid);
return -1;
}
}
hcfree (pidbin);
#elif defined (_WIN)
HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, rd->pid);
char *pidbin = (char *) hcmalloc (HCBUFSIZ_LARGE);
char *pidbin2 = (char *) hcmalloc (HCBUFSIZ_LARGE);
int pidbin_len = GetModuleFileName (NULL, pidbin, HCBUFSIZ_LARGE);
int pidbin2_len = GetModuleFileNameEx (hProcess, NULL, pidbin2, HCBUFSIZ_LARGE);
pidbin[pidbin_len] = 0;
pidbin2[pidbin2_len] = 0;
if (pidbin2_len)
{
if (strcmp (pidbin, pidbin2) == 0)
{
event_log_error (hashcat_ctx, "Already an instance %s running on pid %d", pidbin2, rd->pid);
return -1;
}
}
hcfree (pidbin2);
hcfree (pidbin);
#endif
}
if (rd->version < RESTORE_VERSION_MIN)
{
event_log_error (hashcat_ctx, "Cannot use outdated %s. Please remove it.", eff_restore_file);
return -1;
}
hcfree (rd);
return 0;
}
static int init_restore (hashcat_ctx_t *hashcat_ctx)
{
restore_ctx_t *restore_ctx = hashcat_ctx->restore_ctx;
@ -128,21 +28,11 @@ static int init_restore (hashcat_ctx_t *hashcat_ctx)
restore_ctx->rd = rd;
const int rc = check_running_process (hashcat_ctx);
if (rc == -1) return -1;
rd->version = RESTORE_VERSION_CUR;
rd->argc = restore_ctx->argc;
rd->argv = restore_ctx->argv;
#if defined (_POSIX)
rd->pid = getpid ();
#elif defined (_WIN)
rd->pid = GetCurrentProcessId ();
#endif
if (getcwd (rd->cwd, 255) == NULL)
{
event_log_error (hashcat_ctx, "getcwd(): %m");
@ -387,12 +277,6 @@ int restore_ctx_init (hashcat_ctx_t *hashcat_ctx, int argc, char **argv)
return -1;
}
#if defined (_POSIX)
rd->pid = getpid ();
#elif defined (_WIN)
rd->pid = GetCurrentProcessId ();
#endif
user_options_init (hashcat_ctx);
const int rc_options_getopt = user_options_getopt (hashcat_ctx, rd->argc, rd->argv);

@ -75,10 +75,11 @@ void goodbye_screen (hashcat_ctx_t *hashcat_ctx, const time_t proc_start, const
if (user_options->show == true) return;
if (user_options->left == true) return;
char start_buf[32], stop_buf[32];
char start_buf[32]; memset (start_buf, 0, sizeof (start_buf));
char stop_buf[32]; memset (start_buf, 0, sizeof (stop_buf));
event_log_info_nn (hashcat_ctx, "Started: %s", ctime_r (&proc_start, start_buf));
event_log_info_nn (hashcat_ctx, "Stopped: %s", ctime_r (&proc_stop, stop_buf));
event_log_info_nn (hashcat_ctx, "Stopped: %s", ctime_r (&proc_stop, stop_buf));
}
int setup_console ()

@ -1268,6 +1268,7 @@ int user_options_check_files (hashcat_ctx_t *hashcat_ctx)
logfile_ctx_t *logfile_ctx = hashcat_ctx->logfile_ctx;
outcheck_ctx_t *outcheck_ctx = hashcat_ctx->outcheck_ctx;
outfile_ctx_t *outfile_ctx = hashcat_ctx->outfile_ctx;
pidfile_ctx_t *pidfile_ctx = hashcat_ctx->pidfile_ctx;
potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx;
user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
user_options_t *user_options = hashcat_ctx->user_options;
@ -1551,6 +1552,18 @@ int user_options_check_files (hashcat_ctx_t *hashcat_ctx)
}
}
// pidfile
if (hc_path_exist (pidfile_ctx->filename) == true)
{
if (hc_path_write (pidfile_ctx->filename) == false)
{
event_log_error (hashcat_ctx, "%s: %m", pidfile_ctx->filename);
return -1;
}
}
// potfile
if (potfile_ctx->enabled == true)

Loading…
Cancel
Save