Skip to content

Commit

Permalink
fix: Fix can not get any logs issue (#361)
Browse files Browse the repository at this point in the history
The service did not read logs by gio, the gio-qt have include qt6 and qt5 libraries since v0.0.14, rollback codes.

Log: Fix can not get any logs issue.
  • Loading branch information
re2zero authored Feb 8, 2025
1 parent 98815d8 commit 43f350d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 108 deletions.
5 changes: 2 additions & 3 deletions logViewerService/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ find_package(Qt${QT_DESIRED_VERSION} REQUIRED COMPONENTS ${qt_required_component
find_package(Dtk${DTK_VERSION_MAJOR} COMPONENTS Core Gui REQUIRED)

find_package(PolkitQt${QT_DESIRED_VERSION}-1)
if (QT_DESIRED_VERSION MATCHES 5)
pkg_check_modules(Gio REQUIRED gio-qt)
endif()
pkg_check_modules(Gio REQUIRED gio-qt${DTK_VERSION_MAJOR})


set(LINK_LIBS
Qt${QT_DESIRED_VERSION}::Core
Expand Down
70 changes: 9 additions & 61 deletions logViewerService/logviewerservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#include <unistd.h>
#include <fstream>

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <polkit-qt5-1/PolkitQt1/Authority>
#include <dgiofile.h>
#include <dgiovolume.h>
#include <dgiovolumemanager.h>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <polkit-qt5-1/PolkitQt1/Authority>
#else
#include <polkit-qt6-1/PolkitQt1/Authority>
#endif
Expand Down Expand Up @@ -652,17 +652,9 @@ QStringList LogViewerService::getHomePaths()
QStringList LogViewerService::getExternalDevPaths()
{
QStringList devPaths;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const QList<QExplicitlySharedDataPointer<DGioMount> > mounts = getMounts_safe();
#else
const QList<DMount> mounts = getMounts_safe();
#endif
for (auto mount : mounts) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QString uri = mount->getRootFile()->uri();
#else
QString uri = mount.source();
#endif
QString scheme = QUrl(uri).scheme();

// sbm路径判断,分为gvfs挂载和cifs挂载两种
Expand All @@ -679,12 +671,8 @@ QStringList LogViewerService::getExternalDevPaths()
if ((scheme == "file") || //usb device
(scheme == "gphoto2") || //phone photo
(scheme == "mtp")) { //android file
// QExplicitlySharedDataPointer<DGioFile> locationFile = mount->getDefaultLocationFile();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QString path = mount->getDefaultLocationFile()->path();
#else
QString path = mount.target();
#endif
QExplicitlySharedDataPointer<DGioFile> locationFile = mount->getDefaultLocationFile();
QString path = locationFile->path();
if (path.startsWith("/media/")) {
QFlags <QFileDevice::Permission> power = QFile::permissions(path);
if (power.testFlag(QFile::WriteUser)) {
Expand All @@ -698,38 +686,11 @@ QStringList LogViewerService::getExternalDevPaths()
}

//可重入版本的getMounts
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QList<QExplicitlySharedDataPointer<DMount>> LogViewerService::getMounts_safe()
#else
QList<DMount> LogViewerService::getMounts_safe()
#endif
QList<QExplicitlySharedDataPointer<DGioMount> > LogViewerService::getMounts_safe()
{
static QMutex mutex;
mutex.lock();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
auto result = DGioVolumeManager::getMounts();
#else
QList<DMount> result;
QFile mountsFile("/proc/mounts");
if (mountsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&mountsFile);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList parts = line.split(' ');
if (parts.size() >= 4) {
Mount mount(new MountData);
mount.setSource(parts.at(0));
mount.setTarget(parts.at(1));
mount.setFilesystemType(parts.at(2));
mount.setOptions(parts.at(3));
result.append(mount);
}
}
mountsFile.close();
} else {
qWarning() << "Failed to open /proc/mounts";
}
#endif
mutex.unlock();
return result;
}
Expand Down Expand Up @@ -1095,23 +1056,10 @@ bool LogViewerService::isValidInvoker(bool checkAuth/* = true*/)
uint pid = conn.interface()->servicePid(msg.service()).value();

// 判断是否存在执行路径且是否存在于可调用者名单中
QFileInfo initNsMntFileInfo("/proc/1/ns/mnt");
QFileInfo senderNsMntFileInfo(QString("/proc/%1/ns/mnt").arg(pid));
QString initNsMnt;
QString senderNsMnt;

if (!initNsMntFileInfo.isSymLink() || !senderNsMntFileInfo.isSymLink()) {
sendErrorReply(QDBusError::ErrorType::Failed, "Invalid symlink!!!!!");
return false;
}

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
initNsMnt = initNsMntFileInfo.symLinkTarget().trimmed();
senderNsMnt = senderNsMntFileInfo.symLinkTarget().trimmed();
#else
initNsMnt = initNsMntFileInfo.readLink().trimmed();
senderNsMnt = senderNsMntFileInfo.readLink().trimmed();
#endif
QFile initNsMntFile("/proc/1/ns/mnt");
QFile senderNsMntFile(QString("/proc/%1/ns/mnt").arg(pid));
auto initNsMnt = initNsMntFile.symLinkTarget().trimmed().remove(0, QString("/proc/1/ns/mnt").length());
auto senderNsMnt = senderNsMntFile.symLinkTarget().trimmed().remove(0, QString("/proc/%1/ns/mnt").arg(pid).length());
if (initNsMnt != senderNsMnt) {
sendErrorReply(QDBusError::ErrorType::Failed, "Illegal calls!!!!!");
return false;
Expand Down
47 changes: 3 additions & 44 deletions logViewerService/logviewerservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,14 @@
#ifndef LOGVIEWERSERVICE_H
#define LOGVIEWERSERVICE_H

#include <dgiomount.h>

#include <QObject>
#include <QDBusContext>
#include <QScopedPointer>
#include <QProcess>
#include <QTemporaryDir>
#include <QDBusUnixFileDescriptor>
#include <QTextStream>

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <dgiomount.h>

using DMount = DGioMount;

#else
class MountData : public QSharedData {
public:
QString source;
QString target;
QString filesystemType;
QString options;
};

class Mount {
public:
Mount() : d(new MountData) {}
Mount(const Mount& other) : d(other.d) {}
Mount(MountData* data) : d(data) {}

QString source() const { return d->source; }
QString target() const { return d->target; }
QString filesystemType() const { return d->filesystemType; }
QString options() const { return d->options; }

void setSource(const QString& source) { d->source = source; }
void setTarget(const QString& target) { d->target = target; }
void setFilesystemType(const QString& filesystemType) { d->filesystemType = filesystemType; }
void setOptions(const QString& options) { d->options = options; }

private:
QExplicitlySharedDataPointer<MountData> d;
};

using DMount = Mount;

#endif

class QTextStream;
class DGioVolumeManager;
Expand Down Expand Up @@ -87,11 +50,7 @@ public Q_SLOTS:
QStringList getHomePaths();
// 获取外设挂载路径(包括smb路径)
QStringList getExternalDevPaths();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QList<QExplicitlySharedDataPointer<DMount>> getMounts_safe();
#else
QList<DMount> getMounts_safe();
#endif
QList<QExplicitlySharedDataPointer<DGioMount>> getMounts_safe();

private:
QString readLog(const QString &filePath);
Expand Down

0 comments on commit 43f350d

Please sign in to comment.