diff options
| author | Ophestra Umiker <cat@ophivana.moe> | 2024-09-12 20:53:33 +0900 |
|---|---|---|
| committer | Ophestra Umiker <cat@ophivana.moe> | 2024-09-12 20:53:33 +0900 |
| commit | b0aff891661dfa2f438a7264607a4e6e9caef026 (patch) | |
| tree | 323ccc8c09fee45c29e8e1e79b320a4fe6dc8d77 /internal/app/setup.go | |
| parent | 8223a9ee6637495c860aa3531f1db02e8c5bd819 (diff) | |
app: handle launch method in New function
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
Diffstat (limited to 'internal/app/setup.go')
| -rw-r--r-- | internal/app/setup.go | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/internal/app/setup.go b/internal/app/setup.go index 82765dfb..d8378cfd 100644 --- a/internal/app/setup.go +++ b/internal/app/setup.go @@ -9,13 +9,19 @@ import ( "git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/system" + "git.ophivana.moe/cat/fortify/internal/util" ) type App struct { + launchOptionText string + uid int env []string command []string + launchOption uint8 + toolPath string + enablements state.Enablements *user.User @@ -23,6 +29,10 @@ type App struct { // so don't treat it as if it is } +func (a *App) LaunchOption() uint8 { + return a.launchOption +} + func (a *App) setEnablement(e state.Enablement) { if a.enablements.Has(e) { panic("enablement " + e.String() + " set twice") @@ -31,8 +41,8 @@ func (a *App) setEnablement(e state.Enablement) { a.enablements |= e.Mask() } -func New(userName string, args []string) *App { - a := &App{command: args} +func New(userName string, args []string, launchOptionText string) *App { + a := &App{command: args, launchOptionText: launchOptionText} if u, err := user.Lookup(userName); err != nil { if errors.As(err, new(user.UnknownUserError)) { @@ -57,6 +67,47 @@ func New(userName string, args []string) *App { 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).") + } + } + + switch a.launchOptionText { + case "sudo": + a.launchOption = LaunchMethodSudo + if sudoPath, ok := util.Which("sudo"); !ok { + fmt.Println("Did not find 'sudo' in PATH") + os.Exit(1) + } else { + a.toolPath = sudoPath + } + case "bubblewrap": + a.launchOption = LaunchMethodBwrap + if bwrapPath, ok := util.Which("bwrap"); !ok { + fmt.Println("Did not find 'bwrap' in PATH") + os.Exit(1) + } else { + a.toolPath = bwrapPath + } + case "systemd": + a.launchOption = LaunchMethodMachineCtl + if !util.SdBootedV { + fmt.Println("System has not been booted with systemd as init system (PID 1).") + os.Exit(1) + } + + if machineCtlPath, ok := util.Which("machinectl"); !ok { + fmt.Println("Did not find 'machinectl' in PATH") + } else { + a.toolPath = machineCtlPath + } + default: + fmt.Println("invalid launch method") + os.Exit(1) + } + + if system.V.Verbose { + fmt.Println("Determined launch method to be", a.launchOptionText, "with tool at", a.toolPath) } return a |
