Check permission to set fan speed before actually doing it, otherwise X11 becomes...
[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 int cur;
118
119 int rc = get_fan_control (xnvctrl, gpu, &cur);
120
121 if (rc == -1) return -1;
122
123 xnvctrl->XNVCTRLSetTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_GPU, gpu, 0, NV_CTRL_GPU_COOLER_MANUAL_CONTROL, val);
124
125 rc = get_fan_control (xnvctrl, gpu, &cur);
126
127 if (rc == -1) return -1;
128
129 if (cur != val) return -1;
130
131 return 0;
132 }
133
134 int get_core_threshold (XNVCTRL_PTR *xnvctrl, int gpu, int *val)
135 {
136 if (xnvctrl->dpy == NULL) return -1;
137
138 int rc = xnvctrl->XNVCTRLQueryTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_GPU, gpu, 0, NV_CTRL_GPU_CORE_THRESHOLD, val);
139
140 if (!rc) return -1;
141
142 return 0;
143 }
144
145 int get_fan_speed_current (XNVCTRL_PTR *xnvctrl, int gpu, int *val)
146 {
147 if (xnvctrl->dpy == NULL) return -1;
148
149 int rc = xnvctrl->XNVCTRLQueryTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_COOLER, gpu, 0, NV_CTRL_THERMAL_COOLER_CURRENT_LEVEL, val);
150
151 if (!rc) return -1;
152
153 return 0;
154 }
155
156 int get_fan_speed_target (XNVCTRL_PTR *xnvctrl, int gpu, int *val)
157 {
158 if (xnvctrl->dpy == NULL) return -1;
159
160 int rc = xnvctrl->XNVCTRLQueryTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_COOLER, gpu, 0, NV_CTRL_THERMAL_COOLER_LEVEL, val);
161
162 if (!rc) return -1;
163
164 return 0;
165 }
166
167 int set_fan_speed_target (XNVCTRL_PTR *xnvctrl, int gpu, int val)
168 {
169 if (xnvctrl->dpy == NULL) return -1;
170
171 int cur;
172
173 int rc = get_fan_speed_target (xnvctrl, gpu, &cur);
174
175 if (rc == -1) return -1;
176
177 xnvctrl->XNVCTRLSetTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_COOLER, gpu, 0, NV_CTRL_THERMAL_COOLER_LEVEL, val);
178
179 rc = get_fan_speed_target (xnvctrl, gpu, &cur);
180
181 if (rc == -1) return -1;
182
183 if (cur != val) return -1;
184
185 return 0;
186 }