verbose: remove system package interaction

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
Ophestra 2024-09-12 21:07:05 +09:00
parent b0aff89166
commit da6d238d8a
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
13 changed files with 85 additions and 107 deletions

View File

@ -12,6 +12,7 @@ import (
"git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/state"
"git.ophivana.moe/cat/fortify/internal/system" "git.ophivana.moe/cat/fortify/internal/system"
"git.ophivana.moe/cat/fortify/internal/util" "git.ophivana.moe/cat/fortify/internal/util"
"git.ophivana.moe/cat/fortify/internal/verbose"
) )
const ( const (
@ -24,7 +25,7 @@ var (
dbusSystem bool 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) a.setEnablement(state.EnableDBus)
dbusSystem = dsg != nil 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 addr, ok := os.LookupEnv(dbusSessionBusAddress); !ok {
if system.V.Verbose { verbose.Println("D-Bus: DBUS_SESSION_BUS_ADDRESS not set, assuming default format")
fmt.Println("D-Bus: DBUS_SESSION_BUS_ADDRESS not set, assuming default format")
}
sessionBus[0] = fmt.Sprintf("unix:path=/run/user/%d/bus", os.Getuid()) sessionBus[0] = fmt.Sprintf("unix:path=/run/user/%d/bus", os.Getuid())
} else { } else {
sessionBus[0] = addr sessionBus[0] = addr
} }
if addr, ok := os.LookupEnv(dbusSystemBusAddress); !ok { if addr, ok := os.LookupEnv(dbusSystemBusAddress); !ok {
if system.V.Verbose { verbose.Println("D-Bus: DBUS_SYSTEM_BUS_ADDRESS not set, assuming default format")
fmt.Println("D-Bus: DBUS_SYSTEM_BUS_ADDRESS not set, assuming default format")
}
systemBus[0] = "unix:path=/run/dbus/system_bus_socket" systemBus[0] = "unix:path=/run/dbus/system_bus_socket"
} else { } else {
systemBus[0] = addr systemBus[0] = addr
@ -65,15 +62,11 @@ func (a *App) ShareDBus(dse, dsg *dbus.Config, verbose bool) {
p := dbus.New(binPath, sessionBus, systemBus) p := dbus.New(binPath, sessionBus, systemBus)
dse.Log = verbose dse.Log = log
if system.V.Verbose { verbose.Println("D-Bus: sealing session proxy", dse.Args(sessionBus))
fmt.Println("D-Bus: sealing session proxy", dse.Args(sessionBus))
}
if dsg != nil { if dsg != nil {
dsg.Log = verbose dsg.Log = log
if system.V.Verbose { verbose.Println("D-Bus: sealing system proxy", dsg.Args(systemBus))
fmt.Println("D-Bus: sealing system proxy", dsg.Args(systemBus))
}
} }
if err := p.Seal(dse, dsg); err != nil { if err := p.Seal(dse, dsg); err != nil {
state.Fatal("D-Bus: invalid config when sealing proxy,", err) 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) ready := make(chan bool, 1)
done := make(chan struct{}) done := make(chan struct{})
if system.V.Verbose { verbose.Printf("Starting session bus proxy '%s' for address '%s'\n", dbusAddress[0], sessionBus[0])
fmt.Printf("Starting session bus proxy '%s' for address '%s'\n", dbusAddress[0], sessionBus[0])
if dsg != nil { if dsg != nil {
fmt.Printf("Starting system bus proxy '%s' for address '%s'\n", dbusAddress[1], systemBus[0]) verbose.Printf("Starting system bus proxy '%s' for address '%s'\n", dbusAddress[1], systemBus[0])
}
} }
if err := p.Start(&ready); err != nil { if err := p.Start(&ready); err != nil {
state.Fatal("D-Bus: error starting proxy,", err) state.Fatal("D-Bus: error starting proxy,", err)
} }
if system.V.Verbose { verbose.Println("D-Bus proxy launch:", p)
fmt.Println("D-Bus proxy launch:", p)
}
go func() { go func() {
if err := p.Wait(); err != nil { if err := p.Wait(); err != nil {
fmt.Println("warn: D-Bus proxy returned error,", err) fmt.Println("warn: D-Bus proxy returned error,", err)
} else { } else {
if system.V.Verbose { verbose.Println("D-Bus proxy uneventful wait")
fmt.Println("D-Bus proxy uneventful wait")
}
} }
if err := os.Remove(target); err != nil && !errors.Is(err, os.ErrNotExist) { if err := os.Remove(target); err != nil && !errors.Is(err, os.ErrNotExist) {
fmt.Println("Error removing dangling D-Bus socket:", err) 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]) state.RegisterRevertPath(systemBus[1])
} }
} }
if system.V.Verbose { verbose.Printf("Session bus proxy '%s' for address '%s' configured\n", dbusAddress[0], sessionBus[0])
fmt.Printf("Session bus proxy '%s' for address '%s' configured\n", dbusAddress[0], sessionBus[0])
if dsg != nil { if dsg != nil {
fmt.Printf("System bus proxy '%s' for address '%s' configured\n", dbusAddress[1], systemBus[0]) verbose.Printf("System bus proxy '%s' for address '%s' configured\n", dbusAddress[1], systemBus[0])
}
} }
} }

View File

@ -11,6 +11,7 @@ import (
"git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/state"
"git.ophivana.moe/cat/fortify/internal/system" "git.ophivana.moe/cat/fortify/internal/system"
"git.ophivana.moe/cat/fortify/internal/util" "git.ophivana.moe/cat/fortify/internal/util"
"git.ophivana.moe/cat/fortify/internal/verbose"
) )
func (a *App) SharePulse() { func (a *App) SharePulse() {
@ -49,9 +50,7 @@ func (a *App) SharePulse() {
pulseCookieSource := util.DiscoverPulseCookie() pulseCookieSource := util.DiscoverPulseCookie()
pulseCookieFinal := path.Join(system.V.Share, "pulse-cookie") pulseCookieFinal := path.Join(system.V.Share, "pulse-cookie")
a.AppendEnv(util.PulseCookie, pulseCookieFinal) a.AppendEnv(util.PulseCookie, pulseCookieFinal)
if system.V.Verbose { verbose.Printf("Publishing PulseAudio cookie '%s' to '%s'\n", pulseCookieSource, pulseCookieFinal)
fmt.Printf("Publishing PulseAudio cookie '%s' to '%s'\n", pulseCookieSource, pulseCookieFinal)
}
if err = util.CopyFile(pulseCookieFinal, pulseCookieSource); err != nil { if err = util.CopyFile(pulseCookieFinal, pulseCookieSource); err != nil {
state.Fatal("Error copying PulseAudio cookie:", err) state.Fatal("Error copying PulseAudio cookie:", err)
} }
@ -61,8 +60,6 @@ func (a *App) SharePulse() {
state.RegisterRevertPath(pulseCookieFinal) state.RegisterRevertPath(pulseCookieFinal)
} }
if system.V.Verbose { verbose.Printf("PulseAudio dir '%s' configured\n", pulse)
fmt.Printf("PulseAudio dir '%s' configured\n", pulse)
}
} }
} }

