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
This commit is contained in:
parent
887611b122
commit
17c219fd6f
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=364946
@ -260,36 +260,33 @@ enum {
|
|||||||
static int
|
static int
|
||||||
device_sysctl_handler(SYSCTL_HANDLER_ARGS)
|
device_sysctl_handler(SYSCTL_HANDLER_ARGS)
|
||||||
{
|
{
|
||||||
|
struct sbuf sb;
|
||||||
device_t dev = (device_t)arg1;
|
device_t dev = (device_t)arg1;
|
||||||
const char *value;
|
|
||||||
char *buf;
|
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
buf = NULL;
|
sbuf_new_for_sysctl(&sb, NULL, 1024, req);
|
||||||
switch (arg2) {
|
switch (arg2) {
|
||||||
case DEVICE_SYSCTL_DESC:
|
case DEVICE_SYSCTL_DESC:
|
||||||
value = dev->desc ? dev->desc : "";
|
sbuf_cpy(&sb, dev->desc ? dev->desc : "");
|
||||||
break;
|
break;
|
||||||
case DEVICE_SYSCTL_DRIVER:
|
case DEVICE_SYSCTL_DRIVER:
|
||||||
value = dev->driver ? dev->driver->name : "";
|
sbuf_cpy(&sb, dev->driver ? dev->driver->name : "");
|
||||||
break;
|
break;
|
||||||
case DEVICE_SYSCTL_LOCATION:
|
case DEVICE_SYSCTL_LOCATION:
|
||||||
value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
|
bus_child_location_sb(dev, &sb);
|
||||||
bus_child_location_str(dev, buf, 1024);
|
|
||||||
break;
|
break;
|
||||||
case DEVICE_SYSCTL_PNPINFO:
|
case DEVICE_SYSCTL_PNPINFO:
|
||||||
value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
|
bus_child_pnpinfo_sb(dev, &sb);
|
||||||
bus_child_pnpinfo_str(dev, buf, 1024);
|
|
||||||
break;
|
break;
|
||||||
case DEVICE_SYSCTL_PARENT:
|
case DEVICE_SYSCTL_PARENT:
|
||||||
value = dev->parent ? dev->parent->nameunit : "";
|
sbuf_cpy(&sb, dev->parent ? dev->parent->nameunit : "");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
sbuf_delete(&sb);
|
||||||
return (EINVAL);
|
return (EINVAL);
|
||||||
}
|
}
|
||||||
error = SYSCTL_OUT_STR(req, value);
|
error = sbuf_finish(&sb);
|
||||||
if (buf != NULL)
|
sbuf_delete(&sb);
|
||||||
free(buf, M_BUS);
|
|
||||||
return (error);
|
return (error);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -5464,13 +5461,13 @@ SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | CTLFLAG_RD |
|
|||||||
static int
|
static int
|
||||||
sysctl_devices(SYSCTL_HANDLER_ARGS)
|
sysctl_devices(SYSCTL_HANDLER_ARGS)
|
||||||
{
|
{
|
||||||
|
struct sbuf sb;
|
||||||
int *name = (int *)arg1;
|
int *name = (int *)arg1;
|
||||||
u_int namelen = arg2;
|
u_int namelen = arg2;
|
||||||
int index;
|
int index;
|
||||||
device_t dev;
|
device_t dev;
|
||||||
struct u_device *udev;
|
struct u_device *udev;
|
||||||
int error;
|
int error;
|
||||||
char *walker, *ep;
|
|
||||||
|
|
||||||
if (namelen != 2)
|
if (namelen != 2)
|
||||||
return (EINVAL);
|
return (EINVAL);
|
||||||
@ -5501,34 +5498,21 @@ sysctl_devices(SYSCTL_HANDLER_ARGS)
|
|||||||
udev->dv_devflags = dev->devflags;
|
udev->dv_devflags = dev->devflags;
|
||||||
udev->dv_flags = dev->flags;
|
udev->dv_flags = dev->flags;
|
||||||
udev->dv_state = dev->state;
|
udev->dv_state = dev->state;
|
||||||
walker = udev->dv_fields;
|
sbuf_new(&sb, udev->dv_fields, sizeof(udev->dv_fields), SBUF_FIXEDLEN);
|
||||||
ep = walker + sizeof(udev->dv_fields);
|
sbuf_cat(&sb, dev->nameunit);
|
||||||
#define CP(src) \
|
sbuf_putc(&sb, '\0');
|
||||||
if ((src) == NULL) \
|
sbuf_cat(&sb, dev->desc);
|
||||||
*walker++ = '\0'; \
|
sbuf_putc(&sb, '\0');
|
||||||
else { \
|
sbuf_cat(&sb, dev->driver != NULL ? dev->driver->name : '\0');
|
||||||
strlcpy(walker, (src), ep - walker); \
|
sbuf_putc(&sb, '\0');
|
||||||
walker += strlen(walker) + 1; \
|
bus_child_pnpinfo_sb(dev, &sb);
|
||||||
} \
|
sbuf_putc(&sb, '\0');
|
||||||
if (walker >= ep) \
|
bus_child_location_sb(dev, &sb);
|
||||||
break;
|
sbuf_putc(&sb, '\0');
|
||||||
|
error = sbuf_finish(&sb);
|
||||||
do {
|
if (error == 0)
|
||||||
CP(dev->nameunit);
|
error = SYSCTL_OUT(req, udev, sizeof(*udev));
|
||||||
CP(dev->desc);
|
sbuf_delete(&sb);
|
||||||
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));
|
|
||||||
free(udev, M_BUS);
|
free(udev, M_BUS);
|
||||||
return (error);
|
return (error);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user