1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/*
* SPDX-FileCopyrightText: 2024 Bohdan Onofriichuk <bogdan.onofriuchuk@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "deviceerrormonitor_p.h"
#include <QProcess>
#include <QRegularExpression>
#include <QStringView>
#include "devicenotifier_debug.h"
#include <Solid/OpticalDisc>
#include <Solid/OpticalDrive>
#include <Solid/StorageDrive>
#include <Solid/StorageVolume>
#include <KLocalizedString>
#include <KNotification>
#include <processcore/process.h>
#include <processcore/processes.h>
#include "devicestatemonitor_p.h"
using namespace Qt::StringLiterals;
DeviceErrorMonitor::DeviceErrorMonitor(QObject *parent)
: QObject(parent)
{
}
DeviceErrorMonitor::~DeviceErrorMonitor()
{
}
std::shared_ptr<DeviceErrorMonitor> DeviceErrorMonitor::instance()
{
static std::weak_ptr<DeviceErrorMonitor> s_clip;
if (s_clip.expired()) {
std::shared_ptr<DeviceErrorMonitor> ptr{new DeviceErrorMonitor};
s_clip = ptr;
return ptr;
}
return s_clip.lock();
}
void DeviceErrorMonitor::addMonitoringDevice(const QString &udi)
{
Solid::Device device(udi);
if (!device.isValid()) {
return;
}
Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
if (access) {
connect(access, &Solid::StorageAccess::teardownDone, this, [this](Solid::ErrorType error, const QVariant &errorData, const QString &udi) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Error Monitor: "
<< "Teardown signal arrived for device " << udi;
onSolidReply(SolidReplyType::Teardown, error, errorData, udi);
});
connect(access, &Solid::StorageAccess::setupDone, this, [this](Solid::ErrorType error, const QVariant &errorData, const QString &udi) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Error Monitor: "
<< "Setup signal arrived for device " << udi;
onSolidReply(SolidReplyType::Setup, error, errorData, udi);
});
}
if (device.is<Solid::OpticalDisc>()) {
Solid::OpticalDrive *drive = device.parent().as<Solid::OpticalDrive>();
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Error Monitor: "
<< "Eject signal arrived for device " << udi;
connect(drive, &Solid::OpticalDrive::ejectDone, this, [this](Solid::ErrorType error, const QVariant &errorData, const QString &udi) {
onSolidReply(SolidReplyType::Eject, error, errorData, udi);
});
}
}
void DeviceErrorMonitor::removeMonitoringDevice(const QString &udi)
{
Solid::Device device(udi);
if (device.is<Solid::StorageVolume>()) {
Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
if (access) {
access->disconnect(this);
}
}
if (auto it = m_deviceErrors.constFind(udi); it != m_deviceErrors.cend()) {
m_deviceErrors.erase(it);
}
}
Solid::ErrorType DeviceErrorMonitor::getError(const QString &udi)
{
if (auto it = m_deviceErrors.constFind(udi); it != m_deviceErrors.cend()) {
return it->first;
}
return Solid::ErrorType::NoError;
}
QString DeviceErrorMonitor::getErrorMassage(const QString &udi)
{
if (auto it = m_deviceErrors.constFind(udi); it != m_deviceErrors.cend()) {
return it->second;
}
return {};
}
bool DeviceErrorMonitor::isSafelyRemovable(const QString &udi) const
{
Solid::Device device(udi);
if (device.is<Solid::StorageVolume>()) {
auto drive = getAncestorAs<Solid::StorageDrive>(device);
if (!drive /* Already removed from elsewhere */ || !drive->isValid()) {
return true;
}
return !drive->isInUse() && (drive->isHotpluggable() || drive->isRemovable());
}
auto access = device.as<Solid::StorageAccess>();
if (access) {
return !access->isAccessible();
}
// If this check fails, the device has been already physically
// ejected, so no need to say that it is safe to remove it
return false;
}
void DeviceErrorMonitor::queryBlockingApps(const QString &devicePath)
{
QProcess *p = new QProcess;
connect(p, &QProcess::errorOccurred, [=, this](QProcess::ProcessError) {
Q_EMIT blockingAppsReady({});
p->deleteLater();
});
connect(p, &QProcess::finished, [p, this](int, QProcess::ExitStatus) {
QStringList blockApps;
const QString out = QString::fromLatin1(p->readAll());
const auto pidList = QStringView(out).split(QRegularExpression(QStringLiteral("\\s+")), Qt::SkipEmptyParts);
KSysGuard::Processes procs;
for (const QStringView pidStr : pidList) {
int pid = pidStr.toInt();
if (!pid) {
continue;
}
procs.updateOrAddProcess(pid);
KSysGuard::Process *proc = procs.getProcess(pid);
if (!blockApps.contains(proc->name())) {
blockApps << proc->name();
}
}
Q_EMIT blockingAppsReady(blockApps);
p->deleteLater();
});
p->start(QStringLiteral("lsof"), {QStringLiteral("-t"), devicePath});
// p.start(QStringLiteral("fuser"), {QStringLiteral("-m"), devicePath});
}
void DeviceErrorMonitor::onSolidReply(SolidReplyType type, Solid::ErrorType error, const QVariant &errorData, const QString &udi)
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Error Monitor: "
<< "Reply arrived for device " << udi << " arrived";
if (error == Solid::ErrorType::NoError && type == SolidReplyType::Setup) {
notify(Solid::ErrorType::NoError, QString(), QString(), udi);
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Error Monitor: "
<< "No error for device " << udi;
return;
}
QString errorMsg;
switch (error) {
case Solid::ErrorType::NoError:
if (type != SolidReplyType::Setup && isSafelyRemovable(udi)) {
KNotification::event(QStringLiteral("safelyRemovable"),
i18n("Device Status"),
i18n("A device can now be safely removed"),
u"device-notifier"_s,
KNotification::CloseOnTimeout,
u"devicenotifications"_s);
errorMsg = i18n("This device can now be safely removed.");
}
break;
case Solid::ErrorType::UnauthorizedOperation:
switch (type) {
case SolidReplyType::Setup:
errorMsg = i18n("You are not authorized to mount this device.");
break;
case SolidReplyType::Teardown:
errorMsg = i18nc("Remove is less technical for unmount", "You are not authorized to remove this device.");
break;
case SolidReplyType::Eject:
errorMsg = i18n("You are not authorized to eject this disc.");
break;
}
break;
case Solid::ErrorType::DeviceBusy: {
if (type == SolidReplyType::Setup) { // can this even happen?
errorMsg = i18n("Could not mount this device as it is busy.");
} else {
QString deviceUdi = udi;
if (type == SolidReplyType::Eject) {
const auto discs = Solid::Device::listFromType(Solid::DeviceInterface::OpticalDisc);
for (const auto &disc : discs) {
if (disc.parentUdi() == udi) {
deviceUdi = disc.udi();
break;
}
}
if (deviceUdi.isNull()) {
Q_ASSERT_X(false, Q_FUNC_INFO, "This should not happen, bail out");
}
}
Solid::Device device(deviceUdi);
Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
// Without that, our lambda function would capture an uninitialized object, resulting in UB
// and random crashes
QMetaObject::Connection *c = new QMetaObject::Connection();
*c = connect(this, &DeviceErrorMonitor::blockingAppsReady, [c, error, errorData, deviceUdi, this](const QStringList &blockApps) {
QString errorMessage;
if (blockApps.isEmpty()) {
errorMessage = i18n("One or more files on this device are open within an application.");
} else {
errorMessage = i18np("One or more files on this device are opened in application \"%2\".",
"One or more files on this device are opened in following applications: %2.",
blockApps.size(),
blockApps.join(i18nc("separator in list of apps blocking device unmount", ", ")));
}
notify(error, errorMessage, errorData.toString(), deviceUdi);
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Error Monitor: " << "Error for device " << deviceUdi << " error: " << error
<< " error message:" << errorMessage;
disconnect(*c);
delete c;
});
queryBlockingApps(access->filePath());
return;
}
break;
}
case Solid::ErrorType::UserCanceled:
// don't point out the obvious to the user, do nothing here
break;
default:
switch (type) {
case SolidReplyType::Setup:
errorMsg = i18n("Could not mount this device.");
break;
case SolidReplyType::Teardown:
errorMsg = i18nc("Remove is less technical for unmount", "Could not remove this device.");
break;
case SolidReplyType::Eject:
errorMsg = i18n("Could not eject this disc.");
break;
}
break;
}
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Error Monitor: "
<< "Error for device " << udi << " error: " << error << " error message:" << errorMsg;
notify(error, errorMsg, errorData.toString(), udi);
}
void DeviceErrorMonitor::notify(Solid::ErrorType error, const QString &errorMessage, const QString &description, const QString &udi)
{
Q_UNUSED(description)
if (!errorMessage.isEmpty()) {
m_deviceErrors[udi].first = error;
m_deviceErrors[udi].second = errorMessage;
} else {
if (auto it = m_deviceErrors.constFind(udi); it != m_deviceErrors.cend()) {
m_deviceErrors.erase(it);
}
}
Q_EMIT errorDataChanged(udi);
}
#include "moc_deviceerrormonitor_p.cpp"
|