Skip to content

Commit f7ae5d1

Browse files
committed
udev-builtin-net_id: drop redundant copy of USB identifier in names_usb()
This makes the names based on the USB identifier (and possibly with PCI specifier) built in names_usb() No functional change, just refactoring.
1 parent 045fb96 commit f7ae5d1

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

src/udev/udev-builtin-net_id.c

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
typedef enum NetNameType {
4747
NET_UNDEF,
4848
NET_PCI,
49-
NET_USB,
5049
NET_BCMA,
5150
} NetNameType;
5251

@@ -59,7 +58,6 @@ typedef struct NetNames {
5958
char pci_onboard[ALTIFNAMSIZ];
6059
const char *pci_onboard_label;
6160

62-
char usb_ports[ALTIFNAMSIZ];
6361
char bcma_core[ALTIFNAMSIZ];
6462
} NetNames;
6563

@@ -958,14 +956,17 @@ static int get_usb_specifier(sd_device *dev, char **ret) {
958956
return 0;
959957
}
960958

961-
static int names_usb(sd_device *dev, NetNames *names) {
959+
static int names_usb(sd_device *dev, const char *prefix, NetNames *names, bool test) {
962960
_cleanup_free_ char *suffix = NULL;
963961
sd_device *usbdev;
964962
int r;
965963

966964
assert(dev);
965+
assert(prefix);
967966
assert(names);
968967

968+
/* USB device */
969+
969970
r = sd_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface", &usbdev);
970971
if (r < 0)
971972
return log_device_debug_errno(dev, r, "Could not find usb parent device: %m");
@@ -974,11 +975,26 @@ static int names_usb(sd_device *dev, NetNames *names) {
974975
if (r < 0)
975976
return r;
976977

977-
size_t l = strscpy(names->usb_ports, sizeof(names->usb_ports), suffix);
978-
if (l == 0)
979-
names->usb_ports[0] = '\0';
980-
else
981-
names->type = NET_USB;
978+
/* If the USB bus is on PCI bus, then suffix the USB specifier to the name based on the PCI bus. */
979+
if (names) {
980+
char buf[ALTIFNAMSIZ];
981+
if (names->pci_path[0] &&
982+
snprintf_ok(buf, sizeof buf, "%s%s%s", prefix, names->pci_path, suffix))
983+
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", buf);
984+
985+
if (names->pci_slot[0] &&
986+
snprintf_ok(buf, sizeof buf, "%s%s%s", prefix, names->pci_slot, suffix))
987+
udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", buf);
988+
}
989+
990+
if (!naming_scheme_has(NAMING_USB_HOST))
991+
return 0;
992+
993+
/* Otherwise, e.g. on-chip asics that have USB ports, use the USB specifier as is. */
994+
char str[ALTIFNAMSIZ];
995+
if (snprintf_ok(str, sizeof str, "%s%s", prefix, suffix))
996+
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
997+
982998
return 0;
983999
}
9841000

@@ -1346,22 +1362,10 @@ static int builtin_net_id(UdevEvent *event, int argc, char *argv[], bool test) {
13461362

13471363
/* get PCI based path names */
13481364
r = names_pci(dev, &names);
1349-
if (r < 0) {
1350-
/*
1351-
* check for usb devices that are not off pci interfaces to
1352-
* support various on-chip asics that have usb ports
1353-
*/
1354-
if (r == -ENOENT &&
1355-
naming_scheme_has(NAMING_USB_HOST) &&
1356-
names_usb(dev, &names) >= 0 && names.type == NET_USB) {
1357-
char str[ALTIFNAMSIZ];
1358-
1359-
if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.usb_ports))
1360-
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
1361-
}
1362-
1365+
if (r == -ENOENT)
1366+
(void) names_usb(dev, prefix, NULL, test);
1367+
if (r < 0)
13631368
return 0;
1364-
}
13651369

13661370
/* plain PCI device */
13671371
if (names.type == NET_PCI) {
@@ -1387,19 +1391,7 @@ static int builtin_net_id(UdevEvent *event, int argc, char *argv[], bool test) {
13871391
return 0;
13881392
}
13891393

1390-
/* USB device */
1391-
if (names_usb(dev, &names) >= 0 && names.type == NET_USB) {
1392-
char str[ALTIFNAMSIZ];
1393-
1394-
if (names.pci_path[0] &&
1395-
snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_path, names.usb_ports))
1396-
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
1397-
1398-
if (names.pci_slot[0] &&
1399-
snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_slot, names.usb_ports))
1400-
udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
1401-
return 0;
1402-
}
1394+
(void) names_usb(dev, prefix, &names, test);
14031395

14041396
/* Broadcom bus */
14051397
if (names_bcma(dev, &names) >= 0 && names.type == NET_BCMA) {

0 commit comments

Comments
 (0)