diff --git a/include/affinity.h b/include/affinity.h index 742fc8fb8..ff88f85a3 100644 --- a/include/affinity.h +++ b/include/affinity.h @@ -17,12 +17,13 @@ #if defined (__APPLE__) #include #include +#include #endif // __APPLE__ #if defined (_WIN) #include #endif // _WIN -void set_cpu_affinity (char *cpu_affinity); +int set_cpu_affinity (hashcat_ctx_t *hashcat_ctx); #endif // _AFFINITY_H diff --git a/src/affinity.c b/src/affinity.c index b408fb058..0837156fe 100644 --- a/src/affinity.c +++ b/src/affinity.c @@ -10,7 +10,7 @@ #include "common.h" #include "types.h" #include "memory.h" -#include "logging.h" +#include "event.h" #include "affinity.h" #if defined (__APPLE__) @@ -29,7 +29,7 @@ static int CPU_ISSET (int num, cpu_set_t *cs) return (cs->count & (1 << num)); } -static int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set) +static int pthread_setaffinity_np (hashcat_ctx_t *hashcat_ctx, pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set) { int core; @@ -40,78 +40,86 @@ static int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t thread_affinity_policy_data_t policy = { core }; - const int rc = thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1); - - if (rc != KERN_SUCCESS) - { - log_error ("ERROR: %s : %d", "thread_policy_set()", rc); - - exit (-1); - } - - return rc; + return thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1); } #endif -void set_cpu_affinity (char *cpu_affinity) +#if defined (__FreeBSD__) +typedef cpuset_t cpu_set_t; +#endif + +int set_cpu_affinity (hashcat_ctx_t *hashcat_ctx) { - #if defined(_WIN) + const user_options_t *user_options = hashcat_ctx->user_options; + + if (user_options->cpu_affinity == NULL) return 0; + + #if defined (_WIN) DWORD_PTR aff_mask = 0; - #elif defined(__FreeBSD__) - cpuset_t cpuset; - CPU_ZERO (&cpuset); - #elif defined(_POSIX) + #else cpu_set_t cpuset; CPU_ZERO (&cpuset); #endif - if (cpu_affinity) + char *devices = mystrdup (user_options->cpu_affinity); + + char *next = strtok (devices, ","); + + do { - char *devices = mystrdup (cpu_affinity); + int cpu_id = atoi (next); + + if (cpu_id == 0) + { + #if defined (_WIN) + aff_mask = 0; + #else + CPU_ZERO (&cpuset); + #endif - char *next = strtok (devices, ","); + break; + } - do + if (cpu_id > 32) { - int cpu_id = atoi (next); + event_log_error (hashcat_ctx, "ERROR: Invalid cpu_id %u specified", cpu_id); - if (cpu_id == 0) - { - #if defined (_WIN) - aff_mask = 0; - #elif defined (_POSIX) - CPU_ZERO (&cpuset); - #endif + return (-1); + } - break; - } + #if defined (_WIN) + aff_mask |= 1u << (cpu_id - 1); + #else + CPU_SET ((cpu_id - 1), &cpuset); + #endif - if (cpu_id > 32) - { - log_error ("ERROR: Invalid cpu_id %u specified", cpu_id); + } while ((next = strtok (NULL, ",")) != NULL); - exit (-1); - } + myfree (devices); - #if defined (_WIN) - aff_mask |= 1u << (cpu_id - 1); - #elif defined (_POSIX) - CPU_SET ((cpu_id - 1), &cpuset); - #endif + #if defined (_WIN) + + SetProcessAffinityMask (GetCurrentProcess (), aff_mask); - } while ((next = strtok (NULL, ",")) != NULL); + if (SetThreadAffinityMask (GetCurrentThread (), aff_mask) == 0) + { + event_log_error (hashcat_ctx, "ERROR: %s", "SetThreadAffinityMask()"); - myfree (devices); + return -1; } - #if defined( _WIN) - SetProcessAffinityMask (GetCurrentProcess (), aff_mask); - SetThreadAffinityMask (GetCurrentThread (), aff_mask); - #elif defined(__FreeBSD__) - pthread_t thread = pthread_self (); - pthread_setaffinity_np (thread, sizeof (cpuset_t), &cpuset); - #elif defined(_POSIX) + #else + pthread_t thread = pthread_self (); - pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset); + + if (pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset) == -1) + { + event_log_error (hashcat_ctx, "ERROR: %s", "pthread_setaffinity_np()"); + + return -1; + } + #endif + + return 0; } diff --git a/src/hashcat.c b/src/hashcat.c index 59f0d10c6..0ea3c91ca 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -845,17 +845,17 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold user_options_extra_init (hashcat_ctx); - // from here all user configuration is pre-processed so we can start logging + // from here all user configuration is pre-processed so we can start logging if we want to EVENT (EVENT_LOGFILE_TOP_INITIALIZE); /** - * To help users a bit + * cpu affinity */ - setup_environment_variables (); + const int rc_affinity = set_cpu_affinity (hashcat_ctx); - setup_umask (); + if (rc_affinity == -1) return -1; /** * prepare seeding for random number generator, required by logfile and rules generator @@ -863,6 +863,14 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold setup_seeding (user_options->rp_gen_seed_chgd, user_options->rp_gen_seed); + /** + * To help users a bit + */ + + setup_environment_variables (); + + setup_umask (); + /** * tuning db */ @@ -912,6 +920,7 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold const int rc_potfile_init = potfile_init (hashcat_ctx); if (rc_potfile_init == -1) return -1; + /** * dictstat init */ @@ -938,15 +947,6 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold if (rc_debugfile_init == -1) return -1; - /** - * cpu affinity - */ - - if (user_options->cpu_affinity) - { - set_cpu_affinity (user_options->cpu_affinity); - } - /** * Init OpenCL library loader */