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
This commit is contained in:
Mark Johnston 2020-09-02 18:17:08 +00:00
parent 847ab36bf2
commit a0efcf6400
2 changed files with 29 additions and 4 deletions

View File

@ -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;

View File

@ -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;