2024-09-09 03:11:50 +09:00
|
|
|
package dbus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-09-24 18:02:56 +09:00
|
|
|
"io"
|
2024-09-09 03:11:50 +09:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"sync"
|
2024-09-24 16:11:08 +09:00
|
|
|
|
|
|
|
"git.ophivana.moe/cat/fortify/helper"
|
2024-09-09 03:11:50 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// Proxy holds references to a xdg-dbus-proxy process, and should never be copied.
|
|
|
|
// Once sealed, configuration changes will no longer be possible and attempting to do so will result in a panic.
|
|
|
|
type Proxy struct {
|
|
|
|
cmd *exec.Cmd
|
|
|
|
|
|
|
|
statP [2]*os.File
|
|
|
|
argsP [2]*os.File
|
|
|
|
|
|
|
|
path string
|
2024-09-09 21:13:00 +09:00
|
|
|
session [2]string
|
|
|
|
system [2]string
|
2024-09-09 03:11:50 +09:00
|
|
|
|
|
|
|
wait *chan error
|
|
|
|
read *chan error
|
|
|
|
ready *chan bool
|
|
|
|
|
2024-09-24 18:02:56 +09:00
|
|
|
seal io.WriterTo
|
2024-09-09 03:11:50 +09:00
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Proxy) String() string {
|
2024-09-09 21:13:00 +09:00
|
|
|
if p == nil {
|
|
|
|
return "(invalid dbus proxy)"
|
|
|
|
}
|
|
|
|
|
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
|
2024-09-09 03:11:50 +09:00
|
|
|
if p.cmd != nil {
|
|
|
|
return p.cmd.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.seal != nil {
|
2024-09-24 18:02:56 +09:00
|
|
|
return p.seal.(helper.Args).String()
|
2024-09-09 03:11:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return "(unsealed dbus proxy)"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seal seals the Proxy instance.
|
2024-09-09 21:13:00 +09:00
|
|
|
func (p *Proxy) Seal(session, system *Config) error {
|
2024-09-09 03:11:50 +09:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
|
|
|
if p.seal != nil {
|
|
|
|
panic("dbus proxy sealed twice")
|
|
|
|
}
|
2024-09-09 21:13:00 +09:00
|
|
|
|
|
|
|
if session == nil && system == nil {
|
|
|
|
return errors.New("no configuration to seal")
|
|
|
|
}
|
2024-09-09 03:11:50 +09:00
|
|
|
|
2024-09-24 16:11:08 +09:00
|
|
|
seal := helper.NewArgs()
|
2024-09-09 03:11:50 +09:00
|
|
|
|
2024-09-24 16:11:08 +09:00
|
|
|
var args []string
|
2024-09-09 21:13:00 +09:00
|
|
|
if session != nil {
|
2024-09-24 16:11:08 +09:00
|
|
|
args = append(args, session.Args(p.session)...)
|
2024-09-09 03:11:50 +09:00
|
|
|
}
|
2024-09-09 21:13:00 +09:00
|
|
|
if system != nil {
|
2024-09-24 16:11:08 +09:00
|
|
|
args = append(args, system.Args(p.system)...)
|
|
|
|
}
|
|
|
|
if err := seal.Seal(args); err != nil {
|
|
|
|
return err
|
2024-09-09 21:13:00 +09:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:11:08 +09:00
|
|
|
p.seal = seal
|
2024-09-09 03:11:50 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a reference to a new unsealed Proxy.
|
2024-09-09 21:13:00 +09:00
|
|
|
func New(binPath string, session, system [2]string) *Proxy {
|
|
|
|
return &Proxy{path: binPath, session: session, system: system}
|
2024-09-09 03:11:50 +09:00
|
|
|
}
|