-
Notifications
You must be signed in to change notification settings - Fork 37
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
fix(yggctl): prefer system bus when run as root #143
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that refactoring is quite good. I would like to know why the environment variable DBUS_SESSION_BUS_ADDRESS
was not empty in your case. I think that it is not normal situation. I was not able to reproduce this. See my comments in #142 Thus I'm not sure if os.Geteuid() > 0
is necessary.
8a0a71e
to
9db8d2e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some request and suggestion.
cmd/yggctl/actions.go
Outdated
|
||
conn, err = connect() | ||
if err != nil { | ||
if os.Getenv("DBUS_SESSION_BUS_ADDRESS") != "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should be changed to:
if os.Getenv("DBUS_SESSION_BUS_ADDRESS") != "" && os.Geteuid() > 0 {
otherwise this function will return incorrect error messages for the case you are trying to solve.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I have better proposal to avoid checking conditions twice. What about this:
func connectBus() (*dbus.Conn, error) {
var connect func(...dbus.ConnOption) (*dbus.Conn, error)
var conn *dbus.Conn
var err error
var errMsg string
if os.Getenv("DBUS_SESSION_BUS_ADDRESS") != "" && os.Geteuid() > 0 {
connect = dbus.ConnectSessionBus
errMsg = "cannot connect to session bus (" + os.Getenv("DBUS_SESSION_BUS_ADDRESS") + "): %w"
} else {
connect = dbus.ConnectSystemBus
errMsg = "cannot connect to system bus: %w"
}
conn, err = connect()
if err != nil {
return nil, fmt.Errorf(errMsg, err)
}
return conn, nil
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Yea. This should work. And I completely forgot my Geteuid check when I rewrote the function. 🤦
yggctl will attempt to connect to the session bus only if DBUS_SESSION_BUS_ADDRESS is non-empty and the effective UID of the process is greater than zero. This results in root invocations of yggctl preferring the system bus over the session bus. Signed-off-by: Link Dupont <link@sub-pop.net>
9db8d2e
to
4e62969
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, Thanks for updates 👍
yggctl will attempt to connect to the session bus only if
DBUS_SESSION_BUS_ADDRESS is non-empty and the effective UID of the
process is greater than zero. This results in root invocations of yggctl
preferring the system bus over the session bus.
Fixes: #142