2024-09-09 03:11:50 +09:00
|
|
|
package dbus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-09-29 15:27:29 +09:00
|
|
|
"io"
|
2024-10-07 12:48:20 +09:00
|
|
|
"strconv"
|
2024-09-25 01:17:38 +09:00
|
|
|
|
|
|
|
"git.ophivana.moe/cat/fortify/helper"
|
2024-09-09 03:11:50 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// Start launches the D-Bus proxy and sets up the Wait method.
|
|
|
|
// ready should be buffered and should only be received from once.
|
2024-09-29 15:27:29 +09:00
|
|
|
func (p *Proxy) Start(ready chan error, output io.Writer) error {
|
2024-09-09 03:11:50 +09:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
|
|
|
if p.seal == nil {
|
|
|
|
return errors.New("proxy not sealed")
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:55:27 +09:00
|
|
|
h := helper.New(p.seal, p.name,
|
2024-10-07 12:48:20 +09:00
|
|
|
func(argsFD, statFD int) []string {
|
|
|
|
if statFD == -1 {
|
|
|
|
return []string{"--args=" + strconv.Itoa(argsFD)}
|
|
|
|
} else {
|
|
|
|
return []string{"--args=" + strconv.Itoa(argsFD), "--fd=" + strconv.Itoa(statFD)}
|
|
|
|
}
|
|
|
|
},
|
2024-09-09 03:11:50 +09:00
|
|
|
)
|
2024-10-07 15:37:52 +09:00
|
|
|
cmd := h.Unwrap()
|
2024-09-25 01:17:38 +09:00
|
|
|
// xdg-dbus-proxy does not need to inherit the environment
|
2024-10-07 15:37:52 +09:00
|
|
|
cmd.Env = []string{}
|
2024-09-09 03:11:50 +09:00
|
|
|
|
2024-09-29 15:27:29 +09:00
|
|
|
if output != nil {
|
2024-10-07 15:37:52 +09:00
|
|
|
cmd.Stdout = output
|
|
|
|
cmd.Stderr = output
|
2024-09-29 15:24:54 +09:00
|
|
|
}
|
2024-09-25 01:17:38 +09:00
|
|
|
if err := h.StartNotify(ready); err != nil {
|
2024-09-09 03:11:50 +09:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-25 01:17:38 +09:00
|
|
|
p.helper = h
|
2024-09-09 03:11:50 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait waits for xdg-dbus-proxy to exit or fault.
|
|
|
|
func (p *Proxy) Wait() error {
|
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
|
2024-09-25 01:17:38 +09:00
|
|
|
if p.helper == nil {
|
|
|
|
return errors.New("proxy not started")
|
2024-09-09 03:11:50 +09:00
|
|
|
}
|
|
|
|
|
2024-09-25 01:17:38 +09:00
|
|
|
return p.helper.Wait()
|
2024-09-09 03:11:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the status file descriptor passed to xdg-dbus-proxy, causing it to stop.
|
|
|
|
func (p *Proxy) Close() error {
|
2024-09-25 01:17:38 +09:00
|
|
|
return p.helper.Close()
|
2024-09-09 03:11:50 +09:00
|
|
|
}
|