1package launcher
2
3import (
4 "context"
5 "os/exec"
6 "time"
7
8 "github.com/bnema/zerowrap"
9)
10
11// sessionVars are the displays D-Bus and systemd user services (portals,
12// notification daemons) need to reach the session.
13var sessionVars = []string{"WAYLAND_DISPLAY", "DISPLAY", "XDG_CURRENT_DESKTOP", "XDG_SESSION_TYPE"}
14
15// ExportSession shares the session's displays with D-Bus and systemd user
16// services, and waits for it: clients started next may activate them.
17func ExportSession(ctx context.Context, env []string, log zerowrap.Logger) {
18 ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
19 defer cancel()
20 runSessionCommand(ctx, env, log, append([]string{"--systemd"}, sessionVars...), "session exported")
21}
22
23// UnexportSession withdraws the displays from the systemd user manager on
24// exit, so services started after logout do not reach a dead display.
25func UnexportSession(env []string, log zerowrap.Logger) {
26 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
27 defer cancel()
28 path, err := childLookPath("systemctl", env)
29 if err != nil {
30 return
31 }
32 cmd := exec.CommandContext(ctx, path, append([]string{"--user", "unset-environment"}, sessionVars...)...)
33 cmd.Env = env
34 if err := cmd.Run(); err != nil {
35 log.Warn().Err(err).Msg("session unexport failed")
36 }
37}
38
39func runSessionCommand(ctx context.Context, env []string, log zerowrap.Logger, args []string, done string) {
40 path, err := childLookPath("dbus-update-activation-environment", env)
41 if err != nil {
42 log.Warn().Err(err).Msg("session export skipped: dbus-update-activation-environment not found")
43 return
44 }
45 cmd := exec.CommandContext(ctx, path, args...)
46 cmd.Env = env
47 if err := cmd.Run(); err != nil {
48 log.Warn().Err(err).Msg("session export failed")
49 return
50 }
51 log.Info().Strs("vars", sessionVars).Msg(done)
52}