M3 networkmanager: the person's session
The watcher finds the person's graphical session from logind and the user manager's environment, and acts on a session's start once that environment names a display; the user manager sends no PropertiesChanged for Environment, so it is read again at the file events on the X server's socket folder and the session's runtime directory, for at most 60 s. Builds: design 08 sign-in-page §15, §16, §17
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
package main
|
||||
|
||||
// The person's graphical session (design 08 sign-in-page §16, §17): found over the system bus from logind, its
|
||||
// session bus at the user's RuntimePath and its display in the user manager's environment; its start heard as
|
||||
// logind's SessionNew, then waited for until the user manager's environment names a display. The user manager sends
|
||||
// no PropertiesChanged for its Environment (EmitsChangedSignal false), so the environment is read again at each file
|
||||
// event of the kernel on the X server's socket folder and on the session's RuntimePath, for at most 60 s (§17).
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// sessionWait is the most the watcher waits, after a session started, for the user manager's environment to name a
|
||||
// display (§17).
|
||||
const sessionWait = 60 * time.Second
|
||||
|
||||
// x11Sockets is the folder an X server makes its socket in.
|
||||
var x11Sockets = "/tmp/.X11-unix"
|
||||
|
||||
// loginSession is what logind says of one session: its id and object, its account, type, whether it is remote, its
|
||||
// state, and its user's runtime directory.
|
||||
type loginSession struct {
|
||||
ID string
|
||||
Path string
|
||||
UID uint32
|
||||
Type string
|
||||
Remote bool
|
||||
State string
|
||||
RuntimePath string
|
||||
}
|
||||
|
||||
// graphical says whether the session is one the person sits at: x11 or wayland, not remote, active or online.
|
||||
func (s loginSession) graphical() bool {
|
||||
return (s.Type == "x11" || s.Type == "wayland") && !s.Remote && (s.State == "active" || s.State == "online")
|
||||
}
|
||||
|
||||
// Session is the person's graphical session as the watcher reaches it: its session bus, its runtime directory and the
|
||||
// display its user manager's environment names.
|
||||
type Session struct {
|
||||
ID string
|
||||
Type string
|
||||
RuntimePath string
|
||||
Display []string
|
||||
}
|
||||
|
||||
// Bus is the session's bus address.
|
||||
func (s Session) Bus() string { return "unix:path=" + s.RuntimePath + "/bus" }
|
||||
|
||||
// Env is the environment a program run in the session gets: the watcher's own, with the session's bus and runtime
|
||||
// directory.
|
||||
func (s Session) Env() []string {
|
||||
var env []string
|
||||
for _, kv := range os.Environ() {
|
||||
if !strings.HasPrefix(kv, "DBUS_SESSION_BUS_ADDRESS=") && !strings.HasPrefix(kv, "XDG_RUNTIME_DIR=") {
|
||||
env = append(env, kv)
|
||||
}
|
||||
}
|
||||
return append(env, "DBUS_SESSION_BUS_ADDRESS="+s.Bus(), "XDG_RUNTIME_DIR="+s.RuntimePath)
|
||||
}
|
||||
|
||||
// displayIn is the display an environment names for a session of its type: DISPLAY and XAUTHORITY for x11,
|
||||
// WAYLAND_DISPLAY for wayland; none when it names none.
|
||||
func displayIn(env []string, sessionType string) []string {
|
||||
want := map[string]bool{"DISPLAY": sessionType == "x11", "XAUTHORITY": sessionType == "x11",
|
||||
"WAYLAND_DISPLAY": sessionType == "wayland"}
|
||||
var out []string
|
||||
named := false
|
||||
for _, kv := range env {
|
||||
name, value, _ := strings.Cut(kv, "=")
|
||||
if want[name] && value != "" {
|
||||
out = append(out, kv)
|
||||
named = named || name == "DISPLAY" || name == "WAYLAND_DISPLAY"
|
||||
}
|
||||
}
|
||||
if !named {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// findSession is the person's graphical session now, or nil: a session of the watcher's account that is graphical,
|
||||
// whose user manager's environment names a display (§16).
|
||||
func (w *Watcher) findSession() (*Session, error) {
|
||||
if w.bus == nil {
|
||||
return nil, fmt.Errorf("not subscribed on the system bus")
|
||||
}
|
||||
sessions, err := w.bus.sessions(w.UID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, s := range sessions {
|
||||
if !s.graphical() {
|
||||
continue
|
||||
}
|
||||
if found := w.sessionWithDisplay(s); found != nil {
|
||||
return found, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// sessionWithDisplay is s reached, when its user manager's environment names a display; else nil.
|
||||
func (w *Watcher) sessionWithDisplay(s loginSession) *Session {
|
||||
found := Session{ID: s.ID, Type: s.Type, RuntimePath: s.RuntimePath}
|
||||
env, err := w.Environment(found.Bus())
|
||||
if err != nil {
|
||||
journal("reading the user manager's environment of session %s failed: %v", s.ID, err)
|
||||
return nil
|
||||
}
|
||||
if found.Display = displayIn(env, s.Type); found.Display == nil {
|
||||
return nil
|
||||
}
|
||||
return &found
|
||||
}
|
||||
|
||||
// sessionStart is the session start being waited for: the session, until when, and the wait's file events.
|
||||
type sessionStart struct {
|
||||
session loginSession
|
||||
files <-chan struct{}
|
||||
bound <-chan time.Time
|
||||
stop chan struct{}
|
||||
}
|
||||
|
||||
// sessionNew acts on logind's SessionNew: for a graphical session of the watcher's account, it waits for the user
|
||||
// manager's environment to name a display, reading it again at each file event (§17).
|
||||
func (w *Watcher) sessionNew(path string) {
|
||||
s, err := w.bus.session(path)
|
||||
if err != nil {
|
||||
journal("reading the new session %s failed: %v", path, err)
|
||||
return
|
||||
}
|
||||
if s.UID != w.UID || (s.Type != "x11" && s.Type != "wayland") || s.Remote {
|
||||
return
|
||||
}
|
||||
w.endSessionWait()
|
||||
stop := make(chan struct{})
|
||||
w.starting = &sessionStart{session: s, stop: stop, bound: w.After(sessionWait),
|
||||
files: w.FileEvents([]string{x11Sockets, s.RuntimePath}, stop)}
|
||||
journal("session %s (%s) started; waiting for its display", s.ID, s.Type)
|
||||
}
|
||||
|
||||
// sessionFiles acts on a file event during the wait: the environment is read again, and once it names a display the
|
||||
// session's start is acted on (§15's third moment).
|
||||
func (w *Watcher) sessionFiles() {
|
||||
if w.starting == nil {
|
||||
return
|
||||
}
|
||||
found := w.sessionWithDisplay(w.starting.session)
|
||||
if found == nil {
|
||||
return
|
||||
}
|
||||
w.endSessionWait()
|
||||
w.sessionStarted(*found)
|
||||
}
|
||||
|
||||
// sessionBound ends a wait that found no display within its bound.
|
||||
func (w *Watcher) sessionBound() {
|
||||
if w.starting == nil {
|
||||
return
|
||||
}
|
||||
journal("session %s's start was not acted on: its user manager's environment named no display within %s",
|
||||
w.starting.session.ID, sessionWait)
|
||||
w.noteSession("not acted on: no display named within " + sessionWait.String())
|
||||
w.endSessionWait()
|
||||
}
|
||||
|
||||
func (w *Watcher) endSessionWait() {
|
||||
if w.starting != nil {
|
||||
close(w.starting.stop)
|
||||
w.starting = nil
|
||||
}
|
||||
}
|
||||
|
||||
// sessionStarted is §15's third moment: with the present rating PORTAL, the check on and a connection active, the page
|
||||
// is opened and the person told; else nothing. It reads the present rating, never a kept event.
|
||||
func (w *Watcher) sessionStarted(s Session) {
|
||||
if w.bus == nil {
|
||||
journal("session %s started while not subscribed on the system bus: nothing opened", s.ID)
|
||||
w.noteSession("nothing: not subscribed on the system bus")
|
||||
return
|
||||
}
|
||||
r, err := w.bus.rating()
|
||||
if err != nil {
|
||||
journal("session %s started; reading the rating failed: %v", s.ID, err)
|
||||
w.noteSession("nothing: the rating did not read")
|
||||
return
|
||||
}
|
||||
if !r.checkOn() || !webAddress(r.CheckURI) || r.Connectivity != ratingPortal || !r.active() {
|
||||
journal("session %s started with the rating %s: nothing opened", s.ID, ratingName(r.Connectivity))
|
||||
w.noteSession("nothing: the rating is " + ratingName(r.Connectivity))
|
||||
return
|
||||
}
|
||||
journal("session %s started with the rating PORTAL: opening the sign-in page", s.ID)
|
||||
w.noteSession("opened the sign-in page")
|
||||
w.present(&s, r.Network, r.CheckURI)
|
||||
}
|
||||
|
||||
func (w *Watcher) noteSession(did string) {
|
||||
w.state.LastSession = &sessionNote{At: w.Now(), Did: did}
|
||||
}
|
||||
|
||||
// sessionNote is the last session start and what the watcher did.
|
||||
type sessionNote struct {
|
||||
At time.Time `json:"at"`
|
||||
Did string `json:"did"`
|
||||
}
|
||||
|
||||
// desk is the person's desktop as the watcher reaches it: opening an address in their session (§18).
|
||||
type desk interface {
|
||||
open(s Session, address string) openOutcome
|
||||
}
|
||||
|
||||
// openOutcome is how an open ended: opened, opening (still running at its bound) or failed, with the program and the
|
||||
// reason of a failure.
|
||||
type openOutcome struct {
|
||||
Address string `json:"address"`
|
||||
Program string `json:"program,omitempty"`
|
||||
Outcome string `json:"outcome"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
// The outcomes of an open.
|
||||
const (
|
||||
outcomeOpened = "opened"
|
||||
outcomeOpening = "opening"
|
||||
outcomeFailed = "failed"
|
||||
)
|
||||
|
||||
// present opens the address in the person's session, s or the one found now (§15, §18); with no session found it opens
|
||||
// nothing, and the events are kept all the same (§15, §16).
|
||||
func (w *Watcher) present(s *Session, network, address string) {
|
||||
if s == nil {
|
||||
found, err := w.findSession()
|
||||
if err != nil {
|
||||
journal("finding the person's session failed: %v; the sign-in page of %q is not opened", err, network)
|
||||
return
|
||||
}
|
||||
if found == nil {
|
||||
journal("no graphical session: the sign-in page of %q is not opened", network)
|
||||
return
|
||||
}
|
||||
s = found
|
||||
}
|
||||
o := w.Desk.open(*s, address)
|
||||
o.At = w.Now()
|
||||
w.state.LastOpen = &o
|
||||
if o.Outcome == outcomeFailed {
|
||||
journal("opening %s in session %s failed: %s", address, s.ID, o.Reason)
|
||||
} else {
|
||||
journal("the sign-in page %s is %s in %s", address, o.Outcome, o.Program)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user