summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Boccassi <luca.boccassi@microsoft.com>2021-01-20 20:48:18 +0000
committerGitHub <noreply@github.com>2021-01-20 20:48:18 +0000
commit7504f599e1331c6e0ba1780a307178fd0a7f4d3a (patch)
tree9867d62d3f4a10c328ff319c7df7c31c8879dce7
parent4dbc0be2e5b2e4ad250b93f7f16194486a0a31db (diff)
parentcecaba2003dcf8185083de862d8d7f7249b062dc (diff)
downloadsystemd-7504f599e1331c6e0ba1780a307178fd0a7f4d3a.tar.gz
Merge pull request #18325 from ssahani/more-cleanup
Tree wide various cleanup
-rw-r--r--src/analyze/analyze.c6
-rw-r--r--src/basic/btrfs-util.c29
-rw-r--r--src/libsystemd/sd-device/device-monitor.c8
-rw-r--r--src/libsystemd/sd-event/sd-event.c12
-rw-r--r--src/libsystemd/sd-journal/sd-journal.c14
-rw-r--r--src/login/logind-brightness.c9
-rw-r--r--src/machine/image-dbus.c6
-rw-r--r--src/network/networkd-ndisc.c6
-rw-r--r--src/network/networkd-network.c8
-rw-r--r--src/udev/udev-rules.c10
10 files changed, 37 insertions, 71 deletions
diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c
index 9920f2a856..a45cc00853 100644
--- a/src/analyze/analyze.c
+++ b/src/analyze/analyze.c
@@ -1022,7 +1022,6 @@ static int list_dependencies(sd_bus *bus, const char *name) {
static int analyze_critical_chain(int argc, char *argv[], void *userdata) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(unit_times_freep) struct unit_times *times = NULL;
- struct unit_times *u;
Hashmap *h;
int n, r;
@@ -1038,7 +1037,7 @@ static int analyze_critical_chain(int argc, char *argv[], void *userdata) {
if (!h)
return log_oom();
- for (u = times; u->has_data; u++) {
+ for (struct unit_times *u = times; u->has_data; u++) {
r = hashmap_put(h, u->name, u);
if (r < 0)
return log_error_errno(r, "Failed to add entry to hashmap: %m");
@@ -1065,7 +1064,6 @@ static int analyze_blame(int argc, char *argv[], void *userdata) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(unit_times_freep) struct unit_times *times = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
- struct unit_times *u;
TableCell *cell;
int n, r;
@@ -1105,7 +1103,7 @@ static int analyze_blame(int argc, char *argv[], void *userdata) {
if (r < 0)
return r;
- for (u = times; u->has_data; u++) {
+ for (struct unit_times *u = times; u->has_data; u++) {
if (u->time <= 0)
continue;
diff --git a/src/basic/btrfs-util.c b/src/basic/btrfs-util.c
index 2634659aa0..fa435739ba 100644
--- a/src/basic/btrfs-util.c
+++ b/src/basic/btrfs-util.c
@@ -673,7 +673,7 @@ int btrfs_qgroup_get_quota(const char *path, uint64_t qgroupid, BtrfsQuotaInfo *
int btrfs_subvol_find_subtree_qgroup(int fd, uint64_t subvol_id, uint64_t *ret) {
uint64_t level, lowest = (uint64_t) -1, lowest_qgroupid = 0;
_cleanup_free_ uint64_t *qgroups = NULL;
- int r, n, i;
+ int r, n;
assert(fd >= 0);
assert(ret);
@@ -703,7 +703,7 @@ int btrfs_subvol_find_subtree_qgroup(int fd, uint64_t subvol_id, uint64_t *ret)
if (n < 0)
return n;
- for (i = 0; i < n; i++) {
+ for (int i = 0; i < n; i++) {
uint64_t id;
r = btrfs_qgroupid_split(qgroups[i], &level, &id);
@@ -824,7 +824,6 @@ int btrfs_qgroup_set_limit_fd(int fd, uint64_t qgroupid, uint64_t referenced_max
.lim.max_rfer = referenced_max,
.lim.flags = BTRFS_QGROUP_LIMIT_MAX_RFER,
};
- unsigned c;
int r;
assert(fd >= 0);
@@ -843,7 +842,7 @@ int btrfs_qgroup_set_limit_fd(int fd, uint64_t qgroupid, uint64_t referenced_max
args.qgroupid = qgroupid;
- for (c = 0;; c++) {
+ for (unsigned c = 0;; c++) {
if (ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args) < 0) {
if (errno == EBUSY && c < 10) {
@@ -924,7 +923,6 @@ static int qgroup_create_or_destroy(int fd, bool b, uint64_t qgroupid) {
.create = b,
.qgroupid = qgroupid,
};
- unsigned c;
int r;
r = btrfs_is_filesystem(fd);
@@ -933,7 +931,7 @@ static int qgroup_create_or_destroy(int fd, bool b, uint64_t qgroupid) {
if (r == 0)
return -ENOTTY;
- for (c = 0;; c++) {
+ for (unsigned c = 0;; c++) {
if (ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args) < 0) {
/* On old kernels if quota is not enabled, we get EINVAL. On newer kernels we get
@@ -968,7 +966,7 @@ int btrfs_qgroup_destroy(int fd, uint64_t qgroupid) {
int btrfs_qgroup_destroy_recursive(int fd, uint64_t qgroupid) {
_cleanup_free_ uint64_t *qgroups = NULL;
uint64_t subvol_id;
- int i, n, r;
+ int n, r;
/* Destroys the specified qgroup, but unassigns it from all
* its parents first. Also, it recursively destroys all
@@ -983,7 +981,7 @@ int btrfs_qgroup_destroy_recursive(int fd, uint64_t qgroupid) {
if (n < 0)
return n;
- for (i = 0; i < n; i++) {
+ for (int i = 0; i < n; i++) {
uint64_t id;
r = btrfs_qgroupid_split(qgroups[i], NULL, &id);
@@ -1043,7 +1041,6 @@ static int qgroup_assign_or_unassign(int fd, bool b, uint64_t child, uint64_t pa
.src = child,
.dst = parent,
};
- unsigned c;
int r;
r = btrfs_is_filesystem(fd);
@@ -1052,7 +1049,7 @@ static int qgroup_assign_or_unassign(int fd, bool b, uint64_t child, uint64_t pa
if (r == 0)
return -ENOTTY;
- for (c = 0;; c++) {
+ for (unsigned c = 0;; c++) {
r = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
if (r < 0) {
if (errno == EBUSY && c < 10) {
@@ -1351,7 +1348,7 @@ int btrfs_qgroup_copy_limits(int fd, uint64_t old_qgroupid, uint64_t new_qgroupi
static int copy_quota_hierarchy(int fd, uint64_t old_subvol_id, uint64_t new_subvol_id) {
_cleanup_free_ uint64_t *old_qgroups = NULL, *old_parent_qgroups = NULL;
bool copy_from_parent = false, insert_intermediary_qgroup = false;
- int n_old_qgroups, n_old_parent_qgroups, r, i;
+ int n_old_qgroups, n_old_parent_qgroups, r;
uint64_t old_parent_id;
assert(fd >= 0);
@@ -1375,9 +1372,8 @@ static int copy_quota_hierarchy(int fd, uint64_t old_subvol_id, uint64_t new_sub
return n_old_parent_qgroups;
}
- for (i = 0; i < n_old_qgroups; i++) {
+ for (int i = 0; i < n_old_qgroups; i++) {
uint64_t id;
- int j;
r = btrfs_qgroupid_split(old_qgroups[i], NULL, &id);
if (r < 0)
@@ -1392,7 +1388,7 @@ static int copy_quota_hierarchy(int fd, uint64_t old_subvol_id, uint64_t new_sub
break;
}
- for (j = 0; j < n_old_parent_qgroups; j++)
+ for (int j = 0; j < n_old_parent_qgroups; j++)
if (old_parent_qgroups[j] == old_qgroups[i])
/* The old subvolume shared a common
* parent qgroup with its parent
@@ -1880,12 +1876,11 @@ int btrfs_subvol_auto_qgroup_fd(int fd, uint64_t subvol_id, bool insert_intermed
if (insert_intermediary_qgroup) {
uint64_t lowest = 256, new_qgroupid;
bool created = false;
- int i;
/* Determine the lowest qgroup that the parent
* subvolume is assigned to. */
- for (i = 0; i < n; i++) {
+ for (int i = 0; i < n; i++) {
uint64_t level;
r = btrfs_qgroupid_split(qgroups[i], &level, NULL);
@@ -1910,7 +1905,7 @@ int btrfs_subvol_auto_qgroup_fd(int fd, uint64_t subvol_id, bool insert_intermed
if (r >= 0)
changed = created = true;
- for (i = 0; i < n; i++) {
+ for (int i = 0; i < n; i++) {
r = btrfs_qgroup_assign(fd, new_qgroupid, qgroups[i]);
if (r < 0 && r != -EEXIST) {
if (created)
diff --git a/src/libsystemd/sd-device/device-monitor.c b/src/libsystemd/sd-device/device-monitor.c
index fd5900704d..48f3c707e3 100644
--- a/src/libsystemd/sd-device/device-monitor.c
+++ b/src/libsystemd/sd-device/device-monitor.c
@@ -732,15 +732,13 @@ _public_ int sd_device_monitor_filter_add_match_subsystem_devtype(sd_device_moni
return -ENOMEM;
}
- r = hashmap_ensure_allocated(&m->subsystem_filter, NULL);
+ r = hashmap_ensure_put(&m->subsystem_filter, NULL, s, d);
if (r < 0)
return r;
- r = hashmap_put(m->subsystem_filter, s, d);
- if (r < 0)
- return r;
+ TAKE_PTR(s);
+ TAKE_PTR(d);
- s = d = NULL;
m->filter_uptodate = false;
return 0;
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
index 3f1a6776fe..010e628242 100644
--- a/src/libsystemd/sd-event/sd-event.c
+++ b/src/libsystemd/sd-event/sd-event.c
@@ -629,10 +629,6 @@ static int event_make_signal_data(
return 0;
}
} else {
- r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
- if (r < 0)
- return r;
-
d = new(struct signal_data, 1);
if (!d)
return -ENOMEM;
@@ -643,7 +639,7 @@ static int event_make_signal_data(
.priority = priority,
};
- r = hashmap_put(e->signal_data, &d->priority, d);
+ r = hashmap_ensure_put(&e->signal_data, &uint64_hash_ops, &d->priority, d);
if (r < 0) {
free(d);
return r;
@@ -1727,10 +1723,6 @@ static int event_make_inotify_data(
fd = fd_move_above_stdio(fd);
- r = hashmap_ensure_allocated(&e->inotify_data, &uint64_hash_ops);
- if (r < 0)
- return r;
-
d = new(struct inotify_data, 1);
if (!d)
return -ENOMEM;
@@ -1741,7 +1733,7 @@ static int event_make_inotify_data(
.priority = priority,
};
- r = hashmap_put(e->inotify_data, &d->priority, d);
+ r = hashmap_ensure_put(&e->inotify_data, &uint64_hash_ops, &d->priority, d);
if (r < 0) {
d->fd = safe_close(d->fd);
free(d);
diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c
index 27251a2597..ff26f5d7e7 100644
--- a/src/libsystemd/sd-journal/sd-journal.c
+++ b/src/libsystemd/sd-journal/sd-journal.c
@@ -63,7 +63,7 @@ static bool journal_pid_changed(sd_journal *j) {
}
static int journal_put_error(sd_journal *j, int r, const char *path) {
- char *copy;
+ _cleanup_free_ char *copy = NULL;
int k;
/* Memorize an error we encountered, and store which
@@ -80,27 +80,21 @@ static int journal_put_error(sd_journal *j, int r, const char *path) {
if (r >= 0)
return r;
- k = hashmap_ensure_allocated(&j->errors, NULL);
- if (k < 0)
- return k;
-
if (path) {
copy = strdup(path);
if (!copy)
return -ENOMEM;
- } else
- copy = NULL;
+ }
- k = hashmap_put(j->errors, INT_TO_PTR(r), copy);
+ k = hashmap_ensure_put(&j->errors, NULL, INT_TO_PTR(r), copy);
if (k < 0) {
- free(copy);
-
if (k == -EEXIST)
return 0;
return k;
}
+ TAKE_PTR(copy);
return 0;
}
diff --git a/src/login/logind-brightness.c b/src/login/logind-brightness.c
index a6a1603396..f4306749ec 100644
--- a/src/login/logind-brightness.c
+++ b/src/login/logind-brightness.c
@@ -217,10 +217,6 @@ int manager_write_brightness(
return 0;
}
- r = hashmap_ensure_allocated(&m->brightness_writers, &brightness_writer_hash_ops);
- if (r < 0)
- return log_oom();
-
w = new(BrightnessWriter, 1);
if (!w)
return log_oom();
@@ -234,9 +230,12 @@ int manager_write_brightness(
if (!w->path)
return log_oom();
- r = hashmap_put(m->brightness_writers, w->path, w);
+ r = hashmap_ensure_put(&m->brightness_writers, &brightness_writer_hash_ops, w->path, w);
+ if (r == -ENOMEM)
+ return log_oom();
if (r < 0)
return log_error_errno(r, "Failed to add brightness writer to hashmap: %m");
+
w->manager = m;
r = set_add_message(&w->current_messages, message);
diff --git a/src/machine/image-dbus.c b/src/machine/image-dbus.c
index 4c4f900527..f74cabd7fb 100644
--- a/src/machine/image-dbus.c
+++ b/src/machine/image-dbus.c
@@ -390,10 +390,6 @@ static int image_object_find(sd_bus *bus, const char *path, const char *interfac
return 1;
}
- r = hashmap_ensure_allocated(&m->image_cache, &image_hash_ops);
- if (r < 0)
- return r;
-
if (!m->image_cache_defer_event) {
r = sd_event_add_defer(m->event, &m->image_cache_defer_event, image_flush_cache, m);
if (r < 0)
@@ -416,7 +412,7 @@ static int image_object_find(sd_bus *bus, const char *path, const char *interfac
image->userdata = m;
- r = hashmap_put(m->image_cache, image->name, image);
+ r = hashmap_ensure_put(&m->image_cache, &image_hash_ops, image->name, image);
if (r < 0) {
image_unref(image);
return r;
diff --git a/src/network/networkd-ndisc.c b/src/network/networkd-ndisc.c
index 3f1837f591..82de316bc3 100644
--- a/src/network/networkd-ndisc.c
+++ b/src/network/networkd-ndisc.c
@@ -1544,11 +1544,9 @@ int config_parse_address_generation_type(
token->prefix = buffer.in6;
}
- r = ordered_set_ensure_allocated(&network->ipv6_tokens, &ipv6_token_hash_ops);
- if (r < 0)
+ r = ordered_set_ensure_put(&network->ipv6_tokens, &ipv6_token_hash_ops, token);
+ if (r == -ENOMEM)
return log_oom();
-
- r = ordered_set_put(network->ipv6_tokens, token);
if (r == -EEXIST)
log_syntax(unit, LOG_DEBUG, filename, line, r,
"IPv6 token '%s' is duplicated, ignoring: %m", rvalue);
diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c
index 97a5f1b0d1..32f9140237 100644
--- a/src/network/networkd-network.c
+++ b/src/network/networkd-network.c
@@ -805,11 +805,9 @@ int config_parse_stacked_netdev(const char *unit,
if (!name)
return log_oom();
- r = hashmap_ensure_allocated(h, &string_hash_ops);
- if (r < 0)
+ r = hashmap_ensure_put(h, &string_hash_ops, name, INT_TO_PTR(kind));
+ if (r == -ENOMEM)
return log_oom();
-
- r = hashmap_put(*h, name, INT_TO_PTR(kind));
if (r < 0)
log_syntax(unit, LOG_WARNING, filename, line, r,
"Cannot add NetDev '%s' to network, ignoring assignment: %m", name);
@@ -817,7 +815,7 @@ int config_parse_stacked_netdev(const char *unit,
log_syntax(unit, LOG_DEBUG, filename, line, r,
"NetDev '%s' specified twice, ignoring.", name);
else
- name = NULL;
+ TAKE_PTR(name);
return 0;
}
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
index db4f79fc9a..66eef11d05 100644
--- a/src/udev/udev-rules.c
+++ b/src/udev/udev-rules.c
@@ -2147,19 +2147,17 @@ static int udev_rule_apply_token_to_event(
if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL))
ordered_hashmap_clear_free_key(event->run_list);
- r = ordered_hashmap_ensure_allocated(&event->run_list, NULL);
- if (r < 0)
- return log_oom();
-
(void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
cmd = strdup(buf);
if (!cmd)
return log_oom();
- r = ordered_hashmap_put(event->run_list, cmd, token->data);
- if (r < 0)
+ r = ordered_hashmap_ensure_put(&event->run_list, NULL, cmd, token->data);
+ if (r == -ENOMEM)
return log_oom();
+ if (r < 0)
+ return log_rule_error_errno(dev, rules, r, "Failed to store command '%s': %m", cmd);
TAKE_PTR(cmd);