Debian Codegraph @

src/dsgapplication.cpp

raw
1 // SPDX-FileCopyrightText: 2022-2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dsgapplication.h"
6
7 #include <sys/syscall.h>
8 #include <unistd.h>
9
10 #include <QDir>
11 #include <QFile>
12 #include <QByteArray>
13 #include <QCoreApplication>
14 #include <QDebug>
15 #include <QDBusUnixFileDescriptor>
16 #include <QDBusReply>
17 #include <QRegularExpression>
18 #include <QLoggingCategory>
19 #include <QDBusConnectionInterface>
20
21 #include <DDBusInterface>
22
23 #ifdef QT_DEBUG
24 Q_LOGGING_CATEGORY(dsgApp, "dtk.core.dsg")
25 #else
26 Q_LOGGING_CATEGORY(dsgApp, "dtk.core.dsg", QtInfoMsg)
27 #endif
28
29 DCORE_BEGIN_NAMESPACE
30
31 static inline QByteArray getSelfAppId() {
32 // The env is set by the application starter(eg, org.desktopspec.ApplicationManager service)
33 QByteArray selfId = qgetenv("DSG_APP_ID");
34 if (!selfId.isEmpty())
35 return selfId;
36 return DSGApplication::getId(QCoreApplication::applicationPid());
37 }
38
39 static bool isServiceActivatable(const QString &service)
40 {
41 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(service))
42 return false;
43
44 const QDBusReply<QStringList> activatableNames = QDBusConnection::sessionBus().interface()->
45 callWithArgumentList(QDBus::AutoDetect,
46 QLatin1String("ListActivatableNames"),
47 QList<QVariant>());
48
49 return activatableNames.value().contains(service);
50 }
51
52 // Format appId to valid.
53 static QByteArray formatAppId(const QByteArray &appId)
54 {
55 static const QRegularExpression regex("[^\\w\\-\\.]");
56 QString format(appId);
57 format.replace(QDir::separator(), ".");
58 format = format.replace(regex, QStringLiteral("-"));
59 const QString InvalidPrefix{"."};
60 if (format.startsWith(InvalidPrefix))
61 format = format.mid(InvalidPrefix.size());
62 return format.toLocal8Bit();
63 }
64
65 QByteArray DSGApplication::id()
66 {
67 static QByteArray selfId = getSelfAppId();
68 if (!selfId.isEmpty())
69 return selfId;
70 QByteArray result = selfId;
71 if (!qEnvironmentVariableIsSet("DTK_DISABLED_FALLBACK_APPID")) {
72 result = QCoreApplication::applicationName().toLocal8Bit();
73 if (result.isEmpty()) {
74 QFile file("/proc/self/cmdline");
75 if (file.open(QIODevice::ReadOnly))
76 result = file.readLine();
77 }
78 if (result.isEmpty()) {
79 const QFileInfo file(QFile::symLinkTarget("/proc/self/exe"));
80 if (file.exists())
81 result = file.absoluteFilePath().toLocal8Bit();
82 }
83 if (!result.isEmpty()) {
84 result = formatAppId(result);
85 qCDebug(dsgApp) << "The applicatiion ID is fallback to " << result;
86 }
87 }
88 if (result.isEmpty())
89 qCWarning(dsgApp) << "The application ID is empty.";
90
91 return result;
92 }
93
94 QByteArray DSGApplication::getId(qint64 pid)
95 {
96 if (!isServiceActivatable("org.desktopspec.ApplicationManager1")) {
97 qCInfo(dsgApp) << "Can't getId from AM for the " << pid << ", because AM is unavailable.";
98 return QByteArray();
99 }
100
101 int pidfd = syscall(SYS_pidfd_open, pid, 0);
102 if (pidfd < 0) {
103 qCWarning(dsgApp) << "pidfd open failed:" << strerror(errno) << ", the pid:" << pid;
104 return QByteArray();
105 }
106
107 DDBusInterface infc("org.desktopspec.ApplicationManager1",
108 "/org/desktopspec/ApplicationManager1",
109 "org.desktopspec.ApplicationManager1");
110
111 QDBusReply<QString> reply = infc.call("Identify", QVariant::fromValue(QDBusUnixFileDescriptor(pidfd)));
112 // see QDBusUnixFileDescriptor: The original file descriptor is not touched and must be closed by the user.
113 close(pidfd);
114
115 if (!reply.isValid()) {
116 qCWarning(dsgApp) << "Identify from AM failed." << reply.error().message();
117 return QByteArray();
118 }
119
120 const QByteArray appId = reply.value().toLatin1();
121 qCInfo(dsgApp) << "AppId is fetched from AM, and value is " << appId;
122 return appId;
123 }
124
125 DCORE_END_NAMESPACE