linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tomas Mudrunka <tomas.mudrunka@gmail.com>
To: jirislaby@kernel.org
Cc: corbet@lwn.net, gregkh@linuxfoundation.org,
	linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-serial@vger.kernel.org, rdunlap@infradead.org,
	tomas.mudrunka@gmail.com
Subject: [PATCH v10] /proc/sysrq-trigger: accept multiple keys at once
Date: Mon, 20 Nov 2023 12:14:51 +0100	[thread overview]
Message-ID: <20231120111451.527952-1-tomas.mudrunka@gmail.com> (raw)
In-Reply-To: <ea2fd8b3-95cf-4d50-8fc7-f1391b23a433@kernel.org>

This way we can do:
`echo _reisub > /proc/sysrq-trigger`
Instead of:
`for i in r e i s u b; do echo "$i" > /proc/sysrq-trigger; done;`

This can be very useful when trying to execute sysrq combo remotely
or from userspace. When sending keys in multiple separate writes,
userspace (eg. bash or ssh) can be killed before whole combo is completed.
Therefore putting all keys in single write is more robust approach.

Signed-off-by: Tomas Mudrunka <tomas.mudrunka@gmail.com>
---
V9 -> V10: More english fixes
V8 -> V9: Fixed english bit more
V7 -> V8: Added this list of changes
V6 -> V7: Fixed english in documentation
V5 -> V6: Documentation now has notice about undefined behavior
V4 -> V5: Added this list of changes
V3 -> V4: Bulk is now bool instead of char (and fixed typo)
V2 -> V3: Fixed code styling (and introduced typo)
V1 -> V2: Bulk mode only activated by underscore now, added docs

 Documentation/admin-guide/sysrq.rst | 11 ++++++++++-
 drivers/tty/sysrq.c                 | 19 ++++++++++++++++---
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/Documentation/admin-guide/sysrq.rst b/Documentation/admin-guide/sysrq.rst
index 51906e473..2f2e5bd44 100644
--- a/Documentation/admin-guide/sysrq.rst
+++ b/Documentation/admin-guide/sysrq.rst
@@ -75,10 +75,19 @@ On other
 	submit a patch to be included in this section.
 
 On all
-	Write a character to /proc/sysrq-trigger.  e.g.::
+	Write a single character to /proc/sysrq-trigger.
+	Only the first character is processed, the rest of the string is
+	ignored. However, it is not recommended to write any extra characters
+	as the behavior is undefined and might change in the future versions.
+	E.g.::
 
 		echo t > /proc/sysrq-trigger
 
+	Alternatively, write multiple characters prepended by underscore.
+	This way, all characters will be processed. E.g.::
+
+		echo _reisub > /proc/sysrq-trigger
+
 The :kbd:`<command key>` is case sensitive.
 
 What are the 'command' keys?
diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
index 6b4a28bcf..02217e3c9 100644
--- a/drivers/tty/sysrq.c
+++ b/drivers/tty/sysrq.c
@@ -1150,16 +1150,29 @@ EXPORT_SYMBOL(unregister_sysrq_key);
 #ifdef CONFIG_PROC_FS
 /*
  * writing 'C' to /proc/sysrq-trigger is like sysrq-C
+ * Normally, only the first character written is processed.
+ * However, if the first character is an underscore,
+ * all characters are processed.
  */
 static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
 				   size_t count, loff_t *ppos)
 {
-	if (count) {
+	bool bulk = false;
+	size_t i;
+
+	for (i = 0; i < count; i++) {
 		char c;
 
-		if (get_user(c, buf))
+		if (get_user(c, buf + i))
 			return -EFAULT;
-		__handle_sysrq(c, false);
+
+		if (c == '_')
+			bulk = true;
+		else
+			__handle_sysrq(c, false);
+
+		if (!bulk)
+			break;
 	}
 
 	return count;
-- 
2.42.1

  reply	other threads:[~2023-11-20 11:15 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-13 18:22 [PATCH] /proc/sysrq-trigger: accept multiple keys at once Tomas Mudrunka
2023-11-13 18:33 ` Greg Kroah-Hartman
2023-11-13 21:09   ` Tomáš Mudruňka
2023-11-13 21:37     ` Greg Kroah-Hartman
2023-11-14  9:35       ` [PATCH v2] " Tomas Mudrunka
2023-11-14  9:44         ` Jiri Slaby
2023-11-14  9:49           ` [PATCH v3] " Tomas Mudrunka
2023-11-14  9:50             ` Jiri Slaby
2023-11-14  9:55               ` [PATCH v4] " Tomas Mudrunka
2023-11-14 12:14                 ` Greg KH
2023-11-14 12:41                   ` [PATCH v5] " Tomas Mudrunka
2023-11-14 15:12                     ` [PATCH v6] " Tomas Mudrunka
2023-11-14 18:04                       ` Jiri Slaby
2023-11-14 22:00                         ` Randy Dunlap
2023-11-15 10:10                           ` Jiri Slaby
2023-11-15 10:27                             ` [PATCH v7] " Tomas Mudrunka
2023-11-15 10:29                             ` [PATCH v8] " Tomas Mudrunka
2023-11-15 10:34                             ` [PATCH v9] " Tomas Mudrunka
2023-11-20  7:45                               ` Jiri Slaby
2023-11-20 11:14                                 ` Tomas Mudrunka [this message]
2023-11-20 11:32                                   ` [PATCH v10] " Jiri Slaby

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=20231120111451.527952-1-tomas.mudrunka@gmail.com \
    --to=tomas.mudrunka@gmail.com \
    --cc=corbet@lwn.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=jirislaby@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=rdunlap@infradead.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).