From owner-svn-src-head@freebsd.org Sat Aug 29 04:30:13 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 45DE83CC26C; Sat, 29 Aug 2020 04:30:13 +0000 (UTC) (envelope-from imp@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 "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Bdk5w5ZQMz3dvW; Sat, 29 Aug 2020 04:30:12 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 87A692048A; Sat, 29 Aug 2020 04:30:12 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 07T4UCk7007929; Sat, 29 Aug 2020 04:30:12 GMT (envelope-from imp@FreeBSD.org) Received: (from imp@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 07T4UCM4007928; Sat, 29 Aug 2020 04:30:12 GMT (envelope-from imp@FreeBSD.org) Message-Id: <202008290430.07T4UCM4007928@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: imp set sender to imp@FreeBSD.org using -f From: Warner Losh Date: Sat, 29 Aug 2020 04:30:12 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r364946 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: imp X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 364946 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 29 Aug 2020 04:30:13 -0000 Author: imp Date: Sat Aug 29 04:30:12 2020 New Revision: 364946 URL: https://svnweb.freebsd.org/changeset/base/364946 Log: Move to using sbuf for some sysctl in newbus Convert two different sysctl to using sbuf. First, for all the default sysctls we implement for each device driver that's attached. This is a pure sbuf conversion. Second, convert sysctl_devices to fill its buffer with sbuf rather than a hand-rolled crappy thing I wrote years ago. Reviewed by: cem, markj Differential Revision: https://reviews.freebsd.org/D26206 Modified: head/sys/kern/subr_bus.c Modified: head/sys/kern/subr_bus.c ============================================================================== --- head/sys/kern/subr_bus.c Sat Aug 29 04:30:06 2020 (r364945) +++ head/sys/kern/subr_bus.c Sat Aug 29 04:30:12 2020 (r364946) @@ -260,36 +260,33 @@ enum { static int device_sysctl_handler(SYSCTL_HANDLER_ARGS) { + struct sbuf sb; device_t dev = (device_t)arg1; - const char *value; - char *buf; int error; - buf = NULL; + sbuf_new_for_sysctl(&sb, NULL, 1024, req); switch (arg2) { case DEVICE_SYSCTL_DESC: - value = dev->desc ? dev->desc : ""; + sbuf_cpy(&sb, dev->desc ? dev->desc : ""); break; case DEVICE_SYSCTL_DRIVER: - value = dev->driver ? dev->driver->name : ""; + sbuf_cpy(&sb, dev->driver ? dev->driver->name : ""); break; case DEVICE_SYSCTL_LOCATION: - value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO); - bus_child_location_str(dev, buf, 1024); + bus_child_location_sb(dev, &sb); break; case DEVICE_SYSCTL_PNPINFO: - value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO); - bus_child_pnpinfo_str(dev, buf, 1024); + bus_child_pnpinfo_sb(dev, &sb); break; case DEVICE_SYSCTL_PARENT: - value = dev->parent ? dev->parent->nameunit : ""; + sbuf_cpy(&sb, dev->parent ? dev->parent->nameunit : ""); break; default: + sbuf_delete(&sb); return (EINVAL); } - error = SYSCTL_OUT_STR(req, value); - if (buf != NULL) - free(buf, M_BUS); + error = sbuf_finish(&sb); + sbuf_delete(&sb); return (error); } @@ -5464,13 +5461,13 @@ SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | static int sysctl_devices(SYSCTL_HANDLER_ARGS) { + struct sbuf sb; int *name = (int *)arg1; u_int namelen = arg2; int index; device_t dev; struct u_device *udev; int error; - char *walker, *ep; if (namelen != 2) return (EINVAL); @@ -5501,34 +5498,21 @@ sysctl_devices(SYSCTL_HANDLER_ARGS) udev->dv_devflags = dev->devflags; udev->dv_flags = dev->flags; udev->dv_state = dev->state; - walker = udev->dv_fields; - ep = walker + sizeof(udev->dv_fields); -#define CP(src) \ - if ((src) == NULL) \ - *walker++ = '\0'; \ - else { \ - strlcpy(walker, (src), ep - walker); \ - walker += strlen(walker) + 1; \ - } \ - if (walker >= ep) \ - break; - - do { - CP(dev->nameunit); - CP(dev->desc); - CP(dev->driver != NULL ? dev->driver->name : NULL); - bus_child_pnpinfo_str(dev, walker, ep - walker); - walker += strlen(walker) + 1; - if (walker >= ep) - break; - bus_child_location_str(dev, walker, ep - walker); - walker += strlen(walker) + 1; - if (walker >= ep) - break; - *walker++ = '\0'; - } while (0); -#undef CP - error = SYSCTL_OUT(req, udev, sizeof(*udev)); + sbuf_new(&sb, udev->dv_fields, sizeof(udev->dv_fields), SBUF_FIXEDLEN); + sbuf_cat(&sb, dev->nameunit); + sbuf_putc(&sb, '\0'); + sbuf_cat(&sb, dev->desc); + sbuf_putc(&sb, '\0'); + sbuf_cat(&sb, dev->driver != NULL ? dev->driver->name : '\0'); + sbuf_putc(&sb, '\0'); + bus_child_pnpinfo_sb(dev, &sb); + sbuf_putc(&sb, '\0'); + bus_child_location_sb(dev, &sb); + sbuf_putc(&sb, '\0'); + error = sbuf_finish(&sb); + if (error == 0) + error = SYSCTL_OUT(req, udev, sizeof(*udev)); + sbuf_delete(&sb); free(udev, M_BUS); return (error); }