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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
/*
* temperature.c: functions that populate the drive temperature variables
* in each drives context structure.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// #define _LARGEFILE64_SOURCE
// #define _FILE_OFFSET_BITS 64
#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <dirent.h>
#include <sys/time.h>
#include "nwipe.h"
#include "context.h"
#include "method.h"
#include "device.h"
#include "prng.h"
#include "options.h"
#include "device.h"
#include "logging.h"
#include "temperature.h"
#include "miscellaneous.h"
extern int terminate_signal;
int nwipe_init_temperature( nwipe_context_t* c )
{
/* See header definition for description of function
*/
DIR* dir;
DIR* dir2;
const char dirpath[] = "/sys/class/hwmon";
char dirpath_tmp[256];
char dirpath_tmp2[256];
char dirpath_hwmonX[256];
char device[256];
char device_context_name[256];
// const char dirpath[] = "/home/nick/mouse/hwmon1";
struct dirent* dp;
struct dirent* dp2;
/* Why Initialise with 1000000 (defined as NO_TEMPERATURE_DATA)?
* Because the GUI needs to know whether data has been obtained
* so it can display appropriate information when a
* device is unable to provide temperature data */
c->templ_has_hwmon_data = 0;
c->temp1_crit = NO_TEMPERATURE_DATA;
c->temp1_highest = NO_TEMPERATURE_DATA;
c->temp1_input = NO_TEMPERATURE_DATA;
c->temp1_lcrit = NO_TEMPERATURE_DATA;
c->temp1_lowest = NO_TEMPERATURE_DATA;
c->temp1_max = NO_TEMPERATURE_DATA;
c->temp1_min = NO_TEMPERATURE_DATA;
c->temp1_monitored_wipe_max = NO_TEMPERATURE_DATA;
c->temp1_monitored_wipe_min = NO_TEMPERATURE_DATA;
c->temp1_monitored_wipe_avg = NO_TEMPERATURE_DATA;
c->temp1_flash_rate = 0;
c->temp1_flash_rate_counter = 0;
c->temp1_path[0] = 0;
c->temp1_time = 0;
/* Each hwmonX directory is processed in turn and once a hwmonX directory has been
* found that is a block device and the block device name matches the drive
* name in the current context then the path to ../hwmonX is constructed and written
* to the drive context structure '* c'. This path is used in the nwipe_update_temperature
* function to retrieve temperature data and store it in the device context
*/
if( ( dir = opendir( dirpath ) ) != NULL )
{
/* Process each hwmonX sub directory in turn */
while( ( dp = readdir( dir ) ) != NULL )
{
/* Does the directory start with 'hwmon' */
if( strstr( dp->d_name, "hwmon" ) != NULL )
{
if( nwipe_options.verbose )
{
/* print a empty line to separate the different hwmon sensors */
nwipe_log( NWIPE_LOG_DEBUG, "hwmon:" );
}
strcpy( dirpath_tmp, dirpath );
strcat( dirpath_tmp, "/" );
strcat( dirpath_tmp, dp->d_name );
strcpy( dirpath_hwmonX, dirpath_tmp );
strcat( dirpath_tmp, "/device/block" );
/* Depending on the class of block device, the device name may
* appear in different sub-directories. So we try to open each
* directory that are known to contain block devices. These are
* /sys/class/hwmon/hwmonX/device/block
* /sys/class/hwmon/hwmonX/device/nvme/nvme0
* /sys/class/hwmon/hwmonX/device/
*/
if( ( dir2 = opendir( dirpath_tmp ) ) == NULL )
{
if( nwipe_options.verbose )
{
nwipe_log( NWIPE_LOG_DEBUG, "hwmon: %s doesn't exist, trying next path", dirpath_tmp );
}
strcpy( dirpath_tmp2, dirpath_hwmonX );
strcat( dirpath_tmp2, "/device/nvme/nvme0" );
strcpy( dirpath_tmp, dirpath_tmp2 );
if( ( dir2 = opendir( dirpath_tmp ) ) == NULL )
{
if( nwipe_options.verbose )
{
nwipe_log( NWIPE_LOG_DEBUG, "hwmon: %s doesn't exist, trying next path", dirpath_tmp );
}
strcpy( dirpath_tmp2, dirpath_hwmonX );
strcat( dirpath_tmp2, "/device" );
strcpy( dirpath_tmp, dirpath_tmp2 );
if( ( dir2 = opendir( dirpath_tmp ) ) == NULL )
{
if( nwipe_options.verbose )
{
nwipe_log(
NWIPE_LOG_DEBUG, "hwmon: %s doesn't exist, no more paths to try", dirpath_tmp );
}
continue;
}
}
}
if( dir2 != NULL )
{
if( nwipe_options.verbose )
{
nwipe_log( NWIPE_LOG_DEBUG, "hwmon: Found %s", dirpath_tmp );
}
/* Read the device name */
while( ( dp2 = readdir( dir2 ) ) != NULL )
{
if( nwipe_options.verbose )
{
nwipe_log( NWIPE_LOG_DEBUG, "hwmon: dirpath_tmp=%s/%s", dirpath_tmp, &dp2->d_name[0] );
}
/* Skip the '.' and '..' directories */
if( dp2->d_name[0] == '.' )
{
continue;
}
strcpy( device, dp2->d_name );
/* Create a copy of the device name from the context but strip the path from it, right justify
* device name, prefix with spaces so length is 8. */
nwipe_strip_path( device_context_name, c->device_name );
/* Remove leading/training whitespace from a string and left justify result */
trim( device_context_name );
/* Does the hwmon device match the device for this drive context */
if( strcmp( device, device_context_name ) != 0 )
{
/* No, so try next hwmon device */
continue;
}
else
{
/* Match ! This hwmon device matches this context, so write the hwmonX path to the context
*/
nwipe_log( NWIPE_LOG_NOTICE, "hwmon: %s has temperature monitoring", device, dirpath_tmp );
if( nwipe_options.verbose )
{
nwipe_log( NWIPE_LOG_DEBUG, "hwmon: %s found in %s", device, dirpath_tmp );
}
/* Copy the hwmon path to the drive context structure */
strcpy( c->temp1_path, dirpath_hwmonX );
c->templ_has_hwmon_data = 1;
}
}
closedir( dir2 );
}
}
}
closedir( dir );
}
/* if no hwmon data available try scsi access (SAS Disks are known to be not working in hwmon */
if( c->templ_has_hwmon_data == 0 && ( c->device_type == NWIPE_DEVICE_SAS || c->device_type == NWIPE_DEVICE_SCSI ) )
{
nwipe_log( NWIPE_LOG_NOTICE, "no hwmon data for %s, try to get SCSI data", c->device_name );
if( nwipe_init_scsi_temperature( c ) == 0 )
{
c->templ_has_scsitemp_data = 1;
nwipe_log( NWIPE_LOG_INFO, "got SCSI temperature data for %s", c->device_name );
}
else
{
c->templ_has_scsitemp_data = 0;
nwipe_log( NWIPE_LOG_INFO, "got no SCSI temperature data for %s", c->device_name );
}
}
return 0;
}
float timedifference_msec( struct timeval tv_start, struct timeval tv_end )
{
/* helper function for time measurement in msec */
return ( tv_end.tv_sec - tv_start.tv_sec ) * 1000.0f + ( tv_end.tv_usec - tv_start.tv_usec ) / 1000.0f;
}
void* nwipe_update_temperature_thread( void* ptr )
{
int i;
/* Set up the structs we will use for the data required. */
nwipe_thread_data_ptr_t* nwipe_thread_data_ptr;
nwipe_context_t** c;
nwipe_misc_thread_data_t* nwipe_misc_thread_data;
/* Retrieve from the pointer passed to the function. */
nwipe_thread_data_ptr = (nwipe_thread_data_ptr_t*) ptr;
c = nwipe_thread_data_ptr->c;
nwipe_misc_thread_data = nwipe_thread_data_ptr->nwipe_misc_thread_data;
/* mark start second of update */
time_t nwipe_timemark = time( NULL );
/* update immediately on entry to thread */
for( i = 0; i < nwipe_misc_thread_data->nwipe_enumerated; i++ )
{
nwipe_update_temperature( c[i] );
if( terminate_signal == 1 )
{
break;
}
}
while( terminate_signal != 1 )
{
/* Update all drive/s but never repeat checking the
* set of drive/s faster than once every 2 seconds */
if( time( NULL ) > ( nwipe_timemark + 1 ) )
{
nwipe_timemark = time( NULL );
for( i = 0; i < nwipe_misc_thread_data->nwipe_enumerated; i++ )
{
nwipe_update_temperature( c[i] );
}
}
else
{
sleep( 1 );
}
}
return NULL;
}
void nwipe_update_temperature( nwipe_context_t* c )
{
/* Warning !! This function should only be called by nwipe_update_temperature_thread()
* Due to delays of upto 2 seconds with some drives, especially SAS in obtaining
* temperatures while wiping, the delays being worse the more drives you are wiping. Updating
* temperatures are performed within it's own thread so it doesn't cause momentary freezes
* in the GUI interface.
*
* For the given drive context obtain the path to it's hwmon temperature settings
* and read then write the temperature values back to the context. A numeric ascii to integer conversion is
* performed. The temperaures should be updated no more frequently than every 60 seconds
*/
char temperature_label[NUMBER_OF_FILES][20] = {
"temp1_crit", "temp1_highest", "temp1_input", "temp1_lcrit", "temp1_lowest", "temp1_max", "temp1_min" };
int* temperature_pcontext[NUMBER_OF_FILES] = {
&( c->temp1_crit ),
&( c->temp1_highest ),
&( c->temp1_input ),
&( c->temp1_lcrit ),
&( c->temp1_lowest ),
&( c->temp1_max ),
&( c->temp1_min ) };
char path[256];
char temperature[256];
FILE* fptr;
int idx;
int result;
struct timeval tv_start;
struct timeval tv_end;
float delta_t;
/* avoid being called more often than 1x per 60 seconds */
time_t nwipe_time_now = time( NULL );
if( nwipe_time_now - c->temp1_time < 60 )
{
return;
}
/* measure time it takes to get the temperatures */
gettimeofday( &tv_start, 0 );
/* try to get temperatures from hwmon, standard */
if( c->templ_has_hwmon_data == 1 )
{
for( idx = 0; idx < NUMBER_OF_FILES; idx++ )
{
/* Construct the full path including filename */
strcpy( path, c->temp1_path );
strcat( path, "/" );
strcat( path, &( temperature_label[idx][0] ) );
/* Open the file */
if( ( fptr = fopen( path, "r" ) ) != NULL )
{
/* Acquire data until we reach a newline */
result = fscanf( fptr, "%[^\n]", temperature );
/* Convert numeric ascii to binary integer */
*( temperature_pcontext[idx] ) = atoi( temperature );
/* Divide by 1000 to get degrees celsius */
*( temperature_pcontext[idx] ) = *( temperature_pcontext[idx] ) / 1000;
if( nwipe_options.verbose )
{
nwipe_log( NWIPE_LOG_NOTICE, "hwmon: %s %dC", path, *( temperature_pcontext[idx] ) );
}
fclose( fptr );
}
else
{
if( nwipe_options.verbose )
{
nwipe_log( NWIPE_LOG_NOTICE, "hwmon: Unable to open %s", path );
}
}
}
}
else
{
/* alternative method to get temperature from SCSI/SAS disks */
if( c->device_type == NWIPE_DEVICE_SAS || c->device_type == NWIPE_DEVICE_SCSI )
{
if( c->templ_has_scsitemp_data == 1 )
{
if( nwipe_options.verbose )
{
nwipe_log( NWIPE_LOG_NOTICE, "hddtemp: %s temp1_crit %dC", c->device_name, c->temp1_crit );
nwipe_log( NWIPE_LOG_NOTICE, "hddtemp: %s temp1_highest %dC", c->device_name, c->temp1_highest );
nwipe_log( NWIPE_LOG_NOTICE, "hddtemp: %s temp1_input %dC", c->device_name, c->temp1_input );
nwipe_log( NWIPE_LOG_NOTICE, "hddtemp: %s temp1_lcrit %dC", c->device_name, c->temp1_lcrit );
nwipe_log( NWIPE_LOG_NOTICE, "hddtemp: %s temp1_lowest %dC", c->device_name, c->temp1_lowest );
nwipe_log( NWIPE_LOG_NOTICE, "hddtemp: %s temp1_max %dC", c->device_name, c->temp1_max );
nwipe_log( NWIPE_LOG_NOTICE, "hddtemp: %s temp1_min %dC", c->device_name, c->temp1_min );
}
if( nwipe_get_scsi_temperature( c ) != 0 )
{
nwipe_log( NWIPE_LOG_ERROR, "get_scsi_temperature error" );
}
}
}
}
/* Update the time stamp that records when we checked the temperature,
* this is used by the GUI to check temperatures periodically, typically
* every 60 seconds */
c->temp1_time = time( NULL );
gettimeofday( &tv_end, 0 );
delta_t = timedifference_msec( tv_start, tv_end );
if( nwipe_options.verbose )
{
nwipe_log( NWIPE_LOG_NOTICE, "get temperature for %s took %f ms", c->device_name, delta_t );
}
return;
}
void nwipe_log_drives_temperature_limits( nwipe_context_t* c )
{
/* See header for description of function
*/
char temperature_limits_txt[500];
int idx = 0;
/*
* Initialise the character string, as we are building it a few
* characters at a time and it's important there it is populated
* with all zeros as we are using strlen() as we build the line up.
*/
memset( &temperature_limits_txt, 0, sizeof( temperature_limits_txt ) );
if( c->temp1_crit != NO_TEMPERATURE_DATA )
{
snprintf( temperature_limits_txt,
sizeof( temperature_limits_txt ),
"Temperature limits for %s, critical=%ic, ",
c->device_name,
c->temp1_crit );
}
else
{
snprintf( temperature_limits_txt,
sizeof( temperature_limits_txt ),
"Temperature limits for %s, critical=N/A, ",
c->device_name );
}
idx = strlen( temperature_limits_txt );
if( c->temp1_max != NO_TEMPERATURE_DATA )
{
snprintf( &temperature_limits_txt[idx], ( sizeof( temperature_limits_txt ) - idx ), "max=%ic, ", c->temp1_max );
}
else
{
snprintf( &temperature_limits_txt[idx], ( sizeof( temperature_limits_txt ) - idx ), "max=N/A, " );
}
idx = strlen( temperature_limits_txt );
if( c->temp1_highest != NO_TEMPERATURE_DATA )
{
snprintf( &temperature_limits_txt[idx],
( sizeof( temperature_limits_txt ) - idx ),
"highest=%ic, ",
c->temp1_highest );
}
else
{
snprintf( &temperature_limits_txt[idx], ( sizeof( temperature_limits_txt ) - idx ), "highest=N/A, " );
}
idx = strlen( temperature_limits_txt );
if( c->temp1_lowest != NO_TEMPERATURE_DATA )
{
snprintf(
&temperature_limits_txt[idx], ( sizeof( temperature_limits_txt ) - idx ), "lowest=%ic, ", c->temp1_lowest );
}
else
{
snprintf( &temperature_limits_txt[idx], ( sizeof( temperature_limits_txt ) - idx ), "lowest=N/A, " );
}
idx = strlen( temperature_limits_txt );
if( c->temp1_min != NO_TEMPERATURE_DATA )
{
snprintf( &temperature_limits_txt[idx], ( sizeof( temperature_limits_txt ) - idx ), "min=%ic, ", c->temp1_min );
}
else
{
snprintf( &temperature_limits_txt[idx], ( sizeof( temperature_limits_txt ) - idx ), "min=N/A, " );
}
idx = strlen( temperature_limits_txt );
if( c->temp1_lcrit != NO_TEMPERATURE_DATA )
{
snprintf( &temperature_limits_txt[idx],
( sizeof( temperature_limits_txt ) - idx ),
"low critical=%ic.",
c->temp1_lcrit );
}
else
{
snprintf( &temperature_limits_txt[idx], ( sizeof( temperature_limits_txt ) - idx ), "low critical=N/A. " );
}
nwipe_log( NWIPE_LOG_INFO, "%s", temperature_limits_txt );
return;
}
|