diff --git a/meson.build b/meson.build index 8766e7b018..39c427cd26 100644 --- a/meson.build +++ b/meson.build @@ -1409,6 +1409,7 @@ install_libsystemd_static = static_library( journal_client_sources, basic_sources, basic_gcrypt_sources, + disable_mempool_c, include_directories : includes, build_by_default : static_libsystemd != 'false', install : static_libsystemd != 'false', diff --git a/src/libsystemd/sd-device/device-util.h b/src/libsystemd/sd-device/device-util.h index 42461c1991..03969d650d 100644 --- a/src/libsystemd/sd-device/device-util.h +++ b/src/libsystemd/sd-device/device-util.h @@ -1,8 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -#include "util.h" - #define FOREACH_DEVICE_PROPERTY(device, key, value) \ for (key = sd_device_get_property_first(device, &(value)); \ key; \ diff --git a/src/libsystemd/sd-device/test-sd-device-thread.c b/src/libsystemd/sd-device/test-sd-device-thread.c new file mode 100644 index 0000000000..9f1c02373f --- /dev/null +++ b/src/libsystemd/sd-device/test-sd-device-thread.c @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include +#include +#include +#include + +#include "sd-device.h" + +#include "device-util.h" +#include "macro.h" + +static void* thread(void *p) { + sd_device **d = p; + + assert_se(!(*d = sd_device_unref(*d))); + + return NULL; +} + +int main(int argc, char *argv[]) { + sd_device *loopback; + pthread_t t; + const char *key, *value; + + assert_se(unsetenv("SYSTEMD_MEMPOOL") == 0); + + assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0); + + FOREACH_DEVICE_PROPERTY(loopback, key, value) + printf("%s=%s\n", key, value); + + assert_se(pthread_create(&t, NULL, thread, &loopback) == 0); + assert_se(pthread_join(t, NULL) == 0); + + assert_se(!loopback); + + return 0; +} diff --git a/src/libsystemd/sd-device/test-udev-device-thread.c b/src/libsystemd/sd-device/test-udev-device-thread.c new file mode 100644 index 0000000000..2fc0f59185 --- /dev/null +++ b/src/libsystemd/sd-device/test-udev-device-thread.c @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include +#include +#include +#include + +#include "libudev.h" + +#include "macro.h" + +static void* thread(void *p) { + struct udev_device **d = p; + + assert_se(!(*d = udev_device_unref(*d))); + + return NULL; +} + +int main(int argc, char *argv[]) { + struct udev_device *loopback; + pthread_t t; + + assert_se(unsetenv("SYSTEMD_MEMPOOL") == 0); + + assert_se(loopback = udev_device_new_from_syspath(NULL, "/sys/class/net/lo")); + + assert_se(udev_device_get_properties_list_entry(loopback)); + + assert_se(pthread_create(&t, NULL, thread, &loopback) == 0); + assert_se(pthread_join(t, NULL) == 0); + + assert_se(!loopback); + + return 0; +} diff --git a/src/test/meson.build b/src/test/meson.build index 97cca3269c..34dedb4f96 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -399,6 +399,10 @@ tests += [ [], []], + [['src/test/test-set-disable-mempool.c'], + [], + [threads]], + [['src/test/test-bitmap.c'], [], []], @@ -900,6 +904,18 @@ tests += [ [['src/libsystemd/sd-device/test-sd-device.c'], [], []], + + [['src/libsystemd/sd-device/test-sd-device-thread.c'], + [libbasic, + libshared_static, + libsystemd], + [threads]], + + [['src/libsystemd/sd-device/test-udev-device-thread.c'], + [libbasic, + libshared_static, + libudev], + [threads]], ] if cxx.found() diff --git a/src/test/test-nss.c b/src/test/test-nss.c index 3c193f8ac2..d9440682d2 100644 --- a/src/test/test-nss.c +++ b/src/test/test-nss.c @@ -1,22 +1,22 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include -#include #include +#include +#include "af-list.h" +#include "alloc-util.h" +#include "errno-list.h" +#include "hexdecoct.h" +#include "hostname-util.h" +#include "in-addr-util.h" +#include "local-addresses.h" #include "log.h" #include "nss-util.h" #include "path-util.h" -#include "string-util.h" -#include "alloc-util.h" -#include "in-addr-util.h" -#include "hexdecoct.h" -#include "af-list.h" #include "stdio-util.h" +#include "string-util.h" #include "strv.h" -#include "errno-list.h" -#include "hostname-util.h" -#include "local-addresses.h" static const char* nss_status_to_string(enum nss_status status, char *buf, size_t buf_len) { switch (status) { diff --git a/src/test/test-set-disable-mempool.c b/src/test/test-set-disable-mempool.c new file mode 100644 index 0000000000..aea83d2679 --- /dev/null +++ b/src/test/test-set-disable-mempool.c @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include + +#include "process-util.h" +#include "set.h" +#include "tests.h" + +#define NUM 100 + +static void* thread(void *p) { + Set **s = p; + + assert_se(s); + assert_se(*s); + + assert_se(!is_main_thread()); + assert_se(set_size(*s) == NUM); + *s = set_free(*s); + + return NULL; +} + +static void test_one(const char *val) { + pthread_t t; + int x[NUM] = {}; + unsigned i; + Set *s; + + log_info("Testing with SYSTEMD_MEMPOOL=%s", val); + assert_se(setenv("SYSTEMD_MEMPOOL", val, true) == 0); + assert_se(is_main_thread()); + + assert_se(s = set_new(NULL)); + for (i = 0; i < NUM; i++) + assert_se(set_put(s, &x[i])); + + assert_se(pthread_create(&t, NULL, thread, &s) == 0); + assert_se(pthread_join(t, NULL) == 0); + + assert_se(!s); +} + +int main(int argc, char *argv[]) { + test_setup_logging(LOG_DEBUG); + + test_one("0"); + /* The value $SYSTEMD_MEMPOOL= is cached. So the following + * test should also succeed. */ + test_one("1"); + + return 0; +} diff --git a/src/udev/meson.build b/src/udev/meson.build index 665cb83fb8..aba50eb6b7 100644 --- a/src/udev/meson.build +++ b/src/udev/meson.build @@ -124,6 +124,7 @@ install_libudev_static = static_library( shared_sources, libsystemd_sources, libudev_sources, + disable_mempool_c, include_directories : includes, build_by_default : static_libudev != 'false', install : static_libudev != 'false', @@ -135,7 +136,7 @@ install_libudev_static = static_library( libudev = shared_library( 'udev', - 'udev.h', # pick a header file at random to work around old meson bug + disable_mempool_c, version : libudev_version, include_directories : includes, link_args : ['-shared',