Skip to content

Commit baab0a6

Browse files
authored
Merge pull request systemd#28619 from yuwata/udev-builtin-net_id-cleanups-part3
udev: several cleanups and fixlets for net_id builtin (part 3)
2 parents 241dbc6 + 5660e68 commit baab0a6

File tree

1 file changed

+24
-41
lines changed

1 file changed

+24
-41
lines changed

src/udev/udev-builtin-net_id.c

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ typedef struct NetNames {
6363
char bcma_core[ALTIFNAMSIZ];
6464
} NetNames;
6565

66-
typedef struct LinkInfo {
67-
int ifindex;
68-
int iflink;
69-
int iftype;
70-
} LinkInfo;
71-
7266
/* skip intermediate virtio devices */
7367
static sd_device *skip_virtio(sd_device *dev) {
7468
/* there can only ever be one virtio bus per parent device, so we can
@@ -266,13 +260,12 @@ static int pci_get_onboard_index(sd_device *dev, unsigned *ret) {
266260
return 0;
267261
}
268262

269-
static int dev_pci_onboard(sd_device *dev, const LinkInfo *info, NetNames *names) {
263+
static int dev_pci_onboard(sd_device *dev, NetNames *names) {
270264
_cleanup_free_ char *port = NULL;
271265
unsigned idx = 0; /* avoid false maybe-uninitialized warning */
272266
int r;
273267

274268
assert(dev);
275-
assert(info);
276269
assert(names);
277270

278271
/* retrieve on-board index number and label from firmware */
@@ -306,6 +299,8 @@ static int is_pci_multifunction(sd_device *dev) {
306299
size_t len;
307300
int r;
308301

302+
assert(dev);
303+
309304
r = sd_device_get_syspath(dev, &syspath);
310305
if (r < 0)
311306
return r;
@@ -326,17 +321,16 @@ static int is_pci_multifunction(sd_device *dev) {
326321
}
327322

328323
static bool is_pci_ari_enabled(sd_device *dev) {
329-
const char *a;
330-
331-
if (sd_device_get_sysattr_value(dev, "ari_enabled", &a) < 0)
332-
return false;
324+
assert(dev);
333325

334-
return streq(a, "1");
326+
return device_get_sysattr_bool(dev, "ari_enabled") > 0;
335327
}
336328

337329
static bool is_pci_bridge(sd_device *dev) {
338330
const char *v, *p;
339331

332+
assert(dev);
333+
340334
if (sd_device_get_sysattr_value(dev, "modalias", &v) < 0)
341335
return false;
342336

@@ -563,13 +557,12 @@ static int get_pci_slot_specifiers(
563557
return 0;
564558
}
565559

566-
static int dev_pci_slot(sd_device *dev, const LinkInfo *info, NetNames *names) {
560+
static int dev_pci_slot(sd_device *dev, NetNames *names) {
567561
_cleanup_free_ char *domain = NULL, *bus_and_slot = NULL, *func = NULL, *port = NULL;
568562
uint32_t hotplug_slot = 0; /* avoid false maybe-uninitialized warning */
569563
int r;
570564

571565
assert(dev);
572-
assert(info);
573566
assert(names);
574567

575568
r = get_pci_slot_specifiers(names->pcidev, &domain, &bus_and_slot, &func);
@@ -847,15 +840,14 @@ static int names_devicetree(sd_device *dev, const char *prefix, bool test) {
847840
return -ENOENT;
848841
}
849842

850-
static int names_pci(sd_device *dev, const LinkInfo *info, NetNames *names) {
843+
static int names_pci(sd_device *dev, NetNames *names) {
851844
_cleanup_(sd_device_unrefp) sd_device *physfn_pcidev = NULL;
852845
_cleanup_free_ char *virtfn_suffix = NULL;
853846
sd_device *parent;
854847
const char *subsystem;
855848
int r;
856849

857850
assert(dev);
858-
assert(info);
859851
assert(names);
860852

861853
r = sd_device_get_parent(dev, &parent);
@@ -884,8 +876,8 @@ static int names_pci(sd_device *dev, const LinkInfo *info, NetNames *names) {
884876

885877
/* If this is an SR-IOV virtual device, get base name using physical device and add virtfn suffix. */
886878
vf_names.pcidev = physfn_pcidev;
887-
dev_pci_onboard(dev, info, &vf_names);
888-
dev_pci_slot(dev, info, &vf_names);
879+
dev_pci_onboard(dev, &vf_names);
880+
dev_pci_slot(dev, &vf_names);
889881

890882
if (vf_names.pci_onboard[0])
891883
if (strlen(vf_names.pci_onboard) + strlen(virtfn_suffix) < sizeof(names->pci_onboard))
@@ -900,8 +892,8 @@ static int names_pci(sd_device *dev, const LinkInfo *info, NetNames *names) {
900892
strscpyl(names->pci_path, sizeof(names->pci_path),
901893
vf_names.pci_path, virtfn_suffix, NULL);
902894
} else {
903-
dev_pci_onboard(dev, info, names);
904-
dev_pci_slot(dev, info, names);
895+
dev_pci_onboard(dev, names);
896+
dev_pci_slot(dev, names);
905897
}
906898

907899
return 0;
@@ -956,11 +948,11 @@ static int names_usb(sd_device *dev, NetNames *names) {
956948

957949
/* append USB config number, suppress the common config == 1 */
958950
if (!streq(config, "1"))
959-
l = strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL);
951+
l = strpcpyl(&s, l, "c", config, NULL);
960952

961953
/* append USB interface number, suppress the interface == 0 */
962954
if (!streq(interf, "0"))
963-
l = strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL);
955+
l = strpcpyl(&s, l, "i", interf, NULL);
964956
if (l == 0)
965957
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ENAMETOOLONG),
966958
"Generated USB name would be too long.");
@@ -1290,41 +1282,32 @@ static int get_ifname_prefix(sd_device *dev, const char **ret) {
12901282
}
12911283
}
12921284

1293-
static int get_link_info(sd_device *dev, LinkInfo *info) {
1294-
int r;
1285+
static int device_is_stacked(sd_device *dev) {
1286+
int ifindex, iflink, r;
12951287

12961288
assert(dev);
1297-
assert(info);
12981289

1299-
r = sd_device_get_ifindex(dev, &info->ifindex);
1290+
r = sd_device_get_ifindex(dev, &ifindex);
13001291
if (r < 0)
13011292
return r;
13021293

1303-
r = device_get_sysattr_int(dev, "iflink", &info->iflink);
1294+
r = device_get_sysattr_int(dev, "iflink", &iflink);
13041295
if (r < 0)
13051296
return r;
13061297

1307-
r = device_get_sysattr_int(dev, "type", &info->iftype);
1308-
if (r < 0)
1309-
return r;
1310-
1311-
return 0;
1298+
return ifindex != iflink;
13121299
}
13131300

13141301
static int builtin_net_id(UdevEvent *event, int argc, char *argv[], bool test) {
13151302
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
13161303
const char *prefix;
13171304
NetNames names = {};
1318-
LinkInfo info = {};
13191305
int r;
13201306

1321-
r = get_link_info(dev, &info);
1322-
if (r < 0)
1323-
return r;
1324-
13251307
/* skip stacked devices, like VLANs, ... */
1326-
if (info.ifindex != info.iflink)
1327-
return 0;
1308+
r = device_is_stacked(dev);
1309+
if (r != 0)
1310+
return r;
13281311

13291312
r = get_ifname_prefix(dev, &prefix);
13301313
if (r < 0) {
@@ -1343,7 +1326,7 @@ static int builtin_net_id(UdevEvent *event, int argc, char *argv[], bool test) {
13431326
(void) names_xen(dev, prefix, test);
13441327

13451328
/* get PCI based path names */
1346-
r = names_pci(dev, &info, &names);
1329+
r = names_pci(dev, &names);
13471330
if (r < 0) {
13481331
/*
13491332
* check for usb devices that are not off pci interfaces to

0 commit comments

Comments
 (0)