From nobody Sat Feb 05 21:35:39 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id C8C7B19A18CA; Sat, 5 Feb 2022 21:35:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Jrm0q5BKQz3Dqm; Sat, 5 Feb 2022 21:35:39 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644096939; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hKBuuIYpNVE7VZ8Do/EXafs6K7RQHIVq2Ym8XUzWTfQ=; b=FRYk/3/00opeqI0/KfSjGKFpiNS5ZYvo58tlVbDNVjAw1Nfn3yBL5Bc+8wPkQ8zfdSaThJ ve7Cv+aMGDlalwKYRVFIxj1GbDolZZc3V9ShH0kwhbutkygNPMj3EtHmJO/vESOQuUbLYg kza2yE/NaiHKifTQEK0srRSFcpTua83cuqGdWxDj2jrrdtZaaNhz54Z2aDL4YcRDjw5sLV vz5Fya5SX2Si3y1izsITaDDDCTJ04d+5z+NmyxwXdsaatJslXSLs6Fat8cNasWw1TrY6aZ ZcC+nVnWl7VDcCtnpvDt0JvOQNudROf5pLqmw85YRW3oXTHMqWsEF0kQAbxhKg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 874B4266A9; Sat, 5 Feb 2022 21:35:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 215LZdvQ055370; Sat, 5 Feb 2022 21:35:39 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 215LZdbx055369; Sat, 5 Feb 2022 21:35:39 GMT (envelope-from git) Date: Sat, 5 Feb 2022 21:35:39 GMT Message-Id: <202202052135.215LZdbx055369@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: c999e3481d93 - main - dmesg: detect wrapped msgbuf on the kernel side and if so, skip first line List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c999e3481d936980354d09e3d6a138e5dde5fabc Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644096939; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hKBuuIYpNVE7VZ8Do/EXafs6K7RQHIVq2Ym8XUzWTfQ=; b=CB7eei628x+TFYi2b2XxZOHZjngOPXIoCnU0vtK3DiOOO1ArtvSAu9xJzfG7cJdcDwmca1 TagU06YdPC8ivrrE4oavFR+sR2ZKh0KNZgVH2rtx5wjcDl2RfGI2a0mCau8m0CgL8NzQLa sW6wzM0j17WUZI0Icjc8ryKBJQRBZUHxzgB59dyHWS+Kgiy5EG1izphzELrCpY/M5mQQjj SMzpIjphubzdSYae8w+xN0wu0qWRvcm9VD4kI+Eutb8PnyOZkpoQmnWzAOLTPMmRYCa8xm xUqF4V1AgejNeguTkRzEp44mKA0sO5X8dpRcVtZ3NsY30E4SPRSMBhHiVQeB9w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1644096939; a=rsa-sha256; cv=none; b=GXVYp+ZiFKJv02TRAQEfldoEiCYAgCr4kU+KSh15ZSP7/QZsQKLlozdvpwgfqRzXHAw05m GA8qiBRIm2ow70uQS9T9aYjuZXiLTKB3TJCXBOMHLJjOyEvLHAx5aGtqwB6l5d0ijg8jgC NdQkNzcfnbexjt77V+apVKjqJ2deWxZm+Dns+90PiukuPed22DHFPils8M7f0RgDI6WNV7 paceU20AxdWxhXVKIEidfI5qb17Ymi/a6hY7iCSjCTB+/Pcr0OaBVdXo+wYjIRWqH/GKf8 iUaMrltTKkEdhmLiULBI+7davSRw8qXmfZ8Z8myAwk7u/bWeslKmDZoBfRdK1w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=c999e3481d936980354d09e3d6a138e5dde5fabc commit c999e3481d936980354d09e3d6a138e5dde5fabc Author: Gleb Smirnoff AuthorDate: 2022-02-05 21:25:38 +0000 Commit: Gleb Smirnoff CommitDate: 2022-02-05 21:35:31 +0000 dmesg: detect wrapped msgbuf on the kernel side and if so, skip first line Since 59f256ec35d3 dmesg(8) will always skip first line of the message buffer, cause it might be incomplete. The problem is that in most cases it is complete, valid and contains the "---<>---" marker. This skip can be disabled with '-a', but that would also unhide all non-kernel messages. Move this functionality from dmesg(8) to kernel, since kernel actually knows if wrap has happened or not. The main motivation for the change is not actually the value of the "---<>---" marker. The problem breaks unit tests, that clear message buffer, perform a test and then check the message buffer for a result. Example of such test is sys/kern/sonewconn_overflow. --- sbin/dmesg/dmesg.c | 4 ---- sys/kern/subr_prf.c | 23 ++++++++++++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/sbin/dmesg/dmesg.c b/sbin/dmesg/dmesg.c index 469582204f7b..f266e6b98b39 100644 --- a/sbin/dmesg/dmesg.c +++ b/sbin/dmesg/dmesg.c @@ -184,10 +184,6 @@ main(int argc, char *argv[]) /* Strip leading \0's */ while (*p == '\0') p++; - } else if (!all) { - /* Skip the first line, since it is probably incomplete. */ - p = memchr(p, '\n', ep - p); - p++; } for (; p < ep; p = nextp) { nextp = memchr(p, '\n', ep - p); diff --git a/sys/kern/subr_prf.c b/sys/kern/subr_prf.c index 1106587ebbe7..62a0bd0a3699 100644 --- a/sys/kern/subr_prf.c +++ b/sys/kern/subr_prf.c @@ -1058,9 +1058,10 @@ msgbufinit(void *ptr, int size) static int sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS) { - char buf[128]; + char buf[128], *bp; u_int seq; int error, len; + bool wrap; error = priv_check(req->td, PRIV_MSGBUF); if (error) @@ -1069,13 +1070,29 @@ sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS) /* Read the whole buffer, one chunk at a time. */ mtx_lock(&msgbuf_lock); msgbuf_peekbytes(msgbufp, NULL, 0, &seq); + wrap = (seq != 0); for (;;) { len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq); mtx_unlock(&msgbuf_lock); if (len == 0) return (SYSCTL_OUT(req, "", 1)); /* add nulterm */ - - error = sysctl_handle_opaque(oidp, buf, len, req); + if (wrap) { + /* Skip the first line, as it is probably incomplete. */ + bp = memchr(buf, '\n', len); + if (bp == NULL) { + mtx_lock(&msgbuf_lock); + continue; + } + wrap = false; + bp++; + len -= bp - buf; + if (len == 0) { + mtx_lock(&msgbuf_lock); + continue; + } + } else + bp = buf; + error = sysctl_handle_opaque(oidp, bp, len, req); if (error) return (error);