2 Commits

Author SHA1 Message Date
localhorst 6b72736f00 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:13:28 +02:00
localhorst 831a892041 show HDD warnings based on sectors 2026-04-28 21:20:30 +02:00
9 changed files with 468 additions and 74 deletions
+11 -2
View File
@@ -72,7 +72,10 @@ private:
uint32_t u32ErrorCount = 0U;
uint32_t u32PowerOnHours = 0U; // in hours
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;
private:
@@ -106,6 +109,9 @@ public:
uint32_t getPowerOnHours(void); // in hours
uint32_t getPowerCycles(void);
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 setDriveSMARTData(std::string modelFamily,
@@ -115,7 +121,10 @@ public:
uint32_t errorCount,
uint32_t powerOnHours,
uint32_t powerCycles,
uint32_t temperature);
uint32_t temperature,
uint32_t reallocatedSectors,
uint32_t pendingSectors,
uint32_t uncorrectableSectors);
std::string sCapacityToText();
std::string sErrorCountToText();
+1 -1
View File
@@ -8,7 +8,7 @@
#ifndef REHDD_H_
#define REHDD_H_
#define REHDD_VERSION "V1.3.0"
#define REHDD_VERSION "V1.3.1"
// Drive handling Settings
#define WORSE_HOURS 19200 // mark drive if at this limit or beyond
+3
View File
@@ -28,6 +28,9 @@ private:
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);
static bool parseReallocatedSectors(std::string sLine, uint32_t &reallocatedSectors);
static bool parsePendingSectors(std::string sLine, uint32_t &pendingSectors);
static bool parseUncorrectableSectors(std::string sLine, uint32_t &uncorrectableSectors);
};
#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 *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 *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);
void displaySelectedDrive(Drive &drive, int stdscrX, int stdscrY);
+22 -1
View File
@@ -140,6 +140,21 @@ uint32_t Drive::getTemperature(void)
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()
{
char acBuffer[16];
@@ -226,7 +241,10 @@ void Drive::setDriveSMARTData(string modelFamily,
uint32_t errorCount,
uint32_t powerOnHours,
uint32_t powerCycle,
uint32_t temperature)
uint32_t temperature,
uint32_t reallocatedSectors,
uint32_t pendingSectors,
uint32_t uncorrectableSectors)
{
this->sSmartData.sModelFamily = modelFamily;
this->sSmartData.sModelName = modelName;
@@ -236,6 +254,9 @@ void Drive::setDriveSMARTData(string modelFamily,
this->sSmartData.u32PowerOnHours = powerOnHours;
this->sSmartData.u32PowerCycles = powerCycle;
this->sSmartData.u32Temperature = temperature;
this->sSmartData.u32ReallocatedSectors = reallocatedSectors;
this->sSmartData.u32PendingSectors = pendingSectors;
this->sSmartData.u32UncorrectableSectors = uncorrectableSectors;
}
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
// 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
Logger::logThis()->info("Delete new drive, because already attached: " + itNew->getModelName());
#endif
+276 -64
View File
@@ -29,16 +29,20 @@ Shred::~Shred()
/**
* \brief shred drive with shred
* \param pointer of Drive instance
* \return void
* \param pointer of Drive instance
* \param file descriptor for signaling
* \return 0 on success, -1 on error
*/
int Shred::shredDrive(Drive *drive, int *ipSignalFd)
{
ostringstream address;
address << (void const *)&(*drive);
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->bWasChecked = false;
drive->setTaskPercentage(0.0);
drive->u32DriveChecksumAfterShredding = UINT32_MAX;
drive->state = Drive::TaskState::SHRED_ACTIVE;
@@ -46,53 +50,137 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
#ifdef DRYRUN
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);
usleep(20000);
}
// Only mark as shredded if DRYRUN completed successfully
drive->bWasShredded = true;
drive->setTaskPercentage(0.0);
drive->state = Drive::TaskState::NONE;
Logger::logThis()->info("DRYRUN completed - Drive: " + drive->getSerial());
return 0;
#endif
#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];
// open random source
// Open random source
Logger::logThis()->info("Shred-Task: Opening random source: " + string(randomsrc) + " - Drive: " + drive->getSerial());
randomSrcFileDiscr = open(randomsrc, O_RDONLY | O_LARGEFILE);
if (randomSrcFileDiscr == -1)
{
std::string errorMsg(strerror(errno));
Logger::logThis()->error("Shred-Task: Open random source failed! " + errorMsg + " - Drive: " + drive->getSerial());
perror(randomsrc);
cleanup();
int savedErrno = errno;
Logger::logThis()->error("Shred-Task: Open random source failed! Path: " + string(randomsrc) +
" - Error: " + strerror(savedErrno) + " (errno: " + to_string(savedErrno) + ")" +
" - 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;
}
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);
if (driveFileDiscr == -1)
{
std::string errorMsg(strerror(errno));
Logger::logThis()->error("Shred-Task: Open drive failed! " + errorMsg + " - Drive: " + drive->getSerial());
perror(cpDrivePath);
cleanup();
int savedErrno = errno;
string errorDetail;
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;
}
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));
if (readRet <= 0)
{
std::string errorMsg(strerror(errno));
Logger::logThis()->error("Shred-Task: Read random key failed! " + errorMsg + " - Drive: " + drive->getSerial());
perror(randomsrc);
int savedErrno = errno;
Logger::logThis()->error("Shred-Task: Read random key failed! Expected: " + to_string(sizeof(ucKey)) +
" bytes, Got: " + to_string(readRet) + " bytes" +
" - Error: " + strerror(savedErrno) + " (errno: " + to_string(savedErrno) + ")" +
" - 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;
}
Logger::logThis()->info("Shred-Task: Random key read successfully (" + to_string(readRet) + " bytes) - Drive: " + drive->getSerial());
tfng_prng_seedkey(ucKey);
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();
shredSpeed.chronoShredTimestamp = std::chrono::system_clock::now(); // set inital timestamp for speed metric
shredSpeed.ulSpeedMetricBytesWritten = 0U; // uses to calculate speed metric
@@ -102,9 +190,12 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
Logger::logThis()->info("Shred-Task: Bytes-Size of Drive: " + to_string(this->ulDriveByteSize) + " - Drive: " + drive->getSerial());
#endif
// Main shredding loop
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))
{
@@ -114,11 +205,29 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
while (ulDriveByteCounter < ulDriveByteSize)
{
int iBytesToShred = 0; // Bytes that will be overwritten in this chunk-iteration
// Check if task was aborted
if (drive->state.load() != Drive::TaskState::SHRED_ACTIVE)
{
Logger::logThis()->info("Shred-Task: Aborted by user at " + to_string(d32Percent) +
"% in iteration " + to_string(uiShredIterationCounter + 1) +
" - Drive: " + drive->getSerial());
drive->setTaskPercentage(0);
d32Percent = 0.00;
d32TmpPercent = 0.00;
cleanup();
// CRITICAL: Mark as NOT shredded on abort
drive->state = Drive::TaskState::NONE;
drive->bWasShredded = false;
drive->bWasChecked = false;
return -1;
}
int iBytesToShred = 0;
if (uiShredIterationCounter != (SHRED_ITERATIONS - 1))
{
// NOT last shred iteration --> generate new random data
// Generate random data for this chunk
tfng_prng_genrandom(caTfngData, TFNG_DATA_SIZE);
}
@@ -135,10 +244,20 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
if (iByteShredded <= 0)
{
std::string errorMsg(strerror(errno));
Logger::logThis()->error("Shred-Task: Write to drive failed! " + errorMsg + " - Drive: " + drive->getSerial());
perror("unable to write random data");
int savedErrno = errno;
Logger::logThis()->error("Shred-Task: Write to drive failed! Attempted: " + to_string(iBytesToShred) +
" 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();
// 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;
}
@@ -150,7 +269,10 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
ulDriveByteOverallCount += iByteShredded;
d32Percent = this->calcProgress();
#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
if ((d32Percent - d32TmpPercent) >= 0.01)
@@ -158,36 +280,37 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
// set shred percantage
drive->setTaskPercentage(d32TmpPercent);
d32TmpPercent = d32Percent;
// signal process in shreding
// signal process in shredding
write(*ipSignalFd, "A", 1);
}
if (drive->state != Drive::TaskState::SHRED_ACTIVE)
{
drive->setTaskPercentage(0);
d32Percent = 0.00;
d32TmpPercent = 0.00;
ulDriveByteCounter = 0U;
Logger::logThis()->info("Aborted shred for: " + drive->getModelName() + "-" + drive->getSerial());
cleanup();
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))
{
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();
// 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;
}
// end one shred iteration
}
// end of all shred iteratio
tfng_prng_seedkey(NULL); // reset random generator
// All shred iterations completed successfully
tfng_prng_seedkey(NULL);
// ONLY mark as shredded if ALL iterations completed AND fsync succeeded
drive->bWasShredded = true;
Logger::logThis()->info("Shred-Task finished - Drive: " + drive->getModelName() + "-" + drive->getSerial() + " @" + address.str());
Logger::logThis()->info("Shred-Task finished successfully - Drive: " + drive->getModelName() + "-" + drive->getSerial() + " @" + address.str());
#ifdef ZERO_CHECK
drive->state = Drive::TaskState::CHECK_ACTIVE;
Logger::logThis()->info("Check-Task started - Drive: " + drive->getModelName() + "-" + drive->getSerial() + " @" + address.str());
@@ -196,33 +319,49 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
if (drive->u32DriveChecksumAfterShredding != 0)
{
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
{
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
cleanup();
#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);
}
else
{
Logger::logThis()->warning("Shred-Task: Skipping print due to checksum failure - Drive: " + drive->getSerial());
}
drive->state = Drive::TaskState::NONE;
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;
}
/**
* \brief calc shredding progress in %
* \param current byte index of the drive
* \param current shred iteration
* \return double percentage
*/
double Shred::calcProgress()
@@ -232,54 +371,95 @@ double Shred::calcProgress()
#ifdef ZERO_CHECK
uiMaxShredIteration++; // increment because we will check after SHRED_ITERATIONS the drive for non-zero bytes
#endif
if (this->ulDriveByteSize == 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)
{
if (0 != lseek(file, 0L, SEEK_SET))
off_t result = lseek(file, 0L, SEEK_SET);
if (result == -1)
{
perror("unable to rewind drive");
Logger::logThis()->info("Unable to rewind drive! - fileDescriptor: " + to_string(file));
int savedErrno = errno;
Logger::logThis()->error("Unable to rewind drive! Error: " + string(strerror(savedErrno)) +
" (errno: " + to_string(savedErrno) + ") - fileDescriptor: " + to_string(file));
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 liDriveSizeTmp = lseek(file, 0L, SEEK_END);
off_t liDriveSizeTmp = lseek(file, 0L, SEEK_END);
if (liDriveSizeTmp == -1)
{
perror("unable to get drive size");
Logger::logThis()->info("Unable to get drive size! - fileDescriptor: " + to_string(file));
int savedErrno = errno;
Logger::logThis()->error("Unable to get drive size! Error: " + string(strerror(savedErrno)) +
" (errno: " + to_string(savedErrno) + ") - fileDescriptor: " + to_string(file));
return 0L;
}
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
liDriveSizeTmp = DEMO_DRIVE_SIZE;
Logger::logThis()->info("DEMO_DRIVE_SIZE active - using size: " + to_string(liDriveSizeTmp) + " bytes");
#endif
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 uiChecksum = 0;
unsigned long ulDriveByteCounter = 0U;
Logger::logThis()->info("Check-Task: Starting checksum verification - Drive: " + drive->getSerial());
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;
if ((ulDriveByteSize - ulDriveByteCounter) < CHUNK_SIZE)
{
iBytesToCheck = (ulDriveByteSize - ulDriveByteCounter);
@@ -289,6 +469,18 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSig
iBytesToCheck = CHUNK_SIZE;
}
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++)
{
uiChecksum += caReadBuffer[iReadBytesCounter];
@@ -301,7 +493,10 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSig
drive->sShredSpeed.store(shredSpeed);
#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
if (((d32Percent - d32TmpPercent) >= 0.01) || (d32Percent == 100.0))
@@ -314,12 +509,29 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSig
write(*ipSignalFd, "A", 1);
}
}
Logger::logThis()->info("Check-Task: Verification complete - Final checksum: " + to_string(uiChecksum) + " - Drive: " + drive->getSerial());
drive->bWasChecked = true;
return uiChecksum;
}
/**
* \brief cleanup - close file descriptors
*/
void Shred::cleanup()
{
close(driveFileDiscr);
close(randomSrcFileDiscr);
if (driveFileDiscr != -1)
{
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;
}
}
+128 -1
View File
@@ -23,6 +23,9 @@ void SMART::readSMARTData(Drive *drive)
uint32_t powerOnHours = 0U;
uint32_t powerCycles = 0U;
uint32_t temperature = 0U;
uint32_t reallocatedSectors = 0U;
uint32_t pendingSectors = 0U;
uint32_t uncorrectableSectors = 0U;
modelFamily.clear();
modelName.clear();
@@ -57,6 +60,9 @@ void SMART::readSMARTData(Drive *drive)
SMART::parsePowerOnHours(sLine, powerOnHours);
SMART::parsePowerCycles(sLine, powerCycles);
SMART::parseTemperature(sLine, temperature);
SMART::parseReallocatedSectors(sLine, reallocatedSectors);
SMART::parsePendingSectors(sLine, pendingSectors);
SMART::parseUncorrectableSectors(sLine, uncorrectableSectors);
}
free(cLine);
@@ -70,7 +76,7 @@ void SMART::readSMARTData(Drive *drive)
}
}
drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycles, temperature); // write data in drive
drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycles, temperature, reallocatedSectors, pendingSectors, uncorrectableSectors); // write data in drive
}
/**
@@ -309,3 +315,124 @@ bool SMART::parseTemperature(string sLine, uint32_t &temperature)
return false;
}
}
/**
* \brief parse Reallocated Sectors Count (SMART ID 0x05)
* \param string output line of smartctl
* \param uint32_t parsed reallocated sectors count
* \return bool if parsing was possible
*/
bool SMART::parseReallocatedSectors(string sLine, uint32_t &reallocatedSectors)
{
string search("\"id\": 5,");
size_t found = sLine.find(search);
if (found != string::npos)
{
// Found attribute ID 5 (Reallocated_Sector_Ct)
// Now we need to find the raw value in the next lines
// smartctl JSON format: "raw": { "value": <number>, ... }
return true; // Mark that we found the attribute
}
// Look for the raw value if we're in the right attribute
search = "\"value\":";
found = sLine.find(search);
if (found != string::npos && sLine.find("\"raw\":") != string::npos)
{
// Extract value after "value":
sLine.erase(0U, sLine.find("\"value\":") + 8U);
// Remove trailing characters
size_t comma = sLine.find(",");
if (comma != string::npos)
{
sLine = sLine.substr(0, comma);
}
// Remove whitespace
sLine.erase(remove(sLine.begin(), sLine.end(), ' '), sLine.end());
if (!sLine.empty() && sLine.find_first_not_of("0123456789") == string::npos)
{
reallocatedSectors = stoul(sLine);
return true;
}
}
return false;
}
/**
* \brief parse Current Pending Sector Count (SMART ID 0xC5)
* \param string output line of smartctl
* \param uint32_t parsed pending sectors count
* \return bool if parsing was possible
*/
bool SMART::parsePendingSectors(string sLine, uint32_t &pendingSectors)
{
string search("\"id\": 197,"); // 0xC5 = 197 decimal
size_t found = sLine.find(search);
if (found != string::npos)
{
return true; // Mark that we found the attribute
}
// Look for the raw value
search = "\"value\":";
found = sLine.find(search);
if (found != string::npos && sLine.find("\"raw\":") != string::npos)
{
sLine.erase(0U, sLine.find("\"value\":") + 8U);
size_t comma = sLine.find(",");
if (comma != string::npos)
{
sLine = sLine.substr(0, comma);
}
sLine.erase(remove(sLine.begin(), sLine.end(), ' '), sLine.end());
if (!sLine.empty() && sLine.find_first_not_of("0123456789") == string::npos)
{
pendingSectors = stoul(sLine);
return true;
}
}
return false;
}
/**
* \brief parse Offline Uncorrectable Sectors (SMART ID 0xC6)
* \param string output line of smartctl
* \param uint32_t parsed uncorrectable sectors count
* \return bool if parsing was possible
*/
bool SMART::parseUncorrectableSectors(string sLine, uint32_t &uncorrectableSectors)
{
string search("\"id\": 198,"); // 0xC6 = 198 decimal
size_t found = sLine.find(search);
if (found != string::npos)
{
return true; // Mark that we found the attribute
}
// Look for the raw value
search = "\"value\":";
found = sLine.find(search);
if (found != string::npos && sLine.find("\"raw\":") != string::npos)
{
sLine.erase(0U, sLine.find("\"value\":") + 8U);
size_t comma = sLine.find(",");
if (comma != string::npos)
{
sLine = sLine.substr(0, comma);
}
sLine.erase(remove(sLine.begin(), sLine.end(), ' '), sLine.end());
if (!sLine.empty() && sLine.find_first_not_of("0123456789") == string::npos)
{
uncorrectableSectors = stoul(sLine);
return true;
}
}
return false;
}
+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
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
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);
}
}
@@ -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;
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";
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;
}