From nobody Wed Sep 29 09:42:38 2021 X-Original-To: scsi@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id E08121799884 for ; Wed, 29 Sep 2021 09:42:40 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4HKBHh61Fqz3CJt for ; Wed, 29 Sep 2021 09:42:40 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from [192.168.0.88] (unknown [195.64.148.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) (Authenticated sender: avg/mail) by smtp.freebsd.org (Postfix) with ESMTPSA id 740AA27EAF for ; Wed, 29 Sep 2021 09:42:40 +0000 (UTC) (envelope-from avg@FreeBSD.org) To: scsi@freebsd.org From: Andriy Gapon Subject: libcam: resolve symbolic links before processing device name Message-ID: <1d8d9137-bd03-f9c3-e320-f509bef9fcb2@FreeBSD.org> Date: Wed, 29 Sep 2021 12:42:38 +0300 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:78.0) Gecko/20100101 Firefox/78.0 Thunderbird/78.14.0 List-Id: SCSI subsystem List-Archive: https://lists.freebsd.org/archives/freebsd-scsi List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-scsi@freebsd.org X-BeenThere: freebsd-scsi@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-ThisMailContainsUnwantedMimeParts: N 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 unit (and handle some special device name prefixes) and then uses cam_lookup_pass() to find a matching pass device. I would like to propose to extend 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. The patch: 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); What it "unlocks" (the example works only if /dev/diskid/DISK-QM00015 is a symlink): root@freebsd:~ # smartctl -iA /dev/diskid/DISK-QM00015 smartctl 7.2 2020-12-30 r5155 [FreeBSD 13.0-STABLE amd64] (local build) Copyright (C) 2002-20, Bruce Allen, Christian Franke, www.smartmontools.org === START OF INFORMATION SECTION === Device Model: QEMU HARDDISK Serial Number: QM00015 Firmware Version: 2.5+ User Capacity: 1,073,741,824 bytes [1.07 GB] Sector Size: 512 bytes logical/physical TRIM Command: Available, deterministic Device is: Not in smartctl database [for details use: -P showall] ATA Version is: ATA/ATAPI-7, ATA/ATAPI-5 published, ANSI NCITS 340-2000 Local Time is: Wed Sep 29 09:39:30 2021 UTC SMART support is: Available - device has SMART capability. SMART support is: Enabled === START OF READ SMART DATA SECTION === SMART Attributes Data Structure revision number: 1 Vendor Specific SMART Attributes with Thresholds: ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE 1 Raw_Read_Error_Rate 0x0003 100 100 006 Pre-fail Always - 0 3 Spin_Up_Time 0x0003 100 100 000 Pre-fail Always - 16 4 Start_Stop_Count 0x0002 100 100 020 Old_age Always - 100 5 Reallocated_Sector_Ct 0x0003 100 100 036 Pre-fail Always - 0 9 Power_On_Hours 0x0003 100 100 000 Pre-fail Always - 1 12 Power_Cycle_Count 0x0003 100 100 000 Pre-fail Always - 0 190 Airflow_Temperature_Cel 0x0003 069 069 050 Pre-fail Always - 31 (Min/Max 31/31) -- Andriy Gapon