2017-01-27 15:29:18 +00:00
|
|
|
/**
|
|
|
|
* Author......: See docs/credits.txt
|
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "event.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;
|
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
HCFILE fp;
|
2017-01-27 15:29:18 +00:00
|
|
|
|
2019-07-01 15:27:08 +00:00
|
|
|
if (hc_fopen (&fp, pidfile_filename, "rb") == false) return 0;
|
2017-01-27 15:29:18 +00:00
|
|
|
|
|
|
|
pidfile_data_t *pd = (pidfile_data_t *) hcmalloc (sizeof (pidfile_data_t));
|
|
|
|
|
2019-06-27 18:18:47 +00:00
|
|
|
const size_t nread = hc_fread (pd, sizeof (pidfile_data_t), 1, &fp);
|
2017-01-27 15:29:18 +00:00
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
hc_fclose (&fp);
|
2017-01-27 15:29:18 +00:00
|
|
|
|
|
|
|
if (nread != 1)
|
|
|
|
{
|
2018-11-14 08:32:06 +00:00
|
|
|
//event_log_error (hashcat_ctx, "Cannot read %s", pidfile_filename);
|
2017-01-27 15:29:18 +00:00
|
|
|
|
2017-02-14 15:58:18 +00:00
|
|
|
hcfree (pd);
|
|
|
|
|
2018-11-14 08:32:06 +00:00
|
|
|
return 0;
|
2017-01-27 15:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pd->pid)
|
|
|
|
{
|
2017-02-23 23:55:06 +00:00
|
|
|
#if defined (_WIN)
|
2017-01-27 15:29:18 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2017-02-14 15:40:08 +00:00
|
|
|
hcfree (pd);
|
|
|
|
|
|
|
|
hcfree (pidbin);
|
|
|
|
hcfree (pidbin2);
|
|
|
|
|
2017-01-27 15:29:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hcfree (pidbin);
|
2017-02-14 15:40:08 +00:00
|
|
|
hcfree (pidbin2);
|
2017-01-27 15:29:18 +00:00
|
|
|
|
2017-02-23 23:55:06 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
char *pidbin;
|
|
|
|
|
2019-06-03 13:43:56 +00:00
|
|
|
hc_asprintf (&pidbin, "/proc/%u/exe", pd->pid);
|
2017-02-23 23:55:06 +00:00
|
|
|
|
|
|
|
if (hc_path_exist (pidbin) == true)
|
|
|
|
{
|
2019-06-03 13:43:56 +00:00
|
|
|
// pid info
|
|
|
|
|
|
|
|
char *pidexe = (char *) hcmalloc (HCBUFSIZ_TINY);
|
|
|
|
|
|
|
|
const ssize_t pidexe_len = readlink (pidbin, pidexe, HCBUFSIZ_TINY - 1);
|
|
|
|
|
|
|
|
pidexe[pidexe_len] = 0;
|
|
|
|
|
|
|
|
// self info
|
|
|
|
|
|
|
|
char *selfexe = (char *) hcmalloc (HCBUFSIZ_TINY);
|
|
|
|
|
|
|
|
const ssize_t selfexe_len = readlink ("/proc/self/exe", selfexe, HCBUFSIZ_TINY - 1);
|
|
|
|
|
|
|
|
selfexe[selfexe_len] = 0;
|
|
|
|
|
|
|
|
// compare
|
|
|
|
|
|
|
|
const int r = strncmp (pidexe, selfexe, selfexe_len);
|
|
|
|
|
|
|
|
if (r == 0)
|
|
|
|
{
|
|
|
|
event_log_error (hashcat_ctx, "Already an instance '%s' running on pid %u", pidexe, pd->pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
hcfree (selfexe);
|
|
|
|
|
|
|
|
hcfree (pidexe);
|
2017-02-23 23:55:06 +00:00
|
|
|
|
|
|
|
hcfree (pd);
|
|
|
|
|
|
|
|
hcfree (pidbin);
|
|
|
|
|
2019-06-03 13:43:56 +00:00
|
|
|
if (r == 0) return -1;
|
|
|
|
|
|
|
|
return 0;
|
2017-02-23 23:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hcfree (pidbin);
|
|
|
|
|
2017-01-27 15:29:18 +00:00
|
|
|
#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;
|
|
|
|
|
2017-02-23 23:55:06 +00:00
|
|
|
#if defined (_WIN)
|
2017-01-27 15:29:18 +00:00
|
|
|
pd->pid = GetCurrentProcessId ();
|
2017-02-23 23:55:06 +00:00
|
|
|
#else
|
|
|
|
pd->pid = getpid ();
|
2017-01-27 15:29:18 +00:00
|
|
|
#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;
|
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
HCFILE fp;
|
2017-01-27 15:29:18 +00:00
|
|
|
|
2019-07-01 15:27:08 +00:00
|
|
|
if (hc_fopen (&fp, pidfile_filename, "wb") == false)
|
2017-01-27 15:29:18 +00:00
|
|
|
{
|
2017-02-04 01:53:50 +00:00
|
|
|
event_log_error (hashcat_ctx, "%s: %s", pidfile_filename, strerror (errno));
|
2017-01-27 15:29:18 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:18:47 +00:00
|
|
|
hc_fwrite (pd, sizeof (pidfile_data_t), 1, &fp);
|
2017-01-27 15:29:18 +00:00
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
hc_fflush (&fp);
|
|
|
|
|
|
|
|
hc_fclose (&fp);
|
2017-01-27 15:29:18 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2017-02-17 07:59:22 +00:00
|
|
|
pidfile_ctx->pidfile_written = false;
|
|
|
|
|
2017-01-27 15:29:18 +00:00
|
|
|
const int rc_init_pidfile = init_pidfile (hashcat_ctx);
|
|
|
|
|
|
|
|
if (rc_init_pidfile == -1) return -1;
|
|
|
|
|
2017-02-17 07:59:22 +00:00
|
|
|
const int rc = write_pidfile (hashcat_ctx);
|
|
|
|
|
|
|
|
if (rc == 0) pidfile_ctx->pidfile_written = true;
|
2017-01-27 15:29:18 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pidfile_ctx_destroy (hashcat_ctx_t *hashcat_ctx)
|
|
|
|
{
|
|
|
|
pidfile_ctx_t *pidfile_ctx = hashcat_ctx->pidfile_ctx;
|
|
|
|
|
2017-02-17 07:59:22 +00:00
|
|
|
if (pidfile_ctx->pidfile_written == true)
|
|
|
|
{
|
|
|
|
unlink (pidfile_ctx->filename);
|
|
|
|
}
|
|
|
|
|
2017-01-27 15:29:18 +00:00
|
|
|
hcfree (pidfile_ctx->filename);
|
|
|
|
|
|
|
|
hcfree (pidfile_ctx->pd);
|
|
|
|
|
|
|
|
memset (pidfile_ctx, 0, sizeof (pidfile_ctx_t));
|
|
|
|
}
|