Skip to content

Commit 87f9300

Browse files
authored
Merge pull request systemd#18850 from yuwata/sd-device-monitor-cleanups
sd-device-monitor: trivial cleanups
2 parents cb0e818 + 30e2c8c commit 87f9300

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

src/libsystemd/sd-device/device-enumerator.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ _public_ int sd_device_enumerator_add_match_sysattr(sd_device_enumerator *enumer
116116
else
117117
hashmap = &enumerator->nomatch_sysattr;
118118

119+
/* Do not use string_has_ops_free_free or hashmap_put_strdup() here, as this may be called
120+
* multiple times with the same sysattr but different value. */
119121
r = hashmap_put_strdup_full(hashmap, &trivial_hash_ops_free_free, sysattr, value);
120122
if (r <= 0)
121123
return r;
@@ -131,6 +133,8 @@ _public_ int sd_device_enumerator_add_match_property(sd_device_enumerator *enume
131133
assert_return(enumerator, -EINVAL);
132134
assert_return(property, -EINVAL);
133135

136+
/* Do not use string_has_ops_free_free or hashmap_put_strdup() here, as this may be called
137+
* multiple times with the same property but different value. */
134138
r = hashmap_put_strdup_full(&enumerator->match_property, &trivial_hash_ops_free_free, property, value);
135139
if (r <= 0)
136140
return r;

src/libsystemd/sd-device/device-monitor.c

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ static int monitor_set_nl_address(sd_device_monitor *m) {
8383
}
8484

8585
int device_monitor_allow_unicast_sender(sd_device_monitor *m, sd_device_monitor *sender) {
86-
assert_return(m, -EINVAL);
87-
assert_return(sender, -EINVAL);
86+
assert(m);
87+
assert(sender);
8888

8989
m->snl_trusted_sender.nl.nl_pid = sender->snl.nl.nl_pid;
9090
return 0;
@@ -104,7 +104,7 @@ int device_monitor_disconnect(sd_device_monitor *m) {
104104
}
105105

106106
int device_monitor_get_fd(sd_device_monitor *m) {
107-
assert_return(m, -EINVAL);
107+
assert(m);
108108

109109
return m->sock;
110110
}
@@ -114,8 +114,8 @@ int device_monitor_new_full(sd_device_monitor **ret, MonitorNetlinkGroup group,
114114
_cleanup_close_ int sock = -1;
115115
int r;
116116

117+
assert(group >= 0 && group < _MONITOR_NETLINK_GROUP_MAX);
117118
assert_return(ret, -EINVAL);
118-
assert_return(group >= 0 && group < _MONITOR_NETLINK_GROUP_MAX, -EINVAL);
119119

120120
if (group == MONITOR_GROUP_UDEV &&
121121
access("/run/udev/control", F_OK) < 0 &&
@@ -304,7 +304,7 @@ _public_ sd_event_source *sd_device_monitor_get_event_source(sd_device_monitor *
304304
int device_monitor_enable_receiving(sd_device_monitor *m) {
305305
int r;
306306

307-
assert_return(m, -EINVAL);
307+
assert(m);
308308

309309
r = sd_device_monitor_filter_update(m);
310310
if (r < 0)
@@ -334,8 +334,8 @@ static sd_device_monitor *device_monitor_free(sd_device_monitor *m) {
334334

335335
(void) sd_device_monitor_detach_event(m);
336336

337-
hashmap_free_free_free(m->subsystem_filter);
338-
set_free_free(m->tag_filter);
337+
hashmap_free(m->subsystem_filter);
338+
set_free(m->tag_filter);
339339

340340
return mfree(m);
341341
}
@@ -346,8 +346,8 @@ static int passes_filter(sd_device_monitor *m, sd_device *device) {
346346
const char *tag, *subsystem, *devtype, *s, *d = NULL;
347347
int r;
348348

349-
assert_return(m, -EINVAL);
350-
assert_return(device, -EINVAL);
349+
assert(m);
350+
assert(device);
351351

352352
if (hashmap_isempty(m->subsystem_filter))
353353
goto tag;
@@ -413,6 +413,7 @@ int device_monitor_receive_device(sd_device_monitor *m, sd_device **ret) {
413413
bool is_initialized = false;
414414
int r;
415415

416+
assert(m);
416417
assert(ret);
417418

418419
buflen = recvmsg(m->sock, &smsg, 0);
@@ -507,10 +508,10 @@ static uint64_t string_bloom64(const char *str) {
507508
uint64_t bits = 0;
508509
uint32_t hash = string_hash32(str);
509510

510-
bits |= 1LLU << (hash & 63);
511-
bits |= 1LLU << ((hash >> 6) & 63);
512-
bits |= 1LLU << ((hash >> 12) & 63);
513-
bits |= 1LLU << ((hash >> 18) & 63);
511+
bits |= UINT64_C(1) << (hash & 63);
512+
bits |= UINT64_C(1) << ((hash >> 6) & 63);
513+
bits |= UINT64_C(1) << ((hash >> 12) & 63);
514+
bits |= UINT64_C(1) << ((hash >> 18) & 63);
514515
return bits;
515516
}
516517

@@ -717,41 +718,32 @@ _public_ int sd_device_monitor_filter_update(sd_device_monitor *m) {
717718
}
718719

719720
_public_ int sd_device_monitor_filter_add_match_subsystem_devtype(sd_device_monitor *m, const char *subsystem, const char *devtype) {
720-
_cleanup_free_ char *s = NULL, *d = NULL;
721721
int r;
722722

723723
assert_return(m, -EINVAL);
724724
assert_return(subsystem, -EINVAL);
725725

726-
s = strdup(subsystem);
727-
if (!s)
728-
return -ENOMEM;
729-
730-
if (devtype) {
731-
d = strdup(devtype);
732-
if (!d)
733-
return -ENOMEM;
734-
}
735-
736-
r = hashmap_ensure_put(&m->subsystem_filter, NULL, s, d);
737-
if (r < 0)
726+
/* Do not use string_has_ops_free_free or hashmap_put_strdup() here, as this may be called
727+
* multiple times with the same subsystem but different devtypes. */
728+
r = hashmap_put_strdup_full(&m->subsystem_filter, &trivial_hash_ops_free_free, subsystem, devtype);
729+
if (r <= 0)
738730
return r;
739731

740-
TAKE_PTR(s);
741-
TAKE_PTR(d);
742-
743732
m->filter_uptodate = false;
744-
745-
return 0;
733+
return r;
746734
}
747735

748736
_public_ int sd_device_monitor_filter_add_match_tag(sd_device_monitor *m, const char *tag) {
737+
int r;
738+
749739
assert_return(m, -EINVAL);
750740
assert_return(tag, -EINVAL);
751741

752-
int r = set_put_strdup(&m->tag_filter, tag);
753-
if (r > 0)
754-
m->filter_uptodate = false;
742+
r = set_put_strdup(&m->tag_filter, tag);
743+
if (r <= 0)
744+
return r;
745+
746+
m->filter_uptodate = false;
755747
return r;
756748
}
757749

@@ -760,8 +752,8 @@ _public_ int sd_device_monitor_filter_remove(sd_device_monitor *m) {
760752

761753
assert_return(m, -EINVAL);
762754

763-
m->subsystem_filter = hashmap_free_free_free(m->subsystem_filter);
764-
m->tag_filter = set_free_free(m->tag_filter);
755+
m->subsystem_filter = hashmap_free(m->subsystem_filter);
756+
m->tag_filter = set_free(m->tag_filter);
765757

766758
if (setsockopt(m->sock, SOL_SOCKET, SO_DETACH_FILTER, &filter, sizeof(filter)) < 0)
767759
return -errno;

0 commit comments

Comments
 (0)