feat: add app-identifier for convenient
Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
parent
9d2cee79fe
commit
16d8e21598
@ -1,3 +1,4 @@
|
|||||||
add_subdirectory(dde-application-manager)
|
add_subdirectory(dde-application-manager)
|
||||||
add_subdirectory(app-launch-helper)
|
add_subdirectory(app-launch-helper)
|
||||||
add_subdirectory(app-update-notifier)
|
add_subdirectory(app-update-notifier)
|
||||||
|
add_subdirectory(app-identifier)
|
||||||
|
1
apps/app-identifier/CMakeLists.txt
Normal file
1
apps/app-identifier/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
add_subdirectory(src)
|
15
apps/app-identifier/src/CMakeLists.txt
Normal file
15
apps/app-identifier/src/CMakeLists.txt
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
set(BIN_NAME app-identifier)
|
||||||
|
|
||||||
|
add_executable(${BIN_NAME} main.cpp)
|
||||||
|
|
||||||
|
target_link_libraries(${BIN_NAME} PRIVATE
|
||||||
|
Qt${QT_VERSION_MAJOR}::Core
|
||||||
|
Qt${QT_VERSION_MAJOR}::DBus
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(${BIN_NAME} PRIVATE
|
||||||
|
${PROJECT_SOURCE_DIR}/src
|
||||||
|
${PROJECT_BINARY_DIR}/src
|
||||||
|
)
|
||||||
|
|
||||||
|
install(TARGETS ${BIN_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
|
69
apps/app-identifier/src/main.cpp
Normal file
69
apps/app-identifier/src/main.cpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QCommandLineOption>
|
||||||
|
#include <QCommandLineParser>
|
||||||
|
#include <QDBusMessage>
|
||||||
|
|
||||||
|
#include "global.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
QCoreApplication app{argc, argv};
|
||||||
|
QCoreApplication::setApplicationName("app-identifier");
|
||||||
|
QCoreApplication::setApplicationVersion("1.0");
|
||||||
|
|
||||||
|
QCommandLineParser parser;
|
||||||
|
parser.setApplicationDescription("Identify in what capacity the process is running.");
|
||||||
|
parser.addHelpOption();
|
||||||
|
parser.addVersionOption();
|
||||||
|
parser.addPositionalArgument("PIDs", "PIDs to identify.", "[pid1 pid2 pid3 ...]");
|
||||||
|
|
||||||
|
parser.process(app);
|
||||||
|
auto PIDs = parser.positionalArguments();
|
||||||
|
if (PIDs.isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto slice = PIDs.first().split(' ', Qt::SkipEmptyParts);
|
||||||
|
QList<pid_t> PIDList{};
|
||||||
|
std::transform(slice.cbegin(), slice.cend(), std::back_inserter(PIDList), [](const QString &pid) {
|
||||||
|
bool ok{true};
|
||||||
|
auto result = pid.toInt(&ok);
|
||||||
|
if (!ok) {
|
||||||
|
qFatal() << "failed to convert:" << pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
return static_cast<pid_t>(result);
|
||||||
|
});
|
||||||
|
|
||||||
|
auto con = QDBusConnection::sessionBus();
|
||||||
|
std::for_each(PIDList.cbegin(), PIDList.cend(), [&con](pid_t pid) {
|
||||||
|
auto pidfd = pidfd_open(pid, 0);
|
||||||
|
if (pidfd == -1) {
|
||||||
|
qFatal() << "failed to open pidfd:" << std::strerror(errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto msg = QDBusMessage::createMethodCall(
|
||||||
|
DDEApplicationManager1ServiceName, DDEApplicationManager1ObjectPath, ApplicationManager1Interface, "Identify");
|
||||||
|
msg.setArguments({QVariant::fromValue(QDBusUnixFileDescriptor{pidfd})});
|
||||||
|
|
||||||
|
auto reply = con.call(msg);
|
||||||
|
if (reply.type() != QDBusMessage::ReplyMessage) {
|
||||||
|
qWarning() << "failed to Identify process" << pid << reply.errorMessage();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto appID = reply.arguments().first().toString();
|
||||||
|
if (!appID.isEmpty()) {
|
||||||
|
qInfo() << "The capacity of process" << pid << "is:" << appID;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
qWarning() << "failed to get appID of process" << pid;
|
||||||
|
});
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -23,6 +23,7 @@
|
|||||||
#include <QUuid>
|
#include <QUuid>
|
||||||
#include <QLoggingCategory>
|
#include <QLoggingCategory>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sys/syscall.h>
|
||||||
#include "constant.h"
|
#include "constant.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
@ -652,4 +653,9 @@ inline QString getObjectPathFromAppId(const QString &appId)
|
|||||||
return QString{DDEApplicationManager1ObjectPath} + "/" + QUuid::createUuid().toString(QUuid::Id128);
|
return QString{DDEApplicationManager1ObjectPath} + "/" + QUuid::createUuid().toString(QUuid::Id128);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline int pidfd_open(pid_t pid, uint flags)
|
||||||
|
{
|
||||||
|
return syscall(SYS_pidfd_open, pid, flags);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -2,26 +2,20 @@
|
|||||||
//
|
//
|
||||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
#include "dbus/applicationmanager1service.h"
|
|
||||||
#include "dbus/applicationservice.h"
|
|
||||||
#include "cgroupsidentifier.h"
|
|
||||||
#include "constant.h"
|
|
||||||
#include "dbus/instanceadaptor.h"
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <QDBusConnection>
|
#include <QDBusConnection>
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <chrono>
|
|
||||||
#include <QDBusUnixFileDescriptor>
|
#include <QDBusUnixFileDescriptor>
|
||||||
|
|
||||||
namespace {
|
#include "dbus/applicationmanager1service.h"
|
||||||
int pidfd_open(pid_t pid, uint flags)
|
#include "dbus/applicationservice.h"
|
||||||
{
|
#include "cgroupsidentifier.h"
|
||||||
return syscall(SYS_pidfd_open, pid, flags);
|
#include "constant.h"
|
||||||
}
|
#include "dbus/instanceadaptor.h"
|
||||||
} // namespace
|
#include "global.h"
|
||||||
|
|
||||||
class TestApplicationManager : public testing::Test
|
class TestApplicationManager : public testing::Test
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user