Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbus: add support for querying unit by PID #385

Merged
merged 2 commits into from
Dec 13, 2021

Conversation

dylanratcliffe
Copy link
Contributor

This exposes the functionality of the GetUnitByPID method and could be useful in situations where you're trying to find which unit owns a given process.

I chose to convert from dbus.ObjectPath to a string (the name of the unit) since almost nothing in this library accepts an dbus.ObjectPath as an input, and the methods for converting back and forth are private and not available to users. The current output format could then be passed into ListUnitsByNames in order to get further information.

Allows users to get the unit name by PID. Similar to `systemctl status {pid}`
dbus/methods.go Outdated

// GetUnitByPIDContext returns the unit name for a given PID. The PID must refer
// to an existing system process
func (c *Conn) GetUnitByPIDContext(ctx context.Context, pid uint32) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can drop the Context suffix from here (it's an historical artifact added for method that started their existence before context was a thing).

I agree getting the unit name is probably the most common case, but the object path could be useful too in some case.
I'd suggest having a plain GetUnitByPID that directly returns the object path, and an additional GetUnitNameByPID which internally does some translation. No need for a separate getUnitInternal helper, that logic can be folded directly into the latter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me, should be addressed

@lucab
Copy link
Contributor

lucab commented Dec 13, 2021

Welcome and thanks for the PR! Overall it looks nice, I've left some minor suggestions in review.

PID-based queries can now return either a name or a path
@lucab lucab changed the title Added ability to query based on PID dbus: add support for querying unit by PID Dec 13, 2021
Copy link
Contributor

@lucab lucab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, and thanks for adding test coverage too!

@lucab lucab merged commit f5a75de into coreos:main Dec 13, 2021
@dylanratcliffe dylanratcliffe deleted the get-unit-pid branch December 13, 2021 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants