22c7c2ada9cfc4060aa1c610f0dca73bb3df034d
[m6w6/libmemcached] / test / setup.cpp
1 #include "mem_config.h"
2 #include "test/conf.h"
3 #include "test/lib/env.hpp"
4 #include "test/lib/random.hpp"
5 #include <cstdlib>
6 #include <cstdio>
7 #include <cstring>
8 #include <csignal>
9 #include <iostream>
10 #include <string>
11 #include <unistd.h>
12
13 static void sigchld(int, siginfo_t *si, void *) {
14 switch (si->si_code) {
15 case CLD_EXITED:
16 // expected
17 break;
18 case CLD_KILLED:
19 if (si->si_status == SIGKILL) {
20 // expected
21 break;
22 }
23 [[fallthrough]];
24 case CLD_DUMPED:
25 std::cerr << "Server{pid="
26 << std::to_string(si->si_pid)
27 << "} died: "
28 << strsignal(si->si_status)
29 << std::endl;
30 break;
31 }
32 }
33
34 static inline void setup_signals() {
35 struct sigaction sa;
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 # define LSAN_OPTIONS \
60 "suppressions=" SOURCES_ROOT "/test/LeakSanitizer.suppressions," \
61 ""
62 static inline void setup_asan(char **argv) {
63 const auto set = getenv("ASAN_OPTIONS");
64
65 if (!set || !*set) {
66 SET_ENV_EX(asan, "ASAN_OPTIONS", ASAN_OPTIONS, 0);
67 execvp(argv[0], argv);
68 perror("exec()");
69 }
70 }
71 static inline void setup_lsan(char **argv) {
72 const auto set = getenv("LSAN_OPTIONS");
73
74 if (!set || !*set) {
75 SET_ENV_EX(lsan, "LSAN_OPTIONS", LSAN_OPTIONS, 0);
76 execvp(argv[0], argv);
77 perror("exec()");
78 }
79 }
80 #else
81 # define setup_asan(a) (void) a
82 # define setup_lsan(a) (void) a
83 #endif
84
85 #if LIBMEMCACHED_WITH_SASL_SUPPORT
86 static inline void setup_sasl() {
87 SET_ENV_EX(sasl_pwdb, "MEMCACHED_SASL_PWDB", LIBMEMCACHED_WITH_SASL_PWDB, 0);
88 SET_ENV_EX(sasl_conf, "SASL_CONF_PATH", LIBMEMCACHED_WITH_SASL_CONF, 0);
89 }
90 #else
91 # define setup_sasl()
92 #endif
93
94 static inline void setup_random() {
95 random_setup();
96 }
97
98 int setup(int &, char ***argv) {
99 setup_signals();
100 setup_random();
101 setup_asan(*argv);
102 setup_lsan(*argv);
103 setup_sasl();
104
105 return 0;
106 }