cfa18e080cceb444a4534834e0aedcbb7ba4468f
[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 struct sigaction sa;
35 memset(&sa, 0, sizeof(sa));
36 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART | SA_SIGINFO | SA_NODEFER;
37
38 sa.sa_sigaction = sigchld;
39 if (0 > sigaction(SIGCHLD, &sa, nullptr)) {
40 perror("sigaction(CHLD)");
41 }
42 }
43
44 #if HAVE_ASAN
45 // this has to be a macro for putenv() to work
46 # define ASAN_OPTIONS \
47 "check_initialization_order=1," \
48 "replace_str=1," \
49 "replace_intrin=1," \
50 "detect_stack_use_after_return=1," \
51 "alloc_dealloc_mismatch=1," \
52 "new_delete_type_mismatch=1," \
53 "detect_odr_violation=2," \
54 "halt_on_error=0," \
55 "verify_asan_link_order=1," \
56 "abort_on_error=0," \
57 ""
58 static inline void setup_asan(char **argv) {
59 const auto set = getenv("ASAN_OPTIONS");
60
61 if (!set || !*set) {
62 SET_ENV_EX(asan, "ASAN_OPTIONS", ASAN_OPTIONS, 0);
63 execvp(argv[0], argv);
64 perror("exec()");
65 }
66 }
67 #else
68 # define setup_asan(a) (void) a
69 #endif
70
71 #if LIBMEMCACHED_WITH_SASL_SUPPORT
72 static inline void setup_sasl() {
73 SET_ENV_EX(sasl_pwdb, "MEMCACHED_SASL_PWDB", LIBMEMCACHED_WITH_SASL_PWDB, 0);
74 SET_ENV_EX(sasl_conf, "SASL_CONF_PATH", LIBMEMCACHED_WITH_SASL_CONF, 0);
75 }
76 #else
77 # define setup_sasl()
78 #endif
79
80 static inline void setup_random() {
81 random_setup();
82 }
83
84 int setup(int &, char ***argv) {
85 setup_signals();
86 setup_random();
87 setup_asan(*argv);
88 setup_sasl();
89
90 return 0;
91 }