M2 networkmanager: the watcher hears the rating
uplink-tools watch hears the network manager's PropertiesChanged on the system bus, keeps sign-in-asked and sign-in-ended as the rules say, at most 256 of them, and subscribes again when the system bus's socket comes back or on rating-again; nothing reads the rating on a timer. Builds: design 08 sign-in-page §12, §14, §20, §21, §22
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
package main
|
||||
|
||||
// The watcher's system bus, through godbus: the network manager's properties and its PropertiesChanged, logind's
|
||||
// SessionNew, and the system bus's socket coming back, heard as a file event of the kernel on its folder.
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"unsafe"
|
||||
|
||||
"github.com/godbus/dbus/v5"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const (
|
||||
nmName = "org.freedesktop.NetworkManager"
|
||||
nmPath = dbus.ObjectPath("/org/freedesktop/NetworkManager")
|
||||
nmActive = "org.freedesktop.NetworkManager.Connection.Active"
|
||||
nmDevice = "org.freedesktop.NetworkManager.Device"
|
||||
propsIface = "org.freedesktop.DBus.Properties"
|
||||
login1Name = "org.freedesktop.login1"
|
||||
login1Path = dbus.ObjectPath("/org/freedesktop/login1")
|
||||
login1Iface = "org.freedesktop.login1.Manager"
|
||||
)
|
||||
|
||||
// systemSocket is the system bus's socket, whose folder the watcher watches when its connection dropped.
|
||||
var systemSocket = "/run/dbus/system_bus_socket"
|
||||
|
||||
// ratingProperties are the network manager's properties whose change the watcher acts on (§14).
|
||||
var ratingProperties = map[string]bool{"Connectivity": true, "PrimaryConnection": true,
|
||||
"ConnectivityCheckEnabled": true, "ConnectivityCheckAvailable": true, "ConnectivityCheckUri": true}
|
||||
|
||||
type dbusSystem struct {
|
||||
conn *dbus.Conn
|
||||
}
|
||||
|
||||
func dialSystemBus() (systemBus, error) {
|
||||
conn, err := dbus.ConnectSystemBus()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dbusSystem{conn: conn}, nil
|
||||
}
|
||||
|
||||
func (d *dbusSystem) close() { d.conn.Close() }
|
||||
|
||||
func (d *dbusSystem) listen() (<-chan signal, error) {
|
||||
if err := d.conn.AddMatchSignal(dbus.WithMatchObjectPath(nmPath), dbus.WithMatchInterface(propsIface),
|
||||
dbus.WithMatchMember("PropertiesChanged")); err != nil {
|
||||
return nil, fmt.Errorf("hearing the network manager: %w", err)
|
||||
}
|
||||
if err := d.conn.AddMatchSignal(dbus.WithMatchObjectPath(login1Path), dbus.WithMatchInterface(login1Iface),
|
||||
dbus.WithMatchMember("SessionNew")); err != nil {
|
||||
return nil, fmt.Errorf("hearing logind: %w", err)
|
||||
}
|
||||
in := make(chan *dbus.Signal, 64)
|
||||
d.conn.Signal(in)
|
||||
out := make(chan signal)
|
||||
go func() {
|
||||
// godbus closes in when the connection drops.
|
||||
for s := range in {
|
||||
if heard, ok := readSignal(s); ok {
|
||||
out <- heard
|
||||
}
|
||||
}
|
||||
close(out)
|
||||
}()
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// readSignal is what the watcher hears in a signal of the system bus: the network manager's PropertiesChanged naming
|
||||
// a property of the rating, or logind's SessionNew with the new session's object.
|
||||
func readSignal(s *dbus.Signal) (signal, bool) {
|
||||
switch {
|
||||
case s.Path == nmPath && s.Name == propsIface+".PropertiesChanged" && len(s.Body) >= 3:
|
||||
if iface, _ := s.Body[0].(string); iface != nmName {
|
||||
return signal{}, false
|
||||
}
|
||||
changed, _ := s.Body[1].(map[string]dbus.Variant)
|
||||
for name := range changed {
|
||||
if ratingProperties[name] {
|
||||
return signal{Rating: true}, true
|
||||
}
|
||||
}
|
||||
invalidated, _ := s.Body[2].([]string)
|
||||
for _, name := range invalidated {
|
||||
if ratingProperties[name] {
|
||||
return signal{Rating: true}, true
|
||||
}
|
||||
}
|
||||
case s.Path == login1Path && s.Name == login1Iface+".SessionNew" && len(s.Body) >= 2:
|
||||
if path, ok := s.Body[1].(dbus.ObjectPath); ok {
|
||||
return signal{Session: string(path)}, true
|
||||
}
|
||||
}
|
||||
return signal{}, false
|
||||
}
|
||||
|
||||
func (d *dbusSystem) rating() (Rating, error) {
|
||||
nm := d.conn.Object(nmName, nmPath)
|
||||
var r Rating
|
||||
for name, into := range map[string]any{"Connectivity": &r.Connectivity, "ConnectivityCheckEnabled": &r.CheckEnabled,
|
||||
"ConnectivityCheckAvailable": &r.CheckAvailable, "ConnectivityCheckUri": &r.CheckURI} {
|
||||
v, err := nm.GetProperty(nmName + "." + name)
|
||||
if err != nil {
|
||||
return Rating{}, fmt.Errorf("the network manager's %s: %w", name, err)
|
||||
}
|
||||
if err := v.Store(into); err != nil {
|
||||
return Rating{}, fmt.Errorf("the network manager's %s: %w", name, err)
|
||||
}
|
||||
}
|
||||
v, err := nm.GetProperty(nmName + ".PrimaryConnection")
|
||||
if err != nil {
|
||||
return Rating{}, fmt.Errorf("the network manager's PrimaryConnection: %w", err)
|
||||
}
|
||||
path, _ := v.Value().(dbus.ObjectPath)
|
||||
r.Connection = string(path)
|
||||
if !r.active() {
|
||||
return r, nil
|
||||
}
|
||||
// A connection that went between the two reads is read as it is said: its name and link empty.
|
||||
active := d.conn.Object(nmName, path)
|
||||
if v, err := active.GetProperty(nmActive + ".Id"); err == nil {
|
||||
r.Network, _ = v.Value().(string)
|
||||
}
|
||||
if v, err := active.GetProperty(nmActive + ".Devices"); err == nil {
|
||||
if devices, _ := v.Value().([]dbus.ObjectPath); len(devices) > 0 {
|
||||
if v, err := d.conn.Object(nmName, devices[0]).GetProperty(nmDevice + ".Interface"); err == nil {
|
||||
r.Link, _ = v.Value().(string)
|
||||
}
|
||||
}
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// systemSocketThere fires once the system bus's socket is there: at once when it is, else when it is made in its
|
||||
// folder (a file event of the kernel). Closing stop ends the wait.
|
||||
func systemSocketThere(stop <-chan struct{}) <-chan struct{} {
|
||||
return fileThere(systemSocket, stop)
|
||||
}
|
||||
|
||||
// fileThere fires once path is there: at once when it is, else at the file event that makes it in its folder.
|
||||
func fileThere(path string, stop <-chan struct{}) <-chan struct{} {
|
||||
out := make(chan struct{})
|
||||
fd, err := unix.InotifyInit1(unix.IN_CLOEXEC | unix.IN_NONBLOCK)
|
||||
if err != nil {
|
||||
journal("watching %s failed: %v", filepath.Dir(path), err)
|
||||
return out
|
||||
}
|
||||
if _, err := unix.InotifyAddWatch(fd, filepath.Dir(path), unix.IN_CREATE|unix.IN_MOVED_TO); err != nil {
|
||||
journal("watching %s failed: %v", filepath.Dir(path), err)
|
||||
unix.Close(fd)
|
||||
return out
|
||||
}
|
||||
f := os.NewFile(uintptr(fd), "inotify")
|
||||
go func() {
|
||||
<-stop
|
||||
f.Close()
|
||||
}()
|
||||
go func() {
|
||||
defer f.Close()
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
sendOnce(out, stop)
|
||||
return
|
||||
}
|
||||
for {
|
||||
names, err := readInotify(f)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, name := range names {
|
||||
if name == filepath.Base(path) {
|
||||
sendOnce(out, stop)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
return out
|
||||
}
|
||||
|
||||
func sendOnce(out chan<- struct{}, stop <-chan struct{}) {
|
||||
select {
|
||||
case out <- struct{}{}:
|
||||
case <-stop:
|
||||
}
|
||||
}
|
||||
|
||||
// readInotify reads the names of one read's file events; an error once the descriptor is closed.
|
||||
func readInotify(f *os.File) ([]string, error) {
|
||||
buf := make([]byte, 64*(unix.SizeofInotifyEvent+unix.NAME_MAX+1))
|
||||
n, err := f.Read(buf)
|
||||
if err != nil {
|
||||
if !errors.Is(err, os.ErrClosed) {
|
||||
journal("a watch of file events failed: %v", err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
var names []string
|
||||
for off := 0; off+unix.SizeofInotifyEvent <= n; {
|
||||
ev := (*unix.InotifyEvent)(unsafe.Pointer(&buf[off]))
|
||||
nameAt := off + unix.SizeofInotifyEvent
|
||||
end := nameAt + int(ev.Len)
|
||||
if end > n {
|
||||
break
|
||||
}
|
||||
name := buf[nameAt:end]
|
||||
for i, b := range name {
|
||||
if b == 0 {
|
||||
name = name[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
names = append(names, string(name))
|
||||
off = end
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
Reference in New Issue
Block a user