Skip to content

Commit 84f7bd7

Browse files
committed
sd-netlink: make sd_genl_message_new() or friends return -EOPNOTSUPP if a module is not supported by the kernel
1 parent 4d4d898 commit 84f7bd7

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/libsystemd/sd-netlink/netlink-genl.c

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,42 @@ void genl_clear_family(sd_netlink *nl) {
5353
nl->genl_family_by_id = hashmap_free_with_destructor(nl->genl_family_by_id, genl_family_free);
5454
}
5555

56+
static int genl_family_new_unsupported(
57+
sd_netlink *nl,
58+
const char *family_name,
59+
const NLTypeSystem *type_system) {
60+
61+
_cleanup_(genl_family_freep) GenericNetlinkFamily *f = NULL;
62+
int r;
63+
64+
assert(nl);
65+
assert(family_name);
66+
assert(type_system);
67+
68+
/* Kernel does not support the genl family? To prevent from resolving the family name again,
69+
* let's store the family with zero id to indicate that. */
70+
71+
f = new(GenericNetlinkFamily, 1);
72+
if (!f)
73+
return -ENOMEM;
74+
75+
*f = (GenericNetlinkFamily) {
76+
.type_system = type_system,
77+
};
78+
79+
f->name = strdup(family_name);
80+
if (!f->name)
81+
return -ENOMEM;
82+
83+
r = hashmap_ensure_put(&nl->genl_family_by_name, &string_hash_ops, f->name, f);
84+
if (r < 0)
85+
return r;
86+
87+
f->genl = nl;
88+
TAKE_PTR(f);
89+
return 0;
90+
}
91+
5692
static int genl_family_new(
5793
sd_netlink *nl,
5894
const char *expected_family_name,
@@ -79,28 +115,6 @@ static int genl_family_new(
79115
.type_system = type_system,
80116
};
81117

82-
if (sd_netlink_message_is_error(message)) {
83-
int e;
84-
85-
/* Kernel does not support the genl family? To prevent from resolving the family name
86-
* again, let's store the family with zero id to indicate that. */
87-
88-
e = sd_netlink_message_get_errno(message);
89-
if (e >= 0) /* Huh? */
90-
e = -EOPNOTSUPP;
91-
92-
f->name = strdup(expected_family_name);
93-
if (!f->name)
94-
return e;
95-
96-
if (hashmap_ensure_put(&nl->genl_family_by_name, &string_hash_ops, f->name, f) < 0)
97-
return e;
98-
99-
f->genl = nl;
100-
TAKE_PTR(f);
101-
return e;
102-
}
103-
104118
r = sd_genl_message_get_family_name(nl, message, &family_name);
105119
if (r < 0)
106120
return r;
@@ -261,9 +275,10 @@ static int genl_family_get_by_name_internal(
261275
if (r < 0)
262276
return r;
263277

264-
r = sd_netlink_call(nl, req, 0, &reply);
265-
if (r < 0)
266-
return r;
278+
if (sd_netlink_call(nl, req, 0, &reply) < 0) {
279+
(void) genl_family_new_unsupported(nl, name, type_system);
280+
return -EOPNOTSUPP;
281+
}
267282

268283
return genl_family_new(nl, name, type_system, reply, ret);
269284
}

0 commit comments

Comments
 (0)