From nobody Tue Jan 30 11:08:30 2024 X-Original-To: dev-commits-src-all@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 4TPMq33V4qz58dQM; Tue, 30 Jan 2024 11:08:31 +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 4TPMq31yMsz4HgM; Tue, 30 Jan 2024 11:08:31 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1706612911; 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=m4If+AXt3IzpXI3ke07ptOuehaCq/IrSl4IGbF1XSC0=; b=NGPYedixPk6XvpfsX7d3YKSLW8Nc/CKMUiOUchWOCNFBbwvw4XJU3GxKQTGNDAjM+srD4r 5c34psJxh8Ny6jJ2aEj7buUIhLH/gIYy6W8hhmK8TsAi5dS9Cdxj/rWE/VWFeJOmpBI7o7 iWAQG3ZxNSubKUEYXLyXl2lts/9Il18zYryq6aXhUKWK5dpCBRks8h3lPBRRZyDHI5e6Fw MFAtDYARyYC0X0gwCFAcvhWNFtp8qKI/mRRXQGNG215F5Vzmv1EqLyuOeVbIqxuRowifZs TQyaHVF3FqtRuSTah2aSmLWkfCIO9XeHrxEC0+P4wTZsc1OnxBoThjTdU3FA0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1706612911; 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=m4If+AXt3IzpXI3ke07ptOuehaCq/IrSl4IGbF1XSC0=; b=va6eo8hXEUEwQKF9pdPBMpR6zWibKZScvPMYY2+8jwjFi0QUX0WLIyhXnXAarCabfY8sSB dS7hCTcXmaEODkdQg1t6v6f1htqee+GBhavOn0qV9maPOk/x9XZ/rIub1Wjg3DOUIxao01 R+x0lJo90OBh9lnwabOC4cg/Ln8329c75Oqtersvf8lWWwiqSJUoATeG/+yD5V7EEHPjyw CkIlLDmIIY6VEDENKvY8gdmMWai8JMLW09WVGw9S9JTfmw2eo4W9RexZ6cwtqTfZn0Wl7i FpsXDBwS2/JK9j4qDUDrtYU0ZiBYnZ4qOlB1Sqty2dVpZjNfQmGuukr7nDc9RA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1706612911; a=rsa-sha256; cv=none; b=hhg3wMUNtJTovKzSMfIsgr3ya3PB7CT/8CB1GsEGOGENWIC2DS6BkLyf+3nPP82aErWfu+ Pv0h24XMT51FGBK2yvJF6+epqamMrFDn1VGXIIPtsmrAGDYXvPbPQIKdKan2qncxAc1WiL a2Ky6aC4BZWF5C4TmyOFEkgqBbVEyNL0U9CepSU82jl1LOrGRdBlNTQ9HUCF+zBbHGcPuB 2P30cxkgJgXnCHDk52hoqtX7QMhg+BnwWrGbGRN2Fs8vJuOeDVfG/+MSdSg96ltZ2CMvBO LaxRK1/M2PqKpjWw+ifg5J8/gPNvj45klyEfVOLMvqsfD27XTd5uK6LC6xW62w== 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 4TPMq2725ZzYXD; Tue, 30 Jan 2024 11:08:30 +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 40UB8UJ5076118; Tue, 30 Jan 2024 11:08:30 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40UB8UNj076115; Tue, 30 Jan 2024 11:08:30 GMT (envelope-from git) Date: Tue, 30 Jan 2024 11:08:30 GMT Message-Id: <202401301108.40UB8UNj076115@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Christos Margiolis Subject: git: 847d9b31df5e - stable/14 - subr_bus: introduce device_set_descf() and modify allocation logic List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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/stable/14 X-Git-Reftype: branch X-Git-Commit: 847d9b31df5e6d9fb41a0d4827b88fb4cd21068e Auto-Submitted: auto-generated The branch stable/14 has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=847d9b31df5e6d9fb41a0d4827b88fb4cd21068e commit 847d9b31df5e6d9fb41a0d4827b88fb4cd21068e Author: Christos Margiolis AuthorDate: 2024-01-16 16:49:15 +0000 Commit: Christos Margiolis CommitDate: 2024-01-30 11:07:29 +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 (cherry picked from commit 6b6914c1e21b625503a1b8d8d5cfdfbc4c6a6acd) --- 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 77b2038628be..505298899561 100644 --- a/share/man/man9/Makefile +++ b/share/man/man9/Makefile @@ -1011,6 +1011,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 040b3556d156..d3db7ca5431c 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);