From 83af555c9775e91990a87aff6a7afda8970b60f1 Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Sun, 8 Sep 2024 13:19:48 +0900 Subject: [PATCH] state/print: collect and output state information of all users The -state flag now outputs state of all users. The old behaviour can be accessed via the -state-current flag, user is selected via -u. Signed-off-by: Ophestra Umiker --- internal/state/print.go | 104 +++++++++++++++++++++++++++++++++++++ internal/state/register.go | 6 +++ internal/state/track.go | 59 +-------------------- 3 files changed, 111 insertions(+), 58 deletions(-) create mode 100644 internal/state/print.go diff --git a/internal/state/print.go b/internal/state/print.go new file mode 100644 index 0000000..6610ada --- /dev/null +++ b/internal/state/print.go @@ -0,0 +1,104 @@ +package state + +import ( + "flag" + "fmt" + "os" + "strconv" + "strings" + "text/tabwriter" + + "git.ophivana.moe/cat/fortify/internal/system" +) + +var ( + stateActionEarly bool + stateActionEarlyC bool +) + +func init() { + flag.BoolVar(&stateActionEarly, "state", false, "print state information of active launchers") + flag.BoolVar(&stateActionEarlyC, "state-current", false, "print state information of active launchers for the specified user") +} + +func Early() { + var w *tabwriter.Writer + + switch { + case stateActionEarly: + if runDir, err := os.ReadDir(system.V.RunDir); err != nil { + fmt.Println("Error reading runtime directory:", err) + } else { + for _, e := range runDir { + if !e.IsDir() { + if system.V.Verbose { + fmt.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()) + } + continue + } + + printLauncherState(e.Name(), &w) + } + } + case stateActionEarlyC: + printLauncherState(u.Uid, &w) + default: + return + } + + if w != nil { + if err := w.Flush(); err != nil { + fmt.Println("warn: error formatting output:", err) + } + } else { + fmt.Println("No information available.") + } + + os.Exit(0) +} + +func printLauncherState(uid string, w **tabwriter.Writer) { + launchers, err := readLaunchers(uid) + if err != nil { + fmt.Println("Error reading launchers:", err) + os.Exit(1) + } + + if *w == nil { + *w = tabwriter.NewWriter(os.Stdout, 0, 1, 4, ' ', 0) + + if !system.V.Verbose { + _, _ = fmt.Fprintln(*w, "\tUID\tPID\tEnablements\tLauncher\tCommand") + } else { + _, _ = fmt.Fprintln(*w, "\tUID\tPID\tArgv") + } + } + + for _, state := range launchers { + enablementsDescription := strings.Builder{} + for i := Enablement(0); i < enableLength; i++ { + if state.Capability.Has(i) { + enablementsDescription.WriteString(", " + i.String()) + } + } + if enablementsDescription.Len() == 0 { + enablementsDescription.WriteString("none") + } + + if !system.V.Verbose { + _, _ = 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) + } else { + _, _ = fmt.Fprintf(*w, "\t%s\t%d\t%s\n", + uid, state.PID, state.Argv) + } + } +} diff --git a/internal/state/register.go b/internal/state/register.go index 25c2289..ac502ae 100644 --- a/internal/state/register.go +++ b/internal/state/register.go @@ -1,5 +1,11 @@ package state +var ( + cleanupCandidate []string + enablements *Enablements + xcbActionComplete bool +) + func RegisterRevertPath(p string) { cleanupCandidate = append(cleanupCandidate, p) } diff --git a/internal/state/track.go b/internal/state/track.go index 96e9353..198abeb 100644 --- a/internal/state/track.go +++ b/internal/state/track.go @@ -3,15 +3,11 @@ package state import ( "encoding/gob" "errors" - "flag" - "fmt" "io/fs" "os" "os/exec" "path" "strconv" - "strings" - "text/tabwriter" "git.ophivana.moe/cat/fortify/internal/system" ) @@ -20,11 +16,7 @@ import ( // this and launcher should eventually be replaced by a server process var ( - stateActionEarly bool - statePath string - cleanupCandidate []string - xcbActionComplete bool - enablements *Enablements + statePath string ) type launcherState struct { @@ -35,55 +27,6 @@ type launcherState struct { Capability Enablements } -func init() { - flag.BoolVar(&stateActionEarly, "state", false, "query state value of current active launchers") -} - -func Early() { - if !stateActionEarly { - return - } - - launchers, err := readLaunchers(u.Uid) - if err != nil { - fmt.Println("Error reading launchers:", err) - os.Exit(1) - } - - stdout := tabwriter.NewWriter(os.Stdout, 0, 1, 4, ' ', 0) - if !system.V.Verbose { - _, _ = fmt.Fprintln(stdout, "\tPID\tEnablements\tLauncher\tCommand") - } else { - _, _ = fmt.Fprintln(stdout, "\tPID\tArgv") - } - - for _, state := range launchers { - enablementsDescription := strings.Builder{} - for i := Enablement(0); i < enableLength; i++ { - if state.Capability.Has(i) { - enablementsDescription.WriteString(", " + i.String()) - } - } - if enablementsDescription.Len() == 0 { - enablementsDescription.WriteString("none") - } - - if !system.V.Verbose { - _, _ = fmt.Fprintf(stdout, "\t%d\t%s\t%s\t%s\n", - state.PID, strings.TrimPrefix(enablementsDescription.String(), ", "), state.Launcher, - state.Command) - } else { - _, _ = fmt.Fprintf(stdout, "\t%d\t%s\n", - state.PID, state.Argv) - } - } - if err = stdout.Flush(); err != nil { - fmt.Println("warn: error formatting output:", err) - } - - os.Exit(0) -} - // SaveProcess called after process start, before wait func SaveProcess(uid string, cmd *exec.Cmd) error { statePath = path.Join(system.V.RunDir, uid, strconv.Itoa(cmd.Process.Pid))