【转载】D-Bus系列之获取发送者UID及PID的方法
转载自:https://qytz-notes.readthedocs.io/tech/dbus/dbus-get-uid.html
D-Bus系列之获取发送者UID及PID的方法
获取PID及UID的原理
org.freedesktop.DBus提供了一系列的消息,其中就有根据服务名获取进程PID及UID的接口
"org.freedesktop.DBus", --服务
"/org/freedesktop/DBus", --对象
"org.freedesktop.DBus", --接口
"GetConnectionUnixProcessID", --方法
UINT32 GetConnectionUnixProcessID (in STRING bus_name);
"GetConnectionUnixUser", --方法
UINT32 GetConnectionUnixUser (in STRING bus_name);
QT DBUS获取的方法及示例
QT DBUS提供了相关调用的封装:
- const QDBusMessage & QDBusContext::message () const Returns the message that generated this call.
- QString QDBusMessage::service () const Returns the name of the service or the bus address of the remote method call.
- QDBusReply QDBusConnectionInterface::servicePid ( const QString & serviceName ) const Returns the Unix Process ID (PID) for the process currently holding the bus service serviceName.
例如:
bool SomeMethod( const QString &name )
{
qDebug() << "PID is: " << connection().interface()->servicePid( message().service() );
}
D-Bus glib绑定及GDBus
D-Bus Glib 的绑定提供了获取发送者名字的方法:
const char *dbus_message_get_sender(DBusMessage *message);
但是没有提供获取进程PID及UID的方法,需自己编写代码调用GetConnectionUnixProcessID 和GetConnectionUnixUser方法。 好像GDbus也没有提供,只找到了g_dbus_message_get_sender()方法。
/* proxy for getting PID info */
g_dbus_proxy_new_for_bus(G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
NULL,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
NULL,
(GAsyncReadyCallback)dbus_proxy_connect_cb,
NULL);
void
dbus_proxy_connect_cb(GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GError *error = NULL;
dbus_proxy = g_dbus_proxy_new_finish (res, &error);
if (error) {
g_warning("dbus_proxy_connect_cb failed: %s", error->message);
g_error_free(error);
dbus_proxy = NULL;
}
else {
g_debug("dbus_proxy_connect_cb succeeded");
}
}
gboolean
handle_request_sys_state (PowerdSource *obj, GDBusMethodInvocation *invocation, int state)
{
// get the name of the dbus object that called us
owner = g_dbus_method_invocation_get_sender(invocation);
if (dbus_proxy) {
result = g_dbus_proxy_call_sync(dbus_proxy,
"GetConnectionUnixProcessID",
g_variant_new("(s)", owner),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (error) {
g_error("Unable to get PID for %s: %s", owner, error->message);
g_error_free(error);
error = NULL;
}
else {
g_variant_get(result, "(u)", &owner_pid);
g_info("request is from pid %d\n", owner_pid);
}
}
...
}
参考资料
- D-Bus Specification
- Getting the PID and Process Name From a dbus Caller in C
- Get sender PID from DBUS
- Qt doc及DBus-Glib、GDBus文档
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!