diff options
| author | Ophestra Umiker <cat@ophivana.moe> | 2024-09-09 03:16:54 +0900 |
|---|---|---|
| committer | Ophestra Umiker <cat@ophivana.moe> | 2024-09-09 03:16:54 +0900 |
| commit | 38ef2b4d0caf48a5f6bb9d472b76764cbf14d23d (patch) | |
| tree | b419ee1d5b700feb43dd061ba336c6fd7f0ccd59 /internal | |
| parent | 357cc4ce4d2888033a8163fe39861c8a88301da6 (diff) | |
app/dbus: manage dbus proxy and pass address to child
This commit adds code that starts and registers the D-Bus proxy, as well as cleanup code that tracks and closes the daemon once our child exits. A few more flags were added to pass D-Bus config to xdg-dbus-proxy.
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/app/dbus.go | 84 | ||||
| -rw-r--r-- | internal/app/run.go | 3 | ||||
| -rw-r--r-- | internal/state/exit.go | 19 | ||||
| -rw-r--r-- | internal/state/register.go | 10 |
4 files changed, 113 insertions, 3 deletions
diff --git a/internal/app/dbus.go b/internal/app/dbus.go index 89394f15..651c0c73 100644 --- a/internal/app/dbus.go +++ b/internal/app/dbus.go @@ -1,14 +1,92 @@ package app import ( + "errors" "fmt" + "os" + "path" + "strconv" + "git.ophivana.moe/cat/fortify/dbus" + "git.ophivana.moe/cat/fortify/internal/acl" "git.ophivana.moe/cat/fortify/internal/state" + "git.ophivana.moe/cat/fortify/internal/system" + "git.ophivana.moe/cat/fortify/internal/util" ) -func (a *App) ShareDBus() { +const dbusSessionBusAddress = "DBUS_SESSION_BUS_ADDRESS" + +var dbusAddress string + +func (a *App) ShareDBus(c *dbus.Config) { a.setEnablement(state.EnableDBus) - // TODO: start xdg-dbus-proxy - fmt.Println("warn: dbus proxy not implemented") + var binPath, address string + target := path.Join(system.V.Share, strconv.Itoa(os.Getpid())) + + if b, ok := util.Which("xdg-dbus-proxy"); !ok { + state.Fatal("D-Bus: Did not find 'xdg-dbus-proxy' in PATH") + } else { + binPath = b + } + + if addr, ok := os.LookupEnv(dbusSessionBusAddress); !ok { + state.Fatal("D-Bus: DBUS_SESSION_BUS_ADDRESS not set") + } else { + address = addr + } + + c.Log = system.V.Verbose + p := dbus.New(binPath, address, target) + if system.V.Verbose { + fmt.Println("D-Bus: sealing proxy", c.Args(address, target)) + } + if err := p.Seal(c); err != nil { + state.Fatal("D-Bus: invalid config when sealing proxy,", err) + } + + ready := make(chan bool, 1) + done := make(chan struct{}) + + if system.V.Verbose { + fmt.Printf("Starting session bus proxy '%s' for address '%s'\n", dbusAddress, address) + } + if err := p.Start(&ready); err != nil { + state.Fatal("D-Bus: error starting proxy,", err) + } + if system.V.Verbose { + fmt.Println("D-Bus proxy launch:", p) + } + + go func() { + if err := p.Wait(); err != nil { + fmt.Println("warn: D-Bus proxy returned error,", err) + } else { + if system.V.Verbose { + fmt.Println("D-Bus proxy uneventful wait") + } + } + if err := os.Remove(target); err != nil && !errors.Is(err, os.ErrNotExist) { + fmt.Println("Error removing dangling D-Bus socket:", err) + } + done <- struct{}{} + }() + + // register early to enable Fatal cleanup + state.RegisterDBus(p, &done) + dbusAddress = "unix:path=" + target + + if !<-ready { + state.Fatal("D-Bus: proxy did not start correctly") + } + + a.AppendEnv(dbusSessionBusAddress, dbusAddress) + if err := acl.UpdatePerm(target, a.UID(), acl.Read, acl.Write); err != nil { + state.Fatal(fmt.Sprintf("Error preparing D-Bus proxy '%s':", dbusAddress), err) + } else { + state.RegisterRevertPath(target) + } + if system.V.Verbose { + fmt.Printf("Session bus proxy '%s' for address '%s' configured\n", dbusAddress, address) + } } diff --git a/internal/app/run.go b/internal/app/run.go index 46e0270d..4e4e578d 100644 --- a/internal/app/run.go +++ b/internal/app/run.go @@ -166,6 +166,9 @@ func (a *App) commandBuilderMachineCtl() (args []string) { if executable, err := os.Executable(); err != nil { state.Fatal("Error reading executable path:", err) } else { + if a.enablements.Has(state.EnableDBus) { + innerCommand.WriteString(dbusSessionBusAddress + "=" + "'" + dbusAddress + "' ") + } innerCommand.WriteString("exec " + executable + " -V") } args = append(args, innerCommand.String()) diff --git a/internal/state/exit.go b/internal/state/exit.go index 3da1f48e..4ba0a9d8 100644 --- a/internal/state/exit.go +++ b/internal/state/exit.go @@ -65,4 +65,23 @@ func BeforeExit() { fmt.Printf("Stripped ACL entry for user '%s' from '%s'\n", u.Username, candidate) } } + + if dbusProxy != nil { + if system.V.Verbose { + fmt.Println("D-Bus proxy registered, cleaning up") + } + + if err := dbusProxy.Close(); err != nil { + if errors.Is(err, os.ErrClosed) { + if system.V.Verbose { + fmt.Println("D-Bus proxy already closed") + } + } else { + fmt.Println("Error closing D-Bus proxy:", err) + } + } + + // wait for Proxy.Wait to return + <-*dbusDone + } } diff --git a/internal/state/register.go b/internal/state/register.go index ac502ae8..be9ce540 100644 --- a/internal/state/register.go +++ b/internal/state/register.go @@ -1,9 +1,14 @@ package state +import "git.ophivana.moe/cat/fortify/dbus" + var ( cleanupCandidate []string enablements *Enablements xcbActionComplete bool + + dbusProxy *dbus.Proxy + dbusDone *chan struct{} ) func RegisterRevertPath(p string) { @@ -23,3 +28,8 @@ func XcbActionComplete() { } xcbActionComplete = true } + +func RegisterDBus(p *dbus.Proxy, done *chan struct{}) { + dbusProxy = p + dbusDone = done +} |
