git: 86a8ea7444c8 - stable/14 - nvmecontrol: Allow optional /dev/ for device names

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Tue, 27 Aug 2024 18:07:17 UTC
The branch stable/14 has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=86a8ea7444c850c287200c47f95ddded6826e943

commit 86a8ea7444c850c287200c47f95ddded6826e943
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2024-04-28 17:01:24 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2024-08-27 16:35:34 +0000

    nvmecontrol: Allow optional /dev/ for device names
    
    nvmecontrol operates on devices. Allow a user to specify the /dev/ if
    they want. Any device that starts with / will be treated as if it was a
    full path for maximum flexbility.
    
    Sponsored by:           Netflix
    
    (cherry picked from commit b12cae88cfb6286bc85a47b36ddd84f52b5c38ca)
---
 sbin/nvmecontrol/nvmecontrol.8 | 4 ++--
 sbin/nvmecontrol/nvmecontrol.c | 7 +++++--
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/sbin/nvmecontrol/nvmecontrol.8 b/sbin/nvmecontrol/nvmecontrol.8
index b5a85b1ab9f5..1310184ac309 100644
--- a/sbin/nvmecontrol/nvmecontrol.8
+++ b/sbin/nvmecontrol/nvmecontrol.8
@@ -33,7 +33,7 @@
 .\"
 .\" Author: Jim Harris <jimharris@FreeBSD.org>
 .\"
-.Dd April 17, 2024
+.Dd May 3, 2024
 .Dt NVMECONTROL 8
 .Os
 .Sh NAME
@@ -687,7 +687,7 @@ or
 .Pa nvdZ .
 The leading
 .Pa /dev/
-is omitted.
+may be omitted.
 Where
 .Aq Ar device-id
 is required, you can use either the
diff --git a/sbin/nvmecontrol/nvmecontrol.c b/sbin/nvmecontrol/nvmecontrol.c
index 92fbf9e25378..3876093451e1 100644
--- a/sbin/nvmecontrol/nvmecontrol.c
+++ b/sbin/nvmecontrol/nvmecontrol.c
@@ -146,9 +146,12 @@ read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata)
 int
 open_dev(const char *str, int *fd, int write, int exit_on_error)
 {
-	char		full_path[64];
+	char		full_path[MAXPATHLEN];
 
-	snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
+	if (str[0] == '/')	/* Full path */
+		strlcpy(full_path, str, sizeof(full_path));
+	else			/* Add /dev/ */
+		snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
 	*fd = open(full_path, write ? O_RDWR : O_RDONLY);
 	if (*fd < 0) {
 		if (exit_on_error) {