From 3a48b5f21db022d745fd91736eb166ac70075b8e Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Tue, 1 Aug 2023 04:18:52 +0900
Subject: [PATCH 7017/7019] udev-builtin-net_id: split-out
 pci_get_onboard_index() from dev_pci_onboard()

This also changes the onboard index type to unsigned, as it is at most
2^16 - 1. Hence, unsigned is enough.

Then, we can use device_get_sysattr_unsigned() helper function.

This also drops mostly redundant debugging logs. The index will be
logged later anyway.

(cherry picked from commit d6ec0fe22938aa6067b34d58739637405910c011)
---
 src/udev/udev-builtin-net_id.c | 57 +++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 25 deletions(-)

diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c
index 17fc2e2073..7eaaf9530e 100644
--- a/src/udev/udev-builtin-net_id.c
+++ b/src/udev/udev-builtin-net_id.c
@@ -230,7 +230,7 @@ static int get_port_specifier(sd_device *dev, bool fallback_to_dev_id, char **re
         return 0;
 }
 
-static bool is_valid_onboard_index(unsigned long idx) {
+static bool is_valid_onboard_index(unsigned idx) {
         /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to
          * report for example). Let's define a cut-off where we don't consider the index reliable anymore. We
          * pick some arbitrary cut-off, which is somewhere beyond the realistic number of physical network
@@ -240,47 +240,54 @@ static bool is_valid_onboard_index(unsigned long idx) {
         return idx <= (naming_scheme_has(NAMING_16BIT_INDEX) ? ONBOARD_16BIT_INDEX_MAX : ONBOARD_14BIT_INDEX_MAX);
 }
 
-/* retrieve on-board index number and label from firmware */
-static int dev_pci_onboard(sd_device *dev, const LinkInfo *info, NetNames *names) {
-        _cleanup_free_ char *port = NULL;
-        unsigned long idx;
-        const char *attr;
+static int pci_get_onboard_index(sd_device *dev, unsigned *ret) {
+        unsigned idx;
         int r;
 
         assert(dev);
-        assert(info);
-        assert(names);
+        assert(ret);
 
         /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
-        if (sd_device_get_sysattr_value(names->pcidev, "acpi_index", &attr) >= 0)
-                log_device_debug(names->pcidev, "acpi_index=%s", attr);
-        else {
+        r = device_get_sysattr_unsigned(dev, "acpi_index", &idx);
+        if (r < 0)
                 /* SMBIOS type 41 — Onboard Devices Extended Information */
-                r = sd_device_get_sysattr_value(names->pcidev, "index", &attr);
-                if (r < 0)
-                        return r;
-                log_device_debug(names->pcidev, "index=%s", attr);
-        }
-
-        r = safe_atolu(attr, &idx);
+                r = device_get_sysattr_unsigned(dev, "index", &idx);
         if (r < 0)
-                return log_device_debug_errno(names->pcidev, r,
-                                              "Failed to parse onboard index \"%s\": %m", attr);
+                return r;
+
         if (idx == 0 && !naming_scheme_has(NAMING_ZERO_ACPI_INDEX))
-                return log_device_debug_errno(names->pcidev, SYNTHETIC_ERRNO(EINVAL),
+                return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
                                               "Naming scheme does not allow onboard index==0.");
         if (!is_valid_onboard_index(idx))
-                return log_device_debug_errno(names->pcidev, SYNTHETIC_ERRNO(ENOENT),
-                                              "Not a valid onboard index: %lu", idx);
+                return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ENOENT),
+                                              "Not a valid onboard index: %u", idx);
+
+        *ret = idx;
+        return 0;
+}
+
+static int dev_pci_onboard(sd_device *dev, const LinkInfo *info, NetNames *names) {
+        _cleanup_free_ char *port = NULL;
+        unsigned idx = 0;  /* avoid false maybe-uninitialized warning */
+        int r;
+
+        assert(dev);
+        assert(info);
+        assert(names);
+
+        /* retrieve on-board index number and label from firmware */
+        r = pci_get_onboard_index(names->pcidev, &idx);
+        if (r < 0)
+                return r;
 
         r = get_port_specifier(dev, /* fallback_to_dev_id = */ false, &port);
         if (r < 0)
                 return r;
 
-        if (!snprintf_ok(names->pci_onboard, sizeof(names->pci_onboard), "o%lu%s", idx, strempty(port)))
+        if (!snprintf_ok(names->pci_onboard, sizeof(names->pci_onboard), "o%u%s", idx, strempty(port)))
                 names->pci_onboard[0] = '\0';
 
-        log_device_debug(dev, "Onboard index identifier: index=%lu port=%s %s %s",
+        log_device_debug(dev, "Onboard index identifier: index=%u port=%s %s %s",
                          idx, strna(port),
                          special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), empty_to_na(names->pci_onboard));
 
-- 
2.43.0