View File

@ -10,6 +10,7 @@ import (
"git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/state"
"git.ophivana.moe/cat/fortify/internal/system" "git.ophivana.moe/cat/fortify/internal/system"
"git.ophivana.moe/cat/fortify/internal/util" "git.ophivana.moe/cat/fortify/internal/util"
"git.ophivana.moe/cat/fortify/internal/verbose"
) )
const ( const (
@ -48,9 +49,7 @@ func (a *App) Run() {
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Dir = system.V.RunDir cmd.Dir = system.V.RunDir
if system.V.Verbose { verbose.Println("Executing:", cmd)
fmt.Println("Executing:", cmd)
}
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
state.Fatal("Error starting process:", err) state.Fatal("Error starting process:", err)
@ -71,9 +70,7 @@ func (a *App) Run() {
} }
} }
if system.V.Verbose { verbose.Println("Process exited with exit code", r)
fmt.Println("Process exited with exit code", r)
}
state.BeforeExit() state.BeforeExit()
os.Exit(r) os.Exit(r)
} }
@ -86,9 +83,7 @@ func (a *App) commandBuilderSudo() (args []string) {
// -A? // -A?
if _, ok := os.LookupEnv(sudoAskPass); ok { if _, ok := os.LookupEnv(sudoAskPass); ok {
if system.V.Verbose { verbose.Printf("%s set, adding askpass flag\n", sudoAskPass)
fmt.Printf("%s set, adding askpass flag\n", sudoAskPass)
}
args = append(args, "-A") args = append(args, "-A")
} }
@ -115,7 +110,7 @@ func (a *App) commandBuilderMachineCtl() (args []string) {
args = append(args, "shell", "--uid="+a.Username) args = append(args, "shell", "--uid="+a.Username)
// --quiet // --quiet
if !system.V.Verbose { if !verbose.Get() {
args = append(args, "--quiet") args = append(args, "--quiet")
} }

View File

@ -8,8 +8,8 @@ import (
"strconv" "strconv"
"git.ophivana.moe/cat/fortify/internal/state" "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/util"
"git.ophivana.moe/cat/fortify/internal/verbose"
) )
type App struct { type App struct {
@ -65,11 +65,9 @@ func New(userName string, args []string, launchOptionText string) *App {
a.uid = u a.uid = u
} }
if system.V.Verbose { verbose.Println("Running as user", a.Username, "("+a.Uid+"),", "command:", a.command)
fmt.Println("Running as user", a.Username, "("+a.Uid+"),", "command:", a.command)
if util.SdBootedV { if util.SdBootedV {
fmt.Println("System booted with systemd as init system (PID 1).") verbose.Println("System booted with systemd as init system (PID 1).")
}
} }
switch a.launchOptionText { switch a.launchOptionText {
@ -106,9 +104,7 @@ func New(userName string, args []string, launchOptionText string) *App {
os.Exit(1) os.Exit(1)
} }
if system.V.Verbose { verbose.Println("Determined launch method to be", a.launchOptionText, "with tool at", a.toolPath)
fmt.Println("Determined launch method to be", a.launchOptionText, "with tool at", a.toolPath)
}
return a return a
} }

View File

@ -8,6 +8,7 @@ import (
"git.ophivana.moe/cat/fortify/internal/acl" "git.ophivana.moe/cat/fortify/internal/acl"
"git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/state"
"git.ophivana.moe/cat/fortify/internal/system" "git.ophivana.moe/cat/fortify/internal/system"
"git.ophivana.moe/cat/fortify/internal/verbose"
) )
const ( const (
@ -30,8 +31,6 @@ func (a *App) ShareWayland() {
} else { } else {
state.RegisterRevertPath(wp) state.RegisterRevertPath(wp)
} }
if system.V.Verbose { verbose.Printf("Wayland socket '%s' configured\n", w)
fmt.Printf("Wayland socket '%s' configured\n", w)
}
} }
} }

