]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/sdk/test/common/random_fork_test.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / sdk / test / common / random_fork_test.cc
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifdef __unix__
5 // Verifies that IDs don't clash after forking the process.
6 //
7 // See https://github.com/opentracing-contrib/nginx-opentracing/issues/52
8 # include "src/common/random.h"
9
10 # include <sys/mman.h>
11 # include <sys/types.h>
12 # include <sys/wait.h>
13 # include <unistd.h>
14 # include <cstdio>
15 # include <cstdlib>
16 # include <iostream>
17 using opentelemetry::sdk::common::Random;
18
19 static uint64_t *child_id;
20
21 int main()
22 {
23 // Set up shared memory to communicate between parent and child processes.
24 //
25 // See https://stackoverflow.com/a/13274800/4447365
26 child_id = static_cast<uint64_t *>(
27 mmap(nullptr, sizeof(*child_id), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0));
28 *child_id = 0;
29 if (fork() == 0)
30 {
31 *child_id = Random::GenerateRandom64();
32 exit(EXIT_SUCCESS);
33 }
34 else
35 {
36 wait(nullptr);
37 auto parent_id = Random::GenerateRandom64();
38 auto child_id_copy = *child_id;
39 munmap(static_cast<void *>(child_id), sizeof(*child_id));
40 if (parent_id == child_id_copy)
41 {
42 std::cerr << "Child and parent ids are the same value " << parent_id << "\n";
43 return -1;
44 }
45 }
46 return 0;
47 }
48 #else
49 int main()
50 {
51 return 0;
52 }
53 #endif