thread safe randoms
[awesomized/libmemcached] / test / setup.cpp
1 #include "mem_config.h"
2 #include "test/lib/env.hpp"
3 #include "test/lib/random.hpp"
4 #include <cstdlib>
5 #include <cstdio>
6 #include <cstring>
7 #include <csignal>
8 #include <iostream>
9 #include <string>
10 #include <unistd.h>
11
12 static void sigchld(int, siginfo_t *si, void *) {
13 switch (si->si_code) {
14 case CLD_EXITED:
15 // expected
16 break;
17 case CLD_KILLED:
18 if (si->si_status == SIGKILL) {
19 // expected
20 break;
21 }
22 [[fallthrough]];
23 case CLD_DUMPED:
24 std::cerr << "Server{pid="
25 << std::to_string(si->si_pid)
26 << "} died: "
27 << strsignal(si->si_status)
28 << std::endl;
29 break;
30 }
31 }
32
33 static inline void setup_signals() {
34 struct sigaction sa;
35
36 memset(&sa, 0, sizeof(sa));
37 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART | SA_SIGINFO | SA_NODEFER;
38
39 sa.sa_sigaction = sigchld;
40 if (0 > sigaction(SIGCHLD, &sa, nullptr)) {
41 perror("sigaction(CHLD)");
42 }
43 }
44
45 #if HAVE_ASAN
46 // this has to be a macro for putenv() to work
47 # define ASAN_OPTIONS \
48 "check_initialization_order=1," \
49 "replace_str=1," \
50 "replace_intrin=1," \
51 "detect_stack_use_after_return=1," \
52 "alloc_dealloc_mismatch=1," \
53 "new_delete_type_mismatch=1," \
54 "detect_odr_violation=2," \
55 "halt_on_error=0," \
56 "verify_asan_link_order=1," \
57 "abort_on_error=0," \
58 ""
59 static inline void setup_asan(char **argv) {
60 const auto set = getenv("ASAN_OPTIONS");
61
62 if (!set || !*set) {
63 SET_ENV_EX(asan, "ASAN_OPTIONS", ASAN_OPTIONS, 0);
64 execvp(argv[0], argv);
65 perror("exec()");
66 }
67 }
68 #else
69 # define setup_asan(a) (void) a
70 #endif
71
72 #if LIBMEMCACHED_WITH_SASL_SUPPORT
73 static inline void setup_sasl() {
74 SET_ENV_EX(sasl_pwdb, "MEMCACHED_SASL_PWDB", LIBMEMCACHED_WITH_SASL_PWDB, 0);
75 SET_ENV_EX(sasl_conf, "SASL_CONF_PATH", LIBMEMCACHED_WITH_SASL_CONF, 0);
76 }
77 #else
78 # define setup_sasl()
79 #endif
80
81 int setup(int &, char ***argv) {
82 random_setup();
83
84 setup_signals();
85 setup_asan(*argv);
86 setup_sasl();
87
88 return 0;
89 }