Skip to content

Commit ae1269f

Browse files
added a sanity check to startup, fails gracefully if run as root or cannot write to workdir
1 parent 9fe613b commit ae1269f

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ package main
2222

2323
import (
2424
"embed"
25+
"os"
2526
"sync"
27+
"time"
2628

2729
"github.com/JacksonTheMaster/StationeersServerUI/v5/src/cli"
2830
"github.com/JacksonTheMaster/StationeersServerUI/v5/src/core/loader"
@@ -38,6 +40,11 @@ func main() {
3840
var wg sync.WaitGroup
3941
logger.ConfigureConsole()
4042
loader.SetupWorkingDir()
43+
if err := loader.SanityCheck(); err != nil {
44+
logger.Main.Error("Sanity check failed, exiting in 10 secconds: " + err.Error())
45+
time.Sleep(10 * time.Second)
46+
os.Exit(1)
47+
}
4148
logger.Main.Debug("Initializing resources...")
4249
loader.InitVirtFS(v1uiFS)
4350
logger.Install.Info("Starting setup...")

src/core/loader/helpers.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,39 @@ func SetupWorkingDir() error {
195195
}
196196
return nil
197197
}
198+
199+
func SanityCheck() error {
200+
201+
if runtime.GOOS == "windows" {
202+
return nil
203+
}
204+
// Check if running as root (UID 0)
205+
if os.Geteuid() == 0 {
206+
return fmt.Errorf("root: SSUI should not be run as root")
207+
}
208+
209+
// Check if current working directory is writable
210+
workDir, err := os.Getwd()
211+
if err != nil {
212+
return fmt.Errorf("failed to get working directory: %w", err)
213+
}
214+
215+
// Try to create a temporary file to test write permissions
216+
testFile := filepath.Join(workDir, ".write_test")
217+
if err := os.WriteFile(testFile, []byte("test"), 0600); err != nil {
218+
return fmt.Errorf("cannot write to working directory %s: %w", workDir, err)
219+
}
220+
// Clean up test file
221+
if err := os.Remove(testFile); err != nil {
222+
return fmt.Errorf("failed to clean up sanity check writetest file: %w", err)
223+
}
224+
225+
// Check if steamcmd package is installed (requires further testing, disabled for now)
226+
//cmd := exec.Command("dpkg-query", "-W", "-f='${Status}'", "steamcmd")
227+
//output, err := cmd.CombinedOutput()
228+
//if err == nil && strings.Contains(string(output), "install ok installed") {
229+
// return fmt.Errorf("steamcmd package is installed")
230+
//}
231+
232+
return nil
233+
}

0 commit comments

Comments
 (0)