tmds / Tmds.DBus

D-Bus for .NET
MIT License
282 stars 54 forks source link

How to get process id of a calling process #287

Closed IldarAbdullin-okta closed 5 months ago

IldarAbdullin-okta commented 5 months ago

Hi,

First of all, thanks a lot for the excellent library. I didn't have any problems to build dbus service and register it with systemd.

In my POC I would like to dig dipper and get some information about the calling process. My intention is to get PID of the calling process every time server methods are called, find process in the system and check the signature. I know there is a DBus API GetConnectionUnixProcessID that can be used for that purpose but not sure how and when to call it.

Any hints or example will be highly appreciated!

tmds commented 5 months ago

every time server methods are called

This is only possible with the Tmds.DBus.Protocol library.

You can get the Sender from the Message on the MethodContext in your IMethodHandler. Or alternatively, you can use a Connection.AddMatchAsync.

I know there is a DBus API GetConnectionUnixProcessID that can be used for that purpose but not sure how and when to call it.

Calling this is no different from calling any other method. You need a proxy that calls the org.freedesktop.DBus service's org.freedesktop.DBus interface GetConnectionUnixProcessID method at path /org/freedesktop/DBus. You can generate the proxy using a code generator.

You pass this the Sender from the Message, and the D-Bus daemon will give you back its pid.

IldarAbdullin-okta commented 5 months ago

Thanks for the quick response!

So basically I can still register and start the service via DBus library and do message handling via Protocol library. That makes sense!