1package launcher
2
3import (
4 "context"
5 "net"
6 "os/exec"
7 "time"
8
9 "github.com/bnema/zerowrap"
10)
11
12// sessionVars are the displays D-Bus and systemd user services (portals,
13// notification daemons) need to reach the session.
14var sessionVars = []string{"WAYLAND_DISPLAY", "DISPLAY", "XDG_CURRENT_DESKTOP", "XDG_SESSION_TYPE"}
15
16// portalUnits are the desktop portal and its backends. They read the
17// session variables once, at start: a portal started without them (before
18// the export, or after the last session ended) never serves this session.
19// Portals are per user, like the exported variables: another graphical
20// session of the same user running at once is not supported.
21const portalUnits = "xdg-desktop-portal*.service"
22
23// ExportSession shares the session's displays with D-Bus and systemd user
24// services, and waits for it: clients started next may activate them. Portals
25// already running restart to read them; the restart does not block, since a
26// backend may connect to the display before it serves.
27func ExportSession(ctx context.Context, env []string, log zerowrap.Logger) {
28 ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
29 defer cancel()
30 if !runSessionCommand(ctx, env, log, append([]string{"--systemd"}, sessionVars...), "session exported") {
31 return
32 }
33 runSystemctl(ctx, env, log, []string{"--no-block", "try-restart", portalUnits}, "portal restart failed")
34}
35
36// UnexportSession withdraws the displays from the systemd user manager on
37// exit, so services started after logout do not reach a dead display, and
38// stops the portals that still hold them.
39func UnexportSession(env []string, log zerowrap.Logger) {
40 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
41 defer cancel()
42 runSystemctl(ctx, env, log, append([]string{"unset-environment"}, sessionVars...), "session unexport failed")
43 runSystemctl(ctx, env, log, []string{"--no-block", "stop", portalUnits}, "portal stop failed")
44}
45
46// runSystemctl runs systemctl --user with args; a missing systemctl is skipped.
47func runSystemctl(ctx context.Context, env []string, log zerowrap.Logger, args []string, failed string) {
48 path, err := childLookPath("systemctl", env)
49 if err != nil {
50 return
51 }
52 cmd := exec.CommandContext(ctx, path, append([]string{"--user"}, args...)...)
53 cmd.Env = env
54 if err := cmd.Run(); err != nil {
55 log.Warn().Err(err).Strs("args", args).Msg(failed)
56 }
57}
58
59// runSessionCommand runs dbus-update-activation-environment and reports
60// whether it succeeded.
61func runSessionCommand(ctx context.Context, env []string, log zerowrap.Logger, args []string, done string) bool {
62 path, err := childLookPath("dbus-update-activation-environment", env)
63 if err != nil {
64 log.Warn().Err(err).Msg("session export skipped: dbus-update-activation-environment not found")
65 return false
66 }
67 cmd := exec.CommandContext(ctx, path, args...)
68 cmd.Env = env
69 if err := cmd.Run(); err != nil {
70 log.Warn().Err(err).Msg("session export failed")
71 return false
72 }
73 log.Info().Strs("vars", sessionVars).Msg(done)
74 return true
75}
76
77// NotifyReady tells the systemd user manager that the compositor is ready.
78func NotifyReady(socket string, log zerowrap.Logger) {
79 if socket == "" {
80 return
81 }
82 conn, err := net.DialUnix("unixgram", nil, &net.UnixAddr{Name: socket, Net: "unixgram"})
83 if err != nil {
84 log.Warn().Err(err).Msg("session readiness notification failed")
85 return
86 }
87 defer conn.Close()
88 if _, err := conn.Write([]byte("READY=1")); err != nil {
89 log.Warn().Err(err).Msg("session readiness notification failed")
90 return
91 }
92 log.Info().Msg("session ready")
93}