2024-09-04 01:20:12 +09:00
|
|
|
package state
|
2024-07-16 14:19:43 +09:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2024-09-08 02:24:01 +09:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
2024-09-04 01:20:12 +09:00
|
|
|
|
|
|
|
"git.ophivana.moe/cat/fortify/internal/system"
|
2024-07-16 14:19:43 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// we unfortunately have to assume there are never races between processes
|
|
|
|
// this and launcher should eventually be replaced by a server process
|
|
|
|
|
|
|
|
var (
|
|
|
|
stateActionEarly bool
|
|
|
|
statePath string
|
|
|
|
cleanupCandidate []string
|
|
|
|
xcbActionComplete bool
|
2024-09-08 02:24:01 +09:00
|
|
|
enablements *Enablements
|
2024-07-16 14:19:43 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
type launcherState struct {
|
2024-09-08 02:24:01 +09:00
|
|
|
PID int
|
|
|
|
Launcher string
|
|
|
|
Argv []string
|
|
|
|
Command []string
|
|
|
|
Capability Enablements
|
2024-07-16 14:19:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&stateActionEarly, "state", false, "query state value of current active launchers")
|
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
func Early() {
|
2024-07-16 14:19:43 +09:00
|
|
|
if !stateActionEarly {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-08 02:24:01 +09:00
|
|
|
launchers, err := readLaunchers(u.Uid)
|
2024-07-16 14:19:43 +09:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error reading launchers:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-09-08 02:24:01 +09:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-07-16 14:19:43 +09:00
|
|
|
for _, state := range launchers {
|
2024-09-08 02:24:01 +09:00
|
|
|
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)
|
2024-07-16 14:19:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// 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))
|
2024-07-16 14:19:43 +09:00
|
|
|
state := launcherState{
|
2024-09-08 02:24:01 +09:00
|
|
|
PID: cmd.Process.Pid,
|
|
|
|
Launcher: cmd.Path,
|
|
|
|
Argv: cmd.Args,
|
|
|
|
Command: command,
|
|
|
|
Capability: *enablements,
|
2024-07-16 14:19:43 +09:00
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
if err := os.Mkdir(path.Join(system.V.RunDir, uid), 0700); err != nil && !errors.Is(err, fs.ErrExist) {
|
2024-07-16 14:19:43 +09:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, err := os.OpenFile(statePath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
defer func() {
|
|
|
|
if f.Close() != nil {
|
|
|
|
// unreachable
|
|
|
|
panic("state file closed prematurely")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return gob.NewEncoder(f).Encode(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 02:24:01 +09:00
|
|
|
func readLaunchers(uid string) ([]*launcherState, error) {
|
2024-07-16 14:19:43 +09:00
|
|
|
var f *os.File
|
|
|
|
var r []*launcherState
|
2024-09-08 02:24:01 +09:00
|
|
|
launcherPrefix := path.Join(system.V.RunDir, uid)
|
2024-07-16 14:19:43 +09:00
|
|
|
|
|
|
|
if pl, err := os.ReadDir(launcherPrefix); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
for _, e := range pl {
|
|
|
|
if err = func() error {
|
|
|
|
if f, err = os.Open(path.Join(launcherPrefix, e.Name())); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
defer func() {
|
|
|
|
if f.Close() != nil {
|
|
|
|
// unreachable
|
|
|
|
panic("foreign state file closed prematurely")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var s launcherState
|
|
|
|
r = append(r, &s)
|
|
|
|
return gob.NewDecoder(f).Decode(&s)
|
|
|
|
}
|
|
|
|
}(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|