From 3783d23b8d608c3bf4a2dedd6b4bfb9165439bed Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 27 Feb 2026 18:33:40 +0200 Subject: internal: validate live CLI mode behavior --- internal/ior.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'internal/ior.go') diff --git a/internal/ior.go b/internal/ior.go index bf0fb1f..cdddc24 100644 --- a/internal/ior.go +++ b/internal/ior.go @@ -4,6 +4,7 @@ import "C" import ( "context" + "errors" "fmt" "os" "os/signal" @@ -35,6 +36,9 @@ var ( runTraceFn = runTrace runTraceWithContextFn = runTraceWithContext runTUIFn = tui.RunWithTraceStarter + getEUID = os.Geteuid + + errRootPrivilegesRequired = errors.New("tracing requires root privileges (run with sudo)") ) type tracepointModule interface { @@ -140,12 +144,22 @@ func Run() error { } func dispatchRun(cfg flags.Flags) error { + if err := validateRunConfig(cfg); err != nil { + return err + } if shouldRunTraceMode(cfg) { return runTraceFn() } return runTUIFn(tuiTraceStarterFromRunTrace(runTraceWithContextFn)) } +func validateRunConfig(cfg flags.Flags) error { + if cfg.LiveFlamegraph && cfg.FlamegraphEnable { + return errors.New("-live and -flamegraph are mutually exclusive") + } + return nil +} + func shouldRunTraceMode(cfg flags.Flags) bool { return cfg.PlainMode || cfg.FlamegraphEnable || cfg.LiveFlamegraph || cfg.PprofEnable } @@ -202,6 +216,10 @@ func runTrace() error { } func runTraceWithContext(parentCtx context.Context, started chan<- struct{}, configure func(*eventLoop)) error { + if getEUID() != 0 { + return errRootPrivilegesRequired + } + verbose := started == nil logln := func(...any) {} if verbose { @@ -328,5 +346,5 @@ func signalTraceStarted(started chan<- struct{}) { } func shouldAutoStopByDuration(cfg flags.Flags) bool { - return cfg.PlainMode || cfg.FlamegraphEnable || cfg.PprofEnable + return cfg.PlainMode || cfg.FlamegraphEnable || cfg.LiveFlamegraph || cfg.PprofEnable } -- cgit v1.2.3