aboutsummaryrefslogtreecommitdiffhomepage
path: root/dbus/setup.go
blob: 8fb7b1b5d252d4a165f853bf75c16d2b5ff29d65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package dbus

import (
	"errors"
	"os"
	"os/exec"
	"strings"
	"sync"
)

// 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

	address [2]string
	path    string

	wait  *chan error
	read  *chan error
	ready *chan bool

	seal *string
	lock sync.RWMutex
}

func (p *Proxy) String() string {
	if p.cmd != nil {
		return p.cmd.String()
	}

	if p.seal != nil {
		return *p.seal
	}

	return "(unsealed dbus proxy)"
}

// Seal seals the Proxy instance.
func (p *Proxy) Seal(c *Config) error {
	p.lock.Lock()
	defer p.lock.Unlock()

	if p.seal != nil {
		panic("dbus proxy sealed twice")
	}
	args := c.Args(p.address[0], p.address[1])

	seal := strings.Builder{}
	for _, arg := range args {
		// reject argument strings containing null
		for _, b := range arg {
			if b == '\x00' {
				return errors.New("argument contains null")
			}
		}

		// write null terminated argument
		seal.WriteString(arg)
		seal.WriteByte('\x00')
	}
	v := seal.String()
	p.seal = &v
	return nil
}

// New returns a reference to a new unsealed Proxy.
func New(binPath, address, path string) *Proxy {
	return &Proxy{path: binPath, address: [2]string{address, path}}
}