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

Set XAUTHORITY for desktop process when possible #1369

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions ee/desktop/runner/runner_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package runner
import (
"context"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -62,15 +64,15 @@ func (r *DesktopUsersProcessesRunner) runAsUser(ctx context.Context, uid string,
}

// Set any necessary environment variables on the command (like DISPLAY)
envVars := r.userEnvVars(ctx, uid)
envVars := r.userEnvVars(ctx, uid, runningUser.Username)
for k, v := range envVars {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}

return cmd.Start()
}

func (r *DesktopUsersProcessesRunner) userEnvVars(ctx context.Context, uid string) map[string]string {
func (r *DesktopUsersProcessesRunner) userEnvVars(ctx context.Context, uid string, username string) map[string]string {
envVars := make(map[string]string)

uidInt, err := strconv.ParseInt(uid, 10, 32)
Expand Down Expand Up @@ -133,6 +135,11 @@ func (r *DesktopUsersProcessesRunner) userEnvVars(ctx context.Context, uid strin
// /usr/share/applications/mimeinfo.cache does not contain any applications installed via snap.
envVars["XDG_DATA_DIRS"] = "/usr/local/share/:/usr/share/:/var/lib/snapd/desktop"

// We need xauthority set in order to launch the browser on Ubuntu 23.04
if xauthorityLocation := r.getXauthority(ctx, uid, username); xauthorityLocation != "" {
envVars["XAUTHORITY"] = xauthorityLocation
}

return envVars
}

Expand Down Expand Up @@ -214,3 +221,33 @@ func (r *DesktopUsersProcessesRunner) displayFromXwayland(ctx context.Context, u

return defaultDisplay
}

// getXauthority checks known locations for the xauthority file
func (r *DesktopUsersProcessesRunner) getXauthority(ctx context.Context, uid string, username string) string {
xdgRuntimeDir := filepath.Join("run", "user", uid)

// Glob for Wayland matches first
waylandXAuthorityLocationPattern := filepath.Join(xdgRuntimeDir, ".mutter-Xwaylandauth.*")
if matches, err := filepath.Glob(waylandXAuthorityLocationPattern); err == nil && len(matches) > 0 {
return matches[0]
}

// Next, check default X11 location
x11XauthorityLocation := filepath.Join(xdgRuntimeDir, "gdm", "Xauthority")
if _, err := os.Stat(x11XauthorityLocation); err == nil {
return x11XauthorityLocation
}

// Default location is $HOME/.Xauthority -- try that before giving up
homeLocation := filepath.Join("/home", username, ".Xauthority")
if _, err := os.Stat(homeLocation); err == nil {
return homeLocation
}

level.Debug(r.logger).Log("msg", "could not find xauthority in any known location",
"wayland", waylandXAuthorityLocationPattern,
"x11", x11XauthorityLocation,
"default", homeLocation)

return ""
}