582b86ff6e3a2e9d55d3970c399a33fc8691b937
[hashcat.git] / src / ext_xnvctrl.c
1 /**
2 * Authors.....: Jens Steube <jens.steube@gmail.com>
3 * License.....: MIT
4 */
5
6 #include <ext_xnvctrl.h>
7
8 int xnvctrl_init (XNVCTRL_PTR *xnvctrl)
9 {
10 if (!xnvctrl) return (-1);
11
12 memset (xnvctrl, 0, sizeof (XNVCTRL_PTR));
13
14 #ifdef _WIN
15
16 // unsupport platform?
17 return (-1);
18
19 #elif _POSIX
20
21 xnvctrl->lib_x11 = dlopen ("libX11.so", RTLD_LAZY);
22
23 if (xnvctrl->lib_x11 == NULL)
24 {
25 if (data.quiet == 0) log_info ("WARNING: load X11 library failed, proceed without X11 HWMon enabled.");
26
27 return -1;
28 }
29
30 xnvctrl->lib_xnvctrl = dlopen ("libXNVCtrl.so", RTLD_LAZY);
31
32 if (xnvctrl->lib_xnvctrl == NULL)
33 {
34 xnvctrl->lib_xnvctrl = dlopen ("libXNVCtrl.so.0", RTLD_LAZY);
35
36 if (xnvctrl->lib_xnvctrl == NULL)
37 {
38 if (data.quiet == 0) log_info ("WARNING: load XNVCTRL library failed, proceed without XNVCTRL HWMon enabled.");
39
40 return -1;
41 }
42 }
43
44 xnvctrl->XOpenDisplay = dlsym (xnvctrl->lib_x11, "XOpenDisplay");
45 xnvctrl->XCloseDisplay = dlsym (xnvctrl->lib_x11, "XCloseDisplay");
46
47 xnvctrl->XNVCTRLQueryTargetAttribute = dlsym (xnvctrl->lib_xnvctrl, "XNVCTRLQueryTargetAttribute");
48 xnvctrl->XNVCTRLSetTargetAttribute = dlsym (xnvctrl->lib_xnvctrl, "XNVCTRLSetTargetAttribute");
49
50 #endif
51
52 // not using HC_LOAD_FUNC() here, because we're using 2 libraries and therefore have 2 different variable names for them
53
54 return 0;
55 }
56
57 void xnvctrl_close (XNVCTRL_PTR *xnvctrl)
58 {
59 if (xnvctrl)
60 {
61 #if _POSIX
62
63 if (xnvctrl->lib_x11)
64 {
65 dlclose (xnvctrl->lib_x11);
66 }
67
68 if (xnvctrl->lib_xnvctrl)
69 {
70 dlclose (xnvctrl->lib_xnvctrl);
71 }
72
73 #endif
74
75 myfree (xnvctrl);
76 }
77 }
78
79 int hm_XNVCTRL_XOpenDisplay (XNVCTRL_PTR *xnvctrl)
80 {
81 void *dpy = xnvctrl->XOpenDisplay (NULL);
82
83 if (dpy == NULL)
84 {
85 xnvctrl->dpy = NULL;
86
87 return -1;
88 }
89
90 xnvctrl->dpy = dpy;
91
92 return 0;
93 }
94
95 void hm_XNVCTRL_XCloseDisplay (XNVCTRL_PTR *xnvctrl)
96 {
97 if (xnvctrl->dpy == NULL) return;
98
99 xnvctrl->XCloseDisplay (xnvctrl->dpy);
100 }
101
102 int get_fan_control (XNVCTRL_PTR *xnvctrl, int gpu, int *val)
103 {
104 if (xnvctrl->dpy == NULL) return -1;
105
106 int rc = xnvctrl->XNVCTRLQueryTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_GPU, gpu, 0, NV_CTRL_GPU_COOLER_MANUAL_CONTROL, val);
107
108 if (!rc) return -1;
109
110 return 0;
111 }
112
113 int set_fan_control (XNVCTRL_PTR *xnvctrl, int gpu, int val)
114 {
115 if (xnvctrl->dpy == NULL) return -1;
116
117 xnvctrl->XNVCTRLSetTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_GPU, gpu, 0, NV_CTRL_GPU_COOLER_MANUAL_CONTROL, val);
118
119 int cur;
120
121 int rc = get_fan_control (xnvctrl, gpu, &cur);
122
123 if (rc == -1) return -1;
124
125 if (cur != val) return -1;
126
127 return 0;
128 }
129
130 int get_core_threshold (XNVCTRL_PTR *xnvctrl, int gpu, int *val)
131 {
132 if (xnvctrl->dpy == NULL) return -1;
133
134 int rc = xnvctrl->XNVCTRLQueryTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_GPU, gpu, 0, NV_CTRL_GPU_CORE_THRESHOLD, val);
135
136 if (!rc) return -1;
137
138 return 0;
139 }
140
141 int get_fan_speed_current (XNVCTRL_PTR *xnvctrl, int gpu, int *val)
142 {
143 int rc = xnvctrl->XNVCTRLQueryTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_COOLER, gpu, 0, NV_CTRL_THERMAL_COOLER_CURRENT_LEVEL, val);
144
145 if (!rc) return -1;
146
147 return 0;
148 }
149
150 int get_fan_speed_target (XNVCTRL_PTR *xnvctrl, int gpu, int *val)
151 {
152 if (xnvctrl->dpy == NULL) return -1;
153
154 int rc = xnvctrl->XNVCTRLQueryTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_COOLER, gpu, 0, NV_CTRL_THERMAL_COOLER_LEVEL, val);
155
156 if (!rc) return -1;
157
158 return 0;
159 }
160
161 int set_fan_speed_target (XNVCTRL_PTR *xnvctrl, int gpu, int val)
162 {
163 if (xnvctrl->dpy == NULL) return -1;
164
165 xnvctrl->XNVCTRLSetTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_COOLER, gpu, 0, NV_CTRL_THERMAL_COOLER_LEVEL, val);
166
167 int cur;
168
169 int rc = get_fan_speed_target (xnvctrl, gpu, &cur);
170
171 if (rc == -1) return -1;
172
173 if (cur != val) return -1;
174
175 return 0;
176 }