View File

@ -5,7 +5,7 @@ import (
"os" "os"
"git.ophivana.moe/cat/fortify/internal/state" "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" "git.ophivana.moe/cat/fortify/internal/xcb"
) )
@ -21,9 +21,7 @@ func (a *App) ShareX() {
// add environment variable for new process // add environment variable for new process
a.AppendEnv(display, d) a.AppendEnv(display, d)
if system.V.Verbose { verbose.Printf("X11: Adding XHost entry SI:localuser:%s to display '%s'\n", a.Username, d)
fmt.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 { 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) state.Fatal(fmt.Sprintf("Error adding XHost entry to '%s':", d), err)
} else { } else {

View File

@ -7,7 +7,7 @@ import (
"os" "os"
"git.ophivana.moe/cat/fortify/internal/acl" "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" "git.ophivana.moe/cat/fortify/internal/xcb"
) )
@ -24,9 +24,7 @@ func BeforeExit() {
} }
if statePath == "" { if statePath == "" {
if system.V.Verbose { verbose.Println("State path is unset")
fmt.Println("State path is unset")
}
} else { } else {
if err := os.Remove(statePath); err != nil && !errors.Is(err, fs.ErrNotExist) { if err := os.Remove(statePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
fmt.Println("Error removing state file:", err) fmt.Println("Error removing state file:", err)
@ -38,20 +36,14 @@ func BeforeExit() {
os.Exit(1) os.Exit(1)
} else if len(d) > 0 { } else if len(d) > 0 {
// other launchers are still active // other launchers are still active
if system.V.Verbose { verbose.Printf("Found %d active launchers, exiting without cleaning up\n", len(d))
fmt.Printf("Found %d active launchers, exiting without cleaning up\n", len(d))
}
return return
} }
if system.V.Verbose { verbose.Println("No other launchers active, will clean up")
fmt.Println("No other launchers active, will clean up")
}
if xcbActionComplete { if xcbActionComplete {
if system.V.Verbose { verbose.Printf("X11: Removing XHost entry SI:localuser:%s\n", u.Username)
fmt.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 { if err := xcb.ChangeHosts(xcb.HostModeDelete, xcb.FamilyServerInterpreted, "localuser\x00"+u.Username); err != nil {
fmt.Println("Error removing XHost entry:", err) fmt.Println("Error removing XHost entry:", err)
} }
@ -61,21 +53,15 @@ func BeforeExit() {
if err := acl.UpdatePerm(candidate, uid); err != nil { if err := acl.UpdatePerm(candidate, uid); err != nil {
fmt.Printf("Error stripping ACL entry from '%s': %s\n", candidate, err) fmt.Printf("Error stripping ACL entry from '%s': %s\n", candidate, err)
} }
if system.V.Verbose { verbose.Printf("Stripped ACL entry for user '%s' from '%s'\n", u.Username, candidate)
fmt.Printf("Stripped ACL entry for user '%s' from '%s'\n", u.Username, candidate)
}
} }
if dbusProxy != nil { if dbusProxy != nil {
if system.V.Verbose { verbose.Println("D-Bus proxy registered, cleaning up")
fmt.Println("D-Bus proxy registered, cleaning up")
}
if err := dbusProxy.Close(); err != nil { if err := dbusProxy.Close(); err != nil {
if errors.Is(err, os.ErrClosed) { if errors.Is(err, os.ErrClosed) {
if system.V.Verbose { verbose.Println("D-Bus proxy already closed")
fmt.Println("D-Bus proxy already closed")
}
} else { } else {
fmt.Println("Error closing D-Bus proxy:", err) fmt.Println("Error closing D-Bus proxy:", err)
} }

View File

@ -9,6 +9,7 @@ import (
"text/tabwriter" "text/tabwriter"
"git.ophivana.moe/cat/fortify/internal/system" "git.ophivana.moe/cat/fortify/internal/system"
"git.ophivana.moe/cat/fortify/internal/verbose"
) )
var ( var (
@ -31,16 +32,12 @@ func Early() {
} else { } else {
for _, e := range runDir { for _, e := range runDir {
if !e.IsDir() { if !e.IsDir() {
if system.V.Verbose { verbose.Println("Skipped non-directory entry", e.Name())
fmt.Println("Skipped non-directory entry", e.Name())
}
continue continue
} }
if _, err = strconv.Atoi(e.Name()); err != nil { if _, err = strconv.Atoi(e.Name()); err != nil {
if system.V.Verbose { verbose.Println("Skipped non-uid entry", e.Name())
fmt.Println("Skipped non-uid entry", e.Name())
}
continue continue
} }
@ -74,7 +71,7 @@ func printLauncherState(uid string, w **tabwriter.Writer) {
if *w == nil { if *w == nil {
*w = tabwriter.NewWriter(os.Stdout, 0, 1, 4, ' ', 0) *w = tabwriter.NewWriter(os.Stdout, 0, 1, 4, ' ', 0)
if !system.V.Verbose { if !verbose.Get() {
_, _ = fmt.Fprintln(*w, "\tUID\tPID\tEnablements\tLauncher\tCommand") _, _ = fmt.Fprintln(*w, "\tUID\tPID\tEnablements\tLauncher\tCommand")
} else { } else {
_, _ = fmt.Fprintln(*w, "\tUID\tPID\tArgv") _, _ = fmt.Fprintln(*w, "\tUID\tPID\tArgv")
@ -92,7 +89,7 @@ func printLauncherState(uid string, w **tabwriter.Writer) {
enablementsDescription.WriteString("none") enablementsDescription.WriteString("none")
} }
if !system.V.Verbose { if !verbose.Get() {
_, _ = fmt.Fprintf(*w, "\t%s\t%d\t%s\t%s\t%s\n", _, _ = fmt.Fprintf(*w, "\t%s\t%d\t%s\t%s\t%s\n",
uid, state.PID, strings.TrimPrefix(enablementsDescription.String(), ", "), state.Launcher, uid, state.PID, strings.TrimPrefix(enablementsDescription.String(), ", "), state.Launcher,
state.Command) state.Command)

View File

@ -7,12 +7,12 @@ import (
"strconv" "strconv"
) )
func Retrieve(verbose bool) { func Retrieve() {
if V != nil { if V != nil {
panic("system info retrieved twice") 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 { if r, ok := os.LookupEnv(xdgRuntimeDir); !ok {
fmt.Println("Env variable", xdgRuntimeDir, "unset") fmt.Println("Env variable", xdgRuntimeDir, "unset")

View File

@ -8,7 +8,6 @@ type Values struct {
Share string Share string
Runtime string Runtime string
RunDir string RunDir string
Verbose bool
} }
var V *Values var V *Values

15
internal/verbose/print.go Normal file
View File

@ -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...)
}
}

13
internal/verbose/state.go Normal file
View File

@ -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)
}

20
main.go
View File

@ -17,6 +17,7 @@ import (
"git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/state"
"git.ophivana.moe/cat/fortify/internal/system" "git.ophivana.moe/cat/fortify/internal/system"
"git.ophivana.moe/cat/fortify/internal/util" "git.ophivana.moe/cat/fortify/internal/util"
"git.ophivana.moe/cat/fortify/internal/verbose"
) )
var ( var (
@ -48,6 +49,7 @@ func tryVersion() {
func main() { func main() {
flag.Parse() flag.Parse()
verbose.Set(flagVerbose)
// launcher payload early exit // launcher payload early exit
app.Early(printVersion) app.Early(printVersion)
@ -56,7 +58,7 @@ func main() {
tryVersion() tryVersion()
tryLicense() tryLicense()
system.Retrieve(flagVerbose) system.Retrieve()
a = app.New(userName, flag.Args(), launchOptionText) a = app.New(userName, flag.Args(), launchOptionText)
state.Set(*a.User, a.Command(), a.UID()) state.Set(*a.User, a.Command(), a.UID())
@ -114,22 +116,20 @@ func main() {
a.AppendEnv("XDG_RUNTIME_DIR", cr) a.AppendEnv("XDG_RUNTIME_DIR", cr)
a.AppendEnv("XDG_SESSION_CLASS", "user") a.AppendEnv("XDG_SESSION_CLASS", "user")
a.AppendEnv("XDG_SESSION_TYPE", "tty") a.AppendEnv("XDG_SESSION_TYPE", "tty")
if system.V.Verbose { verbose.Printf("Child runtime data dir '%s' configured\n", cr)
fmt.Printf("Child runtime data dir '%s' configured\n", cr)
}
} }
} }
// warn about target user home directory ownership // warn about target user home directory ownership
if stat, err := os.Stat(a.HomeDir); err != nil { if stat, err := os.Stat(a.HomeDir); err != nil {
if system.V.Verbose { if verbose.Get() {
switch { switch {
case errors.Is(err, fs.ErrPermission): 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): 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: 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 return
@ -154,9 +154,7 @@ func main() {
} else { } else {
state.RegisterRevertPath(system.V.Runtime) state.RegisterRevertPath(system.V.Runtime)
} }
if system.V.Verbose { verbose.Printf("Runtime data dir '%s' configured\n", system.V.Runtime)
fmt.Printf("Runtime data dir '%s' configured\n", system.V.Runtime)
}
} }
if mustWayland { if mustWayland {