forked from hashcat/hashcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffinity.c
132 lines (97 loc) · 2.4 KB
/
affinity.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Author......: See docs/credits.txt
* License.....: MIT
*/
#include "common.h"
#include "types.h"
#include "memory.h"
#include "event.h"
#include "affinity.h"
#if defined (__APPLE__)
static void CPU_ZERO (cpu_set_t *cs)
{
cs->count = 0;
}
static void CPU_SET (int num, cpu_set_t *cs)
{
cs->count |= (1 << num);
}
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)
{
int core;
for (core = 0; core < (8 * (int) cpu_size); core++)
{
if (CPU_ISSET (core, cpu_set)) break;
}
thread_affinity_policy_data_t policy = { core };
return thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1);
}
#endif
#if defined (__FreeBSD__)
#include <pthread_np.h>
typedef cpuset_t cpu_set_t;
#endif
int set_cpu_affinity (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx)
{
#if defined (__CYGWIN__)
return 0;
#else
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;
#else
cpu_set_t cpuset;
CPU_ZERO (&cpuset);
#endif
char *devices = hcstrdup (user_options->cpu_affinity);
if (devices == NULL) return -1;
char *saveptr = NULL;
char *next = strtok_r (devices, ",", &saveptr);
do
{
const int cpu_id = (const int) strtol (next, NULL, 10);
if (cpu_id == 0)
{
#if defined (_WIN)
aff_mask = 0;
#else
CPU_ZERO (&cpuset);
#endif
break;
}
if (cpu_id > 32)
{
event_log_error (hashcat_ctx, "Invalid cpu_id %d specified.", cpu_id);
hcfree (devices);
return -1;
}
#if defined (_WIN)
aff_mask |= 1u << (cpu_id - 1);
#else
CPU_SET ((cpu_id - 1), &cpuset);
#endif
} while ((next = strtok_r ((char *) NULL, ",", &saveptr)) != NULL);
hcfree (devices);
#if defined (_WIN)
SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
if (SetThreadAffinityMask (GetCurrentThread (), aff_mask) == 0)
{
event_log_error (hashcat_ctx, "%s", "SetThreadAffinityMask().");
return -1;
}
#else
pthread_t thread = pthread_self ();
if (pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset) == -1)
{
event_log_error (hashcat_ctx, "%s", "pthread_setaffinity_np().");
return -1;
}
#endif
return 0;
#endif
}