From da6d238d8a15fa583b384b547decbddcda28ff8b Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Thu, 12 Sep 2024 21:07:05 +0900 Subject: [PATCH] verbose: remove system package interaction Signed-off-by: Ophestra Umiker --- internal/app/dbus.go | 47 +++++++++++++------------------------ internal/app/pulse.go | 9 +++---- internal/app/run.go | 15 ++++-------- internal/app/setup.go | 14 ++++------- internal/app/wayland.go | 5 ++-- internal/app/x.go | 6 ++--- internal/state/exit.go | 30 +++++++---------------- internal/state/print.go | 13 ++++------ internal/system/retrieve.go | 4 ++-- internal/system/value.go | 1 - internal/verbose/print.go | 15 ++++++++++++ internal/verbose/state.go | 13 ++++++++++ main.go | 20 +++++++--------- 13 files changed, 85 insertions(+), 107 deletions(-) create mode 100644 internal/verbose/print.go create mode 100644 internal/verbose/state.go diff --git a/internal/app/dbus.go b/internal/app/dbus.go index 44a7f91..60cf3f5 100644 --- a/internal/app/dbus.go +++ b/internal/app/dbus.go @@ -12,6 +12,7 @@ import ( "git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/system" "git.ophivana.moe/cat/fortify/internal/util" + "git.ophivana.moe/cat/fortify/internal/verbose" ) const ( @@ -24,7 +25,7 @@ var ( dbusSystem bool ) -func (a *App) ShareDBus(dse, dsg *dbus.Config, verbose bool) { +func (a *App) ShareDBus(dse, dsg *dbus.Config, log bool) { a.setEnablement(state.EnableDBus) dbusSystem = dsg != nil @@ -46,18 +47,14 @@ func (a *App) ShareDBus(dse, dsg *dbus.Config, verbose bool) { } if addr, ok := os.LookupEnv(dbusSessionBusAddress); !ok { - if system.V.Verbose { - fmt.Println("D-Bus: DBUS_SESSION_BUS_ADDRESS not set, assuming default format") - } + verbose.Println("D-Bus: DBUS_SESSION_BUS_ADDRESS not set, assuming default format") sessionBus[0] = fmt.Sprintf("unix:path=/run/user/%d/bus", os.Getuid()) } else { sessionBus[0] = addr } if addr, ok := os.LookupEnv(dbusSystemBusAddress); !ok { - if system.V.Verbose { - fmt.Println("D-Bus: DBUS_SYSTEM_BUS_ADDRESS not set, assuming default format") - } + verbose.Println("D-Bus: DBUS_SYSTEM_BUS_ADDRESS not set, assuming default format") systemBus[0] = "unix:path=/run/dbus/system_bus_socket" } else { systemBus[0] = addr @@ -65,15 +62,11 @@ func (a *App) ShareDBus(dse, dsg *dbus.Config, verbose bool) { p := dbus.New(binPath, sessionBus, systemBus) - dse.Log = verbose - if system.V.Verbose { - fmt.Println("D-Bus: sealing session proxy", dse.Args(sessionBus)) - } + dse.Log = log + verbose.Println("D-Bus: sealing session proxy", dse.Args(sessionBus)) if dsg != nil { - dsg.Log = verbose - if system.V.Verbose { - fmt.Println("D-Bus: sealing system proxy", dsg.Args(systemBus)) - } + dsg.Log = log + verbose.Println("D-Bus: sealing system proxy", dsg.Args(systemBus)) } if err := p.Seal(dse, dsg); err != nil { state.Fatal("D-Bus: invalid config when sealing proxy,", err) @@ -82,26 +75,20 @@ func (a *App) ShareDBus(dse, dsg *dbus.Config, verbose bool) { 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[0], sessionBus[0]) - if dsg != nil { - fmt.Printf("Starting system bus proxy '%s' for address '%s'\n", dbusAddress[1], systemBus[0]) - } + verbose.Printf("Starting session bus proxy '%s' for address '%s'\n", dbusAddress[0], sessionBus[0]) + if dsg != nil { + verbose.Printf("Starting system bus proxy '%s' for address '%s'\n", dbusAddress[1], systemBus[0]) } 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) - } + verbose.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") - } + verbose.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) @@ -130,10 +117,8 @@ func (a *App) ShareDBus(dse, dsg *dbus.Config, verbose bool) { state.RegisterRevertPath(systemBus[1]) } } - if system.V.Verbose { - fmt.Printf("Session bus proxy '%s' for address '%s' configured\n", dbusAddress[0], sessionBus[0]) - if dsg != nil { - fmt.Printf("System bus proxy '%s' for address '%s' configured\n", dbusAddress[1], systemBus[0]) - } + verbose.Printf("Session bus proxy '%s' for address '%s' configured\n", dbusAddress[0], sessionBus[0]) + if dsg != nil { + verbose.Printf("System bus proxy '%s' for address '%s' configured\n", dbusAddress[1], systemBus[0]) } } diff --git a/internal/app/pulse.go b/internal/app/pulse.go index 848ac71..d7e08ba 100644 --- a/internal/app/pulse.go +++ b/internal/app/pulse.go @@ -11,6 +11,7 @@ import ( "git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/system" "git.ophivana.moe/cat/fortify/internal/util" + "git.ophivana.moe/cat/fortify/internal/verbose" ) func (a *App) SharePulse() { @@ -49,9 +50,7 @@ func (a *App) SharePulse() { pulseCookieSource := util.DiscoverPulseCookie() pulseCookieFinal := path.Join(system.V.Share, "pulse-cookie") a.AppendEnv(util.PulseCookie, pulseCookieFinal) - if system.V.Verbose { - fmt.Printf("Publishing PulseAudio cookie '%s' to '%s'\n", pulseCookieSource, pulseCookieFinal) - } + verbose.Printf("Publishing PulseAudio cookie '%s' to '%s'\n", pulseCookieSource, pulseCookieFinal) if err = util.CopyFile(pulseCookieFinal, pulseCookieSource); err != nil { state.Fatal("Error copying PulseAudio cookie:", err) } @@ -61,8 +60,6 @@ func (a *App) SharePulse() { state.RegisterRevertPath(pulseCookieFinal) } - if system.V.Verbose { - fmt.Printf("PulseAudio dir '%s' configured\n", pulse) - } + verbose.Printf("PulseAudio dir '%s' configured\n", pulse) } } diff --git a/internal/app/run.go b/internal/app/run.go index 0871e89..53548d7 100644 --- a/internal/app/run.go +++ b/internal/app/run.go @@ -10,6 +10,7 @@ import ( "git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/system" "git.ophivana.moe/cat/fortify/internal/util" + "git.ophivana.moe/cat/fortify/internal/verbose" ) const ( @@ -48,9 +49,7 @@ func (a *App) Run() { cmd.Stderr = os.Stderr cmd.Dir = system.V.RunDir - if system.V.Verbose { - fmt.Println("Executing:", cmd) - } + verbose.Println("Executing:", cmd) if err := cmd.Start(); err != nil { state.Fatal("Error starting process:", err) @@ -71,9 +70,7 @@ func (a *App) Run() { } } - if system.V.Verbose { - fmt.Println("Process exited with exit code", r) - } + verbose.Println("Process exited with exit code", r) state.BeforeExit() os.Exit(r) } @@ -86,9 +83,7 @@ func (a *App) commandBuilderSudo() (args []string) { // -A? if _, ok := os.LookupEnv(sudoAskPass); ok { - if system.V.Verbose { - fmt.Printf("%s set, adding askpass flag\n", sudoAskPass) - } + verbose.Printf("%s set, adding askpass flag\n", sudoAskPass) args = append(args, "-A") } @@ -115,7 +110,7 @@ func (a *App) commandBuilderMachineCtl() (args []string) { args = append(args, "shell", "--uid="+a.Username) // --quiet - if !system.V.Verbose { + if !verbose.Get() { args = append(args, "--quiet") } diff --git a/internal/app/setup.go b/internal/app/setup.go index d8378cf..c6b4461 100644 --- a/internal/app/setup.go +++ b/internal/app/setup.go @@ -8,8 +8,8 @@ import ( "strconv" "git.ophivana.moe/cat/fortify/internal/state" - "git.ophivana.moe/cat/fortify/internal/system" "git.ophivana.moe/cat/fortify/internal/util" + "git.ophivana.moe/cat/fortify/internal/verbose" ) type App struct { @@ -65,11 +65,9 @@ func New(userName string, args []string, launchOptionText string) *App { a.uid = u } - if system.V.Verbose { - fmt.Println("Running as user", a.Username, "("+a.Uid+"),", "command:", a.command) - if util.SdBootedV { - fmt.Println("System booted with systemd as init system (PID 1).") - } + verbose.Println("Running as user", a.Username, "("+a.Uid+"),", "command:", a.command) + if util.SdBootedV { + verbose.Println("System booted with systemd as init system (PID 1).") } switch a.launchOptionText { @@ -106,9 +104,7 @@ func New(userName string, args []string, launchOptionText string) *App { os.Exit(1) } - if system.V.Verbose { - fmt.Println("Determined launch method to be", a.launchOptionText, "with tool at", a.toolPath) - } + verbose.Println("Determined launch method to be", a.launchOptionText, "with tool at", a.toolPath) return a } diff --git a/internal/app/wayland.go b/internal/app/wayland.go index 3b0be85..1256871 100644 --- a/internal/app/wayland.go +++ b/internal/app/wayland.go @@ -8,6 +8,7 @@ import ( "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/verbose" ) const ( @@ -30,8 +31,6 @@ func (a *App) ShareWayland() { } else { state.RegisterRevertPath(wp) } - if system.V.Verbose { - fmt.Printf("Wayland socket '%s' configured\n", w) - } + verbose.Printf("Wayland socket '%s' configured\n", w) } } diff --git a/internal/app/x.go b/internal/app/x.go index 414870b..20c0de1 100644 --- a/internal/app/x.go +++ b/internal/app/x.go @@ -5,7 +5,7 @@ import ( "os" "git.ophivana.moe/cat/fortify/internal/state" - "git.ophivana.moe/cat/fortify/internal/system" + "git.ophivana.moe/cat/fortify/internal/verbose" "git.ophivana.moe/cat/fortify/internal/xcb" ) @@ -21,9 +21,7 @@ func (a *App) ShareX() { // add environment variable for new process a.AppendEnv(display, d) - if system.V.Verbose { - fmt.Printf("X11: Adding XHost entry SI:localuser:%s to display '%s'\n", a.Username, d) - } + verbose.Printf("X11: Adding XHost entry SI:localuser:%s to display '%s'\n", a.Username, d) if err := xcb.ChangeHosts(xcb.HostModeInsert, xcb.FamilyServerInterpreted, "localuser\x00"+a.Username); err != nil { state.Fatal(fmt.Sprintf("Error adding XHost entry to '%s':", d), err) } else { diff --git a/internal/state/exit.go b/internal/state/exit.go index 4ba0a9d..0eb80a8 100644 --- a/internal/state/exit.go +++ b/internal/state/exit.go @@ -7,7 +7,7 @@ import ( "os" "git.ophivana.moe/cat/fortify/internal/acl" - "git.ophivana.moe/cat/fortify/internal/system" + "git.ophivana.moe/cat/fortify/internal/verbose" "git.ophivana.moe/cat/fortify/internal/xcb" ) @@ -24,9 +24,7 @@ func BeforeExit() { } if statePath == "" { - if system.V.Verbose { - fmt.Println("State path is unset") - } + verbose.Println("State path is unset") } else { if err := os.Remove(statePath); err != nil && !errors.Is(err, fs.ErrNotExist) { fmt.Println("Error removing state file:", err) @@ -38,20 +36,14 @@ func BeforeExit() { os.Exit(1) } else if len(d) > 0 { // other launchers are still active - if system.V.Verbose { - fmt.Printf("Found %d active launchers, exiting without cleaning up\n", len(d)) - } + verbose.Printf("Found %d active launchers, exiting without cleaning up\n", len(d)) return } - if system.V.Verbose { - fmt.Println("No other launchers active, will clean up") - } + verbose.Println("No other launchers active, will clean up") if xcbActionComplete { - if system.V.Verbose { - fmt.Printf("X11: Removing XHost entry SI:localuser:%s\n", u.Username) - } + verbose.Printf("X11: Removing XHost entry SI:localuser:%s\n", u.Username) if err := xcb.ChangeHosts(xcb.HostModeDelete, xcb.FamilyServerInterpreted, "localuser\x00"+u.Username); err != nil { fmt.Println("Error removing XHost entry:", err) } @@ -61,21 +53,15 @@ func BeforeExit() { if err := acl.UpdatePerm(candidate, uid); err != nil { fmt.Printf("Error stripping ACL entry from '%s': %s\n", candidate, err) } - if system.V.Verbose { - fmt.Printf("Stripped ACL entry for user '%s' from '%s'\n", u.Username, candidate) - } + verbose.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") - } + verbose.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") - } + verbose.Println("D-Bus proxy already closed") } else { fmt.Println("Error closing D-Bus proxy:", err) } diff --git a/internal/state/print.go b/internal/state/print.go index 6610ada..0881f19 100644 --- a/internal/state/print.go +++ b/internal/state/print.go @@ -9,6 +9,7 @@ import ( "text/tabwriter" "git.ophivana.moe/cat/fortify/internal/system" + "git.ophivana.moe/cat/fortify/internal/verbose" ) var ( @@ -31,16 +32,12 @@ func Early() { } else { for _, e := range runDir { if !e.IsDir() { - if system.V.Verbose { - fmt.Println("Skipped non-directory entry", e.Name()) - } + verbose.Println("Skipped non-directory entry", e.Name()) continue } if _, err = strconv.Atoi(e.Name()); err != nil { - if system.V.Verbose { - fmt.Println("Skipped non-uid entry", e.Name()) - } + verbose.Println("Skipped non-uid entry", e.Name()) continue } @@ -74,7 +71,7 @@ func printLauncherState(uid string, w **tabwriter.Writer) { if *w == nil { *w = tabwriter.NewWriter(os.Stdout, 0, 1, 4, ' ', 0) - if !system.V.Verbose { + if !verbose.Get() { _, _ = fmt.Fprintln(*w, "\tUID\tPID\tEnablements\tLauncher\tCommand") } else { _, _ = fmt.Fprintln(*w, "\tUID\tPID\tArgv") @@ -92,7 +89,7 @@ func printLauncherState(uid string, w **tabwriter.Writer) { enablementsDescription.WriteString("none") } - if !system.V.Verbose { + if !verbose.Get() { _, _ = fmt.Fprintf(*w, "\t%s\t%d\t%s\t%s\t%s\n", uid, state.PID, strings.TrimPrefix(enablementsDescription.String(), ", "), state.Launcher, state.Command) diff --git a/internal/system/retrieve.go b/internal/system/retrieve.go index d9f0e3d..0679842 100644 --- a/internal/system/retrieve.go +++ b/internal/system/retrieve.go @@ -7,12 +7,12 @@ import ( "strconv" ) -func Retrieve(verbose bool) { +func Retrieve() { if V != nil { panic("system info retrieved twice") } - v := &Values{Share: path.Join(os.TempDir(), "fortify."+strconv.Itoa(os.Geteuid())), Verbose: verbose} + v := &Values{Share: path.Join(os.TempDir(), "fortify."+strconv.Itoa(os.Geteuid()))} if r, ok := os.LookupEnv(xdgRuntimeDir); !ok { fmt.Println("Env variable", xdgRuntimeDir, "unset") diff --git a/internal/system/value.go b/internal/system/value.go index fa5458f..c34e7fa 100644 --- a/internal/system/value.go +++ b/internal/system/value.go @@ -8,7 +8,6 @@ type Values struct { Share string Runtime string RunDir string - Verbose bool } var V *Values diff --git a/internal/verbose/print.go b/internal/verbose/print.go new file mode 100644 index 0000000..420df2d --- /dev/null +++ b/internal/verbose/print.go @@ -0,0 +1,15 @@ +package verbose + +import "fmt" + +func Println(a ...any) { + if verbose.Load() { + fmt.Println(a...) + } +} + +func Printf(format string, a ...any) { + if verbose.Load() { + fmt.Printf(format, a...) + } +} diff --git a/internal/verbose/state.go b/internal/verbose/state.go new file mode 100644 index 0000000..e63e7a1 --- /dev/null +++ b/internal/verbose/state.go @@ -0,0 +1,13 @@ +package verbose + +import "sync/atomic" + +var verbose = new(atomic.Bool) + +func Get() bool { + return verbose.Load() +} + +func Set(v bool) { + verbose.Store(v) +} diff --git a/main.go b/main.go index 1411b91..85ac5a8 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ import ( "git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/system" "git.ophivana.moe/cat/fortify/internal/util" + "git.ophivana.moe/cat/fortify/internal/verbose" ) var ( @@ -48,6 +49,7 @@ func tryVersion() { func main() { flag.Parse() + verbose.Set(flagVerbose) // launcher payload early exit app.Early(printVersion) @@ -56,7 +58,7 @@ func main() { tryVersion() tryLicense() - system.Retrieve(flagVerbose) + system.Retrieve() a = app.New(userName, flag.Args(), launchOptionText) state.Set(*a.User, a.Command(), a.UID()) @@ -114,22 +116,20 @@ func main() { a.AppendEnv("XDG_RUNTIME_DIR", cr) a.AppendEnv("XDG_SESSION_CLASS", "user") a.AppendEnv("XDG_SESSION_TYPE", "tty") - if system.V.Verbose { - fmt.Printf("Child runtime data dir '%s' configured\n", cr) - } + verbose.Printf("Child runtime data dir '%s' configured\n", cr) } } // warn about target user home directory ownership if stat, err := os.Stat(a.HomeDir); err != nil { - if system.V.Verbose { + if verbose.Get() { switch { case errors.Is(err, fs.ErrPermission): - fmt.Printf("User %s home directory %s is not accessible", a.Username, a.HomeDir) + fmt.Printf("User %s home directory %s is not accessible\n", a.Username, a.HomeDir) case errors.Is(err, fs.ErrNotExist): - fmt.Printf("User %s home directory %s does not exist", a.Username, a.HomeDir) + fmt.Printf("User %s home directory %s does not exis\n", a.Username, a.HomeDir) default: - fmt.Printf("Error stat user %s home directory %s: %s", a.Username, a.HomeDir, err) + fmt.Printf("Error stat user %s home directory %s: %s\n", a.Username, a.HomeDir, err) } } return @@ -154,9 +154,7 @@ func main() { } else { state.RegisterRevertPath(system.V.Runtime) } - if system.V.Verbose { - fmt.Printf("Runtime data dir '%s' configured\n", system.V.Runtime) - } + verbose.Printf("Runtime data dir '%s' configured\n", system.V.Runtime) } if mustWayland {