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:
jochen
2026-11-24 21:18:36 +01:00
parent dc59860541
commit fee3f8ab8b
5 changed files with 584 additions and 16 deletions
@@ -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
}