From nobody Tue Jan 16 16:50:10 2024 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 4TDw3l2V2mz57bXF; Tue, 16 Jan 2024 16:50:11 +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 4TDw3l0jSBz4c84; Tue, 16 Jan 2024 16:50:11 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705423811; 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=Myub/I6DNmrmT1gq+xX/BfqI6G/A4uXdpVsW1EN9rMc=; b=d86yY2CKRaWq9AIE8UcebdeI/iEm+IlRQ4iQNCIIrM5x1lBDH8qTBpxyTQRov8N8wObBTF JajEUmyfH45Whbki7yGz4Oov6GPF6U8HqxpRsei1nPgdbAGF241qYmy4i3QcEToep/DHDP U8OPNky9jK6h0VvyOK6J51MvHAhlI7990odFc2AihaOXlIjJzdP9B6objdhJGsUgw77doQ KqQ7JBxKHUNap9xwstUFuiq88hzYrXptO/T11aKhLw5yUKgraOscP49W4t53+58QBWUZ9W Ujsg6a6rrNZmqMOGOhbeC2SlLphO2SInY4IPWaUhdnvLwJIzngF16LffUpXa2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705423811; 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=Myub/I6DNmrmT1gq+xX/BfqI6G/A4uXdpVsW1EN9rMc=; b=WJUF4YQhrOY9EvzQ4RZMBPbqScDC0zSqr5VngMOyq8z477tpYCwjQP/2eLxhBQR9+1TEse bdPoT8I1eLcFDTTwD9sM/3y6ljCGnRBSitGw470YV7pDsqLfWy0uhUOV17N+wYFuHCAl9K vzBfYo/QRjywY320RSedggsqEeSS4hglQVTFv81+KQJ1zsNPWpH9zoHnYyI0p/hl11EHbM 1EbF5Wcx8d33koxAySbDxRj2pJgdztEDU+rcc4Fv+fAmQ+HpiRmCkNu5tgvgsAZQsUUB53 sZiXDY3AcXPolubi+BwE6aMyQL15bOE0rm0tNI8lv1RbT7Ug2F7+kLCETpmkow== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1705423811; a=rsa-sha256; cv=none; b=fSr/Kz0NeEUuoViUQ3nhgiUF5LcUZ93VlnXNamw1JoVMe4oG9ZgD7bA3z+kLOckwJIOEb4 vvkmASwqFCFXpsoQqaHTlVZZKFObgcwQvUqlRZKjsohpC2FkNX+FUCkCEnJYrvprwJqBMk X0XKMas6k/SVF6MTnulTGQ93cTEX0Qp+QvgE4QeF/AZaKeHxSYVVxz4iYmx1U9sjvx4nmu nEfgrgrw3pN3hT3o5Bvh6vlEu+l/9br2GkkvmcoSBAyTuJVFlry3e/HiBS4minTMS1LT16 3lfDkOn+bUSgyTA+U1wh7cBpc0rfWqlrz+F2Q7VetPO0LQE9dbMt2ii554ZRog== 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 4TDw3k6x8Gznq5; Tue, 16 Jan 2024 16:50:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 40GGoAPU076065; Tue, 16 Jan 2024 16:50:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40GGoAj2076056; Tue, 16 Jan 2024 16:50:10 GMT (envelope-from git) Date: Tue, 16 Jan 2024 16:50:10 GMT Message-Id: <202401161650.40GGoAj2076056@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Christos Margiolis Subject: git: 6b6914c1e21b - main - subr_bus: introduce device_set_descf() and modify allocation logic 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: christos X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6b6914c1e21b625503a1b8d8d5cfdfbc4c6a6acd Auto-Submitted: auto-generated The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=6b6914c1e21b625503a1b8d8d5cfdfbc4c6a6acd commit 6b6914c1e21b625503a1b8d8d5cfdfbc4c6a6acd Author: Christos Margiolis AuthorDate: 2024-01-16 16:49:15 +0000 Commit: Christos Margiolis CommitDate: 2024-01-16 16:49:15 +0000 subr_bus: introduce device_set_descf() and modify allocation logic device_set_descf() is a printf-like version of device_set_desc(). Allocation code has been transferred from device_set_desc_internal() to device_set_desc_copy() and device_set_descf() to avoid complicating device_set_desc_internal(). The "copy" argument in device_set_desc_internal() has been replaced with a flag which is set when the description string has been allocated with M_BUS. Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Reviewed by: imp, markj Differential Revision: https://reviews.freebsd.org/D43370 --- share/man/man9/Makefile | 1 + share/man/man9/device_set_desc.9 | 8 +++++++- sys/kern/subr_bus.c | 43 ++++++++++++++++++++++++++-------------- sys/sys/bus.h | 1 + 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile index 863963d24474..3f9572796fa1 100644 --- a/share/man/man9/Makefile +++ b/share/man/man9/Makefile @@ -1012,6 +1012,7 @@ MLINKS+=device_get_sysctl.9 device_get_sysctl_ctx.9 \ MLINKS+=device_quiet.9 device_is_quiet.9 \ device_quiet.9 device_verbose.9 MLINKS+=device_set_desc.9 device_get_desc.9 \ + device_set_desc.9 device_set_descf.9 \ device_set_desc.9 device_set_desc_copy.9 MLINKS+=device_set_flags.9 device_get_flags.9 MLINKS+=devstat.9 devicestat.9 \ diff --git a/share/man/man9/device_set_desc.9 b/share/man/man9/device_set_desc.9 index 69d6cfe18c6b..5037c90fedb1 100644 --- a/share/man/man9/device_set_desc.9 +++ b/share/man/man9/device_set_desc.9 @@ -26,11 +26,12 @@ .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd June 16, 1998 +.Dd January 9, 2024 .Dt DEVICE_SET_DESC 9 .Os .Sh NAME .Nm device_set_desc , +.Nm device_set_descf , .Nm device_set_desc_copy , .Nm device_get_desc .Nd access the description of a device @@ -40,6 +41,8 @@ .Ft void .Fn device_set_desc "device_t dev" "const char *desc" .Ft void +.Fn device_set_descf "device_t dev" "const char *fmt" "..." +.Ft void .Fn device_set_desc_copy "device_t dev" "const char *desc" .Ft const char * .Fn device_get_desc "device_t dev" @@ -54,6 +57,9 @@ is used to set the description if the string passed is a temporary buffer which will be overwritten. In this case, the system will copy the string, otherwise the pointer passed will be used directly. +.Fn device_set_descf +is a printf-like version of +.Fn device_set_desc . .Sh SEE ALSO .Xr device 9 .Sh AUTHORS diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c index 4f7053cc2613..21c2835f2ae6 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -1997,7 +1997,7 @@ device_log(device_t dev, int pri, const char * fmt, ...) * @internal */ static void -device_set_desc_internal(device_t dev, const char* desc, int copy) +device_set_desc_internal(device_t dev, const char *desc, bool allocated) { if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { free(dev->desc, M_BUS); @@ -2005,16 +2005,9 @@ device_set_desc_internal(device_t dev, const char* desc, int copy) dev->desc = NULL; } - if (copy && desc) { - dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); - if (dev->desc) { - strcpy(dev->desc, desc); - dev->flags |= DF_DESCMALLOCED; - } - } else { - /* Avoid a -Wcast-qual warning */ - dev->desc = (char *)(uintptr_t) desc; - } + if (allocated && desc) + dev->flags |= DF_DESCMALLOCED; + dev->desc = __DECONST(char *, desc); bus_data_generation_update(); } @@ -2027,9 +2020,26 @@ device_set_desc_internal(device_t dev, const char* desc, int copy) * call to device_set_desc() or device_set_desc_copy()). */ void -device_set_desc(device_t dev, const char* desc) +device_set_desc(device_t dev, const char *desc) +{ + device_set_desc_internal(dev, desc, false); +} + +/** + * @brief Set the device's description + * + * A printf-like version of device_set_desc(). + */ +void +device_set_descf(device_t dev, const char *fmt, ...) { - device_set_desc_internal(dev, desc, FALSE); + va_list ap; + char *buf = NULL; + + va_start(ap, fmt); + vasprintf(&buf, M_BUS, fmt, ap); + va_end(ap); + device_set_desc_internal(dev, buf, true); } /** @@ -2039,9 +2049,12 @@ device_set_desc(device_t dev, const char* desc) * the device description is generated, (e.g. with sprintf()). */ void -device_set_desc_copy(device_t dev, const char* desc) +device_set_desc_copy(device_t dev, const char *desc) { - device_set_desc_internal(dev, desc, TRUE); + char *buf; + + buf = strdup_flags(desc, M_BUS, M_NOWAIT); + device_set_desc_internal(dev, buf, true); } /** diff --git a/sys/sys/bus.h b/sys/sys/bus.h index 2ec735659452..581e81352be4 100644 --- a/sys/sys/bus.h +++ b/sys/sys/bus.h @@ -660,6 +660,7 @@ int device_quiesce(device_t dev); void device_quiet(device_t dev); void device_quiet_children(device_t dev); void device_set_desc(device_t dev, const char* desc); +void device_set_descf(device_t dev, const char* fmt, ...) __printflike(2, 3); void device_set_desc_copy(device_t dev, const char* desc); int device_set_devclass(device_t dev, const char *classname); int device_set_devclass_fixed(device_t dev, const char *classname);