fix include
[awesomized/libmemcached] / test / setup.cpp
1 #include "mem_config.h"
2 #include "test/lib/env.hpp"
3 #include <cstdlib>
4 #include <cstdio>
5 #include <cstring>
6 #include <csignal>
7 #include <iostream>
8 #include <string>
9 #include <unistd.h>
10
11 static void sigchld(int, siginfo_t *si, void *) {
12 switch (si->si_code) {
13 case CLD_EXITED:
14 // expected
15 break;
16 case CLD_KILLED:
17 if (si->si_status == SIGKILL) {
18 // expected
19 break;
20 }
21 [[fallthrough]];
22 case CLD_DUMPED:
23 std::cerr << "Server{pid="
24 << std::to_string(si->si_pid)
25 << "} died: "
26 << strsignal(si->si_status)
27 << std::endl;
28 break;
29 }
30 }
31
32 static inline void setup_signals() {
33 struct sigaction sa;
34
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 int setup(int &, char ***argv) {
81 setup_signals();
82 setup_asan(*argv);
83 setup_sasl();
84
85 return 0;
86 }