aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/state/print.go
blob: 83912d32154a8e4b88aa0832df33172fd5e3d706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package state

import (
	"fmt"
	"os"
	"strconv"
	"strings"
	"text/tabwriter"

	"git.ophivana.moe/cat/fortify/internal/verbose"
)

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
			}

			if _, err = strconv.Atoi(e.Name()); err != nil {
				verbose.Println("Skipped non-uid entry", e.Name())
				continue
			}

			MustPrintLauncherState(w, runDirPath, e.Name())
		}
	}
}

func MustPrintLauncherState(w **tabwriter.Writer, runDirPath, uid string) {
	launchers, err := ReadLaunchers(runDirPath, 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 !verbose.Get() {
			_, _ = 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 !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)
		} else {
			_, _ = fmt.Fprintf(*w, "\t%s\t%d\t%s\n",
				uid, state.PID, state.Argv)
		}
	}
}