2024-09-08 13:19:48 +09:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2024-09-17 13:48:42 +09:00
|
|
|
"git.ophivana.moe/cat/fortify/internal"
|
2024-09-12 21:07:05 +09:00
|
|
|
"git.ophivana.moe/cat/fortify/internal/verbose"
|
2024-09-08 13:19:48 +09:00
|
|
|
)
|
|
|
|
|
2024-09-16 20:31:15 +09:00
|
|
|
func MustPrintLauncherStateGlobal(w **tabwriter.Writer, runDirPath string) {
|
|
|
|
if dirs, err := os.ReadDir(runDirPath); err != nil {
|
|
|
|
fmt.Println("Error reading runtime directory:", err)
|
|
|
|
} else {
|
|
|
|
for _, e := range dirs {
|
|
|
|
if !e.IsDir() {
|
|
|
|
verbose.Println("Skipped non-directory entry", e.Name())
|
|
|
|
continue
|
|
|
|
}
|
2024-09-08 13:19:48 +09:00
|
|
|
|
2024-09-16 20:31:15 +09:00
|
|
|
if _, err = strconv.Atoi(e.Name()); err != nil {
|
|
|
|
verbose.Println("Skipped non-uid entry", e.Name())
|
|
|
|
continue
|
2024-09-08 13:19:48 +09:00
|
|
|
}
|
|
|
|
|
2024-09-16 20:31:15 +09:00
|
|
|
MustPrintLauncherState(w, runDirPath, e.Name())
|
2024-09-08 13:19:48 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 20:31:15 +09:00
|
|
|
func MustPrintLauncherState(w **tabwriter.Writer, runDirPath, uid string) {
|
2024-09-17 13:48:42 +09:00
|
|
|
launchers, err := ReadLaunchers(runDirPath, uid, true)
|
2024-09-08 13:19:48 +09:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error reading launchers:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *w == nil {
|
|
|
|
*w = tabwriter.NewWriter(os.Stdout, 0, 1, 4, ' ', 0)
|
|
|
|
|
2024-09-12 21:07:05 +09:00
|
|
|
if !verbose.Get() {
|
2024-09-08 13:19:48 +09:00
|
|
|
_, _ = fmt.Fprintln(*w, "\tUID\tPID\tEnablements\tLauncher\tCommand")
|
|
|
|
} else {
|
|
|
|
_, _ = fmt.Fprintln(*w, "\tUID\tPID\tArgv")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, state := range launchers {
|
|
|
|
enablementsDescription := strings.Builder{}
|
2024-09-17 13:48:42 +09:00
|
|
|
for i := internal.Enablement(0); i < internal.EnableLength; i++ {
|
2024-09-08 13:19:48 +09:00
|
|
|
if state.Capability.Has(i) {
|
|
|
|
enablementsDescription.WriteString(", " + i.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if enablementsDescription.Len() == 0 {
|
|
|
|
enablementsDescription.WriteString("none")
|
|
|
|
}
|
|
|
|
|
2024-09-12 21:07:05 +09:00
|
|
|
if !verbose.Get() {
|
2024-09-08 13:19:48 +09:00
|
|
|
_, _ = 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|