diff --git a/src/basic/meson.build b/src/basic/meson.build index e5852f32f9..c62e4a3b59 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -148,7 +148,6 @@ basic_sources = files(''' ratelimit.h raw-clone.h raw-reboot.h - refcnt.h replace-var.c replace-var.h rlimit-util.c diff --git a/src/basic/refcnt.h b/src/basic/refcnt.h deleted file mode 100644 index 40f9a84a22..0000000000 --- a/src/basic/refcnt.h +++ /dev/null @@ -1,54 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1+ */ -#pragma once - -/* A type-safe atomic refcounter. - * - * DO NOT USE THIS UNLESS YOU ACTUALLY CARE ABOUT THREAD SAFETY! */ - -typedef struct { - volatile unsigned _value; -} RefCount; - -#define REFCNT_GET(r) ((r)._value) -#define REFCNT_INC(r) (__sync_add_and_fetch(&(r)._value, 1)) -#define REFCNT_DEC(r) (__sync_sub_and_fetch(&(r)._value, 1)) - -#define REFCNT_INIT ((RefCount) { ._value = 1 }) - -#define _DEFINE_ATOMIC_REF_FUNC(type, name, scope) \ - scope type *name##_ref(type *p) { \ - if (!p) \ - return NULL; \ - \ - assert_se(REFCNT_INC(p->n_ref) >= 2); \ - return p; \ - } - -#define _DEFINE_ATOMIC_UNREF_FUNC(type, name, free_func, scope) \ - scope type *name##_unref(type *p) { \ - if (!p) \ - return NULL; \ - \ - if (REFCNT_DEC(p->n_ref) > 0) \ - return NULL; \ - \ - return free_func(p); \ - } - -#define DEFINE_ATOMIC_REF_FUNC(type, name) \ - _DEFINE_ATOMIC_REF_FUNC(type, name,) -#define DEFINE_PUBLIC_ATOMIC_REF_FUNC(type, name) \ - _DEFINE_ATOMIC_REF_FUNC(type, name, _public_) - -#define DEFINE_ATOMIC_UNREF_FUNC(type, name, free_func) \ - _DEFINE_ATOMIC_UNREF_FUNC(type, name, free_func,) -#define DEFINE_PUBLIC_ATOMIC_UNREF_FUNC(type, name, free_func) \ - _DEFINE_ATOMIC_UNREF_FUNC(type, name, free_func, _public_) - -#define DEFINE_ATOMIC_REF_UNREF_FUNC(type, name, free_func) \ - DEFINE_ATOMIC_REF_FUNC(type, name); \ - DEFINE_ATOMIC_UNREF_FUNC(type, name, free_func); - -#define DEFINE_PUBLIC_ATOMIC_REF_UNREF_FUNC(type, name, free_func) \ - DEFINE_PUBLIC_ATOMIC_REF_FUNC(type, name); \ - DEFINE_PUBLIC_ATOMIC_UNREF_FUNC(type, name, free_func); diff --git a/src/libsystemd-network/test-lldp.c b/src/libsystemd-network/test-lldp.c index b6c896f0a1..7406f94ce0 100644 --- a/src/libsystemd-network/test-lldp.c +++ b/src/libsystemd-network/test-lldp.c @@ -15,6 +15,7 @@ #include "lldp-network.h" #include "macro.h" #include "string-util.h" +#include "tests.h" #define TEST_LLDP_PORT "em1" #define TEST_LLDP_TYPE_SYSTEM_NAME "systemd-lldp" @@ -365,7 +366,7 @@ static void test_multiple_neighbors_sorted(sd_event *e) { int main(int argc, char *argv[]) { _cleanup_(sd_event_unrefp) sd_event *e = NULL; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); /* LLDP reception tests */ assert_se(sd_event_new(&e) == 0); diff --git a/src/libsystemd/sd-bus/bus-internal.h b/src/libsystemd/sd-bus/bus-internal.h index 04864d4df0..63f1404758 100644 --- a/src/libsystemd/sd-bus/bus-internal.h +++ b/src/libsystemd/sd-bus/bus-internal.h @@ -13,7 +13,6 @@ #include "hashmap.h" #include "list.h" #include "prioq.h" -#include "refcnt.h" #include "socket-util.h" #include "util.h" @@ -171,15 +170,7 @@ enum bus_auth { }; struct sd_bus { - /* We use atomic ref counting here since sd_bus_message - objects retain references to their originating sd_bus but - we want to allow them to be processed in a different - thread. We won't provide full thread safety, but only the - bare minimum that makes it possible to use sd_bus and - sd_bus_message objects independently and on different - threads as long as each object is used only once at the - same time. */ - RefCount n_ref; + unsigned n_ref; enum bus_state state; int input_fd, output_fd; diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 69f1519976..ba791db4f2 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -236,7 +236,7 @@ _public_ int sd_bus_new(sd_bus **ret) { return -ENOMEM; *b = (sd_bus) { - .n_ref = REFCNT_INIT, + .n_ref = 1, .input_fd = -1, .output_fd = -1, .inotify_fd = -1, @@ -1585,7 +1585,7 @@ void bus_enter_closing(sd_bus *bus) { bus_set_state(bus, BUS_CLOSING); } -DEFINE_PUBLIC_ATOMIC_REF_UNREF_FUNC(sd_bus, sd_bus, bus_free); +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus, sd_bus, bus_free); _public_ int sd_bus_is_open(sd_bus *bus) { assert_return(bus, -EINVAL); diff --git a/src/libsystemd/sd-bus/test-bus-address.c b/src/libsystemd/sd-bus/test-bus-address.c index c58c52a778..70a6df10f9 100644 --- a/src/libsystemd/sd-bus/test-bus-address.c +++ b/src/libsystemd/sd-bus/test-bus-address.c @@ -4,6 +4,7 @@ #include "log.h" #include "string-util.h" #include "strv.h" +#include "tests.h" static void test_one_address(sd_bus *b, const char *host, @@ -59,9 +60,7 @@ static void test_bus_set_address_system_remote(char **args) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_INFO); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_INFO); test_bus_set_address_system_remote(argv + 1); diff --git a/src/libsystemd/sd-bus/test-bus-cleanup.c b/src/libsystemd/sd-bus/test-bus-cleanup.c index bea722ba06..99d335e3fc 100644 --- a/src/libsystemd/sd-bus/test-bus-cleanup.c +++ b/src/libsystemd/sd-bus/test-bus-cleanup.c @@ -7,7 +7,6 @@ #include "bus-internal.h" #include "bus-message.h" #include "bus-util.h" -#include "refcnt.h" #include "tests.h" static bool use_system_bus = false; @@ -16,7 +15,7 @@ static void test_bus_new(void) { _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; assert_se(sd_bus_new(&bus) == 0); - printf("after new: refcount %u\n", REFCNT_GET(bus->n_ref)); + assert_se(bus->n_ref == 1); } static int test_bus_open(void) { @@ -32,7 +31,7 @@ static int test_bus_open(void) { } assert_se(r >= 0); - printf("after open: refcount %u\n", REFCNT_GET(bus->n_ref)); + assert_se(bus->n_ref >= 1); /* we send a hello message when opening, so the count is above 1 */ return 0; } @@ -45,10 +44,10 @@ static void test_bus_new_method_call(void) { assert_se(sd_bus_message_new_method_call(bus, &m, "a.service.name", "/an/object/path", "an.interface.name", "AMethodName") >= 0); - printf("after message_new_method_call: refcount %u\n", REFCNT_GET(bus->n_ref)); - + assert_se(m->n_ref == 1); /* We hold the only reference to the message */ + assert_se(bus->n_ref >= 2); sd_bus_flush_close_unref(bus); - printf("after bus_flush_close_unref: refcount %u\n", m->n_ref); + assert_se(m->n_ref == 1); } static void test_bus_new_signal(void) { @@ -59,10 +58,10 @@ static void test_bus_new_signal(void) { assert_se(sd_bus_message_new_signal(bus, &m, "/an/object/path", "an.interface.name", "Name") >= 0); - printf("after message_new_signal: refcount %u\n", REFCNT_GET(bus->n_ref)); - + assert_se(m->n_ref == 1); /* We hold the only reference to the message */ + assert_se(bus->n_ref >= 2); sd_bus_flush_close_unref(bus); - printf("after bus_flush_close_unref: refcount %u\n", m->n_ref); + assert_se(m->n_ref == 1); } int main(int argc, char **argv) { diff --git a/src/libsystemd/sd-bus/test-bus-introspect.c b/src/libsystemd/sd-bus/test-bus-introspect.c index 940dbfe0e7..9c8e93e897 100644 --- a/src/libsystemd/sd-bus/test-bus-introspect.c +++ b/src/libsystemd/sd-bus/test-bus-introspect.c @@ -2,6 +2,7 @@ #include "bus-introspect.h" #include "log.h" +#include "tests.h" static int prop_get(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error) { return -EINVAL; @@ -29,7 +30,7 @@ static const sd_bus_vtable vtable[] = { int main(int argc, char *argv[]) { struct introspect intro; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); assert_se(introspect_begin(&intro, false) >= 0); diff --git a/src/libsystemd/sd-bus/test-bus-queue-ref-cycle.c b/src/libsystemd/sd-bus/test-bus-queue-ref-cycle.c index 70901c30b0..f6506fb93f 100644 --- a/src/libsystemd/sd-bus/test-bus-queue-ref-cycle.c +++ b/src/libsystemd/sd-bus/test-bus-queue-ref-cycle.c @@ -1,10 +1,10 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include "sd-bus.h" #include "main-func.h" -#include "sd-bus.h" #include "tests.h" -static int run(int argc, char *argv[]) { +static int test_ref_unref(void) { sd_bus_message *m = NULL; sd_bus *bus = NULL; int r; @@ -41,4 +41,16 @@ static int run(int argc, char *argv[]) { return 0; } +static int run(int argc, char *argv[]) { + int r; + + test_setup_logging(LOG_INFO); + + r = test_ref_unref(); + if (r < 0) + return r; + + return 0; +} + DEFINE_MAIN_FUNCTION(run); diff --git a/src/libsystemd/sd-bus/test-bus-watch-bind.c b/src/libsystemd/sd-bus/test-bus-watch-bind.c index 4b3da30079..2723866e08 100644 --- a/src/libsystemd/sd-bus/test-bus-watch-bind.c +++ b/src/libsystemd/sd-bus/test-bus-watch-bind.c @@ -16,6 +16,7 @@ #include "socket-util.h" #include "string-util.h" #include "tmpfile-util.h" +#include "tests.h" static int method_foobar(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { log_info("Got Foobar() call."); @@ -198,7 +199,7 @@ int main(int argc, char *argv[]) { pthread_t server, client1, client2; char *path; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); /* We use /dev/shm here rather than /tmp, since some weird distros might set up /tmp as some weird fs that * doesn't support inotify properly. */ diff --git a/src/libsystemd/sd-device/device-private.c b/src/libsystemd/sd-device/device-private.c index 76267a1e74..0d0bdca800 100644 --- a/src/libsystemd/sd-device/device-private.c +++ b/src/libsystemd/sd-device/device-private.c @@ -18,7 +18,6 @@ #include "mkdir.h" #include "parse-util.h" #include "path-util.h" -#include "refcnt.h" #include "set.h" #include "string-table.h" #include "string-util.h" diff --git a/src/libsystemd/sd-hwdb/sd-hwdb.c b/src/libsystemd/sd-hwdb/sd-hwdb.c index 233944c078..d8c37c3fd4 100644 --- a/src/libsystemd/sd-hwdb/sd-hwdb.c +++ b/src/libsystemd/sd-hwdb/sd-hwdb.c @@ -18,12 +18,11 @@ #include "hashmap.h" #include "hwdb-internal.h" #include "hwdb-util.h" -#include "refcnt.h" #include "string-util.h" #include "util.h" struct sd_hwdb { - RefCount n_ref; + unsigned n_ref; FILE *f; struct stat st; @@ -316,27 +315,27 @@ _public_ int sd_hwdb_new(sd_hwdb **ret) { if (!hwdb) return -ENOMEM; - hwdb->n_ref = REFCNT_INIT; + hwdb->n_ref = 1; /* find hwdb.bin in hwdb_bin_paths */ NULSTR_FOREACH(hwdb_bin_path, hwdb_bin_paths) { + log_debug("Trying to open \"%s\"...", hwdb_bin_path); hwdb->f = fopen(hwdb_bin_path, "re"); if (hwdb->f) break; - else if (errno == ENOENT) - continue; - else + if (errno != ENOENT) return log_debug_errno(errno, "Failed to open %s: %m", hwdb_bin_path); } - if (!hwdb->f) { - log_debug("hwdb.bin does not exist, please run 'systemd-hwdb update'"); - return -ENOENT; - } + if (!hwdb->f) + return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), + "hwdb.bin does not exist, please run 'systemd-hwdb update'"); - if (fstat(fileno(hwdb->f), &hwdb->st) < 0 || - (size_t) hwdb->st.st_size < offsetof(struct trie_header_f, strings_len) + 8) - return log_debug_errno(errno, "Failed to read %s: %m", hwdb_bin_path); + if (fstat(fileno(hwdb->f), &hwdb->st) < 0) + return log_debug_errno(errno, "Failed to stat %s: %m", hwdb_bin_path); + if (hwdb->st.st_size < (off_t) offsetof(struct trie_header_f, strings_len) + 8) + return log_debug_errno(SYNTHETIC_ERRNO(EIO), + "File %s is too short: %m", hwdb_bin_path); hwdb->map = mmap(0, hwdb->st.st_size, PROT_READ, MAP_SHARED, fileno(hwdb->f), 0); if (hwdb->map == MAP_FAILED) @@ -370,7 +369,7 @@ static sd_hwdb *hwdb_free(sd_hwdb *hwdb) { return mfree(hwdb); } -DEFINE_PUBLIC_ATOMIC_REF_UNREF_FUNC(sd_hwdb, sd_hwdb, hwdb_free) +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_hwdb, sd_hwdb, hwdb_free) bool hwdb_validate(sd_hwdb *hwdb) { bool found = false; @@ -383,12 +382,11 @@ bool hwdb_validate(sd_hwdb *hwdb) { return false; /* if hwdb.bin doesn't exist anywhere, we need to update */ - NULSTR_FOREACH(p, hwdb_bin_paths) { + NULSTR_FOREACH(p, hwdb_bin_paths) if (stat(p, &st) >= 0) { found = true; break; } - } if (!found) return true; diff --git a/src/libsystemd/sd-netlink/netlink-internal.h b/src/libsystemd/sd-netlink/netlink-internal.h index 98de4f0a83..a8af3fc63b 100644 --- a/src/libsystemd/sd-netlink/netlink-internal.h +++ b/src/libsystemd/sd-netlink/netlink-internal.h @@ -8,7 +8,6 @@ #include "list.h" #include "netlink-types.h" #include "prioq.h" -#include "refcnt.h" #define RTNL_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC)) @@ -56,7 +55,7 @@ struct sd_netlink_slot { }; struct sd_netlink { - RefCount n_ref; + unsigned n_ref; int fd; @@ -114,7 +113,7 @@ struct netlink_container { }; struct sd_netlink_message { - RefCount n_ref; + unsigned n_ref; sd_netlink *rtnl; diff --git a/src/libsystemd/sd-netlink/netlink-message.c b/src/libsystemd/sd-netlink/netlink-message.c index f878fc9471..bf3ac95400 100644 --- a/src/libsystemd/sd-netlink/netlink-message.c +++ b/src/libsystemd/sd-netlink/netlink-message.c @@ -12,7 +12,6 @@ #include "netlink-internal.h" #include "netlink-types.h" #include "netlink-util.h" -#include "refcnt.h" #include "socket-util.h" #include "util.h" @@ -36,7 +35,7 @@ int message_new_empty(sd_netlink *rtnl, sd_netlink_message **ret) { if (!m) return -ENOMEM; - m->n_ref = REFCNT_INIT; + m->n_ref = 1; m->protocol = rtnl->protocol; m->sealed = false; @@ -96,12 +95,10 @@ int sd_netlink_message_request_dump(sd_netlink_message *m, int dump) { return 0; } -DEFINE_ATOMIC_REF_FUNC(sd_netlink_message, sd_netlink_message); +DEFINE_TRIVIAL_REF_FUNC(sd_netlink_message, sd_netlink_message); sd_netlink_message *sd_netlink_message_unref(sd_netlink_message *m) { - sd_netlink_message *t; - - while (m && REFCNT_DEC(m->n_ref) == 0) { + while (m && --m->n_ref == 0) { unsigned i; free(m->hdr); @@ -109,7 +106,7 @@ sd_netlink_message *sd_netlink_message_unref(sd_netlink_message *m) { for (i = 0; i <= m->n_containers; i++) free(m->containers[i].attributes); - t = m; + sd_netlink_message *t = m; m = m->next; free(t); } diff --git a/src/libsystemd/sd-netlink/netlink-socket.c b/src/libsystemd/sd-netlink/netlink-socket.c index 432e8e8c06..d1c95b260f 100644 --- a/src/libsystemd/sd-netlink/netlink-socket.c +++ b/src/libsystemd/sd-netlink/netlink-socket.c @@ -14,7 +14,6 @@ #include "netlink-internal.h" #include "netlink-types.h" #include "netlink-util.h" -#include "refcnt.h" #include "socket-util.h" #include "util.h" diff --git a/src/libsystemd/sd-netlink/rtnl-message.c b/src/libsystemd/sd-netlink/rtnl-message.c index 2d4d00e0eb..ccc497de09 100644 --- a/src/libsystemd/sd-netlink/rtnl-message.c +++ b/src/libsystemd/sd-netlink/rtnl-message.c @@ -12,7 +12,6 @@ #include "netlink-internal.h" #include "netlink-types.h" #include "netlink-util.h" -#include "refcnt.h" #include "socket-util.h" #include "util.h" diff --git a/src/libsystemd/sd-netlink/sd-netlink.c b/src/libsystemd/sd-netlink/sd-netlink.c index d83952d0cc..f3e267f007 100644 --- a/src/libsystemd/sd-netlink/sd-netlink.c +++ b/src/libsystemd/sd-netlink/sd-netlink.c @@ -28,7 +28,7 @@ static int sd_netlink_new(sd_netlink **ret) { return -ENOMEM; *rtnl = (sd_netlink) { - .n_ref = REFCNT_INIT, + .n_ref = 1, .fd = -1, .sockaddr.nl.nl_family = AF_NETLINK, .original_pid = getpid_cached(), @@ -182,7 +182,7 @@ static sd_netlink *netlink_free(sd_netlink *rtnl) { return mfree(rtnl); } -DEFINE_ATOMIC_REF_UNREF_FUNC(sd_netlink, sd_netlink, netlink_free); +DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_netlink, sd_netlink, netlink_free); static void rtnl_seal_message(sd_netlink *rtnl, sd_netlink_message *m) { assert(rtnl); diff --git a/src/locale/test-keymap-util.c b/src/locale/test-keymap-util.c index 2f82891d60..f96f5b07ec 100644 --- a/src/locale/test-keymap-util.c +++ b/src/locale/test-keymap-util.c @@ -4,6 +4,7 @@ #include "keymap-util.h" #include "log.h" #include "string-util.h" +#include "tests.h" static void test_find_language_fallback(void) { _cleanup_free_ char *ans = NULL, *ans2 = NULL; @@ -189,8 +190,7 @@ static void test_x11_convert_to_vconsole(void) { } int main(int argc, char **argv) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); + test_setup_logging(LOG_DEBUG); test_find_language_fallback(); test_find_converted_keymap(); diff --git a/src/resolve/test-resolved-etc-hosts.c b/src/resolve/test-resolved-etc-hosts.c index dbff889e3e..ca3590066c 100644 --- a/src/resolve/test-resolved-etc-hosts.c +++ b/src/resolve/test-resolved-etc-hosts.c @@ -10,6 +10,7 @@ #include "log.h" #include "resolved-etc-hosts.h" #include "strv.h" +#include "tests.h" #include "tmpfile-util.h" static void test_parse_etc_hosts_system(void) { @@ -137,9 +138,7 @@ static void test_parse_file(const char *fname) { } int main(int argc, char **argv) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); if (argc == 1) { test_parse_etc_hosts_system(); diff --git a/src/resolve/test-resolved-packet.c b/src/resolve/test-resolved-packet.c index 22ae26ce12..c9a511beba 100644 --- a/src/resolve/test-resolved-packet.c +++ b/src/resolve/test-resolved-packet.c @@ -2,6 +2,7 @@ #include "log.h" #include "resolved-dns-packet.h" +#include "tests.h" static void test_dns_packet_new(void) { size_t i; @@ -23,10 +24,7 @@ static void test_dns_packet_new(void) { } int main(int argc, char **argv) { - - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_dns_packet_new(); diff --git a/src/test/test-nss.c b/src/test/test-nss.c index 20aa6cf01f..f1bba2a353 100644 --- a/src/test/test-nss.c +++ b/src/test/test-nss.c @@ -12,11 +12,13 @@ #include "in-addr-util.h" #include "local-addresses.h" #include "log.h" +#include "main-func.h" #include "nss-util.h" #include "path-util.h" #include "stdio-util.h" #include "string-util.h" #include "strv.h" +#include "tests.h" static const char* nss_status_to_string(enum nss_status status, char *buf, size_t buf_len) { switch (status) { @@ -485,7 +487,7 @@ static int parse_argv(int argc, char **argv, return 0; } -int main(int argc, char **argv) { +static int run(int argc, char **argv) { _cleanup_free_ char *dir = NULL; _cleanup_strv_free_ char **modules = NULL, **names = NULL; _cleanup_free_ struct local_address *addresses = NULL; @@ -493,8 +495,7 @@ int main(int argc, char **argv) { char **module; int r; - log_set_max_level(LOG_INFO); - log_parse_environment(); + test_setup_logging(LOG_INFO); r = parse_argv(argc, argv, &modules, &names, &addresses, &n_addresses); if (r < 0) { @@ -504,13 +505,15 @@ int main(int argc, char **argv) { dir = dirname_malloc(argv[0]); if (!dir) - return EXIT_FAILURE; + return log_oom(); STRV_FOREACH(module, modules) { r = test_one_module(dir, *module, names, addresses, n_addresses); if (r < 0) - return EXIT_FAILURE; + return r; } - return EXIT_SUCCESS; + return 0; } + +DEFINE_MAIN_FUNCTION(run);