diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/jaegertracing/opentelemetry-cpp/sdk/test/common/random_fork_test.cc | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/jaegertracing/opentelemetry-cpp/sdk/test/common/random_fork_test.cc')
-rw-r--r-- | src/jaegertracing/opentelemetry-cpp/sdk/test/common/random_fork_test.cc | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/jaegertracing/opentelemetry-cpp/sdk/test/common/random_fork_test.cc b/src/jaegertracing/opentelemetry-cpp/sdk/test/common/random_fork_test.cc new file mode 100644 index 000000000..2db8b9fcd --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/sdk/test/common/random_fork_test.cc @@ -0,0 +1,53 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifdef __unix__ +// Verifies that IDs don't clash after forking the process. +// +// See https://github.com/opentracing-contrib/nginx-opentracing/issues/52 +# include "src/common/random.h" + +# include <sys/mman.h> +# include <sys/types.h> +# include <sys/wait.h> +# include <unistd.h> +# include <cstdio> +# include <cstdlib> +# include <iostream> +using opentelemetry::sdk::common::Random; + +static uint64_t *child_id; + +int main() +{ + // Set up shared memory to communicate between parent and child processes. + // + // See https://stackoverflow.com/a/13274800/4447365 + child_id = static_cast<uint64_t *>( + mmap(nullptr, sizeof(*child_id), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0)); + *child_id = 0; + if (fork() == 0) + { + *child_id = Random::GenerateRandom64(); + exit(EXIT_SUCCESS); + } + else + { + wait(nullptr); + auto parent_id = Random::GenerateRandom64(); + auto child_id_copy = *child_id; + munmap(static_cast<void *>(child_id), sizeof(*child_id)); + if (parent_id == child_id_copy) + { + std::cerr << "Child and parent ids are the same value " << parent_id << "\n"; + return -1; + } + } + return 0; +} +#else +int main() +{ + return 0; +} +#endif |