diff options
| author | Ophestra Umiker <cat@ophivana.moe> | 2024-09-22 00:29:36 +0900 |
|---|---|---|
| committer | Ophestra Umiker <cat@ophivana.moe> | 2024-09-22 01:15:39 +0900 |
| commit | 62cb8a91b603546314034ded1e9d6ddfbd6ef106 (patch) | |
| tree | 981d0c6daf04595e0118f8a48c0898cdd6d44e05 /internal/app/pulse.go | |
| parent | 11832a937924030699a6304a3ba9b5884532b730 (diff) | |
app: clean up interactions and handle all application state and setup/teardown
There was an earlier attempt of cleaning up the app package however it ended up creating even more of a mess and the code structure largely still looked like Ego with state setup scattered everywhere and a bunch of ugly hacks had to be implemented to keep track of all of them. In this commit the entire app package is rewritten to track everything that has to do with an app in one thread safe value.
In anticipation of the client/server split also made changes:
- Console messages are cleaned up to be consistent
- State tracking is fully rewritten to be cleaner and usable for multiple process and client/server
- Encapsulate errors to easier identify type of action causing the error as well as additional info
- System-level setup operations is grouped in a way that can be collectively committed/reverted
and gracefully handles errors returned by each operation
- Resource sharing is made more fine-grained with PID-scoped resources whenever possible,
a few remnants (X11, Wayland, PulseAudio) will be addressed when a generic proxy is available
- Application setup takes a JSON-friendly config struct and deterministically generates system setup operations
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
Diffstat (limited to 'internal/app/pulse.go')
| -rw-r--r-- | internal/app/pulse.go | 109 |
1 files changed, 0 insertions, 109 deletions
diff --git a/internal/app/pulse.go b/internal/app/pulse.go deleted file mode 100644 index 8b4921ed..00000000 --- a/internal/app/pulse.go +++ /dev/null @@ -1,109 +0,0 @@ -package app - -import ( - "errors" - "fmt" - "io/fs" - "os" - "path" - - "git.ophivana.moe/cat/fortify/acl" - "git.ophivana.moe/cat/fortify/internal" - "git.ophivana.moe/cat/fortify/internal/util" - "git.ophivana.moe/cat/fortify/internal/verbose" -) - -const ( - pulseServer = "PULSE_SERVER" - pulseCookie = "PULSE_COOKIE" - - home = "HOME" - xdgConfigHome = "XDG_CONFIG_HOME" -) - -func (a *App) SharePulse() { - a.setEnablement(internal.EnablePulse) - - // ensure PulseAudio directory ACL (e.g. `/run/user/%d/pulse`) - pulse := path.Join(a.runtimePath, "pulse") - pulseS := path.Join(pulse, "native") - if s, err := os.Stat(pulse); err != nil { - if !errors.Is(err, fs.ErrNotExist) { - internal.Fatal("Error accessing PulseAudio directory:", err) - } - internal.Fatal(fmt.Sprintf("PulseAudio dir '%s' not found", pulse)) - } else { - // add environment variable for new process - a.AppendEnv(pulseServer, "unix:"+pulseS) - if err = acl.UpdatePerm(pulse, a.UID(), acl.Execute); err != nil { - internal.Fatal("Error preparing PulseAudio:", err) - } else { - a.exit.RegisterRevertPath(pulse) - } - - // ensure PulseAudio socket permission (e.g. `/run/user/%d/pulse/native`) - if s, err = os.Stat(pulseS); err != nil { - if errors.Is(err, fs.ErrNotExist) { - internal.Fatal("PulseAudio directory found but socket does not exist") - } - internal.Fatal("Error accessing PulseAudio socket:", err) - } else { - if m := s.Mode(); m&0o006 != 0o006 { - internal.Fatal(fmt.Sprintf("Unexpected permissions on '%s':", pulseS), m) - } - } - - // Publish current user's pulse-cookie for target user - pulseCookieSource := discoverPulseCookie() - pulseCookieFinal := path.Join(a.sharePath, "pulse-cookie") - a.AppendEnv(pulseCookie, pulseCookieFinal) - verbose.Printf("Publishing PulseAudio cookie '%s' to '%s'\n", pulseCookieSource, pulseCookieFinal) - if err = util.CopyFile(pulseCookieFinal, pulseCookieSource); err != nil { - internal.Fatal("Error copying PulseAudio cookie:", err) - } - if err = acl.UpdatePerm(pulseCookieFinal, a.UID(), acl.Read); err != nil { - internal.Fatal("Error publishing PulseAudio cookie:", err) - } else { - a.exit.RegisterRevertPath(pulseCookieFinal) - } - - verbose.Printf("PulseAudio dir '%s' configured\n", pulse) - } -} - -// discoverPulseCookie try various standard methods to discover the current user's PulseAudio authentication cookie -func discoverPulseCookie() string { - if p, ok := os.LookupEnv(pulseCookie); ok { - return p - } - - if p, ok := os.LookupEnv(home); ok { - p = path.Join(p, ".pulse-cookie") - if s, err := os.Stat(p); err != nil { - if !errors.Is(err, fs.ErrNotExist) { - internal.Fatal("Error accessing PulseAudio cookie:", err) - // unreachable - return p - } - } else if !s.IsDir() { - return p - } - } - - if p, ok := os.LookupEnv(xdgConfigHome); ok { - p = path.Join(p, "pulse", "cookie") - if s, err := os.Stat(p); err != nil { - if !errors.Is(err, fs.ErrNotExist) { - internal.Fatal("Error accessing PulseAudio cookie:", err) - // unreachable - return p - } - } else if !s.IsDir() { - return p - } - } - - internal.Fatal(fmt.Sprintf("Cannot locate PulseAudio cookie (tried $%s, $%s/pulse/cookie, $%s/.pulse-cookie)", - pulseCookie, xdgConfigHome, home)) - return "" -} |
