7 Commits

Author SHA1 Message Date
localhorst 716ab5614f add comments 2026-05-01 15:28:02 +02:00
localhorst e017aeca0b fix pointer (again) 2026-05-01 15:22:35 +02:00
localhorst 42a1567b32 Fix error handling if shred failes (#96)
fixes #95

Reviewed-on: #96
Co-authored-by: localhorst <localhorst@mosad.xyz>
Co-committed-by: localhorst <localhorst@mosad.xyz>
2026-05-01 15:18:31 +02:00
localhorst 95f5037529 Merge branch 'master' into feature/adaptive-chunksize 2026-05-01 15:11:42 +02:00
localhorst 55481b86fd Show HDD warnings based on sectors (#97)
If one of the following metrics is >0 an warning is shown

* Reallocated_Sector_Count
* Current_Pending_Sector
* Offline_Uncorrectable

Reviewed-on: #97
Co-authored-by: localhorst <localhorst@mosad.xyz>
Co-committed-by: localhorst <localhorst@mosad.xyz>
2026-05-01 15:03:15 +02:00
localhorst 5478a871f1 Fix error handling if shred failes (#96)
fixes #95

Reviewed-on: #96
Co-authored-by: localhorst <localhorst@mosad.xyz>
Co-committed-by: localhorst <localhorst@mosad.xyz>
2026-05-01 13:12:39 +02:00
localhorst 50e88c8e84 Best throughput tracker with chunksize 2026-04-28 21:54:49 +02:00
10 changed files with 944 additions and 354 deletions
+11 -2
View File
@@ -72,7 +72,10 @@ private:
uint32_t u32ErrorCount = 0U; uint32_t u32ErrorCount = 0U;
uint32_t u32PowerOnHours = 0U; // in hours uint32_t u32PowerOnHours = 0U; // in hours
uint32_t u32PowerCycles = 0U; uint32_t u32PowerCycles = 0U;
uint32_t u32Temperature = 0U; // in Fahrenheit, just kidding: degree Celsius uint32_t u32Temperature = 0U; // in Fahrenheit, just kidding: degree Celsius
uint32_t u32ReallocatedSectors = 0U; // ID 0x05 - Reallocated Sectors Count
uint32_t u32PendingSectors = 0U; // ID 0xC5 - Current Pending Sector Count
uint32_t u32UncorrectableSectors = 0U; // ID 0xC6 - Offline Uncorrectable Sector Count
} sSmartData; } sSmartData;
private: private:
@@ -106,6 +109,9 @@ public:
uint32_t getPowerOnHours(void); // in hours uint32_t getPowerOnHours(void); // in hours
uint32_t getPowerCycles(void); uint32_t getPowerCycles(void);
uint32_t getTemperature(void); // in Fahrenheit, just kidding: degree Celsius uint32_t getTemperature(void); // in Fahrenheit, just kidding: degree Celsius
uint32_t getReallocatedSectors(void);
uint32_t getPendingSectors(void);
uint32_t getUncorrectableSectors(void);
void checkFrozenDrive(void); void checkFrozenDrive(void);
void setDriveSMARTData(std::string modelFamily, void setDriveSMARTData(std::string modelFamily,
@@ -115,7 +121,10 @@ public:
uint32_t errorCount, uint32_t errorCount,
uint32_t powerOnHours, uint32_t powerOnHours,
uint32_t powerCycles, uint32_t powerCycles,
uint32_t temperature); uint32_t temperature,
uint32_t reallocatedSectors,
uint32_t pendingSectors,
uint32_t uncorrectableSectors);
std::string sCapacityToText(); std::string sCapacityToText();
std::string sErrorCountToText(); std::string sErrorCountToText();
+2 -2
View File
@@ -8,7 +8,7 @@
#ifndef REHDD_H_ #ifndef REHDD_H_
#define REHDD_H_ #define REHDD_H_
#define REHDD_VERSION "V1.3.0" #define REHDD_VERSION "V1.4.0-dev"
// Drive handling Settings // Drive handling Settings
#define WORSE_HOURS 19200 // mark drive if at this limit or beyond #define WORSE_HOURS 19200 // mark drive if at this limit or beyond
@@ -20,7 +20,7 @@
// Logger Settings // Logger Settings
#define LOG_PATH "./reHDD.log" #define LOG_PATH "./reHDD.log"
#define DESCRIPTION "reHDD - Copyright Hendrik Schutter 2025" #define DESCRIPTION "reHDD - Copyright Hendrik Schutter 2026"
#define DEVICE_ID "generic" #define DEVICE_ID "generic"
#define SOFTWARE_VERSION REHDD_VERSION #define SOFTWARE_VERSION REHDD_VERSION
#define HARDWARE_VERSION "generic" #define HARDWARE_VERSION "generic"
+48 -4
View File
@@ -16,9 +16,28 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <chrono>
#define CHUNK_SIZE 1024 * 1024 * 32 // amount of bytes that are overwritten at once --> 32MB // Adaptive chunk size optimization - uncomment to enable
#define TFNG_DATA_SIZE CHUNK_SIZE // amount of bytes used by tfng #define ADAPTIVE_CHUNK_SIZE
// Chunk size configuration
#define CHUNK_SIZE_START 1024 * 1024 * 32 // Starting chunk size: 32MB
#define CHUNK_SIZE_MIN 1024 * 1024 * 4 // Minimum chunk size: 4MB
#define CHUNK_SIZE_MAX 1024 * 1024 * 128 // Maximum chunk size: 128MB
#define CHUNK_SIZE_STEP_UP 1024 * 1024 * 2 // Increase step: 2MB
#define CHUNK_SIZE_STEP_DOWN 1024 * 1024 * 4 // Decrease step: 4MB
#define CHUNK_MEASURE_INTERVAL 64 // Measure performance every 64 chunks
#ifdef ADAPTIVE_CHUNK_SIZE
// Use max buffer size when adaptive mode is enabled
#define CHUNK_SIZE CHUNK_SIZE_MAX
#define TFNG_DATA_SIZE CHUNK_SIZE_MAX
#else
// Use fixed chunk size when adaptive mode is disabled
#define CHUNK_SIZE CHUNK_SIZE_START
#define TFNG_DATA_SIZE CHUNK_SIZE
#endif
// #define DEMO_DRIVE_SIZE 1024*1024*256L // 256MB // #define DEMO_DRIVE_SIZE 1024*1024*256L // 256MB
// #define DEMO_DRIVE_SIZE 1024*1024*1024L // 1GB // #define DEMO_DRIVE_SIZE 1024*1024*1024L // 1GB
@@ -33,22 +52,47 @@ protected:
public: public:
Shred(); Shred();
~Shred(); ~Shred();
int shredDrive(Drive *drive, int *ipSignalFd); int shredDrive(Drive* drive, int* ipSignalFd);
private: private:
fileDescriptor randomSrcFileDiscr; fileDescriptor randomSrcFileDiscr;
fileDescriptor driveFileDiscr; fileDescriptor driveFileDiscr;
#ifdef ADAPTIVE_CHUNK_SIZE
unsigned char* caTfngData; // Dynamic buffer allocation for adaptive mode
unsigned char* caReadBuffer; // Dynamic buffer allocation for adaptive mode
#else
unsigned char caTfngData[TFNG_DATA_SIZE]; unsigned char caTfngData[TFNG_DATA_SIZE];
unsigned char caReadBuffer[CHUNK_SIZE]; unsigned char caReadBuffer[CHUNK_SIZE];
#endif
unsigned long ulDriveByteSize; unsigned long ulDriveByteSize;
unsigned long ulDriveByteOverallCount = 0; // all bytes shredded in all iterations + checking -> used for progress calculation unsigned long ulDriveByteOverallCount = 0; // all bytes shredded in all iterations + checking -> used for progress calculation
double d32Percent = 0.0; double d32Percent = 0.0;
double d32TmpPercent = 0.0; double d32TmpPercent = 0.0;
#ifdef ADAPTIVE_CHUNK_SIZE
// Adaptive chunk size optimization members
size_t currentChunkSize;
size_t bestChunkSize;
unsigned int chunkCounter;
std::chrono::high_resolution_clock::time_point measurementStartTime;
double bestThroughputMBps;
double lastThroughputMBps;
unsigned long bytesWrittenInMeasurement;
bool throughputIncreasing;
// Adaptive methods
void startMeasurement();
void evaluateThroughput(Drive* drive);
void adjustChunkSize(Drive* drive);
size_t getCurrentChunkSize() const;
#endif
inline double calcProgress(); inline double calcProgress();
int iRewindDrive(fileDescriptor file); int iRewindDrive(fileDescriptor file);
long getDriveSizeInBytes(fileDescriptor file); long getDriveSizeInBytes(fileDescriptor file);
unsigned int uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSignalFd); unsigned int uiCalcChecksum(fileDescriptor file, Drive* drive, int* ipSignalFd);
void cleanup(); void cleanup();
}; };
+16 -11
View File
@@ -10,24 +10,29 @@
#include "reHDD.h" #include "reHDD.h"
/**
* @brief SMART data reader for drives
*
* Parses smartctl JSON output to extract:
* - Device information (model, serial, capacity)
* - Power statistics (hours, cycles)
* - Temperature
* - Critical sector counts (reallocated, pending, uncorrectable)
*
* Uses deterministic state machine parser for reliable multi-line JSON parsing.
*/
class SMART class SMART
{ {
protected: protected:
public: public:
/**
* @brief Read S.M.A.R.T. data from drive and populate Drive object
* @param drive Pointer to Drive instance to populate with SMART data
*/
static void readSMARTData(Drive *drive); static void readSMARTData(Drive *drive);
private: private:
SMART(void); SMART(void); // Utility class - no instances
static bool parseExitStatus(std::string sLine, uint8_t &status);
static bool parseModelFamily(std::string sLine, std::string &modelFamily);
static bool parseModelName(std::string sLine, std::string &modelName);
static bool parseSerial(std::string sLine, std::string &serial);
static bool parseCapacity(std::string sLine, uint64_t &capacity);
static bool parseErrorCount(std::string sLine, uint32_t &errorCount);
static bool parsePowerOnHours(std::string sLine, uint32_t &powerOnHours);
static bool parsePowerCycles(std::string sLine, uint32_t &powerCycles);
static bool parseTemperature(std::string sLine, uint32_t &temperature);
}; };
#endif // SMART_H_ #endif // SMART_H_
+1 -1
View File
@@ -76,7 +76,7 @@ private:
static WINDOW *createMenuView(int iXSize, int iYSize, int iXStart, int iYStart, struct MenuState menustate); static WINDOW *createMenuView(int iXSize, int iYSize, int iXStart, int iYStart, struct MenuState menustate);
static WINDOW *createDialog(int iXSize, int iYSize, int iXStart, int iYStart, std::string selectedTask, std::string optionA, std::string optionB); static WINDOW *createDialog(int iXSize, int iYSize, int iXStart, int iYStart, std::string selectedTask, std::string optionA, std::string optionB);
static WINDOW *createFrozenWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, std::string sModelFamily, std::string sModelName, std::string sSerial, std::string sProgress); static WINDOW *createFrozenWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, std::string sModelFamily, std::string sModelName, std::string sSerial, std::string sProgress);
static WINDOW *createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, uint32_t u32PowerOnHours, uint32_t u32PowerCycles, uint32_t u32ErrorCount, uint32_t u32Temperature); static WINDOW *createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, uint32_t u32PowerOnHours, uint32_t u32PowerCycles, uint32_t u32ErrorCount, uint32_t u32Temperature, uint32_t u32ReallocatedSectors, uint32_t u32PendingSectors, uint32_t u32UncorrectableSectors);
static WINDOW *createZeroChecksumWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, std::string sModelFamily, std::string sModelName, std::string sSerial, uint32_t u32Checksum); static WINDOW *createZeroChecksumWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, std::string sModelFamily, std::string sModelName, std::string sSerial, uint32_t u32Checksum);
void displaySelectedDrive(Drive &drive, int stdscrX, int stdscrY); void displaySelectedDrive(Drive &drive, int stdscrX, int stdscrY);
+22 -1
View File
@@ -140,6 +140,21 @@ uint32_t Drive::getTemperature(void)
return sSmartData.u32Temperature; return sSmartData.u32Temperature;
} }
uint32_t Drive::getReallocatedSectors(void)
{
return sSmartData.u32ReallocatedSectors;
}
uint32_t Drive::getPendingSectors(void)
{
return sSmartData.u32PendingSectors;
}
uint32_t Drive::getUncorrectableSectors(void)
{
return sSmartData.u32UncorrectableSectors;
}
string Drive::sCapacityToText() string Drive::sCapacityToText()
{ {
char acBuffer[16]; char acBuffer[16];
@@ -226,7 +241,10 @@ void Drive::setDriveSMARTData(string modelFamily,
uint32_t errorCount, uint32_t errorCount,
uint32_t powerOnHours, uint32_t powerOnHours,
uint32_t powerCycle, uint32_t powerCycle,
uint32_t temperature) uint32_t temperature,
uint32_t reallocatedSectors,
uint32_t pendingSectors,
uint32_t uncorrectableSectors)
{ {
this->sSmartData.sModelFamily = modelFamily; this->sSmartData.sModelFamily = modelFamily;
this->sSmartData.sModelName = modelName; this->sSmartData.sModelName = modelName;
@@ -236,6 +254,9 @@ void Drive::setDriveSMARTData(string modelFamily,
this->sSmartData.u32PowerOnHours = powerOnHours; this->sSmartData.u32PowerOnHours = powerOnHours;
this->sSmartData.u32PowerCycles = powerCycle; this->sSmartData.u32PowerCycles = powerCycle;
this->sSmartData.u32Temperature = temperature; this->sSmartData.u32Temperature = temperature;
this->sSmartData.u32ReallocatedSectors = reallocatedSectors;
this->sSmartData.u32PendingSectors = pendingSectors;
this->sSmartData.u32UncorrectableSectors = uncorrectableSectors;
} }
void Drive::setTimestamp() void Drive::setTimestamp()
+1 -1
View File
@@ -332,7 +332,7 @@ void reHDD::filterNewDrives(list<Drive> *plistOldDrives, list<Drive> *plistNewDr
{ {
itOld->bIsOffline = false; // drive is still attached itOld->bIsOffline = false; // drive is still attached
// copy new smart data to existing drive // copy new smart data to existing drive
itOld->setDriveSMARTData(itNew->getModelFamily(), itNew->getModelName(), itNew->getSerial(), itNew->getCapacity(), itNew->getErrorCount(), itNew->getPowerOnHours(), itNew->getPowerCycles(), itNew->getTemperature()); itOld->setDriveSMARTData(itNew->getModelFamily(), itNew->getModelName(), itNew->getSerial(), itNew->getCapacity(), itNew->getErrorCount(), itNew->getPowerOnHours(), itNew->getPowerCycles(), itNew->getTemperature(), itNew->getReallocatedSectors(), itNew->getPendingSectors(), itNew->getUncorrectableSectors());
#ifdef LOG_LEVEL_HIGH #ifdef LOG_LEVEL_HIGH
Logger::logThis()->info("Delete new drive, because already attached: " + itNew->getModelName()); Logger::logThis()->info("Delete new drive, because already attached: " + itNew->getModelName());
#endif #endif
+472 -59
View File
@@ -21,24 +21,185 @@ const static char *randomsrc = (char *)"/dev/urandom";
Shred::Shred() Shred::Shred()
{ {
#ifdef ADAPTIVE_CHUNK_SIZE
// Allocate aligned buffers for maximum chunk size
if (posix_memalign((void **)&caTfngData, 4096, CHUNK_SIZE_MAX) != 0)
{
Logger::logThis()->error("Failed to allocate aligned buffer for tfng data");
caTfngData = nullptr;
}
if (posix_memalign((void **)&caReadBuffer, 4096, CHUNK_SIZE_MAX) != 0)
{
Logger::logThis()->error("Failed to allocate aligned buffer for read buffer");
caReadBuffer = nullptr;
}
// Initialize adaptive tracking variables
currentChunkSize = CHUNK_SIZE_START;
bestChunkSize = CHUNK_SIZE_START;
chunkCounter = 0;
bestThroughputMBps = 0.0;
lastThroughputMBps = 0.0;
bytesWrittenInMeasurement = 0;
throughputIncreasing = true;
Logger::logThis()->info("Adaptive chunk size optimization ENABLED - Starting with " +
to_string(currentChunkSize / (1024 * 1024)) + " MB chunks");
#endif
} }
Shred::~Shred() Shred::~Shred()
{ {
#ifdef ADAPTIVE_CHUNK_SIZE
if (caTfngData != nullptr)
{
free(caTfngData);
caTfngData = nullptr;
}
if (caReadBuffer != nullptr)
{
free(caReadBuffer);
caReadBuffer = nullptr;
}
#endif
}
#ifdef ADAPTIVE_CHUNK_SIZE
/**
* \brief Start performance measurement interval
* \return void
*/
void Shred::startMeasurement()
{
measurementStartTime = std::chrono::high_resolution_clock::now();
bytesWrittenInMeasurement = 0;
chunkCounter = 0;
} }
/** /**
* \brief shred drive with shred * \brief shred drive with shred
* \param pointer of Drive instance * \param pointer of Drive instance
* \param file descriptor for signaling
* \return 0 on success, -1 on error
*/
void Shred::evaluateThroughput(Drive *drive)
{
auto measurementEndTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = measurementEndTime - measurementStartTime;
double elapsedSeconds = elapsed.count();
if (elapsedSeconds > 0.0)
{
double throughputMBps = (bytesWrittenInMeasurement / (1024.0 * 1024.0)) / elapsedSeconds;
lastThroughputMBps = throughputMBps;
Logger::logThis()->info("Throughput measurement - ChunkSize: " +
to_string(currentChunkSize / (1024 * 1024)) + " MB, " +
"Throughput: " + to_string((int)throughputMBps) + " MB/s, " +
"Best: " + to_string((int)bestThroughputMBps) + " MB/s" +
" - Drive: " + drive->getSerial());
// Check if this is better than our best
if (throughputMBps > bestThroughputMBps)
{
bestThroughputMBps = throughputMBps;
bestChunkSize = currentChunkSize;
throughputIncreasing = true;
Logger::logThis()->info("NEW BEST throughput: " + to_string((int)bestThroughputMBps) +
" MB/s with " + to_string(currentChunkSize / (1024 * 1024)) +
" MB chunks - Drive: " + drive->getSerial());
}
else
{
throughputIncreasing = false;
}
}
// Adjust chunk size for next measurement interval
adjustChunkSize(drive);
// Start new measurement
startMeasurement();
}
/**
* \brief Adjust chunk size based on throughput trend
* \param pointer to Drive instance
* \return void * \return void
*/ */
void Shred::adjustChunkSize(Drive *drive)
{
size_t oldChunkSize = currentChunkSize;
if (throughputIncreasing)
{
// Throughput is improving - increase chunk size
currentChunkSize += CHUNK_SIZE_STEP_UP;
// Clamp to maximum
if (currentChunkSize > CHUNK_SIZE_MAX)
{
currentChunkSize = CHUNK_SIZE_MAX;
Logger::logThis()->info("Reached maximum chunk size: " +
to_string(currentChunkSize / (1024 * 1024)) + " MB" +
" - Drive: " + drive->getSerial());
}
}
else
{
// Throughput decreased - decrease chunk size to find sweet spot
if (currentChunkSize > CHUNK_SIZE_STEP_DOWN)
{
currentChunkSize -= CHUNK_SIZE_STEP_DOWN;
}
// Clamp to minimum
if (currentChunkSize < CHUNK_SIZE_MIN)
{
currentChunkSize = CHUNK_SIZE_MIN;
Logger::logThis()->info("Reached minimum chunk size: " +
to_string(currentChunkSize / (1024 * 1024)) + " MB" +
" - Drive: " + drive->getSerial());
}
}
if (oldChunkSize != currentChunkSize)
{
Logger::logThis()->info("Adjusted chunk size: " +
to_string(oldChunkSize / (1024 * 1024)) + " MB -> " +
to_string(currentChunkSize / (1024 * 1024)) + " MB" +
" - Drive: " + drive->getSerial());
}
}
/**
* \brief Get current chunk size for adaptive mode
* \return current chunk size in bytes
*/
size_t Shred::getCurrentChunkSize() const
{
return currentChunkSize;
}
#endif
/**
* \brief shred drive with shred
* \param pointer of Drive instance
* \param file descriptor for signaling
* \return 0 on success, -1 on error
*/
int Shred::shredDrive(Drive *drive, int *ipSignalFd) int Shred::shredDrive(Drive *drive, int *ipSignalFd)
{ {
ostringstream address; ostringstream address;
address << (void const *)&(*drive); address << (void const *)&(*drive);
Logger::logThis()->info("Shred-Task started - Drive: " + drive->getModelName() + "-" + drive->getSerial() + " @" + address.str()); Logger::logThis()->info("Shred-Task started - Drive: " + drive->getModelName() + "-" + drive->getSerial() + " @" + address.str());
drive->bWasShredStarted = true; // Mark drive as partly shredded
// Mark as started but NOT shredded yet
drive->bWasShredStarted = true;
drive->bWasShredded = false; drive->bWasShredded = false;
drive->bWasChecked = false;
drive->setTaskPercentage(0.0); drive->setTaskPercentage(0.0);
drive->u32DriveChecksumAfterShredding = UINT32_MAX; drive->u32DriveChecksumAfterShredding = UINT32_MAX;
drive->state = Drive::TaskState::SHRED_ACTIVE; drive->state = Drive::TaskState::SHRED_ACTIVE;
@@ -46,99 +207,222 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
#ifdef DRYRUN #ifdef DRYRUN
for (int i = 0; i <= 100; i++) for (int i = 0; i <= 100; i++)
{ {
drive->setTaskPercentage(i + 0.05); if (drive->state.load() != Drive::TaskState::SHRED_ACTIVE)
{
Logger::logThis()->info("Shred-Task aborted during DRYRUN - Drive: " + drive->getSerial());
drive->setTaskPercentage(i + 0.05);
drive->state = Drive::TaskState::NONE;
drive->bWasShredded = false; // CRITICAL: Mark as NOT shredded on abort
return -1;
}
drive->setTaskPercentage((double)i);
write(*ipSignalFd, "A", 1); write(*ipSignalFd, "A", 1);
usleep(20000); usleep(20000);
} }
// Only mark as shredded if DRYRUN completed successfully
drive->bWasShredded = true; drive->bWasShredded = true;
drive->setTaskPercentage(0.0);
drive->state = Drive::TaskState::NONE;
Logger::logThis()->info("DRYRUN completed - Drive: " + drive->getSerial());
return 0;
#endif #endif
#ifndef DRYRUN #ifndef DRYRUN
const char *cpDrivePath = drive->getPath().c_str(); string sDrivePath = drive->getPath();
const char *cpDrivePath = sDrivePath.c_str();
unsigned char ucKey[TFNG_KEY_SIZE]; unsigned char ucKey[TFNG_KEY_SIZE];
// open random source #ifdef ADAPTIVE_CHUNK_SIZE
// Validate buffers were allocated
if (caTfngData == nullptr || caReadBuffer == nullptr)
{
Logger::logThis()->error("Shred-Task: Aligned buffers not allocated! - Drive: " + drive->getSerial());
return -1;
}
#endif
// Open random source
Logger::logThis()->info("Shred-Task: Opening random source: " + string(randomsrc) + " - Drive: " + drive->getSerial());
randomSrcFileDiscr = open(randomsrc, O_RDONLY | O_LARGEFILE); randomSrcFileDiscr = open(randomsrc, O_RDONLY | O_LARGEFILE);
if (randomSrcFileDiscr == -1) if (randomSrcFileDiscr == -1)
{ {
std::string errorMsg(strerror(errno)); int savedErrno = errno;
Logger::logThis()->error("Shred-Task: Open random source failed! " + errorMsg + " - Drive: " + drive->getSerial()); Logger::logThis()->error("Shred-Task: Open random source failed! Path: " + string(randomsrc) +
perror(randomsrc); " - Error: " + strerror(savedErrno) + " (errno: " + to_string(savedErrno) + ")" +
cleanup(); " - Drive: " + drive->getSerial());
// Reset drive state on error - NOT shredded
drive->state = Drive::TaskState::NONE;
drive->setTaskPercentage(0.0);
drive->bWasShredStarted = false;
drive->bWasShredded = false;
return -1; return -1;
} }
Logger::logThis()->info("Shred-Task: Random source opened successfully (fd: " + to_string(randomSrcFileDiscr) + ") - Drive: " + drive->getSerial());
// open disk // Open disk
driveFileDiscr = open(cpDrivePath, O_RDWR | O_LARGEFILE); driveFileDiscr = open(cpDrivePath, O_RDWR | O_LARGEFILE);
if (driveFileDiscr == -1) if (driveFileDiscr == -1)
{ {
std::string errorMsg(strerror(errno)); int savedErrno = errno;
Logger::logThis()->error("Shred-Task: Open drive failed! " + errorMsg + " - Drive: " + drive->getSerial()); string errorDetail;
perror(cpDrivePath);
cleanup(); switch (savedErrno)
{
case ENOMEDIUM:
errorDetail = "No medium found (drive may be empty or disconnected)";
break;
case EACCES:
errorDetail = "Permission denied (need root/sudo?)";
break;
case ENOENT:
errorDetail = "Drive not found (device may have been removed)";
break;
case EROFS:
errorDetail = "Read-only file system";
break;
case EBUSY:
errorDetail = "Drive is busy (may be mounted or in use)";
break;
case EINVAL:
errorDetail = "Invalid argument";
break;
default:
errorDetail = strerror(savedErrno);
break;
}
Logger::logThis()->error("Shred-Task: Open drive failed! Path: " + string(cpDrivePath) +
" - Error: " + errorDetail + " (errno: " + to_string(savedErrno) + ")" +
" - Drive: " + drive->getSerial() + " - Model: " + drive->getModelName());
// Close random source before returning
close(randomSrcFileDiscr);
randomSrcFileDiscr = -1;
// Reset drive state on error - NOT shredded
drive->state = Drive::TaskState::NONE;
drive->setTaskPercentage(0.0);
drive->bWasShredStarted = false;
drive->bWasShredded = false;
return -1; return -1;
} }
Logger::logThis()->info("Shred-Task: Drive opened successfully (fd: " + to_string(driveFileDiscr) + ") - Drive: " + drive->getSerial());
// read key for random generator // Read key for random generator
Logger::logThis()->info("Shred-Task: Reading random key - Drive: " + drive->getSerial());
ssize_t readRet = read(randomSrcFileDiscr, ucKey, sizeof(ucKey)); ssize_t readRet = read(randomSrcFileDiscr, ucKey, sizeof(ucKey));
if (readRet <= 0) if (readRet <= 0)
{ {
std::string errorMsg(strerror(errno)); int savedErrno = errno;
Logger::logThis()->error("Shred-Task: Read random key failed! " + errorMsg + " - Drive: " + drive->getSerial()); Logger::logThis()->error("Shred-Task: Read random key failed! Expected: " + to_string(sizeof(ucKey)) +
perror(randomsrc); " bytes, Got: " + to_string(readRet) + " bytes" +
" - Error: " + strerror(savedErrno) + " (errno: " + to_string(savedErrno) + ")" +
" - Drive: " + drive->getSerial());
cleanup(); cleanup();
// Reset drive state on error - NOT shredded
drive->state = Drive::TaskState::NONE;
drive->setTaskPercentage(0.0);
drive->bWasShredStarted = false;
drive->bWasShredded = false;
return -1; return -1;
} }
Logger::logThis()->info("Shred-Task: Random key read successfully (" + to_string(readRet) + " bytes) - Drive: " + drive->getSerial());
tfng_prng_seedkey(ucKey); tfng_prng_seedkey(ucKey);
this->ulDriveByteSize = getDriveSizeInBytes(driveFileDiscr); this->ulDriveByteSize = getDriveSizeInBytes(driveFileDiscr);
if (this->ulDriveByteSize == 0)
{
Logger::logThis()->error("Shred-Task: Drive size is 0 bytes! Drive may be empty or size detection failed - Drive: " + drive->getSerial());
cleanup();
// Reset drive state on error - NOT shredded
drive->state = Drive::TaskState::NONE;
drive->setTaskPercentage(0.0);
drive->bWasShredStarted = false;
drive->bWasShredded = false;
return -1;
}
Drive::ShredSpeed shredSpeed = drive->sShredSpeed.load(); Drive::ShredSpeed shredSpeed = drive->sShredSpeed.load();
shredSpeed.chronoShredTimestamp = std::chrono::system_clock::now(); // set inital timestamp for speed metric shredSpeed.chronoShredTimestamp = std::chrono::system_clock::now();
shredSpeed.ulSpeedMetricBytesWritten = 0U; // uses to calculate speed metric shredSpeed.ulSpeedMetricBytesWritten = 0U;
drive->sShredSpeed.store(shredSpeed); drive->sShredSpeed.store(shredSpeed);
#ifdef LOG_LEVEL_HIGH #ifdef LOG_LEVEL_HIGH
Logger::logThis()->info("Shred-Task: Bytes-Size of Drive: " + to_string(this->ulDriveByteSize) + " - Drive: " + drive->getSerial()); Logger::logThis()->info("Shred-Task: Bytes-Size of Drive: " + to_string(this->ulDriveByteSize) + " - Drive: " + drive->getSerial());
#endif #endif
#ifdef ADAPTIVE_CHUNK_SIZE
// Start first measurement interval
startMeasurement();
#endif
// Main shredding loop
for (unsigned int uiShredIterationCounter = 0U; uiShredIterationCounter < SHRED_ITERATIONS; uiShredIterationCounter++) for (unsigned int uiShredIterationCounter = 0U; uiShredIterationCounter < SHRED_ITERATIONS; uiShredIterationCounter++)
{ {
unsigned long ulDriveByteCounter = 0U; // used for one shred-iteration to keep track of the current drive position // Logger::logThis()->info("Shred-Task: Starting iteration " + to_string(uiShredIterationCounter + 1) + "/" + to_string(SHRED_ITERATIONS) + " - Drive: " + drive->getSerial());
unsigned long ulDriveByteCounter = 0U;
if (uiShredIterationCounter == (SHRED_ITERATIONS - 1)) if (uiShredIterationCounter == (SHRED_ITERATIONS - 1))
{ {
// last shred iteration --> overwrite (just the write chunk) bytes with zeros instead with random data // last shred iteration --> overwrite (just the write chunk) bytes with zeros instead with random data
#ifdef ADAPTIVE_CHUNK_SIZE
memset(caTfngData, 0U, CHUNK_SIZE_MAX);
#else
memset(caTfngData, 0U, CHUNK_SIZE); memset(caTfngData, 0U, CHUNK_SIZE);
#endif
} }
while (ulDriveByteCounter < ulDriveByteSize) while (ulDriveByteCounter < ulDriveByteSize)
{ {
int iBytesToShred = 0; // Bytes that will be overwritten in this chunk-iteration #ifdef ADAPTIVE_CHUNK_SIZE
size_t activeChunkSize = getCurrentChunkSize();
#else
size_t activeChunkSize = CHUNK_SIZE;
#endif
int iBytesToShred = 0;
if (uiShredIterationCounter != (SHRED_ITERATIONS - 1)) if (uiShredIterationCounter != (SHRED_ITERATIONS - 1))
{ {
// NOT last shred iteration --> generate new random data #ifdef ADAPTIVE_CHUNK_SIZE
tfng_prng_genrandom(caTfngData, activeChunkSize);
#else
tfng_prng_genrandom(caTfngData, TFNG_DATA_SIZE); tfng_prng_genrandom(caTfngData, TFNG_DATA_SIZE);
#endif
} }
if ((ulDriveByteSize - ulDriveByteCounter) < CHUNK_SIZE) if ((ulDriveByteSize - ulDriveByteCounter) < activeChunkSize)
{ {
iBytesToShred = (ulDriveByteSize - ulDriveByteCounter); iBytesToShred = (ulDriveByteSize - ulDriveByteCounter);
} }
else else
{ {
iBytesToShred = CHUNK_SIZE; iBytesToShred = activeChunkSize;
} }
int iByteShredded = write(driveFileDiscr, caTfngData, iBytesToShred); int iByteShredded = write(driveFileDiscr, caTfngData, iBytesToShred);
if (iByteShredded <= 0) if (iByteShredded <= 0)
{ {
std::string errorMsg(strerror(errno)); int savedErrno = errno;
Logger::logThis()->error("Shred-Task: Write to drive failed! " + errorMsg + " - Drive: " + drive->getSerial()); Logger::logThis()->error("Shred-Task: Write to drive failed! Attempted: " + to_string(iBytesToShred) +
perror("unable to write random data"); " bytes, Written: " + to_string(iByteShredded) + " bytes" +
" - Position: " + to_string(ulDriveByteCounter) + "/" + to_string(ulDriveByteSize) +
" - Iteration: " + to_string(uiShredIterationCounter + 1) + "/" + to_string(SHRED_ITERATIONS) +
" - Error: " + strerror(savedErrno) + " (errno: " + to_string(savedErrno) + ")" +
" - Drive: " + drive->getSerial());
cleanup(); cleanup();
// CRITICAL: Mark as NOT shredded on write failure
drive->state = Drive::TaskState::NONE;
drive->setTaskPercentage(0.0);
drive->bWasShredded = false;
drive->bWasChecked = false;
return -1; return -1;
} }
@@ -148,17 +432,33 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
ulDriveByteCounter += iByteShredded; ulDriveByteCounter += iByteShredded;
ulDriveByteOverallCount += iByteShredded; ulDriveByteOverallCount += iByteShredded;
#ifdef ADAPTIVE_CHUNK_SIZE
bytesWrittenInMeasurement += iByteShredded;
chunkCounter++;
// Evaluate throughput after measurement interval
if (chunkCounter >= CHUNK_MEASURE_INTERVAL)
{
evaluateThroughput(drive);
}
#endif
d32Percent = this->calcProgress(); d32Percent = this->calcProgress();
#ifdef LOG_LEVEL_HIGH #ifdef LOG_LEVEL_HIGH
Logger::logThis()->info("Shred-Task: ByteCount: " + to_string(ulDriveByteCounter) + " - iteration: " + to_string((uiShredIterationCounter + 1)) + " - progress: " + to_string(d32Percent) + " - Drive: " + drive->getSerial()); Logger::logThis()->info("Shred-Task: ByteCount: " + to_string(ulDriveByteCounter) +
" - iteration: " + to_string((uiShredIterationCounter + 1)) +
" - progress: " + to_string(d32Percent) + "%" +
" - Drive: " + drive->getSerial());
#endif #endif
if ((d32Percent - d32TmpPercent) >= 0.01) if ((d32Percent - d32TmpPercent) >= 0.01)
{ {
// set shred percantage // set shred percentage
drive->setTaskPercentage(d32TmpPercent); drive->setTaskPercentage(d32TmpPercent);
d32TmpPercent = d32Percent; d32TmpPercent = d32Percent;
// signal process in shreding // signal process in shredding
write(*ipSignalFd, "A", 1); write(*ipSignalFd, "A", 1);
} }
@@ -168,26 +468,45 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
d32Percent = 0.00; d32Percent = 0.00;
d32TmpPercent = 0.00; d32TmpPercent = 0.00;
ulDriveByteCounter = 0U; ulDriveByteCounter = 0U;
Logger::logThis()->info("Aborted shred for: " + drive->getModelName() + "-" + drive->getSerial()); Logger::logThis()->info("Aborted shred for: " + drive->getModelName() + "-" + drive->getSerial());
cleanup(); cleanup();
return -1; return -1;
} }
// end one chunk write
} }
Logger::logThis()->info("Shred-Task: Iteration " + to_string(uiShredIterationCounter + 1) + "/" +
to_string(SHRED_ITERATIONS) + " completed - Drive: " + drive->getSerial());
// Rewind drive for next iteration
if (0 != iRewindDrive(driveFileDiscr)) if (0 != iRewindDrive(driveFileDiscr))
{ {
Logger::logThis()->error("Shred-Task: Unable to rewind drive! - Drive: " + drive->getSerial()); Logger::logThis()->error("Shred-Task: Unable to rewind drive after iteration " +
to_string(uiShredIterationCounter + 1) + " - Drive: " + drive->getSerial());
cleanup(); cleanup();
// CRITICAL: Mark as NOT shredded on rewind failure
drive->state = Drive::TaskState::NONE;
drive->setTaskPercentage(0.0);
drive->bWasShredded = false;
drive->bWasChecked = false;
return -1; return -1;
} }
// end one shred iteration
} }
// end of all shred iteratio
tfng_prng_seedkey(NULL); // reset random generator #ifdef ADAPTIVE_CHUNK_SIZE
Logger::logThis()->info("Shred completed - Optimal chunk size: " +
to_string(bestChunkSize / (1024 * 1024)) + " MB, " +
"Best throughput: " + to_string((int)bestThroughputMBps) + " MB/s" +
" - Drive: " + drive->getSerial());
#endif
// All shred iterations completed successfully
tfng_prng_seedkey(NULL);
// ONLY mark as shredded if ALL iterations completed AND fsync succeeded
drive->bWasShredded = true; drive->bWasShredded = true;
Logger::logThis()->info("Shred-Task finished - Drive: " + drive->getModelName() + "-" + drive->getSerial() + " @" + address.str()); Logger::logThis()->info("Shred-Task finished - Drive: " + drive->getModelName() + "-" + drive->getSerial() + " @" + address.str());
#ifdef ZERO_CHECK #ifdef ZERO_CHECK
drive->state = Drive::TaskState::CHECK_ACTIVE; drive->state = Drive::TaskState::CHECK_ACTIVE;
Logger::logThis()->info("Check-Task started - Drive: " + drive->getModelName() + "-" + drive->getSerial() + " @" + address.str()); Logger::logThis()->info("Check-Task started - Drive: " + drive->getModelName() + "-" + drive->getSerial() + " @" + address.str());
@@ -196,33 +515,49 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
if (drive->u32DriveChecksumAfterShredding != 0) if (drive->u32DriveChecksumAfterShredding != 0)
{ {
drive->state = Drive::TaskState::CHECK_FAILED; drive->state = Drive::TaskState::CHECK_FAILED;
Logger::logThis()->info("Shred-Task: Checksum not zero: " + to_string(drive->u32DriveChecksumAfterShredding) + " - Drive: " + drive->getSerial()); Logger::logThis()->error("Check-Task: Checksum verification failed! Expected: 0, Got: " +
to_string(drive->u32DriveChecksumAfterShredding) + " - Drive: " + drive->getSerial());
} }
else else
{ {
drive->state = Drive::TaskState::CHECK_SUCCESSFUL; drive->state = Drive::TaskState::CHECK_SUCCESSFUL;
Logger::logThis()->info("Shred-Task: Checksum zero: " + to_string(drive->u32DriveChecksumAfterShredding) + " - Drive: " + drive->getSerial()); drive->bWasChecked = true;
Logger::logThis()->info("Check-Task: Checksum verification passed (zero) - Drive: " + drive->getSerial());
} }
#endif #endif
cleanup(); cleanup();
#endif #endif
if ((drive->state.load() == Drive::TaskState::SHRED_ACTIVE) || (drive->state.load() == Drive::TaskState::CHECK_SUCCESSFUL) || (drive->state == Drive::TaskState::CHECK_FAILED)) // Final state handling - ONLY process if shred actually completed
Drive::TaskState finalState = drive->state.load();
// Only do final processing if we reached a completion state
// (not if we returned early with errors)
if ((finalState == Drive::TaskState::SHRED_ACTIVE) ||
(finalState == Drive::TaskState::CHECK_SUCCESSFUL) ||
(finalState == Drive::TaskState::CHECK_FAILED))
{ {
if (drive->state != Drive::TaskState::CHECK_FAILED) if (finalState != Drive::TaskState::CHECK_FAILED)
{ {
Logger::logThis()->info("Shred-Task: Triggering print for drive - Drive: " + drive->getSerial());
Printer::getPrinter()->print(drive); Printer::getPrinter()->print(drive);
} }
else
{
Logger::logThis()->warning("Shred-Task: Skipping print due to checksum failure - Drive: " + drive->getSerial());
}
drive->state = Drive::TaskState::NONE; drive->state = Drive::TaskState::NONE;
drive->setTaskPercentage(0.0); drive->setTaskPercentage(0.0);
Logger::logThis()->info("Finished shred/check for: " + drive->getModelName() + "-" + drive->getSerial()); Logger::logThis()->info("Completed shred/check for: " + drive->getModelName() + "-" + drive->getSerial());
} }
return 0; return 0;
} }
/** /**
* \brief calc shredding progress in % * \brief calc shredding progress in %
* \param current byte index of the drive
* \param current shred iteration
* \return double percentage * \return double percentage
*/ */
double Shred::calcProgress() double Shred::calcProgress()
@@ -232,63 +567,121 @@ double Shred::calcProgress()
#ifdef ZERO_CHECK #ifdef ZERO_CHECK
uiMaxShredIteration++; // increment because we will check after SHRED_ITERATIONS the drive for non-zero bytes uiMaxShredIteration++; // increment because we will check after SHRED_ITERATIONS the drive for non-zero bytes
#endif #endif
if (this->ulDriveByteSize == 0) if (this->ulDriveByteSize == 0)
return 0.0; return 0.0;
return (double)(((double)ulDriveByteOverallCount) / ((double)this->ulDriveByteSize * uiMaxShredIteration)) * 100.0f;
return (double)(((double)ulDriveByteOverallCount) / ((double)this->ulDriveByteSize * uiMaxShredIteration)) * 100.0;
} }
/**
* \brief rewind drive to beginning
* \param file descriptor
* \return 0 on success, -1 on error
*/
int Shred::iRewindDrive(fileDescriptor file) int Shred::iRewindDrive(fileDescriptor file)
{ {
if (0 != lseek(file, 0L, SEEK_SET)) off_t result = lseek(file, 0L, SEEK_SET);
if (result == -1)
{ {
perror("unable to rewind drive"); int savedErrno = errno;
Logger::logThis()->info("Unable to rewind drive! - fileDescriptor: " + to_string(file)); Logger::logThis()->error("Unable to rewind drive! Error: " + string(strerror(savedErrno)) +
" (errno: " + to_string(savedErrno) + ") - fileDescriptor: " + to_string(file));
return -1; return -1;
} }
else else if (result != 0)
{ {
return 0; Logger::logThis()->error("Rewind position mismatch! Expected: 0, Got: " + to_string(result) +
" - fileDescriptor: " + to_string(file));
return -1;
} }
return 0;
} }
/**
* \brief get drive size in bytes
* \param file descriptor
* \return size in bytes, 0 on error
*/
long Shred::getDriveSizeInBytes(fileDescriptor file) long Shred::getDriveSizeInBytes(fileDescriptor file)
{ {
long liDriveSizeTmp = lseek(file, 0L, SEEK_END); off_t liDriveSizeTmp = lseek(file, 0L, SEEK_END);
if (liDriveSizeTmp == -1) if (liDriveSizeTmp == -1)
{ {
perror("unable to get drive size"); int savedErrno = errno;
Logger::logThis()->info("Unable to get drive size! - fileDescriptor: " + to_string(file)); Logger::logThis()->error("Unable to get drive size! Error: " + string(strerror(savedErrno)) +
" (errno: " + to_string(savedErrno) + ") - fileDescriptor: " + to_string(file));
return 0L; return 0L;
} }
if (0 != iRewindDrive(file)) if (0 != iRewindDrive(file))
{ {
liDriveSizeTmp = 0L; Logger::logThis()->error("Unable to rewind after size detection - fileDescriptor: " + to_string(file));
return 0L;
} }
#ifdef DEMO_DRIVE_SIZE #ifdef DEMO_DRIVE_SIZE
liDriveSizeTmp = DEMO_DRIVE_SIZE; liDriveSizeTmp = DEMO_DRIVE_SIZE;
Logger::logThis()->info("DEMO_DRIVE_SIZE active - using size: " + to_string(liDriveSizeTmp) + " bytes");
#endif #endif
return liDriveSizeTmp; return liDriveSizeTmp;
} }
/**
* \brief calculate checksum of drive (verify all zeros)
* \param file descriptor
* \param pointer to Drive instance
* \param signal file descriptor
* \return checksum value (0 = all zeros)
*/
unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSignalFd) unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSignalFd)
{ {
unsigned int uiChecksum = 0; unsigned int uiChecksum = 0;
unsigned long ulDriveByteCounter = 0U; unsigned long ulDriveByteCounter = 0U;
Logger::logThis()->info("Check-Task: Starting checksum verification - Drive: " + drive->getSerial());
#ifdef ADAPTIVE_CHUNK_SIZE
size_t checkChunkSize = CHUNK_SIZE_MAX;
#else
size_t checkChunkSize = CHUNK_SIZE;
#endif
while (ulDriveByteCounter < ulDriveByteSize) while (ulDriveByteCounter < ulDriveByteSize)
{ {
// Check if task was aborted
if (drive->state.load() != Drive::TaskState::CHECK_ACTIVE)
{
Logger::logThis()->info("Check-Task: Aborted by user at " + to_string(d32Percent) + "% - Drive: " + drive->getSerial());
return UINT32_MAX; // Return non-zero to indicate incomplete check
}
int iBytesToCheck = 0; int iBytesToCheck = 0;
if ((ulDriveByteSize - ulDriveByteCounter) < CHUNK_SIZE) if ((ulDriveByteSize - ulDriveByteCounter) < checkChunkSize)
{ {
iBytesToCheck = (ulDriveByteSize - ulDriveByteCounter); iBytesToCheck = (ulDriveByteSize - ulDriveByteCounter);
} }
else else
{ {
iBytesToCheck = CHUNK_SIZE; iBytesToCheck = checkChunkSize;
} }
int iReadBytes = read(file, caReadBuffer, iBytesToCheck); int iReadBytes = read(file, caReadBuffer, iBytesToCheck);
if (iReadBytes <= 0)
{
int savedErrno = errno;
Logger::logThis()->error("Check-Task: Read failed! Attempted: " + to_string(iBytesToCheck) +
" bytes, Read: " + to_string(iReadBytes) + " bytes" +
" - Position: " + to_string(ulDriveByteCounter) + "/" + to_string(ulDriveByteSize) +
" - Error: " + strerror(savedErrno) + " (errno: " + to_string(savedErrno) + ")" +
" - Drive: " + drive->getSerial());
return UINT32_MAX; // Return non-zero to indicate read failure
}
for (int iReadBytesCounter = 0U; iReadBytesCounter < iReadBytes; iReadBytesCounter++) for (int iReadBytesCounter = 0U; iReadBytesCounter < iReadBytes; iReadBytesCounter++)
{ {
uiChecksum += caReadBuffer[iReadBytesCounter]; uiChecksum += caReadBuffer[iReadBytesCounter];
@@ -301,7 +694,10 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSig
drive->sShredSpeed.store(shredSpeed); drive->sShredSpeed.store(shredSpeed);
#ifdef LOG_LEVEL_HIGH #ifdef LOG_LEVEL_HIGH
Logger::logThis()->info("Shred-Task (Checksum): ByteCount: " + to_string(ulDriveByteCounter) + " - progress: " + to_string(d32Percent) + " - Drive: " + drive->getSerial()); Logger::logThis()->info("Check-Task: ByteCount: " + to_string(ulDriveByteCounter) +
" - progress: " + to_string(d32Percent) + "%" +
" - checksum so far: " + to_string(uiChecksum) +
" - Drive: " + drive->getSerial());
#endif #endif
if (((d32Percent - d32TmpPercent) >= 0.01) || (d32Percent == 100.0)) if (((d32Percent - d32TmpPercent) >= 0.01) || (d32Percent == 100.0))
@@ -314,12 +710,29 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSig
write(*ipSignalFd, "A", 1); write(*ipSignalFd, "A", 1);
} }
} }
Logger::logThis()->info("Check-Task: Verification complete - Final checksum: " + to_string(uiChecksum) + " - Drive: " + drive->getSerial());
drive->bWasChecked = true; drive->bWasChecked = true;
return uiChecksum; return uiChecksum;
} }
/**
* \brief cleanup - close file descriptors
*/
void Shred::cleanup() void Shred::cleanup()
{ {
close(driveFileDiscr); if (driveFileDiscr != -1)
close(randomSrcFileDiscr); {
Logger::logThis()->info("Shred-Task: Closing drive file descriptor: " + to_string(driveFileDiscr));
close(driveFileDiscr);
driveFileDiscr = -1;
}
if (randomSrcFileDiscr != -1)
{
Logger::logThis()->info("Shred-Task: Closing random source file descriptor: " + to_string(randomSrcFileDiscr));
close(randomSrcFileDiscr);
randomSrcFileDiscr = -1;
}
} }
+344 -268
View File
@@ -6,306 +6,382 @@
*/ */
#include "../include/reHDD.h" #include "../include/reHDD.h"
#include <sys/wait.h> // For WIFSIGNALED, WTERMSIG
using namespace std; using namespace std;
/**
* \brief Parse context for SMART attribute values
*/
struct SMARTParseContext
{
// Device information (top-level JSON fields)
string modelFamily;
string modelName;
string serial;
uint64_t capacity;
// Power and temperature (top-level JSON fields)
uint32_t errorCount;
uint32_t powerOnHours;
uint32_t powerCycles;
uint32_t temperature;
// Critical sector counts (from ata_smart_attributes table)
uint32_t reallocatedSectors; // ID 5
uint32_t pendingSectors; // ID 197
uint32_t uncorrectableSectors; // ID 198
// Parser state machine
enum State
{
SEARCHING, // Looking for next field
IN_ATTRIBUTE_5, // Inside ID 5 object
IN_ATTRIBUTE_197, // Inside ID 197 object
IN_ATTRIBUTE_198, // Inside ID 198 object
IN_RAW_SECTION // Inside "raw": { } of current attribute
};
State state;
int currentAttributeId; // Which attribute are we parsing? (5, 197, 198)
SMARTParseContext()
: capacity(0),
errorCount(0),
powerOnHours(0),
powerCycles(0),
temperature(0),
reallocatedSectors(0),
pendingSectors(0),
uncorrectableSectors(0),
state(SEARCHING),
currentAttributeId(0)
{
}
};
/**
* \brief Extract JSON string value
* \param line containing "key": "value"
* \return extracted string value
*/
static string extractStringValue(const string &line)
{
size_t colonPos = line.find(": ");
if (colonPos == string::npos)
return "";
size_t firstQuote = line.find('"', colonPos + 2);
if (firstQuote == string::npos)
return "";
size_t secondQuote = line.find('"', firstQuote + 1);
if (secondQuote == string::npos)
return "";
return line.substr(firstQuote + 1, secondQuote - firstQuote - 1);
}
/**
* \brief Extract JSON integer value
* \param line containing "key": number
* \return extracted integer value
*/
static uint64_t extractIntegerValue(const string &line)
{
size_t colonPos = line.find(": ");
if (colonPos == string::npos)
return 0;
string valueStr = line.substr(colonPos + 2);
// Remove whitespace, commas, braces
valueStr.erase(remove_if(valueStr.begin(), valueStr.end(),
[](char c)
{ return c == ' ' || c == ',' || c == '}' || c == '\n'; }),
valueStr.end());
// Verify it's a valid number
if (valueStr.empty() || valueStr.find_first_not_of("0123456789") != string::npos)
return 0;
try
{
return stoull(valueStr);
}
catch (...)
{
return 0;
}
}
/**
* \brief Process a single line of JSON output
* \param line from smartctl JSON output
* \param context parsing context with state
* \return void
*/
static void processLine(const string &line, SMARTParseContext &ctx)
{
// Trim whitespace for consistent parsing
string trimmed = line;
size_t firstNonSpace = trimmed.find_first_not_of(" \t\r\n");
if (firstNonSpace != string::npos)
{
trimmed = trimmed.substr(firstNonSpace);
}
// Parse top-level device information
if (trimmed.find("\"model_family\":") == 0)
{
ctx.modelFamily = extractStringValue(line);
return;
}
if (trimmed.find("\"model_name\":") == 0)
{
ctx.modelName = extractStringValue(line);
return;
}
if (trimmed.find("\"serial_number\":") == 0)
{
ctx.serial = extractStringValue(line);
return;
}
// Parse capacity from user_capacity.bytes
if (trimmed.find("\"bytes\":") == 0)
{
ctx.capacity = extractIntegerValue(line);
return;
}
// Parse error count from self_test log
if (trimmed.find("\"error_count_total\":") == 0)
{
ctx.errorCount = extractIntegerValue(line);
return;
}
// Parse power-on hours
if (trimmed.find("\"hours\":") == 0)
{
ctx.powerOnHours = extractIntegerValue(line);
return;
}
// Parse power cycle count
if (trimmed.find("\"power_cycle_count\":") == 0)
{
ctx.powerCycles = extractIntegerValue(line);
return;
}
// Parse temperature
if (trimmed.find("\"current\":") == 0 && ctx.temperature == 0)
{
// Only parse first occurrence (temperature section, not other "current" fields)
ctx.temperature = extractIntegerValue(line);
return;
}
// State machine for SMART attributes parsing
switch (ctx.state)
{
case SMARTParseContext::SEARCHING:
// Look for critical attribute IDs
if (trimmed.find("\"id\": 5,") == 0)
{
ctx.state = SMARTParseContext::IN_ATTRIBUTE_5;
ctx.currentAttributeId = 5;
}
else if (trimmed.find("\"id\": 197,") == 0)
{
ctx.state = SMARTParseContext::IN_ATTRIBUTE_197;
ctx.currentAttributeId = 197;
}
else if (trimmed.find("\"id\": 198,") == 0)
{
ctx.state = SMARTParseContext::IN_ATTRIBUTE_198;
ctx.currentAttributeId = 198;
}
break;
case SMARTParseContext::IN_ATTRIBUTE_5:
case SMARTParseContext::IN_ATTRIBUTE_197:
case SMARTParseContext::IN_ATTRIBUTE_198:
// Look for "raw": { start
if (trimmed.find("\"raw\":") == 0)
{
ctx.state = SMARTParseContext::IN_RAW_SECTION;
}
// Look for end of attribute object (more indented closing brace = end of attribute)
// " }," or " }" at attribute level (6 spaces)
else if (line.find(" },") == 0 || line.find(" }") == 0)
{
ctx.state = SMARTParseContext::SEARCHING;
ctx.currentAttributeId = 0;
}
break;
case SMARTParseContext::IN_RAW_SECTION:
// Look for "value": number inside raw section
if (trimmed.find("\"value\":") == 0)
{
uint64_t value = extractIntegerValue(line);
// Store value in appropriate field based on current attribute
if (ctx.currentAttributeId == 5)
{
ctx.reallocatedSectors = static_cast<uint32_t>(value);
}
else if (ctx.currentAttributeId == 197)
{
ctx.pendingSectors = static_cast<uint32_t>(value);
}
else if (ctx.currentAttributeId == 198)
{
ctx.uncorrectableSectors = static_cast<uint32_t>(value);
}
// Stay in raw section - closing brace will exit
}
// Look for end of raw object (less indented = back to attribute level)
// " }" at raw level (8 spaces)
else if (line.find(" }") == 0)
{
// Return to attribute state (raw section closed)
ctx.state = (ctx.currentAttributeId == 5) ? SMARTParseContext::IN_ATTRIBUTE_5 : (ctx.currentAttributeId == 197) ? SMARTParseContext::IN_ATTRIBUTE_197
: SMARTParseContext::IN_ATTRIBUTE_198;
}
break;
}
}
/** /**
* \brief get and set S.M.A.R.T. values in Drive * \brief get and set S.M.A.R.T. values in Drive
* \param pointer of Drive instance * \param pointer of Drive instance
* \return void * \return void
*/ */
void SMART::readSMARTData(Drive *drive) void SMART::readSMARTData(Drive *drive)
{ {
string modelFamily; SMARTParseContext ctx;
string modelName; uint8_t exitStatus = 255U;
string serial;
uint64_t capacity = 0U;
uint32_t errorCount = 0U;
uint32_t powerOnHours = 0U;
uint32_t powerCycles = 0U;
uint32_t temperature = 0U;
modelFamily.clear(); // Command order optimized for USB adapters
modelName.clear(); // Standard commands first, then device-specific variants
serial.clear(); string sSmartctlCommands[] = {
" --json -a ", // Try standard first
" --json -d sat -a ", // SAT (SCSI/ATA Translation) - most USB adapters
" --json -d usbjmicron -a ", // USB JMicron
" --json -d usbprolific -a ", // USB Prolific
" --json -d usbsunplus -a " // USB Sunplus
};
string sSmartctlCommands[] = {" --json -a ", " --json -d sntjmicron -a ", " --json -d sntasmedia -a ", " --json -d sntrealtek -a ", " --json -d sat -a "}; for (const string &sSmartctlCommand : sSmartctlCommands)
for (string sSmartctlCommand : sSmartctlCommands)
{ {
string sCMD = ("smartctl"); // Build command with timeout
string sCMD = "timeout 5 smartctl"; // 5 second timeout prevents hanging
sCMD.append(sSmartctlCommand); sCMD.append(sSmartctlCommand);
sCMD.append(drive->getPath()); sCMD.append(drive->getPath());
const char *cpComand = sCMD.c_str(); // Note: stderr NOT suppressed for debugging
// Logger::logThis()->info(cpComand); Logger::logThis()->info("SMART: Executing: " + sCMD);
FILE *outputfileSmart = popen(cpComand, "r"); // Execute smartctl with timeout protection
size_t len = 0U; // length of found line FILE *outputfileSmart = popen(sCMD.c_str(), "r");
char *cLine = NULL; // found line if (outputfileSmart == nullptr)
uint8_t status = 255U;
while ((getline(&cLine, &len, outputfileSmart)) != -1)
{ {
string sLine = string(cLine); Logger::logThis()->error("SMART: Failed to execute smartctl");
continue;
}
SMART::parseExitStatus(sLine, status); // Reset context for new attempt
SMART::parseModelFamily(sLine, modelFamily); ctx = SMARTParseContext();
SMART::parseModelName(sLine, modelName);
SMART::parseSerial(sLine, serial); // Parse output line by line
SMART::parseCapacity(sLine, capacity); char *cLine = nullptr;
SMART::parseErrorCount(sLine, errorCount); size_t len = 0;
SMART::parsePowerOnHours(sLine, powerOnHours); int lineCount = 0;
SMART::parsePowerCycles(sLine, powerCycles);
SMART::parseTemperature(sLine, temperature); while (getline(&cLine, &len, outputfileSmart) != -1)
{
string sLine(cLine);
lineCount++;
// Parse exit status
if (sLine.find("\"exit_status\":") != string::npos)
{
exitStatus = static_cast<uint8_t>(extractIntegerValue(sLine));
}
// Process this line
processLine(sLine, ctx);
} }
free(cLine); free(cLine);
pclose(outputfileSmart); int pcloseStatus = pclose(outputfileSmart);
if (status == 0U) Logger::logThis()->info("SMART: Parsed " + to_string(lineCount) + " lines, exit status: " + to_string(exitStatus));
// Check if timeout killed the process
if (WIFSIGNALED(pcloseStatus) && WTERMSIG(pcloseStatus) == SIGTERM)
{ {
// Found S.M.A.R.T. data with this command Logger::logThis()->warning("SMART: Command timed out (5s) - skipping to next variant");
// Logger::logThis()->info("Found S.M.A.R.T. data with this command"); continue;
break;
} }
}
drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycles, temperature); // write data in drive // IGNORE exit status - instead check if we got valid data!
} // Exit status 64 means "error log contains errors" but SMART data is still valid
// Exit status 4 means "some prefail attributes concerning" but data is valid
/** // What matters: Did we parse model name and serial?
* \brief parse ExitStatus if (!ctx.modelName.empty() && !ctx.serial.empty())
* \param string output line of smartctl
* \param uint8_t parsed status
* \return bool if parsing was possible
*/
bool SMART::parseExitStatus(string sLine, uint8_t &status)
{
string search("\"exit_status\": ");
size_t found = sLine.find(search);
if (found != string::npos)
{
sLine.erase(0U, sLine.find(": ") + 1U);
status = stol(sLine);
return true;
}
else
{
return false;
}
}
/**
* \brief parse ModelFamily
* \param string output line of smartctl
* \param string parsed model family
* \return bool if parsing was possible
*/
bool SMART::parseModelFamily(string sLine, string &modelFamily)
{
string search("\"model_family\": ");
size_t found = sLine.find(search);
if (found != string::npos)
{
sLine.erase(0U, sLine.find(": ") + 3U);
if (sLine.length() >= 3U)
{ {
sLine.erase(sLine.length() - 3U, 3U); Logger::logThis()->info("SMART: Successfully parsed data");
} Logger::logThis()->info("SMART: Model: " + ctx.modelName);
modelFamily = sLine; Logger::logThis()->info("SMART: Serial: " + ctx.serial);
return true; Logger::logThis()->info("SMART: Capacity: " + to_string(ctx.capacity) + " bytes");
} Logger::logThis()->info("SMART: Power-On Hours: " + to_string(ctx.powerOnHours));
else Logger::logThis()->info("SMART: Temperature: " + to_string(ctx.temperature) + " C");
{ Logger::logThis()->info("SMART: Reallocated Sectors: " + to_string(ctx.reallocatedSectors));
return false; Logger::logThis()->info("SMART: Pending Sectors: " + to_string(ctx.pendingSectors));
} Logger::logThis()->info("SMART: Uncorrectable Sectors: " + to_string(ctx.uncorrectableSectors));
}
/** if (exitStatus != 0)
* \brief parse ModelName {
* \param string output line of smartctl Logger::logThis()->info("SMART: Note - exit status " + to_string(exitStatus) + " indicates warnings/errors in SMART log");
* \param string parsed model name }
* \return bool if parsing was possible
*/
bool SMART::parseModelName(string sLine, string &modelName)
{
string search("\"model_name\": ");
size_t found = sLine.find(search);
if (found != string::npos)
{
sLine.erase(0U, sLine.find(": ") + 3U);
if (sLine.length() >= 3U)
{
sLine.erase(sLine.length() - 3U, 3U);
}
modelName = sLine;
return true;
}
else
{
return false;
}
}
/** break; // Success - we got data!
* \brief parse Serial
* \param string output line of smartctl
* \param string parsed serial
* \return bool if parsing was possible
*/
bool SMART::parseSerial(string sLine, string &serial)
{
string search("\"serial_number\": ");
size_t found = sLine.find(search);
if (found != string::npos)
{
sLine.erase(0, sLine.find(": ") + 3);
if (sLine.length() >= 3U)
{
sLine.erase(sLine.length() - 3U, 3U);
}
serial = sLine;
return true;
}
else
{
return false;
}
}
/**
* \brief parse Capacity
* \param string output line of smartctl
* \param string parsed capacity
* \return bool if parsing was possible
*/
bool SMART::parseCapacity(string sLine, uint64_t &capacity)
{
string search("\"bytes\": ");
size_t found = sLine.find(search);
if (found != string::npos)
{
sLine.erase(0, sLine.find(": ") + 2);
if (sLine.length() >= 1U)
{
sLine.erase(sLine.length() - 1U, 1U);
}
capacity = stol(sLine);
return true;
}
else
{
return false;
}
}
/**
* \brief parse ErrorCount
* \param string output line of smartctl
* \param uint32_t parsed error count
* \return bool if parsing was possible
*/
bool SMART::parseErrorCount(string sLine, uint32_t &errorCount)
{
string search("\"error_count_total\": ");
size_t found = sLine.find(search);
if (found != string::npos)
{
sLine.erase(0U, sLine.find(": ") + 2U);
if (sLine.length() >= 2U)
{
sLine.erase(sLine.length() - 2U, 2U);
}
errorCount = stol(sLine);
return true;
}
else
{
return false;
}
}
/**
* \brief parse PowerOnHours
* \param string output line of smartctl\
* \param uint32_t parsed power on hours
* \return bool if parsing was possible
*/
bool SMART::parsePowerOnHours(string sLine, uint32_t &powerOnHours)
{
string search("\"hours\": ");
size_t found = sLine.find(search);
if (found != string::npos)
{
sLine.erase(0U, sLine.find(": ") + 2U);
if (sLine.length() >= 1U)
{
sLine.erase(sLine.length() - 1U, 1U);
}
powerOnHours = stol(sLine);
return true;
}
else
{
return false;
}
}
/**
* \brief parse PowerCycle
* \param string output line of smartctl
* \param uint32_t parsed power cycles
* \return bool if parsing was possible
*/
bool SMART::parsePowerCycles(string sLine, uint32_t &powerCycles)
{
string search("\"power_cycle_count\": ");
size_t found = sLine.find(search);
if (found != string::npos)
{
sLine.erase(0, sLine.find(": ") + 2);
if (sLine.length() >= 2U)
{
sLine.erase(sLine.length() - 2U, 2U);
}
powerCycles = stol(sLine);
return true;
}
else
{
return false;
}
}
/**
* \brief parse temperature
* \param string output line of smartctl
* \param uint32_t parsed temperature
* \return bool if parsing was possible
*/
bool SMART::parseTemperature(string sLine, uint32_t &temperature)
{
string search("\"current\": ");
size_t found = sLine.find(search);
if (found != string::npos)
{
sLine.erase(0U, sLine.find(": ") + 2U);
if (sLine.length() >= 1U)
{
sLine.erase(sLine.length() - 1U, 2U);
}
if (sLine == "{")
{
temperature = 0U; // this drive doesn't support temperature
} }
else else
{ {
temperature = stol(sLine); Logger::logThis()->warning("SMART: No valid data parsed (exit status: " + to_string(exitStatus) + ")");
} }
return true;
} }
else
// Check if we got ANY data
if (ctx.modelName.empty() && ctx.serial.empty())
{ {
return false; Logger::logThis()->warning("SMART: No SMART data available for this drive - may not support SMART or need root privileges");
// Try basic device info without SMART (use hdparm or similar as fallback)
// For now, just log that SMART is not available
ctx.modelName = "SMART not available";
ctx.serial = "N/A";
} }
// Write parsed data to drive
drive->setDriveSMARTData(
ctx.modelFamily,
ctx.modelName,
ctx.serial,
ctx.capacity,
ctx.errorCount,
ctx.powerOnHours,
ctx.powerCycles,
ctx.temperature,
ctx.reallocatedSectors,
ctx.pendingSectors,
ctx.uncorrectableSectors);
} }
+25 -3
View File
@@ -110,10 +110,10 @@ void TUI::updateTUI(list<Drive> *plistDrives, uint8_t u8SelectedEntry)
bSelectedEntry = true; // mark this drive in entries list bSelectedEntry = true; // mark this drive in entries list
displaySelectedDrive(*it, u16StdscrX, u16StdscrY); displaySelectedDrive(*it, u16StdscrX, u16StdscrY);
if ((it->getPowerOnHours() >= WORSE_HOURS) || (it->getPowerCycles() >= WORSE_POWERUP) || (it->getErrorCount() > 0) || (it->getTemperature() >= WORSE_TEMPERATURE)) if ((it->getPowerOnHours() >= WORSE_HOURS) || (it->getPowerCycles() >= WORSE_POWERUP) || (it->getErrorCount() > 0) || (it->getTemperature() >= WORSE_TEMPERATURE) || (it->getReallocatedSectors() > 0) || (it->getPendingSectors() > 0) || (it->getUncorrectableSectors() > 0))
{ {
// smart values are bad --> show warning // smart values are bad --> show warning
smartWarning = createSmartWarning(50, 10, ((u16StdscrX) - (int)(u16StdscrX / 2) + 35), (int)(u16StdscrY / 2) - 5, it->getPath(), it->getPowerOnHours(), it->getPowerCycles(), it->getErrorCount(), it->getTemperature()); smartWarning = createSmartWarning(50, 14, ((u16StdscrX) - (int)(u16StdscrX / 2) + 35), (int)(u16StdscrY / 2) - 7, it->getPath(), it->getPowerOnHours(), it->getPowerCycles(), it->getErrorCount(), it->getTemperature(), it->getReallocatedSectors(), it->getPendingSectors(), it->getUncorrectableSectors());
wrefresh(smartWarning); wrefresh(smartWarning);
} }
} }
@@ -721,7 +721,7 @@ void TUI::displaySelectedDrive(Drive &drive, int stdscrX, int stdscrY)
} }
} }
WINDOW *TUI::createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart, string sPath, uint32_t u32PowerOnHours, uint32_t u32PowerCycles, uint32_t u32ErrorCount, uint32_t u32Temperature) WINDOW *TUI::createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart, string sPath, uint32_t u32PowerOnHours, uint32_t u32PowerCycles, uint32_t u32ErrorCount, uint32_t u32Temperature, uint32_t u32ReallocatedSectors, uint32_t u32PendingSectors, uint32_t u32UncorrectableSectors)
{ {
WINDOW *newWindow; WINDOW *newWindow;
newWindow = newwin(iYSize, iXSize, iYStart, iXStart); newWindow = newwin(iYSize, iXSize, iYStart, iXStart);
@@ -763,6 +763,28 @@ WINDOW *TUI::createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart
{ {
string sLineTmp = "Drive too hot: " + to_string(u32Temperature) + " C"; string sLineTmp = "Drive too hot: " + to_string(u32Temperature) + " C";
mvwaddstr(newWindow, u16Line++, (iXSize / 2) - (sLine01.size() / 2), sLineTmp.c_str()); mvwaddstr(newWindow, u16Line++, (iXSize / 2) - (sLine01.size() / 2), sLineTmp.c_str());
u16Line++;
} }
if (u32ReallocatedSectors > 0)
{
string sLineTmp = "CRITICAL: Reallocated sectors detected: " + to_string(u32ReallocatedSectors);
mvwaddstr(newWindow, u16Line++, (iXSize / 2) - (sLine01.size() / 2), sLineTmp.c_str());
u16Line++;
}
if (u32PendingSectors > 0)
{
string sLineTmp = "CRITICAL: Pending sectors detected: " + to_string(u32PendingSectors);
mvwaddstr(newWindow, u16Line++, (iXSize / 2) - (sLine01.size() / 2), sLineTmp.c_str());
u16Line++;
}
if (u32UncorrectableSectors > 0)
{
string sLineTmp = "CRITICAL: Uncorrectable sectors: " + to_string(u32UncorrectableSectors);
mvwaddstr(newWindow, u16Line++, (iXSize / 2) - (sLine01.size() / 2), sLineTmp.c_str());
}
return newWindow; return newWindow;
} }