fleet/orbit/pkg/execuser/execuser_darwin.go
Lucas Manuel Rodriguez 0823cc7e76
Fix orbit active GUI session detection to start Fleet Desktop and key escrowing on Linux (#39777)
Resolves #36024 and #34501.

Main change is about stop using the first user in the `users` command
output [*] and instead use `loginctl` commands to pick the correct
current active GUI user.

[*] `users` was returning empty on some new distributions, and in
multi-sessions we were always picking the first one (even if it wasn't
active).

- [X] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.

## Testing

- [x] QA'd all new/changed functionality manually

## fleetd/orbit/Fleet Desktop

- [x] Verified compatibility with the latest released version of Fleet
(see [Must
rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md))
- [x] If the change applies to only one platform, confirmed that
`runtime.GOOS` is used as needed to isolate changes
- [x] Verified that fleetd runs on macOS, Linux and Windows
- [x] Verified auto-update works from the released version of component
to the new version (see [tools/tuf/test](../tools/tuf/test/README.md))


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

* **Bug Fixes**
* Fixed Fleet Desktop startup to correctly detect and use the active GUI
session on Linux systems.
* Improved GUI user detection for dialog prompts, ensuring system
dialogs run in the proper user context.

* **Improvements**
* Enhanced error reporting and logging clarity for GUI session detection
failures.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-16 11:41:16 -03:00

64 lines
1.7 KiB
Go

package execuser
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
)
// run uses macOS open command to start application as the current login user.
// Note that the child process spawns a new process in user space and thus it is not
// effective to add a context to this function to cancel the child process.
func run(path string, opts eopts) (lastLogs string, err error) {
info, err := os.Stat(path)
if err != nil {
return "", fmt.Errorf("stat path %q: %w", path, err)
}
if !info.IsDir() {
return "", fmt.Errorf("path is not an .app directory: %s", path)
}
var arg []string
if opts.stderrPath != "" {
arg = append(arg, "--stderr", opts.stderrPath)
}
// set environment variables
for _, nv := range opts.env {
arg = append(arg, "--env", fmt.Sprintf("%s=%s", nv[0], nv[1]))
}
// set the path to be executed
arg = append(arg, path)
// set the program arguments
if len(opts.args) > 0 {
arg = append(arg, "--args")
for _, nv := range opts.args {
arg = append(arg, nv[0], nv[1])
}
}
var cmd *exec.Cmd
// If we have a user in the options, use sudo to run "open" as that user
if opts.user != "" {
arg = append([]string{"-u", opts.user, "/usr/bin/open"}, arg...)
cmd = exec.Command("sudo", arg...)
} else {
// Otherwise, just run "open" as the current user
cmd = exec.Command("/usr/bin/open", arg...)
}
tw := &TransientWriter{}
cmd.Stderr = io.MultiWriter(tw, os.Stderr)
cmd.Stdout = io.MultiWriter(tw, os.Stdout)
if err := cmd.Run(); err != nil {
return tw.String(), fmt.Errorf("open path %q: %w", path, err)
}
return tw.String(), nil
}
func runWithOutput(path string, opts eopts) (output []byte, exitCode int, err error) {
return nil, 0, errors.New("not implemented")
}