package node import ( "context" "encoding/json" "os/exec" "strings" "sync" "time" ) const powerSchema = "missioncore.node-power/v1" type powerCommand struct { Schema string `json:"schema"` ID string `json:"id"` BindingID string `json:"binding_id"` Epoch string `json:"epoch"` Action string `json:"action"` ExpiresAt float64 `json:"expires_at"` } type powerResult struct { ID string `json:"id"` State string `json:"state"` } // NodePower has no durable queue. A new process has a new epoch, so an old // confirmation cannot shut down a restarted or rebound board. type NodePower struct { mu sync.Mutex epoch string command powerCommand result powerResult seen map[string]bool now func() time.Time run func(context.Context, string) ([]byte, error) } func NewNodePower() *NodePower { return &NodePower{epoch: token(), seen: map[string]bool{}, now: time.Now, run: loginPower} } func loginPower(ctx context.Context, method string) ([]byte, error) { // Fixed D-Bus methods only; never a shell, arbitrary unit or user-supplied argv. args := []string{"--system", "--timeout=3", "call", "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", method} if method == "PowerOff" { args = append(args, "b", "false") } return exec.CommandContext(ctx, "/usr/bin/busctl", args...).Output() } func (p *NodePower) Snapshot(ctx context.Context) any { call, cancel := context.WithTimeout(ctx, 3*time.Second) defer cancel() out, err := p.run(call, "CanPowerOff") p.mu.Lock() defer p.mu.Unlock() p.expire() return map[string]any{"schema": powerSchema, "epoch": p.epoch, "available": err == nil && strings.TrimSpace(string(out)) == `s "yes"` && len(p.seen) < 64 && p.result.State != "accepted" && p.result.State != "executing" && p.result.State != "submitted", "result": p.result} } func (p *NodePower) expire() { if p.result.State == "accepted" && float64(p.now().UnixNano())/1e9 >= p.command.ExpiresAt { p.result.State = "expired" } } // Called while Pairing.mu still proves this heartbeat belongs to the current // owner. Acceptance is reported on the next heartbeat; only its matching ack // can execute the command. Lost replies and repeated requests never execute twice. func (p *NodePower) Accept(ctx context.Context, binding string, raw, ackRaw json.RawMessage) { p.mu.Lock() defer p.mu.Unlock() p.expire() var ack string if json.Unmarshal(ackRaw, &ack) == nil && ack == p.result.ID && p.result.State == "accepted" && binding == p.command.BindingID { p.result.State = "executing" go func() { call, cancel := context.WithTimeout(ctx, 4*time.Second) defer cancel() _, err := p.run(call, "PowerOff") p.mu.Lock() defer p.mu.Unlock() if err != nil { p.result.State = "failed" } else { p.result.State = "submitted" } }() return } var c powerCommand if json.Unmarshal(raw, &c) != nil || c.Schema != powerSchema || c.Action != "poweroff" || c.BindingID != binding || c.Epoch != p.epoch || len(c.ID) != 36 || p.seen[c.ID] { return } if p.result.State == "accepted" || p.result.State == "executing" || p.result.State == "submitted" || len(p.seen) >= 64 { return } now := float64(p.now().UnixNano()) / 1e9 p.seen[c.ID] = true p.command = c p.result = powerResult{ID: c.ID, State: "accepted"} if c.ExpiresAt <= now || c.ExpiresAt > now+30 { p.result.State = "expired" } }