From 3ffd12bfbe9f91b392d80fbf6a8b2def2a13bf6d Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 12 Oct 2018 11:12:07 +0900 Subject: [PATCH 1/7] test: add tests for $SYSTEMD_MEMPOOL= This adds tests for b4f607433cac749b617e15b3d5d122322ed2bc71 and 205c085bc36c2c61a09dc40621d8561b135d9b57 (#9792). --- src/test/meson.build | 4 +++ src/test/test-set-disable-mempool.c | 53 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/test/test-set-disable-mempool.c diff --git a/src/test/meson.build b/src/test/meson.build index 97cca3269c..daf5e7c76a 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'], [], []], 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; +} From 657ccaac1b898e6cb4865df38d1cf09c50f6c0ac Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 12 Oct 2018 11:43:49 +0900 Subject: [PATCH 2/7] sd-device: drop unnecessary header --- src/libsystemd/sd-device/device-util.h | 2 -- 1 file changed, 2 deletions(-) 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; \ From a6ee01caf3409ba9820e8824b9262fbac31a9f77 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 12 Oct 2018 11:45:20 +0900 Subject: [PATCH 3/7] test: add test for 'thread safety' of sd-device This adds a test for a5d8835c78112206bbf0812dd4cb471f803bfe88. --- .../sd-device/test-sd-device-thread.c | 39 +++++++++++++++++++ src/test/meson.build | 6 +++ 2 files changed, 45 insertions(+) create mode 100644 src/libsystemd/sd-device/test-sd-device-thread.c 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/test/meson.build b/src/test/meson.build index daf5e7c76a..9ce0bdec78 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -904,6 +904,12 @@ tests += [ [['src/libsystemd/sd-device/test-sd-device.c'], [], []], + + [['src/libsystemd/sd-device/test-sd-device-thread.c'], + [libbasic, + libshared_static, + libsystemd], + [threads]], ] if cxx.found() From 5eddbba3a1b2e7d611db0fe6c1b0970fef741b92 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 12 Oct 2018 11:55:03 +0900 Subject: [PATCH 4/7] meson: do not use mempool from libudev.so Follow-up for a5d8835c78112206bbf0812dd4cb471f803bfe88. --- src/udev/meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/udev/meson.build b/src/udev/meson.build index 665cb83fb8..5d1d75e749 100644 --- a/src/udev/meson.build +++ b/src/udev/meson.build @@ -135,7 +135,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', From cb3e926a5da4f73b48f71b541febe646bc1008d7 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 12 Oct 2018 11:56:45 +0900 Subject: [PATCH 5/7] test: add test for 'thread safety' of libudev This adds a test for 715a970548d03fed18dc66c411c8b42ff21029cf. --- .../sd-device/test-udev-device-thread.c | 36 +++++++++++++++++++ src/test/meson.build | 6 ++++ 2 files changed, 42 insertions(+) create mode 100644 src/libsystemd/sd-device/test-udev-device-thread.c 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 9ce0bdec78..34dedb4f96 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -910,6 +910,12 @@ tests += [ libshared_static, libsystemd], [threads]], + + [['src/libsystemd/sd-device/test-udev-device-thread.c'], + [libbasic, + libshared_static, + libudev], + [threads]], ] if cxx.found() From be44b572f3543e589e381f8a0a04ade71c4c2e03 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 12 Oct 2018 11:59:55 +0900 Subject: [PATCH 6/7] meson: fix '-Dstatic-libsystemd=true' or '-Dstatic-libudev=true' Follow-up for a5d8835c78112206bbf0812dd4cb471f803bfe88. --- meson.build | 1 + src/udev/meson.build | 1 + 2 files changed, 2 insertions(+) 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/udev/meson.build b/src/udev/meson.build index 5d1d75e749..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', From eb43e8a76d3943ebf8ece32c334c011257990651 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 12 Oct 2018 17:29:52 +0900 Subject: [PATCH 7/7] test: sort included headers --- src/test/test-nss.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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) {