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:
@@ -218,3 +218,105 @@ func readInotify(f *os.File) ([]string, error) {
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
func (d *dbusSystem) sessions(uid uint32) ([]loginSession, error) {
|
||||
var listed []struct {
|
||||
ID string
|
||||
UID uint32
|
||||
User string
|
||||
Seat string
|
||||
Path dbus.ObjectPath
|
||||
}
|
||||
if err := d.conn.Object(login1Name, login1Path).Call(login1Iface+".ListSessions", 0).Store(&listed); err != nil {
|
||||
return nil, fmt.Errorf("logind's sessions: %w", err)
|
||||
}
|
||||
var out []loginSession
|
||||
for _, l := range listed {
|
||||
if l.UID != uid {
|
||||
continue
|
||||
}
|
||||
s, err := d.session(string(l.Path))
|
||||
if err != nil {
|
||||
journal("reading session %s failed: %v", l.ID, err)
|
||||
continue
|
||||
}
|
||||
out = append(out, s)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (d *dbusSystem) session(path string) (loginSession, error) {
|
||||
o := d.conn.Object(login1Name, dbus.ObjectPath(path))
|
||||
s := loginSession{Path: path}
|
||||
var user struct {
|
||||
UID uint32
|
||||
Path dbus.ObjectPath
|
||||
}
|
||||
for name, into := range map[string]any{"Id": &s.ID, "Type": &s.Type, "Remote": &s.Remote, "State": &s.State,
|
||||
"User": &user} {
|
||||
v, err := o.GetProperty("org.freedesktop.login1.Session." + name)
|
||||
if err != nil {
|
||||
return s, fmt.Errorf("session %s's %s: %w", path, name, err)
|
||||
}
|
||||
if err := dbus.Store([]any{v.Value()}, into); err != nil {
|
||||
return s, fmt.Errorf("session %s's %s: %w", path, name, err)
|
||||
}
|
||||
}
|
||||
s.UID = user.UID
|
||||
v, err := d.conn.Object(login1Name, user.Path).GetProperty("org.freedesktop.login1.User.RuntimePath")
|
||||
if err != nil {
|
||||
return s, fmt.Errorf("the user %d's RuntimePath: %w", user.UID, err)
|
||||
}
|
||||
s.RuntimePath, _ = v.Value().(string)
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// userEnvironment is the user manager's Environment, read over the session bus at bus.
|
||||
func userEnvironment(bus string) ([]string, error) {
|
||||
conn, err := dbus.Connect(bus)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
v, err := conn.Object("org.freedesktop.systemd1", "/org/freedesktop/systemd1").
|
||||
GetProperty("org.freedesktop.systemd1.Manager.Environment")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
env, _ := v.Value().([]string)
|
||||
return env, nil
|
||||
}
|
||||
|
||||
// fileEvents fires at each file event of the kernel that makes a file in one of dirs (a burst is one), until stop
|
||||
// closes. A folder that is not there is not watched, and said.
|
||||
func fileEvents(dirs []string, stop <-chan struct{}) <-chan struct{} {
|
||||
out := make(chan struct{}, 1)
|
||||
fd, err := unix.InotifyInit1(unix.IN_CLOEXEC | unix.IN_NONBLOCK)
|
||||
if err != nil {
|
||||
journal("watching %v failed: %v", dirs, err)
|
||||
return out
|
||||
}
|
||||
for _, dir := range dirs {
|
||||
if _, err := unix.InotifyAddWatch(fd, dir, unix.IN_CREATE|unix.IN_MOVED_TO); err != nil {
|
||||
journal("watching %s failed: %v", dir, err)
|
||||
}
|
||||
}
|
||||
f := os.NewFile(uintptr(fd), "inotify")
|
||||
go func() {
|
||||
<-stop
|
||||
f.Close()
|
||||
}()
|
||||
go func() {
|
||||
defer f.Close()
|
||||
for {
|
||||
if _, err := readInotify(f); err != nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case out <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}()
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// The person's session as logind says it: x11, local, active, of the watcher's account.
|
||||
func personsSession() loginSession {
|
||||
return loginSession{ID: "2", Path: "/org/freedesktop/login1/session/_32", UID: 1000, Type: "x11", State: "active",
|
||||
RuntimePath: "/run/user/1000"}
|
||||
}
|
||||
|
||||
// aSessionWatcher is a subscribed watcher whose stood-in user manager names a display once named is set; the test acts
|
||||
// as its loop, calling what a file event and the bound's end call.
|
||||
func aSessionWatcher(t *testing.T, sys *fakeSystem, named *bool) *Watcher {
|
||||
t.Helper()
|
||||
w := aWatcher(t, sys)
|
||||
files, bound := make(chan struct{}, 1), make(chan time.Time, 1)
|
||||
w.FileEvents = func(dirs []string, stop <-chan struct{}) <-chan struct{} {
|
||||
if strings.Join(dirs, " ") != "/tmp/.X11-unix /run/user/1000" {
|
||||
t.Errorf("the session's start is waited for on %v", dirs)
|
||||
}
|
||||
return files
|
||||
}
|
||||
w.After = func(d time.Duration) <-chan time.Time {
|
||||
if d != 60*time.Second {
|
||||
t.Errorf("the session's start is waited for %s", d)
|
||||
}
|
||||
return bound
|
||||
}
|
||||
w.Environment = func(bus string) ([]string, error) {
|
||||
if bus != "unix:path=/run/user/1000/bus" {
|
||||
t.Errorf("the environment is read over %s", bus)
|
||||
}
|
||||
if *named {
|
||||
return []string{"HOME=/home/person", "DISPLAY=:1", "XAUTHORITY=/home/person/.Xauthority"}, nil
|
||||
}
|
||||
return []string{"HOME=/home/person"}, nil
|
||||
}
|
||||
w.subscribe()
|
||||
return w
|
||||
}
|
||||
|
||||
// A session that starts while the rating is PORTAL opens the page once, when the user manager's environment names its
|
||||
// display; one that starts while it is not opens nothing; one whose display is not named within the bound is not acted
|
||||
// on, and says so (design 08 sign-in-page §15, §16, §17, §25).
|
||||
func TestASessionStartingWhilePortalOpensThePageOnce(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
rating Rating
|
||||
named bool
|
||||
opens int
|
||||
did string
|
||||
}{
|
||||
{"PORTAL", guest(ratingPortal), true, 1, "opened the sign-in page"},
|
||||
{"FULL", guest(ratingFull), true, 0, "nothing: the rating is FULL"},
|
||||
{"no display named", guest(ratingPortal), false, 0, "not acted on: no display named within 1m0s"},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
lines := journalLines(t)
|
||||
sys := &fakeSystem{}
|
||||
sys.set(guest(ratingFull))
|
||||
named := false
|
||||
w := aSessionWatcher(t, sys, &named)
|
||||
desk := w.Desk.(*fakeDesk)
|
||||
sys.sessions = []loginSession{personsSession(), {ID: "9", Path: "/org/freedesktop/login1/session/_39", UID: 1001,
|
||||
Type: "x11", State: "active", RuntimePath: "/run/user/1001"}}
|
||||
w.heardSignal(signal{Session: "/org/freedesktop/login1/session/_39"})
|
||||
if w.starting != nil {
|
||||
t.Fatal("another account's session is waited for")
|
||||
}
|
||||
sys.set(c.rating)
|
||||
w.heardSignal(signal{Session: personsSession().Path})
|
||||
w.sessionFiles()
|
||||
if len(desk.opened()) != 0 || w.starting == nil {
|
||||
t.Fatal("acted on a session whose display is not named yet")
|
||||
}
|
||||
named = c.named
|
||||
w.sessionFiles()
|
||||
if w.starting != nil {
|
||||
w.sessionBound()
|
||||
}
|
||||
if got := desk.opened(); len(got) != c.opens || (c.opens == 1 && got[0] != "2 http://check.example.net/") {
|
||||
t.Fatalf("opened %q", got)
|
||||
}
|
||||
if w.state.LastSession == nil || w.state.LastSession.Did != c.did {
|
||||
t.Fatalf("the session's start is said as %+v", w.state.LastSession)
|
||||
}
|
||||
if c.named == false && !strings.Contains(strings.Join(lines(), "\n"), "start was not acted on") {
|
||||
t.Fatal("a start not acted on was not written to the journal")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// With no graphical session found the watcher opens nothing and still keeps the events: a remote session, one in no
|
||||
// graphical type and one whose environment names no display are no session (§15, §16, §25).
|
||||
func TestNoSessionOpensNothingAndStillKeeps(t *testing.T) {
|
||||
journalLines(t)
|
||||
sys := &fakeSystem{}
|
||||
named := false
|
||||
w := aSessionWatcher(t, sys, &named)
|
||||
remote, tty := personsSession(), personsSession()
|
||||
remote.Remote, tty.Type = true, "tty"
|
||||
sys.sessions = []loginSession{remote, tty, personsSession()}
|
||||
w.heard(guest(ratingFull), false)
|
||||
w.heard(guest(ratingPortal), false)
|
||||
w.present(nil, "guest-example", "http://check.example.net/")
|
||||
if got := w.Desk.(*fakeDesk).opened(); len(got) != 0 {
|
||||
t.Fatalf("opened %q with no session", got)
|
||||
}
|
||||
if kept := keptSaid(t, w.Folder); len(kept) == 0 || kept[len(kept)-1] != "sign-in-asked guest-example wlan0" {
|
||||
t.Fatalf("kept %q", kept)
|
||||
}
|
||||
named = true
|
||||
if s, err := w.findSession(); err != nil || s == nil || s.ID != "2" || strings.Join(s.Display, " ") != "DISPLAY=:1 XAUTHORITY=/home/person/.Xauthority" {
|
||||
t.Fatalf("found %+v %v", s, err)
|
||||
}
|
||||
if got := displayIn([]string{"WAYLAND_DISPLAY=wayland-0", "DISPLAY=:0"}, "wayland"); strings.Join(got, " ") != "WAYLAND_DISPLAY=wayland-0" {
|
||||
t.Fatalf("a wayland session's display is %v", got)
|
||||
}
|
||||
}
|
||||
@@ -98,6 +98,9 @@ type signal struct {
|
||||
type systemBus interface {
|
||||
listen() (<-chan signal, error)
|
||||
rating() (Rating, error)
|
||||
// sessions is every session of the account uid; session is one, by its object.
|
||||
sessions(uid uint32) ([]loginSession, error)
|
||||
session(path string) (loginSession, error)
|
||||
close()
|
||||
}
|
||||
|
||||
@@ -112,13 +115,15 @@ type openAsk struct {
|
||||
|
||||
// watcherState is the watcher's answer to `state` (§23's read).
|
||||
type watcherState struct {
|
||||
Subscribed bool `json:"subscribed"`
|
||||
Rating string `json:"rating,omitempty"`
|
||||
RatingAt *time.Time `json:"rating_at,omitempty"`
|
||||
Ask *openAsk `json:"ask,omitempty"`
|
||||
AsksOpened int `json:"asks_opened"`
|
||||
AsksEnded int `json:"asks_ended"`
|
||||
DroppedPastBound int `json:"dropped_past_bound"`
|
||||
Subscribed bool `json:"subscribed"`
|
||||
Rating string `json:"rating,omitempty"`
|
||||
RatingAt *time.Time `json:"rating_at,omitempty"`
|
||||
Ask *openAsk `json:"ask,omitempty"`
|
||||
AsksOpened int `json:"asks_opened"`
|
||||
AsksEnded int `json:"asks_ended"`
|
||||
DroppedPastBound int `json:"dropped_past_bound"`
|
||||
LastOpen *openOutcome `json:"last_open,omitempty"`
|
||||
LastSession *sessionNote `json:"last_session,omitempty"`
|
||||
}
|
||||
|
||||
// asked is an ask on the watcher's socket, handed to its loop, and the answer it sends back.
|
||||
@@ -141,7 +146,17 @@ type Watcher struct {
|
||||
// folder; closing stop ends the wait.
|
||||
SocketThere func(stop <-chan struct{}) <-chan struct{}
|
||||
Now func() time.Time
|
||||
// UID is the watcher's account, whose sessions are the person's.
|
||||
UID uint32
|
||||
// Environment reads the user manager's environment over a session's bus.
|
||||
Environment func(bus string) ([]string, error)
|
||||
// FileEvents fires at each file event of the kernel that makes a file in one of dirs, until stop closes.
|
||||
FileEvents func(dirs []string, stop <-chan struct{}) <-chan struct{}
|
||||
// After is a stated bound's end.
|
||||
After func(time.Duration) <-chan time.Time
|
||||
Desk desk
|
||||
|
||||
starting *sessionStart
|
||||
asks chan asked
|
||||
bus systemBus
|
||||
signals <-chan signal
|
||||
@@ -154,7 +169,8 @@ type Watcher struct {
|
||||
}
|
||||
|
||||
func newWatcher(f Folder) *Watcher {
|
||||
return &Watcher{Folder: f, Dial: dialSystemBus, SocketThere: systemSocketThere, Now: time.Now, asks: make(chan asked)}
|
||||
return &Watcher{Folder: f, Dial: dialSystemBus, SocketThere: systemSocketThere, Now: time.Now, asks: make(chan asked),
|
||||
UID: uint32(os.Getuid()), Environment: userEnvironment, FileEvents: fileEvents, After: time.After}
|
||||
}
|
||||
|
||||
// watch is `uplink-tools watch`: the watcher, until its process is stopped.
|
||||
@@ -206,6 +222,10 @@ func (w *Watcher) run(stop <-chan struct{}) {
|
||||
continue
|
||||
}
|
||||
w.heardSignal(s)
|
||||
case <-w.startingFiles():
|
||||
w.sessionFiles()
|
||||
case <-w.startingBound():
|
||||
w.sessionBound()
|
||||
case <-w.socketAt:
|
||||
w.socketAt = nil
|
||||
w.subscribe()
|
||||
@@ -258,6 +278,23 @@ func (w *Watcher) heardSignal(s signal) {
|
||||
if s.Rating {
|
||||
w.readRating()
|
||||
}
|
||||
if s.Session != "" {
|
||||
w.sessionNew(s.Session)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Watcher) startingFiles() <-chan struct{} {
|
||||
if w.starting == nil {
|
||||
return nil
|
||||
}
|
||||
return w.starting.files
|
||||
}
|
||||
|
||||
func (w *Watcher) startingBound() <-chan time.Time {
|
||||
if w.starting == nil {
|
||||
return nil
|
||||
}
|
||||
return w.starting.bound
|
||||
}
|
||||
|
||||
// readRating reads the rating now and acts on it: as the watcher's start the first time, as a change after.
|
||||
|
||||
@@ -14,12 +14,13 @@ import (
|
||||
// The system bus stood in: the rating the test sets, each connection's signals on an unbuffered channel (a send
|
||||
// returns once the watcher's loop took it), and the system bus's socket fired by the test.
|
||||
type fakeSystem struct {
|
||||
mu sync.Mutex
|
||||
now Rating
|
||||
reads int
|
||||
dials int
|
||||
buses []*fakeBus
|
||||
socket chan struct{}
|
||||
mu sync.Mutex
|
||||
now Rating
|
||||
sessions []loginSession
|
||||
reads int
|
||||
dials int
|
||||
buses []*fakeBus
|
||||
socket chan struct{}
|
||||
}
|
||||
|
||||
type fakeBus struct {
|
||||
@@ -56,7 +57,28 @@ func (s *fakeSystem) counts() (int, int) {
|
||||
}
|
||||
|
||||
func (b *fakeBus) listen() (<-chan signal, error) { return b.signals, nil }
|
||||
func (b *fakeBus) close() { b.closed = true }
|
||||
func (b *fakeBus) sessions(uid uint32) ([]loginSession, error) {
|
||||
b.sys.mu.Lock()
|
||||
defer b.sys.mu.Unlock()
|
||||
var out []loginSession
|
||||
for _, s := range b.sys.sessions {
|
||||
if s.UID == uid {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
func (b *fakeBus) session(path string) (loginSession, error) {
|
||||
b.sys.mu.Lock()
|
||||
defer b.sys.mu.Unlock()
|
||||
for _, s := range b.sys.sessions {
|
||||
if s.Path == path {
|
||||
return s, nil
|
||||
}
|
||||
}
|
||||
return loginSession{}, fmt.Errorf("no session %s", path)
|
||||
}
|
||||
func (b *fakeBus) close() { b.closed = true }
|
||||
func (b *fakeBus) rating() (Rating, error) {
|
||||
b.sys.mu.Lock()
|
||||
defer b.sys.mu.Unlock()
|
||||
@@ -95,7 +117,34 @@ func aWatcher(t *testing.T, sys *fakeSystem) *Watcher {
|
||||
defer mu.Unlock()
|
||||
tick = tick.Add(time.Second)
|
||||
return tick
|
||||
}, SocketThere: func(stop <-chan struct{}) <-chan struct{} { return sys.socket }, asks: make(chan asked)}
|
||||
}, SocketThere: func(stop <-chan struct{}) <-chan struct{} { return sys.socket }, asks: make(chan asked),
|
||||
UID: 1000, Desk: &fakeDesk{}, Environment: func(string) ([]string, error) { return nil, nil },
|
||||
FileEvents: func([]string, <-chan struct{}) <-chan struct{} { return nil },
|
||||
After: func(time.Duration) <-chan time.Time { return nil }}
|
||||
}
|
||||
|
||||
// The desktop stood in: each open it was asked, answered with outcome.
|
||||
type fakeDesk struct {
|
||||
mu sync.Mutex
|
||||
opens []string
|
||||
outcome openOutcome
|
||||
}
|
||||
|
||||
func (d *fakeDesk) open(s Session, address string) openOutcome {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
d.opens = append(d.opens, s.ID+" "+address)
|
||||
o := d.outcome
|
||||
if o.Outcome == "" {
|
||||
o = openOutcome{Address: address, Program: "browser.desktop", Outcome: outcomeOpened}
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
func (d *fakeDesk) opened() []string {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
return append([]string(nil), d.opens...)
|
||||
}
|
||||
|
||||
// keptSaid is every kept event as `<event> <why> <network> <link>`, in order.
|
||||
|
||||
Reference in New Issue
Block a user