Received: by 2002:a05:7412:b130:b0:e2:908c:2ebd with SMTP id az48csp2140427rdb; Mon, 20 Nov 2023 03:15:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IFjx1OrONfG8DJh9u8M3fdp+GTGSSan1U4KglMD8SLFlIOuhM29LwjTcJYtY1AIP2XjXSe0 X-Received: by 2002:a17:90b:1647:b0:27d:8d42:6def with SMTP id il7-20020a17090b164700b0027d8d426defmr5052667pjb.34.1700478935586; Mon, 20 Nov 2023 03:15:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700478935; cv=none; d=google.com; s=arc-20160816; b=OOcc86YmlW9jTseTKtxaM8BQu0opGXPisxGRqPE0B+R/H+JoBD48lodAGUOCA2GrSC /9dPewgAouq8AddthlMHwZlvewATyh9nOF5Xyx1+jxB4oi8EU+b1CeoR0TPp8mJR+FN9 HKQCB5iCblSXsBZ3SrksE1KfQWlcqhbMwTzT1P6nzb66ML59/6rIgF2CrhtzkuwrBGX7 TCPaLkKpDEslpcA1/CvTZ04Ni0j9VPYmI3RE9u9XU1Ig9wMGrDXzlKnQ9a4gg+qKzjlm N47cP8KbtqHktESy+rqGYxHUB16AJqWS3TolIQSRhFZfSzs0gWv3supYwzwwUnP8dKNy qojg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Kx4OSzBy71ErWYuTZ7i+vm9Kv+eXaGYsd47vFpY0rQ0=; fh=6j36m1jHKU7KTFZCDlX/JQDgr7Jf8Wr470TB4VSI5uk=; b=tGkEAzSXVXIDnVdpow9L1tSGoyN8RGzhfXMIjvh7Wiuc5x/ikkclqWgrINdp7PJ0aY nxbf7QtdXvEVwIXTPDNnkXpeSTxSJ4vpWfJxF5j/a3LRX/vRrHM0Df7URp7oeLbwEqQC w3gQHWtyFow/DTYYudKKqYfBbo0/3nKZcdu6vCkHkKbSPQir2oLjZ5sxPvN3lXf8pO8W apb1+mm+gZ3eGjM8IfQMC0a9YNhlbtgLzfWhPSsFmS97BhxCUunBwG0BSqU0n3Plf3bZ VfCMHTZeGJnDyLQMlnlc1eY04IsPDL9sSTUbMt+f0X/a/e454aUqFUx50bIiGcCHONcu lpLQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id b6-20020a170902d30600b001c1e1fe16cbsi7988860plc.255.2023.11.20.03.15.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 03:15:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id A3EDC8026DD4; Mon, 20 Nov 2023 03:15:33 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233243AbjKTLPX (ORCPT + 99 others); Mon, 20 Nov 2023 06:15:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233250AbjKTLPE (ORCPT ); Mon, 20 Nov 2023 06:15:04 -0500 Received: from harvie.cz (harvie.cz [77.87.242.242]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D73CB194; Mon, 20 Nov 2023 03:15:00 -0800 (PST) Received: from anemophobia.amit.cz (unknown [31.30.84.130]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by harvie.cz (Postfix) with ESMTPSA id 1E01F18023E; Mon, 20 Nov 2023 12:14:58 +0100 (CET) From: Tomas Mudrunka 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 Message-ID: <20231120111451.527952-1-tomas.mudrunka@gmail.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=0.7 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_PASS,SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 20 Nov 2023 03:15:33 -0800 (PST) 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 --- 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:`` 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