summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2022-02-23 06:51:06 +0900
committerGitHub <noreply@github.com>2022-02-23 06:51:06 +0900
commit05d8ad0f855a9d32e3509cb68cd54c6bf0003073 (patch)
tree4c61fa99e57b83fa6ae760e190e7a5cb5ee431e7 /src
parentcab4ed0f4a5cb1f2e2d236082370e0a7982b9fbf (diff)
parentbe99883e131ef422f8278ec1d099520996a78bb0 (diff)
downloadsystemd-05d8ad0f855a9d32e3509cb68cd54c6bf0003073.tar.gz
Merge pull request #22596 from yuwata/test-fix-fd-leaks
test: fix fd leaks
Diffstat (limited to 'src')
-rw-r--r--src/libsystemd-network/test-dhcp-client.c17
-rw-r--r--src/libsystemd-network/test-dhcp6-client.c5
-rw-r--r--src/libsystemd-network/test-ndisc-ra.c19
-rw-r--r--src/libsystemd-network/test-ndisc-rs.c30
-rw-r--r--src/libsystemd/sd-journal/test-catalog.c3
-rw-r--r--src/oom/test-oomd-util.c5
-rw-r--r--src/test/test-fs-util.c5
-rw-r--r--src/test/test-psi-util.c5
-rw-r--r--src/test/test-tmpfiles.c3
9 files changed, 33 insertions, 59 deletions
diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c
index 476dcce85e..d1d104196a 100644
--- a/src/libsystemd-network/test-dhcp-client.c
+++ b/src/libsystemd-network/test-dhcp-client.c
@@ -31,13 +31,6 @@ static bool verbose = true;
static int test_fd[2];
static test_callback_recv_t callback_recv;
static be32_t xid;
-static sd_event_source *test_hangcheck;
-
-static int test_dhcp_hangcheck(sd_event_source *s, uint64_t usec, void *userdata) {
- assert_not_reached();
-
- return 0;
-}
static void test_request_basic(sd_event *e) {
int r;
@@ -514,19 +507,15 @@ static void test_addr_acq(sd_event *e) {
callback_recv = test_addr_acq_recv_discover;
- assert_se(sd_event_add_time_relative(
- e, &test_hangcheck,
- clock_boottime_or_monotonic(),
- 2 * USEC_PER_SEC, 0,
- test_dhcp_hangcheck, NULL) >= 0);
+ assert_se(sd_event_add_time_relative(e, NULL, clock_boottime_or_monotonic(),
+ 2 * USEC_PER_SEC, 0,
+ NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
res = sd_dhcp_client_start(client);
assert_se(IN_SET(res, 0, -EINPROGRESS));
assert_se(sd_event_loop(e) >= 0);
- test_hangcheck = sd_event_source_unref(test_hangcheck);
-
assert_se(sd_dhcp_client_set_callback(client, NULL, NULL) >= 0);
assert_se(sd_dhcp_client_stop(client) >= 0);
sd_dhcp_client_unref(client);
diff --git a/src/libsystemd-network/test-dhcp6-client.c b/src/libsystemd-network/test-dhcp6-client.c
index 3b25df7625..f53e093d77 100644
--- a/src/libsystemd-network/test-dhcp6-client.c
+++ b/src/libsystemd-network/test-dhcp6-client.c
@@ -78,8 +78,6 @@ static int test_ifindex = 42;
static unsigned test_client_sent_message_count = 0;
static sd_dhcp6_client *client_ref = NULL;
-STATIC_DESTRUCTOR_REGISTER(client_ref, sd_dhcp6_client_unrefp);
-
static void test_client_basic(void) {
_cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL;
int v;
@@ -981,7 +979,7 @@ static void test_dhcp6_client(void) {
assert_se(sd_event_new(&e) >= 0);
assert_se(sd_event_add_time_relative(e, NULL, clock_boottime_or_monotonic(),
2 * USEC_PER_SEC, 0,
- NULL, INT_TO_PTR(ETIMEDOUT)) >= 0);
+ NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
assert_se(sd_dhcp6_client_new(&client) >= 0);
assert_se(sd_dhcp6_client_attach_event(client, e, 0) >= 0);
@@ -1004,6 +1002,7 @@ static void test_dhcp6_client(void) {
assert_se(test_client_sent_message_count == 4);
+ assert_se(!sd_dhcp6_client_unref(client_ref));
test_fd[1] = safe_close(test_fd[1]);
}
diff --git a/src/libsystemd-network/test-ndisc-ra.c b/src/libsystemd-network/test-ndisc-ra.c
index 7b961f4761..83186a97bb 100644
--- a/src/libsystemd-network/test-ndisc-ra.c
+++ b/src/libsystemd-network/test-ndisc-ra.c
@@ -51,7 +51,6 @@ static uint8_t advertisement[] = {
0x72, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
-static sd_event_source *test_hangcheck;
static bool test_stopped;
static int test_fd[2];
static sd_event_source *recv_router_advertisement;
@@ -101,13 +100,6 @@ static const struct in6_addr test_rdnss = { { { 0x20, 0x01, 0x0d, 0xb8,
static const char *test_dnssl[] = { "lab.intra",
NULL };
-static int test_rs_hangcheck(sd_event_source *s, uint64_t usec,
- void *userdata) {
- assert_se(false);
-
- return 0;
-}
-
static void test_radv_prefix(void) {
sd_radv_prefix *p;
@@ -341,16 +333,13 @@ static void test_ra(void) {
assert_se(sd_event_add_io(e, &recv_router_advertisement, test_fd[0],
EPOLLIN, radv_recv, ra) >= 0);
- assert_se(sd_event_add_time_relative(
- e, &test_hangcheck, clock_boottime_or_monotonic(),
- 2 *USEC_PER_SEC, 0,
- test_rs_hangcheck, NULL) >= 0);
+ assert_se(sd_event_add_time_relative(e, NULL, clock_boottime_or_monotonic(),
+ 2 * USEC_PER_SEC, 0,
+ NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
assert_se(sd_radv_start(ra) >= 0);
- sd_event_loop(e);
-
- test_hangcheck = sd_event_source_unref(test_hangcheck);
+ assert_se(sd_event_loop(e) >= 0);
ra = sd_radv_unref(ra);
assert_se(!ra);
diff --git a/src/libsystemd-network/test-ndisc-rs.c b/src/libsystemd-network/test-ndisc-rs.c
index 1366b44115..10915e1d4c 100644
--- a/src/libsystemd-network/test-ndisc-rs.c
+++ b/src/libsystemd-network/test-ndisc-rs.c
@@ -22,7 +22,6 @@ static struct ether_addr mac_addr = {
};
static bool verbose = false;
-static sd_event_source *test_hangcheck;
static int test_fd[2];
static sd_ndisc *test_timeout_nd;
@@ -166,13 +165,6 @@ static void router_dump(sd_ndisc_router *rt) {
}
}
-static int test_rs_hangcheck(sd_event_source *s, uint64_t usec,
- void *userdata) {
- assert_se(false);
-
- return 0;
-}
-
int icmp6_bind_router_solicitation(int ifindex) {
assert_se(ifindex == 42);
@@ -285,10 +277,9 @@ static void test_rs(void) {
assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
assert_se(sd_ndisc_set_callback(nd, test_callback, e) >= 0);
- assert_se(sd_event_add_time_relative(
- e, &test_hangcheck, clock_boottime_or_monotonic(),
- 30 * USEC_PER_SEC, 0,
- test_rs_hangcheck, NULL) >= 0);
+ assert_se(sd_event_add_time_relative(e, NULL, clock_boottime_or_monotonic(),
+ 30 * USEC_PER_SEC, 0,
+ NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
assert_se(sd_ndisc_stop(nd) >= 0);
assert_se(sd_ndisc_start(nd) >= 0);
@@ -297,9 +288,7 @@ static void test_rs(void) {
assert_se(sd_ndisc_start(nd) >= 0);
- sd_event_loop(e);
-
- test_hangcheck = sd_event_source_unref(test_hangcheck);
+ assert_se(sd_event_loop(e) >= 0);
nd = sd_ndisc_unref(nd);
assert_se(!nd);
@@ -379,16 +368,13 @@ static void test_timeout(void) {
assert_se(sd_ndisc_set_ifindex(nd, 42) >= 0);
assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
- assert_se(sd_event_add_time_relative(
- e, &test_hangcheck, clock_boottime_or_monotonic(),
- 30 * USEC_PER_SEC, 0,
- test_rs_hangcheck, NULL) >= 0);
+ assert_se(sd_event_add_time_relative(e, NULL, clock_boottime_or_monotonic(),
+ 30 * USEC_PER_SEC, 0,
+ NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
assert_se(sd_ndisc_start(nd) >= 0);
- sd_event_loop(e);
-
- test_hangcheck = sd_event_source_unref(test_hangcheck);
+ assert_se(sd_event_loop(e) >= 0);
nd = sd_ndisc_unref(nd);
diff --git a/src/libsystemd/sd-journal/test-catalog.c b/src/libsystemd/sd-journal/test-catalog.c
index 316c3b1634..ad06221175 100644
--- a/src/libsystemd/sd-journal/test-catalog.c
+++ b/src/libsystemd/sd-journal/test-catalog.c
@@ -196,6 +196,7 @@ static void test_catalog_file_lang(void) {
int main(int argc, char *argv[]) {
_cleanup_(unlink_tempfilep) char database[] = "/tmp/test-catalog.XXXXXX";
+ _cleanup_close_ int fd = -1;
_cleanup_free_ char *text = NULL;
int r;
@@ -218,7 +219,7 @@ int main(int argc, char *argv[]) {
test_catalog_import_merge();
test_catalog_import_merge_no_body();
- assert_se(mkostemp_safe(database) >= 0);
+ assert_se((fd = mkostemp_safe(database)) >= 0);
test_catalog_update(database);
diff --git a/src/oom/test-oomd-util.c b/src/oom/test-oomd-util.c
index f0283677f7..13d9e60f16 100644
--- a/src/oom/test-oomd-util.c
+++ b/src/oom/test-oomd-util.c
@@ -5,6 +5,7 @@
#include "alloc-util.h"
#include "cgroup-setup.h"
#include "cgroup-util.h"
+#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "oomd-util.h"
@@ -13,6 +14,7 @@
#include "string-util.h"
#include "strv.h"
#include "tests.h"
+#include "tmpfile-util.h"
static int fork_and_sleep(unsigned sleep_min) {
usec_t n, timeout, ts;
@@ -244,12 +246,13 @@ static void test_oomd_update_cgroup_contexts_between_hashmaps(void) {
static void test_oomd_system_context_acquire(void) {
_cleanup_(unlink_tempfilep) char path[] = "/oomdgetsysctxtestXXXXXX";
+ _cleanup_close_ int fd = -1;
OomdSystemContext ctx;
if (geteuid() != 0)
return (void) log_tests_skipped("not root");
- assert_se(mkstemp(path));
+ assert_se((fd = mkostemp_safe(path)) >= 0);
assert_se(oomd_system_context_acquire("/verylikelynonexistentpath", &ctx) == -ENOENT);
diff --git a/src/test/test-fs-util.c b/src/test/test-fs-util.c
index 602ce75f98..f53a3ebf59 100644
--- a/src/test/test-fs-util.c
+++ b/src/test/test-fs-util.c
@@ -29,10 +29,11 @@ static const char *arg_test_dir = NULL;
TEST(chase_symlinks) {
_cleanup_free_ char *result = NULL;
+ _cleanup_close_ int pfd = -1;
char *temp;
const char *top, *p, *pslash, *q, *qslash;
struct stat st;
- int r, pfd;
+ int r;
temp = strjoina(arg_test_dir ?: "/tmp", "/test-chase.XXXXXX");
assert_se(mkdtemp(temp));
@@ -318,6 +319,7 @@ TEST(chase_symlinks) {
assert_se(fstat(pfd, &st) >= 0);
assert_se(S_ISLNK(st.st_mode));
result = mfree(result);
+ pfd = safe_close(pfd);
/* s1 -> s2 -> nonexistent */
q = strjoina(temp, "/s1");
@@ -331,6 +333,7 @@ TEST(chase_symlinks) {
assert_se(fstat(pfd, &st) >= 0);
assert_se(S_ISLNK(st.st_mode));
result = mfree(result);
+ pfd = safe_close(pfd);
/* Test CHASE_STEP */
diff --git a/src/test/test-psi-util.c b/src/test/test-psi-util.c
index ed465b807e..111671c5a9 100644
--- a/src/test/test-psi-util.c
+++ b/src/test/test-psi-util.c
@@ -1,20 +1,23 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "alloc-util.h"
+#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "parse-util.h"
#include "psi-util.h"
#include "tests.h"
+#include "tmpfile-util.h"
TEST(read_mem_pressure) {
_cleanup_(unlink_tempfilep) char path[] = "/tmp/pressurereadtestXXXXXX";
+ _cleanup_close_ int fd = -1;
ResourcePressure rp;
if (geteuid() != 0)
return (void) log_tests_skipped("not root");
- assert_se(mkstemp(path));
+ assert_se((fd = mkostemp_safe(path)) >= 0);
assert_se(read_resource_pressure("/verylikelynonexistentpath", PRESSURE_TYPE_SOME, &rp) < 0);
assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);
diff --git a/src/test/test-tmpfiles.c b/src/test/test-tmpfiles.c
index 99243eb77a..f26701767f 100644
--- a/src/test/test-tmpfiles.c
+++ b/src/test/test-tmpfiles.c
@@ -35,7 +35,7 @@ TEST(tmpfiles) {
assert_se(endswith(ans, " (deleted)"));
fd2 = mkostemp_safe(pattern);
- assert_se(fd >= 0);
+ assert_se(fd2 >= 0);
assert_se(unlink(pattern) == 0);
assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd2) > 0);
@@ -47,6 +47,7 @@ TEST(tmpfiles) {
pattern = strjoina(p, "/tmpfiles-test");
assert_se(tempfn_random(pattern, NULL, &d) >= 0);
+ fd = safe_close(fd);
fd = open_tmpfile_linkable(d, O_RDWR|O_CLOEXEC, &tmp);
assert_se(fd >= 0);
assert_se(write(fd, "foobar\n", 7) == 7);