verbose: remove system package interaction

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
Ophestra Umiker 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/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])
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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 {

View File

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

View File

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

View File

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

View File

@ -8,7 +8,6 @@ type Values struct {
Share string
Runtime string
RunDir string
Verbose bool
}
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/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 {