aboutsummaryrefslogtreecommitdiffhomepage
path: root/dbus/dbus.go
diff options
context:
space:
mode:
authorOphestra Umiker <cat@ophivana.moe>2024-09-24 16:11:08 +0900
committerOphestra Umiker <cat@ophivana.moe>2024-09-24 16:11:08 +0900
commit000607da5fca0e101cb43db9b1c1b466e3292d35 (patch)
tree3dcf1cdbf086bca5883964324442de42e17776bb /dbus/dbus.go
parent1cb90c0840e6d2bdef9ed8b9cb5ae54fc1b6c91c (diff)
helper: separate helper args fd builder from dbus
This method of passing arguments is used in bubblewrap as well as other tools, this commit separates the argument builder/writer to the helper package and generalise it as an interface. Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
Diffstat (limited to 'dbus/dbus.go')
-rw-r--r--dbus/dbus.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/dbus/dbus.go b/dbus/dbus.go
new file mode 100644
index 00000000..d0e82e17
--- /dev/null
+++ b/dbus/dbus.go
@@ -0,0 +1,84 @@
+package dbus
+
+import (
+ "errors"
+ "os"
+ "os/exec"
+ "sync"
+
+ "git.ophivana.moe/cat/fortify/helper"
+)
+
+// 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
+ session [2]string
+ system [2]string
+
+ wait *chan error
+ read *chan error
+ ready *chan bool
+
+ seal helper.Args
+ lock sync.RWMutex
+}
+
+func (p *Proxy) String() string {
+ if p == nil {
+ return "(invalid dbus proxy)"
+ }
+
+ p.lock.RLock()
+ defer p.lock.RUnlock()
+
+ if p.cmd != nil {
+ return p.cmd.String()
+ }
+
+ if p.seal != nil {
+ return p.seal.String()
+ }
+
+ return "(unsealed dbus proxy)"
+}
+
+// Seal seals the Proxy instance.
+func (p *Proxy) Seal(session, system *Config) error {
+ p.lock.Lock()
+ defer p.lock.Unlock()
+
+ if p.seal != nil {
+ panic("dbus proxy sealed twice")
+ }
+
+ if session == nil && system == nil {
+ return errors.New("no configuration to seal")
+ }
+
+ seal := helper.NewArgs()
+
+ var args []string
+ if session != nil {
+ args = append(args, session.Args(p.session)...)
+ }
+ if system != nil {
+ args = append(args, system.Args(p.system)...)
+ }
+ if err := seal.Seal(args); err != nil {
+ return err
+ }
+
+ p.seal = seal
+ return nil
+}
+
+// New returns a reference to a new unsealed Proxy.
+func New(binPath string, session, system [2]string) *Proxy {
+ return &Proxy{path: binPath, session: session, system: system}
+}