Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 2 Sep 2020 18:17:08 +0000 (UTC)
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r365268 - in head: sbin/sysctl sys/kern
Message-ID:  <202009021817.082IH8nM005599@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: markj
Date: Wed Sep  2 18:17:08 2020
New Revision: 365268
URL: https://svnweb.freebsd.org/changeset/base/365268

Log:
  Add sysctl(8) formatting for hw.pagesizes.
  
  - Change the type of hw.pagesizes to OPAQUE, since it returns an array.
  - Modify the handler to only truncate the returned length if the caller
    supplied an output buffer.  This allows use of the trick of passing a
    NULL output buffer to fetch the output size, while preserving
    compatibility if MAXPAGESIZES is increased.
  - Add a "S,pagesize" formatter to sysctl(8).
  
  Reviewed by:	alc, kib
  MFC after:	2 weeks
  Sponsored by:	Juniper Networks, Inc.
  Sponsored by:	Klara, Inc.
  Differential Revision:	https://reviews.freebsd.org/D26239

Modified:
  head/sbin/sysctl/sysctl.c
  head/sys/kern/kern_mib.c

Modified: head/sbin/sysctl/sysctl.c
==============================================================================
--- head/sbin/sysctl/sysctl.c	Wed Sep  2 18:16:43 2020	(r365267)
+++ head/sbin/sysctl/sysctl.c	Wed Sep  2 18:17:08 2020	(r365268)
@@ -697,6 +697,29 @@ S_input_id(size_t l2, void *p)
 	return (0);
 }
 
+static int
+S_pagesizes(size_t l2, void *p)
+{
+	char buf[256];
+	u_long *ps;
+	size_t l;
+	int i;
+
+	l = snprintf(buf, sizeof(buf), "{ ");
+	ps = p;
+	for (i = 0; i * sizeof(*ps) < l2 && ps[i] != 0 && l < sizeof(buf);
+	    i++) {
+		l += snprintf(&buf[l], sizeof(buf) - l,
+		    "%s%lu", i == 0 ? "" : ", ", ps[i]);
+	}
+	if (l < sizeof(buf))
+		(void)snprintf(&buf[l], sizeof(buf) - l, " }");
+
+	printf("%s", buf);
+
+	return (0);
+}
+
 #ifdef __amd64__
 static int
 S_efi_map(size_t l2, void *p)
@@ -1002,6 +1025,8 @@ show_var(int *oid, int nlen)
 			func = S_vmtotal;
 		else if (strcmp(fmt, "S,input_id") == 0)
 			func = S_input_id;
+		else if (strcmp(fmt, "S,pagesizes") == 0)
+			func = S_pagesizes;
 #ifdef __amd64__
 		else if (strcmp(fmt, "S,efi_map_header") == 0)
 			func = S_efi_map;

Modified: head/sys/kern/kern_mib.c
==============================================================================
--- head/sys/kern/kern_mib.c	Wed Sep  2 18:16:43 2020	(r365267)
+++ head/sys/kern/kern_mib.c	Wed Sep  2 18:17:08 2020	(r365268)
@@ -246,22 +246,22 @@ sysctl_hw_pagesizes(SYSCTL_HANDLER_ARGS)
 			pagesizes32[i] = (uint32_t)pagesizes[i];
 
 		len = sizeof(pagesizes32);
-		if (len > req->oldlen)
+		if (len > req->oldlen && req->oldptr != NULL)
 			len = req->oldlen;
 		error = SYSCTL_OUT(req, pagesizes32, len);
 	} else
 #endif
 	{
 		len = sizeof(pagesizes);
-		if (len > req->oldlen)
+		if (len > req->oldlen && req->oldptr != NULL)
 			len = req->oldlen;
 		error = SYSCTL_OUT(req, pagesizes, len);
 	}
 	return (error);
 }
 SYSCTL_PROC(_hw, OID_AUTO, pagesizes,
-    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 
-    sysctl_hw_pagesizes, "LU",
+    CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
+    sysctl_hw_pagesizes, "S,pagesizes",
     "Supported page sizes");
 
 int adaptive_machine_arch = 1;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202009021817.082IH8nM005599>