Merge branch 'runtest' into v1.x
[m6w6/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 cout << " - Setting up signals ... ";
35
36 struct sigaction sa;
37 memset(&sa, 0, sizeof(sa));
38 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART | SA_SIGINFO | SA_NODEFER;
39
40 sa.sa_sigaction = sigchld;
41 if (0 > sigaction(SIGCHLD, &sa, nullptr)) {
42 perror("sigaction(CHLD)");
43 } else {
44 cout << "done\n";
45 }
46 }
47
48 #if HAVE_ASAN
49 // this has to be a macro for putenv() to work
50 # define ASAN_OPTIONS \
51 "check_initialization_order=1," \
52 "replace_str=1," \
53 "replace_intrin=1," \
54 "detect_stack_use_after_return=1," \
55 "alloc_dealloc_mismatch=1," \
56 "new_delete_type_mismatch=1," \
57 "detect_odr_violation=2," \
58 "halt_on_error=0," \
59 "verify_asan_link_order=1," \
60 "abort_on_error=0," \
61 ""
62 static inline void setup_asan(char **argv) {
63 const auto set = getenv("ASAN_OPTIONS");
64
65 cout << " - Setting up ASAN ... ";
66
67 if (!set || !*set) {
68 SET_ENV_EX(asan, "ASAN_OPTIONS", ASAN_OPTIONS, 0);
69 cout << "re-exec\n";
70 execvp(argv[0], argv);
71 perror("exec()");
72 }
73 cout << "done\n";
74 }
75 #else
76 # define setup_asan(a) (void) a
77 #endif
78
79 #if LIBMEMCACHED_WITH_SASL_SUPPORT
80 static inline void setup_sasl() {
81 cout << " - Setting up SASL ... ";
82
83 SET_ENV_EX(sasl_pwdb, "MEMCACHED_SASL_PWDB", LIBMEMCACHED_WITH_SASL_PWDB, 0);
84 SET_ENV_EX(sasl_conf, "SASL_CONF_PATH", LIBMEMCACHED_WITH_SASL_CONF, 0);
85
86 cout << "done\n";
87 }
88 #else
89 # define setup_sasl()
90 #endif
91
92 static inline void setup_random() {
93 cout << " - Setting up RNG ... ";
94
95 random_setup();
96
97 cout << "done\n";
98 }
99
100 int setup(int &, char ***argv) {
101 cout << "Starting " << **argv << " (pid=" << getpid() << ") ... \n";
102
103 setup_signals();
104 setup_random();
105 setup_asan(*argv);
106 setup_sasl();
107
108 return 0;
109 }