diff options
-rw-r--r-- | share/man/man9/Makefile | 1 | ||||
-rw-r--r-- | share/man/man9/device_set_desc.9 | 8 | ||||
-rw-r--r-- | sys/kern/subr_bus.c | 43 | ||||
-rw-r--r-- | 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 863963d24474..3f9572796fa1 100644 --- a/share/man/man9/Makefile +++ b/share/man/man9/Makefile @@ -1012,6 +1012,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 4f7053cc2613..21c2835f2ae6 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); |