From: John Kacur <jkacur@redhat.com>
To: linux-rt-users@vger.kernel.org
Subject: [PATCH 6/6] tuna: Add centralized input validation for CPU numbers and RT priorities
Date: Mon, 22 Jun 2026 16:21:31 -0400 [thread overview]
Message-ID: <20260622202131.182067-7-jkacur@redhat.com> (raw)
In-Reply-To: <20260622202131.182067-1-jkacur@redhat.com>
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 Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: John Kacur <jkacur@redhat.com>
---
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
prev parent reply other threads:[~2026-06-22 20:21 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-22 20:21 [PATCH 0/6] User experience and code quality improvements John Kacur
2026-06-22 20:21 ` [PATCH 1/6] tuna: Warn when user-specified patterns match zero threads or IRQs John Kacur
2026-06-22 20:21 ` [PATCH 2/6] tuna: Suppress output when pattern matches zero results John Kacur
2026-06-22 20:21 ` [PATCH 3/6] tuna: Make warnings opt-in with -w/--warn flag John Kacur
2026-06-22 20:21 ` [PATCH 4/6] tuna: Enhance what_is command with new kernel thread descriptions and usability improvements John Kacur
2026-06-22 20:21 ` [PATCH 5/6] tuna: Add comprehensive docstrings to converter functions John Kacur
2026-06-22 20:21 ` John Kacur [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260622202131.182067-7-jkacur@redhat.com \
--to=jkacur@redhat.com \
--cc=linux-rt-users@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).