git: 1abf1e8c6b13 - main - cam_get_device: resolve path links before parsing device name

From: Andriy Gapon <avg_at_FreeBSD.org>
Date: Wed, 26 Jan 2022 09:27:10 UTC
The branch main has been updated by avg:

URL: https://cgit.FreeBSD.org/src/commit/?id=1abf1e8c6b13f50eed08fef6fb1ade280894f1e4

commit 1abf1e8c6b13f50eed08fef6fb1ade280894f1e4
Author:     Andriy Gapon <avg@FreeBSD.org>
AuthorDate: 2021-11-27 18:49:08 +0000
Commit:     Andriy Gapon <avg@FreeBSD.org>
CommitDate: 2022-01-26 09:25:31 +0000

    cam_get_device: resolve path links before parsing device name
    
    The CAM subsystem uses bus:taget:lun tuple to address peripherals.  But
    for convenience many userland programs such as camcontrol accept devices
    names such as da0.  There is a libcam function, cam_open_device, to
    support that.  It first calls cam_get_device() to parse the device name
    as a driver name and a unit (and handle some special device name
    prefixes) and then uses cam_lookup_pass() to find a matching pass
    device.
    
    This change extends cam_get_device() to apply realpath(3) to the device
    name before parsing it.  This will allow to use tools such as camcontrol
    and smartctl with symbolic links that could be friendlier (more
    distinguished) names for devices.
    
    MFC after:      3 weeks
    Relnotes:       maybe
---
 lib/libcam/camlib.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/lib/libcam/camlib.c b/lib/libcam/camlib.c
index 438b0e502fe0..6ebdb30ab82b 100644
--- a/lib/libcam/camlib.c
+++ b/lib/libcam/camlib.c
@@ -128,10 +128,13 @@ cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit)
 	}
 
 	/*
-	 * We can be rather destructive to the path string.  Make a copy of
-	 * it so we don't hose the user's string.
+	 * Resolve the given path to a real device path in case we are given
+	 * an alias or other symbolic link.  If the path cannot be resolved
+	 * then try to parse it as is.
 	 */
-	newpath = (char *)strdup(path);
+	newpath = realpath(path, NULL);
+	if (newpath == NULL)
+		newpath = strdup(path);
 	if (newpath == NULL)
 		return (-1);