Get the sender PID from gDBus

Amira S amira.solo at gmail.com
Sun May 22 08:40:35 UTC 2022


Hi everyone,

I need to get the client/sender PID using my gDBus service for a SYSTEM
message bus on Linux.

I create the message bus by calling g_bus_own_name with
G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT, and then I see that the Acquire
handlers are called with a GDBusConnection.

According to the documentation, it is possible to obtain a GCredentials
from the connection by calling g_dbus_connection_get_peer_credentials
<https://www.freedesktop.org/software/gstreamer-sdk/data/docs/latest/gio/GDBusConnection.html#g-dbus-connection-get-peer-credentials>,
(and from there I can get a unix ucred that has the pid), but I always get
a NULL credentials.
The documentation for this function says:
"Gets the credentials of the authenticated peer. This will always return
NULL
<https://www.freedesktop.org/software/gstreamer-sdk/data/docs/latest/glib/glib/glib-Standard-Macros.html#NULL:CAPS>
 unless *connection* acted as a server (e.g.
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
<https://www.freedesktop.org/software/gstreamer-sdk/data/docs/latest/gio/GDBusConnection.html#G-DBUS-CONNECTION-FLAGS-AUTHENTICATION-SERVER:CAPS>
was
passed) when set up and the client passed credentials as part of the
authentication process."

How can I set the connection type, if I use the generated code dbus created?

I also tried getting the PID by calling programmatically the following
command:

dbus-send --system --print-reply --dest=org.freedesktop.DBus
 /org/freedesktop/DBus org.freedesktop.DBus.GetConnectionUnixProcessID
‘string:com.microsoft.mscrypt’

But I always receive the server's PID, and not the client's.

Any idea if/how it is possible to get the peer's PID connecting to a SYSTEM
bus?

Thanks,
Amira.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/dbus/attachments/20220522/d5be0349/attachment.htm>


More information about the dbus mailing list