]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev-builtin-net_id: split-out pci_get_onboard_index() from dev_pci_onboard() 28609/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 31 Jul 2023 19:18:52 +0000 (04:18 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 1 Aug 2023 02:20:48 +0000 (11:20 +0900)
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.

src/udev/udev-builtin-net_id.c

index f99d4479f0aef4e213e4387829f875087107b6cf..e538fc7d9415c487fa11af0e0c692bfc350915f6 100644 (file)
@@ -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));