From 36ffd509deb4347f76217b44908b62f5d7aa7bb8 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 19 Aug 2021 08:58:47 -0700 Subject: [PATCH] net/dns: avoid Linux PolicyKit GUI dialog during tests Fixes #2672 Signed-off-by: Brad Fitzpatrick --- net/dns/direct.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/net/dns/direct.go b/net/dns/direct.go index 0fd0e0ee0..879c62bbf 100644 --- a/net/dns/direct.go +++ b/net/dns/direct.go @@ -259,7 +259,7 @@ func (m directManager) SetDNS(config OSConfig) error { // try to manage DNS through resolved when it's around, but as a // best-effort fallback if we messed up the detection, try to // restart resolved to make the system configuration consistent. - if isResolvedRunning() { + if isResolvedRunning() && !runningAsGUIDesktopUser() { exec.Command("systemctl", "restart", "systemd-resolved.service").Run() } @@ -319,7 +319,7 @@ func (m directManager) Close() error { return err } - if isResolvedRunning() { + if isResolvedRunning() && !runningAsGUIDesktopUser() { exec.Command("systemctl", "restart", "systemd-resolved.service").Run() // Best-effort. } @@ -385,3 +385,12 @@ func (fs directFS) ReadFile(name string) ([]byte, error) { func (fs directFS) WriteFile(name string, contents []byte, perm os.FileMode) error { return ioutil.WriteFile(fs.path(name), contents, perm) } + +// runningAsGUIDesktopUser reports whether it seems that this code is +// being run as a regular user on a Linux desktop. This is a quick +// hack to fix Issue 2672 where PolicyKit pops up a GUI dialog asking +// to proceed we do a best effort attempt to restart +// systemd-resolved.service. There's surely a better way. +func runningAsGUIDesktopUser() bool { + return os.Getuid() != 0 && os.Getenv("DISPLAY") != "" +}