[PATCH 06/36] tuna: Add centralized input validation for CPU numbers and RT priorities
John Kacur <[email protected]> Fri, 10 Jul 2026 10:14:44 -0400
| Newsgroups | org.kernel.vger.linux-rt-users |
|---|---|
| Message-ID | <[email protected]> |
Add a validation block in main() that validates converted arguments before command dispatch. This provides early detection of invalid inputs with clear, user-friendly error messages. CPU number validation: - Validates that all CPU numbers in cpu_list are within valid range (0 to nr_cpus-1) - Applies to commands: move, spread, isolate, include, run, save, show_threads, show_irqs, gui, cpu_power - Error message shows invalid CPUs and system's valid range RT priority validation: - Validates that RT priorities are within valid range (1-99) - Applies to commands: priority, run - Validates both POLICY:RTPRIO format and priority-only format - Only validates when a policy is specified or priority is non-zero Old behavior with invalid inputs: - Invalid CPU: Either silent failure (show_threads) or confusing tracebacks (move, isolate) - Invalid RT priority: Generic "Invalid argument" errno message New behavior: - Clear error message (e.g., "tuna: Invalid CPU numbers: [999]. System has 16 CPUs (0-15)") - Fails fast before attempting operations - No verbose usage output, just the concise error Benefits: - Better user experience with clear error messages - Centralized validation logic (easier to maintain) - Prevents confusing error messages from system calls This completes task #6 from the argparse enhancements tracking document. Assisted-by: Claude:claude-sonnet-4-5 Signed-off-by: John Kacur <[email protected]> --- tuna-cmd.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tuna-cmd.py b/tuna-cmd.py index f1605896c33c..b9a071414125 100755 --- a/tuna-cmd.py +++ b/tuna-cmd.py @@ -852,6 +852,32 @@ def main(): if 'irq_list' in vars(args) and isinstance(args.irq_list, list) and not args.irq_list: print("[WARNING] IRQ pattern matched 0 IRQs", file=sys.stderr) + # Validate converted arguments + if args.command in ['move', 'm', 'spread', 'x', 'isolate', 'i', 'include', 'I', 'run', 'r', 'save', 's', 'show_threads', 'show_irqs', 'gui', 'g', 'cpu_power']: + if 'cpu_list' in vars(args) and args.cpu_list: + nr_cpus = utils.get_nr_cpus() + invalid_cpus = [c for c in args.cpu_list if c >= nr_cpus] + if invalid_cpus: + print(f"[ERROR] Invalid CPU numbers: {invalid_cpus}. System has {nr_cpus} CPUs (0-{nr_cpus-1})", file=sys.stderr) + sys.exit(2) + + if args.command in ['priority', 'p', 'run', 'r']: + if 'priority' in vars(args) and args.priority: + policy, rtprio = args.priority + # Only validate rtprio if a policy was specified or if rtprio is non-zero + if policy is not None or rtprio != 0: + # RT policies (FIFO, RR) require priority 1-99 + # SCHED_FIFO = 1, SCHED_RR = 2 in Linux + if policy in (1, 2) and rtprio is not None: + if rtprio < 1 or rtprio > 99: + print(f"[ERROR] Invalid RT priority: {rtprio}. RT priorities must be 1-99", file=sys.stderr) + sys.exit(2) + # If only priority specified (no policy), assume RT and validate + elif policy is None and rtprio != 0: + if rtprio < 1 or rtprio > 99: + print(f"[ERROR] Invalid RT priority: {rtprio}. RT priorities must be 1-99", file=sys.stderr) + sys.exit(2) + # if args.command in ['apply', 'a']: # apply_config(args.profilename) -- 2.54.0