diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 965b537..07bcfea 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(dde-application-manager) add_subdirectory(app-launch-helper) add_subdirectory(app-update-notifier) +add_subdirectory(app-identifier) diff --git a/apps/app-identifier/CMakeLists.txt b/apps/app-identifier/CMakeLists.txt new file mode 100644 index 0000000..febd4f0 --- /dev/null +++ b/apps/app-identifier/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(src) diff --git a/apps/app-identifier/src/CMakeLists.txt b/apps/app-identifier/src/CMakeLists.txt new file mode 100644 index 0000000..7843136 --- /dev/null +++ b/apps/app-identifier/src/CMakeLists.txt @@ -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}) diff --git a/apps/app-identifier/src/main.cpp b/apps/app-identifier/src/main.cpp new file mode 100644 index 0000000..ef82fcb --- /dev/null +++ b/apps/app-identifier/src/main.cpp @@ -0,0 +1,69 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: LGPL-3.0-or-later + +#include +#include +#include +#include + +#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 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(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; +} diff --git a/src/global.h b/src/global.h index e4a8c64..0937041 100644 --- a/src/global.h +++ b/src/global.h @@ -23,6 +23,7 @@ #include #include #include +#include #include "constant.h" #include "config.h" @@ -652,4 +653,9 @@ inline QString getObjectPathFromAppId(const QString &appId) 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 diff --git a/tests/ut_applicationmanager.cpp b/tests/ut_applicationmanager.cpp index 3dfacb6..e2c69c4 100644 --- a/tests/ut_applicationmanager.cpp +++ b/tests/ut_applicationmanager.cpp @@ -2,26 +2,20 @@ // // 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 #include #include #include #include #include -#include #include -namespace { -int pidfd_open(pid_t pid, uint flags) -{ - return syscall(SYS_pidfd_open, pid, flags); -} -} // namespace +#include "dbus/applicationmanager1service.h" +#include "dbus/applicationservice.h" +#include "cgroupsidentifier.h" +#include "constant.h" +#include "dbus/instanceadaptor.h" +#include "global.h" class TestApplicationManager : public testing::Test {