Add helpers to read the executable path for a given PID

This commit is contained in:
Tobias C. Berner 2021-12-17 06:54:53 +00:00 committed by Vlad Zahorodnii
parent 2da3276a95
commit 1bc7afe8aa
5 changed files with 59 additions and 1 deletions

View file

@ -285,6 +285,16 @@ ecm_add_qtwayland_server_protocol_kde(SERVER_LIB_SRCS
)
add_library(KWaylandServer ${SERVER_LIB_SRCS})
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
target_sources(KWaylandServer PRIVATE utils/executable_path_proc.cpp)
elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
target_sources(KWaylandServer PRIVATE utils/executable_path_sysctl.cpp)
else()
message(FATAL_ERROR "Unsupported platform ${CMAKE_SYSTEM_NAME}")
endif()
add_library(Plasma::KWaylandServer ALIAS KWaylandServer)
ecm_generate_export_header(KWaylandServer
BASE_NAME

View file

@ -5,6 +5,7 @@
*/
#include "clientconnection.h"
#include "display.h"
#include "utils/executable_path.h"
// Qt
#include <QFileInfo>
#include <QVector>
@ -44,7 +45,7 @@ ClientConnectionPrivate::ClientConnectionPrivate(wl_client *c, Display *display,
listener.notify = destroyListenerCallback;
wl_client_add_destroy_listener(c, &listener);
wl_client_get_credentials(client, &pid, &user, &group);
executablePath = QFileInfo(QStringLiteral("/proc/%1/exe").arg(pid)).symLinkTarget();
executablePath = executablePathFromPid(pid);
}
ClientConnectionPrivate::~ClientConnectionPrivate()

View file

@ -0,0 +1,11 @@
/*
SPDX-FileCopyrightText: 2021 Tobias C. Berner <tcberner@FreeBSD.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#pragma once
#include <QString>
QString executablePathFromPid(pid_t);

View file

@ -0,0 +1,14 @@
/*
SPDX-FileCopyrightText: 2021 Tobias C. Berner <tcberner@FreeBSD.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "executable_path.h"
#include <QFileInfo>
QString executablePathFromPid(pid_t pid)
{
return QFileInfo(QStringLiteral("/proc/%1/exe").arg(pid)).symLinkTarget();
}

View file

@ -0,0 +1,22 @@
/*
SPDX-FileCopyrightText: 2021 Tobias C. Berner <tcberner@FreeBSD.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include "executable_path.h"
QString executablePathFromPid(pid_t pid)
{
const int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, static_cast<int>(pid)};
char buf[MAXPATHLEN];
size_t cb = sizeof(buf);
if (sysctl(mib, 4, buf, &cb, nullptr, 0) == 0) {
return QString::fromLocal8Bit(realpath(buf, nullptr));
}
return QString();
}